2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "condition-internal.hpp"
10 #include "consumer.hpp"
11 #include "event-notifier-error-accounting.hpp"
12 #include "kern-modules.hpp"
13 #include "kernel-consumer.hpp"
15 #include "lttng-sessiond.hpp"
16 #include "lttng-syscall.hpp"
17 #include "modprobe.hpp"
18 #include "notification-thread-commands.hpp"
20 #include "sessiond-config.hpp"
21 #include "tracker.hpp"
24 #include <common/common.hpp>
25 #include <common/hashtable/utils.hpp>
26 #include <common/kernel-ctl/kernel-ctl.hpp>
27 #include <common/kernel-ctl/kernel-ioctl.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/trace-chunk.hpp>
30 #include <common/tracker.hpp>
31 #include <common/utils.hpp>
33 #include <lttng/condition/event-rule-matches-internal.hpp>
34 #include <lttng/condition/event-rule-matches.h>
35 #include <lttng/event-rule/event-rule-internal.hpp>
36 #include <lttng/event-rule/event-rule.h>
37 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
38 #include <lttng/event.h>
39 #include <lttng/lttng-error.h>
40 #include <lttng/tracker.h>
41 #include <lttng/userspace-probe-internal.hpp>
42 #include <lttng/userspace-probe.h>
49 #include <sys/types.h>
53 * Key used to reference a channel between the sessiond and the consumer. This
54 * is only read and updated with the session_list lock held.
56 static uint64_t next_kernel_channel_key
;
58 static const char *module_proc_lttng
= "/proc/lttng";
60 static int kernel_tracer_fd
= -1;
61 static int kernel_tracer_event_notifier_group_fd
= -1;
62 static int kernel_tracer_event_notifier_group_notification_fd
= -1;
63 static struct cds_lfht
*kernel_token_to_event_notifier_rule_ht
;
66 * Add context on a kernel channel.
68 * Assumes the ownership of ctx.
70 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
, struct ltt_kernel_context
*ctx
)
77 DBG("Adding context to channel %s", chan
->channel
->name
);
78 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
82 /* Exists but not available for this kernel */
83 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
86 /* If EEXIST, we just ignore the error */
90 PERROR("add context ioctl");
91 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
98 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
103 trace_kernel_destroy_context(ctx
);
109 * Create a new kernel session, register it to the kernel tracer and add it to
110 * the session daemon session.
112 int kernel_create_session(struct ltt_session
*session
)
115 struct ltt_kernel_session
*lks
;
117 LTTNG_ASSERT(session
);
119 /* Allocate data structure */
120 lks
= trace_kernel_create_session();
121 if (lks
== nullptr) {
126 /* Kernel tracer session creation */
127 ret
= kernctl_create_session(kernel_tracer_fd
);
129 PERROR("ioctl kernel create session");
134 /* Prevent fd duplication after execlp() */
135 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
137 PERROR("fcntl session fd");
140 lks
->id
= session
->id
;
141 lks
->consumer_fds_sent
= 0;
142 session
->kernel_session
= lks
;
144 DBG("Kernel session created (fd: %d)", lks
->fd
);
147 * This is necessary since the creation time is present in the session
148 * name when it is generated.
150 if (session
->has_auto_generated_name
) {
151 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
153 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
156 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
161 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
163 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
172 trace_kernel_destroy_session(lks
);
173 trace_kernel_free_session(lks
);
179 * Create a kernel channel, register it to the kernel tracer and add it to the
182 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
)
185 struct ltt_kernel_channel
*lkc
;
187 LTTNG_ASSERT(session
);
190 /* Allocate kernel channel */
191 lkc
= trace_kernel_create_channel(chan
);
192 if (lkc
== nullptr) {
196 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
198 lkc
->channel
->attr
.overwrite
,
199 lkc
->channel
->attr
.subbuf_size
,
200 lkc
->channel
->attr
.num_subbuf
,
201 lkc
->channel
->attr
.switch_timer_interval
,
202 lkc
->channel
->attr
.read_timer_interval
,
203 lkc
->channel
->attr
.live_timer_interval
,
204 lkc
->channel
->attr
.output
);
206 /* Kernel tracer channel creation */
207 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
209 PERROR("ioctl kernel create channel");
213 /* Setup the channel fd */
215 /* Prevent fd duplication after execlp() */
216 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
218 PERROR("fcntl session fd");
221 /* Add channel to session */
222 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
223 session
->channel_count
++;
224 lkc
->session
= session
;
225 lkc
->key
= ++next_kernel_channel_key
;
227 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
243 * Create a kernel event notifier group, register it to the kernel tracer and
244 * add it to the kernel session.
246 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
251 LTTNG_ASSERT(event_notifier_group_fd
);
253 /* Kernel event notifier group creation. */
254 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
256 PERROR("Failed to create kernel event notifier group");
263 /* Prevent fd duplication after execlp(). */
264 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
266 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
271 DBG("Created kernel event notifier group: fd = %d", local_fd
);
272 *event_notifier_group_fd
= local_fd
;
277 ret
= close(local_fd
);
279 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
288 * Compute the offset of the instrumentation byte in the binary based on the
289 * function probe location using the ELF lookup method.
291 * Returns 0 on success and set the offset out parameter to the offset of the
293 * Returns -1 on error
295 static int extract_userspace_probe_offset_function_elf(
296 const struct lttng_userspace_probe_location
*probe_location
,
303 const char *symbol
= nullptr;
304 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
305 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
307 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
308 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
310 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
316 lookup_method_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup
);
318 LTTNG_ASSERT(lookup_method_type
==
319 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
321 symbol
= lttng_userspace_probe_location_function_get_function_name(probe_location
);
327 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
333 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
335 DBG("userspace probe offset calculation failed for "
341 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t) (*offset
));
347 * Compute the offsets of the instrumentation bytes in the binary based on the
348 * tracepoint probe location using the SDT lookup method. This function
349 * allocates the offsets buffer, the caller must free it.
351 * Returns 0 on success and set the offset out parameter to the offsets of the
353 * Returns -1 on error.
355 static int extract_userspace_probe_offset_tracepoint_sdt(
356 const struct lttng_userspace_probe_location
*probe_location
,
360 uint32_t *offsets_count
)
362 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
363 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
364 const char *probe_name
= nullptr, *provider_name
= nullptr;
368 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
369 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
371 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
377 lookup_method_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup
);
379 LTTNG_ASSERT(lookup_method_type
==
380 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
382 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(probe_location
);
388 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(probe_location
);
389 if (!provider_name
) {
394 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
400 ret
= run_as_extract_sdt_probe_offsets(
401 fd
, provider_name
, probe_name
, uid
, gid
, offsets
, offsets_count
);
403 DBG("userspace probe offset calculation failed for sdt "
410 if (*offsets_count
== 0) {
411 DBG("no userspace probe offset found");
415 DBG("%u userspace probe SDT offsets found for %s:%s at:",
419 for (i
= 0; i
< *offsets_count
; i
++) {
420 DBG("\t0x%jd", (intmax_t) ((*offsets
)[i
]));
426 static int userspace_probe_add_callsite(const struct lttng_userspace_probe_location
*location
,
431 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= nullptr;
432 enum lttng_userspace_probe_location_lookup_method_type type
;
435 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
436 if (!lookup_method
) {
441 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
443 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
445 struct lttng_kernel_abi_event_callsite callsite
;
448 ret
= extract_userspace_probe_offset_function_elf(location
, uid
, gid
, &offset
);
450 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
454 callsite
.u
.uprobe
.offset
= offset
;
455 ret
= kernctl_add_callsite(fd
, &callsite
);
457 WARN("Failed to add callsite to ELF userspace probe.");
458 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
463 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
466 uint64_t *offsets
= nullptr;
467 uint32_t offsets_count
;
468 struct lttng_kernel_abi_event_callsite callsite
;
471 * This call allocates the offsets buffer. This buffer must be freed
474 ret
= extract_userspace_probe_offset_tracepoint_sdt(
475 location
, uid
, gid
, &offsets
, &offsets_count
);
477 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
480 for (i
= 0; i
< offsets_count
; i
++) {
481 callsite
.u
.uprobe
.offset
= offsets
[i
];
482 ret
= kernctl_add_callsite(fd
, &callsite
);
484 WARN("Failed to add callsite to SDT userspace probe");
485 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
494 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
502 * Extract the offsets of the instrumentation point for the different lookup
505 static int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
506 struct ltt_kernel_session
*session
,
510 const struct lttng_userspace_probe_location
*location
= nullptr;
513 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
515 location
= lttng_event_get_userspace_probe_location(ev
);
521 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
, fd
);
523 WARN("Failed to add callsite to userspace probe event '%s'", ev
->name
);
531 * Extract the offsets of the instrumentation point for the different look-up
534 static int userspace_probe_event_rule_add_callsites(const struct lttng_event_rule
*rule
,
535 const struct lttng_credentials
*creds
,
539 enum lttng_event_rule_status status
;
540 enum lttng_event_rule_type event_rule_type
;
541 const struct lttng_userspace_probe_location
*location
= nullptr;
546 event_rule_type
= lttng_event_rule_get_type(rule
);
547 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
549 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
550 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
555 ret
= userspace_probe_add_callsite(
556 location
, lttng_credentials_get_uid(creds
), lttng_credentials_get_gid(creds
), fd
);
558 WARN("Failed to add callsite to user space probe object: fd = %d", fd
);
566 * Create a kernel event, enable it to the kernel tracer and add it to the
567 * channel event list of the kernel session.
568 * We own filter_expression and filter.
570 int kernel_create_event(struct lttng_event
*ev
,
571 struct ltt_kernel_channel
*channel
,
572 char *filter_expression
,
573 struct lttng_bytecode
*filter
)
576 enum lttng_error_code ret
;
577 struct ltt_kernel_event
*event
;
580 LTTNG_ASSERT(channel
);
582 /* We pass ownership of filter_expression and filter */
583 ret
= trace_kernel_create_event(ev
, filter_expression
, filter
, &event
);
584 if (ret
!= LTTNG_OK
) {
588 fd
= kernctl_create_event(channel
->fd
, event
->event
);
592 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
595 WARN("Event type not implemented");
596 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
599 WARN("Event %s not found!", ev
->name
);
600 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
603 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
604 PERROR("create event ioctl");
609 event
->type
= ev
->type
;
611 /* Prevent fd duplication after execlp() */
612 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
614 PERROR("fcntl session fd");
618 err
= kernctl_filter(event
->fd
, filter
);
622 ret
= LTTNG_ERR_FILTER_NOMEM
;
625 ret
= LTTNG_ERR_FILTER_INVAL
;
632 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
633 ret
= (lttng_error_code
) userspace_probe_event_add_callsites(
634 ev
, channel
->session
, event
->fd
);
636 goto add_callsite_error
;
640 err
= kernctl_enable(event
->fd
);
644 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
647 PERROR("enable kernel event");
648 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
654 /* Add event to event list */
655 cds_list_add(&event
->list
, &channel
->events_list
.head
);
656 channel
->event_count
++;
658 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
668 closeret
= close(event
->fd
);
670 PERROR("close event fd");
680 * Disable a kernel channel.
682 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
688 ret
= kernctl_disable(chan
->fd
);
690 PERROR("disable chan ioctl");
695 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
707 * Enable a kernel channel.
709 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
715 ret
= kernctl_enable(chan
->fd
);
716 if (ret
< 0 && ret
!= -EEXIST
) {
717 PERROR("Enable kernel chan");
722 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
734 * Enable a kernel event.
736 int kernel_enable_event(struct ltt_kernel_event
*event
)
742 ret
= kernctl_enable(event
->fd
);
746 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
749 PERROR("enable kernel event");
756 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
765 * Disable a kernel event.
767 int kernel_disable_event(struct ltt_kernel_event
*event
)
773 ret
= kernctl_disable(event
->fd
);
775 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
782 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
791 * Disable a kernel event notifier.
793 static int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
800 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
803 ret
= kernctl_disable(event
->fd
);
805 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
812 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
, event
->fd
, event
->token
);
818 static struct process_attr_tracker
*
819 _kernel_get_process_attr_tracker(struct ltt_kernel_session
*session
,
820 enum lttng_process_attr process_attr
)
822 switch (process_attr
) {
823 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
824 return session
->tracker_pid
;
825 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
826 return session
->tracker_vpid
;
827 case LTTNG_PROCESS_ATTR_USER_ID
:
828 return session
->tracker_uid
;
829 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
830 return session
->tracker_vuid
;
831 case LTTNG_PROCESS_ATTR_GROUP_ID
:
832 return session
->tracker_gid
;
833 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
834 return session
->tracker_vgid
;
840 const struct process_attr_tracker
*
841 kernel_get_process_attr_tracker(struct ltt_kernel_session
*session
,
842 enum lttng_process_attr process_attr
)
844 return (const struct process_attr_tracker
*) _kernel_get_process_attr_tracker(session
,
848 enum lttng_error_code
849 kernel_process_attr_tracker_set_tracking_policy(struct ltt_kernel_session
*session
,
850 enum lttng_process_attr process_attr
,
851 enum lttng_tracking_policy policy
)
854 enum lttng_error_code ret_code
= LTTNG_OK
;
855 struct process_attr_tracker
*tracker
=
856 _kernel_get_process_attr_tracker(session
, process_attr
);
857 enum lttng_tracking_policy previous_policy
;
860 ret_code
= LTTNG_ERR_INVALID
;
864 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
865 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
867 ret_code
= LTTNG_ERR_UNK
;
871 if (previous_policy
== policy
) {
876 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
877 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
879 * Maintain a special case for the process ID process
880 * attribute tracker as it was the only supported
881 * attribute prior to 2.12.
883 ret
= kernctl_track_pid(session
->fd
, -1);
885 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
888 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
889 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
891 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
893 * Maintain a special case for the process ID process
894 * attribute tracker as it was the only supported
895 * attribute prior to 2.12.
897 ret
= kernctl_untrack_pid(session
->fd
, -1);
899 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
905 /* kern-ctl error handling */
911 ret_code
= LTTNG_ERR_INVALID
;
914 ret_code
= LTTNG_ERR_NOMEM
;
917 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
920 ret_code
= LTTNG_ERR_UNK
;
927 enum lttng_error_code
928 kernel_process_attr_tracker_inclusion_set_add_value(struct ltt_kernel_session
*session
,
929 enum lttng_process_attr process_attr
,
930 const struct process_attr_value
*value
)
932 int ret
, integral_value
;
933 enum lttng_error_code ret_code
;
934 struct process_attr_tracker
*tracker
;
935 enum process_attr_tracker_status status
;
938 * Convert process attribute tracker value to the integral
939 * representation required by the kern-ctl API.
941 switch (process_attr
) {
942 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
943 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
944 integral_value
= (int) value
->value
.pid
;
946 case LTTNG_PROCESS_ATTR_USER_ID
:
947 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
948 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
951 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
952 if (ret_code
!= LTTNG_OK
) {
955 integral_value
= (int) uid
;
957 integral_value
= (int) value
->value
.uid
;
960 case LTTNG_PROCESS_ATTR_GROUP_ID
:
961 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
962 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
965 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
966 if (ret_code
!= LTTNG_OK
) {
969 integral_value
= (int) gid
;
971 integral_value
= (int) value
->value
.gid
;
975 ret_code
= LTTNG_ERR_INVALID
;
979 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
981 ret_code
= LTTNG_ERR_INVALID
;
985 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
986 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
988 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
989 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
991 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
992 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
994 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
996 ret_code
= LTTNG_ERR_UNK
;
1002 DBG("Kernel track %s %d for session id %" PRIu64
,
1003 lttng_process_attr_to_string(process_attr
),
1006 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1008 * Maintain a special case for the process ID process attribute
1009 * tracker as it was the only supported attribute prior to 2.12.
1011 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1013 ret
= kernctl_track_id(session
->fd
, process_attr
, integral_value
);
1016 ret_code
= LTTNG_OK
;
1020 kernel_wait_quiescent();
1022 /* kern-ctl error handling */
1025 ret_code
= LTTNG_OK
;
1028 ret_code
= LTTNG_ERR_INVALID
;
1031 ret_code
= LTTNG_ERR_NOMEM
;
1034 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1037 ret_code
= LTTNG_ERR_UNK
;
1041 /* Attempt to remove the value from the tracker. */
1042 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1043 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1044 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1045 lttng_process_attr_to_string(process_attr
),
1052 enum lttng_error_code
1053 kernel_process_attr_tracker_inclusion_set_remove_value(struct ltt_kernel_session
*session
,
1054 enum lttng_process_attr process_attr
,
1055 const struct process_attr_value
*value
)
1057 int ret
, integral_value
;
1058 enum lttng_error_code ret_code
;
1059 struct process_attr_tracker
*tracker
;
1060 enum process_attr_tracker_status status
;
1063 * Convert process attribute tracker value to the integral
1064 * representation required by the kern-ctl API.
1066 switch (process_attr
) {
1067 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1068 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1069 integral_value
= (int) value
->value
.pid
;
1071 case LTTNG_PROCESS_ATTR_USER_ID
:
1072 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1073 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1076 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
1077 if (ret_code
!= LTTNG_OK
) {
1080 integral_value
= (int) uid
;
1082 integral_value
= (int) value
->value
.uid
;
1085 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1086 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1087 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1090 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1091 if (ret_code
!= LTTNG_OK
) {
1094 integral_value
= (int) gid
;
1096 integral_value
= (int) value
->value
.gid
;
1100 ret_code
= LTTNG_ERR_INVALID
;
1104 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1106 ret_code
= LTTNG_ERR_INVALID
;
1110 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1111 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1113 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1114 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1116 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1117 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1119 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1121 ret_code
= LTTNG_ERR_UNK
;
1127 DBG("Kernel track %s %d for session id %" PRIu64
,
1128 lttng_process_attr_to_string(process_attr
),
1131 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1133 * Maintain a special case for the process ID process attribute
1134 * tracker as it was the only supported attribute prior to 2.12.
1136 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1138 ret
= kernctl_untrack_id(session
->fd
, process_attr
, integral_value
);
1141 ret_code
= LTTNG_OK
;
1144 kernel_wait_quiescent();
1146 /* kern-ctl error handling */
1149 ret_code
= LTTNG_OK
;
1152 ret_code
= LTTNG_ERR_INVALID
;
1155 ret_code
= LTTNG_ERR_NOMEM
;
1158 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1161 ret_code
= LTTNG_ERR_UNK
;
1165 /* Attempt to add the value to the tracker. */
1166 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1167 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1168 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1169 lttng_process_attr_to_string(process_attr
),
1177 * Create kernel metadata, open from the kernel tracer and add it to the
1180 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1183 struct ltt_kernel_metadata
*lkm
= nullptr;
1185 LTTNG_ASSERT(session
);
1187 /* Allocate kernel metadata */
1188 lkm
= trace_kernel_create_metadata();
1189 if (lkm
== nullptr) {
1193 /* Kernel tracer metadata creation */
1194 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1200 lkm
->key
= ++next_kernel_channel_key
;
1201 /* Prevent fd duplication after execlp() */
1202 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1204 PERROR("fcntl session fd");
1207 session
->metadata
= lkm
;
1209 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1214 trace_kernel_destroy_metadata(lkm
);
1220 * Start tracing session.
1222 int kernel_start_session(struct ltt_kernel_session
*session
)
1226 LTTNG_ASSERT(session
);
1228 ret
= kernctl_start_session(session
->fd
);
1230 PERROR("ioctl start session");
1234 DBG("Kernel session started");
1243 * Make a kernel wait to make sure in-flight probe have completed.
1245 void kernel_wait_quiescent()
1248 int fd
= kernel_tracer_fd
;
1250 DBG("Kernel quiescent wait on %d", fd
);
1252 ret
= kernctl_wait_quiescent(fd
);
1254 PERROR("wait quiescent ioctl");
1255 ERR("Kernel quiescent wait failed");
1260 * Force flush buffer of metadata.
1262 int kernel_metadata_flush_buffer(int fd
)
1266 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1268 ret
= kernctl_buffer_flush(fd
);
1270 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1277 * Force flush buffer for channel.
1279 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1282 struct ltt_kernel_stream
*stream
;
1284 LTTNG_ASSERT(channel
);
1286 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1288 cds_list_for_each_entry (stream
, &channel
->stream_list
.head
, list
) {
1289 DBG("Flushing channel stream %d", stream
->fd
);
1290 ret
= kernctl_buffer_flush(stream
->fd
);
1293 ERR("Fail to flush buffer for stream %d (ret: %d)", stream
->fd
, ret
);
1301 * Stop tracing session.
1303 int kernel_stop_session(struct ltt_kernel_session
*session
)
1307 LTTNG_ASSERT(session
);
1309 ret
= kernctl_stop_session(session
->fd
);
1314 DBG("Kernel session stopped");
1323 * Open stream of channel, register it to the kernel tracer and add it
1324 * to the stream list of the channel.
1326 * Note: given that the streams may appear in random order wrt CPU
1327 * number (e.g. cpu hotplug), the index value of the stream number in
1328 * the stream name is not necessarily linked to the CPU number.
1330 * Return the number of created stream. Else, a negative value.
1332 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1335 struct ltt_kernel_stream
*lks
;
1337 LTTNG_ASSERT(channel
);
1339 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1340 lks
= trace_kernel_create_stream(channel
->channel
->name
, channel
->stream_count
);
1341 if (lks
== nullptr) {
1350 /* Prevent fd duplication after execlp() */
1351 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1353 PERROR("fcntl session fd");
1356 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1357 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1359 /* Add stream to channel stream list */
1360 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1361 channel
->stream_count
++;
1363 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
, lks
->state
);
1366 return channel
->stream_count
;
1373 * Open the metadata stream and set it to the kernel session.
1375 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1379 LTTNG_ASSERT(session
);
1381 ret
= kernctl_create_stream(session
->metadata
->fd
);
1383 PERROR("kernel create metadata stream");
1387 DBG("Kernel metadata stream created (fd: %d)", ret
);
1388 session
->metadata_stream_fd
= ret
;
1389 /* Prevent fd duplication after execlp() */
1390 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1392 PERROR("fcntl session fd");
1402 * Get the event list from the kernel tracer and return the number of elements.
1404 ssize_t
kernel_list_events(struct lttng_event
**events
)
1408 size_t nbmem
, count
= 0;
1410 struct lttng_event
*elist
;
1412 LTTNG_ASSERT(events
);
1414 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1416 PERROR("kernel tracepoint list");
1420 fp
= fdopen(fd
, "r");
1421 if (fp
== nullptr) {
1422 PERROR("kernel tracepoint list fdopen");
1427 * Init memory size counter
1428 * See kernel-ctl.h for explanation of this value
1430 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1431 elist
= calloc
<lttng_event
>(nbmem
);
1432 if (elist
== nullptr) {
1433 PERROR("alloc list events");
1438 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1439 if (count
>= nbmem
) {
1440 struct lttng_event
*new_elist
;
1443 new_nbmem
= nbmem
<< 1;
1444 DBG("Reallocating event list from %zu to %zu bytes", nbmem
, new_nbmem
);
1445 new_elist
= (lttng_event
*) realloc(elist
,
1446 new_nbmem
* sizeof(struct lttng_event
));
1447 if (new_elist
== nullptr) {
1448 PERROR("realloc list events");
1454 /* Zero the new memory */
1455 memset(new_elist
+ nbmem
,
1457 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1461 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1462 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1463 elist
[count
].enabled
= -1;
1469 DBG("Kernel list events done (%zu events)", count
);
1471 ret
= fclose(fp
); /* closes both fp and fd */
1487 * Get kernel version and validate it.
1489 int kernel_validate_version(struct lttng_kernel_abi_tracer_version
*version
,
1490 struct lttng_kernel_abi_tracer_abi_version
*abi_version
)
1494 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1496 ERR("Failed to retrieve the lttng-modules version");
1500 /* Validate version */
1501 if (version
->major
!= VERSION_MAJOR
) {
1502 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1507 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1509 ERR("Failed to retrieve lttng-modules ABI version");
1512 if (abi_version
->major
!= LTTNG_KERNEL_ABI_MAJOR_VERSION
) {
1513 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1516 LTTNG_KERNEL_ABI_MAJOR_VERSION
);
1519 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1523 abi_version
->minor
);
1530 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1535 * Kernel work-arounds called at the start of sessiond main().
1537 int init_kernel_workarounds()
1543 * boot_id needs to be read once before being used concurrently
1544 * to deal with a Linux kernel race. A fix is proposed for
1545 * upstream, but the work-around is needed for older kernels.
1547 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1554 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1556 /* Ignore error, we don't really care */
1568 * Teardown of a kernel session, keeping data required by destroy notifiers.
1570 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1572 struct lttng_trace_chunk
*trace_chunk
;
1574 if (ksess
== nullptr) {
1575 DBG3("No kernel session when tearing down session");
1579 DBG("Tearing down kernel session");
1580 trace_chunk
= ksess
->current_trace_chunk
;
1583 * Destroy channels on the consumer if at least one FD has been sent and we
1584 * are in no output mode because the streams are in *no* monitor mode so we
1585 * have to send a command to clean them up or else they leaked.
1587 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1589 struct consumer_socket
*socket
;
1590 struct lttng_ht_iter iter
;
1592 /* For each consumer socket. */
1594 cds_lfht_for_each_entry (
1595 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1596 struct ltt_kernel_channel
*chan
;
1598 /* For each channel, ask the consumer to destroy it. */
1599 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1600 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1602 /* Consumer is probably dead. Use next socket. */
1610 /* Close any relayd session */
1611 consumer_output_send_destroy_relayd(ksess
->consumer
);
1613 trace_kernel_destroy_session(ksess
);
1614 lttng_trace_chunk_put(trace_chunk
);
1617 /* Teardown of data required by destroy notifiers. */
1618 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1620 if (ksess
== nullptr) {
1623 trace_kernel_free_session(ksess
);
1627 * Destroy a kernel channel object. It does not do anything on the tracer side.
1629 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1631 struct ltt_kernel_session
*ksess
= nullptr;
1633 LTTNG_ASSERT(kchan
);
1634 LTTNG_ASSERT(kchan
->channel
);
1636 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1638 /* Update channel count of associated session. */
1639 if (kchan
->session
) {
1640 /* Keep pointer reference so we can update it after the destroy. */
1641 ksess
= kchan
->session
;
1644 trace_kernel_destroy_channel(kchan
);
1647 * At this point the kernel channel is not visible anymore. This is safe
1648 * since in order to work on a visible kernel session, the tracing session
1649 * lock (ltt_session.lock) MUST be acquired.
1652 ksess
->channel_count
--;
1657 * Take a snapshot for a given kernel session.
1659 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1661 enum lttng_error_code
kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1662 const struct consumer_output
*output
,
1663 uint64_t nb_packets_per_stream
)
1665 int err
, ret
, saved_metadata_fd
;
1666 enum lttng_error_code status
= LTTNG_OK
;
1667 struct consumer_socket
*socket
;
1668 struct lttng_ht_iter iter
;
1669 struct ltt_kernel_metadata
*saved_metadata
;
1670 char *trace_path
= nullptr;
1671 size_t consumer_path_offset
= 0;
1673 LTTNG_ASSERT(ksess
);
1674 LTTNG_ASSERT(ksess
->consumer
);
1675 LTTNG_ASSERT(output
);
1677 DBG("Kernel snapshot record started");
1679 /* Save current metadata since the following calls will change it. */
1680 saved_metadata
= ksess
->metadata
;
1681 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1685 ret
= kernel_open_metadata(ksess
);
1687 status
= LTTNG_ERR_KERN_META_FAIL
;
1691 ret
= kernel_open_metadata_stream(ksess
);
1693 status
= LTTNG_ERR_KERN_META_FAIL
;
1694 goto error_open_stream
;
1697 trace_path
= setup_channel_trace_path(ksess
->consumer
, "", &consumer_path_offset
);
1699 status
= LTTNG_ERR_INVALID
;
1702 /* Send metadata to consumer and snapshot everything. */
1703 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1704 struct ltt_kernel_channel
*chan
;
1706 pthread_mutex_lock(socket
->lock
);
1707 /* This stream must not be monitored by the consumer. */
1708 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1709 pthread_mutex_unlock(socket
->lock
);
1711 status
= LTTNG_ERR_KERN_META_FAIL
;
1712 goto error_consumer
;
1715 /* For each channel, ask the consumer to snapshot it. */
1716 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1717 status
= consumer_snapshot_channel(socket
,
1721 &trace_path
[consumer_path_offset
],
1722 nb_packets_per_stream
);
1723 if (status
!= LTTNG_OK
) {
1724 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1725 goto error_consumer
;
1729 /* Snapshot metadata, */
1730 status
= consumer_snapshot_channel(socket
,
1731 ksess
->metadata
->key
,
1734 &trace_path
[consumer_path_offset
],
1736 if (status
!= LTTNG_OK
) {
1737 goto error_consumer
;
1741 * The metadata snapshot is done, ask the consumer to destroy it since
1742 * it's not monitored on the consumer side.
1744 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1748 /* Close newly opened metadata stream. It's now on the consumer side. */
1749 err
= close(ksess
->metadata_stream_fd
);
1751 PERROR("close snapshot kernel");
1755 trace_kernel_destroy_metadata(ksess
->metadata
);
1757 /* Restore metadata state.*/
1758 ksess
->metadata
= saved_metadata
;
1759 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1766 * Get the syscall mask array from the kernel tracer.
1768 * Return 0 on success else a negative value. In both case, syscall_mask should
1771 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1773 LTTNG_ASSERT(syscall_mask
);
1774 LTTNG_ASSERT(nr_bits
);
1776 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1779 static int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1782 struct lttng_kernel_abi_tracer_abi_version abi
;
1784 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1786 ERR("Failed to retrieve lttng-modules ABI version");
1790 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1796 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1799 * Return 1 on success, 0 when feature is not supported, negative value in case
1802 int kernel_supports_ring_buffer_snapshot_sample_positions()
1805 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1807 return kernel_tracer_abi_greater_or_equal(2, 3);
1811 * Check for the support of the packet sequence number via abi version number.
1813 * Return 1 on success, 0 when feature is not supported, negative value in case
1816 int kernel_supports_ring_buffer_packet_sequence_number()
1819 * Packet sequence number was introduced in LTTng 2.8,
1820 * lttng-modules ABI 2.1.
1822 return kernel_tracer_abi_greater_or_equal(2, 1);
1826 * Check for the support of event notifiers via abi version number.
1828 * Return 1 on success, 0 when feature is not supported, negative value in case
1831 int kernel_supports_event_notifiers()
1834 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1836 return kernel_tracer_abi_greater_or_equal(2, 6);
1840 * Rotate a kernel session.
1842 * Return LTTNG_OK on success or else an LTTng error code.
1844 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1847 enum lttng_error_code status
= LTTNG_OK
;
1848 struct consumer_socket
*socket
;
1849 struct lttng_ht_iter iter
;
1850 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1852 LTTNG_ASSERT(ksess
);
1853 LTTNG_ASSERT(ksess
->consumer
);
1855 DBG("Rotate kernel session %s started (session %" PRIu64
")", session
->name
, session
->id
);
1860 * Note that this loop will end after one iteration given that there is
1861 * only one kernel consumer.
1863 cds_lfht_for_each_entry (ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1864 struct ltt_kernel_channel
*chan
;
1866 /* For each channel, ask the consumer to rotate it. */
1867 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1868 DBG("Rotate kernel channel %" PRIu64
", session %s",
1871 ret
= consumer_rotate_channel(socket
,
1874 /* is_metadata_channel */ false);
1876 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1882 * Rotate the metadata channel.
1884 ret
= consumer_rotate_channel(socket
,
1885 ksess
->metadata
->key
,
1887 /* is_metadata_channel */ true);
1889 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1899 enum lttng_error_code
kernel_create_channel_subdirectories(const struct ltt_kernel_session
*ksess
)
1901 enum lttng_error_code ret
= LTTNG_OK
;
1902 enum lttng_trace_chunk_status chunk_status
;
1905 LTTNG_ASSERT(ksess
->current_trace_chunk
);
1908 * Create the index subdirectory which will take care
1909 * of implicitly creating the channel's path.
1911 chunk_status
= lttng_trace_chunk_create_subdirectory(
1912 ksess
->current_trace_chunk
, DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1913 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1914 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1923 * Setup necessary data for kernel tracer action.
1925 int init_kernel_tracer()
1928 bool is_root
= !getuid();
1930 /* Modprobe lttng kernel modules */
1931 ret
= modprobe_lttng_control();
1936 /* Open debugfs lttng */
1937 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1938 if (kernel_tracer_fd
< 0) {
1939 DBG("Failed to open %s", module_proc_lttng
);
1943 /* Validate kernel version */
1944 ret
= kernel_validate_version(&the_kernel_tracer_version
, &the_kernel_tracer_abi_version
);
1949 ret
= modprobe_lttng_data();
1954 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1959 WARN("Kernel tracer does not support buffer monitoring. "
1960 "The monitoring timer of channels in the kernel domain "
1961 "will be set to 0 (disabled).");
1964 ret
= kernel_supports_event_notifiers();
1966 ERR("Failed to check for kernel tracer event notifier support");
1969 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1971 /* This is not fatal. */
1972 WARN("Failed to create kernel event notifier group");
1973 kernel_tracer_event_notifier_group_fd
= -1;
1975 enum event_notifier_error_accounting_status error_accounting_status
;
1976 enum lttng_error_code error_code_ret
=
1977 kernel_create_event_notifier_group_notification_fd(
1978 &kernel_tracer_event_notifier_group_notification_fd
);
1980 if (error_code_ret
!= LTTNG_OK
) {
1984 error_accounting_status
= event_notifier_error_accounting_register_kernel(
1985 kernel_tracer_event_notifier_group_fd
);
1986 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1987 ERR("Failed to initialize event notifier error accounting for kernel tracer");
1988 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
1992 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
1993 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
1994 if (!kernel_token_to_event_notifier_rule_ht
) {
1995 goto error_token_ht
;
1999 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2001 kernel_tracer_event_notifier_group_fd
,
2002 kernel_tracer_event_notifier_group_notification_fd
);
2004 ret
= syscall_init_table(kernel_tracer_fd
);
2006 ERR("Unable to populate syscall table. Syscall tracing won't "
2007 "work for this session daemon.");
2013 modprobe_remove_lttng_control();
2014 ret
= close(kernel_tracer_fd
);
2016 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd
);
2019 kernel_tracer_fd
= -1;
2020 return LTTNG_ERR_KERN_VERSION
;
2023 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2025 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2026 kernel_tracer_event_notifier_group_notification_fd
);
2029 kernel_tracer_event_notifier_group_notification_fd
= -1;
2032 ret
= close(kernel_tracer_event_notifier_group_fd
);
2034 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2035 kernel_tracer_event_notifier_group_fd
);
2038 kernel_tracer_event_notifier_group_fd
= -1;
2040 ret
= close(kernel_tracer_fd
);
2042 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd
);
2045 kernel_tracer_fd
= -1;
2048 modprobe_remove_lttng_control();
2051 WARN("No kernel tracer available");
2052 kernel_tracer_fd
= -1;
2054 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2056 return LTTNG_ERR_KERN_NA
;
2060 void cleanup_kernel_tracer()
2062 DBG2("Closing kernel event notifier group notification file descriptor");
2063 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2064 int ret
= notification_thread_command_remove_tracer_event_source(
2065 the_notification_thread_handle
,
2066 kernel_tracer_event_notifier_group_notification_fd
);
2067 if (ret
!= LTTNG_OK
) {
2068 ERR("Failed to remove kernel event notifier notification from notification thread");
2071 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2073 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2074 kernel_tracer_event_notifier_group_notification_fd
);
2077 kernel_tracer_event_notifier_group_notification_fd
= -1;
2080 if (kernel_token_to_event_notifier_rule_ht
) {
2081 const int ret
= cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht
, nullptr);
2082 LTTNG_ASSERT(ret
== 0);
2085 DBG2("Closing kernel event notifier group file descriptor");
2086 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2087 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2090 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2091 kernel_tracer_event_notifier_group_fd
);
2094 kernel_tracer_event_notifier_group_fd
= -1;
2097 DBG2("Closing kernel fd");
2098 if (kernel_tracer_fd
>= 0) {
2099 const int ret
= close(kernel_tracer_fd
);
2102 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2106 kernel_tracer_fd
= -1;
2109 free(syscall_table
);
2112 bool kernel_tracer_is_initialized()
2114 return kernel_tracer_fd
>= 0;
2118 * Clear a kernel session.
2120 * Return LTTNG_OK on success or else an LTTng error code.
2122 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2125 enum lttng_error_code status
= LTTNG_OK
;
2126 struct consumer_socket
*socket
;
2127 struct lttng_ht_iter iter
;
2128 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2130 LTTNG_ASSERT(ksess
);
2131 LTTNG_ASSERT(ksess
->consumer
);
2133 DBG("Clear kernel session %s (session %" PRIu64
")", session
->name
, session
->id
);
2137 if (ksess
->active
) {
2138 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2139 status
= LTTNG_ERR_FATAL
;
2144 * Note that this loop will end after one iteration given that there is
2145 * only one kernel consumer.
2147 cds_lfht_for_each_entry (ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2148 struct ltt_kernel_channel
*chan
;
2150 /* For each channel, ask the consumer to clear it. */
2151 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
2152 DBG("Clear kernel channel %" PRIu64
", session %s",
2155 ret
= consumer_clear_channel(socket
, chan
->key
);
2161 if (!ksess
->metadata
) {
2163 * Nothing to do for the metadata.
2164 * This is a snapshot session.
2165 * The metadata is genererated on the fly.
2171 * Clear the metadata channel.
2172 * Metadata channel is not cleared per se but we still need to
2173 * perform a rotation operation on it behind the scene.
2175 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2184 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2185 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2188 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2196 enum lttng_error_code
2197 kernel_create_event_notifier_group_notification_fd(int *event_notifier_group_notification_fd
)
2199 int local_fd
= -1, ret
;
2200 enum lttng_error_code error_code_ret
;
2202 LTTNG_ASSERT(event_notifier_group_notification_fd
);
2204 ret
= kernctl_create_event_notifier_group_notification_fd(
2205 kernel_tracer_event_notifier_group_fd
);
2207 PERROR("Failed to create kernel event notifier group notification file descriptor");
2208 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2214 /* Prevent fd duplication after execlp(). */
2215 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2217 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2219 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2223 DBG("Created kernel notifier group notification file descriptor: fd = %d", local_fd
);
2224 error_code_ret
= LTTNG_OK
;
2225 *event_notifier_group_notification_fd
= local_fd
;
2229 if (local_fd
>= 0) {
2230 ret
= close(local_fd
);
2232 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2237 return error_code_ret
;
2240 enum lttng_error_code
2241 kernel_destroy_event_notifier_group_notification_fd(int event_notifier_group_notification_fd
)
2243 enum lttng_error_code ret_code
= LTTNG_OK
;
2245 DBG("Closing event notifier group notification file descriptor: fd = %d",
2246 event_notifier_group_notification_fd
);
2247 if (event_notifier_group_notification_fd
>= 0) {
2248 const int ret
= close(event_notifier_group_notification_fd
);
2250 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2251 event_notifier_group_notification_fd
);
2258 static unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2260 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
2262 return lttng_condition_hash(condition
);
2265 static int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2267 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2268 const struct lttng_trigger
*trigger
= (lttng_trigger
*) key
;
2270 event_notifier_rule
=
2271 caa_container_of(node
, const struct ltt_kernel_event_notifier_rule
, ht_node
);
2273 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2276 static enum lttng_error_code
kernel_create_event_notifier_rule(
2277 struct lttng_trigger
*trigger
, const struct lttng_credentials
*creds
, uint64_t token
)
2279 int err
, fd
, ret
= 0;
2280 enum lttng_error_code error_code_ret
;
2281 enum lttng_condition_status condition_status
;
2282 enum lttng_condition_type condition_type
;
2283 enum lttng_event_rule_type event_rule_type
;
2284 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2285 struct lttng_kernel_abi_event_notifier kernel_event_notifier
= {};
2286 unsigned int capture_bytecode_count
= 0, i
;
2287 const struct lttng_condition
*condition
= nullptr;
2288 const struct lttng_event_rule
*event_rule
= nullptr;
2289 enum lttng_condition_status cond_status
;
2291 LTTNG_ASSERT(trigger
);
2293 condition
= lttng_trigger_get_const_condition(trigger
);
2294 LTTNG_ASSERT(condition
);
2296 condition_type
= lttng_condition_get_type(condition
);
2297 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2299 /* Does not acquire a reference. */
2300 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2301 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2302 LTTNG_ASSERT(event_rule
);
2304 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2305 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2307 error_code_ret
= trace_kernel_create_event_notifier_rule(
2310 lttng_condition_event_rule_matches_get_error_counter_index(condition
),
2311 &event_notifier_rule
);
2312 if (error_code_ret
!= LTTNG_OK
) {
2316 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(event_rule
,
2317 &kernel_event_notifier
);
2318 if (error_code_ret
!= LTTNG_OK
) {
2322 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2323 kernel_event_notifier
.error_counter_idx
=
2324 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
2326 fd
= kernctl_create_event_notifier(kernel_tracer_event_notifier_group_fd
,
2327 &kernel_event_notifier
);
2331 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2334 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2335 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2338 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2339 kernel_event_notifier
.event
.name
);
2340 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2343 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2345 kernel_event_notifier
.event
.name
);
2346 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2351 event_notifier_rule
->fd
= fd
;
2352 /* Prevent fd duplication after execlp(). */
2353 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2355 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2357 error_code_ret
= LTTNG_ERR_FATAL
;
2358 goto set_cloexec_error
;
2361 if (event_notifier_rule
->filter
) {
2362 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2366 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2369 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2376 if (lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
) {
2377 ret
= userspace_probe_event_rule_add_callsites(
2378 event_rule
, creds
, event_notifier_rule
->fd
);
2380 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2381 goto add_callsite_error
;
2385 /* Set the capture bytecode if any. */
2386 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2387 condition
, &capture_bytecode_count
);
2388 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2390 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2391 const struct lttng_bytecode
*capture_bytecode
=
2392 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2395 if (capture_bytecode
== nullptr) {
2396 ERR("Unexpected NULL capture bytecode on condition");
2397 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2401 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2403 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2404 event_notifier_rule
->fd
);
2405 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2410 err
= kernctl_enable(event_notifier_rule
->fd
);
2414 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2417 PERROR("enable kernel event notifier");
2418 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2424 /* Add trigger to kernel token mapping in the hash table. */
2426 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
,
2427 hash_trigger(trigger
),
2428 &event_notifier_rule
->ht_node
);
2431 DBG("Created kernel event notifier: name = '%s', fd = %d",
2432 kernel_event_notifier
.event
.name
,
2433 event_notifier_rule
->fd
);
2443 const int close_ret
= close(event_notifier_rule
->fd
);
2446 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2447 event_notifier_rule
->fd
);
2451 free(event_notifier_rule
);
2453 return error_code_ret
;
2456 enum lttng_error_code
kernel_register_event_notifier(struct lttng_trigger
*trigger
,
2457 const struct lttng_credentials
*cmd_creds
)
2459 enum lttng_error_code ret
;
2460 enum lttng_condition_status status
;
2461 enum lttng_domain_type domain_type
;
2462 const struct lttng_event_rule
*event_rule
;
2463 const struct lttng_condition
*const condition
= lttng_trigger_get_const_condition(trigger
);
2464 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2466 LTTNG_ASSERT(condition
);
2468 /* Does not acquire a reference to the event rule. */
2469 status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2470 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2472 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2473 LTTNG_ASSERT(domain_type
== LTTNG_DOMAIN_KERNEL
);
2475 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2476 if (ret
!= LTTNG_OK
) {
2477 ERR("Failed to create kernel event notifier rule");
2483 enum lttng_error_code
kernel_unregister_event_notifier(const struct lttng_trigger
*trigger
)
2485 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2486 struct cds_lfht_node
*node
;
2487 struct cds_lfht_iter iter
;
2488 enum lttng_error_code error_code_ret
;
2493 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2494 hash_trigger(trigger
),
2499 node
= cds_lfht_iter_get_node(&iter
);
2501 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2505 token_event_rule_element
=
2506 caa_container_of(node
, struct ltt_kernel_event_notifier_rule
, ht_node
);
2508 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2510 error_code_ret
= LTTNG_ERR_FATAL
;
2514 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2515 error_code_ret
= LTTNG_OK
;
2520 return error_code_ret
;
2523 int kernel_get_notification_fd()
2525 return kernel_tracer_event_notifier_group_notification_fd
;