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/kernel-probe.h>
17 #include <lttng/userspace-probe.h>
18 #include <lttng/userspace-probe-internal.h>
19 #include <lttng/event-rule/event-rule.h>
20 #include <lttng/event-rule/event-rule-internal.h>
21 #include <lttng/event-rule/kernel-probe.h>
22 #include <lttng/event-rule/kernel-probe-internal.h>
23 #include <lttng/event-rule/syscall.h>
24 #include <lttng/event-rule/syscall-internal.h>
25 #include <lttng/event-rule/tracepoint.h>
26 #include <lttng/event-rule/tracepoint-internal.h>
27 #include <lttng/event-rule/userspace-probe-internal.h>
28 #include <common/common.h>
29 #include <common/defaults.h>
30 #include <common/trace-chunk.h>
31 #include <common/macros.h>
34 #include "trace-kernel.h"
35 #include "lttng-sessiond.h"
36 #include "notification-thread-commands.h"
39 * Find the channel name for the given kernel session.
41 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
42 const char *name
, struct ltt_kernel_session
*session
)
44 struct ltt_kernel_channel
*chan
;
50 * If we receive an empty string for channel name, it means the
51 * default channel name is requested.
54 name
= DEFAULT_CHANNEL_NAME
;
56 DBG("Trying to find channel %s", name
);
58 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
59 if (strcmp(name
, chan
->channel
->name
) == 0) {
60 DBG("Found channel by name %s", name
);
69 * Find the event for the given channel.
71 struct ltt_kernel_event
*trace_kernel_find_event(
72 char *name
, struct ltt_kernel_channel
*channel
,
73 enum lttng_event_type type
,
74 struct lttng_bytecode
*filter
)
76 struct ltt_kernel_event
*ev
;
82 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
83 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
86 if (strcmp(name
, ev
->event
->name
)) {
89 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
92 if (ev
->filter
&& filter
) {
93 if (ev
->filter
->len
!= filter
->len
||
94 memcmp(ev
->filter
->data
, filter
->data
,
103 DBG("Found event %s for channel %s", name
,
104 channel
->channel
->name
);
112 * Find the event name for the given channel.
114 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
115 char *name
, struct ltt_kernel_channel
*channel
,
116 enum lttng_event_type type
)
118 struct ltt_kernel_event
*ev
;
124 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
125 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
128 if (strcmp(name
, ev
->event
->name
)) {
135 DBG("Found event %s for channel %s", name
,
136 channel
->channel
->name
);
144 * Allocate and initialize a kernel session data structure.
146 * Return pointer to structure or NULL.
148 struct ltt_kernel_session
*trace_kernel_create_session(void)
150 struct ltt_kernel_session
*lks
= NULL
;
152 /* Allocate a new ltt kernel session */
153 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
155 PERROR("create kernel session zmalloc");
159 /* Init data structure */
161 lks
->metadata_stream_fd
= -1;
162 lks
->channel_count
= 0;
163 lks
->stream_count_global
= 0;
164 lks
->metadata
= NULL
;
165 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
167 lks
->tracker_pid
= process_attr_tracker_create();
168 if (!lks
->tracker_pid
) {
171 lks
->tracker_vpid
= process_attr_tracker_create();
172 if (!lks
->tracker_vpid
) {
175 lks
->tracker_uid
= process_attr_tracker_create();
176 if (!lks
->tracker_uid
) {
179 lks
->tracker_vuid
= process_attr_tracker_create();
180 if (!lks
->tracker_vuid
) {
183 lks
->tracker_gid
= process_attr_tracker_create();
184 if (!lks
->tracker_gid
) {
187 lks
->tracker_vgid
= process_attr_tracker_create();
188 if (!lks
->tracker_vgid
) {
191 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
192 if (lks
->consumer
== NULL
) {
199 process_attr_tracker_destroy(lks
->tracker_pid
);
200 process_attr_tracker_destroy(lks
->tracker_vpid
);
201 process_attr_tracker_destroy(lks
->tracker_uid
);
202 process_attr_tracker_destroy(lks
->tracker_vuid
);
203 process_attr_tracker_destroy(lks
->tracker_gid
);
204 process_attr_tracker_destroy(lks
->tracker_vgid
);
212 * Allocate and initialize a kernel channel data structure.
214 * Return pointer to structure or NULL.
216 struct ltt_kernel_channel
*trace_kernel_create_channel(
217 struct lttng_channel
*chan
)
219 struct ltt_kernel_channel
*lkc
;
220 struct lttng_channel_extended
*extended
= NULL
;
224 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
226 PERROR("ltt_kernel_channel zmalloc");
230 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
231 if (lkc
->channel
== NULL
) {
232 PERROR("lttng_channel zmalloc");
236 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
238 PERROR("lttng_channel_channel zmalloc");
241 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
242 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
243 lkc
->channel
->attr
.extended
.ptr
= extended
;
247 * If we receive an empty string for channel name, it means the
248 * default channel name is requested.
250 if (chan
->name
[0] == '\0') {
251 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
252 sizeof(lkc
->channel
->name
));
254 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
257 lkc
->stream_count
= 0;
258 lkc
->event_count
= 0;
260 lkc
->published_to_notification_thread
= false;
261 /* Init linked list */
262 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
263 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
264 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
278 * Allocate and init a kernel context object.
280 * Return the allocated object or NULL on error.
282 struct ltt_kernel_context
*trace_kernel_create_context(
283 struct lttng_kernel_context
*ctx
)
285 struct ltt_kernel_context
*kctx
;
287 kctx
= zmalloc(sizeof(*kctx
));
289 PERROR("zmalloc kernel context");
294 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
301 * Allocate and init a kernel context object from an existing kernel context
304 * Return the allocated object or NULL on error.
306 struct ltt_kernel_context
*trace_kernel_copy_context(
307 struct ltt_kernel_context
*kctx
)
309 struct ltt_kernel_context
*kctx_copy
;
312 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
314 PERROR("zmalloc ltt_kernel_context");
318 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
319 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
326 * Allocate and initialize a kernel event. Set name and event type.
327 * We own filter_expression, and filter.
329 * Return pointer to structure or NULL.
331 enum lttng_error_code
trace_kernel_create_event(
332 struct lttng_event
*ev
, char *filter_expression
,
333 struct lttng_bytecode
*filter
,
334 struct ltt_kernel_event
**kernel_event
)
336 enum lttng_error_code ret
;
337 struct lttng_kernel_event
*attr
;
338 struct ltt_kernel_event
*local_kernel_event
;
339 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
343 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
344 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
345 if (local_kernel_event
== NULL
|| attr
== NULL
) {
346 PERROR("kernel event zmalloc");
347 ret
= LTTNG_ERR_NOMEM
;
352 case LTTNG_EVENT_PROBE
:
353 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
354 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
355 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
356 strncpy(attr
->u
.kprobe
.symbol_name
,
357 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
358 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
360 case LTTNG_EVENT_USERSPACE_PROBE
:
362 const struct lttng_userspace_probe_location
* location
= NULL
;
363 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
365 location
= lttng_event_get_userspace_probe_location(ev
);
367 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
372 * From this point on, the specific term 'uprobe' is used
373 * instead of the generic 'userspace probe' because it's the
374 * technology used at the moment for this instrumentation.
375 * LTTng currently implements userspace probes using uprobes.
376 * In the interactions with the kernel tracer, we use the
379 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
381 lookup
= lttng_userspace_probe_location_get_lookup_method(
384 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
389 * From the kernel tracer's perspective, all userspace probe
390 * event types are all the same: a file and an offset.
392 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
393 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
394 /* Get the file descriptor on the target binary. */
396 lttng_userspace_probe_location_function_get_binary_fd(location
);
399 * Save a reference to the probe location used during
400 * the listing of events.
402 userspace_probe_location
=
403 lttng_userspace_probe_location_copy(location
);
405 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
406 /* Get the file descriptor on the target binary. */
408 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
411 * Save a reference to the probe location used during the listing of
414 userspace_probe_location
=
415 lttng_userspace_probe_location_copy(location
);
418 DBG("Unsupported lookup method type");
419 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
424 case LTTNG_EVENT_FUNCTION
:
425 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
426 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
427 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
428 strncpy(attr
->u
.kretprobe
.symbol_name
,
429 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
430 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
432 case LTTNG_EVENT_FUNCTION_ENTRY
:
433 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
434 strncpy(attr
->u
.ftrace
.symbol_name
,
435 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
436 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
438 case LTTNG_EVENT_TRACEPOINT
:
439 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
441 case LTTNG_EVENT_SYSCALL
:
442 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
443 attr
->u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_ALL
;
444 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRYEXIT
;
445 attr
->u
.syscall
.match
= LTTNG_KERNEL_SYSCALL_MATCH_NAME
;
447 case LTTNG_EVENT_ALL
:
448 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
451 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
452 ret
= LTTNG_ERR_INVALID
;
456 /* Copy event name */
457 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
458 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
460 /* Setting up a kernel event */
461 local_kernel_event
->fd
= -1;
462 local_kernel_event
->event
= attr
;
463 local_kernel_event
->enabled
= 1;
464 local_kernel_event
->filter_expression
= filter_expression
;
465 local_kernel_event
->filter
= filter
;
466 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
468 *kernel_event
= local_kernel_event
;
473 free(filter_expression
);
475 free(local_kernel_event
);
481 * Allocate and initialize a kernel token event rule.
483 * Return pointer to structure or NULL.
485 enum lttng_error_code
trace_kernel_create_event_notifier_rule(
486 struct lttng_trigger
*trigger
,
488 uint64_t error_counter_index
,
489 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
491 enum lttng_error_code ret
= LTTNG_OK
;
492 enum lttng_condition_type condition_type
;
493 enum lttng_event_rule_type event_rule_type
;
494 enum lttng_condition_status condition_status
;
495 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
496 const struct lttng_condition
*condition
= NULL
;
497 const struct lttng_event_rule
*event_rule
= NULL
;
499 assert(event_notifier_rule
);
501 condition
= lttng_trigger_get_const_condition(trigger
);
504 condition_type
= lttng_condition_get_type(condition
);
505 assert(condition_type
== LTTNG_CONDITION_TYPE_ON_EVENT
);
507 condition_status
= lttng_condition_on_event_get_rule(
508 condition
, &event_rule
);
509 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
512 event_rule_type
= lttng_event_rule_get_type(event_rule
);
513 assert(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
515 local_kernel_token_event_rule
=
516 zmalloc(sizeof(struct ltt_kernel_event_notifier_rule
));
517 if (local_kernel_token_event_rule
== NULL
) {
518 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
519 ret
= LTTNG_ERR_NOMEM
;
523 local_kernel_token_event_rule
->fd
= -1;
524 local_kernel_token_event_rule
->enabled
= 1;
525 local_kernel_token_event_rule
->token
= token
;
526 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
528 /* Get the reference of the event rule. */
529 lttng_trigger_get(trigger
);
531 local_kernel_token_event_rule
->trigger
= trigger
;
532 /* The event rule still owns the filter and bytecode. */
533 local_kernel_token_event_rule
->filter
=
534 lttng_event_rule_get_filter_bytecode(event_rule
);
536 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
537 local_kernel_token_event_rule
->token
);
539 *event_notifier_rule
= local_kernel_token_event_rule
;
544 * Initialize a kernel trigger from an event rule.
546 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
547 const struct lttng_event_rule
*rule
,
548 struct lttng_kernel_event_notifier
*kernel_event_notifier
)
550 enum lttng_error_code ret_code
;
554 switch (lttng_event_rule_get_type(rule
)) {
555 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
557 uint64_t address
= 0, offset
= 0;
558 const char *symbol_name
= NULL
;
559 const struct lttng_kernel_probe_location
*location
= NULL
;
560 enum lttng_kernel_probe_location_status k_status
;
561 enum lttng_event_rule_status status
;
563 status
= lttng_event_rule_kernel_probe_get_location(rule
, &location
);
564 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
565 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
569 switch (lttng_kernel_probe_location_get_type(location
)) {
570 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
572 k_status
= lttng_kernel_probe_location_address_get_address(
574 assert(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
577 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
579 k_status
= lttng_kernel_probe_location_symbol_get_offset(
581 assert(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
582 symbol_name
= lttng_kernel_probe_location_symbol_get_name(
590 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_KPROBE
;
591 kernel_event_notifier
->event
.u
.kprobe
.addr
= address
;
592 kernel_event_notifier
->event
.u
.kprobe
.offset
= offset
;
594 strncpy_ret
= lttng_strncpy(
595 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
,
596 symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
599 ret_code
= LTTNG_ERR_INVALID
;
604 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
606 status
= lttng_event_rule_kernel_probe_get_event_name(rule
, &name
);
607 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
611 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
613 const struct lttng_userspace_probe_location
* location
= NULL
;
614 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
615 enum lttng_event_rule_status status
;
617 status
= lttng_event_rule_userspace_probe_get_location(rule
, &location
);
618 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
619 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
623 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_UPROBE
;
625 lookup
= lttng_userspace_probe_location_get_lookup_method(
628 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
633 * From the kernel tracer's perspective, all userspace probe
634 * event types are all the same: a file and an offset.
636 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
637 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
638 /* Get the file descriptor on the target binary. */
639 kernel_event_notifier
->event
.u
.uprobe
.fd
=
640 lttng_userspace_probe_location_function_get_binary_fd(location
);
643 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
644 /* Get the file descriptor on the target binary. */
645 kernel_event_notifier
->event
.u
.uprobe
.fd
=
646 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
652 status
= lttng_event_rule_userspace_probe_get_event_name(
654 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
658 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
660 const enum lttng_domain_type domain
=
661 lttng_event_rule_get_domain_type(rule
);
662 const enum lttng_event_rule_status status
=
663 lttng_event_rule_tracepoint_get_pattern(
666 assert(domain
== LTTNG_DOMAIN_KERNEL
);
667 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
668 kernel_event_notifier
->event
.instrumentation
=
669 LTTNG_KERNEL_TRACEPOINT
;
674 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
676 const enum lttng_event_rule_status status
=
677 lttng_event_rule_syscall_get_pattern(
680 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
682 kernel_event_notifier
->event
.instrumentation
=
683 LTTNG_KERNEL_SYSCALL
;
684 kernel_event_notifier
->event
.u
.syscall
.abi
=
685 LTTNG_KERNEL_SYSCALL_ABI_ALL
;
686 kernel_event_notifier
->event
.u
.syscall
.entryexit
=
687 LTTNG_KERNEL_SYSCALL_ENTRY
;
688 kernel_event_notifier
->event
.u
.syscall
.match
=
689 LTTNG_KERNEL_SYSCALL_MATCH_NAME
;
693 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
699 strncpy_ret
= lttng_strncpy(kernel_event_notifier
->event
.name
, name
,
700 LTTNG_KERNEL_SYM_NAME_LEN
);
702 ret_code
= LTTNG_ERR_INVALID
;
710 * Allocate and initialize a kernel metadata.
712 * Return pointer to structure or NULL.
714 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
717 struct ltt_kernel_metadata
*lkm
;
718 struct lttng_channel
*chan
;
720 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
721 chan
= zmalloc(sizeof(struct lttng_channel
));
722 if (lkm
== NULL
|| chan
== NULL
) {
723 PERROR("kernel metadata zmalloc");
728 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
730 ERR("Failed to initialize metadata channel name to `%s`",
731 DEFAULT_METADATA_NAME
);
735 /* Set default attributes */
736 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
737 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
738 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
739 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
740 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
744 * The metadata channel of kernel sessions must use the "mmap"
745 * back-end since the consumer daemon accumulates complete
746 * metadata units before sending them to the relay daemon in
747 * live mode. The consumer daemon also needs to extract the contents
748 * of the metadata cache when computing a rotation position.
750 * In both cases, it is not possible to rely on the splice
751 * back-end as the consumer daemon may need to accumulate more
752 * content than can be backed by the ring buffer's underlying
755 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
756 chan
->attr
.tracefile_size
= 0;
757 chan
->attr
.tracefile_count
= 0;
758 chan
->attr
.live_timer_interval
= 0;
773 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
776 * Return pointer to structure or NULL.
778 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
782 struct ltt_kernel_stream
*lks
;
786 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
788 PERROR("kernel stream zmalloc");
793 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
795 PERROR("snprintf stream name");
798 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
812 * Cleanup kernel stream structure.
814 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
818 DBG("[trace] Closing stream fd %d", stream
->fd
);
819 /* Close kernel fd */
820 if (stream
->fd
>= 0) {
823 ret
= close(stream
->fd
);
828 /* Remove from stream list */
829 cds_list_del(&stream
->list
);
835 * Cleanup kernel event structure.
837 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
841 if (event
->fd
>= 0) {
844 DBG("[trace] Closing event fd %d", event
->fd
);
845 /* Close kernel fd */
846 ret
= close(event
->fd
);
851 DBG("[trace] Tearing down event (no associated file descriptor)");
854 /* Remove from event list */
855 cds_list_del(&event
->list
);
857 free(event
->filter_expression
);
865 * Cleanup kernel event structure.
867 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
869 struct ltt_kernel_event_notifier_rule
*rule
= caa_container_of(rcu_node
,
870 struct ltt_kernel_event_notifier_rule
, rcu_node
);
875 void trace_kernel_destroy_event_notifier_rule(
876 struct ltt_kernel_event_notifier_rule
*event
)
880 if (event
->fd
>= 0) {
881 const int ret
= close(event
->fd
);
883 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
886 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
890 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
893 lttng_trigger_put(event
->trigger
);
894 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
897 * Cleanup kernel context structure.
899 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
904 cds_list_del(&ctx
->list
);
910 * Cleanup kernel channel structure.
912 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
914 struct ltt_kernel_stream
*stream
, *stmp
;
915 struct ltt_kernel_event
*event
, *etmp
;
916 struct ltt_kernel_context
*ctx
, *ctmp
;
918 enum lttng_error_code status
;
922 DBG("[trace] Closing channel fd %d", channel
->fd
);
923 /* Close kernel fd */
924 if (channel
->fd
>= 0) {
925 ret
= close(channel
->fd
);
931 /* For each stream in the channel list */
932 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
933 trace_kernel_destroy_stream(stream
);
936 /* For each event in the channel list */
937 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
938 trace_kernel_destroy_event(event
);
941 /* For each context in the channel list */
942 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
943 trace_kernel_destroy_context(ctx
);
946 /* Remove from channel list */
947 cds_list_del(&channel
->list
);
949 if (the_notification_thread_handle
&&
950 channel
->published_to_notification_thread
) {
951 status
= notification_thread_command_remove_channel(
952 the_notification_thread_handle
, channel
->key
,
953 LTTNG_DOMAIN_KERNEL
);
954 assert(status
== LTTNG_OK
);
956 free(channel
->channel
->attr
.extended
.ptr
);
957 free(channel
->channel
);
962 * Cleanup kernel metadata structure.
964 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
968 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
969 /* Close kernel fd */
970 if (metadata
->fd
>= 0) {
973 ret
= close(metadata
->fd
);
979 free(metadata
->conf
);
984 * Cleanup kernel session structure
986 * Should *NOT* be called with RCU read-side lock held.
988 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
990 struct ltt_kernel_channel
*channel
, *ctmp
;
995 DBG("[trace] Closing session fd %d", session
->fd
);
996 /* Close kernel fds */
997 if (session
->fd
>= 0) {
998 ret
= close(session
->fd
);
1004 if (session
->metadata_stream_fd
>= 0) {
1005 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1006 ret
= close(session
->metadata_stream_fd
);
1012 if (session
->metadata
!= NULL
) {
1013 trace_kernel_destroy_metadata(session
->metadata
);
1016 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
1017 trace_kernel_destroy_channel(channel
);
1021 /* Free elements needed by destroy notifiers. */
1022 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1024 /* Wipe consumer output object */
1025 consumer_output_put(session
->consumer
);
1027 process_attr_tracker_destroy(session
->tracker_pid
);
1028 process_attr_tracker_destroy(session
->tracker_vpid
);
1029 process_attr_tracker_destroy(session
->tracker_uid
);
1030 process_attr_tracker_destroy(session
->tracker_vuid
);
1031 process_attr_tracker_destroy(session
->tracker_gid
);
1032 process_attr_tracker_destroy(session
->tracker_vgid
);