2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "consumer.hpp"
10 #include "lttng-sessiond.hpp"
11 #include "notification-thread-commands.hpp"
12 #include "trace-kernel.hpp"
14 #include <common/common.hpp>
15 #include <common/defaults.hpp>
16 #include <common/macros.hpp>
17 #include <common/trace-chunk.hpp>
19 #include <lttng/event-rule/event-rule-internal.hpp>
20 #include <lttng/event-rule/event-rule.h>
21 #include <lttng/event-rule/kernel-kprobe-internal.hpp>
22 #include <lttng/event-rule/kernel-kprobe.h>
23 #include <lttng/event-rule/kernel-syscall-internal.hpp>
24 #include <lttng/event-rule/kernel-syscall.h>
25 #include <lttng/event-rule/kernel-tracepoint-internal.hpp>
26 #include <lttng/event-rule/kernel-tracepoint.h>
27 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
28 #include <lttng/event-rule/kernel-uprobe.h>
29 #include <lttng/event.h>
30 #include <lttng/kernel-probe.h>
31 #include <lttng/lttng-error.h>
32 #include <lttng/userspace-probe-internal.hpp>
33 #include <lttng/userspace-probe.h>
41 * Find the channel name for the given kernel session.
43 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(const char *name
,
44 struct ltt_kernel_session
*session
)
46 struct ltt_kernel_channel
*chan
;
48 LTTNG_ASSERT(session
);
52 * If we receive an empty string for channel name, it means the
53 * default channel name is requested.
56 name
= DEFAULT_CHANNEL_NAME
;
58 DBG("Trying to find channel %s", name
);
60 cds_list_for_each_entry (chan
, &session
->channel_list
.head
, list
) {
61 if (strcmp(name
, chan
->channel
->name
) == 0) {
62 DBG("Found channel by name %s", name
);
71 * Find the event for the given channel.
73 struct ltt_kernel_event
*trace_kernel_find_event(char *name
,
74 struct ltt_kernel_channel
*channel
,
75 enum lttng_event_type type
,
76 struct lttng_bytecode
*filter
)
78 struct ltt_kernel_event
*ev
;
82 LTTNG_ASSERT(channel
);
84 cds_list_for_each_entry (ev
, &channel
->events_list
.head
, list
) {
85 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
88 if (strcmp(name
, ev
->event
->name
) != 0) {
91 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
94 if (ev
->filter
&& filter
) {
95 if (ev
->filter
->len
!= filter
->len
||
96 memcmp(ev
->filter
->data
, filter
->data
, filter
->len
) != 0) {
106 DBG("Found event %s for channel %s", name
, channel
->channel
->name
);
114 * Find the event name for the given channel.
116 struct ltt_kernel_event
*trace_kernel_get_event_by_name(char *name
,
117 struct ltt_kernel_channel
*channel
,
118 enum lttng_event_type type
)
120 struct ltt_kernel_event
*ev
;
124 LTTNG_ASSERT(channel
);
126 cds_list_for_each_entry (ev
, &channel
->events_list
.head
, list
) {
127 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
130 if (strcmp(name
, ev
->event
->name
) != 0) {
139 DBG("Found event %s for channel %s", name
, channel
->channel
->name
);
147 * Allocate and initialize a kernel session data structure.
149 * Return pointer to structure or NULL.
151 struct ltt_kernel_session
*trace_kernel_create_session()
153 struct ltt_kernel_session
*lks
= nullptr;
155 /* Allocate a new ltt kernel session */
156 lks
= zmalloc
<ltt_kernel_session
>();
157 if (lks
== nullptr) {
158 PERROR("create kernel session zmalloc");
162 /* Init data structure */
164 lks
->metadata_stream_fd
= -1;
165 lks
->channel_count
= 0;
166 lks
->stream_count_global
= 0;
167 lks
->metadata
= nullptr;
168 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
170 lks
->tracker_pid
= process_attr_tracker_create();
171 if (!lks
->tracker_pid
) {
174 lks
->tracker_vpid
= process_attr_tracker_create();
175 if (!lks
->tracker_vpid
) {
178 lks
->tracker_uid
= process_attr_tracker_create();
179 if (!lks
->tracker_uid
) {
182 lks
->tracker_vuid
= process_attr_tracker_create();
183 if (!lks
->tracker_vuid
) {
186 lks
->tracker_gid
= process_attr_tracker_create();
187 if (!lks
->tracker_gid
) {
190 lks
->tracker_vgid
= process_attr_tracker_create();
191 if (!lks
->tracker_vgid
) {
194 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
195 if (lks
->consumer
== nullptr) {
202 process_attr_tracker_destroy(lks
->tracker_pid
);
203 process_attr_tracker_destroy(lks
->tracker_vpid
);
204 process_attr_tracker_destroy(lks
->tracker_uid
);
205 process_attr_tracker_destroy(lks
->tracker_vuid
);
206 process_attr_tracker_destroy(lks
->tracker_gid
);
207 process_attr_tracker_destroy(lks
->tracker_vgid
);
215 * Allocate and initialize a kernel channel data structure.
217 * Return pointer to structure or NULL.
219 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
)
221 struct ltt_kernel_channel
*lkc
;
222 struct lttng_channel_extended
*extended
= nullptr;
226 lkc
= zmalloc
<ltt_kernel_channel
>();
227 if (lkc
== nullptr) {
228 PERROR("ltt_kernel_channel zmalloc");
232 lkc
->channel
= zmalloc
<lttng_channel
>();
233 if (lkc
->channel
== nullptr) {
234 PERROR("lttng_channel zmalloc");
238 extended
= zmalloc
<lttng_channel_extended
>();
240 PERROR("lttng_channel_channel zmalloc");
243 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
244 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
245 lkc
->channel
->attr
.extended
.ptr
= extended
;
249 * If we receive an empty string for channel name, it means the
250 * default channel name is requested.
252 if (chan
->name
[0] == '\0') {
253 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
, sizeof(lkc
->channel
->name
));
255 lkc
->channel
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
258 lkc
->stream_count
= 0;
259 lkc
->event_count
= 0;
261 lkc
->published_to_notification_thread
= false;
262 /* Init linked list */
263 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
264 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
265 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
279 * Allocate and init a kernel context object.
281 * Return the allocated object or NULL on error.
283 struct ltt_kernel_context
*trace_kernel_create_context(struct lttng_kernel_abi_context
*ctx
)
285 struct ltt_kernel_context
*kctx
;
287 kctx
= zmalloc
<ltt_kernel_context
>();
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(struct ltt_kernel_context
*kctx
)
308 struct ltt_kernel_context
*kctx_copy
;
311 kctx_copy
= zmalloc
<ltt_kernel_context
>();
313 PERROR("zmalloc ltt_kernel_context");
317 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
318 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
325 * Allocate and initialize a kernel event. Set name and event type.
326 * We own filter_expression, and filter.
328 * Return pointer to structure or NULL.
330 enum lttng_error_code
trace_kernel_create_event(struct lttng_event
*ev
,
331 char *filter_expression
,
332 struct lttng_bytecode
*filter
,
333 struct ltt_kernel_event
**kernel_event
)
335 enum lttng_error_code ret
;
336 struct lttng_kernel_abi_event
*attr
;
337 struct ltt_kernel_event
*local_kernel_event
;
338 struct lttng_userspace_probe_location
*userspace_probe_location
= nullptr;
342 local_kernel_event
= zmalloc
<ltt_kernel_event
>();
343 attr
= zmalloc
<lttng_kernel_abi_event
>();
344 if (local_kernel_event
== nullptr || attr
== nullptr) {
345 PERROR("kernel event zmalloc");
346 ret
= LTTNG_ERR_NOMEM
;
351 case LTTNG_EVENT_PROBE
:
352 attr
->instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
353 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
354 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
355 strncpy(attr
->u
.kprobe
.symbol_name
,
356 ev
->attr
.probe
.symbol_name
,
357 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
358 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
360 case LTTNG_EVENT_USERSPACE_PROBE
:
362 const struct lttng_userspace_probe_location
*location
= nullptr;
363 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
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_ABI_UPROBE
;
381 lookup
= lttng_userspace_probe_location_get_lookup_method(location
);
383 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
388 * From the kernel tracer's perspective, all userspace probe
389 * event types are all the same: a file and an offset.
391 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
392 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
393 /* Get the file descriptor on the target binary. */
395 lttng_userspace_probe_location_function_get_binary_fd(location
);
398 * Save a reference to the probe location used during
399 * the listing of events.
401 userspace_probe_location
= 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
= lttng_userspace_probe_location_copy(location
);
415 DBG("Unsupported lookup method type");
416 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
421 case LTTNG_EVENT_FUNCTION
:
422 attr
->instrumentation
= LTTNG_KERNEL_ABI_KRETPROBE
;
423 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
424 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
425 strncpy(attr
->u
.kretprobe
.symbol_name
,
426 ev
->attr
.probe
.symbol_name
,
427 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
,
434 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
435 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
437 case LTTNG_EVENT_TRACEPOINT
:
438 attr
->instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
440 case LTTNG_EVENT_SYSCALL
:
441 attr
->instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
442 attr
->u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
443 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
444 attr
->u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
446 case LTTNG_EVENT_ALL
:
447 attr
->instrumentation
= LTTNG_KERNEL_ABI_ALL
;
450 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
451 ret
= LTTNG_ERR_INVALID
;
455 /* Copy event name */
456 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
457 attr
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
459 /* Setting up a kernel event */
460 local_kernel_event
->fd
= -1;
461 local_kernel_event
->event
= attr
;
462 local_kernel_event
->enabled
= true;
463 local_kernel_event
->filter_expression
= filter_expression
;
464 local_kernel_event
->filter
= filter
;
465 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
467 *kernel_event
= local_kernel_event
;
472 free(filter_expression
);
474 free(local_kernel_event
);
480 * Allocate and initialize a kernel token event rule.
482 * Return pointer to structure or NULL.
484 enum lttng_error_code
485 trace_kernel_create_event_notifier_rule(struct lttng_trigger
*trigger
,
487 uint64_t error_counter_index
,
488 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
490 enum lttng_error_code ret
= LTTNG_OK
;
491 enum lttng_condition_type condition_type
;
492 enum lttng_event_rule_type event_rule_type
;
493 enum lttng_condition_status condition_status
;
494 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
495 const struct lttng_condition
*condition
= nullptr;
496 const struct lttng_event_rule
*event_rule
= nullptr;
498 LTTNG_ASSERT(event_notifier_rule
);
500 condition
= lttng_trigger_get_const_condition(trigger
);
501 LTTNG_ASSERT(condition
);
503 condition_type
= lttng_condition_get_type(condition
);
504 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
506 condition_status
= lttng_condition_event_rule_matches_get_rule(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
= zmalloc
<ltt_kernel_event_notifier_rule
>();
514 if (local_kernel_token_event_rule
== nullptr) {
515 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
516 ret
= LTTNG_ERR_NOMEM
;
520 local_kernel_token_event_rule
->fd
= -1;
521 local_kernel_token_event_rule
->enabled
= true;
522 local_kernel_token_event_rule
->token
= token
;
523 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
525 /* Get the reference of the event rule. */
526 lttng_trigger_get(trigger
);
528 local_kernel_token_event_rule
->trigger
= trigger
;
529 /* The event rule still owns the filter and bytecode. */
530 local_kernel_token_event_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
532 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
533 local_kernel_token_event_rule
->token
);
535 *event_notifier_rule
= local_kernel_token_event_rule
;
540 * Initialize a kernel trigger from an event rule.
542 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
543 const struct lttng_event_rule
*rule
,
544 struct lttng_kernel_abi_event_notifier
*kernel_event_notifier
)
546 enum lttng_error_code ret_code
;
550 switch (lttng_event_rule_get_type(rule
)) {
551 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
553 uint64_t address
= 0, offset
= 0;
554 const char *symbol_name
= nullptr;
555 const struct lttng_kernel_probe_location
*location
= nullptr;
556 enum lttng_kernel_probe_location_status k_status
;
557 enum lttng_event_rule_status status
;
559 status
= lttng_event_rule_kernel_kprobe_get_location(rule
, &location
);
560 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
561 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
565 switch (lttng_kernel_probe_location_get_type(location
)) {
566 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
569 lttng_kernel_probe_location_address_get_address(location
, &address
);
570 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
573 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
575 k_status
= lttng_kernel_probe_location_symbol_get_offset(location
, &offset
);
576 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
577 symbol_name
= lttng_kernel_probe_location_symbol_get_name(location
);
584 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
585 kernel_event_notifier
->event
.u
.kprobe
.addr
= address
;
586 kernel_event_notifier
->event
.u
.kprobe
.offset
= offset
;
588 strncpy_ret
= lttng_strncpy(
589 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
,
591 sizeof(kernel_event_notifier
->event
.u
.kprobe
.symbol_name
));
594 ret_code
= LTTNG_ERR_INVALID
;
599 kernel_event_notifier
->event
.u
.kprobe
600 .symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
602 status
= lttng_event_rule_kernel_kprobe_get_event_name(rule
, &name
);
603 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
607 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
609 const struct lttng_userspace_probe_location
*location
= nullptr;
610 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
611 enum lttng_event_rule_status status
;
613 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
614 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
615 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
619 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
621 lookup
= lttng_userspace_probe_location_get_lookup_method(location
);
623 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
628 * From the kernel tracer's perspective, all userspace probe
629 * event types are all the same: a file and an offset.
631 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
632 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
633 /* Get the file descriptor on the target binary. */
634 kernel_event_notifier
->event
.u
.uprobe
.fd
=
635 lttng_userspace_probe_location_function_get_binary_fd(location
);
638 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
639 /* Get the file descriptor on the target binary. */
640 kernel_event_notifier
->event
.u
.uprobe
.fd
=
641 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
647 status
= lttng_event_rule_kernel_uprobe_get_event_name(rule
, &name
);
648 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
652 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
654 const enum lttng_event_rule_status status
=
655 lttng_event_rule_kernel_tracepoint_get_name_pattern(rule
, &name
);
657 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
658 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
663 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
665 const enum lttng_event_rule_status status
=
666 lttng_event_rule_kernel_syscall_get_name_pattern(rule
, &name
);
667 const enum lttng_event_rule_kernel_syscall_emission_site emission_site
=
668 lttng_event_rule_kernel_syscall_get_emission_site(rule
);
669 enum lttng_kernel_abi_syscall_entryexit entryexit
;
671 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
672 LTTNG_ASSERT(emission_site
!=
673 LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
);
675 switch (emission_site
) {
676 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
677 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRY
;
679 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
680 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_EXIT
;
682 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
683 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
690 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
691 kernel_event_notifier
->event
.u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
692 kernel_event_notifier
->event
.u
.syscall
.entryexit
= entryexit
;
693 kernel_event_notifier
->event
.u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
702 strncpy_ret
= lttng_strncpy(
703 kernel_event_notifier
->event
.name
, name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
705 ret_code
= LTTNG_ERR_INVALID
;
713 * Allocate and initialize a kernel metadata.
715 * Return pointer to structure or NULL.
717 struct ltt_kernel_metadata
*trace_kernel_create_metadata()
720 struct ltt_kernel_metadata
*lkm
;
721 struct lttng_channel
*chan
;
723 lkm
= zmalloc
<ltt_kernel_metadata
>();
724 chan
= zmalloc
<lttng_channel
>();
725 if (lkm
== nullptr || chan
== nullptr) {
726 PERROR("kernel metadata zmalloc");
730 ret
= lttng_strncpy(chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
732 ERR("Failed to initialize metadata channel name to `%s`", DEFAULT_METADATA_NAME
);
736 /* Set default attributes */
737 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
738 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
739 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
740 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
741 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
745 * The metadata channel of kernel sessions must use the "mmap"
746 * back-end since the consumer daemon accumulates complete
747 * metadata units before sending them to the relay daemon in
748 * live mode. The consumer daemon also needs to extract the contents
749 * of the metadata cache when computing a rotation position.
751 * In both cases, it is not possible to rely on the splice
752 * back-end as the consumer daemon may need to accumulate more
753 * content than can be backed by the ring buffer's underlying
756 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
757 chan
->attr
.tracefile_size
= 0;
758 chan
->attr
.tracefile_count
= 0;
759 chan
->attr
.live_timer_interval
= 0;
774 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
777 * Return pointer to structure or NULL.
779 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
, unsigned int count
)
782 struct ltt_kernel_stream
*lks
;
786 lks
= zmalloc
<ltt_kernel_stream
>();
787 if (lks
== nullptr) {
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
)
816 LTTNG_ASSERT(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
=
870 caa_container_of(rcu_node
, struct ltt_kernel_event_notifier_rule
, rcu_node
);
875 void trace_kernel_destroy_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
879 if (event
->fd
>= 0) {
880 const int ret
= close(event
->fd
);
882 DBG("Closing kernel event notifier rule file descriptor: fd = %d", event
->fd
);
884 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
888 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
891 lttng_trigger_put(event
->trigger
);
892 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
895 * Cleanup kernel context structure.
897 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
902 cds_list_del(&ctx
->list
);
908 * Cleanup kernel channel structure.
910 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
912 struct ltt_kernel_stream
*stream
, *stmp
;
913 struct ltt_kernel_event
*event
, *etmp
;
914 struct ltt_kernel_context
*ctx
, *ctmp
;
916 enum lttng_error_code status
;
918 LTTNG_ASSERT(channel
);
920 DBG("[trace] Closing channel fd %d", channel
->fd
);
921 /* Close kernel fd */
922 if (channel
->fd
>= 0) {
923 ret
= close(channel
->fd
);
929 /* For each stream in the channel list */
930 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->stream_list
.head
, list
) {
931 trace_kernel_destroy_stream(stream
);
934 /* For each event in the channel list */
935 cds_list_for_each_entry_safe (event
, etmp
, &channel
->events_list
.head
, list
) {
936 trace_kernel_destroy_event(event
);
939 /* For each context in the channel list */
940 cds_list_for_each_entry_safe (ctx
, ctmp
, &channel
->ctx_list
, list
) {
941 trace_kernel_destroy_context(ctx
);
944 /* Remove from channel list */
945 cds_list_del(&channel
->list
);
947 if (the_notification_thread_handle
&& channel
->published_to_notification_thread
) {
948 status
= notification_thread_command_remove_channel(
949 the_notification_thread_handle
, channel
->key
, LTTNG_DOMAIN_KERNEL
);
950 LTTNG_ASSERT(status
== LTTNG_OK
);
952 free(channel
->channel
->attr
.extended
.ptr
);
953 free(channel
->channel
);
958 * Cleanup kernel metadata structure.
960 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
962 LTTNG_ASSERT(metadata
);
964 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
965 /* Close kernel fd */
966 if (metadata
->fd
>= 0) {
969 ret
= close(metadata
->fd
);
975 free(metadata
->conf
);
980 * Cleanup kernel session structure
982 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
984 struct ltt_kernel_channel
*channel
, *ctmp
;
987 LTTNG_ASSERT(session
);
989 DBG("[trace] Closing session fd %d", session
->fd
);
990 /* Close kernel fds */
991 if (session
->fd
>= 0) {
992 ret
= close(session
->fd
);
998 if (session
->metadata_stream_fd
>= 0) {
999 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1000 ret
= close(session
->metadata_stream_fd
);
1006 if (session
->metadata
!= nullptr) {
1007 trace_kernel_destroy_metadata(session
->metadata
);
1010 cds_list_for_each_entry_safe (channel
, ctmp
, &session
->channel_list
.head
, list
) {
1011 trace_kernel_destroy_channel(channel
);
1015 /* Free elements needed by destroy notifiers. */
1016 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1018 /* Wipe consumer output object */
1019 consumer_output_put(session
->consumer
);
1021 process_attr_tracker_destroy(session
->tracker_pid
);
1022 process_attr_tracker_destroy(session
->tracker_vpid
);
1023 process_attr_tracker_destroy(session
->tracker_uid
);
1024 process_attr_tracker_destroy(session
->tracker_vuid
);
1025 process_attr_tracker_destroy(session
->tracker_gid
);
1026 process_attr_tracker_destroy(session
->tracker_vgid
);