2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
14 #include <lttng/event.h>
15 #include <lttng/lttng-error.h>
16 #include <lttng/userspace-probe.h>
17 #include <lttng/userspace-probe-internal.h>
18 #include <lttng/event-rule/event-rule.h>
19 #include <lttng/event-rule/event-rule-internal.h>
20 #include <lttng/event-rule/kprobe.h>
21 #include <lttng/event-rule/kprobe-internal.h>
22 #include <lttng/event-rule/kretprobe.h>
23 #include <lttng/event-rule/kretprobe-internal.h>
24 #include <lttng/event-rule/syscall.h>
25 #include <lttng/event-rule/syscall-internal.h>
26 #include <lttng/event-rule/tracepoint.h>
27 #include <lttng/event-rule/tracepoint-internal.h>
28 #include <lttng/event-rule/uprobe-internal.h>
29 #include <common/common.h>
30 #include <common/defaults.h>
31 #include <common/trace-chunk.h>
32 #include <common/macros.h>
35 #include "trace-kernel.h"
36 #include "lttng-sessiond.h"
37 #include "notification-thread-commands.h"
40 * Find the channel name for the given kernel session.
42 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
43 const char *name
, struct ltt_kernel_session
*session
)
45 struct ltt_kernel_channel
*chan
;
51 * If we receive an empty string for channel name, it means the
52 * default channel name is requested.
55 name
= DEFAULT_CHANNEL_NAME
;
57 DBG("Trying to find channel %s", name
);
59 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
60 if (strcmp(name
, chan
->channel
->name
) == 0) {
61 DBG("Found channel by name %s", name
);
70 * Find the event for the given channel.
72 struct ltt_kernel_event
*trace_kernel_find_event(
73 char *name
, struct ltt_kernel_channel
*channel
,
74 enum lttng_event_type type
,
75 struct lttng_bytecode
*filter
)
77 struct ltt_kernel_event
*ev
;
83 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
84 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
87 if (strcmp(name
, ev
->event
->name
)) {
90 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
93 if (ev
->filter
&& filter
) {
94 if (ev
->filter
->len
!= filter
->len
||
95 memcmp(ev
->filter
->data
, filter
->data
,
104 DBG("Found event %s for channel %s", name
,
105 channel
->channel
->name
);
113 * Find the event name for the given channel.
115 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
116 char *name
, struct ltt_kernel_channel
*channel
,
117 enum lttng_event_type type
)
119 struct ltt_kernel_event
*ev
;
125 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
126 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
129 if (strcmp(name
, ev
->event
->name
)) {
136 DBG("Found event %s for channel %s", name
,
137 channel
->channel
->name
);
144 struct ltt_kernel_token_event_rule
*trace_kernel_find_trigger_by_token(
145 struct ltt_kernel_token_event_rule_list
*list
,
148 struct ltt_kernel_token_event_rule
*token_event_rule
;
153 cds_list_for_each_entry(token_event_rule
, &list
->head
, list
) {
154 if (token_event_rule
->token
== token
) {
160 DBG("Found token event rule %" PRIu64
, token
);
161 return token_event_rule
;
168 * Allocate and initialize a kernel session data structure.
170 * Return pointer to structure or NULL.
172 struct ltt_kernel_session
*trace_kernel_create_session(void)
174 struct ltt_kernel_session
*lks
= NULL
;
176 /* Allocate a new ltt kernel session */
177 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
179 PERROR("create kernel session zmalloc");
183 /* Init data structure */
185 lks
->metadata_stream_fd
= -1;
186 lks
->channel_count
= 0;
187 lks
->stream_count_global
= 0;
188 lks
->metadata
= NULL
;
189 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
191 lks
->tracker_pid
= process_attr_tracker_create();
192 if (!lks
->tracker_pid
) {
195 lks
->tracker_vpid
= process_attr_tracker_create();
196 if (!lks
->tracker_vpid
) {
199 lks
->tracker_uid
= process_attr_tracker_create();
200 if (!lks
->tracker_uid
) {
203 lks
->tracker_vuid
= process_attr_tracker_create();
204 if (!lks
->tracker_vuid
) {
207 lks
->tracker_gid
= process_attr_tracker_create();
208 if (!lks
->tracker_gid
) {
211 lks
->tracker_vgid
= process_attr_tracker_create();
212 if (!lks
->tracker_vgid
) {
215 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
216 if (lks
->consumer
== NULL
) {
223 process_attr_tracker_destroy(lks
->tracker_pid
);
224 process_attr_tracker_destroy(lks
->tracker_vpid
);
225 process_attr_tracker_destroy(lks
->tracker_uid
);
226 process_attr_tracker_destroy(lks
->tracker_vuid
);
227 process_attr_tracker_destroy(lks
->tracker_gid
);
228 process_attr_tracker_destroy(lks
->tracker_vgid
);
236 * Allocate and initialize a kernel channel data structure.
238 * Return pointer to structure or NULL.
240 struct ltt_kernel_channel
*trace_kernel_create_channel(
241 struct lttng_channel
*chan
)
243 struct ltt_kernel_channel
*lkc
;
244 struct lttng_channel_extended
*extended
= NULL
;
248 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
250 PERROR("ltt_kernel_channel zmalloc");
254 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
255 if (lkc
->channel
== NULL
) {
256 PERROR("lttng_channel zmalloc");
260 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
262 PERROR("lttng_channel_channel zmalloc");
265 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
266 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
267 lkc
->channel
->attr
.extended
.ptr
= extended
;
271 * If we receive an empty string for channel name, it means the
272 * default channel name is requested.
274 if (chan
->name
[0] == '\0') {
275 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
276 sizeof(lkc
->channel
->name
));
278 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
281 lkc
->stream_count
= 0;
282 lkc
->event_count
= 0;
284 lkc
->published_to_notification_thread
= false;
285 /* Init linked list */
286 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
287 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
288 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
302 * Allocate and init a kernel context object.
304 * Return the allocated object or NULL on error.
306 struct ltt_kernel_context
*trace_kernel_create_context(
307 struct lttng_kernel_context
*ctx
)
309 struct ltt_kernel_context
*kctx
;
311 kctx
= zmalloc(sizeof(*kctx
));
313 PERROR("zmalloc kernel context");
318 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
325 * Allocate and init a kernel context object from an existing kernel context
328 * Return the allocated object or NULL on error.
330 struct ltt_kernel_context
*trace_kernel_copy_context(
331 struct ltt_kernel_context
*kctx
)
333 struct ltt_kernel_context
*kctx_copy
;
336 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
338 PERROR("zmalloc ltt_kernel_context");
342 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
343 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
350 * Allocate and initialize a kernel event. Set name and event type.
351 * We own filter_expression, and filter.
353 * Return pointer to structure or NULL.
355 enum lttng_error_code
trace_kernel_create_event(
356 struct lttng_event
*ev
, char *filter_expression
,
357 struct lttng_bytecode
*filter
,
358 struct ltt_kernel_event
**kernel_event
)
360 enum lttng_error_code ret
;
361 struct lttng_kernel_event
*attr
;
362 struct ltt_kernel_event
*local_kernel_event
;
363 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
367 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
368 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
369 if (local_kernel_event
== NULL
|| attr
== NULL
) {
370 PERROR("kernel event zmalloc");
371 ret
= LTTNG_ERR_NOMEM
;
376 case LTTNG_EVENT_PROBE
:
377 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
378 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
379 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
380 strncpy(attr
->u
.kprobe
.symbol_name
,
381 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
382 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
384 case LTTNG_EVENT_USERSPACE_PROBE
:
386 const struct lttng_userspace_probe_location
* location
= NULL
;
387 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
389 location
= lttng_event_get_userspace_probe_location(ev
);
391 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
396 * From this point on, the specific term 'uprobe' is used
397 * instead of the generic 'userspace probe' because it's the
398 * technology used at the moment for this instrumentation.
399 * LTTng currently implements userspace probes using uprobes.
400 * In the interactions with the kernel tracer, we use the
403 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
406 * Only the elf lookup method is supported at the moment.
408 lookup
= lttng_userspace_probe_location_get_lookup_method(
411 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
416 * From the kernel tracer's perspective, all userspace probe
417 * event types are all the same: a file and an offset.
419 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
420 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
421 /* Get the file descriptor on the target binary. */
423 lttng_userspace_probe_location_function_get_binary_fd(location
);
426 * Save a reference to the probe location used during
427 * the listing of events. Close its FD since it won't
428 * be needed for listing.
430 userspace_probe_location
=
431 lttng_userspace_probe_location_copy(location
);
432 ret
= lttng_userspace_probe_location_function_set_binary_fd(
433 userspace_probe_location
, -1);
438 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
439 /* Get the file descriptor on the target binary. */
441 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
444 * Save a reference to the probe location used during the listing of
445 * events. Close its FD since it won't be needed for listing.
447 userspace_probe_location
=
448 lttng_userspace_probe_location_copy(location
);
449 ret
= lttng_userspace_probe_location_tracepoint_set_binary_fd(
450 userspace_probe_location
, -1);
456 DBG("Unsupported lookup method type");
457 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
462 case LTTNG_EVENT_FUNCTION
:
463 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
464 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
465 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
466 strncpy(attr
->u
.kretprobe
.symbol_name
,
467 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
468 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
470 case LTTNG_EVENT_FUNCTION_ENTRY
:
471 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
472 strncpy(attr
->u
.ftrace
.symbol_name
,
473 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
474 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
476 case LTTNG_EVENT_TRACEPOINT
:
477 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
479 case LTTNG_EVENT_SYSCALL
:
480 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
482 case LTTNG_EVENT_ALL
:
483 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
486 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
487 ret
= LTTNG_ERR_INVALID
;
491 /* Copy event name */
492 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
493 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
495 /* Setting up a kernel event */
496 local_kernel_event
->fd
= -1;
497 local_kernel_event
->event
= attr
;
498 local_kernel_event
->enabled
= 1;
499 local_kernel_event
->filter_expression
= filter_expression
;
500 local_kernel_event
->filter
= filter
;
501 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
503 *kernel_event
= local_kernel_event
;
508 free(filter_expression
);
510 free(local_kernel_event
);
516 * Allocate and initialize a kernel token event rule.
518 * Return pointer to structure or NULL.
520 enum lttng_error_code
trace_kernel_create_token_event_rule(
521 struct lttng_event_rule
*event_rule
,
523 struct ltt_kernel_token_event_rule
**kernel_token_event_rule
)
525 enum lttng_error_code ret
= LTTNG_OK
;
526 struct ltt_kernel_token_event_rule
*local_kernel_token_event_rule
;
528 assert(kernel_token_event_rule
);
530 local_kernel_token_event_rule
= zmalloc(sizeof(struct ltt_kernel_token_event_rule
));
531 if (local_kernel_token_event_rule
== NULL
) {
532 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
533 ret
= LTTNG_ERR_NOMEM
;
537 local_kernel_token_event_rule
->fd
= -1;
538 local_kernel_token_event_rule
->enabled
= 1;
539 local_kernel_token_event_rule
->token
= token
;
541 /* Get the reference of the event rule */
542 if (!lttng_event_rule_get(event_rule
)) {
546 local_kernel_token_event_rule
->event_rule
= event_rule
;
547 /* The event rule still own the filter and bytecode */
548 local_kernel_token_event_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
550 DBG3("[trace] Kernel token event rule %" PRIu64
" allocated", local_kernel_token_event_rule
->token
);
552 *kernel_token_event_rule
= local_kernel_token_event_rule
;
558 * Initialize a kernel trigger from an event rule.
560 enum lttng_error_code
trace_kernel_init_trigger_from_event_rule(const struct lttng_event_rule
*rule
,
561 struct lttng_kernel_trigger
*kernel_trigger
)
563 enum lttng_error_code ret
;
564 enum lttng_event_rule_status status
;
565 const char *name
= NULL
;
567 /* TODO: do this for now but have disucssion on if this could be the
568 * responsability of the event_rule itself ala
569 * "lttng_even_rule_generate_kernel_trigger"
571 switch (lttng_event_rule_get_type(rule
)) {
572 case LTTNG_EVENT_RULE_TYPE_KPROBE
:
573 kernel_trigger
->instrumentation
= LTTNG_KERNEL_KPROBE
;
574 kernel_trigger
->u
.kprobe
.addr
= lttng_event_rule_kprobe_get_address(rule
);
575 kernel_trigger
->u
.kprobe
.offset
= lttng_event_rule_kprobe_get_offset(rule
);
576 strncpy(kernel_trigger
->u
.kprobe
.symbol_name
,
577 lttng_event_rule_kprobe_get_symbol_name(rule
), LTTNG_KERNEL_SYM_NAME_LEN
);
578 kernel_trigger
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
579 (void) lttng_event_rule_kprobe_get_name(rule
, &name
);
582 case LTTNG_EVENT_RULE_TYPE_UPROBE
:
584 const struct lttng_userspace_probe_location
* location
= NULL
;
585 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
587 status
= lttng_event_rule_uprobe_get_location(rule
, &location
);
588 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
589 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
593 kernel_trigger
->instrumentation
= LTTNG_KERNEL_UPROBE
;
596 * Only the elf lookup method is supported at the moment.
598 lookup
= lttng_userspace_probe_location_get_lookup_method(
601 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
606 * From the kernel tracer's perspective, all userspace probe
607 * event types are all the same: a file and an offset.
609 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
610 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
611 /* Get the file descriptor on the target binary. */
612 kernel_trigger
->u
.uprobe
.fd
=
613 lttng_userspace_probe_location_function_get_binary_fd(location
);
616 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
617 /* Get the file descriptor on the target binary. */
618 kernel_trigger
->u
.uprobe
.fd
=
619 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
622 DBG("Unsupported lookup method type");
623 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
627 (void) lttng_event_rule_uprobe_get_name(rule
, &name
);
632 case LTTNG_EVENT_RULE_TYPE_KRETPROBE
:
633 assert("Not supported" && 0);
634 kernel_trigger
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
635 kernel_trigger
->u
.kretprobe
.addr
= lttng_event_rule_kretprobe_get_address(rule
);
636 kernel_trigger
->u
.kretprobe
.offset
= lttng_event_rule_kretprobe_get_offset(rule
);
637 strncpy(kernel_trigger
->u
.kretprobe
.symbol_name
,
638 lttng_event_rule_kretprobe_get_symbol_name(rule
), LTTNG_KERNEL_SYM_NAME_LEN
);
639 kernel_trigger
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
640 (void) lttng_event_rule_kretprobe_get_name(rule
, &name
);
643 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
644 /* TODO: assert his is a kernel domain event-rule */
645 kernel_trigger
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
646 (void) lttng_event_rule_tracepoint_get_pattern(rule
, &name
);
649 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
650 kernel_trigger
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
651 (void) lttng_event_rule_syscall_get_pattern(rule
, &name
);
655 ERR("Unknown kernel event rule instrumentation type (%d)", lttng_event_rule_get_type(rule
));
656 ret
= LTTNG_ERR_INVALID
;
661 * WTF is LTTNG_EVENT_ALL??? and LTTNG_EVENT_FUNTION_ENTRY?????
664 /* Copy event name */
665 strncpy(kernel_trigger
->name
, name
, LTTNG_KERNEL_SYM_NAME_LEN
);
666 kernel_trigger
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
672 * Allocate and initialize a kernel metadata.
674 * Return pointer to structure or NULL.
676 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
679 struct ltt_kernel_metadata
*lkm
;
680 struct lttng_channel
*chan
;
682 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
683 chan
= zmalloc(sizeof(struct lttng_channel
));
684 if (lkm
== NULL
|| chan
== NULL
) {
685 PERROR("kernel metadata zmalloc");
690 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
692 ERR("Failed to initialize metadata channel name to `%s`",
693 DEFAULT_METADATA_NAME
);
697 /* Set default attributes */
698 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
699 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
700 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
701 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
702 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
706 * The metadata channel of kernel sessions must use the "mmap"
707 * back-end since the consumer daemon accumulates complete
708 * metadata units before sending them to the relay daemon in
709 * live mode. The consumer daemon also needs to extract the contents
710 * of the metadata cache when computing a rotation position.
712 * In both cases, it is not possible to rely on the splice
713 * back-end as the consumer daemon may need to accumulate more
714 * content than can be backed by the ring buffer's underlying
717 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
718 chan
->attr
.tracefile_size
= 0;
719 chan
->attr
.tracefile_count
= 0;
720 chan
->attr
.live_timer_interval
= 0;
735 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
738 * Return pointer to structure or NULL.
740 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
744 struct ltt_kernel_stream
*lks
;
748 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
750 PERROR("kernel stream zmalloc");
755 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
757 PERROR("snprintf stream name");
760 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
774 * Cleanup kernel stream structure.
776 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
780 DBG("[trace] Closing stream fd %d", stream
->fd
);
781 /* Close kernel fd */
782 if (stream
->fd
>= 0) {
785 ret
= close(stream
->fd
);
790 /* Remove from stream list */
791 cds_list_del(&stream
->list
);
797 * Cleanup kernel event structure.
799 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
803 if (event
->fd
>= 0) {
806 DBG("[trace] Closing event fd %d", event
->fd
);
807 /* Close kernel fd */
808 ret
= close(event
->fd
);
813 DBG("[trace] Tearing down event (no associated fd)");
816 /* Remove from event list */
817 cds_list_del(&event
->list
);
819 free(event
->filter_expression
);
827 * Cleanup kernel event structure.
829 void trace_kernel_destroy_token_event_rule(struct ltt_kernel_token_event_rule
*event
)
831 /* TODO: review in depth to ensure adequate disposing */
834 /* Remove from event list */
835 cds_list_del(&event
->list
);
837 if (event
->fd
>= 0) {
840 DBG("[trace] Closing ,token event rule fd %d", event
->fd
);
841 /* Close kernel fd */
842 ret
= close(event
->fd
);
847 DBG("[trace] Tearing down token event rule (no associated fd)");
850 lttng_event_rule_put(event
->event_rule
);
854 * Cleanup kernel context structure.
856 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
861 cds_list_del(&ctx
->list
);
867 * Cleanup kernel channel structure.
869 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
871 struct ltt_kernel_stream
*stream
, *stmp
;
872 struct ltt_kernel_event
*event
, *etmp
;
873 struct ltt_kernel_context
*ctx
, *ctmp
;
875 enum lttng_error_code status
;
879 DBG("[trace] Closing channel fd %d", channel
->fd
);
880 /* Close kernel fd */
881 if (channel
->fd
>= 0) {
882 ret
= close(channel
->fd
);
888 /* For each stream in the channel list */
889 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
890 trace_kernel_destroy_stream(stream
);
893 /* For each event in the channel list */
894 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
895 trace_kernel_destroy_event(event
);
898 /* For each context in the channel list */
899 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
900 trace_kernel_destroy_context(ctx
);
903 /* Remove from channel list */
904 cds_list_del(&channel
->list
);
906 if (notification_thread_handle
907 && channel
->published_to_notification_thread
) {
908 status
= notification_thread_command_remove_channel(
909 notification_thread_handle
,
910 channel
->key
, LTTNG_DOMAIN_KERNEL
);
911 assert(status
== LTTNG_OK
);
913 free(channel
->channel
->attr
.extended
.ptr
);
914 free(channel
->channel
);
919 * Cleanup kernel metadata structure.
921 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
925 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
926 /* Close kernel fd */
927 if (metadata
->fd
>= 0) {
930 ret
= close(metadata
->fd
);
936 free(metadata
->conf
);
941 * Cleanup kernel session structure
943 * Should *NOT* be called with RCU read-side lock held.
945 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
947 struct ltt_kernel_channel
*channel
, *ctmp
;
952 DBG("[trace] Closing session fd %d", session
->fd
);
953 /* Close kernel fds */
954 if (session
->fd
>= 0) {
955 ret
= close(session
->fd
);
961 if (session
->metadata_stream_fd
>= 0) {
962 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
963 ret
= close(session
->metadata_stream_fd
);
969 if (session
->metadata
!= NULL
) {
970 trace_kernel_destroy_metadata(session
->metadata
);
973 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
974 trace_kernel_destroy_channel(channel
);
978 /* Free elements needed by destroy notifiers. */
979 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
981 /* Wipe consumer output object */
982 consumer_output_put(session
->consumer
);
984 process_attr_tracker_destroy(session
->tracker_pid
);
985 process_attr_tracker_destroy(session
->tracker_vpid
);
986 process_attr_tracker_destroy(session
->tracker_uid
);
987 process_attr_tracker_destroy(session
->tracker_vuid
);
988 process_attr_tracker_destroy(session
->tracker_gid
);
989 process_attr_tracker_destroy(session
->tracker_vgid
);