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) {
104 DBG("Found event %s for channel %s", name
, channel
->channel
->name
);
112 * Find the event name for the given channel.
114 struct ltt_kernel_event
*trace_kernel_get_event_by_name(char *name
,
115 struct ltt_kernel_channel
*channel
,
116 enum lttng_event_type type
)
118 struct ltt_kernel_event
*ev
;
122 LTTNG_ASSERT(channel
);
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
) != 0) {
135 DBG("Found event %s for channel %s", name
, channel
->channel
->name
);
143 * Allocate and initialize a kernel session data structure.
145 * Return pointer to structure or NULL.
147 struct ltt_kernel_session
*trace_kernel_create_session()
149 struct ltt_kernel_session
*lks
= nullptr;
151 /* Allocate a new ltt kernel session */
152 lks
= zmalloc
<ltt_kernel_session
>();
153 if (lks
== nullptr) {
154 PERROR("create kernel session zmalloc");
158 /* Init data structure */
160 lks
->metadata_stream_fd
= -1;
161 lks
->channel_count
= 0;
162 lks
->stream_count_global
= 0;
163 lks
->metadata
= nullptr;
164 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
166 lks
->tracker_pid
= process_attr_tracker_create();
167 if (!lks
->tracker_pid
) {
170 lks
->tracker_vpid
= process_attr_tracker_create();
171 if (!lks
->tracker_vpid
) {
174 lks
->tracker_uid
= process_attr_tracker_create();
175 if (!lks
->tracker_uid
) {
178 lks
->tracker_vuid
= process_attr_tracker_create();
179 if (!lks
->tracker_vuid
) {
182 lks
->tracker_gid
= process_attr_tracker_create();
183 if (!lks
->tracker_gid
) {
186 lks
->tracker_vgid
= process_attr_tracker_create();
187 if (!lks
->tracker_vgid
) {
190 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
191 if (lks
->consumer
== nullptr) {
198 process_attr_tracker_destroy(lks
->tracker_pid
);
199 process_attr_tracker_destroy(lks
->tracker_vpid
);
200 process_attr_tracker_destroy(lks
->tracker_uid
);
201 process_attr_tracker_destroy(lks
->tracker_vuid
);
202 process_attr_tracker_destroy(lks
->tracker_gid
);
203 process_attr_tracker_destroy(lks
->tracker_vgid
);
211 * Allocate and initialize a kernel channel data structure.
213 * Return pointer to structure or NULL.
215 struct ltt_kernel_channel
*trace_kernel_create_channel(struct lttng_channel
*chan
)
217 struct ltt_kernel_channel
*lkc
;
218 struct lttng_channel_extended
*extended
= nullptr;
222 lkc
= zmalloc
<ltt_kernel_channel
>();
223 if (lkc
== nullptr) {
224 PERROR("ltt_kernel_channel zmalloc");
228 lkc
->channel
= zmalloc
<lttng_channel
>();
229 if (lkc
->channel
== nullptr) {
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
, sizeof(lkc
->channel
->name
));
251 lkc
->channel
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
254 lkc
->stream_count
= 0;
255 lkc
->event_count
= 0;
257 lkc
->published_to_notification_thread
= false;
258 /* Init linked list */
259 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
260 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
261 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
275 * Allocate and init a kernel context object.
277 * Return the allocated object or NULL on error.
279 struct ltt_kernel_context
*trace_kernel_create_context(struct lttng_kernel_abi_context
*ctx
)
281 struct ltt_kernel_context
*kctx
;
283 kctx
= zmalloc
<ltt_kernel_context
>();
285 PERROR("zmalloc kernel context");
290 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
297 * Allocate and init a kernel context object from an existing kernel context
300 * Return the allocated object or NULL on error.
302 struct ltt_kernel_context
*trace_kernel_copy_context(struct ltt_kernel_context
*kctx
)
304 struct ltt_kernel_context
*kctx_copy
;
307 kctx_copy
= zmalloc
<ltt_kernel_context
>();
309 PERROR("zmalloc ltt_kernel_context");
313 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
314 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
321 * Allocate and initialize a kernel event. Set name and event type.
322 * We own filter_expression, and filter.
324 * Return pointer to structure or NULL.
326 enum lttng_error_code
trace_kernel_create_event(struct lttng_event
*ev
,
327 char *filter_expression
,
328 struct lttng_bytecode
*filter
,
329 struct ltt_kernel_event
**kernel_event
)
331 enum lttng_error_code ret
;
332 struct lttng_kernel_abi_event
*attr
;
333 struct ltt_kernel_event
*local_kernel_event
;
334 struct lttng_userspace_probe_location
*userspace_probe_location
= nullptr;
338 local_kernel_event
= zmalloc
<ltt_kernel_event
>();
339 attr
= zmalloc
<lttng_kernel_abi_event
>();
340 if (local_kernel_event
== nullptr || attr
== nullptr) {
341 PERROR("kernel event zmalloc");
342 ret
= LTTNG_ERR_NOMEM
;
347 case LTTNG_EVENT_PROBE
:
348 attr
->instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
349 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
350 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
351 strncpy(attr
->u
.kprobe
.symbol_name
,
352 ev
->attr
.probe
.symbol_name
,
353 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
354 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
356 case LTTNG_EVENT_USERSPACE_PROBE
:
358 const struct lttng_userspace_probe_location
*location
= nullptr;
359 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
361 location
= lttng_event_get_userspace_probe_location(ev
);
363 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
368 * From this point on, the specific term 'uprobe' is used
369 * instead of the generic 'userspace probe' because it's the
370 * technology used at the moment for this instrumentation.
371 * LTTng currently implements userspace probes using uprobes.
372 * In the interactions with the kernel tracer, we use the
375 attr
->instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
377 lookup
= lttng_userspace_probe_location_get_lookup_method(location
);
379 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
384 * From the kernel tracer's perspective, all userspace probe
385 * event types are all the same: a file and an offset.
387 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
388 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
389 /* Get the file descriptor on the target binary. */
391 lttng_userspace_probe_location_function_get_binary_fd(location
);
394 * Save a reference to the probe location used during
395 * the listing of events.
397 userspace_probe_location
= lttng_userspace_probe_location_copy(location
);
399 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
400 /* Get the file descriptor on the target binary. */
402 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
405 * Save a reference to the probe location used during the listing of
408 userspace_probe_location
= lttng_userspace_probe_location_copy(location
);
411 DBG("Unsupported lookup method type");
412 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
417 case LTTNG_EVENT_FUNCTION
:
418 attr
->instrumentation
= LTTNG_KERNEL_ABI_KRETPROBE
;
419 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
420 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
421 strncpy(attr
->u
.kretprobe
.symbol_name
,
422 ev
->attr
.probe
.symbol_name
,
423 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
424 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
426 case LTTNG_EVENT_FUNCTION_ENTRY
:
427 attr
->instrumentation
= LTTNG_KERNEL_ABI_FUNCTION
;
428 strncpy(attr
->u
.ftrace
.symbol_name
,
429 ev
->attr
.ftrace
.symbol_name
,
430 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
431 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
433 case LTTNG_EVENT_TRACEPOINT
:
434 attr
->instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
436 case LTTNG_EVENT_SYSCALL
:
437 attr
->instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
438 attr
->u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
439 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
440 attr
->u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
442 case LTTNG_EVENT_ALL
:
443 attr
->instrumentation
= LTTNG_KERNEL_ABI_ALL
;
446 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
447 ret
= LTTNG_ERR_INVALID
;
451 /* Copy event name */
452 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
453 attr
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
455 /* Setting up a kernel event */
456 local_kernel_event
->fd
= -1;
457 local_kernel_event
->event
= attr
;
458 local_kernel_event
->enabled
= 1;
459 local_kernel_event
->filter_expression
= filter_expression
;
460 local_kernel_event
->filter
= filter
;
461 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
463 *kernel_event
= local_kernel_event
;
468 free(filter_expression
);
470 free(local_kernel_event
);
476 * Allocate and initialize a kernel token event rule.
478 * Return pointer to structure or NULL.
480 enum lttng_error_code
481 trace_kernel_create_event_notifier_rule(struct lttng_trigger
*trigger
,
483 uint64_t error_counter_index
,
484 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
486 enum lttng_error_code ret
= LTTNG_OK
;
487 enum lttng_condition_type condition_type
;
488 enum lttng_event_rule_type event_rule_type
;
489 enum lttng_condition_status condition_status
;
490 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
491 const struct lttng_condition
*condition
= nullptr;
492 const struct lttng_event_rule
*event_rule
= nullptr;
494 LTTNG_ASSERT(event_notifier_rule
);
496 condition
= lttng_trigger_get_const_condition(trigger
);
497 LTTNG_ASSERT(condition
);
499 condition_type
= lttng_condition_get_type(condition
);
500 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
502 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
503 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
504 LTTNG_ASSERT(event_rule
);
506 event_rule_type
= lttng_event_rule_get_type(event_rule
);
507 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
509 local_kernel_token_event_rule
= zmalloc
<ltt_kernel_event_notifier_rule
>();
510 if (local_kernel_token_event_rule
== nullptr) {
511 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
512 ret
= LTTNG_ERR_NOMEM
;
516 local_kernel_token_event_rule
->fd
= -1;
517 local_kernel_token_event_rule
->enabled
= 1;
518 local_kernel_token_event_rule
->token
= token
;
519 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
521 /* Get the reference of the event rule. */
522 lttng_trigger_get(trigger
);
524 local_kernel_token_event_rule
->trigger
= trigger
;
525 /* The event rule still owns the filter and bytecode. */
526 local_kernel_token_event_rule
->filter
= lttng_event_rule_get_filter_bytecode(event_rule
);
528 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
529 local_kernel_token_event_rule
->token
);
531 *event_notifier_rule
= local_kernel_token_event_rule
;
536 * Initialize a kernel trigger from an event rule.
538 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
539 const struct lttng_event_rule
*rule
,
540 struct lttng_kernel_abi_event_notifier
*kernel_event_notifier
)
542 enum lttng_error_code ret_code
;
546 switch (lttng_event_rule_get_type(rule
)) {
547 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
549 uint64_t address
= 0, offset
= 0;
550 const char *symbol_name
= nullptr;
551 const struct lttng_kernel_probe_location
*location
= nullptr;
552 enum lttng_kernel_probe_location_status k_status
;
553 enum lttng_event_rule_status status
;
555 status
= lttng_event_rule_kernel_kprobe_get_location(rule
, &location
);
556 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
557 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
561 switch (lttng_kernel_probe_location_get_type(location
)) {
562 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
565 lttng_kernel_probe_location_address_get_address(location
, &address
);
566 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
569 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
571 k_status
= lttng_kernel_probe_location_symbol_get_offset(location
, &offset
);
572 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
573 symbol_name
= lttng_kernel_probe_location_symbol_get_name(location
);
580 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
581 kernel_event_notifier
->event
.u
.kprobe
.addr
= address
;
582 kernel_event_notifier
->event
.u
.kprobe
.offset
= offset
;
584 strncpy_ret
= lttng_strncpy(
585 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
,
587 sizeof(kernel_event_notifier
->event
.u
.kprobe
.symbol_name
));
590 ret_code
= LTTNG_ERR_INVALID
;
595 kernel_event_notifier
->event
.u
.kprobe
596 .symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
598 status
= lttng_event_rule_kernel_kprobe_get_event_name(rule
, &name
);
599 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
603 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
605 const struct lttng_userspace_probe_location
*location
= nullptr;
606 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
607 enum lttng_event_rule_status status
;
609 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
610 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
611 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
615 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
617 lookup
= lttng_userspace_probe_location_get_lookup_method(location
);
619 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
624 * From the kernel tracer's perspective, all userspace probe
625 * event types are all the same: a file and an offset.
627 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
628 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
629 /* Get the file descriptor on the target binary. */
630 kernel_event_notifier
->event
.u
.uprobe
.fd
=
631 lttng_userspace_probe_location_function_get_binary_fd(location
);
634 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
635 /* Get the file descriptor on the target binary. */
636 kernel_event_notifier
->event
.u
.uprobe
.fd
=
637 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
643 status
= lttng_event_rule_kernel_uprobe_get_event_name(rule
, &name
);
644 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
648 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
650 const enum lttng_event_rule_status status
=
651 lttng_event_rule_kernel_tracepoint_get_name_pattern(rule
, &name
);
653 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
654 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
659 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
661 const enum lttng_event_rule_status status
=
662 lttng_event_rule_kernel_syscall_get_name_pattern(rule
, &name
);
663 const enum lttng_event_rule_kernel_syscall_emission_site emission_site
=
664 lttng_event_rule_kernel_syscall_get_emission_site(rule
);
665 enum lttng_kernel_abi_syscall_entryexit entryexit
;
667 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
668 LTTNG_ASSERT(emission_site
!=
669 LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
);
671 switch (emission_site
) {
672 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
673 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRY
;
675 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
676 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_EXIT
;
678 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
679 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
686 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
687 kernel_event_notifier
->event
.u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
688 kernel_event_notifier
->event
.u
.syscall
.entryexit
= entryexit
;
689 kernel_event_notifier
->event
.u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
698 strncpy_ret
= lttng_strncpy(
699 kernel_event_notifier
->event
.name
, name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
701 ret_code
= LTTNG_ERR_INVALID
;
709 * Allocate and initialize a kernel metadata.
711 * Return pointer to structure or NULL.
713 struct ltt_kernel_metadata
*trace_kernel_create_metadata()
716 struct ltt_kernel_metadata
*lkm
;
717 struct lttng_channel
*chan
;
719 lkm
= zmalloc
<ltt_kernel_metadata
>();
720 chan
= zmalloc
<lttng_channel
>();
721 if (lkm
== nullptr || chan
== nullptr) {
722 PERROR("kernel metadata zmalloc");
726 ret
= lttng_strncpy(chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
728 ERR("Failed to initialize metadata channel name to `%s`", DEFAULT_METADATA_NAME
);
732 /* Set default attributes */
733 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
734 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
735 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
736 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
737 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;
741 * The metadata channel of kernel sessions must use the "mmap"
742 * back-end since the consumer daemon accumulates complete
743 * metadata units before sending them to the relay daemon in
744 * live mode. The consumer daemon also needs to extract the contents
745 * of the metadata cache when computing a rotation position.
747 * In both cases, it is not possible to rely on the splice
748 * back-end as the consumer daemon may need to accumulate more
749 * content than can be backed by the ring buffer's underlying
752 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
753 chan
->attr
.tracefile_size
= 0;
754 chan
->attr
.tracefile_count
= 0;
755 chan
->attr
.live_timer_interval
= 0;
770 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
773 * Return pointer to structure or NULL.
775 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
, unsigned int count
)
778 struct ltt_kernel_stream
*lks
;
782 lks
= zmalloc
<ltt_kernel_stream
>();
783 if (lks
== nullptr) {
784 PERROR("kernel stream zmalloc");
789 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
791 PERROR("snprintf stream name");
794 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
808 * Cleanup kernel stream structure.
810 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
812 LTTNG_ASSERT(stream
);
814 DBG("[trace] Closing stream fd %d", stream
->fd
);
815 /* Close kernel fd */
816 if (stream
->fd
>= 0) {
819 ret
= close(stream
->fd
);
824 /* Remove from stream list */
825 cds_list_del(&stream
->list
);
831 * Cleanup kernel event structure.
833 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
837 if (event
->fd
>= 0) {
840 DBG("[trace] Closing event fd %d", event
->fd
);
841 /* Close kernel fd */
842 ret
= close(event
->fd
);
847 DBG("[trace] Tearing down event (no associated file descriptor)");
850 /* Remove from event list */
851 cds_list_del(&event
->list
);
853 free(event
->filter_expression
);
861 * Cleanup kernel event structure.
863 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
865 struct ltt_kernel_event_notifier_rule
*rule
=
866 caa_container_of(rcu_node
, struct ltt_kernel_event_notifier_rule
, rcu_node
);
871 void trace_kernel_destroy_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
875 if (event
->fd
>= 0) {
876 const int ret
= close(event
->fd
);
878 DBG("Closing kernel event notifier rule file descriptor: fd = %d", event
->fd
);
880 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
884 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
887 lttng_trigger_put(event
->trigger
);
888 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
891 * Cleanup kernel context structure.
893 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
898 cds_list_del(&ctx
->list
);
904 * Cleanup kernel channel structure.
906 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
908 struct ltt_kernel_stream
*stream
, *stmp
;
909 struct ltt_kernel_event
*event
, *etmp
;
910 struct ltt_kernel_context
*ctx
, *ctmp
;
912 enum lttng_error_code status
;
914 LTTNG_ASSERT(channel
);
916 DBG("[trace] Closing channel fd %d", channel
->fd
);
917 /* Close kernel fd */
918 if (channel
->fd
>= 0) {
919 ret
= close(channel
->fd
);
925 /* For each stream in the channel list */
926 cds_list_for_each_entry_safe (stream
, stmp
, &channel
->stream_list
.head
, list
) {
927 trace_kernel_destroy_stream(stream
);
930 /* For each event in the channel list */
931 cds_list_for_each_entry_safe (event
, etmp
, &channel
->events_list
.head
, list
) {
932 trace_kernel_destroy_event(event
);
935 /* For each context in the channel list */
936 cds_list_for_each_entry_safe (ctx
, ctmp
, &channel
->ctx_list
, list
) {
937 trace_kernel_destroy_context(ctx
);
940 /* Remove from channel list */
941 cds_list_del(&channel
->list
);
943 if (the_notification_thread_handle
&& channel
->published_to_notification_thread
) {
944 status
= notification_thread_command_remove_channel(
945 the_notification_thread_handle
, channel
->key
, LTTNG_DOMAIN_KERNEL
);
946 LTTNG_ASSERT(status
== LTTNG_OK
);
948 free(channel
->channel
->attr
.extended
.ptr
);
949 free(channel
->channel
);
954 * Cleanup kernel metadata structure.
956 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
958 LTTNG_ASSERT(metadata
);
960 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
961 /* Close kernel fd */
962 if (metadata
->fd
>= 0) {
965 ret
= close(metadata
->fd
);
971 free(metadata
->conf
);
976 * Cleanup kernel session structure
978 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
980 struct ltt_kernel_channel
*channel
, *ctmp
;
983 LTTNG_ASSERT(session
);
985 DBG("[trace] Closing session fd %d", session
->fd
);
986 /* Close kernel fds */
987 if (session
->fd
>= 0) {
988 ret
= close(session
->fd
);
994 if (session
->metadata_stream_fd
>= 0) {
995 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
996 ret
= close(session
->metadata_stream_fd
);
1002 if (session
->metadata
!= nullptr) {
1003 trace_kernel_destroy_metadata(session
->metadata
);
1006 cds_list_for_each_entry_safe (channel
, ctmp
, &session
->channel_list
.head
, list
) {
1007 trace_kernel_destroy_channel(channel
);
1011 /* Free elements needed by destroy notifiers. */
1012 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1014 /* Wipe consumer output object */
1015 consumer_output_put(session
->consumer
);
1017 process_attr_tracker_destroy(session
->tracker_pid
);
1018 process_attr_tracker_destroy(session
->tracker_vpid
);
1019 process_attr_tracker_destroy(session
->tracker_uid
);
1020 process_attr_tracker_destroy(session
->tracker_vuid
);
1021 process_attr_tracker_destroy(session
->tracker_gid
);
1022 process_attr_tracker_destroy(session
->tracker_vgid
);