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
= zmalloc
<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
<ltt_kernel_channel
>();
227 PERROR("ltt_kernel_channel zmalloc");
231 lkc
->channel
= zmalloc
<lttng_channel
>();
232 if (lkc
->channel
== NULL
) {
233 PERROR("lttng_channel zmalloc");
237 extended
= zmalloc
<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
<ltt_kernel_context
>();
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
<ltt_kernel_context
>();
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
<ltt_kernel_event
>();
345 attr
= zmalloc
<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
<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
,
598 sizeof(kernel_event_notifier
->event
.u
.kprobe
.symbol_name
));
601 ret_code
= LTTNG_ERR_INVALID
;
606 kernel_event_notifier
->event
.u
.kprobe
.symbol_name
[LTTNG_KERNEL_ABI_SYM_NAME_LEN
- 1] = '\0';
608 status
= lttng_event_rule_kernel_kprobe_get_event_name(rule
, &name
);
609 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
613 case LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
:
615 const struct lttng_userspace_probe_location
* location
= NULL
;
616 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
617 enum lttng_event_rule_status status
;
619 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
620 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
621 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
625 kernel_event_notifier
->event
.instrumentation
= LTTNG_KERNEL_ABI_UPROBE
;
627 lookup
= lttng_userspace_probe_location_get_lookup_method(
630 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
635 * From the kernel tracer's perspective, all userspace probe
636 * event types are all the same: a file and an offset.
638 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
639 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
640 /* Get the file descriptor on the target binary. */
641 kernel_event_notifier
->event
.u
.uprobe
.fd
=
642 lttng_userspace_probe_location_function_get_binary_fd(location
);
645 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
646 /* Get the file descriptor on the target binary. */
647 kernel_event_notifier
->event
.u
.uprobe
.fd
=
648 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
654 status
= lttng_event_rule_kernel_uprobe_get_event_name(
656 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
660 case LTTNG_EVENT_RULE_TYPE_KERNEL_TRACEPOINT
:
662 const enum lttng_event_rule_status status
=
663 lttng_event_rule_kernel_tracepoint_get_name_pattern(
666 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
667 kernel_event_notifier
->event
.instrumentation
=
668 LTTNG_KERNEL_ABI_TRACEPOINT
;
673 case LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
:
675 const enum lttng_event_rule_status status
=
676 lttng_event_rule_kernel_syscall_get_name_pattern(
678 const enum lttng_event_rule_kernel_syscall_emission_site
680 lttng_event_rule_kernel_syscall_get_emission_site(rule
);
681 enum lttng_kernel_abi_syscall_entryexit entryexit
;
683 LTTNG_ASSERT(status
== LTTNG_EVENT_RULE_STATUS_OK
);
684 LTTNG_ASSERT(emission_site
!= LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
);
686 switch(emission_site
) {
687 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
688 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRY
;
690 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
691 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_EXIT
;
693 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
694 entryexit
= LTTNG_KERNEL_ABI_SYSCALL_ENTRYEXIT
;
701 kernel_event_notifier
->event
.instrumentation
=
702 LTTNG_KERNEL_ABI_SYSCALL
;
703 kernel_event_notifier
->event
.u
.syscall
.abi
=
704 LTTNG_KERNEL_ABI_SYSCALL_ABI_ALL
;
705 kernel_event_notifier
->event
.u
.syscall
.entryexit
=
707 kernel_event_notifier
->event
.u
.syscall
.match
=
708 LTTNG_KERNEL_ABI_SYSCALL_MATCH_NAME
;
717 strncpy_ret
= lttng_strncpy(kernel_event_notifier
->event
.name
, name
,
718 LTTNG_KERNEL_ABI_SYM_NAME_LEN
);
720 ret_code
= LTTNG_ERR_INVALID
;
728 * Allocate and initialize a kernel metadata.
730 * Return pointer to structure or NULL.
732 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
735 struct ltt_kernel_metadata
*lkm
;
736 struct lttng_channel
*chan
;
738 lkm
= zmalloc
<ltt_kernel_metadata
>();
739 chan
= zmalloc
<lttng_channel
>();
740 if (lkm
== NULL
|| chan
== NULL
) {
741 PERROR("kernel metadata zmalloc");
746 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
748 ERR("Failed to initialize metadata channel name to `%s`",
749 DEFAULT_METADATA_NAME
);
753 /* Set default attributes */
754 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
755 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
756 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
757 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
758 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
762 * The metadata channel of kernel sessions must use the "mmap"
763 * back-end since the consumer daemon accumulates complete
764 * metadata units before sending them to the relay daemon in
765 * live mode. The consumer daemon also needs to extract the contents
766 * of the metadata cache when computing a rotation position.
768 * In both cases, it is not possible to rely on the splice
769 * back-end as the consumer daemon may need to accumulate more
770 * content than can be backed by the ring buffer's underlying
773 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
774 chan
->attr
.tracefile_size
= 0;
775 chan
->attr
.tracefile_count
= 0;
776 chan
->attr
.live_timer_interval
= 0;
791 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
794 * Return pointer to structure or NULL.
796 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
800 struct ltt_kernel_stream
*lks
;
804 lks
= zmalloc
<ltt_kernel_stream
>();
806 PERROR("kernel stream zmalloc");
811 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
813 PERROR("snprintf stream name");
816 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
830 * Cleanup kernel stream structure.
832 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
834 LTTNG_ASSERT(stream
);
836 DBG("[trace] Closing stream fd %d", stream
->fd
);
837 /* Close kernel fd */
838 if (stream
->fd
>= 0) {
841 ret
= close(stream
->fd
);
846 /* Remove from stream list */
847 cds_list_del(&stream
->list
);
853 * Cleanup kernel event structure.
855 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
859 if (event
->fd
>= 0) {
862 DBG("[trace] Closing event fd %d", event
->fd
);
863 /* Close kernel fd */
864 ret
= close(event
->fd
);
869 DBG("[trace] Tearing down event (no associated file descriptor)");
872 /* Remove from event list */
873 cds_list_del(&event
->list
);
875 free(event
->filter_expression
);
883 * Cleanup kernel event structure.
885 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
887 struct ltt_kernel_event_notifier_rule
*rule
= caa_container_of(rcu_node
,
888 struct ltt_kernel_event_notifier_rule
, rcu_node
);
893 void trace_kernel_destroy_event_notifier_rule(
894 struct ltt_kernel_event_notifier_rule
*event
)
898 if (event
->fd
>= 0) {
899 const int ret
= close(event
->fd
);
901 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
904 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
908 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
911 lttng_trigger_put(event
->trigger
);
912 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
915 * Cleanup kernel context structure.
917 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
922 cds_list_del(&ctx
->list
);
928 * Cleanup kernel channel structure.
930 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
932 struct ltt_kernel_stream
*stream
, *stmp
;
933 struct ltt_kernel_event
*event
, *etmp
;
934 struct ltt_kernel_context
*ctx
, *ctmp
;
936 enum lttng_error_code status
;
938 LTTNG_ASSERT(channel
);
940 DBG("[trace] Closing channel fd %d", channel
->fd
);
941 /* Close kernel fd */
942 if (channel
->fd
>= 0) {
943 ret
= close(channel
->fd
);
949 /* For each stream in the channel list */
950 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
951 trace_kernel_destroy_stream(stream
);
954 /* For each event in the channel list */
955 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
956 trace_kernel_destroy_event(event
);
959 /* For each context in the channel list */
960 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
961 trace_kernel_destroy_context(ctx
);
964 /* Remove from channel list */
965 cds_list_del(&channel
->list
);
967 if (the_notification_thread_handle
&&
968 channel
->published_to_notification_thread
) {
969 status
= notification_thread_command_remove_channel(
970 the_notification_thread_handle
, channel
->key
,
971 LTTNG_DOMAIN_KERNEL
);
972 LTTNG_ASSERT(status
== LTTNG_OK
);
974 free(channel
->channel
->attr
.extended
.ptr
);
975 free(channel
->channel
);
980 * Cleanup kernel metadata structure.
982 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
984 LTTNG_ASSERT(metadata
);
986 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
987 /* Close kernel fd */
988 if (metadata
->fd
>= 0) {
991 ret
= close(metadata
->fd
);
997 free(metadata
->conf
);
1002 * Cleanup kernel session structure
1004 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
1006 struct ltt_kernel_channel
*channel
, *ctmp
;
1009 LTTNG_ASSERT(session
);
1011 DBG("[trace] Closing session fd %d", session
->fd
);
1012 /* Close kernel fds */
1013 if (session
->fd
>= 0) {
1014 ret
= close(session
->fd
);
1020 if (session
->metadata_stream_fd
>= 0) {
1021 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1022 ret
= close(session
->metadata_stream_fd
);
1028 if (session
->metadata
!= NULL
) {
1029 trace_kernel_destroy_metadata(session
->metadata
);
1032 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
1033 trace_kernel_destroy_channel(channel
);
1037 /* Free elements needed by destroy notifiers. */
1038 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1040 /* Wipe consumer output object */
1041 consumer_output_put(session
->consumer
);
1043 process_attr_tracker_destroy(session
->tracker_pid
);
1044 process_attr_tracker_destroy(session
->tracker_vpid
);
1045 process_attr_tracker_destroy(session
->tracker_uid
);
1046 process_attr_tracker_destroy(session
->tracker_vuid
);
1047 process_attr_tracker_destroy(session
->tracker_gid
);
1048 process_attr_tracker_destroy(session
->tracker_vgid
);