2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
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/on-event.h>
32 #include <lttng/condition/on-event-internal.h>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.h>
35 #include <lttng/event-rule/userspace-probe-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 * Add context on a kernel channel.
68 * Assumes the ownership of ctx.
70 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
71 struct ltt_kernel_context
*ctx
)
78 DBG("Adding context to channel %s", chan
->channel
->name
);
79 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
83 /* Exists but not available for this kernel */
84 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
87 /* If EEXIST, we just ignore the error */
91 PERROR("add context ioctl");
92 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
99 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
104 trace_kernel_destroy_context(ctx
);
110 * Create a new kernel session, register it to the kernel tracer and add it to
111 * the session daemon session.
113 int kernel_create_session(struct ltt_session
*session
)
116 struct ltt_kernel_session
*lks
;
120 /* Allocate data structure */
121 lks
= trace_kernel_create_session();
127 /* Kernel tracer session creation */
128 ret
= kernctl_create_session(kernel_tracer_fd
);
130 PERROR("ioctl kernel create session");
135 /* Prevent fd duplication after execlp() */
136 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
138 PERROR("fcntl session fd");
141 lks
->id
= session
->id
;
142 lks
->consumer_fds_sent
= 0;
143 session
->kernel_session
= lks
;
145 DBG("Kernel session created (fd: %d)", lks
->fd
);
148 * This is necessary since the creation time is present in the session
149 * name when it is generated.
151 if (session
->has_auto_generated_name
) {
152 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
154 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
157 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
158 session
->id
, session
->name
);
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",
164 session
->id
, session
->name
);
171 trace_kernel_destroy_session(lks
);
172 trace_kernel_free_session(lks
);
178 * Create a kernel channel, register it to the kernel tracer and add it to the
181 int kernel_create_channel(struct ltt_kernel_session
*session
,
182 struct lttng_channel
*chan
)
185 struct ltt_kernel_channel
*lkc
;
190 /* Allocate kernel channel */
191 lkc
= trace_kernel_create_channel(chan
);
196 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
197 chan
->name
, lkc
->channel
->attr
.overwrite
,
198 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
199 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
200 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
202 /* Kernel tracer channel creation */
203 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
205 PERROR("ioctl kernel create channel");
209 /* Setup the channel fd */
211 /* Prevent fd duplication after execlp() */
212 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
214 PERROR("fcntl session fd");
217 /* Add channel to session */
218 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
219 session
->channel_count
++;
220 lkc
->session
= session
;
221 lkc
->key
= ++next_kernel_channel_key
;
223 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
224 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
237 * Create a kernel event notifier group, register it to the kernel tracer and
238 * add it to the kernel session.
240 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
245 assert(event_notifier_group_fd
);
247 /* Kernel event notifier group creation. */
248 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
250 PERROR("Failed to create kernel event notifier group");
257 /* Prevent fd duplication after execlp(). */
258 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
260 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
265 DBG("Created kernel event notifier group: fd = %d", local_fd
);
266 *event_notifier_group_fd
= local_fd
;
271 ret
= close(local_fd
);
273 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
282 * Compute the offset of the instrumentation byte in the binary based on the
283 * function probe location using the ELF lookup method.
285 * Returns 0 on success and set the offset out parameter to the offset of the
287 * Returns -1 on error
290 int extract_userspace_probe_offset_function_elf(
291 const struct lttng_userspace_probe_location
*probe_location
,
292 uid_t uid
, gid_t gid
, uint64_t *offset
)
296 const char *symbol
= NULL
;
297 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
298 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
300 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
301 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
303 lookup
= lttng_userspace_probe_location_get_lookup_method(
311 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
313 assert(lookup_method_type
==
314 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
316 symbol
= lttng_userspace_probe_location_function_get_function_name(
323 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
329 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
331 DBG("userspace probe offset calculation failed for "
332 "function %s", symbol
);
336 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
342 * Compute the offsets of the instrumentation bytes in the binary based on the
343 * tracepoint probe location using the SDT lookup method. This function
344 * allocates the offsets buffer, the caller must free it.
346 * Returns 0 on success and set the offset out parameter to the offsets of the
348 * Returns -1 on error.
351 int extract_userspace_probe_offset_tracepoint_sdt(
352 const struct lttng_userspace_probe_location
*probe_location
,
353 uid_t uid
, gid_t gid
, uint64_t **offsets
,
354 uint32_t *offsets_count
)
356 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
357 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
358 const char *probe_name
= NULL
, *provider_name
= NULL
;
362 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
363 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
365 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
372 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
374 assert(lookup_method_type
==
375 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
378 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
385 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
387 if (!provider_name
) {
392 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
398 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
399 uid
, gid
, offsets
, offsets_count
);
401 DBG("userspace probe offset calculation failed for sdt "
402 "probe %s:%s", provider_name
, probe_name
);
406 if (*offsets_count
== 0) {
407 DBG("no userspace probe offset found");
411 DBG("%u userspace probe SDT offsets found for %s:%s at:",
412 *offsets_count
, provider_name
, probe_name
);
413 for (i
= 0; i
< *offsets_count
; i
++) {
414 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
421 int userspace_probe_add_callsite(
422 const struct lttng_userspace_probe_location
*location
,
423 uid_t uid
, gid_t gid
, int fd
)
425 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
426 enum lttng_userspace_probe_location_lookup_method_type type
;
429 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
430 if (!lookup_method
) {
435 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
437 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
439 struct lttng_kernel_event_callsite callsite
;
442 ret
= extract_userspace_probe_offset_function_elf(location
,
445 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
449 callsite
.u
.uprobe
.offset
= offset
;
450 ret
= kernctl_add_callsite(fd
, &callsite
);
452 WARN("Failed to add callsite to ELF userspace probe.");
453 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
458 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
461 uint64_t *offsets
= NULL
;
462 uint32_t offsets_count
;
463 struct lttng_kernel_event_callsite callsite
;
466 * This call allocates the offsets buffer. This buffer must be freed
469 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
470 uid
, gid
, &offsets
, &offsets_count
);
472 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
475 for (i
= 0; i
< offsets_count
; i
++) {
476 callsite
.u
.uprobe
.offset
= offsets
[i
];
477 ret
= kernctl_add_callsite(fd
, &callsite
);
479 WARN("Failed to add callsite to SDT userspace probe");
480 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
489 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
497 * Extract the offsets of the instrumentation point for the different lookup
501 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
502 struct ltt_kernel_session
*session
, int fd
)
505 const struct lttng_userspace_probe_location
*location
= NULL
;
508 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
510 location
= lttng_event_get_userspace_probe_location(ev
);
516 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
519 WARN("Failed to add callsite to userspace probe event '%s'",
528 * Extract the offsets of the instrumentation point for the different look-up
531 static int userspace_probe_event_rule_add_callsites(
532 const struct lttng_event_rule
*rule
,
533 const struct lttng_credentials
*creds
,
537 enum lttng_event_rule_status status
;
538 enum lttng_event_rule_type event_rule_type
;
539 const struct lttng_userspace_probe_location
*location
= NULL
;
544 event_rule_type
= lttng_event_rule_get_type(rule
);
545 assert(event_rule_type
== LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
);
547 status
= lttng_event_rule_userspace_probe_get_location(rule
, &location
);
548 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
553 ret
= userspace_probe_add_callsite(location
,
554 lttng_credentials_get_uid(creds
),
555 lttng_credentials_get_gid(creds
), fd
);
557 WARN("Failed to add callsite to user space probe object: fd = %d",
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
;
582 /* We pass ownership of filter_expression and filter */
583 ret
= trace_kernel_create_event(ev
, filter_expression
,
585 if (ret
!= LTTNG_OK
) {
589 fd
= kernctl_create_event(channel
->fd
, event
->event
);
593 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
596 WARN("Event type not implemented");
597 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
600 WARN("Event %s not found!", ev
->name
);
601 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
604 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
605 PERROR("create event ioctl");
610 event
->type
= ev
->type
;
612 /* Prevent fd duplication after execlp() */
613 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
615 PERROR("fcntl session fd");
619 err
= kernctl_filter(event
->fd
, filter
);
623 ret
= LTTNG_ERR_FILTER_NOMEM
;
626 ret
= LTTNG_ERR_FILTER_INVAL
;
633 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
634 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
637 goto add_callsite_error
;
641 err
= kernctl_enable(event
->fd
);
645 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
648 PERROR("enable kernel event");
649 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
655 /* Add event to event list */
656 cds_list_add(&event
->list
, &channel
->events_list
.head
);
657 channel
->event_count
++;
659 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
669 closeret
= close(event
->fd
);
671 PERROR("close event fd");
681 * Disable a kernel channel.
683 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
689 ret
= kernctl_disable(chan
->fd
);
691 PERROR("disable chan ioctl");
696 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
697 chan
->channel
->name
, chan
->fd
, chan
->key
);
706 * Enable a kernel channel.
708 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
714 ret
= kernctl_enable(chan
->fd
);
715 if (ret
< 0 && ret
!= -EEXIST
) {
716 PERROR("Enable kernel chan");
721 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
722 chan
->channel
->name
, chan
->fd
, chan
->key
);
731 * Enable a kernel event.
733 int kernel_enable_event(struct ltt_kernel_event
*event
)
739 ret
= kernctl_enable(event
->fd
);
743 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
746 PERROR("enable kernel event");
753 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
762 * Disable a kernel event.
764 int kernel_disable_event(struct ltt_kernel_event
*event
)
770 ret
= kernctl_disable(event
->fd
);
772 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
773 event
->event
->name
, event
->fd
);
778 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
787 * Disable a kernel event notifier.
790 int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
797 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
800 ret
= kernctl_disable(event
->fd
);
802 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
803 event
->fd
, event
->token
);
808 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
,
809 event
->fd
, event
->token
);
816 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
817 struct ltt_kernel_session
*session
,
818 enum lttng_process_attr process_attr
)
820 switch (process_attr
) {
821 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
822 return session
->tracker_pid
;
823 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
824 return session
->tracker_vpid
;
825 case LTTNG_PROCESS_ATTR_USER_ID
:
826 return session
->tracker_uid
;
827 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
828 return session
->tracker_vuid
;
829 case LTTNG_PROCESS_ATTR_GROUP_ID
:
830 return session
->tracker_gid
;
831 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
832 return session
->tracker_vgid
;
838 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
839 struct ltt_kernel_session
*session
,
840 enum lttng_process_attr process_attr
)
842 return (const struct process_attr_tracker
*)
843 _kernel_get_process_attr_tracker(session
, process_attr
);
846 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
847 struct ltt_kernel_session
*session
,
848 enum lttng_process_attr process_attr
,
849 enum lttng_tracking_policy policy
)
852 enum lttng_error_code ret_code
= LTTNG_OK
;
853 struct process_attr_tracker
*tracker
=
854 _kernel_get_process_attr_tracker(session
, process_attr
);
855 enum lttng_tracking_policy previous_policy
;
858 ret_code
= LTTNG_ERR_INVALID
;
862 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
863 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
865 ret_code
= LTTNG_ERR_UNK
;
869 if (previous_policy
== policy
) {
874 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
875 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
877 * Maintain a special case for the process ID process
878 * attribute tracker as it was the only supported
879 * attribute prior to 2.12.
881 ret
= kernctl_track_pid(session
->fd
, -1);
883 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
886 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
887 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
889 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
891 * Maintain a special case for the process ID process
892 * attribute tracker as it was the only supported
893 * attribute prior to 2.12.
895 ret
= kernctl_untrack_pid(session
->fd
, -1);
897 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
903 /* kern-ctl error handling */
909 ret_code
= LTTNG_ERR_INVALID
;
912 ret_code
= LTTNG_ERR_NOMEM
;
915 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
918 ret_code
= LTTNG_ERR_UNK
;
925 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
926 struct ltt_kernel_session
*session
,
927 enum lttng_process_attr process_attr
,
928 const struct process_attr_value
*value
)
930 int ret
, integral_value
;
931 enum lttng_error_code ret_code
;
932 struct process_attr_tracker
*tracker
;
933 enum process_attr_tracker_status status
;
936 * Convert process attribute tracker value to the integral
937 * representation required by the kern-ctl API.
939 switch (process_attr
) {
940 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
941 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
942 integral_value
= (int) value
->value
.pid
;
944 case LTTNG_PROCESS_ATTR_USER_ID
:
945 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
946 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
949 ret_code
= utils_user_id_from_name(
950 value
->value
.user_name
, &uid
);
951 if (ret_code
!= LTTNG_OK
) {
954 integral_value
= (int) uid
;
956 integral_value
= (int) value
->value
.uid
;
959 case LTTNG_PROCESS_ATTR_GROUP_ID
:
960 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
961 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
964 ret_code
= utils_group_id_from_name(
965 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
),
1004 integral_value
, session
->id
);
1005 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1007 * Maintain a special case for the process ID process attribute
1008 * tracker as it was the only supported attribute prior to 2.12.
1010 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1012 ret
= kernctl_track_id(
1013 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(
1044 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1045 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1046 lttng_process_attr_to_string(process_attr
),
1053 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1054 struct ltt_kernel_session
*session
,
1055 enum lttng_process_attr process_attr
,
1056 const struct process_attr_value
*value
)
1058 int ret
, integral_value
;
1059 enum lttng_error_code ret_code
;
1060 struct process_attr_tracker
*tracker
;
1061 enum process_attr_tracker_status status
;
1064 * Convert process attribute tracker value to the integral
1065 * representation required by the kern-ctl API.
1067 switch (process_attr
) {
1068 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1069 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1070 integral_value
= (int) value
->value
.pid
;
1072 case LTTNG_PROCESS_ATTR_USER_ID
:
1073 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1074 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1077 ret_code
= utils_user_id_from_name(
1078 value
->value
.user_name
, &uid
);
1079 if (ret_code
!= LTTNG_OK
) {
1082 integral_value
= (int) uid
;
1084 integral_value
= (int) value
->value
.uid
;
1087 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1088 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1089 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1092 ret_code
= utils_group_id_from_name(
1093 value
->value
.group_name
, &gid
);
1094 if (ret_code
!= LTTNG_OK
) {
1097 integral_value
= (int) gid
;
1099 integral_value
= (int) value
->value
.gid
;
1103 ret_code
= LTTNG_ERR_INVALID
;
1107 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1109 ret_code
= LTTNG_ERR_INVALID
;
1113 status
= process_attr_tracker_inclusion_set_remove_value(
1115 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1117 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1118 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1120 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1121 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1123 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1125 ret_code
= LTTNG_ERR_UNK
;
1131 DBG("Kernel track %s %d for session id %" PRIu64
,
1132 lttng_process_attr_to_string(process_attr
),
1133 integral_value
, session
->id
);
1134 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1136 * Maintain a special case for the process ID process attribute
1137 * tracker as it was the only supported attribute prior to 2.12.
1139 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1141 ret
= kernctl_untrack_id(
1142 session
->fd
, process_attr
, integral_value
);
1145 ret_code
= LTTNG_OK
;
1148 kernel_wait_quiescent();
1150 /* kern-ctl error handling */
1153 ret_code
= LTTNG_OK
;
1156 ret_code
= LTTNG_ERR_INVALID
;
1159 ret_code
= LTTNG_ERR_NOMEM
;
1162 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1165 ret_code
= LTTNG_ERR_UNK
;
1169 /* Attempt to add the value to the tracker. */
1170 status
= process_attr_tracker_inclusion_set_add_value(
1172 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1173 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1174 lttng_process_attr_to_string(process_attr
),
1182 * Create kernel metadata, open from the kernel tracer and add it to the
1185 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1188 struct ltt_kernel_metadata
*lkm
= NULL
;
1192 /* Allocate kernel metadata */
1193 lkm
= trace_kernel_create_metadata();
1198 /* Kernel tracer metadata creation */
1199 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1205 lkm
->key
= ++next_kernel_channel_key
;
1206 /* Prevent fd duplication after execlp() */
1207 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1209 PERROR("fcntl session fd");
1212 session
->metadata
= lkm
;
1214 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1219 trace_kernel_destroy_metadata(lkm
);
1225 * Start tracing session.
1227 int kernel_start_session(struct ltt_kernel_session
*session
)
1233 ret
= kernctl_start_session(session
->fd
);
1235 PERROR("ioctl start session");
1239 DBG("Kernel session started");
1248 * Make a kernel wait to make sure in-flight probe have completed.
1250 void kernel_wait_quiescent(void)
1253 int fd
= kernel_tracer_fd
;
1255 DBG("Kernel quiescent wait on %d", fd
);
1257 ret
= kernctl_wait_quiescent(fd
);
1259 PERROR("wait quiescent ioctl");
1260 ERR("Kernel quiescent wait failed");
1265 * Force flush buffer of metadata.
1267 int kernel_metadata_flush_buffer(int fd
)
1271 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1273 ret
= kernctl_buffer_flush(fd
);
1275 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1282 * Force flush buffer for channel.
1284 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1287 struct ltt_kernel_stream
*stream
;
1291 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1293 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1294 DBG("Flushing channel stream %d", stream
->fd
);
1295 ret
= kernctl_buffer_flush(stream
->fd
);
1298 ERR("Fail to flush buffer for stream %d (ret: %d)",
1307 * Stop tracing session.
1309 int kernel_stop_session(struct ltt_kernel_session
*session
)
1315 ret
= kernctl_stop_session(session
->fd
);
1320 DBG("Kernel session stopped");
1329 * Open stream of channel, register it to the kernel tracer and add it
1330 * to the stream list of the channel.
1332 * Note: given that the streams may appear in random order wrt CPU
1333 * number (e.g. cpu hotplug), the index value of the stream number in
1334 * the stream name is not necessarily linked to the CPU number.
1336 * Return the number of created stream. Else, a negative value.
1338 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1341 struct ltt_kernel_stream
*lks
;
1345 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1346 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1347 channel
->stream_count
);
1357 /* Prevent fd duplication after execlp() */
1358 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1360 PERROR("fcntl session fd");
1363 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1364 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1366 /* Add stream to channel stream list */
1367 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1368 channel
->stream_count
++;
1370 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1374 return channel
->stream_count
;
1381 * Open the metadata stream and set it to the kernel session.
1383 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1389 ret
= kernctl_create_stream(session
->metadata
->fd
);
1391 PERROR("kernel create metadata stream");
1395 DBG("Kernel metadata stream created (fd: %d)", ret
);
1396 session
->metadata_stream_fd
= ret
;
1397 /* Prevent fd duplication after execlp() */
1398 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1400 PERROR("fcntl session fd");
1410 * Get the event list from the kernel tracer and return the number of elements.
1412 ssize_t
kernel_list_events(struct lttng_event
**events
)
1416 size_t nbmem
, count
= 0;
1418 struct lttng_event
*elist
;
1422 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1424 PERROR("kernel tracepoint list");
1428 fp
= fdopen(fd
, "r");
1430 PERROR("kernel tracepoint list fdopen");
1435 * Init memory size counter
1436 * See kernel-ctl.h for explanation of this value
1438 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1439 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1440 if (elist
== NULL
) {
1441 PERROR("alloc list events");
1446 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1447 if (count
>= nbmem
) {
1448 struct lttng_event
*new_elist
;
1451 new_nbmem
= nbmem
<< 1;
1452 DBG("Reallocating event list from %zu to %zu bytes",
1454 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1455 if (new_elist
== NULL
) {
1456 PERROR("realloc list events");
1462 /* Zero the new memory */
1463 memset(new_elist
+ nbmem
, 0,
1464 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1468 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1469 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1470 elist
[count
].enabled
= -1;
1476 DBG("Kernel list events done (%zu events)", count
);
1478 ret
= fclose(fp
); /* closes both fp and fd */
1494 * Get kernel version and validate it.
1496 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1497 struct lttng_kernel_tracer_abi_version
*abi_version
)
1501 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1503 ERR("Failed to retrieve the lttng-modules version");
1507 /* Validate version */
1508 if (version
->major
!= VERSION_MAJOR
) {
1509 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1510 version
->major
, VERSION_MAJOR
);
1513 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1515 ERR("Failed to retrieve lttng-modules ABI version");
1518 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1519 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1520 abi_version
->major
, abi_version
->minor
,
1521 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1524 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1525 version
->major
, version
->minor
,
1526 abi_version
->major
, abi_version
->minor
);
1533 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1538 * Kernel work-arounds called at the start of sessiond main().
1540 int init_kernel_workarounds(void)
1546 * boot_id needs to be read once before being used concurrently
1547 * to deal with a Linux kernel race. A fix is proposed for
1548 * upstream, but the work-around is needed for older kernels.
1550 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1557 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1559 /* Ignore error, we don't really care */
1571 * Teardown of a kernel session, keeping data required by destroy notifiers.
1573 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1575 struct lttng_trace_chunk
*trace_chunk
;
1577 if (ksess
== NULL
) {
1578 DBG3("No kernel session when tearing down session");
1582 DBG("Tearing down kernel session");
1583 trace_chunk
= ksess
->current_trace_chunk
;
1586 * Destroy channels on the consumer if at least one FD has been sent and we
1587 * are in no output mode because the streams are in *no* monitor mode so we
1588 * have to send a command to clean them up or else they leaked.
1590 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1592 struct consumer_socket
*socket
;
1593 struct lttng_ht_iter iter
;
1595 /* For each consumer socket. */
1597 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1598 socket
, node
.node
) {
1599 struct ltt_kernel_channel
*chan
;
1601 /* For each channel, ask the consumer to destroy it. */
1602 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1603 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1605 /* Consumer is probably dead. Use next socket. */
1613 /* Close any relayd session */
1614 consumer_output_send_destroy_relayd(ksess
->consumer
);
1616 trace_kernel_destroy_session(ksess
);
1617 lttng_trace_chunk_put(trace_chunk
);
1620 /* Teardown of data required by destroy notifiers. */
1621 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1623 if (ksess
== NULL
) {
1626 trace_kernel_free_session(ksess
);
1630 * Destroy a kernel channel object. It does not do anything on the tracer side.
1632 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1634 struct ltt_kernel_session
*ksess
= NULL
;
1637 assert(kchan
->channel
);
1639 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1641 /* Update channel count of associated session. */
1642 if (kchan
->session
) {
1643 /* Keep pointer reference so we can update it after the destroy. */
1644 ksess
= kchan
->session
;
1647 trace_kernel_destroy_channel(kchan
);
1650 * At this point the kernel channel is not visible anymore. This is safe
1651 * since in order to work on a visible kernel session, the tracing session
1652 * lock (ltt_session.lock) MUST be acquired.
1655 ksess
->channel_count
--;
1660 * Take a snapshot for a given kernel session.
1662 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1664 enum lttng_error_code
kernel_snapshot_record(
1665 struct ltt_kernel_session
*ksess
,
1666 const struct consumer_output
*output
, int wait
,
1667 uint64_t nb_packets_per_stream
)
1669 int err
, ret
, saved_metadata_fd
;
1670 enum lttng_error_code status
= LTTNG_OK
;
1671 struct consumer_socket
*socket
;
1672 struct lttng_ht_iter iter
;
1673 struct ltt_kernel_metadata
*saved_metadata
;
1674 char *trace_path
= NULL
;
1675 size_t consumer_path_offset
= 0;
1678 assert(ksess
->consumer
);
1681 DBG("Kernel snapshot record started");
1683 /* Save current metadata since the following calls will change it. */
1684 saved_metadata
= ksess
->metadata
;
1685 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1689 ret
= kernel_open_metadata(ksess
);
1691 status
= LTTNG_ERR_KERN_META_FAIL
;
1695 ret
= kernel_open_metadata_stream(ksess
);
1697 status
= LTTNG_ERR_KERN_META_FAIL
;
1698 goto error_open_stream
;
1701 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1702 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1704 status
= LTTNG_ERR_INVALID
;
1707 /* Send metadata to consumer and snapshot everything. */
1708 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1709 socket
, node
.node
) {
1710 struct ltt_kernel_channel
*chan
;
1712 pthread_mutex_lock(socket
->lock
);
1713 /* This stream must not be monitored by the consumer. */
1714 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1715 pthread_mutex_unlock(socket
->lock
);
1717 status
= LTTNG_ERR_KERN_META_FAIL
;
1718 goto error_consumer
;
1721 /* For each channel, ask the consumer to snapshot it. */
1722 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1723 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1724 ksess
->uid
, ksess
->gid
,
1725 &trace_path
[consumer_path_offset
], wait
,
1726 nb_packets_per_stream
);
1727 if (status
!= LTTNG_OK
) {
1728 (void) kernel_consumer_destroy_metadata(socket
,
1730 goto error_consumer
;
1734 /* Snapshot metadata, */
1735 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1736 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1738 if (status
!= LTTNG_OK
) {
1739 goto error_consumer
;
1743 * The metadata snapshot is done, ask the consumer to destroy it since
1744 * it's not monitored on the consumer side.
1746 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1750 /* Close newly opened metadata stream. It's now on the consumer side. */
1751 err
= close(ksess
->metadata_stream_fd
);
1753 PERROR("close snapshot kernel");
1757 trace_kernel_destroy_metadata(ksess
->metadata
);
1759 /* Restore metadata state.*/
1760 ksess
->metadata
= saved_metadata
;
1761 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1768 * Get the syscall mask array from the kernel tracer.
1770 * Return 0 on success else a negative value. In both case, syscall_mask should
1773 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1775 assert(syscall_mask
);
1778 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1782 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1785 struct lttng_kernel_tracer_abi_version abi
;
1787 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1789 ERR("Failed to retrieve lttng-modules ABI version");
1793 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1799 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1802 * Return 1 on success, 0 when feature is not supported, negative value in case
1805 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1808 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1810 return kernel_tracer_abi_greater_or_equal(2, 3);
1814 * Check for the support of the packet sequence number via abi version number.
1816 * Return 1 on success, 0 when feature is not supported, negative value in case
1819 int kernel_supports_ring_buffer_packet_sequence_number(void)
1822 * Packet sequence number was introduced in LTTng 2.8,
1823 * lttng-modules ABI 2.1.
1825 return kernel_tracer_abi_greater_or_equal(2, 1);
1829 * Check for the support of event notifiers via abi version number.
1831 * Return 1 on success, 0 when feature is not supported, negative value in case
1834 int kernel_supports_event_notifiers(void)
1837 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1839 return kernel_tracer_abi_greater_or_equal(2, 6);
1843 * Rotate a kernel session.
1845 * Return LTTNG_OK on success or else an LTTng error code.
1847 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1850 enum lttng_error_code status
= LTTNG_OK
;
1851 struct consumer_socket
*socket
;
1852 struct lttng_ht_iter iter
;
1853 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1856 assert(ksess
->consumer
);
1858 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1859 session
->name
, session
->id
);
1864 * Note that this loop will end after one iteration given that there is
1865 * only one kernel consumer.
1867 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1868 socket
, node
.node
) {
1869 struct ltt_kernel_channel
*chan
;
1871 /* For each channel, ask the consumer to rotate it. */
1872 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1873 DBG("Rotate kernel channel %" PRIu64
", session %s",
1874 chan
->key
, session
->name
);
1875 ret
= consumer_rotate_channel(socket
, chan
->key
,
1876 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1877 /* is_metadata_channel */ false);
1879 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1885 * Rotate the metadata channel.
1887 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1888 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1889 /* is_metadata_channel */ true);
1891 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1901 enum lttng_error_code
kernel_create_channel_subdirectories(
1902 const struct ltt_kernel_session
*ksess
)
1904 enum lttng_error_code ret
= LTTNG_OK
;
1905 enum lttng_trace_chunk_status chunk_status
;
1908 assert(ksess
->current_trace_chunk
);
1911 * Create the index subdirectory which will take care
1912 * of implicitly creating the channel's path.
1914 chunk_status
= lttng_trace_chunk_create_subdirectory(
1915 ksess
->current_trace_chunk
,
1916 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1917 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1918 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1927 * Setup necessary data for kernel tracer action.
1930 int init_kernel_tracer(void)
1933 bool is_root
= !getuid();
1935 /* Modprobe lttng kernel modules */
1936 ret
= modprobe_lttng_control();
1941 /* Open debugfs lttng */
1942 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1943 if (kernel_tracer_fd
< 0) {
1944 DBG("Failed to open %s", module_proc_lttng
);
1948 /* Validate kernel version */
1949 ret
= kernel_validate_version(&the_kernel_tracer_version
,
1950 &the_kernel_tracer_abi_version
);
1955 ret
= modprobe_lttng_data();
1960 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1965 WARN("Kernel tracer does not support buffer monitoring. "
1966 "The monitoring timer of channels in the kernel domain "
1967 "will be set to 0 (disabled).");
1970 ret
= kernel_supports_event_notifiers();
1972 ERR("Failed to check for kernel tracer event notifier support");
1975 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1977 /* This is not fatal. */
1978 WARN("Failed to create kernel event notifier group");
1979 kernel_tracer_event_notifier_group_fd
= -1;
1981 enum event_notifier_error_accounting_status error_accounting_status
;
1982 enum lttng_error_code error_code_ret
=
1983 kernel_create_event_notifier_group_notification_fd(
1984 &kernel_tracer_event_notifier_group_notification_fd
);
1986 if (error_code_ret
!= LTTNG_OK
) {
1990 error_accounting_status
= event_notifier_error_accounting_register_kernel(
1991 kernel_tracer_event_notifier_group_fd
);
1992 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1993 ERR("Failed to initialize event notifier error accounting for kernel tracer");
1994 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
1998 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
1999 DEFAULT_HT_SIZE
, 1, 0,
2000 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
2002 if (!kernel_token_to_event_notifier_rule_ht
) {
2003 goto error_token_ht
;
2007 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2008 kernel_tracer_fd
, kernel_tracer_event_notifier_group_fd
,
2009 kernel_tracer_event_notifier_group_notification_fd
);
2011 ret
= syscall_init_table(kernel_tracer_fd
);
2013 ERR("Unable to populate syscall table. Syscall tracing won't "
2014 "work for this session daemon.");
2020 modprobe_remove_lttng_control();
2021 ret
= close(kernel_tracer_fd
);
2023 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2027 kernel_tracer_fd
= -1;
2028 return LTTNG_ERR_KERN_VERSION
;
2032 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2034 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2035 kernel_tracer_event_notifier_group_notification_fd
);
2038 kernel_tracer_event_notifier_group_notification_fd
= -1;
2041 ret
= close(kernel_tracer_event_notifier_group_fd
);
2043 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2044 kernel_tracer_event_notifier_group_fd
);
2047 kernel_tracer_event_notifier_group_fd
= -1;
2049 ret
= close(kernel_tracer_fd
);
2051 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2055 kernel_tracer_fd
= -1;
2058 modprobe_remove_lttng_control();
2061 WARN("No kernel tracer available");
2062 kernel_tracer_fd
= -1;
2064 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2066 return LTTNG_ERR_KERN_NA
;
2071 void cleanup_kernel_tracer(void)
2073 DBG2("Closing kernel event notifier group notification file descriptor");
2074 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2075 int ret
= notification_thread_command_remove_tracer_event_source(
2076 the_notification_thread_handle
,
2077 kernel_tracer_event_notifier_group_notification_fd
);
2078 if (ret
!= LTTNG_OK
) {
2079 ERR("Failed to remove kernel event notifier notification from notification thread");
2082 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2084 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2085 kernel_tracer_event_notifier_group_notification_fd
);
2088 kernel_tracer_event_notifier_group_notification_fd
= -1;
2091 if (kernel_token_to_event_notifier_rule_ht
) {
2092 const int ret
= cds_lfht_destroy(
2093 kernel_token_to_event_notifier_rule_ht
, NULL
);
2097 DBG2("Closing kernel event notifier group file descriptor");
2098 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2099 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2102 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2103 kernel_tracer_event_notifier_group_fd
);
2106 kernel_tracer_event_notifier_group_fd
= -1;
2109 DBG2("Closing kernel fd");
2110 if (kernel_tracer_fd
>= 0) {
2111 const int ret
= close(kernel_tracer_fd
);
2114 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2118 kernel_tracer_fd
= -1;
2121 free(syscall_table
);
2125 bool kernel_tracer_is_initialized(void)
2127 return kernel_tracer_fd
>= 0;
2131 * Clear a kernel session.
2133 * Return LTTNG_OK on success or else an LTTng error code.
2135 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2138 enum lttng_error_code status
= LTTNG_OK
;
2139 struct consumer_socket
*socket
;
2140 struct lttng_ht_iter iter
;
2141 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2144 assert(ksess
->consumer
);
2146 DBG("Clear kernel session %s (session %" PRIu64
")",
2147 session
->name
, session
->id
);
2151 if (ksess
->active
) {
2152 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2153 status
= LTTNG_ERR_FATAL
;
2158 * Note that this loop will end after one iteration given that there is
2159 * only one kernel consumer.
2161 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2162 socket
, node
.node
) {
2163 struct ltt_kernel_channel
*chan
;
2165 /* For each channel, ask the consumer to clear it. */
2166 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2167 DBG("Clear kernel channel %" PRIu64
", session %s",
2168 chan
->key
, session
->name
);
2169 ret
= consumer_clear_channel(socket
, chan
->key
);
2175 if (!ksess
->metadata
) {
2177 * Nothing to do for the metadata.
2178 * This is a snapshot session.
2179 * The metadata is genererated on the fly.
2185 * Clear the metadata channel.
2186 * Metadata channel is not cleared per se but we still need to
2187 * perform a rotation operation on it behind the scene.
2189 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2198 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2199 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2202 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2210 enum lttng_error_code
kernel_create_event_notifier_group_notification_fd(
2211 int *event_notifier_group_notification_fd
)
2213 int local_fd
= -1, ret
;
2214 enum lttng_error_code error_code_ret
;
2216 assert(event_notifier_group_notification_fd
);
2218 ret
= kernctl_create_event_notifier_group_notification_fd(
2219 kernel_tracer_event_notifier_group_fd
);
2221 PERROR("Failed to create kernel event notifier group notification file descriptor");
2222 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2228 /* Prevent fd duplication after execlp(). */
2229 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2231 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2233 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2237 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2239 error_code_ret
= LTTNG_OK
;
2240 *event_notifier_group_notification_fd
= local_fd
;
2244 if (local_fd
>= 0) {
2245 ret
= close(local_fd
);
2247 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2252 return error_code_ret
;
2255 enum lttng_error_code
kernel_destroy_event_notifier_group_notification_fd(
2256 int event_notifier_group_notification_fd
)
2258 enum lttng_error_code ret_code
= LTTNG_OK
;
2260 DBG("Closing event notifier group notification file descriptor: fd = %d",
2261 event_notifier_group_notification_fd
);
2262 if (event_notifier_group_notification_fd
>= 0) {
2263 const int ret
= close(event_notifier_group_notification_fd
);
2265 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2266 event_notifier_group_notification_fd
);
2274 unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2276 const struct lttng_condition
*condition
=
2277 lttng_trigger_get_const_condition(trigger
);
2279 return lttng_condition_hash(condition
);
2283 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2285 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2286 const struct lttng_trigger
*trigger
= key
;
2288 event_notifier_rule
= caa_container_of(node
,
2289 const struct ltt_kernel_event_notifier_rule
, ht_node
);
2291 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2294 static enum lttng_error_code
kernel_create_event_notifier_rule(
2295 struct lttng_trigger
*trigger
,
2296 const struct lttng_credentials
*creds
, uint64_t token
)
2298 int err
, fd
, ret
= 0;
2299 enum lttng_error_code error_code_ret
;
2300 enum lttng_condition_status condition_status
;
2301 enum lttng_condition_type condition_type
;
2302 enum lttng_event_rule_type event_rule_type
;
2303 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2304 struct lttng_kernel_event_notifier kernel_event_notifier
= {};
2305 unsigned int capture_bytecode_count
= 0, i
;
2306 const struct lttng_condition
*condition
= NULL
;
2307 const struct lttng_event_rule
*event_rule
= NULL
;
2308 enum lttng_condition_status cond_status
;
2312 condition
= lttng_trigger_get_const_condition(trigger
);
2315 condition_type
= lttng_condition_get_type(condition
);
2316 assert(condition_type
== LTTNG_CONDITION_TYPE_ON_EVENT
);
2318 /* Does not acquire a reference. */
2319 condition_status
= lttng_condition_on_event_get_rule(
2320 condition
, &event_rule
);
2321 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2324 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2325 assert(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2327 error_code_ret
= trace_kernel_create_event_notifier_rule(trigger
, token
,
2328 lttng_condition_on_event_get_error_counter_index(condition
),
2329 &event_notifier_rule
);
2330 if (error_code_ret
!= LTTNG_OK
) {
2334 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(
2335 event_rule
, &kernel_event_notifier
);
2336 if (error_code_ret
!= LTTNG_OK
) {
2340 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2341 kernel_event_notifier
.error_counter_idx
=
2342 lttng_condition_on_event_get_error_counter_index(condition
);
2344 fd
= kernctl_create_event_notifier(
2345 kernel_tracer_event_notifier_group_fd
,
2346 &kernel_event_notifier
);
2350 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2353 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2354 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2357 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2358 kernel_event_notifier
.event
.name
);
2359 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2362 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2363 fd
, kernel_event_notifier
.event
.name
);
2364 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2369 event_notifier_rule
->fd
= fd
;
2370 /* Prevent fd duplication after execlp(). */
2371 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2373 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2375 error_code_ret
= LTTNG_ERR_FATAL
;
2376 goto set_cloexec_error
;
2379 if (event_notifier_rule
->filter
) {
2380 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2384 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2387 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2394 if (lttng_event_rule_get_type(event_rule
) ==
2395 LTTNG_EVENT_RULE_TYPE_USERSPACE_PROBE
) {
2396 ret
= userspace_probe_event_rule_add_callsites(
2397 event_rule
, creds
, event_notifier_rule
->fd
);
2399 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2400 goto add_callsite_error
;
2404 /* Set the capture bytecode if any. */
2405 cond_status
= lttng_condition_on_event_get_capture_descriptor_count(
2406 condition
, &capture_bytecode_count
);
2407 assert(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2409 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2410 const struct lttng_bytecode
*capture_bytecode
=
2411 lttng_condition_on_event_get_capture_bytecode_at_index(
2414 if (capture_bytecode
== NULL
) {
2415 ERR("Unexpected NULL capture bytecode on condition");
2416 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2420 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2422 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2423 event_notifier_rule
->fd
);
2424 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2429 err
= kernctl_enable(event_notifier_rule
->fd
);
2433 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2436 PERROR("enable kernel event notifier");
2437 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2443 /* Add trigger to kernel token mapping in the hash table. */
2445 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
, hash_trigger(trigger
),
2446 &event_notifier_rule
->ht_node
);
2449 DBG("Created kernel event notifier: name = '%s', fd = %d",
2450 kernel_event_notifier
.event
.name
,
2451 event_notifier_rule
->fd
);
2461 const int close_ret
= close(event_notifier_rule
->fd
);
2464 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2465 event_notifier_rule
->fd
);
2469 free(event_notifier_rule
);
2471 return error_code_ret
;
2474 enum lttng_error_code
kernel_register_event_notifier(
2475 struct lttng_trigger
*trigger
,
2476 const struct lttng_credentials
*cmd_creds
)
2478 enum lttng_error_code ret
;
2479 enum lttng_condition_status status
;
2480 enum lttng_domain_type domain_type
;
2481 const struct lttng_event_rule
*event_rule
;
2482 const struct lttng_condition
*const condition
=
2483 lttng_trigger_get_const_condition(trigger
);
2484 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2488 /* Does not acquire a reference to the event rule. */
2489 status
= lttng_condition_on_event_get_rule(
2490 condition
, &event_rule
);
2491 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2493 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2494 assert(domain_type
== LTTNG_DOMAIN_KERNEL
);
2496 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2497 if (ret
!= LTTNG_OK
) {
2498 ERR("Failed to create kernel event notifier rule");
2504 enum lttng_error_code
kernel_unregister_event_notifier(
2505 const struct lttng_trigger
*trigger
)
2507 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2508 struct cds_lfht_node
*node
;
2509 struct cds_lfht_iter iter
;
2510 enum lttng_error_code error_code_ret
;
2515 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2516 hash_trigger(trigger
), match_trigger
, trigger
, &iter
);
2518 node
= cds_lfht_iter_get_node(&iter
);
2520 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2524 token_event_rule_element
= caa_container_of(node
,
2525 struct ltt_kernel_event_notifier_rule
, ht_node
);
2527 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2529 error_code_ret
= LTTNG_ERR_FATAL
;
2533 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2534 error_code_ret
= LTTNG_OK
;
2539 return error_code_ret
;
2542 int kernel_get_notification_fd(void)
2544 return kernel_tracer_event_notifier_group_notification_fd
;