2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
17 #include <common/common.h>
18 #include <common/hashtable/utils.h>
19 #include <common/trace-chunk.h>
20 #include <common/kernel-ctl/kernel-ctl.h>
21 #include <common/kernel-ctl/kernel-ioctl.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/tracker.h>
24 #include <common/utils.h>
25 #include <lttng/event.h>
26 #include <lttng/lttng-error.h>
27 #include <lttng/tracker.h>
29 #include <lttng/userspace-probe.h>
30 #include <lttng/userspace-probe-internal.h>
31 #include <lttng/condition/event-rule-matches.h>
32 #include <lttng/condition/event-rule-matches-internal.h>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.h>
35 #include <lttng/event-rule/kernel-uprobe-internal.h>
37 #include "event-notifier-error-accounting.h"
38 #include "lttng-sessiond.h"
39 #include "lttng-syscall.h"
40 #include "condition-internal.h"
43 #include "kernel-consumer.h"
44 #include "kern-modules.h"
45 #include "sessiond-config.h"
50 #include "notification-thread-commands.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 * On some architectures, calling convention details are embedded in the symbol
67 * addresses. Uprobe requires a "clean" symbol offset (or at least, an address
68 * where an instruction boundary would be legal) to add
69 * instrumentation. sanitize_uprobe_offset implements that sanitization logic on
70 * a per-architecture basis.
72 #if defined(__arm__) || defined(__aarch64__)
73 static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset
)
76 * The least significant bit is used when branching to switch to thumb
77 * ISA. However, it's an invalid address for us; mask the least
80 return raw_offset
&= ~0b1;
82 #else /* defined(__arm__) || defined(__aarch64__) */
83 static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset
)
90 * Add context on a kernel channel.
92 * Assumes the ownership of ctx.
94 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
95 struct ltt_kernel_context
*ctx
)
102 DBG("Adding context to channel %s", chan
->channel
->name
);
103 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
107 /* Exists but not available for this kernel */
108 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
111 /* If EEXIST, we just ignore the error */
115 PERROR("add context ioctl");
116 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
123 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
128 trace_kernel_destroy_context(ctx
);
134 * Create a new kernel session, register it to the kernel tracer and add it to
135 * the session daemon session.
137 int kernel_create_session(struct ltt_session
*session
)
140 struct ltt_kernel_session
*lks
;
144 /* Allocate data structure */
145 lks
= trace_kernel_create_session();
151 /* Kernel tracer session creation */
152 ret
= kernctl_create_session(kernel_tracer_fd
);
154 PERROR("ioctl kernel create session");
159 /* Prevent fd duplication after execlp() */
160 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
162 PERROR("fcntl session fd");
165 lks
->id
= session
->id
;
166 lks
->consumer_fds_sent
= 0;
167 session
->kernel_session
= lks
;
169 DBG("Kernel session created (fd: %d)", lks
->fd
);
172 * This is necessary since the creation time is present in the session
173 * name when it is generated.
175 if (session
->has_auto_generated_name
) {
176 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
178 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
181 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
182 session
->id
, session
->name
);
185 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
187 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
188 session
->id
, session
->name
);
195 trace_kernel_destroy_session(lks
);
196 trace_kernel_free_session(lks
);
202 * Create a kernel channel, register it to the kernel tracer and add it to the
205 int kernel_create_channel(struct ltt_kernel_session
*session
,
206 struct lttng_channel
*chan
)
209 struct ltt_kernel_channel
*lkc
;
214 /* Allocate kernel channel */
215 lkc
= trace_kernel_create_channel(chan
);
220 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
221 chan
->name
, lkc
->channel
->attr
.overwrite
,
222 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
223 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
224 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
226 /* Kernel tracer channel creation */
227 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
229 PERROR("ioctl kernel create channel");
233 /* Setup the channel fd */
235 /* Prevent fd duplication after execlp() */
236 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
238 PERROR("fcntl session fd");
241 /* Add channel to session */
242 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
243 session
->channel_count
++;
244 lkc
->session
= session
;
245 lkc
->key
= ++next_kernel_channel_key
;
247 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
248 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
261 * Create a kernel event notifier group, register it to the kernel tracer and
262 * add it to the kernel session.
264 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
269 assert(event_notifier_group_fd
);
271 /* Kernel event notifier group creation. */
272 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
274 PERROR("Failed to create kernel event notifier group");
281 /* Prevent fd duplication after execlp(). */
282 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
284 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
289 DBG("Created kernel event notifier group: fd = %d", local_fd
);
290 *event_notifier_group_fd
= local_fd
;
295 ret
= close(local_fd
);
297 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
306 * Compute the offset of the instrumentation byte in the binary based on the
307 * function probe location using the ELF lookup method.
309 * Returns 0 on success and set the offset out parameter to the offset of the
311 * Returns -1 on error
314 int extract_userspace_probe_offset_function_elf(
315 const struct lttng_userspace_probe_location
*probe_location
,
316 uid_t uid
, gid_t gid
, uint64_t *offset
)
320 const char *symbol
= NULL
;
321 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
322 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
324 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
325 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
327 lookup
= lttng_userspace_probe_location_get_lookup_method(
335 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
337 assert(lookup_method_type
==
338 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
340 symbol
= lttng_userspace_probe_location_function_get_function_name(
347 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
353 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
355 DBG("userspace probe offset calculation failed for "
356 "function %s", symbol
);
360 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
366 * Compute the offsets of the instrumentation bytes in the binary based on the
367 * tracepoint probe location using the SDT lookup method. This function
368 * allocates the offsets buffer, the caller must free it.
370 * Returns 0 on success and set the offset out parameter to the offsets of the
372 * Returns -1 on error.
375 int extract_userspace_probe_offset_tracepoint_sdt(
376 const struct lttng_userspace_probe_location
*probe_location
,
377 uid_t uid
, gid_t gid
, uint64_t **offsets
,
378 uint32_t *offsets_count
)
380 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
381 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
382 const char *probe_name
= NULL
, *provider_name
= NULL
;
386 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
387 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
389 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
396 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
398 assert(lookup_method_type
==
399 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
402 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
409 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
411 if (!provider_name
) {
416 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
422 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
423 uid
, gid
, offsets
, offsets_count
);
425 DBG("userspace probe offset calculation failed for sdt "
426 "probe %s:%s", provider_name
, probe_name
);
430 if (*offsets_count
== 0) {
431 DBG("no userspace probe offset found");
435 DBG("%u userspace probe SDT offsets found for %s:%s at:",
436 *offsets_count
, provider_name
, probe_name
);
437 for (i
= 0; i
< *offsets_count
; i
++) {
438 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
445 int userspace_probe_add_callsite(
446 const struct lttng_userspace_probe_location
*location
,
447 uid_t uid
, gid_t gid
, int fd
)
449 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
450 enum lttng_userspace_probe_location_lookup_method_type type
;
453 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
454 if (!lookup_method
) {
459 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
461 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
463 struct lttng_kernel_abi_event_callsite callsite
;
466 ret
= extract_userspace_probe_offset_function_elf(location
,
469 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
473 callsite
.u
.uprobe
.offset
= sanitize_uprobe_offset(offset
);
474 ret
= kernctl_add_callsite(fd
, &callsite
);
476 WARN("Failed to add callsite to ELF userspace probe.");
477 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
482 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
485 uint64_t *offsets
= NULL
;
486 uint32_t offsets_count
;
487 struct lttng_kernel_abi_event_callsite callsite
;
490 * This call allocates the offsets buffer. This buffer must be freed
493 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
494 uid
, gid
, &offsets
, &offsets_count
);
496 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
499 for (i
= 0; i
< offsets_count
; i
++) {
500 callsite
.u
.uprobe
.offset
= sanitize_uprobe_offset(offsets
[i
]);
501 ret
= kernctl_add_callsite(fd
, &callsite
);
503 WARN("Failed to add callsite to SDT userspace probe");
504 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
513 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
521 * Extract the offsets of the instrumentation point for the different lookup
525 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
526 struct ltt_kernel_session
*session
, int fd
)
529 const struct lttng_userspace_probe_location
*location
= NULL
;
532 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
534 location
= lttng_event_get_userspace_probe_location(ev
);
540 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
543 WARN("Failed to add callsite to userspace probe event '%s'",
552 * Extract the offsets of the instrumentation point for the different look-up
555 static int userspace_probe_event_rule_add_callsites(
556 const struct lttng_event_rule
*rule
,
557 const struct lttng_credentials
*creds
,
561 enum lttng_event_rule_status status
;
562 enum lttng_event_rule_type event_rule_type
;
563 const struct lttng_userspace_probe_location
*location
= NULL
;
568 event_rule_type
= lttng_event_rule_get_type(rule
);
569 assert(event_rule_type
== LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
571 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
572 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
577 ret
= userspace_probe_add_callsite(location
,
578 lttng_credentials_get_uid(creds
),
579 lttng_credentials_get_gid(creds
), fd
);
581 WARN("Failed to add callsite to user space probe object: fd = %d",
590 * Create a kernel event, enable it to the kernel tracer and add it to the
591 * channel event list of the kernel session.
592 * We own filter_expression and filter.
594 int kernel_create_event(struct lttng_event
*ev
,
595 struct ltt_kernel_channel
*channel
,
596 char *filter_expression
,
597 struct lttng_bytecode
*filter
)
600 enum lttng_error_code ret
;
601 struct ltt_kernel_event
*event
;
606 /* We pass ownership of filter_expression and filter */
607 ret
= trace_kernel_create_event(ev
, filter_expression
,
609 if (ret
!= LTTNG_OK
) {
613 fd
= kernctl_create_event(channel
->fd
, event
->event
);
617 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
620 WARN("Event type not implemented");
621 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
624 WARN("Event %s not found!", ev
->name
);
625 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
628 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
629 PERROR("create event ioctl");
634 event
->type
= ev
->type
;
636 /* Prevent fd duplication after execlp() */
637 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
639 PERROR("fcntl session fd");
643 err
= kernctl_filter(event
->fd
, filter
);
647 ret
= LTTNG_ERR_FILTER_NOMEM
;
650 ret
= LTTNG_ERR_FILTER_INVAL
;
657 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
658 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
661 goto add_callsite_error
;
665 err
= kernctl_enable(event
->fd
);
669 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
672 PERROR("enable kernel event");
673 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
679 /* Add event to event list */
680 cds_list_add(&event
->list
, &channel
->events_list
.head
);
681 channel
->event_count
++;
683 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
693 closeret
= close(event
->fd
);
695 PERROR("close event fd");
705 * Disable a kernel channel.
707 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
713 ret
= kernctl_disable(chan
->fd
);
715 PERROR("disable chan ioctl");
720 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
721 chan
->channel
->name
, chan
->fd
, chan
->key
);
730 * Enable a kernel channel.
732 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
738 ret
= kernctl_enable(chan
->fd
);
739 if (ret
< 0 && ret
!= -EEXIST
) {
740 PERROR("Enable kernel chan");
745 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
746 chan
->channel
->name
, chan
->fd
, chan
->key
);
755 * Enable a kernel event.
757 int kernel_enable_event(struct ltt_kernel_event
*event
)
763 ret
= kernctl_enable(event
->fd
);
767 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
770 PERROR("enable kernel event");
777 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
786 * Disable a kernel event.
788 int kernel_disable_event(struct ltt_kernel_event
*event
)
794 ret
= kernctl_disable(event
->fd
);
796 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
797 event
->event
->name
, event
->fd
);
802 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
811 * Disable a kernel event notifier.
814 int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
821 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
824 ret
= kernctl_disable(event
->fd
);
826 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
827 event
->fd
, event
->token
);
832 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
,
833 event
->fd
, event
->token
);
840 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
841 struct ltt_kernel_session
*session
,
842 enum lttng_process_attr process_attr
)
844 switch (process_attr
) {
845 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
846 return session
->tracker_pid
;
847 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
848 return session
->tracker_vpid
;
849 case LTTNG_PROCESS_ATTR_USER_ID
:
850 return session
->tracker_uid
;
851 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
852 return session
->tracker_vuid
;
853 case LTTNG_PROCESS_ATTR_GROUP_ID
:
854 return session
->tracker_gid
;
855 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
856 return session
->tracker_vgid
;
862 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
863 struct ltt_kernel_session
*session
,
864 enum lttng_process_attr process_attr
)
866 return (const struct process_attr_tracker
*)
867 _kernel_get_process_attr_tracker(session
, process_attr
);
870 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
871 struct ltt_kernel_session
*session
,
872 enum lttng_process_attr process_attr
,
873 enum lttng_tracking_policy policy
)
876 enum lttng_error_code ret_code
= LTTNG_OK
;
877 struct process_attr_tracker
*tracker
=
878 _kernel_get_process_attr_tracker(session
, process_attr
);
879 enum lttng_tracking_policy previous_policy
;
882 ret_code
= LTTNG_ERR_INVALID
;
886 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
887 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
889 ret_code
= LTTNG_ERR_UNK
;
893 if (previous_policy
== policy
) {
898 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
899 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
901 * Maintain a special case for the process ID process
902 * attribute tracker as it was the only supported
903 * attribute prior to 2.12.
905 ret
= kernctl_track_pid(session
->fd
, -1);
907 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
910 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
911 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
913 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
915 * Maintain a special case for the process ID process
916 * attribute tracker as it was the only supported
917 * attribute prior to 2.12.
919 ret
= kernctl_untrack_pid(session
->fd
, -1);
921 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
927 /* kern-ctl error handling */
933 ret_code
= LTTNG_ERR_INVALID
;
936 ret_code
= LTTNG_ERR_NOMEM
;
939 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
942 ret_code
= LTTNG_ERR_UNK
;
949 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
950 struct ltt_kernel_session
*session
,
951 enum lttng_process_attr process_attr
,
952 const struct process_attr_value
*value
)
954 int ret
, integral_value
;
955 enum lttng_error_code ret_code
;
956 struct process_attr_tracker
*tracker
;
957 enum process_attr_tracker_status status
;
960 * Convert process attribute tracker value to the integral
961 * representation required by the kern-ctl API.
963 switch (process_attr
) {
964 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
965 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
966 integral_value
= (int) value
->value
.pid
;
968 case LTTNG_PROCESS_ATTR_USER_ID
:
969 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
970 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
973 ret_code
= utils_user_id_from_name(
974 value
->value
.user_name
, &uid
);
975 if (ret_code
!= LTTNG_OK
) {
978 integral_value
= (int) uid
;
980 integral_value
= (int) value
->value
.uid
;
983 case LTTNG_PROCESS_ATTR_GROUP_ID
:
984 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
985 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
988 ret_code
= utils_group_id_from_name(
989 value
->value
.group_name
, &gid
);
990 if (ret_code
!= LTTNG_OK
) {
993 integral_value
= (int) gid
;
995 integral_value
= (int) value
->value
.gid
;
999 ret_code
= LTTNG_ERR_INVALID
;
1003 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1005 ret_code
= LTTNG_ERR_INVALID
;
1009 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1010 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1012 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1013 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1015 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1016 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1018 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1020 ret_code
= LTTNG_ERR_UNK
;
1026 DBG("Kernel track %s %d for session id %" PRIu64
,
1027 lttng_process_attr_to_string(process_attr
),
1028 integral_value
, session
->id
);
1029 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1031 * Maintain a special case for the process ID process attribute
1032 * tracker as it was the only supported attribute prior to 2.12.
1034 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1036 ret
= kernctl_track_id(
1037 session
->fd
, process_attr
, integral_value
);
1040 ret_code
= LTTNG_OK
;
1044 kernel_wait_quiescent();
1046 /* kern-ctl error handling */
1049 ret_code
= LTTNG_OK
;
1052 ret_code
= LTTNG_ERR_INVALID
;
1055 ret_code
= LTTNG_ERR_NOMEM
;
1058 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1061 ret_code
= LTTNG_ERR_UNK
;
1065 /* Attempt to remove the value from the tracker. */
1066 status
= process_attr_tracker_inclusion_set_remove_value(
1068 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1069 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1070 lttng_process_attr_to_string(process_attr
),
1077 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1078 struct ltt_kernel_session
*session
,
1079 enum lttng_process_attr process_attr
,
1080 const struct process_attr_value
*value
)
1082 int ret
, integral_value
;
1083 enum lttng_error_code ret_code
;
1084 struct process_attr_tracker
*tracker
;
1085 enum process_attr_tracker_status status
;
1088 * Convert process attribute tracker value to the integral
1089 * representation required by the kern-ctl API.
1091 switch (process_attr
) {
1092 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1093 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1094 integral_value
= (int) value
->value
.pid
;
1096 case LTTNG_PROCESS_ATTR_USER_ID
:
1097 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1098 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1101 ret_code
= utils_user_id_from_name(
1102 value
->value
.user_name
, &uid
);
1103 if (ret_code
!= LTTNG_OK
) {
1106 integral_value
= (int) uid
;
1108 integral_value
= (int) value
->value
.uid
;
1111 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1112 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1113 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1116 ret_code
= utils_group_id_from_name(
1117 value
->value
.group_name
, &gid
);
1118 if (ret_code
!= LTTNG_OK
) {
1121 integral_value
= (int) gid
;
1123 integral_value
= (int) value
->value
.gid
;
1127 ret_code
= LTTNG_ERR_INVALID
;
1131 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1133 ret_code
= LTTNG_ERR_INVALID
;
1137 status
= process_attr_tracker_inclusion_set_remove_value(
1139 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1141 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1142 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1144 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1145 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1147 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1149 ret_code
= LTTNG_ERR_UNK
;
1155 DBG("Kernel track %s %d for session id %" PRIu64
,
1156 lttng_process_attr_to_string(process_attr
),
1157 integral_value
, session
->id
);
1158 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1160 * Maintain a special case for the process ID process attribute
1161 * tracker as it was the only supported attribute prior to 2.12.
1163 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1165 ret
= kernctl_untrack_id(
1166 session
->fd
, process_attr
, integral_value
);
1169 ret_code
= LTTNG_OK
;
1172 kernel_wait_quiescent();
1174 /* kern-ctl error handling */
1177 ret_code
= LTTNG_OK
;
1180 ret_code
= LTTNG_ERR_INVALID
;
1183 ret_code
= LTTNG_ERR_NOMEM
;
1186 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1189 ret_code
= LTTNG_ERR_UNK
;
1193 /* Attempt to add the value to the tracker. */
1194 status
= process_attr_tracker_inclusion_set_add_value(
1196 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1197 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1198 lttng_process_attr_to_string(process_attr
),
1206 * Create kernel metadata, open from the kernel tracer and add it to the
1209 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1212 struct ltt_kernel_metadata
*lkm
= NULL
;
1216 /* Allocate kernel metadata */
1217 lkm
= trace_kernel_create_metadata();
1222 /* Kernel tracer metadata creation */
1223 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1229 lkm
->key
= ++next_kernel_channel_key
;
1230 /* Prevent fd duplication after execlp() */
1231 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1233 PERROR("fcntl session fd");
1236 session
->metadata
= lkm
;
1238 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1243 trace_kernel_destroy_metadata(lkm
);
1249 * Start tracing session.
1251 int kernel_start_session(struct ltt_kernel_session
*session
)
1257 ret
= kernctl_start_session(session
->fd
);
1259 PERROR("ioctl start session");
1263 DBG("Kernel session started");
1272 * Make a kernel wait to make sure in-flight probe have completed.
1274 void kernel_wait_quiescent(void)
1277 int fd
= kernel_tracer_fd
;
1279 DBG("Kernel quiescent wait on %d", fd
);
1281 ret
= kernctl_wait_quiescent(fd
);
1283 PERROR("wait quiescent ioctl");
1284 ERR("Kernel quiescent wait failed");
1289 * Force flush buffer of metadata.
1291 int kernel_metadata_flush_buffer(int fd
)
1295 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1297 ret
= kernctl_buffer_flush(fd
);
1299 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1306 * Force flush buffer for channel.
1308 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1311 struct ltt_kernel_stream
*stream
;
1315 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1317 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1318 DBG("Flushing channel stream %d", stream
->fd
);
1319 ret
= kernctl_buffer_flush(stream
->fd
);
1322 ERR("Fail to flush buffer for stream %d (ret: %d)",
1331 * Stop tracing session.
1333 int kernel_stop_session(struct ltt_kernel_session
*session
)
1339 ret
= kernctl_stop_session(session
->fd
);
1344 DBG("Kernel session stopped");
1353 * Open stream of channel, register it to the kernel tracer and add it
1354 * to the stream list of the channel.
1356 * Note: given that the streams may appear in random order wrt CPU
1357 * number (e.g. cpu hotplug), the index value of the stream number in
1358 * the stream name is not necessarily linked to the CPU number.
1360 * Return the number of created stream. Else, a negative value.
1362 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1365 struct ltt_kernel_stream
*lks
;
1369 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1370 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1371 channel
->stream_count
);
1381 /* Prevent fd duplication after execlp() */
1382 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1384 PERROR("fcntl session fd");
1387 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1388 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1390 /* Add stream to channel stream list */
1391 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1392 channel
->stream_count
++;
1394 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1398 return channel
->stream_count
;
1405 * Open the metadata stream and set it to the kernel session.
1407 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1413 ret
= kernctl_create_stream(session
->metadata
->fd
);
1415 PERROR("kernel create metadata stream");
1419 DBG("Kernel metadata stream created (fd: %d)", ret
);
1420 session
->metadata_stream_fd
= ret
;
1421 /* Prevent fd duplication after execlp() */
1422 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1424 PERROR("fcntl session fd");
1434 * Get the event list from the kernel tracer and return the number of elements.
1436 ssize_t
kernel_list_events(struct lttng_event
**events
)
1440 size_t nbmem
, count
= 0;
1442 struct lttng_event
*elist
;
1446 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1448 PERROR("kernel tracepoint list");
1452 fp
= fdopen(fd
, "r");
1454 PERROR("kernel tracepoint list fdopen");
1459 * Init memory size counter
1460 * See kernel-ctl.h for explanation of this value
1462 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1463 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1464 if (elist
== NULL
) {
1465 PERROR("alloc list events");
1470 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1471 if (count
>= nbmem
) {
1472 struct lttng_event
*new_elist
;
1475 new_nbmem
= nbmem
<< 1;
1476 DBG("Reallocating event list from %zu to %zu bytes",
1478 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1479 if (new_elist
== NULL
) {
1480 PERROR("realloc list events");
1486 /* Zero the new memory */
1487 memset(new_elist
+ nbmem
, 0,
1488 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1492 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1493 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1494 elist
[count
].enabled
= -1;
1500 DBG("Kernel list events done (%zu events)", count
);
1502 ret
= fclose(fp
); /* closes both fp and fd */
1518 * Get kernel version and validate it.
1520 int kernel_validate_version(struct lttng_kernel_abi_tracer_version
*version
,
1521 struct lttng_kernel_abi_tracer_abi_version
*abi_version
)
1525 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1527 ERR("Failed to retrieve the lttng-modules version");
1531 /* Validate version */
1532 if (version
->major
!= VERSION_MAJOR
) {
1533 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1534 version
->major
, VERSION_MAJOR
);
1537 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1539 ERR("Failed to retrieve lttng-modules ABI version");
1542 if (abi_version
->major
!= LTTNG_KERNEL_ABI_MAJOR_VERSION
) {
1543 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1544 abi_version
->major
, abi_version
->minor
,
1545 LTTNG_KERNEL_ABI_MAJOR_VERSION
);
1548 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1549 version
->major
, version
->minor
,
1550 abi_version
->major
, abi_version
->minor
);
1557 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1562 * Kernel work-arounds called at the start of sessiond main().
1564 int init_kernel_workarounds(void)
1570 * boot_id needs to be read once before being used concurrently
1571 * to deal with a Linux kernel race. A fix is proposed for
1572 * upstream, but the work-around is needed for older kernels.
1574 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1581 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1583 /* Ignore error, we don't really care */
1595 * Teardown of a kernel session, keeping data required by destroy notifiers.
1597 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1599 struct lttng_trace_chunk
*trace_chunk
;
1601 if (ksess
== NULL
) {
1602 DBG3("No kernel session when tearing down session");
1606 DBG("Tearing down kernel session");
1607 trace_chunk
= ksess
->current_trace_chunk
;
1610 * Destroy channels on the consumer if at least one FD has been sent and we
1611 * are in no output mode because the streams are in *no* monitor mode so we
1612 * have to send a command to clean them up or else they leaked.
1614 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1616 struct consumer_socket
*socket
;
1617 struct lttng_ht_iter iter
;
1619 /* For each consumer socket. */
1621 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1622 socket
, node
.node
) {
1623 struct ltt_kernel_channel
*chan
;
1625 /* For each channel, ask the consumer to destroy it. */
1626 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1627 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1629 /* Consumer is probably dead. Use next socket. */
1637 /* Close any relayd session */
1638 consumer_output_send_destroy_relayd(ksess
->consumer
);
1640 trace_kernel_destroy_session(ksess
);
1641 lttng_trace_chunk_put(trace_chunk
);
1644 /* Teardown of data required by destroy notifiers. */
1645 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1647 if (ksess
== NULL
) {
1650 trace_kernel_free_session(ksess
);
1654 * Destroy a kernel channel object. It does not do anything on the tracer side.
1656 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1658 struct ltt_kernel_session
*ksess
= NULL
;
1661 assert(kchan
->channel
);
1663 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1665 /* Update channel count of associated session. */
1666 if (kchan
->session
) {
1667 /* Keep pointer reference so we can update it after the destroy. */
1668 ksess
= kchan
->session
;
1671 trace_kernel_destroy_channel(kchan
);
1674 * At this point the kernel channel is not visible anymore. This is safe
1675 * since in order to work on a visible kernel session, the tracing session
1676 * lock (ltt_session.lock) MUST be acquired.
1679 ksess
->channel_count
--;
1684 * Take a snapshot for a given kernel session.
1686 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1688 enum lttng_error_code
kernel_snapshot_record(
1689 struct ltt_kernel_session
*ksess
,
1690 const struct consumer_output
*output
, int wait
,
1691 uint64_t nb_packets_per_stream
)
1693 int err
, ret
, saved_metadata_fd
;
1694 enum lttng_error_code status
= LTTNG_OK
;
1695 struct consumer_socket
*socket
;
1696 struct lttng_ht_iter iter
;
1697 struct ltt_kernel_metadata
*saved_metadata
;
1698 char *trace_path
= NULL
;
1699 size_t consumer_path_offset
= 0;
1702 assert(ksess
->consumer
);
1705 DBG("Kernel snapshot record started");
1707 /* Save current metadata since the following calls will change it. */
1708 saved_metadata
= ksess
->metadata
;
1709 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1713 ret
= kernel_open_metadata(ksess
);
1715 status
= LTTNG_ERR_KERN_META_FAIL
;
1719 ret
= kernel_open_metadata_stream(ksess
);
1721 status
= LTTNG_ERR_KERN_META_FAIL
;
1722 goto error_open_stream
;
1725 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1726 "", &consumer_path_offset
);
1728 status
= LTTNG_ERR_INVALID
;
1731 /* Send metadata to consumer and snapshot everything. */
1732 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1733 socket
, node
.node
) {
1734 struct ltt_kernel_channel
*chan
;
1736 pthread_mutex_lock(socket
->lock
);
1737 /* This stream must not be monitored by the consumer. */
1738 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1739 pthread_mutex_unlock(socket
->lock
);
1741 status
= LTTNG_ERR_KERN_META_FAIL
;
1742 goto error_consumer
;
1745 /* For each channel, ask the consumer to snapshot it. */
1746 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1747 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1748 ksess
->uid
, ksess
->gid
,
1749 &trace_path
[consumer_path_offset
], wait
,
1750 nb_packets_per_stream
);
1751 if (status
!= LTTNG_OK
) {
1752 (void) kernel_consumer_destroy_metadata(socket
,
1754 goto error_consumer
;
1758 /* Snapshot metadata, */
1759 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1760 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1762 if (status
!= LTTNG_OK
) {
1763 goto error_consumer
;
1767 * The metadata snapshot is done, ask the consumer to destroy it since
1768 * it's not monitored on the consumer side.
1770 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1774 /* Close newly opened metadata stream. It's now on the consumer side. */
1775 err
= close(ksess
->metadata_stream_fd
);
1777 PERROR("close snapshot kernel");
1781 trace_kernel_destroy_metadata(ksess
->metadata
);
1783 /* Restore metadata state.*/
1784 ksess
->metadata
= saved_metadata
;
1785 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1792 * Get the syscall mask array from the kernel tracer.
1794 * Return 0 on success else a negative value. In both case, syscall_mask should
1797 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1799 assert(syscall_mask
);
1802 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1806 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1809 struct lttng_kernel_abi_tracer_abi_version abi
;
1811 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1813 ERR("Failed to retrieve lttng-modules ABI version");
1817 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1823 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1826 * Return 1 on success, 0 when feature is not supported, negative value in case
1829 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1832 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1834 return kernel_tracer_abi_greater_or_equal(2, 3);
1838 * Check for the support of the packet sequence number via abi version number.
1840 * Return 1 on success, 0 when feature is not supported, negative value in case
1843 int kernel_supports_ring_buffer_packet_sequence_number(void)
1846 * Packet sequence number was introduced in LTTng 2.8,
1847 * lttng-modules ABI 2.1.
1849 return kernel_tracer_abi_greater_or_equal(2, 1);
1853 * Check for the support of event notifiers via abi version number.
1855 * Return 1 on success, 0 when feature is not supported, negative value in case
1858 int kernel_supports_event_notifiers(void)
1861 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1863 return kernel_tracer_abi_greater_or_equal(2, 6);
1867 * Rotate a kernel session.
1869 * Return LTTNG_OK on success or else an LTTng error code.
1871 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1874 enum lttng_error_code status
= LTTNG_OK
;
1875 struct consumer_socket
*socket
;
1876 struct lttng_ht_iter iter
;
1877 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1880 assert(ksess
->consumer
);
1882 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1883 session
->name
, session
->id
);
1888 * Note that this loop will end after one iteration given that there is
1889 * only one kernel consumer.
1891 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1892 socket
, node
.node
) {
1893 struct ltt_kernel_channel
*chan
;
1895 /* For each channel, ask the consumer to rotate it. */
1896 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1897 DBG("Rotate kernel channel %" PRIu64
", session %s",
1898 chan
->key
, session
->name
);
1899 ret
= consumer_rotate_channel(socket
, chan
->key
,
1900 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1901 /* is_metadata_channel */ false);
1903 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1909 * Rotate the metadata channel.
1911 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1912 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1913 /* is_metadata_channel */ true);
1915 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1925 enum lttng_error_code
kernel_create_channel_subdirectories(
1926 const struct ltt_kernel_session
*ksess
)
1928 enum lttng_error_code ret
= LTTNG_OK
;
1929 enum lttng_trace_chunk_status chunk_status
;
1932 assert(ksess
->current_trace_chunk
);
1935 * Create the index subdirectory which will take care
1936 * of implicitly creating the channel's path.
1938 chunk_status
= lttng_trace_chunk_create_subdirectory(
1939 ksess
->current_trace_chunk
,
1940 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1941 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1942 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1951 * Setup necessary data for kernel tracer action.
1954 int init_kernel_tracer(void)
1957 bool is_root
= !getuid();
1959 /* Modprobe lttng kernel modules */
1960 ret
= modprobe_lttng_control();
1965 /* Open debugfs lttng */
1966 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1967 if (kernel_tracer_fd
< 0) {
1968 DBG("Failed to open %s", module_proc_lttng
);
1972 /* Validate kernel version */
1973 ret
= kernel_validate_version(&the_kernel_tracer_version
,
1974 &the_kernel_tracer_abi_version
);
1979 ret
= modprobe_lttng_data();
1984 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1989 WARN("Kernel tracer does not support buffer monitoring. "
1990 "The monitoring timer of channels in the kernel domain "
1991 "will be set to 0 (disabled).");
1994 ret
= kernel_supports_event_notifiers();
1996 ERR("Failed to check for kernel tracer event notifier support");
1999 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
2001 /* This is not fatal. */
2002 WARN("Failed to create kernel event notifier group");
2003 kernel_tracer_event_notifier_group_fd
= -1;
2005 enum event_notifier_error_accounting_status error_accounting_status
;
2006 enum lttng_error_code error_code_ret
=
2007 kernel_create_event_notifier_group_notification_fd(
2008 &kernel_tracer_event_notifier_group_notification_fd
);
2010 if (error_code_ret
!= LTTNG_OK
) {
2014 error_accounting_status
= event_notifier_error_accounting_register_kernel(
2015 kernel_tracer_event_notifier_group_fd
);
2016 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2017 ERR("Failed to initialize event notifier error accounting for kernel tracer");
2018 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
2022 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
2023 DEFAULT_HT_SIZE
, 1, 0,
2024 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
2026 if (!kernel_token_to_event_notifier_rule_ht
) {
2027 goto error_token_ht
;
2031 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2032 kernel_tracer_fd
, kernel_tracer_event_notifier_group_fd
,
2033 kernel_tracer_event_notifier_group_notification_fd
);
2035 ret
= syscall_init_table(kernel_tracer_fd
);
2037 ERR("Unable to populate syscall table. Syscall tracing won't "
2038 "work for this session daemon.");
2044 modprobe_remove_lttng_control();
2045 ret
= close(kernel_tracer_fd
);
2047 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2051 kernel_tracer_fd
= -1;
2052 return LTTNG_ERR_KERN_VERSION
;
2056 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2058 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2059 kernel_tracer_event_notifier_group_notification_fd
);
2062 kernel_tracer_event_notifier_group_notification_fd
= -1;
2065 ret
= close(kernel_tracer_event_notifier_group_fd
);
2067 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2068 kernel_tracer_event_notifier_group_fd
);
2071 kernel_tracer_event_notifier_group_fd
= -1;
2073 ret
= close(kernel_tracer_fd
);
2075 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2079 kernel_tracer_fd
= -1;
2082 modprobe_remove_lttng_control();
2085 WARN("No kernel tracer available");
2086 kernel_tracer_fd
= -1;
2088 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2090 return LTTNG_ERR_KERN_NA
;
2095 void cleanup_kernel_tracer(void)
2097 DBG2("Closing kernel event notifier group notification file descriptor");
2098 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2099 int ret
= notification_thread_command_remove_tracer_event_source(
2100 the_notification_thread_handle
,
2101 kernel_tracer_event_notifier_group_notification_fd
);
2102 if (ret
!= LTTNG_OK
) {
2103 ERR("Failed to remove kernel event notifier notification from notification thread");
2106 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2108 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2109 kernel_tracer_event_notifier_group_notification_fd
);
2112 kernel_tracer_event_notifier_group_notification_fd
= -1;
2115 if (kernel_token_to_event_notifier_rule_ht
) {
2116 const int ret
= cds_lfht_destroy(
2117 kernel_token_to_event_notifier_rule_ht
, NULL
);
2121 DBG2("Closing kernel event notifier group file descriptor");
2122 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2123 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2126 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2127 kernel_tracer_event_notifier_group_fd
);
2130 kernel_tracer_event_notifier_group_fd
= -1;
2133 DBG2("Closing kernel fd");
2134 if (kernel_tracer_fd
>= 0) {
2135 const int ret
= close(kernel_tracer_fd
);
2138 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2142 kernel_tracer_fd
= -1;
2145 free(syscall_table
);
2149 bool kernel_tracer_is_initialized(void)
2151 return kernel_tracer_fd
>= 0;
2155 * Clear a kernel session.
2157 * Return LTTNG_OK on success or else an LTTng error code.
2159 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2162 enum lttng_error_code status
= LTTNG_OK
;
2163 struct consumer_socket
*socket
;
2164 struct lttng_ht_iter iter
;
2165 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2168 assert(ksess
->consumer
);
2170 DBG("Clear kernel session %s (session %" PRIu64
")",
2171 session
->name
, session
->id
);
2175 if (ksess
->active
) {
2176 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2177 status
= LTTNG_ERR_FATAL
;
2182 * Note that this loop will end after one iteration given that there is
2183 * only one kernel consumer.
2185 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2186 socket
, node
.node
) {
2187 struct ltt_kernel_channel
*chan
;
2189 /* For each channel, ask the consumer to clear it. */
2190 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2191 DBG("Clear kernel channel %" PRIu64
", session %s",
2192 chan
->key
, session
->name
);
2193 ret
= consumer_clear_channel(socket
, chan
->key
);
2199 if (!ksess
->metadata
) {
2201 * Nothing to do for the metadata.
2202 * This is a snapshot session.
2203 * The metadata is genererated on the fly.
2209 * Clear the metadata channel.
2210 * Metadata channel is not cleared per se but we still need to
2211 * perform a rotation operation on it behind the scene.
2213 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2222 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2223 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2226 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2234 enum lttng_error_code
kernel_create_event_notifier_group_notification_fd(
2235 int *event_notifier_group_notification_fd
)
2237 int local_fd
= -1, ret
;
2238 enum lttng_error_code error_code_ret
;
2240 assert(event_notifier_group_notification_fd
);
2242 ret
= kernctl_create_event_notifier_group_notification_fd(
2243 kernel_tracer_event_notifier_group_fd
);
2245 PERROR("Failed to create kernel event notifier group notification file descriptor");
2246 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2252 /* Prevent fd duplication after execlp(). */
2253 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2255 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2257 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2261 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2263 error_code_ret
= LTTNG_OK
;
2264 *event_notifier_group_notification_fd
= local_fd
;
2268 if (local_fd
>= 0) {
2269 ret
= close(local_fd
);
2271 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2276 return error_code_ret
;
2279 enum lttng_error_code
kernel_destroy_event_notifier_group_notification_fd(
2280 int event_notifier_group_notification_fd
)
2282 enum lttng_error_code ret_code
= LTTNG_OK
;
2284 DBG("Closing event notifier group notification file descriptor: fd = %d",
2285 event_notifier_group_notification_fd
);
2286 if (event_notifier_group_notification_fd
>= 0) {
2287 const int ret
= close(event_notifier_group_notification_fd
);
2289 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2290 event_notifier_group_notification_fd
);
2298 unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2300 const struct lttng_condition
*condition
=
2301 lttng_trigger_get_const_condition(trigger
);
2303 return lttng_condition_hash(condition
);
2307 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2309 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2310 const struct lttng_trigger
*trigger
= key
;
2312 event_notifier_rule
= caa_container_of(node
,
2313 const struct ltt_kernel_event_notifier_rule
, ht_node
);
2315 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2318 static enum lttng_error_code
kernel_create_event_notifier_rule(
2319 struct lttng_trigger
*trigger
,
2320 const struct lttng_credentials
*creds
, uint64_t token
)
2322 int err
, fd
, ret
= 0;
2323 enum lttng_error_code error_code_ret
;
2324 enum lttng_condition_status condition_status
;
2325 enum lttng_condition_type condition_type
;
2326 enum lttng_event_rule_type event_rule_type
;
2327 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2328 struct lttng_kernel_abi_event_notifier kernel_event_notifier
= {};
2329 unsigned int capture_bytecode_count
= 0, i
;
2330 const struct lttng_condition
*condition
= NULL
;
2331 const struct lttng_event_rule
*event_rule
= NULL
;
2332 enum lttng_condition_status cond_status
;
2336 condition
= lttng_trigger_get_const_condition(trigger
);
2339 condition_type
= lttng_condition_get_type(condition
);
2340 assert(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2342 /* Does not acquire a reference. */
2343 condition_status
= lttng_condition_event_rule_matches_get_rule(
2344 condition
, &event_rule
);
2345 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2348 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2349 assert(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2351 error_code_ret
= trace_kernel_create_event_notifier_rule(trigger
, token
,
2352 lttng_condition_event_rule_matches_get_error_counter_index(
2354 &event_notifier_rule
);
2355 if (error_code_ret
!= LTTNG_OK
) {
2359 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(
2360 event_rule
, &kernel_event_notifier
);
2361 if (error_code_ret
!= LTTNG_OK
) {
2365 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2366 kernel_event_notifier
.error_counter_idx
=
2367 lttng_condition_event_rule_matches_get_error_counter_index(
2370 fd
= kernctl_create_event_notifier(
2371 kernel_tracer_event_notifier_group_fd
,
2372 &kernel_event_notifier
);
2376 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2379 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2380 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2383 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2384 kernel_event_notifier
.event
.name
);
2385 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2388 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2389 fd
, kernel_event_notifier
.event
.name
);
2390 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2395 event_notifier_rule
->fd
= fd
;
2396 /* Prevent fd duplication after execlp(). */
2397 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2399 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2401 error_code_ret
= LTTNG_ERR_FATAL
;
2402 goto set_cloexec_error
;
2405 if (event_notifier_rule
->filter
) {
2406 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2410 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2413 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2420 if (lttng_event_rule_get_type(event_rule
) ==
2421 LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
) {
2422 ret
= userspace_probe_event_rule_add_callsites(
2423 event_rule
, creds
, event_notifier_rule
->fd
);
2425 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2426 goto add_callsite_error
;
2430 /* Set the capture bytecode if any. */
2431 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2432 condition
, &capture_bytecode_count
);
2433 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2435 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2436 const struct lttng_bytecode
*capture_bytecode
=
2437 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2440 if (capture_bytecode
== NULL
) {
2441 ERR("Unexpected NULL capture bytecode on condition");
2442 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2446 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2448 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2449 event_notifier_rule
->fd
);
2450 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2455 err
= kernctl_enable(event_notifier_rule
->fd
);
2459 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2462 PERROR("enable kernel event notifier");
2463 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2469 /* Add trigger to kernel token mapping in the hash table. */
2471 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
, hash_trigger(trigger
),
2472 &event_notifier_rule
->ht_node
);
2475 DBG("Created kernel event notifier: name = '%s', fd = %d",
2476 kernel_event_notifier
.event
.name
,
2477 event_notifier_rule
->fd
);
2487 const int close_ret
= close(event_notifier_rule
->fd
);
2490 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2491 event_notifier_rule
->fd
);
2495 free(event_notifier_rule
);
2497 return error_code_ret
;
2500 enum lttng_error_code
kernel_register_event_notifier(
2501 struct lttng_trigger
*trigger
,
2502 const struct lttng_credentials
*cmd_creds
)
2504 enum lttng_error_code ret
;
2505 enum lttng_condition_status status
;
2506 enum lttng_domain_type domain_type
;
2507 const struct lttng_event_rule
*event_rule
;
2508 const struct lttng_condition
*const condition
=
2509 lttng_trigger_get_const_condition(trigger
);
2510 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2514 /* Does not acquire a reference to the event rule. */
2515 status
= lttng_condition_event_rule_matches_get_rule(
2516 condition
, &event_rule
);
2517 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2519 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2520 assert(domain_type
== LTTNG_DOMAIN_KERNEL
);
2522 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2523 if (ret
!= LTTNG_OK
) {
2524 ERR("Failed to create kernel event notifier rule");
2530 enum lttng_error_code
kernel_unregister_event_notifier(
2531 const struct lttng_trigger
*trigger
)
2533 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2534 struct cds_lfht_node
*node
;
2535 struct cds_lfht_iter iter
;
2536 enum lttng_error_code error_code_ret
;
2541 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2542 hash_trigger(trigger
), match_trigger
, trigger
, &iter
);
2544 node
= cds_lfht_iter_get_node(&iter
);
2546 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2550 token_event_rule_element
= caa_container_of(node
,
2551 struct ltt_kernel_event_notifier_rule
, ht_node
);
2553 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2555 error_code_ret
= LTTNG_ERR_FATAL
;
2559 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2560 error_code_ret
= LTTNG_OK
;
2565 return error_code_ret
;
2568 int kernel_get_notification_fd(void)
2570 return kernel_tracer_event_notifier_group_notification_fd
;