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-function.h>
17 #include <lttng/kernel-probe.h>
18 #include <lttng/userspace-probe.h>
19 #include <lttng/userspace-probe-internal.h>
20 #include <lttng/event-rule/event-rule.h>
21 #include <lttng/event-rule/event-rule-internal.h>
22 #include <lttng/event-rule/kernel-function.h>
23 #include <lttng/event-rule/kernel-function-internal.h>
24 #include <lttng/event-rule/kernel-probe.h>
25 #include <lttng/event-rule/kernel-probe-internal.h>
26 #include <lttng/event-rule/syscall.h>
27 #include <lttng/event-rule/syscall-internal.h>
28 #include <lttng/event-rule/tracepoint.h>
29 #include <lttng/event-rule/tracepoint-internal.h>
30 #include <lttng/event-rule/userspace-probe-internal.h>
31 #include <lttng/map/map.h>
32 #include <lttng/map-key-internal.h>
33 #include <common/common.h>
34 #include <common/defaults.h>
35 #include <common/trace-chunk.h>
36 #include <common/macros.h>
39 #include "trace-kernel.h"
40 #include "lttng-sessiond.h"
41 #include "notification-thread-commands.h"
43 /* Next available map key. Access under next_map_key_lock. */
44 static uint64_t _next_map_key
;
45 static pthread_mutex_t next_map_key_lock
= PTHREAD_MUTEX_INITIALIZER
;
48 * Return the incremented value of next_map_key.
50 static uint64_t get_next_map_key(void)
54 pthread_mutex_lock(&next_map_key_lock
);
55 ret
= ++_next_map_key
;
56 pthread_mutex_unlock(&next_map_key_lock
);
61 * Find the channel name for the given kernel session.
63 struct ltt_kernel_channel
*trace_kernel_get_channel_by_name(
64 const char *name
, struct ltt_kernel_session
*session
)
66 struct ltt_kernel_channel
*chan
;
72 * If we receive an empty string for channel name, it means the
73 * default channel name is requested.
76 name
= DEFAULT_CHANNEL_NAME
;
78 DBG("Trying to find channel %s", name
);
80 cds_list_for_each_entry(chan
, &session
->channel_list
.head
, list
) {
81 if (strcmp(name
, chan
->channel
->name
) == 0) {
82 DBG("Found channel by name %s", name
);
91 * Find the map name for the given kernel session.
93 struct ltt_kernel_map
*trace_kernel_get_map_by_name(
94 const char *name
, struct ltt_kernel_session
*session
)
96 struct ltt_kernel_map
*map
;
101 DBG("Trying to find map %s", name
);
103 cds_list_for_each_entry(map
, &session
->map_list
.head
, list
) {
104 enum lttng_map_status status
;
105 const char *cur_map_name
;
107 status
= lttng_map_get_name(map
->map
, &cur_map_name
);
108 assert(status
== LTTNG_MAP_STATUS_OK
);
110 if (strcmp(name
, cur_map_name
) == 0) {
111 DBG("Found map by name %s", name
);
120 * Find the event for the given channel or map.
122 struct ltt_kernel_event
*trace_kernel_find_event(
123 struct ltt_kernel_event_list
*events_list
,
124 uint64_t tracer_token
, char *name
,
125 enum lttng_event_type type
,
126 struct lttng_bytecode
*filter
)
128 struct ltt_kernel_event
*ev
;
133 cds_list_for_each_entry(ev
, &events_list
->head
, list
) {
134 if (ev
->event
->token
!= tracer_token
) {
137 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
140 if (strcmp(name
, ev
->event
->name
)) {
143 if ((ev
->filter
&& !filter
) || (!ev
->filter
&& filter
)) {
146 if (ev
->filter
&& filter
) {
147 if (ev
->filter
->len
!= filter
->len
||
148 memcmp(ev
->filter
->data
, filter
->data
,
157 DBG("Kernel event %s found", name
);
160 DBG("Kernel event %s not found", name
);
166 * Find the event name for the given channel.
168 struct ltt_kernel_event
*trace_kernel_get_event_by_name(
169 char *name
, struct ltt_kernel_channel
*channel
,
170 enum lttng_event_type type
)
172 struct ltt_kernel_event
*ev
;
178 cds_list_for_each_entry(ev
, &channel
->events_list
.head
, list
) {
179 if (type
!= LTTNG_EVENT_ALL
&& ev
->type
!= type
) {
182 if (strcmp(name
, ev
->event
->name
)) {
189 DBG("Found event %s for channel %s", name
,
190 channel
->channel
->name
);
198 * Allocate and initialize a kernel session data structure.
200 * Return pointer to structure or NULL.
202 struct ltt_kernel_session
*trace_kernel_create_session(void)
204 struct ltt_kernel_session
*lks
= NULL
;
206 /* Allocate a new ltt kernel session */
207 lks
= zmalloc(sizeof(struct ltt_kernel_session
));
209 PERROR("create kernel session zmalloc");
213 /* Init data structure */
215 lks
->metadata_stream_fd
= -1;
216 lks
->channel_count
= 0;
218 lks
->stream_count_global
= 0;
219 lks
->metadata
= NULL
;
220 CDS_INIT_LIST_HEAD(&lks
->channel_list
.head
);
221 CDS_INIT_LIST_HEAD(&lks
->map_list
.head
);
223 lks
->tracker_pid
= process_attr_tracker_create();
224 if (!lks
->tracker_pid
) {
227 lks
->tracker_vpid
= process_attr_tracker_create();
228 if (!lks
->tracker_vpid
) {
231 lks
->tracker_uid
= process_attr_tracker_create();
232 if (!lks
->tracker_uid
) {
235 lks
->tracker_vuid
= process_attr_tracker_create();
236 if (!lks
->tracker_vuid
) {
239 lks
->tracker_gid
= process_attr_tracker_create();
240 if (!lks
->tracker_gid
) {
243 lks
->tracker_vgid
= process_attr_tracker_create();
244 if (!lks
->tracker_vgid
) {
247 lks
->consumer
= consumer_create_output(CONSUMER_DST_LOCAL
);
248 if (lks
->consumer
== NULL
) {
255 process_attr_tracker_destroy(lks
->tracker_pid
);
256 process_attr_tracker_destroy(lks
->tracker_vpid
);
257 process_attr_tracker_destroy(lks
->tracker_uid
);
258 process_attr_tracker_destroy(lks
->tracker_vuid
);
259 process_attr_tracker_destroy(lks
->tracker_gid
);
260 process_attr_tracker_destroy(lks
->tracker_vgid
);
268 * Allocate and initialize a kernel channel data structure.
270 * Return pointer to structure or NULL.
272 struct ltt_kernel_channel
*trace_kernel_create_channel(
273 struct lttng_channel
*chan
)
275 struct ltt_kernel_channel
*lkc
;
276 struct lttng_channel_extended
*extended
= NULL
;
280 lkc
= zmalloc(sizeof(struct ltt_kernel_channel
));
282 PERROR("ltt_kernel_channel zmalloc");
286 lkc
->channel
= zmalloc(sizeof(struct lttng_channel
));
287 if (lkc
->channel
== NULL
) {
288 PERROR("lttng_channel zmalloc");
292 extended
= zmalloc(sizeof(struct lttng_channel_extended
));
294 PERROR("lttng_channel_channel zmalloc");
297 memcpy(lkc
->channel
, chan
, sizeof(struct lttng_channel
));
298 memcpy(extended
, chan
->attr
.extended
.ptr
, sizeof(struct lttng_channel_extended
));
299 lkc
->channel
->attr
.extended
.ptr
= extended
;
303 * If we receive an empty string for channel name, it means the
304 * default channel name is requested.
306 if (chan
->name
[0] == '\0') {
307 strncpy(lkc
->channel
->name
, DEFAULT_CHANNEL_NAME
,
308 sizeof(lkc
->channel
->name
));
310 lkc
->channel
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
313 lkc
->stream_count
= 0;
314 lkc
->event_count
= 0;
316 lkc
->published_to_notification_thread
= false;
317 /* Init linked list */
318 CDS_INIT_LIST_HEAD(&lkc
->events_list
.head
);
319 CDS_INIT_LIST_HEAD(&lkc
->stream_list
.head
);
320 CDS_INIT_LIST_HEAD(&lkc
->ctx_list
);
333 struct ltt_kernel_map
*trace_kernel_create_map(
334 const struct lttng_map
*map
)
336 struct ltt_kernel_map
*kernel_map
= NULL
;
337 unsigned int i
, number_dimensions
;
339 kernel_map
= zmalloc(sizeof(*kernel_map
));
341 PERROR("ltt_kernel_map zmalloc");
345 switch (lttng_map_get_boundary_policy(map
)) {
346 case LTTNG_MAP_BOUNDARY_POLICY_OVERFLOW
:
347 kernel_map
->counter_conf
.arithmetic
= LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR
;
353 switch (lttng_map_get_bitness(map
)) {
354 case LTTNG_MAP_BITNESS_32BITS
:
355 kernel_map
->counter_conf
.bitness
= LTTNG_KERNEL_COUNTER_BITNESS_32
;
357 case LTTNG_MAP_BITNESS_64BITS
:
358 kernel_map
->counter_conf
.bitness
= LTTNG_KERNEL_COUNTER_BITNESS_64
;
364 kernel_map
->counter_conf
.coalesce_hits
= lttng_map_get_coalesce_hits(map
);
366 number_dimensions
= lttng_map_get_dimension_count(map
);
367 assert(number_dimensions
<= LTTNG_KERNEL_COUNTER_DIMENSION_MAX
);
369 kernel_map
->counter_conf
.number_dimensions
= number_dimensions
;
371 for (i
= 0; i
< kernel_map
->counter_conf
.number_dimensions
; i
++) {
372 enum lttng_map_status map_status
;
373 uint64_t dimension_length
;
375 map_status
= lttng_map_get_dimension_length(map
, i
,
377 assert(map_status
== LTTNG_MAP_STATUS_OK
);
379 kernel_map
->counter_conf
.dimensions
[i
].size
= dimension_length
;
381 //FIXME: We need to turn on overflow and underflow
382 kernel_map
->counter_conf
.dimensions
[i
].has_overflow
= false;
383 kernel_map
->counter_conf
.dimensions
[i
].has_underflow
= false;
387 kernel_map
->enabled
= 1;
388 kernel_map
->key
= get_next_map_key();
390 kernel_map
->event_counters_ht
= lttng_ht_new(0, LTTNG_HT_TYPE_U64
);
396 * Allocate and init a kernel context object.
398 * Return the allocated object or NULL on error.
400 struct ltt_kernel_context
*trace_kernel_create_context(
401 struct lttng_kernel_context
*ctx
)
403 struct ltt_kernel_context
*kctx
;
405 kctx
= zmalloc(sizeof(*kctx
));
407 PERROR("zmalloc kernel context");
412 memcpy(&kctx
->ctx
, ctx
, sizeof(kctx
->ctx
));
419 * Allocate and init a kernel context object from an existing kernel context
422 * Return the allocated object or NULL on error.
424 struct ltt_kernel_context
*trace_kernel_copy_context(
425 struct ltt_kernel_context
*kctx
)
427 struct ltt_kernel_context
*kctx_copy
;
430 kctx_copy
= zmalloc(sizeof(*kctx_copy
));
432 PERROR("zmalloc ltt_kernel_context");
436 memcpy(kctx_copy
, kctx
, sizeof(*kctx_copy
));
437 memset(&kctx_copy
->list
, 0, sizeof(kctx_copy
->list
));
444 * Allocate and initialize a kernel event. Set name and event type.
445 * We own filter_expression, and filter.
447 * Return pointer to structure or NULL.
449 enum lttng_error_code
trace_kernel_create_event(
450 struct lttng_event
*ev
, char *filter_expression
,
451 struct lttng_bytecode
*filter
,
452 struct ltt_kernel_event
**kernel_event
)
454 enum lttng_error_code ret
;
455 struct lttng_kernel_event
*attr
;
456 struct ltt_kernel_event
*local_kernel_event
;
457 struct lttng_userspace_probe_location
*userspace_probe_location
= NULL
;
461 local_kernel_event
= zmalloc(sizeof(struct ltt_kernel_event
));
462 attr
= zmalloc(sizeof(struct lttng_kernel_event
));
463 if (local_kernel_event
== NULL
|| attr
== NULL
) {
464 PERROR("kernel event zmalloc");
465 ret
= LTTNG_ERR_NOMEM
;
470 case LTTNG_EVENT_PROBE
:
471 attr
->instrumentation
= LTTNG_KERNEL_KPROBE
;
472 attr
->u
.kprobe
.addr
= ev
->attr
.probe
.addr
;
473 attr
->u
.kprobe
.offset
= ev
->attr
.probe
.offset
;
474 strncpy(attr
->u
.kprobe
.symbol_name
,
475 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
476 attr
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
478 case LTTNG_EVENT_USERSPACE_PROBE
:
480 const struct lttng_userspace_probe_location
* location
= NULL
;
481 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
483 location
= lttng_event_get_userspace_probe_location(ev
);
485 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
490 * From this point on, the specific term 'uprobe' is used
491 * instead of the generic 'userspace probe' because it's the
492 * technology used at the moment for this instrumentation.
493 * LTTng currently implements userspace probes using uprobes.
494 * In the interactions with the kernel tracer, we use the
497 attr
->instrumentation
= LTTNG_KERNEL_UPROBE
;
499 lookup
= lttng_userspace_probe_location_get_lookup_method(
502 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
507 * From the kernel tracer's perspective, all userspace probe
508 * event types are all the same: a file and an offset.
510 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
511 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
512 /* Get the file descriptor on the target binary. */
514 lttng_userspace_probe_location_function_get_binary_fd(location
);
517 * Save a reference to the probe location used during
518 * the listing of events.
520 userspace_probe_location
=
521 lttng_userspace_probe_location_copy(location
);
523 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
524 /* Get the file descriptor on the target binary. */
526 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
529 * Save a reference to the probe location used during the listing of
532 userspace_probe_location
=
533 lttng_userspace_probe_location_copy(location
);
536 DBG("Unsupported lookup method type");
537 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
542 case LTTNG_EVENT_FUNCTION
:
543 attr
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
544 attr
->u
.kretprobe
.addr
= ev
->attr
.probe
.addr
;
545 attr
->u
.kretprobe
.offset
= ev
->attr
.probe
.offset
;
546 strncpy(attr
->u
.kretprobe
.symbol_name
,
547 ev
->attr
.probe
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
548 attr
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
550 case LTTNG_EVENT_FUNCTION_ENTRY
:
551 attr
->instrumentation
= LTTNG_KERNEL_FUNCTION
;
552 strncpy(attr
->u
.ftrace
.symbol_name
,
553 ev
->attr
.ftrace
.symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
554 attr
->u
.ftrace
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
556 case LTTNG_EVENT_TRACEPOINT
:
557 attr
->instrumentation
= LTTNG_KERNEL_TRACEPOINT
;
559 case LTTNG_EVENT_SYSCALL
:
560 attr
->instrumentation
= LTTNG_KERNEL_SYSCALL
;
561 attr
->u
.syscall
.abi
= LTTNG_KERNEL_SYSCALL_ABI_ALL
;
562 attr
->u
.syscall
.entryexit
= LTTNG_KERNEL_SYSCALL_ENTRYEXIT
;
563 attr
->u
.syscall
.match
= LTTNG_KERNEL_SYSCALL_MATCH_NAME
;
565 case LTTNG_EVENT_ALL
:
566 attr
->instrumentation
= LTTNG_KERNEL_ALL
;
569 ERR("Unknown kernel instrumentation type (%d)", ev
->type
);
570 ret
= LTTNG_ERR_INVALID
;
574 /* Copy event name */
575 strncpy(attr
->name
, ev
->name
, LTTNG_KERNEL_SYM_NAME_LEN
);
576 attr
->name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
578 /* Setting up a kernel event */
579 local_kernel_event
->fd
= -1;
580 local_kernel_event
->event
= attr
;
581 local_kernel_event
->enabled
= 1;
582 local_kernel_event
->filter_expression
= filter_expression
;
583 local_kernel_event
->filter
= filter
;
584 local_kernel_event
->userspace_probe_location
= userspace_probe_location
;
586 *kernel_event
= local_kernel_event
;
591 free(filter_expression
);
593 free(local_kernel_event
);
599 * Allocate and initialize a kernel token event rule.
601 * Return pointer to structure or NULL.
603 enum lttng_error_code
trace_kernel_create_event_notifier_rule(
604 struct lttng_trigger
*trigger
,
606 uint64_t error_counter_index
,
607 struct ltt_kernel_event_notifier_rule
**event_notifier_rule
)
609 enum lttng_error_code ret
= LTTNG_OK
;
610 enum lttng_condition_type condition_type
;
611 enum lttng_event_rule_type event_rule_type
;
612 enum lttng_condition_status condition_status
;
613 struct ltt_kernel_event_notifier_rule
*local_kernel_token_event_rule
;
614 const struct lttng_condition
*condition
= NULL
;
615 const struct lttng_event_rule
*event_rule
= NULL
;
617 assert(event_notifier_rule
);
619 condition
= lttng_trigger_get_condition(trigger
);
622 condition_type
= lttng_condition_get_type(condition
);
623 assert(condition_type
== LTTNG_CONDITION_TYPE_ON_EVENT
);
625 condition_status
= lttng_condition_on_event_get_rule(
626 condition
, &event_rule
);
627 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
630 event_rule_type
= lttng_event_rule_get_type(event_rule
);
631 assert(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
633 local_kernel_token_event_rule
=
634 zmalloc(sizeof(struct ltt_kernel_event_notifier_rule
));
635 if (local_kernel_token_event_rule
== NULL
) {
636 PERROR("Failed to allocate ltt_kernel_token_event_rule structure");
637 ret
= LTTNG_ERR_NOMEM
;
641 local_kernel_token_event_rule
->fd
= -1;
642 local_kernel_token_event_rule
->enabled
= 1;
643 local_kernel_token_event_rule
->token
= token
;
644 local_kernel_token_event_rule
->error_counter_index
= error_counter_index
;
646 /* Get the reference of the event rule. */
647 lttng_trigger_get(trigger
);
649 local_kernel_token_event_rule
->trigger
= trigger
;
650 /* The event rule still owns the filter and bytecode. */
651 local_kernel_token_event_rule
->filter
=
652 lttng_event_rule_get_filter_bytecode(event_rule
);
654 DBG3("Created kernel event notifier rule: token = %" PRIu64
,
655 local_kernel_token_event_rule
->token
);
657 *event_notifier_rule
= local_kernel_token_event_rule
;
661 enum trace_kernel_event_type
{
662 TRACE_KERNEL_EVENT_TYPE_NOTIFIER
,
663 TRACE_KERNEL_EVENT_TYPE_COUNTER
,
667 * Initialize a kernel event from an event rule.
670 enum lttng_error_code
trace_kernel_init_event_from_event_rule(
671 const struct lttng_event_rule
*rule
,
672 struct lttng_kernel_event
*kernel_event
,
673 enum trace_kernel_event_type event_type
)
675 enum lttng_error_code ret_code
;
679 switch (lttng_event_rule_get_type(rule
)) {
680 case LTTNG_EVENT_RULE_TYPE_KERNEL_PROBE
:
682 uint64_t address
= 0, offset
= 0;
683 const char *symbol_name
= NULL
;
684 const struct lttng_kernel_probe_location
*location
= NULL
;
685 enum lttng_kernel_probe_location_status k_status
;
686 enum lttng_event_rule_status status
;
688 status
= lttng_event_rule_kernel_probe_get_location(rule
, &location
);
689 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
690 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
694 switch (lttng_kernel_probe_location_get_type(location
)) {
695 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_ADDRESS
:
697 k_status
= lttng_kernel_probe_location_address_get_address(
699 assert(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
702 case LTTNG_KERNEL_PROBE_LOCATION_TYPE_SYMBOL_OFFSET
:
704 k_status
= lttng_kernel_probe_location_symbol_get_offset(
706 assert(k_status
== LTTNG_KERNEL_PROBE_LOCATION_STATUS_OK
);
707 symbol_name
= lttng_kernel_probe_location_symbol_get_name(
715 kernel_event
->instrumentation
= LTTNG_KERNEL_KPROBE
;
716 kernel_event
->u
.kprobe
.addr
= address
;
717 kernel_event
->u
.kprobe
.offset
= offset
;
719 strncpy_ret
= lttng_strncpy(
720 kernel_event
->u
.kprobe
.symbol_name
,
721 symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
724 ret_code
= LTTNG_ERR_INVALID
;
729 kernel_event
->u
.kprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
731 status
= lttng_event_rule_kernel_probe_get_event_name(rule
, &name
);
732 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
736 case LTTNG_EVENT_RULE_TYPE_KERNEL_FUNCTION
:
738 uint64_t address
= 0, offset
= 0;
739 const char *symbol_name
= NULL
;
740 const struct lttng_kernel_function_location
*location
= NULL
;
741 enum lttng_kernel_function_location_status k_status
;
742 enum lttng_event_rule_status status
;
744 status
= lttng_event_rule_kernel_function_get_location(rule
, &location
);
745 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
746 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
750 switch (lttng_kernel_function_location_get_type(location
)) {
751 case LTTNG_KERNEL_FUNCTION_LOCATION_TYPE_ADDRESS
:
753 k_status
= lttng_kernel_function_location_address_get_address(
755 assert(k_status
== LTTNG_KERNEL_FUNCTION_LOCATION_STATUS_OK
);
758 case LTTNG_KERNEL_FUNCTION_LOCATION_TYPE_SYMBOL_OFFSET
:
760 k_status
= lttng_kernel_function_location_symbol_get_offset(
762 assert(k_status
== LTTNG_KERNEL_FUNCTION_LOCATION_STATUS_OK
);
763 symbol_name
= lttng_kernel_function_location_symbol_get_name(
771 kernel_event
->instrumentation
= LTTNG_KERNEL_KRETPROBE
;
772 kernel_event
->u
.kretprobe
.addr
= address
;
773 kernel_event
->u
.kretprobe
.offset
= offset
;
775 strncpy_ret
= lttng_strncpy(
776 kernel_event
->u
.kretprobe
.symbol_name
,
777 symbol_name
, LTTNG_KERNEL_SYM_NAME_LEN
);
780 ret_code
= LTTNG_ERR_INVALID
;
785 kernel_event
->u
.kretprobe
.symbol_name
[LTTNG_KERNEL_SYM_NAME_LEN
- 1] = '\0';
787 status
= lttng_event_rule_kernel_function_get_event_name(rule
, &name
);
788 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
792 case LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
:
794 const struct lttng_userspace_probe_location
* location
= NULL
;
795 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
796 enum lttng_event_rule_status status
;
798 status
= lttng_event_rule_userspace_probe_get_location(rule
, &location
);
799 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
800 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
804 kernel_event
->instrumentation
= LTTNG_KERNEL_UPROBE
;
806 lookup
= lttng_userspace_probe_location_get_lookup_method(
809 ret_code
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
814 * From the kernel tracer's perspective, all userspace probe
815 * event types are all the same: a file and an offset.
817 switch (lttng_userspace_probe_location_lookup_method_get_type(lookup
)) {
818 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
819 /* Get the file descriptor on the target binary. */
820 kernel_event
->u
.uprobe
.fd
=
821 lttng_userspace_probe_location_function_get_binary_fd(location
);
824 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
825 /* Get the file descriptor on the target binary. */
826 kernel_event
->u
.uprobe
.fd
=
827 lttng_userspace_probe_location_tracepoint_get_binary_fd(location
);
833 status
= lttng_event_rule_userspace_probe_get_event_name(rule
, &name
);
834 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
838 case LTTNG_EVENT_RULE_TYPE_TRACEPOINT
:
840 const enum lttng_domain_type domain
=
841 lttng_event_rule_get_domain_type(rule
);
842 const enum lttng_event_rule_status status
=
843 lttng_event_rule_tracepoint_get_pattern(
846 assert(domain
== LTTNG_DOMAIN_KERNEL
);
847 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
848 kernel_event
->instrumentation
=
849 LTTNG_KERNEL_TRACEPOINT
;
854 case LTTNG_EVENT_RULE_TYPE_SYSCALL
:
856 const enum lttng_event_rule_status status
=
857 lttng_event_rule_syscall_get_pattern(
860 assert(status
== LTTNG_EVENT_RULE_STATUS_OK
);
862 kernel_event
->instrumentation
=
863 LTTNG_KERNEL_SYSCALL
;
864 kernel_event
->u
.syscall
.abi
=
865 LTTNG_KERNEL_SYSCALL_ABI_ALL
;
866 kernel_event
->u
.syscall
.match
=
867 LTTNG_KERNEL_SYSCALL_MATCH_NAME
;
868 switch (event_type
) {
869 case TRACE_KERNEL_EVENT_TYPE_COUNTER
:
870 kernel_event
->u
.syscall
.entryexit
=
871 LTTNG_KERNEL_SYSCALL_ENTRYEXIT
;
873 case TRACE_KERNEL_EVENT_TYPE_NOTIFIER
:
874 kernel_event
->u
.syscall
.entryexit
=
875 LTTNG_KERNEL_SYSCALL_ENTRY
;
886 strncpy_ret
= lttng_strncpy(kernel_event
->name
, name
,
887 LTTNG_KERNEL_SYM_NAME_LEN
);
889 ret_code
= LTTNG_ERR_INVALID
;
897 enum lttng_error_code
trace_kernel_init_event_notifier_from_event_rule(
898 const struct lttng_event_rule
*rule
,
899 struct lttng_kernel_event_notifier
*kernel_event_notifier
)
901 return trace_kernel_init_event_from_event_rule(rule
,
902 &kernel_event_notifier
->event
,
903 TRACE_KERNEL_EVENT_TYPE_NOTIFIER
);
906 enum lttng_error_code
trace_kernel_init_event_counter_from_event_rule(
907 const struct lttng_event_rule
*rule
,
908 struct lttng_kernel_counter_event
*kernel_counter_event
)
910 return trace_kernel_init_event_from_event_rule(rule
,
911 &kernel_counter_event
->event
,
912 TRACE_KERNEL_EVENT_TYPE_COUNTER
);
916 * Allocate and initialize a kernel metadata.
918 * Return pointer to structure or NULL.
920 struct ltt_kernel_metadata
*trace_kernel_create_metadata(void)
923 struct ltt_kernel_metadata
*lkm
;
924 struct lttng_channel
*chan
;
926 lkm
= zmalloc(sizeof(struct ltt_kernel_metadata
));
927 chan
= zmalloc(sizeof(struct lttng_channel
));
928 if (lkm
== NULL
|| chan
== NULL
) {
929 PERROR("kernel metadata zmalloc");
934 chan
->name
, DEFAULT_METADATA_NAME
, sizeof(chan
->name
));
936 ERR("Failed to initialize metadata channel name to `%s`",
937 DEFAULT_METADATA_NAME
);
941 /* Set default attributes */
942 chan
->attr
.overwrite
= DEFAULT_METADATA_OVERWRITE
;
943 chan
->attr
.subbuf_size
= default_get_metadata_subbuf_size();
944 chan
->attr
.num_subbuf
= DEFAULT_METADATA_SUBBUF_NUM
;
945 chan
->attr
.switch_timer_interval
= DEFAULT_METADATA_SWITCH_TIMER
;
946 chan
->attr
.read_timer_interval
= DEFAULT_METADATA_READ_TIMER
;;
950 * The metadata channel of kernel sessions must use the "mmap"
951 * back-end since the consumer daemon accumulates complete
952 * metadata units before sending them to the relay daemon in
953 * live mode. The consumer daemon also needs to extract the contents
954 * of the metadata cache when computing a rotation position.
956 * In both cases, it is not possible to rely on the splice
957 * back-end as the consumer daemon may need to accumulate more
958 * content than can be backed by the ring buffer's underlying
961 chan
->attr
.output
= LTTNG_EVENT_MMAP
;
962 chan
->attr
.tracefile_size
= 0;
963 chan
->attr
.tracefile_count
= 0;
964 chan
->attr
.live_timer_interval
= 0;
979 * Allocate and initialize a kernel stream. The stream is set to ACTIVE_FD by
982 * Return pointer to structure or NULL.
984 struct ltt_kernel_stream
*trace_kernel_create_stream(const char *name
,
988 struct ltt_kernel_stream
*lks
;
992 lks
= zmalloc(sizeof(struct ltt_kernel_stream
));
994 PERROR("kernel stream zmalloc");
999 ret
= snprintf(lks
->name
, sizeof(lks
->name
), "%s_%u", name
, count
);
1001 PERROR("snprintf stream name");
1004 lks
->name
[sizeof(lks
->name
) - 1] = '\0';
1018 * Cleanup kernel stream structure.
1020 void trace_kernel_destroy_stream(struct ltt_kernel_stream
*stream
)
1024 DBG("[trace] Closing stream fd %d", stream
->fd
);
1025 /* Close kernel fd */
1026 if (stream
->fd
>= 0) {
1029 ret
= close(stream
->fd
);
1034 /* Remove from stream list */
1035 cds_list_del(&stream
->list
);
1041 * Cleanup kernel event structure.
1043 void trace_kernel_destroy_event(struct ltt_kernel_event
*event
)
1047 if (event
->fd
>= 0) {
1050 DBG("[trace] Closing event fd %d", event
->fd
);
1051 /* Close kernel fd */
1052 ret
= close(event
->fd
);
1057 DBG("[trace] Tearing down event (no associated file descriptor)");
1060 /* Remove from event list */
1061 cds_list_del(&event
->list
);
1063 free(event
->filter_expression
);
1064 free(event
->filter
);
1071 * Free kernel event counter structure from RCU context
1073 static void free_event_counter_rcu(struct rcu_head
*rcu_node
)
1075 struct ltt_kernel_event_counter
*event_counter
= caa_container_of(rcu_node
,
1076 struct ltt_kernel_event_counter
, rcu_node
);
1078 free(event_counter
);
1081 * Cleanup kernel event counter structure.
1083 void trace_kernel_destroy_event_counter(struct ltt_kernel_event_counter
*event_counter
)
1085 assert(event_counter
);
1087 if (event_counter
->fd
>= 0) {
1090 DBG("[trace] Closing event counter fd %d", event_counter
->fd
);
1091 /* Close kernel fd */
1092 ret
= close(event_counter
->fd
);
1097 DBG("[trace] Tearing down event counter (no associated file descriptor)");
1100 lttng_map_key_put(event_counter
->key
);
1101 call_rcu(&event_counter
->rcu_node
, free_event_counter_rcu
);
1106 * Cleanup kernel event structure.
1108 static void free_token_event_rule_rcu(struct rcu_head
*rcu_node
)
1110 struct ltt_kernel_event_notifier_rule
*rule
= caa_container_of(rcu_node
,
1111 struct ltt_kernel_event_notifier_rule
, rcu_node
);
1116 void trace_kernel_destroy_event_notifier_rule(
1117 struct ltt_kernel_event_notifier_rule
*event
)
1121 if (event
->fd
>= 0) {
1122 const int ret
= close(event
->fd
);
1124 DBG("Closing kernel event notifier rule file descriptor: fd = %d",
1127 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
1131 DBG("Destroying kernel event notifier rule (no associated file descriptor)");
1134 lttng_trigger_put(event
->trigger
);
1135 call_rcu(&event
->rcu_node
, free_token_event_rule_rcu
);
1138 * Cleanup kernel context structure.
1140 void trace_kernel_destroy_context(struct ltt_kernel_context
*ctx
)
1145 cds_list_del(&ctx
->list
);
1151 * Cleanup kernel channel structure.
1153 void trace_kernel_destroy_channel(struct ltt_kernel_channel
*channel
)
1155 struct ltt_kernel_stream
*stream
, *stmp
;
1156 struct ltt_kernel_event
*event
, *etmp
;
1157 struct ltt_kernel_context
*ctx
, *ctmp
;
1159 enum lttng_error_code status
;
1163 DBG("[trace] Closing channel fd %d", channel
->fd
);
1164 /* Close kernel fd */
1165 if (channel
->fd
>= 0) {
1166 ret
= close(channel
->fd
);
1172 /* For each stream in the channel list */
1173 cds_list_for_each_entry_safe(stream
, stmp
, &channel
->stream_list
.head
, list
) {
1174 trace_kernel_destroy_stream(stream
);
1177 /* For each event in the channel list */
1178 cds_list_for_each_entry_safe(event
, etmp
, &channel
->events_list
.head
, list
) {
1179 trace_kernel_destroy_event(event
);
1182 /* For each context in the channel list */
1183 cds_list_for_each_entry_safe(ctx
, ctmp
, &channel
->ctx_list
, list
) {
1184 trace_kernel_destroy_context(ctx
);
1187 /* Remove from channel list */
1188 cds_list_del(&channel
->list
);
1190 if (notification_thread_handle
1191 && channel
->published_to_notification_thread
) {
1192 status
= notification_thread_command_remove_channel(
1193 notification_thread_handle
,
1194 channel
->key
, LTTNG_DOMAIN_KERNEL
);
1195 assert(status
== LTTNG_OK
);
1197 free(channel
->channel
->attr
.extended
.ptr
);
1198 free(channel
->channel
);
1203 * Cleanup kernel map structure.
1205 void trace_kernel_destroy_map(struct ltt_kernel_map
*map
)
1208 struct ltt_kernel_event_counter
*event_counter
;
1209 struct lttng_ht_iter iter
;
1213 DBG("[trace] Closing map fd %d", map
->fd
);
1214 /* Close kernel fd */
1216 ret
= close(map
->fd
);
1222 /* For each event counter in the map hashtable */
1223 cds_lfht_for_each_entry(map
->event_counters_ht
->ht
, &iter
.iter
,
1224 event_counter
, ht_node
.node
) {
1225 trace_kernel_destroy_event_counter(event_counter
);
1228 /* Remove from map list */
1229 cds_list_del(&map
->list
);
1235 * Cleanup kernel metadata structure.
1237 void trace_kernel_destroy_metadata(struct ltt_kernel_metadata
*metadata
)
1241 DBG("[trace] Closing metadata fd %d", metadata
->fd
);
1242 /* Close kernel fd */
1243 if (metadata
->fd
>= 0) {
1246 ret
= close(metadata
->fd
);
1252 free(metadata
->conf
);
1257 * Cleanup kernel session structure
1259 * Should *NOT* be called with RCU read-side lock held.
1261 void trace_kernel_destroy_session(struct ltt_kernel_session
*session
)
1263 struct ltt_kernel_channel
*channel
, *ctmp
;
1264 struct ltt_kernel_map
*map
, *map_tmp
;
1269 DBG("[trace] Closing session fd %d", session
->fd
);
1270 /* Close kernel fds */
1271 if (session
->fd
>= 0) {
1272 ret
= close(session
->fd
);
1278 if (session
->metadata_stream_fd
>= 0) {
1279 DBG("[trace] Closing metadata stream fd %d", session
->metadata_stream_fd
);
1280 ret
= close(session
->metadata_stream_fd
);
1286 if (session
->metadata
!= NULL
) {
1287 trace_kernel_destroy_metadata(session
->metadata
);
1290 cds_list_for_each_entry_safe(channel
, ctmp
, &session
->channel_list
.head
, list
) {
1291 trace_kernel_destroy_channel(channel
);
1294 cds_list_for_each_entry_safe(map
, map_tmp
, &session
->map_list
.head
, list
) {
1295 trace_kernel_destroy_map(map
);
1299 /* Free elements needed by destroy notifiers. */
1300 void trace_kernel_free_session(struct ltt_kernel_session
*session
)
1302 /* Wipe consumer output object */
1303 consumer_output_put(session
->consumer
);
1305 process_attr_tracker_destroy(session
->tracker_pid
);
1306 process_attr_tracker_destroy(session
->tracker_vpid
);
1307 process_attr_tracker_destroy(session
->tracker_uid
);
1308 process_attr_tracker_destroy(session
->tracker_vuid
);
1309 process_attr_tracker_destroy(session
->tracker_gid
);
1310 process_attr_tracker_destroy(session
->tracker_vgid
);