2 * Copyright (C) 2011 EfficiOS Inc.
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.hpp>
19 #include <lttng/event-rule/event-rule.h>
20 #include <lttng/event-rule/event-rule-internal.hpp>
21 #include <lttng/event-rule/kernel-kprobe.h>
22 #include <lttng/event-rule/kernel-kprobe-internal.hpp>
23 #include <lttng/event-rule/kernel-syscall.h>
24 #include <lttng/event-rule/kernel-syscall-internal.hpp>
25 #include <lttng/event-rule/kernel-tracepoint.h>
26 #include <lttng/event-rule/kernel-tracepoint-internal.hpp>
27 #include <lttng/event-rule/kernel-uprobe.h>
28 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
29 #include <common/common.hpp>
30 #include <common/defaults.hpp>
31 #include <common/trace-chunk.hpp>
32 #include <common/macros.hpp>
34 #include "consumer.hpp"
35 #include "trace-kernel.hpp"
36 #include "lttng-sessiond.hpp"
37 #include "notification-thread-commands.hpp"
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
;
47 LTTNG_ASSERT(session
);
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
;
81 LTTNG_ASSERT(channel
);
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
;
123 LTTNG_ASSERT(channel
);
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
);
145 * Allocate and initialize a kernel session data structure.
147 * Return pointer to structure or NULL.
149 struct ltt_kernel_session
*trace_kernel_create_session(void)
151 struct ltt_kernel_session
*lks
= NULL
;
153 /* Allocate a new ltt kernel session */
154 lks
= new ltt_kernel_session
;
156 PERROR("create kernel session zmalloc");
162 /* Init data structure */
163 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
165 lks
->tracker_pid
= process_attr_tracker_create();
166 if (!lks
->tracker_pid
) {
169 lks
->tracker_vpid
= process_attr_tracker_create();
170 if (!lks
->tracker_vpid
) {
173 lks
->tracker_uid
= process_attr_tracker_create();
174 if (!lks
->tracker_uid
) {
177 lks
->tracker_vuid
= process_attr_tracker_create();
178 if (!lks
->tracker_vuid
) {
181 lks
->tracker_gid
= process_attr_tracker_create();
182 if (!lks
->tracker_gid
) {
185 lks
->tracker_vgid
= process_attr_tracker_create();
186 if (!lks
->tracker_vgid
) {
189 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
190 if (lks
->consumer
== NULL
) {
197 process_attr_tracker_destroy(lks
->tracker_pid
);
198 process_attr_tracker_destroy(lks
->tracker_vpid
);
199 process_attr_tracker_destroy(lks
->tracker_uid
);
200 process_attr_tracker_destroy(lks
->tracker_vuid
);
201 process_attr_tracker_destroy(lks
->tracker_gid
);
202 process_attr_tracker_destroy(lks
->tracker_vgid
);
210 * Allocate and initialize a kernel channel data structure.
212 * Return pointer to structure or NULL.
214 struct ltt_kernel_channel
*trace_kernel_create_channel(
215 struct lttng_channel
*chan
)
217 struct ltt_kernel_channel
*lkc
;
218 struct lttng_channel_extended
*extended
= NULL
;
222 lkc
= zmalloc
<ltt_kernel_channel
>();
224 PERROR("ltt_kernel_channel zmalloc");
228 lkc
->channel
= zmalloc
<lttng_channel
>();
229 if (lkc
->channel
== NULL
) {
230 PERROR("lttng_channel zmalloc");
234 extended
= zmalloc
<lttng_channel_extended
>();
236 PERROR("lttng_channel_channel zmalloc");
239 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
240 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
241 lkc
->channel
->attr
.extended
.ptr
= extended
;
245 * If we receive an empty string for channel name, it means the
246 * default channel name is requested.
248 if (chan
->name
[0] == '\0') {
249 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
250 sizeof(lkc
->channel
->name
));
252 lkc
->channel
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
255 lkc
->stream_count
= 0;
256 lkc
->event_count
= 0;
258 lkc
->published_to_notification_thread
= false;
259 /* Init linked list */
260 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
261 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
262 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
276 * Allocate and init a kernel context object.
278 * Return the allocated object or NULL on error.
280 struct ltt_kernel_context
*trace_kernel_create_context(
281 struct lttng_kernel_abi_context
*ctx
)
283 struct ltt_kernel_context
*kctx
;
285 kctx
= zmalloc
<ltt_kernel_context
>();
287 PERROR("zmalloc kernel context");
292 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
299 * Allocate and init a kernel context object from an existing kernel context
302 * Return the allocated object or NULL on error.
304 struct ltt_kernel_context
*trace_kernel_copy_context(
305 struct ltt_kernel_context
*kctx
)
307 struct ltt_kernel_context
*kctx_copy
;
310 kctx_copy
= zmalloc
<ltt_kernel_context
>();
312 PERROR("zmalloc ltt_kernel_context");
316 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
317 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
324 * Allocate and initialize a kernel event. Set name and event type.
325 * We own filter_expression, and filter.
327 * Return pointer to structure or NULL.
329 enum lttng_error_code
trace_kernel_create_event(
330 struct lttng_event
*ev
, char *filter_expression
,
331 struct lttng_bytecode
*filter
,
332 struct ltt_kernel_event
**kernel_event
)
334 enum lttng_error_code ret
;
335 struct lttng_kernel_abi_event
*attr
;
336 struct ltt_kernel_event
*local_kernel_event
;
337 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
341 local_kernel_event
= zmalloc
<ltt_kernel_event
>();
342 attr
= zmalloc
<lttng_kernel_abi_event
>();
343 if (local_kernel_event
== NULL
|| attr
== NULL
) {
344 PERROR("kernel event zmalloc");
345 ret
= LTTNG_ERR_NOMEM
;
350 case LTTNG_EVENT_PROBE
:
351 attr
->instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
352 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
353 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
354 strncpy(attr
->u
.kprobe
.symbol_name
,
355 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
356 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
358 case LTTNG_EVENT_USERSPACE_PROBE
:
360 const struct lttng_userspace_probe_location
* location
= NULL
;
361 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
363 location
= lttng_event_get_userspace_probe_location(ev
);
365 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
370 * From this point on, the specific term 'uprobe' is used
371 * instead of the generic 'userspace probe' because it's the
372 * technology used at the moment for this instrumentation.
373 * LTTng currently implements userspace probes using uprobes.
374 * In the interactions with the kernel tracer, we use the
377 attr
->instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
379 lookup
= lttng_userspace_probe_location_get_lookup_method(
382 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
387 * From the kernel tracer's perspective, all userspace probe
388 * event types are all the same: a file and an offset.
390 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
391 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
392 /* Get the file descriptor on the target binary. */
394 lttng_userspace_probe_location_function_get_binary_fd(location
);
397 * Save a reference to the probe location used during
398 * the listing of events.
400 userspace_probe_location
=
401 lttng_userspace_probe_location_copy(location
);
403 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
404 /* Get the file descriptor on the target binary. */
406 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
409 * Save a reference to the probe location used during the listing of
412 userspace_probe_location
=
413 lttng_userspace_probe_location_copy(location
);
416 DBG("Unsupported lookup method type");
417 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
422 case LTTNG_EVENT_FUNCTION
:
423 attr
->instrumentation
= LTTNG_KERNEL_ABI_KRETPROBE
;
424 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
425 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
426 strncpy(attr
->u
.kretprobe
.symbol_name
,
427 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
428 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
430 case LTTNG_EVENT_FUNCTION_ENTRY
:
431 attr
->instrumentation
= LTTNG_KERNEL_ABI_FUNCTION
;
432 strncpy(attr
->u
.ftrace
.symbol_name
,
433 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
434 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
436 case LTTNG_EVENT_TRACEPOINT
:
437 attr
->instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
439 case LTTNG_EVENT_SYSCALL
:
440 attr
->instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
441 attr
->u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
442 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
443 attr
->u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
445 case LTTNG_EVENT_ALL
:
446 attr
->instrumentation
= LTTNG_KERNEL_ABI_ALL
;
449 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
450 ret
= LTTNG_ERR_INVALID
;
454 /* Copy event name */
455 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
456 attr
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
458 /* Setting up a kernel event */
459 local_kernel_event
->fd
= -1;
460 local_kernel_event
->event
= attr
;
461 local_kernel_event
->enabled
= 1;
462 local_kernel_event
->filter_expression
= filter_expression
;
463 local_kernel_event
->filter
= filter
;
464 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
466 *kernel_event
= local_kernel_event
;
471 free(filter_expression
);
473 free(local_kernel_event
);
479 * Allocate and initialize a kernel token event rule.
481 * Return pointer to structure or NULL.
483 enum lttng_error_code
trace_kernel_create_event_notifier_rule(
484 struct lttng_trigger
*trigger
,
486 uint64_t error_counter_index
,
487 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
489 enum lttng_error_code ret
= LTTNG_OK
;
490 enum lttng_condition_type condition_type
;
491 enum lttng_event_rule_type event_rule_type
;
492 enum lttng_condition_status condition_status
;
493 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
494 const struct lttng_condition
*condition
= NULL
;
495 const struct lttng_event_rule
*event_rule
= NULL
;
497 LTTNG_ASSERT(event_notifier_rule
);
499 condition
= lttng_trigger_get_const_condition(trigger
);
500 LTTNG_ASSERT(condition
);
502 condition_type
= lttng_condition_get_type(condition
);
503 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
505 condition_status
= lttng_condition_event_rule_matches_get_rule(
506 condition
, &event_rule
);
507 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
508 LTTNG_ASSERT(event_rule
);
510 event_rule_type
= lttng_event_rule_get_type(event_rule
);
511 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
513 local_kernel_token_event_rule
=
514 zmalloc
<ltt_kernel_event_notifier_rule
>();
515 if (local_kernel_token_event_rule
== NULL
) {
516 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
517 ret
= LTTNG_ERR_NOMEM
;
521 local_kernel_token_event_rule
->fd
= -1;
522 local_kernel_token_event_rule
->enabled
= 1;
523 local_kernel_token_event_rule
->token
= token
;
524 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
526 /* Get the reference of the event rule. */
527 lttng_trigger_get(trigger
);
529 local_kernel_token_event_rule
->trigger
= trigger
;
530 /* The event rule still owns the filter and bytecode. */
531 local_kernel_token_event_rule
->filter
=
532 lttng_event_rule_get_filter_bytecode(event_rule
);
534 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
535 local_kernel_token_event_rule
->token
);
537 *event_notifier_rule
= local_kernel_token_event_rule
;
542 * Initialize a kernel trigger from an event rule.
544 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
545 const struct lttng_event_rule
*rule
,
546 struct lttng_kernel_abi_event_notifier
*kernel_event_notifier
)
548 enum lttng_error_code ret_code
;
552 switch (lttng_event_rule_get_type(rule
)) {
553 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
555 uint64_t address
= 0, offset
= 0;
556 const char *symbol_name
= NULL
;
557 const struct lttng_kernel_probe_location
*location
= NULL
;
558 enum lttng_kernel_probe_location_status k_status
;
559 enum lttng_event_rule_status status
;
561 status
= lttng_event_rule_kernel_kprobe_get_location(rule
, &location
);
562 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
563 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
567 switch (lttng_kernel_probe_location_get_type(location
)) {
568 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
570 k_status
= lttng_kernel_probe_location_address_get_address(
572 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
575 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
577 k_status
= lttng_kernel_probe_location_symbol_get_offset(
579 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
580 symbol_name
= lttng_kernel_probe_location_symbol_get_name(
588 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
589 kernel_event_notifier
->event
.u
.kprobe
.addr
= address
;
590 kernel_event_notifier
->event
.u
.kprobe
.offset
= offset
;
592 strncpy_ret
= lttng_strncpy(
593 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
,
595 sizeof(kernel_event_notifier
->event
.u
.kprobe
.symbol_name
));
598 ret_code
= LTTNG_ERR_INVALID
;
603 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
605 status
= lttng_event_rule_kernel_kprobe_get_event_name(rule
, &name
);
606 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
610 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
612 const struct lttng_userspace_probe_location
* location
= NULL
;
613 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
614 enum lttng_event_rule_status status
;
616 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
617 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
618 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
622 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
624 lookup
= lttng_userspace_probe_location_get_lookup_method(
627 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
632 * From the kernel tracer's perspective, all userspace probe
633 * event types are all the same: a file and an offset.
635 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
636 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
637 /* Get the file descriptor on the target binary. */
638 kernel_event_notifier
->event
.u
.uprobe
.fd
=
639 lttng_userspace_probe_location_function_get_binary_fd(location
);
642 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
643 /* Get the file descriptor on the target binary. */
644 kernel_event_notifier
->event
.u
.uprobe
.fd
=
645 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
651 status
= lttng_event_rule_kernel_uprobe_get_event_name(
653 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
657 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
659 const enum lttng_event_rule_status status
=
660 lttng_event_rule_kernel_tracepoint_get_name_pattern(
663 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
664 kernel_event_notifier
->event
.instrumentation
=
665 LTTNG_KERNEL_ABI_TRACEPOINT
;
670 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
672 const enum lttng_event_rule_status status
=
673 lttng_event_rule_kernel_syscall_get_name_pattern(
675 const enum lttng_event_rule_kernel_syscall_emission_site
677 lttng_event_rule_kernel_syscall_get_emission_site(rule
);
678 enum lttng_kernel_abi_syscall_entryexit entryexit
;
680 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
681 LTTNG_ASSERT(emission_site
!= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
);
683 switch(emission_site
) {
684 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
685 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRY
;
687 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
688 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_EXIT
;
690 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
691 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
698 kernel_event_notifier
->event
.instrumentation
=
699 LTTNG_KERNEL_ABI_SYSCALL
;
700 kernel_event_notifier
->event
.u
.syscall
.abi
=
701 LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
702 kernel_event_notifier
->event
.u
.syscall
.entryexit
=
704 kernel_event_notifier
->event
.u
.syscall
.match
=
705 LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
714 strncpy_ret
= lttng_strncpy(kernel_event_notifier
->event
.name
, name
,
715 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
717 ret_code
= LTTNG_ERR_INVALID
;
725 * Allocate and initialize a kernel metadata.
727 * Return pointer to structure or NULL.
729 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
732 struct ltt_kernel_metadata
*lkm
;
733 struct lttng_channel
*chan
;
735 lkm
= zmalloc
<ltt_kernel_metadata
>();
736 chan
= zmalloc
<lttng_channel
>();
737 if (lkm
== NULL
|| chan
== NULL
) {
738 PERROR("kernel metadata zmalloc");
743 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
745 ERR("Failed to initialize metadata channel name to `%s`",
746 DEFAULT_METADATA_NAME
);
750 /* Set default attributes */
751 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
752 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
753 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
754 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
755 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
759 * The metadata channel of kernel sessions must use the "mmap"
760 * back-end since the consumer daemon accumulates complete
761 * metadata units before sending them to the relay daemon in
762 * live mode. The consumer daemon also needs to extract the contents
763 * of the metadata cache when computing a rotation position.
765 * In both cases, it is not possible to rely on the splice
766 * back-end as the consumer daemon may need to accumulate more
767 * content than can be backed by the ring buffer's underlying
770 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
771 chan
->attr
.tracefile_size
= 0;
772 chan
->attr
.tracefile_count
= 0;
773 chan
->attr
.live_timer_interval
= 0;
788 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
791 * Return pointer to structure or NULL.
793 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
797 struct ltt_kernel_stream
*lks
;
801 lks
= zmalloc
<ltt_kernel_stream
>();
803 PERROR("kernel stream zmalloc");
808 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
810 PERROR("snprintf stream name");
813 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
827 * Cleanup kernel stream structure.
829 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
831 LTTNG_ASSERT(stream
);
833 DBG("[trace] Closing stream fd %d", stream
->fd
);
834 /* Close kernel fd */
835 if (stream
->fd
>= 0) {
838 ret
= close(stream
->fd
);
843 /* Remove from stream list */
844 cds_list_del(&stream
->list
);
850 * Cleanup kernel event structure.
852 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
856 if (event
->fd
>= 0) {
859 DBG("[trace] Closing event fd %d", event
->fd
);
860 /* Close kernel fd */
861 ret
= close(event
->fd
);
866 DBG("[trace] Tearing down event (no associated file descriptor)");
869 /* Remove from event list */
870 cds_list_del(&event
->list
);
872 free(event
->filter_expression
);
880 * Cleanup kernel event structure.
882 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
884 struct ltt_kernel_event_notifier_rule
*rule
= caa_container_of(rcu_node
,
885 struct ltt_kernel_event_notifier_rule
, rcu_node
);
890 void trace_kernel_destroy_event_notifier_rule(
891 struct ltt_kernel_event_notifier_rule
*event
)
895 if (event
->fd
>= 0) {
896 const int ret
= close(event
->fd
);
898 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
901 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
905 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
908 lttng_trigger_put(event
->trigger
);
909 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
912 * Cleanup kernel context structure.
914 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
919 cds_list_del(&ctx
->list
);
925 * Cleanup kernel channel structure.
927 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
929 struct ltt_kernel_stream
*stream
, *stmp
;
930 struct ltt_kernel_event
*event
, *etmp
;
931 struct ltt_kernel_context
*ctx
, *ctmp
;
933 enum lttng_error_code status
;
935 LTTNG_ASSERT(channel
);
937 DBG("[trace] Closing channel fd %d", channel
->fd
);
938 /* Close kernel fd */
939 if (channel
->fd
>= 0) {
940 ret
= close(channel
->fd
);
946 /* For each stream in the channel list */
947 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
948 trace_kernel_destroy_stream(stream
);
951 /* For each event in the channel list */
952 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
953 trace_kernel_destroy_event(event
);
956 /* For each context in the channel list */
957 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
958 trace_kernel_destroy_context(ctx
);
961 /* Remove from channel list */
962 cds_list_del(&channel
->list
);
964 if (the_notification_thread_handle
&&
965 channel
->published_to_notification_thread
) {
966 status
= notification_thread_command_remove_channel(
967 the_notification_thread_handle
, channel
->key
,
968 LTTNG_DOMAIN_KERNEL
);
969 LTTNG_ASSERT(status
== LTTNG_OK
);
971 free(channel
->channel
->attr
.extended
.ptr
);
972 free(channel
->channel
);
977 * Cleanup kernel metadata structure.
979 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
981 LTTNG_ASSERT(metadata
);
983 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
984 /* Close kernel fd */
985 if (metadata
->fd
>= 0) {
988 ret
= close(metadata
->fd
);
994 free(metadata
->conf
);
999 * Cleanup kernel session structure
1001 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
1003 struct ltt_kernel_channel
*channel
, *ctmp
;
1006 LTTNG_ASSERT(session
);
1008 DBG("[trace] Closing session fd %d", session
->fd
);
1009 /* Close kernel fds */
1010 if (session
->fd
>= 0) {
1011 ret
= close(session
->fd
);
1017 if (session
->metadata_stream_fd
>= 0) {
1018 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1019 ret
= close(session
->metadata_stream_fd
);
1025 if (session
->metadata
!= NULL
) {
1026 trace_kernel_destroy_metadata(session
->metadata
);
1029 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
1030 trace_kernel_destroy_channel(channel
);
1034 /* Free elements needed by destroy notifiers. */
1035 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1037 /* Wipe consumer output object */
1038 consumer_output_put(session
->consumer
);
1040 process_attr_tracker_destroy(session
->tracker_pid
);
1041 process_attr_tracker_destroy(session
->tracker_vpid
);
1042 process_attr_tracker_destroy(session
->tracker_uid
);
1043 process_attr_tracker_destroy(session
->tracker_vuid
);
1044 process_attr_tracker_destroy(session
->tracker_gid
);
1045 process_attr_tracker_destroy(session
->tracker_vgid
);