2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include <common/common.h>
25 #include <common/defaults.h>
28 #include "trace-kernel.h"
31 * Find the channel name for the given kernel session.
33 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
34 char *name
, struct ltt_kernel_session
*session
)
36 struct ltt_kernel_channel
*chan
;
42 * If we receive an empty string for channel name, it means the
43 * default channel name is requested.
46 name
= DEFAULT_CHANNEL_NAME
;
48 DBG("Trying to find channel %s", name
);
50 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
51 if (strcmp(name
, chan
->channel
->name
) == 0) {
52 DBG("Found channel by name %s", name
);
61 * Find the event for the given channel.
63 struct ltt_kernel_event
*trace_kernel_find_event(
64 char *name
, struct ltt_kernel_channel
*channel
,
65 enum lttng_event_type type
,
66 struct lttng_filter_bytecode
*filter
)
68 struct ltt_kernel_event
*ev
;
74 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
75 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
78 if (strcmp(name
, ev
->event
->name
)) {
81 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
84 if (ev
->filter
&& filter
) {
85 if (ev
->filter
->len
!= filter
->len
||
86 memcmp(ev
->filter
->data
, filter
->data
,
95 DBG("Found event %s for channel %s", name
,
96 channel
->channel
->name
);
104 * Find the event name for the given channel.
106 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
107 char *name
, struct ltt_kernel_channel
*channel
,
108 enum lttng_event_type type
)
110 struct ltt_kernel_event
*ev
;
116 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
117 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
120 if (strcmp(name
, ev
->event
->name
)) {
127 DBG("Found event %s for channel %s", name
,
128 channel
->channel
->name
);
136 * Allocate and initialize a kernel session data structure.
138 * Return pointer to structure or NULL.
140 struct ltt_kernel_session
*trace_kernel_create_session(void)
142 struct ltt_kernel_session
*lks
= NULL
;
144 /* Allocate a new ltt kernel session */
145 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
147 PERROR("create kernel session zmalloc");
151 /* Init data structure */
153 lks
->metadata_stream_fd
= -1;
154 lks
->channel_count
= 0;
155 lks
->stream_count_global
= 0;
156 lks
->metadata
= NULL
;
157 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
159 lks
->tracker_list_pid
= lttng_tracker_list_create();
160 if (!lks
->tracker_list_pid
) {
163 lks
->tracker_list_vpid
= lttng_tracker_list_create();
164 if (!lks
->tracker_list_vpid
) {
167 lks
->tracker_list_uid
= lttng_tracker_list_create();
168 if (!lks
->tracker_list_uid
) {
171 lks
->tracker_list_vuid
= lttng_tracker_list_create();
172 if (!lks
->tracker_list_vuid
) {
175 lks
->tracker_list_gid
= lttng_tracker_list_create();
176 if (!lks
->tracker_list_gid
) {
179 lks
->tracker_list_vgid
= lttng_tracker_list_create();
180 if (!lks
->tracker_list_vgid
) {
183 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
184 if (lks
->consumer
== NULL
) {
191 lttng_tracker_list_destroy(lks
->tracker_list_pid
);
192 lttng_tracker_list_destroy(lks
->tracker_list_vpid
);
193 lttng_tracker_list_destroy(lks
->tracker_list_uid
);
194 lttng_tracker_list_destroy(lks
->tracker_list_vuid
);
195 lttng_tracker_list_destroy(lks
->tracker_list_gid
);
196 lttng_tracker_list_destroy(lks
->tracker_list_vgid
);
204 * Allocate and initialize a kernel channel data structure.
206 * Return pointer to structure or NULL.
208 struct ltt_kernel_channel
*trace_kernel_create_channel(
209 struct lttng_channel
*chan
)
211 struct ltt_kernel_channel
*lkc
;
215 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
217 PERROR("ltt_kernel_channel zmalloc");
221 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
222 if (lkc
->channel
== NULL
) {
223 PERROR("lttng_channel zmalloc");
227 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
230 * If we receive an empty string for channel name, it means the
231 * default channel name is requested.
233 if (chan
->name
[0] == '\0') {
234 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
235 sizeof(lkc
->channel
->name
));
237 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
240 lkc
->stream_count
= 0;
241 lkc
->event_count
= 0;
243 /* Init linked list */
244 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
245 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
246 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
255 * Allocate and init a kernel context object.
257 * Return the allocated object or NULL on error.
259 struct ltt_kernel_context
*trace_kernel_create_context(
260 struct lttng_kernel_context
*ctx
)
262 struct ltt_kernel_context
*kctx
;
264 kctx
= zmalloc(sizeof(*kctx
));
266 PERROR("zmalloc kernel context");
271 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
278 * Allocate and init a kernel context object from an existing kernel context
281 * Return the allocated object or NULL on error.
283 struct ltt_kernel_context
*trace_kernel_copy_context(
284 struct ltt_kernel_context
*kctx
)
286 struct ltt_kernel_context
*kctx_copy
;
289 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
291 PERROR("zmalloc ltt_kernel_context");
295 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
296 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
303 * Allocate and initialize a kernel event. Set name and event type.
304 * We own filter_expression, and filter.
306 * Return pointer to structure or NULL.
308 struct ltt_kernel_event
*trace_kernel_create_event(struct lttng_event
*ev
,
309 char *filter_expression
, struct lttng_filter_bytecode
*filter
)
311 struct ltt_kernel_event
*lke
;
312 struct lttng_kernel_event
*attr
;
316 lke
= zmalloc(sizeof(struct ltt_kernel_event
));
317 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
318 if (lke
== NULL
|| attr
== NULL
) {
319 PERROR("kernel event zmalloc");
324 case LTTNG_EVENT_PROBE
:
325 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
326 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
327 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
328 strncpy(attr
->u
.kprobe
.symbol_name
,
329 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
330 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
332 case LTTNG_EVENT_FUNCTION
:
333 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
334 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
335 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
336 strncpy(attr
->u
.kretprobe
.symbol_name
,
337 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
338 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
340 case LTTNG_EVENT_FUNCTION_ENTRY
:
341 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
342 strncpy(attr
->u
.ftrace
.symbol_name
,
343 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
344 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
346 case LTTNG_EVENT_TRACEPOINT
:
347 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
349 case LTTNG_EVENT_SYSCALL
:
350 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
352 case LTTNG_EVENT_ALL
:
353 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
356 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
360 /* Copy event name */
361 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
362 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
364 /* Setting up a kernel event */
368 lke
->filter_expression
= filter_expression
;
369 lke
->filter
= filter
;
374 free(filter_expression
);
382 * Allocate and initialize a kernel metadata.
384 * Return pointer to structure or NULL.
386 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
388 struct ltt_kernel_metadata
*lkm
;
389 struct lttng_channel
*chan
;
391 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
392 chan
= zmalloc(sizeof(struct lttng_channel
));
393 if (lkm
== NULL
|| chan
== NULL
) {
394 PERROR("kernel metadata zmalloc");
398 /* Set default attributes */
399 chan
->attr
.overwrite
= DEFAULT_CHANNEL_OVERWRITE
;
400 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
401 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
402 chan
->attr
.switch_timer_interval
= DEFAULT_KERNEL_CHANNEL_SWITCH_TIMER
;
403 chan
->attr
.read_timer_interval
= DEFAULT_KERNEL_CHANNEL_READ_TIMER
;
404 chan
->attr
.output
= DEFAULT_KERNEL_CHANNEL_OUTPUT
;
419 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
422 * Return pointer to structure or NULL.
424 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
428 struct ltt_kernel_stream
*lks
;
432 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
434 PERROR("kernel stream zmalloc");
439 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
441 PERROR("snprintf stream name");
444 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
458 * Cleanup kernel stream structure.
460 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
464 DBG("[trace] Closing stream fd %d", stream
->fd
);
465 /* Close kernel fd */
466 if (stream
->fd
>= 0) {
469 ret
= close(stream
->fd
);
474 /* Remove from stream list */
475 cds_list_del(&stream
->list
);
481 * Cleanup kernel event structure.
483 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
487 if (event
->fd
>= 0) {
490 DBG("[trace] Closing event fd %d", event
->fd
);
491 /* Close kernel fd */
492 ret
= close(event
->fd
);
497 DBG("[trace] Tearing down event (no associated fd)");
500 /* Remove from event list */
501 cds_list_del(&event
->list
);
503 free(event
->filter_expression
);
511 * Cleanup kernel context structure.
513 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
518 cds_list_del(&ctx
->list
);
524 * Cleanup kernel channel structure.
526 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
528 struct ltt_kernel_stream
*stream
, *stmp
;
529 struct ltt_kernel_event
*event
, *etmp
;
530 struct ltt_kernel_context
*ctx
, *ctmp
;
535 DBG("[trace] Closing channel fd %d", channel
->fd
);
536 /* Close kernel fd */
537 if (channel
->fd
>= 0) {
538 ret
= close(channel
->fd
);
544 /* For each stream in the channel list */
545 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
546 trace_kernel_destroy_stream(stream
);
549 /* For each event in the channel list */
550 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
551 trace_kernel_destroy_event(event
);
554 /* For each context in the channel list */
555 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
556 trace_kernel_destroy_context(ctx
);
559 /* Remove from channel list */
560 cds_list_del(&channel
->list
);
562 free(channel
->channel
);
567 * Cleanup kernel metadata structure.
569 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
573 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
574 /* Close kernel fd */
575 if (metadata
->fd
>= 0) {
578 ret
= close(metadata
->fd
);
584 free(metadata
->conf
);
589 * Cleanup kernel session structure
591 * Should *NOT* be called with RCU read-side lock held.
593 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
595 struct ltt_kernel_channel
*channel
, *ctmp
;
600 DBG("[trace] Closing session fd %d", session
->fd
);
601 /* Close kernel fds */
602 if (session
->fd
>= 0) {
603 ret
= close(session
->fd
);
609 if (session
->metadata_stream_fd
>= 0) {
610 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
611 ret
= close(session
->metadata_stream_fd
);
617 if (session
->metadata
!= NULL
) {
618 trace_kernel_destroy_metadata(session
->metadata
);
621 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
622 trace_kernel_destroy_channel(channel
);
625 /* Wipe consumer output object */
626 consumer_output_put(session
->consumer
);
628 lttng_tracker_list_destroy(session
->tracker_list_pid
);
629 lttng_tracker_list_destroy(session
->tracker_list_vpid
);
630 lttng_tracker_list_destroy(session
->tracker_list_uid
);
631 lttng_tracker_list_destroy(session
->tracker_list_vuid
);
632 lttng_tracker_list_destroy(session
->tracker_list_gid
);
633 lttng_tracker_list_destroy(session
->tracker_list_vgid
);