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-kprobe.h>
22 #include <lttng/event-rule/kernel-kprobe-internal.h>
23 #include <lttng/event-rule/kernel-syscall.h>
24 #include <lttng/event-rule/kernel-syscall-internal.h>
25 #include <lttng/event-rule/kernel-tracepoint.h>
26 #include <lttng/event-rule/kernel-tracepoint-internal.h>
27 #include <lttng/event-rule/kernel-uprobe.h>
28 #include <lttng/event-rule/kernel-uprobe-internal.h>
29 #include <common/common.h>
30 #include <common/defaults.h>
31 #include <common/trace-chunk.h>
32 #include <common/macros.h>
35 #include "trace-kernel.h"
36 #include "lttng-sessiond.h"
37 #include "notification-thread-commands.h"
40 * Find the channel name for the given kernel session.
42 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
43 const char *name
, struct ltt_kernel_session
*session
)
45 struct ltt_kernel_channel
*chan
;
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
= zmalloc(sizeof(struct ltt_kernel_session
));
156 PERROR("create kernel session zmalloc");
160 /* Init data structure */
162 lks
->metadata_stream_fd
= -1;
163 lks
->channel_count
= 0;
164 lks
->stream_count_global
= 0;
165 lks
->metadata
= NULL
;
166 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
168 lks
->tracker_pid
= process_attr_tracker_create();
169 if (!lks
->tracker_pid
) {
172 lks
->tracker_vpid
= process_attr_tracker_create();
173 if (!lks
->tracker_vpid
) {
176 lks
->tracker_uid
= process_attr_tracker_create();
177 if (!lks
->tracker_uid
) {
180 lks
->tracker_vuid
= process_attr_tracker_create();
181 if (!lks
->tracker_vuid
) {
184 lks
->tracker_gid
= process_attr_tracker_create();
185 if (!lks
->tracker_gid
) {
188 lks
->tracker_vgid
= process_attr_tracker_create();
189 if (!lks
->tracker_vgid
) {
192 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
193 if (lks
->consumer
== NULL
) {
200 process_attr_tracker_destroy(lks
->tracker_pid
);
201 process_attr_tracker_destroy(lks
->tracker_vpid
);
202 process_attr_tracker_destroy(lks
->tracker_uid
);
203 process_attr_tracker_destroy(lks
->tracker_vuid
);
204 process_attr_tracker_destroy(lks
->tracker_gid
);
205 process_attr_tracker_destroy(lks
->tracker_vgid
);
213 * Allocate and initialize a kernel channel data structure.
215 * Return pointer to structure or NULL.
217 struct ltt_kernel_channel
*trace_kernel_create_channel(
218 struct lttng_channel
*chan
)
220 struct ltt_kernel_channel
*lkc
;
221 struct lttng_channel_extended
*extended
= NULL
;
225 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
227 PERROR("ltt_kernel_channel zmalloc");
231 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
232 if (lkc
->channel
== NULL
) {
233 PERROR("lttng_channel zmalloc");
237 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
239 PERROR("lttng_channel_channel zmalloc");
242 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
243 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
244 lkc
->channel
->attr
.extended
.ptr
= extended
;
248 * If we receive an empty string for channel name, it means the
249 * default channel name is requested.
251 if (chan
->name
[0] == '\0') {
252 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
253 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(
284 struct lttng_kernel_abi_context
*ctx
)
286 struct ltt_kernel_context
*kctx
;
288 kctx
= zmalloc(sizeof(*kctx
));
290 PERROR("zmalloc kernel context");
295 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
302 * Allocate and init a kernel context object from an existing kernel context
305 * Return the allocated object or NULL on error.
307 struct ltt_kernel_context
*trace_kernel_copy_context(
308 struct ltt_kernel_context
*kctx
)
310 struct ltt_kernel_context
*kctx_copy
;
313 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
315 PERROR("zmalloc ltt_kernel_context");
319 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
320 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
327 * Allocate and initialize a kernel event. Set name and event type.
328 * We own filter_expression, and filter.
330 * Return pointer to structure or NULL.
332 enum lttng_error_code
trace_kernel_create_event(
333 struct lttng_event
*ev
, char *filter_expression
,
334 struct lttng_bytecode
*filter
,
335 struct ltt_kernel_event
**kernel_event
)
337 enum lttng_error_code ret
;
338 struct lttng_kernel_abi_event
*attr
;
339 struct ltt_kernel_event
*local_kernel_event
;
340 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
344 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
345 attr
= zmalloc(sizeof(struct lttng_kernel_abi_event
));
346 if (local_kernel_event
== NULL
|| attr
== NULL
) {
347 PERROR("kernel event zmalloc");
348 ret
= LTTNG_ERR_NOMEM
;
353 case LTTNG_EVENT_PROBE
:
354 attr
->instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
355 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
356 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
357 strncpy(attr
->u
.kprobe
.symbol_name
,
358 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
359 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
361 case LTTNG_EVENT_USERSPACE_PROBE
:
363 const struct lttng_userspace_probe_location
* location
= NULL
;
364 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
366 location
= lttng_event_get_userspace_probe_location(ev
);
368 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
373 * From this point on, the specific term 'uprobe' is used
374 * instead of the generic 'userspace probe' because it's the
375 * technology used at the moment for this instrumentation.
376 * LTTng currently implements userspace probes using uprobes.
377 * In the interactions with the kernel tracer, we use the
380 attr
->instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
382 lookup
= lttng_userspace_probe_location_get_lookup_method(
385 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
390 * From the kernel tracer's perspective, all userspace probe
391 * event types are all the same: a file and an offset.
393 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
394 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
395 /* Get the file descriptor on the target binary. */
397 lttng_userspace_probe_location_function_get_binary_fd(location
);
400 * Save a reference to the probe location used during
401 * the listing of events.
403 userspace_probe_location
=
404 lttng_userspace_probe_location_copy(location
);
406 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
407 /* Get the file descriptor on the target binary. */
409 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
412 * Save a reference to the probe location used during the listing of
415 userspace_probe_location
=
416 lttng_userspace_probe_location_copy(location
);
419 DBG("Unsupported lookup method type");
420 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
425 case LTTNG_EVENT_FUNCTION
:
426 attr
->instrumentation
= LTTNG_KERNEL_ABI_KRETPROBE
;
427 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
428 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
429 strncpy(attr
->u
.kretprobe
.symbol_name
,
430 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
431 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
433 case LTTNG_EVENT_FUNCTION_ENTRY
:
434 attr
->instrumentation
= LTTNG_KERNEL_ABI_FUNCTION
;
435 strncpy(attr
->u
.ftrace
.symbol_name
,
436 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
437 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
439 case LTTNG_EVENT_TRACEPOINT
:
440 attr
->instrumentation
= LTTNG_KERNEL_ABI_TRACEPOINT
;
442 case LTTNG_EVENT_SYSCALL
:
443 attr
->instrumentation
= LTTNG_KERNEL_ABI_SYSCALL
;
444 attr
->u
.syscall
.abi
= LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
445 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
446 attr
->u
.syscall
.match
= LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
448 case LTTNG_EVENT_ALL
:
449 attr
->instrumentation
= LTTNG_KERNEL_ABI_ALL
;
452 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
453 ret
= LTTNG_ERR_INVALID
;
457 /* Copy event name */
458 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
459 attr
->name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
461 /* Setting up a kernel event */
462 local_kernel_event
->fd
= -1;
463 local_kernel_event
->event
= attr
;
464 local_kernel_event
->enabled
= 1;
465 local_kernel_event
->filter_expression
= filter_expression
;
466 local_kernel_event
->filter
= filter
;
467 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
469 *kernel_event
= local_kernel_event
;
474 free(filter_expression
);
476 free(local_kernel_event
);
482 * Allocate and initialize a kernel token event rule.
484 * Return pointer to structure or NULL.
486 enum lttng_error_code
trace_kernel_create_event_notifier_rule(
487 struct lttng_trigger
*trigger
,
489 uint64_t error_counter_index
,
490 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
492 enum lttng_error_code ret
= LTTNG_OK
;
493 enum lttng_condition_type condition_type
;
494 enum lttng_event_rule_type event_rule_type
;
495 enum lttng_condition_status condition_status
;
496 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
497 const struct lttng_condition
*condition
= NULL
;
498 const struct lttng_event_rule
*event_rule
= NULL
;
500 LTTNG_ASSERT(event_notifier_rule
);
502 condition
= lttng_trigger_get_const_condition(trigger
);
503 LTTNG_ASSERT(condition
);
505 condition_type
= lttng_condition_get_type(condition
);
506 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
508 condition_status
= lttng_condition_event_rule_matches_get_rule(
509 condition
, &event_rule
);
510 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
511 LTTNG_ASSERT(event_rule
);
513 event_rule_type
= lttng_event_rule_get_type(event_rule
);
514 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
516 local_kernel_token_event_rule
=
517 zmalloc(sizeof(struct ltt_kernel_event_notifier_rule
));
518 if (local_kernel_token_event_rule
== NULL
) {
519 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
520 ret
= LTTNG_ERR_NOMEM
;
524 local_kernel_token_event_rule
->fd
= -1;
525 local_kernel_token_event_rule
->enabled
= 1;
526 local_kernel_token_event_rule
->token
= token
;
527 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
529 /* Get the reference of the event rule. */
530 lttng_trigger_get(trigger
);
532 local_kernel_token_event_rule
->trigger
= trigger
;
533 /* The event rule still owns the filter and bytecode. */
534 local_kernel_token_event_rule
->filter
=
535 lttng_event_rule_get_filter_bytecode(event_rule
);
537 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
538 local_kernel_token_event_rule
->token
);
540 *event_notifier_rule
= local_kernel_token_event_rule
;
545 * Initialize a kernel trigger from an event rule.
547 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
548 const struct lttng_event_rule
*rule
,
549 struct lttng_kernel_abi_event_notifier
*kernel_event_notifier
)
551 enum lttng_error_code ret_code
;
555 switch (lttng_event_rule_get_type(rule
)) {
556 case LTTNG_EVENT_RULE_TYPE_KERNEL_KPROBE
:
558 uint64_t address
= 0, offset
= 0;
559 const char *symbol_name
= NULL
;
560 const struct lttng_kernel_probe_location
*location
= NULL
;
561 enum lttng_kernel_probe_location_status k_status
;
562 enum lttng_event_rule_status status
;
564 status
= lttng_event_rule_kernel_kprobe_get_location(rule
, &location
);
565 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
566 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
570 switch (lttng_kernel_probe_location_get_type(location
)) {
571 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
573 k_status
= lttng_kernel_probe_location_address_get_address(
575 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
578 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
580 k_status
= lttng_kernel_probe_location_symbol_get_offset(
582 LTTNG_ASSERT(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
583 symbol_name
= lttng_kernel_probe_location_symbol_get_name(
591 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_KPROBE
;
592 kernel_event_notifier
->event
.u
.kprobe
.addr
= address
;
593 kernel_event_notifier
->event
.u
.kprobe
.offset
= offset
;
595 strncpy_ret
= lttng_strncpy(
596 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
,
597 symbol_name
, LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
600 ret_code
= LTTNG_ERR_INVALID
;
605 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
607 status
= lttng_event_rule_kernel_kprobe_get_event_name(rule
, &name
);
608 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
612 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
614 const struct lttng_userspace_probe_location
* location
= NULL
;
615 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
616 enum lttng_event_rule_status status
;
618 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
619 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
620 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
624 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
626 lookup
= lttng_userspace_probe_location_get_lookup_method(
629 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
634 * From the kernel tracer's perspective, all userspace probe
635 * event types are all the same: a file and an offset.
637 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
638 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
639 /* Get the file descriptor on the target binary. */
640 kernel_event_notifier
->event
.u
.uprobe
.fd
=
641 lttng_userspace_probe_location_function_get_binary_fd(location
);
644 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
645 /* Get the file descriptor on the target binary. */
646 kernel_event_notifier
->event
.u
.uprobe
.fd
=
647 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
653 status
= lttng_event_rule_kernel_uprobe_get_event_name(
655 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
659 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
661 const enum lttng_event_rule_status status
=
662 lttng_event_rule_kernel_tracepoint_get_name_pattern(
665 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
666 kernel_event_notifier
->event
.instrumentation
=
667 LTTNG_KERNEL_ABI_TRACEPOINT
;
672 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
674 const enum lttng_event_rule_status status
=
675 lttng_event_rule_kernel_syscall_get_name_pattern(
677 const enum lttng_event_rule_kernel_syscall_emission_site
679 lttng_event_rule_kernel_syscall_get_emission_site(rule
);
680 enum lttng_kernel_abi_syscall_entryexit entryexit
;
682 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
683 LTTNG_ASSERT(emission_site
!= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
);
685 switch(emission_site
) {
686 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
687 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRY
;
689 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
690 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_EXIT
;
692 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
693 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
700 kernel_event_notifier
->event
.instrumentation
=
701 LTTNG_KERNEL_ABI_SYSCALL
;
702 kernel_event_notifier
->event
.u
.syscall
.abi
=
703 LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
704 kernel_event_notifier
->event
.u
.syscall
.entryexit
=
706 kernel_event_notifier
->event
.u
.syscall
.match
=
707 LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
716 strncpy_ret
= lttng_strncpy(kernel_event_notifier
->event
.name
, name
,
717 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
719 ret_code
= LTTNG_ERR_INVALID
;
727 * Allocate and initialize a kernel metadata.
729 * Return pointer to structure or NULL.
731 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
734 struct ltt_kernel_metadata
*lkm
;
735 struct lttng_channel
*chan
;
737 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
738 chan
= zmalloc(sizeof(struct lttng_channel
));
739 if (lkm
== NULL
|| chan
== NULL
) {
740 PERROR("kernel metadata zmalloc");
745 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
747 ERR("Failed to initialize metadata channel name to `%s`",
748 DEFAULT_METADATA_NAME
);
752 /* Set default attributes */
753 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
754 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
755 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
756 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
757 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
761 * The metadata channel of kernel sessions must use the "mmap"
762 * back-end since the consumer daemon accumulates complete
763 * metadata units before sending them to the relay daemon in
764 * live mode. The consumer daemon also needs to extract the contents
765 * of the metadata cache when computing a rotation position.
767 * In both cases, it is not possible to rely on the splice
768 * back-end as the consumer daemon may need to accumulate more
769 * content than can be backed by the ring buffer's underlying
772 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
773 chan
->attr
.tracefile_size
= 0;
774 chan
->attr
.tracefile_count
= 0;
775 chan
->attr
.live_timer_interval
= 0;
790 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
793 * Return pointer to structure or NULL.
795 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
799 struct ltt_kernel_stream
*lks
;
803 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
805 PERROR("kernel stream zmalloc");
810 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
812 PERROR("snprintf stream name");
815 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
829 * Cleanup kernel stream structure.
831 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
833 LTTNG_ASSERT(stream
);
835 DBG("[trace] Closing stream fd %d", stream
->fd
);
836 /* Close kernel fd */
837 if (stream
->fd
>= 0) {
840 ret
= close(stream
->fd
);
845 /* Remove from stream list */
846 cds_list_del(&stream
->list
);
852 * Cleanup kernel event structure.
854 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
858 if (event
->fd
>= 0) {
861 DBG("[trace] Closing event fd %d", event
->fd
);
862 /* Close kernel fd */
863 ret
= close(event
->fd
);
868 DBG("[trace] Tearing down event (no associated file descriptor)");
871 /* Remove from event list */
872 cds_list_del(&event
->list
);
874 free(event
->filter_expression
);
882 * Cleanup kernel event structure.
884 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
886 struct ltt_kernel_event_notifier_rule
*rule
= caa_container_of(rcu_node
,
887 struct ltt_kernel_event_notifier_rule
, rcu_node
);
892 void trace_kernel_destroy_event_notifier_rule(
893 struct ltt_kernel_event_notifier_rule
*event
)
897 if (event
->fd
>= 0) {
898 const int ret
= close(event
->fd
);
900 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
903 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
907 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
910 lttng_trigger_put(event
->trigger
);
911 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
914 * Cleanup kernel context structure.
916 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
921 cds_list_del(&ctx
->list
);
927 * Cleanup kernel channel structure.
929 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
931 struct ltt_kernel_stream
*stream
, *stmp
;
932 struct ltt_kernel_event
*event
, *etmp
;
933 struct ltt_kernel_context
*ctx
, *ctmp
;
935 enum lttng_error_code status
;
937 LTTNG_ASSERT(channel
);
939 DBG("[trace] Closing channel fd %d", channel
->fd
);
940 /* Close kernel fd */
941 if (channel
->fd
>= 0) {
942 ret
= close(channel
->fd
);
948 /* For each stream in the channel list */
949 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
950 trace_kernel_destroy_stream(stream
);
953 /* For each event in the channel list */
954 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
955 trace_kernel_destroy_event(event
);
958 /* For each context in the channel list */
959 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
960 trace_kernel_destroy_context(ctx
);
963 /* Remove from channel list */
964 cds_list_del(&channel
->list
);
966 if (the_notification_thread_handle
&&
967 channel
->published_to_notification_thread
) {
968 status
= notification_thread_command_remove_channel(
969 the_notification_thread_handle
, channel
->key
,
970 LTTNG_DOMAIN_KERNEL
);
971 LTTNG_ASSERT(status
== LTTNG_OK
);
973 free(channel
->channel
->attr
.extended
.ptr
);
974 free(channel
->channel
);
979 * Cleanup kernel metadata structure.
981 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
983 LTTNG_ASSERT(metadata
);
985 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
986 /* Close kernel fd */
987 if (metadata
->fd
>= 0) {
990 ret
= close(metadata
->fd
);
996 free(metadata
->conf
);
1001 * Cleanup kernel session structure
1003 * Should *NOT* be called with RCU read-side lock held.
1005 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
1007 struct ltt_kernel_channel
*channel
, *ctmp
;
1010 LTTNG_ASSERT(session
);
1012 DBG("[trace] Closing session fd %d", session
->fd
);
1013 /* Close kernel fds */
1014 if (session
->fd
>= 0) {
1015 ret
= close(session
->fd
);
1021 if (session
->metadata_stream_fd
>= 0) {
1022 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1023 ret
= close(session
->metadata_stream_fd
);
1029 if (session
->metadata
!= NULL
) {
1030 trace_kernel_destroy_metadata(session
->metadata
);
1033 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
1034 trace_kernel_destroy_channel(channel
);
1038 /* Free elements needed by destroy notifiers. */
1039 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1041 /* Wipe consumer output object */
1042 consumer_output_put(session
->consumer
);
1044 process_attr_tracker_destroy(session
->tracker_pid
);
1045 process_attr_tracker_destroy(session
->tracker_vpid
);
1046 process_attr_tracker_destroy(session
->tracker_uid
);
1047 process_attr_tracker_destroy(session
->tracker_vuid
);
1048 process_attr_tracker_destroy(session
->tracker_gid
);
1049 process_attr_tracker_destroy(session
->tracker_vgid
);