2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
9 #include "condition-internal.hpp"
10 #include "consumer.hpp"
11 #include "event-notifier-error-accounting.hpp"
12 #include "kern-modules.hpp"
13 #include "kernel-consumer.hpp"
15 #include "lttng-sessiond.hpp"
16 #include "lttng-syscall.hpp"
17 #include "modprobe.hpp"
18 #include "notification-thread-commands.hpp"
20 #include "sessiond-config.hpp"
21 #include "tracker.hpp"
24 #include <common/common.hpp>
25 #include <common/hashtable/utils.hpp>
26 #include <common/kernel-ctl/kernel-ctl.hpp>
27 #include <common/kernel-ctl/kernel-ioctl.hpp>
28 #include <common/sessiond-comm/sessiond-comm.hpp>
29 #include <common/trace-chunk.hpp>
30 #include <common/tracker.hpp>
31 #include <common/urcu.hpp>
32 #include <common/utils.hpp>
34 #include <lttng/condition/event-rule-matches-internal.hpp>
35 #include <lttng/condition/event-rule-matches.h>
36 #include <lttng/event-rule/event-rule-internal.hpp>
37 #include <lttng/event-rule/event-rule.h>
38 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
39 #include <lttng/event.h>
40 #include <lttng/lttng-error.h>
41 #include <lttng/tracker.h>
42 #include <lttng/userspace-probe-internal.hpp>
43 #include <lttng/userspace-probe.h>
50 #include <sys/types.h>
54 * Key used to reference a channel between the sessiond and the consumer. This
55 * is only read and updated with the session_list lock held.
57 static uint64_t next_kernel_channel_key
;
59 static const char *module_proc_lttng
= "/proc/lttng";
61 static int kernel_tracer_fd
= -1;
62 static int kernel_tracer_event_notifier_group_fd
= -1;
63 static int kernel_tracer_event_notifier_group_notification_fd
= -1;
64 static struct cds_lfht
*kernel_token_to_event_notifier_rule_ht
;
67 * Add context on a kernel channel.
69 * Assumes the ownership of ctx.
71 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
, 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
;
118 LTTNG_ASSERT(session
);
120 /* Allocate data structure */
121 lks
= trace_kernel_create_session();
122 if (lks
== nullptr) {
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",
162 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
164 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
173 trace_kernel_destroy_session(lks
);
174 trace_kernel_free_session(lks
);
180 * Create a kernel channel, register it to the kernel tracer and add it to the
183 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
)
186 struct ltt_kernel_channel
*lkc
;
188 LTTNG_ASSERT(session
);
191 /* Allocate kernel channel */
192 lkc
= trace_kernel_create_channel(chan
);
193 if (lkc
== nullptr) {
197 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
199 lkc
->channel
->attr
.overwrite
,
200 lkc
->channel
->attr
.subbuf_size
,
201 lkc
->channel
->attr
.num_subbuf
,
202 lkc
->channel
->attr
.switch_timer_interval
,
203 lkc
->channel
->attr
.read_timer_interval
,
204 lkc
->channel
->attr
.live_timer_interval
,
205 lkc
->channel
->attr
.output
);
207 /* Kernel tracer channel creation */
208 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
210 PERROR("ioctl kernel create channel");
214 /* Setup the channel fd */
216 /* Prevent fd duplication after execlp() */
217 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
219 PERROR("fcntl session fd");
222 /* Add channel to session */
223 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
224 session
->channel_count
++;
225 lkc
->session
= session
;
226 lkc
->key
= ++next_kernel_channel_key
;
228 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
244 * Create a kernel event notifier group, register it to the kernel tracer and
245 * add it to the kernel session.
247 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
252 LTTNG_ASSERT(event_notifier_group_fd
);
254 /* Kernel event notifier group creation. */
255 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
257 PERROR("Failed to create kernel event notifier group");
264 /* Prevent fd duplication after execlp(). */
265 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
267 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
272 DBG("Created kernel event notifier group: fd = %d", local_fd
);
273 *event_notifier_group_fd
= local_fd
;
278 ret
= close(local_fd
);
280 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
289 * Compute the offset of the instrumentation byte in the binary based on the
290 * function probe location using the ELF lookup method.
292 * Returns 0 on success and set the offset out parameter to the offset of the
294 * Returns -1 on error
296 static int extract_userspace_probe_offset_function_elf(
297 const struct lttng_userspace_probe_location
*probe_location
,
304 const char *symbol
= nullptr;
305 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
306 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
308 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
309 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
311 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
317 lookup_method_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup
);
319 LTTNG_ASSERT(lookup_method_type
==
320 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
322 symbol
= lttng_userspace_probe_location_function_get_function_name(probe_location
);
328 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
334 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
336 DBG("userspace probe offset calculation failed for "
342 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t) (*offset
));
348 * Compute the offsets of the instrumentation bytes in the binary based on the
349 * tracepoint probe location using the SDT lookup method. This function
350 * allocates the offsets buffer, the caller must free it.
352 * Returns 0 on success and set the offset out parameter to the offsets of the
354 * Returns -1 on error.
356 static int extract_userspace_probe_offset_tracepoint_sdt(
357 const struct lttng_userspace_probe_location
*probe_location
,
361 uint32_t *offsets_count
)
363 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
364 const struct lttng_userspace_probe_location_lookup_method
*lookup
= nullptr;
365 const char *probe_name
= nullptr, *provider_name
= nullptr;
369 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
370 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
372 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
378 lookup_method_type
= lttng_userspace_probe_location_lookup_method_get_type(lookup
);
380 LTTNG_ASSERT(lookup_method_type
==
381 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
383 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(probe_location
);
389 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(probe_location
);
390 if (!provider_name
) {
395 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
401 ret
= run_as_extract_sdt_probe_offsets(
402 fd
, provider_name
, probe_name
, uid
, gid
, offsets
, offsets_count
);
404 DBG("userspace probe offset calculation failed for sdt "
411 if (*offsets_count
== 0) {
412 DBG("no userspace probe offset found");
416 DBG("%u userspace probe SDT offsets found for %s:%s at:",
420 for (i
= 0; i
< *offsets_count
; i
++) {
421 DBG("\t0x%jd", (intmax_t) ((*offsets
)[i
]));
427 static int userspace_probe_add_callsite(const struct lttng_userspace_probe_location
*location
,
432 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= nullptr;
433 enum lttng_userspace_probe_location_lookup_method_type type
;
436 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
437 if (!lookup_method
) {
442 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
444 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
446 struct lttng_kernel_abi_event_callsite callsite
;
449 ret
= extract_userspace_probe_offset_function_elf(location
, uid
, gid
, &offset
);
451 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
455 callsite
.u
.uprobe
.offset
= offset
;
456 ret
= kernctl_add_callsite(fd
, &callsite
);
458 WARN("Failed to add callsite to ELF userspace probe.");
459 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
464 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
467 uint64_t *offsets
= nullptr;
468 uint32_t offsets_count
;
469 struct lttng_kernel_abi_event_callsite callsite
;
472 * This call allocates the offsets buffer. This buffer must be freed
475 ret
= extract_userspace_probe_offset_tracepoint_sdt(
476 location
, uid
, gid
, &offsets
, &offsets_count
);
478 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
481 for (i
= 0; i
< offsets_count
; i
++) {
482 callsite
.u
.uprobe
.offset
= offsets
[i
];
483 ret
= kernctl_add_callsite(fd
, &callsite
);
485 WARN("Failed to add callsite to SDT userspace probe");
486 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
495 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
503 * Extract the offsets of the instrumentation point for the different lookup
506 static int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
507 struct ltt_kernel_session
*session
,
511 const struct lttng_userspace_probe_location
*location
= nullptr;
514 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
516 location
= lttng_event_get_userspace_probe_location(ev
);
522 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
, fd
);
524 WARN("Failed to add callsite to userspace probe event '%s'", ev
->name
);
532 * Extract the offsets of the instrumentation point for the different look-up
535 static int userspace_probe_event_rule_add_callsites(const struct lttng_event_rule
*rule
,
536 const struct lttng_credentials
*creds
,
540 enum lttng_event_rule_status status
;
541 enum lttng_event_rule_type event_rule_type
;
542 const struct lttng_userspace_probe_location
*location
= nullptr;
547 event_rule_type
= lttng_event_rule_get_type(rule
);
548 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
550 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
551 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
556 ret
= userspace_probe_add_callsite(
557 location
, lttng_credentials_get_uid(creds
), lttng_credentials_get_gid(creds
), fd
);
559 WARN("Failed to add callsite to user space probe object: fd = %d", fd
);
567 * Create a kernel event, enable it to the kernel tracer and add it to the
568 * channel event list of the kernel session.
569 * We own filter_expression and filter.
571 int kernel_create_event(struct lttng_event
*ev
,
572 struct ltt_kernel_channel
*channel
,
573 char *filter_expression
,
574 struct lttng_bytecode
*filter
)
577 enum lttng_error_code ret
;
578 struct ltt_kernel_event
*event
;
581 LTTNG_ASSERT(channel
);
583 /* We pass ownership of filter_expression and filter */
584 ret
= trace_kernel_create_event(ev
, filter_expression
, filter
, &event
);
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
= (lttng_error_code
) userspace_probe_event_add_callsites(
635 ev
, channel
->session
, event
->fd
);
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");
695 chan
->enabled
= false;
696 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
708 * Enable a kernel channel.
710 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
716 ret
= kernctl_enable(chan
->fd
);
717 if (ret
< 0 && ret
!= -EEXIST
) {
718 PERROR("Enable kernel chan");
722 chan
->enabled
= true;
723 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
735 * Enable a kernel event.
737 int kernel_enable_event(struct ltt_kernel_event
*event
)
743 ret
= kernctl_enable(event
->fd
);
747 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
750 PERROR("enable kernel event");
756 event
->enabled
= true;
757 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
766 * Disable a kernel event.
768 int kernel_disable_event(struct ltt_kernel_event
*event
)
774 ret
= kernctl_disable(event
->fd
);
776 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
782 event
->enabled
= false;
783 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
792 * Disable a kernel event notifier.
794 static int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
800 lttng::urcu::read_lock_guard read_lock
;
801 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
803 ret
= kernctl_disable(event
->fd
);
805 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
811 event
->enabled
= false;
812 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
, event
->fd
, event
->token
);
818 static struct process_attr_tracker
*
819 _kernel_get_process_attr_tracker(struct ltt_kernel_session
*session
,
820 enum lttng_process_attr process_attr
)
822 switch (process_attr
) {
823 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
824 return session
->tracker_pid
;
825 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
826 return session
->tracker_vpid
;
827 case LTTNG_PROCESS_ATTR_USER_ID
:
828 return session
->tracker_uid
;
829 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
830 return session
->tracker_vuid
;
831 case LTTNG_PROCESS_ATTR_GROUP_ID
:
832 return session
->tracker_gid
;
833 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
834 return session
->tracker_vgid
;
840 const struct process_attr_tracker
*
841 kernel_get_process_attr_tracker(struct ltt_kernel_session
*session
,
842 enum lttng_process_attr process_attr
)
844 return (const struct process_attr_tracker
*) _kernel_get_process_attr_tracker(session
,
848 enum lttng_error_code
849 kernel_process_attr_tracker_set_tracking_policy(struct ltt_kernel_session
*session
,
850 enum lttng_process_attr process_attr
,
851 enum lttng_tracking_policy policy
)
854 enum lttng_error_code ret_code
= LTTNG_OK
;
855 struct process_attr_tracker
*tracker
=
856 _kernel_get_process_attr_tracker(session
, process_attr
);
857 enum lttng_tracking_policy previous_policy
;
860 ret_code
= LTTNG_ERR_INVALID
;
864 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
865 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
867 ret_code
= LTTNG_ERR_UNK
;
871 if (previous_policy
== policy
) {
876 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
877 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
879 * Maintain a special case for the process ID process
880 * attribute tracker as it was the only supported
881 * attribute prior to 2.12.
883 ret
= kernctl_track_pid(session
->fd
, -1);
885 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
888 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
889 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
891 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
893 * Maintain a special case for the process ID process
894 * attribute tracker as it was the only supported
895 * attribute prior to 2.12.
897 ret
= kernctl_untrack_pid(session
->fd
, -1);
899 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
905 /* kern-ctl error handling */
911 ret_code
= LTTNG_ERR_INVALID
;
914 ret_code
= LTTNG_ERR_NOMEM
;
917 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
920 ret_code
= LTTNG_ERR_UNK
;
927 enum lttng_error_code
928 kernel_process_attr_tracker_inclusion_set_add_value(struct ltt_kernel_session
*session
,
929 enum lttng_process_attr process_attr
,
930 const struct process_attr_value
*value
)
932 int ret
, integral_value
;
933 enum lttng_error_code ret_code
;
934 struct process_attr_tracker
*tracker
;
935 enum process_attr_tracker_status status
;
938 * Convert process attribute tracker value to the integral
939 * representation required by the kern-ctl API.
941 switch (process_attr
) {
942 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
943 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
944 integral_value
= (int) value
->value
.pid
;
946 case LTTNG_PROCESS_ATTR_USER_ID
:
947 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
948 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
951 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
952 if (ret_code
!= LTTNG_OK
) {
955 integral_value
= (int) uid
;
957 integral_value
= (int) value
->value
.uid
;
960 case LTTNG_PROCESS_ATTR_GROUP_ID
:
961 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
962 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
965 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
966 if (ret_code
!= LTTNG_OK
) {
969 integral_value
= (int) gid
;
971 integral_value
= (int) value
->value
.gid
;
975 ret_code
= LTTNG_ERR_INVALID
;
979 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
981 ret_code
= LTTNG_ERR_INVALID
;
985 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
986 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
988 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
989 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
991 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
992 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
994 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
996 ret_code
= LTTNG_ERR_UNK
;
1002 DBG("Kernel track %s %d for session id %" PRIu64
,
1003 lttng_process_attr_to_string(process_attr
),
1006 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1008 * Maintain a special case for the process ID process attribute
1009 * tracker as it was the only supported attribute prior to 2.12.
1011 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1013 ret
= kernctl_track_id(session
->fd
, process_attr
, integral_value
);
1016 ret_code
= LTTNG_OK
;
1020 kernel_wait_quiescent();
1022 /* kern-ctl error handling */
1025 ret_code
= LTTNG_OK
;
1028 ret_code
= LTTNG_ERR_INVALID
;
1031 ret_code
= LTTNG_ERR_NOMEM
;
1034 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1037 ret_code
= LTTNG_ERR_UNK
;
1041 /* Attempt to remove the value from the tracker. */
1042 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1043 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1044 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1045 lttng_process_attr_to_string(process_attr
),
1052 enum lttng_error_code
1053 kernel_process_attr_tracker_inclusion_set_remove_value(struct ltt_kernel_session
*session
,
1054 enum lttng_process_attr process_attr
,
1055 const struct process_attr_value
*value
)
1057 int ret
, integral_value
;
1058 enum lttng_error_code ret_code
;
1059 struct process_attr_tracker
*tracker
;
1060 enum process_attr_tracker_status status
;
1063 * Convert process attribute tracker value to the integral
1064 * representation required by the kern-ctl API.
1066 switch (process_attr
) {
1067 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1068 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1069 integral_value
= (int) value
->value
.pid
;
1071 case LTTNG_PROCESS_ATTR_USER_ID
:
1072 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1073 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1076 ret_code
= utils_user_id_from_name(value
->value
.user_name
, &uid
);
1077 if (ret_code
!= LTTNG_OK
) {
1080 integral_value
= (int) uid
;
1082 integral_value
= (int) value
->value
.uid
;
1085 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1086 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1087 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1090 ret_code
= utils_group_id_from_name(value
->value
.group_name
, &gid
);
1091 if (ret_code
!= LTTNG_OK
) {
1094 integral_value
= (int) gid
;
1096 integral_value
= (int) value
->value
.gid
;
1100 ret_code
= LTTNG_ERR_INVALID
;
1104 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1106 ret_code
= LTTNG_ERR_INVALID
;
1110 status
= process_attr_tracker_inclusion_set_remove_value(tracker
, value
);
1111 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1113 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1114 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1116 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1117 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1119 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1121 ret_code
= LTTNG_ERR_UNK
;
1127 DBG("Kernel track %s %d for session id %" PRIu64
,
1128 lttng_process_attr_to_string(process_attr
),
1131 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1133 * Maintain a special case for the process ID process attribute
1134 * tracker as it was the only supported attribute prior to 2.12.
1136 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1138 ret
= kernctl_untrack_id(session
->fd
, process_attr
, integral_value
);
1141 ret_code
= LTTNG_OK
;
1144 kernel_wait_quiescent();
1146 /* kern-ctl error handling */
1149 ret_code
= LTTNG_OK
;
1152 ret_code
= LTTNG_ERR_INVALID
;
1155 ret_code
= LTTNG_ERR_NOMEM
;
1158 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1161 ret_code
= LTTNG_ERR_UNK
;
1165 /* Attempt to add the value to the tracker. */
1166 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
1167 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1168 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1169 lttng_process_attr_to_string(process_attr
),
1177 * Create kernel metadata, open from the kernel tracer and add it to the
1180 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1183 struct ltt_kernel_metadata
*lkm
= nullptr;
1185 LTTNG_ASSERT(session
);
1187 /* Allocate kernel metadata */
1188 lkm
= trace_kernel_create_metadata();
1189 if (lkm
== nullptr) {
1193 /* Kernel tracer metadata creation */
1194 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1200 lkm
->key
= ++next_kernel_channel_key
;
1201 /* Prevent fd duplication after execlp() */
1202 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1204 PERROR("fcntl session fd");
1207 session
->metadata
= lkm
;
1209 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1214 trace_kernel_destroy_metadata(lkm
);
1220 * Start tracing session.
1222 int kernel_start_session(struct ltt_kernel_session
*session
)
1226 LTTNG_ASSERT(session
);
1228 ret
= kernctl_start_session(session
->fd
);
1230 PERROR("ioctl start session");
1234 DBG("Kernel session started");
1243 * Make a kernel wait to make sure in-flight probe have completed.
1245 void kernel_wait_quiescent()
1248 int fd
= kernel_tracer_fd
;
1250 DBG("Kernel quiescent wait on %d", fd
);
1252 ret
= kernctl_wait_quiescent(fd
);
1254 PERROR("wait quiescent ioctl");
1255 ERR("Kernel quiescent wait failed");
1260 * Force flush buffer of metadata.
1262 int kernel_metadata_flush_buffer(int fd
)
1266 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1268 ret
= kernctl_buffer_flush(fd
);
1270 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1277 * Force flush buffer for channel.
1279 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1282 struct ltt_kernel_stream
*stream
;
1284 LTTNG_ASSERT(channel
);
1286 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1288 cds_list_for_each_entry (stream
, &channel
->stream_list
.head
, list
) {
1289 DBG("Flushing channel stream %d", stream
->fd
);
1290 ret
= kernctl_buffer_flush(stream
->fd
);
1293 ERR("Fail to flush buffer for stream %d (ret: %d)", stream
->fd
, ret
);
1301 * Stop tracing session.
1303 int kernel_stop_session(struct ltt_kernel_session
*session
)
1307 LTTNG_ASSERT(session
);
1309 ret
= kernctl_stop_session(session
->fd
);
1314 DBG("Kernel session stopped");
1323 * Open stream of channel, register it to the kernel tracer and add it
1324 * to the stream list of the channel.
1326 * Note: given that the streams may appear in random order wrt CPU
1327 * number (e.g. cpu hotplug), the index value of the stream number in
1328 * the stream name is not necessarily linked to the CPU number.
1330 * Return the number of created stream. Else, a negative value.
1332 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1335 struct ltt_kernel_stream
*lks
;
1337 LTTNG_ASSERT(channel
);
1339 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1340 lks
= trace_kernel_create_stream(channel
->channel
->name
, channel
->stream_count
);
1341 if (lks
== nullptr) {
1350 /* Prevent fd duplication after execlp() */
1351 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1353 PERROR("fcntl session fd");
1356 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1357 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1359 /* Add stream to channel stream list */
1360 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1361 channel
->stream_count
++;
1363 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
, lks
->state
);
1366 return channel
->stream_count
;
1373 * Open the metadata stream and set it to the kernel session.
1375 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1379 LTTNG_ASSERT(session
);
1381 ret
= kernctl_create_stream(session
->metadata
->fd
);
1383 PERROR("kernel create metadata stream");
1387 DBG("Kernel metadata stream created (fd: %d)", ret
);
1388 session
->metadata_stream_fd
= ret
;
1389 /* Prevent fd duplication after execlp() */
1390 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1392 PERROR("fcntl session fd");
1402 * Get the event list from the kernel tracer and return the number of elements.
1404 ssize_t
kernel_list_events(struct lttng_event
**events
)
1408 size_t nbmem
, count
= 0;
1410 struct lttng_event
*elist
;
1412 LTTNG_ASSERT(events
);
1414 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1416 PERROR("kernel tracepoint list");
1420 fp
= fdopen(fd
, "r");
1421 if (fp
== nullptr) {
1422 PERROR("kernel tracepoint list fdopen");
1427 * Init memory size counter
1428 * See kernel-ctl.h for explanation of this value
1430 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1431 elist
= calloc
<lttng_event
>(nbmem
);
1432 if (elist
== nullptr) {
1433 PERROR("alloc list events");
1438 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1439 if (count
>= nbmem
) {
1440 struct lttng_event
*new_elist
;
1443 new_nbmem
= nbmem
<< 1;
1444 DBG("Reallocating event list from %zu to %zu bytes", nbmem
, new_nbmem
);
1445 new_elist
= (lttng_event
*) realloc(elist
,
1446 new_nbmem
* sizeof(struct lttng_event
));
1447 if (new_elist
== nullptr) {
1448 PERROR("realloc list events");
1454 /* Zero the new memory */
1455 memset(new_elist
+ nbmem
,
1457 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1461 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1462 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1463 elist
[count
].enabled
= -1;
1469 DBG("Kernel list events done (%zu events)", count
);
1471 ret
= fclose(fp
); /* closes both fp and fd */
1487 * Get kernel version and validate it.
1489 int kernel_validate_version(struct lttng_kernel_abi_tracer_version
*version
,
1490 struct lttng_kernel_abi_tracer_abi_version
*abi_version
)
1494 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1496 ERR("Failed to retrieve the lttng-modules version");
1500 /* Validate version */
1501 if (version
->major
!= VERSION_MAJOR
) {
1502 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1507 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1509 ERR("Failed to retrieve lttng-modules ABI version");
1512 if (abi_version
->major
!= LTTNG_KERNEL_ABI_MAJOR_VERSION
) {
1513 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1516 LTTNG_KERNEL_ABI_MAJOR_VERSION
);
1519 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1523 abi_version
->minor
);
1530 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1535 * Kernel work-arounds called at the start of sessiond main().
1537 int init_kernel_workarounds()
1543 * boot_id needs to be read once before being used concurrently
1544 * to deal with a Linux kernel race. A fix is proposed for
1545 * upstream, but the work-around is needed for older kernels.
1547 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1554 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1556 /* Ignore error, we don't really care */
1568 * Teardown of a kernel session, keeping data required by destroy notifiers.
1570 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1572 struct lttng_trace_chunk
*trace_chunk
;
1574 if (ksess
== nullptr) {
1575 DBG3("No kernel session when tearing down session");
1579 DBG("Tearing down kernel session");
1580 trace_chunk
= ksess
->current_trace_chunk
;
1583 * Destroy channels on the consumer if at least one FD has been sent and we
1584 * are in no output mode because the streams are in *no* monitor mode so we
1585 * have to send a command to clean them up or else they leaked.
1587 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1589 struct consumer_socket
*socket
;
1590 struct lttng_ht_iter iter
;
1592 /* For each consumer socket. */
1593 lttng::urcu::read_lock_guard read_lock
;
1595 cds_lfht_for_each_entry (
1596 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1597 struct ltt_kernel_channel
*chan
;
1599 /* For each channel, ask the consumer to destroy it. */
1600 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1601 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1603 /* Consumer is probably dead. Use next socket. */
1610 /* Close any relayd session */
1611 consumer_output_send_destroy_relayd(ksess
->consumer
);
1613 trace_kernel_destroy_session(ksess
);
1614 lttng_trace_chunk_put(trace_chunk
);
1617 /* Teardown of data required by destroy notifiers. */
1618 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1620 if (ksess
== nullptr) {
1623 trace_kernel_free_session(ksess
);
1627 * Destroy a kernel channel object. It does not do anything on the tracer side.
1629 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1631 struct ltt_kernel_session
*ksess
= nullptr;
1633 LTTNG_ASSERT(kchan
);
1634 LTTNG_ASSERT(kchan
->channel
);
1636 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1638 /* Update channel count of associated session. */
1639 if (kchan
->session
) {
1640 /* Keep pointer reference so we can update it after the destroy. */
1641 ksess
= kchan
->session
;
1644 trace_kernel_destroy_channel(kchan
);
1647 * At this point the kernel channel is not visible anymore. This is safe
1648 * since in order to work on a visible kernel session, the tracing session
1649 * lock (ltt_session.lock) MUST be acquired.
1652 ksess
->channel_count
--;
1657 * Take a snapshot for a given kernel session.
1659 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1661 enum lttng_error_code
kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1662 const struct consumer_output
*output
,
1663 uint64_t nb_packets_per_stream
)
1665 int err
, ret
, saved_metadata_fd
;
1666 enum lttng_error_code status
= LTTNG_OK
;
1667 struct consumer_socket
*socket
;
1668 struct lttng_ht_iter iter
;
1669 struct ltt_kernel_metadata
*saved_metadata
;
1670 char *trace_path
= nullptr;
1671 size_t consumer_path_offset
= 0;
1673 LTTNG_ASSERT(ksess
);
1674 LTTNG_ASSERT(ksess
->consumer
);
1675 LTTNG_ASSERT(output
);
1677 DBG("Kernel snapshot record started");
1679 /* Save current metadata since the following calls will change it. */
1680 saved_metadata
= ksess
->metadata
;
1681 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1683 ret
= kernel_open_metadata(ksess
);
1685 status
= LTTNG_ERR_KERN_META_FAIL
;
1689 ret
= kernel_open_metadata_stream(ksess
);
1691 status
= LTTNG_ERR_KERN_META_FAIL
;
1692 goto error_open_stream
;
1695 trace_path
= setup_channel_trace_path(ksess
->consumer
, "", &consumer_path_offset
);
1697 status
= LTTNG_ERR_INVALID
;
1702 /* Send metadata to consumer and snapshot everything. */
1703 lttng::urcu::read_lock_guard read_lock
;
1705 cds_lfht_for_each_entry (output
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1706 struct ltt_kernel_channel
*chan
;
1708 pthread_mutex_lock(socket
->lock
);
1709 /* This stream must not be monitored by the consumer. */
1710 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1711 pthread_mutex_unlock(socket
->lock
);
1713 status
= LTTNG_ERR_KERN_META_FAIL
;
1714 goto error_consumer
;
1717 /* For each channel, ask the consumer to snapshot it. */
1718 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1720 consumer_snapshot_channel(socket
,
1724 &trace_path
[consumer_path_offset
],
1725 nb_packets_per_stream
);
1726 if (status
!= LTTNG_OK
) {
1727 (void) kernel_consumer_destroy_metadata(socket
,
1729 goto error_consumer
;
1733 /* Snapshot metadata, */
1734 status
= consumer_snapshot_channel(socket
,
1735 ksess
->metadata
->key
,
1738 &trace_path
[consumer_path_offset
],
1740 if (status
!= LTTNG_OK
) {
1741 goto error_consumer
;
1745 * The metadata snapshot is done, ask the consumer to destroy it since
1746 * it's not monitored on the consumer side.
1748 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1753 /* Close newly opened metadata stream. It's now on the consumer side. */
1754 err
= close(ksess
->metadata_stream_fd
);
1756 PERROR("close snapshot kernel");
1760 trace_kernel_destroy_metadata(ksess
->metadata
);
1762 /* Restore metadata state.*/
1763 ksess
->metadata
= saved_metadata
;
1764 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1770 * Get the syscall mask array from the kernel tracer.
1772 * Return 0 on success else a negative value. In both case, syscall_mask should
1775 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1777 LTTNG_ASSERT(syscall_mask
);
1778 LTTNG_ASSERT(nr_bits
);
1780 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1783 static int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1786 struct lttng_kernel_abi_tracer_abi_version abi
;
1788 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1790 ERR("Failed to retrieve lttng-modules ABI version");
1794 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1800 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1803 * Return 1 on success, 0 when feature is not supported, negative value in case
1806 int kernel_supports_ring_buffer_snapshot_sample_positions()
1809 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1811 return kernel_tracer_abi_greater_or_equal(2, 3);
1815 * Check for the support of the packet sequence number via abi version number.
1817 * Return 1 on success, 0 when feature is not supported, negative value in case
1820 int kernel_supports_ring_buffer_packet_sequence_number()
1823 * Packet sequence number was introduced in LTTng 2.8,
1824 * lttng-modules ABI 2.1.
1826 return kernel_tracer_abi_greater_or_equal(2, 1);
1830 * Check for the support of event notifiers via abi version number.
1832 * Return 1 on success, 0 when feature is not supported, negative value in case
1835 int kernel_supports_event_notifiers()
1838 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1840 return kernel_tracer_abi_greater_or_equal(2, 6);
1844 * Rotate a kernel session.
1846 * Return LTTNG_OK on success or else an LTTng error code.
1848 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1851 enum lttng_error_code status
= LTTNG_OK
;
1852 struct consumer_socket
*socket
;
1853 struct lttng_ht_iter iter
;
1854 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1856 LTTNG_ASSERT(ksess
);
1857 LTTNG_ASSERT(ksess
->consumer
);
1859 DBG("Rotate kernel session %s started (session %" PRIu64
")", session
->name
, session
->id
);
1863 * Note that this loop will end after one iteration given that there is
1864 * only one kernel consumer.
1866 lttng::urcu::read_lock_guard read_lock
;
1868 cds_lfht_for_each_entry (
1869 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
1870 struct ltt_kernel_channel
*chan
;
1872 /* For each channel, ask the consumer to rotate it. */
1873 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
1874 DBG("Rotate kernel channel %" PRIu64
", session %s",
1877 ret
= consumer_rotate_channel(socket
,
1880 /* is_metadata_channel */ false);
1882 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1888 * Rotate the metadata channel.
1890 ret
= consumer_rotate_channel(socket
,
1891 ksess
->metadata
->key
,
1893 /* is_metadata_channel */ true);
1895 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1905 enum lttng_error_code
kernel_create_channel_subdirectories(const struct ltt_kernel_session
*ksess
)
1907 enum lttng_error_code ret
= LTTNG_OK
;
1908 enum lttng_trace_chunk_status chunk_status
;
1910 lttng::urcu::read_lock_guard read_lock
;
1911 LTTNG_ASSERT(ksess
->current_trace_chunk
);
1914 * Create the index subdirectory which will take care
1915 * of implicitly creating the channel's path.
1917 chunk_status
= lttng_trace_chunk_create_subdirectory(
1918 ksess
->current_trace_chunk
, DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1919 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1920 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1928 * Setup necessary data for kernel tracer action.
1930 int init_kernel_tracer()
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
, &the_kernel_tracer_abi_version
);
1954 ret
= modprobe_lttng_data();
1959 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1964 WARN("Kernel tracer does not support buffer monitoring. "
1965 "The monitoring timer of channels in the kernel domain "
1966 "will be set to 0 (disabled).");
1969 ret
= kernel_supports_event_notifiers();
1971 ERR("Failed to check for kernel tracer event notifier support");
1974 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1976 /* This is not fatal. */
1977 WARN("Failed to create kernel event notifier group");
1978 kernel_tracer_event_notifier_group_fd
= -1;
1980 enum event_notifier_error_accounting_status error_accounting_status
;
1981 enum lttng_error_code error_code_ret
=
1982 kernel_create_event_notifier_group_notification_fd(
1983 &kernel_tracer_event_notifier_group_notification_fd
);
1985 if (error_code_ret
!= LTTNG_OK
) {
1989 error_accounting_status
= event_notifier_error_accounting_register_kernel(
1990 kernel_tracer_event_notifier_group_fd
);
1991 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
1992 ERR("Failed to initialize event notifier error accounting for kernel tracer");
1993 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
1997 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
1998 DEFAULT_HT_SIZE
, 1, 0, CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
, nullptr);
1999 if (!kernel_token_to_event_notifier_rule_ht
) {
2000 goto error_token_ht
;
2004 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2006 kernel_tracer_event_notifier_group_fd
,
2007 kernel_tracer_event_notifier_group_notification_fd
);
2009 ret
= syscall_init_table(kernel_tracer_fd
);
2011 ERR("Unable to populate syscall table. Syscall tracing won't "
2012 "work for this session daemon.");
2018 modprobe_remove_lttng_control();
2019 ret
= close(kernel_tracer_fd
);
2021 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd
);
2024 kernel_tracer_fd
= -1;
2025 return LTTNG_ERR_KERN_VERSION
;
2028 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2030 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2031 kernel_tracer_event_notifier_group_notification_fd
);
2034 kernel_tracer_event_notifier_group_notification_fd
= -1;
2037 ret
= close(kernel_tracer_event_notifier_group_fd
);
2039 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2040 kernel_tracer_event_notifier_group_fd
);
2043 kernel_tracer_event_notifier_group_fd
= -1;
2045 ret
= close(kernel_tracer_fd
);
2047 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd
);
2050 kernel_tracer_fd
= -1;
2053 modprobe_remove_lttng_control();
2056 WARN("No kernel tracer available");
2057 kernel_tracer_fd
= -1;
2059 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2061 return LTTNG_ERR_KERN_NA
;
2065 void cleanup_kernel_tracer()
2067 DBG2("Closing kernel event notifier group notification file descriptor");
2068 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2069 int ret
= notification_thread_command_remove_tracer_event_source(
2070 the_notification_thread_handle
,
2071 kernel_tracer_event_notifier_group_notification_fd
);
2072 if (ret
!= LTTNG_OK
) {
2073 ERR("Failed to remove kernel event notifier notification from notification thread");
2076 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2078 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2079 kernel_tracer_event_notifier_group_notification_fd
);
2082 kernel_tracer_event_notifier_group_notification_fd
= -1;
2085 if (kernel_token_to_event_notifier_rule_ht
) {
2086 const int ret
= cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht
, nullptr);
2087 LTTNG_ASSERT(ret
== 0);
2090 DBG2("Closing kernel event notifier group file descriptor");
2091 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2092 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2095 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2096 kernel_tracer_event_notifier_group_fd
);
2099 kernel_tracer_event_notifier_group_fd
= -1;
2102 DBG2("Closing kernel fd");
2103 if (kernel_tracer_fd
>= 0) {
2104 const int ret
= close(kernel_tracer_fd
);
2107 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2111 kernel_tracer_fd
= -1;
2114 free(syscall_table
);
2117 bool kernel_tracer_is_initialized()
2119 return kernel_tracer_fd
>= 0;
2123 * Clear a kernel session.
2125 * Return LTTNG_OK on success or else an LTTng error code.
2127 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2130 enum lttng_error_code status
= LTTNG_OK
;
2131 struct consumer_socket
*socket
;
2132 struct lttng_ht_iter iter
;
2133 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2135 LTTNG_ASSERT(ksess
);
2136 LTTNG_ASSERT(ksess
->consumer
);
2138 DBG("Clear kernel session %s (session %" PRIu64
")", session
->name
, session
->id
);
2140 if (ksess
->active
) {
2141 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2142 status
= LTTNG_ERR_FATAL
;
2148 * Note that this loop will end after one iteration given that there is
2149 * only one kernel consumer.
2151 lttng::urcu::read_lock_guard read_lock
;
2153 cds_lfht_for_each_entry (
2154 ksess
->consumer
->socks
->ht
, &iter
.iter
, socket
, node
.node
) {
2155 struct ltt_kernel_channel
*chan
;
2157 /* For each channel, ask the consumer to clear it. */
2158 cds_list_for_each_entry (chan
, &ksess
->channel_list
.head
, list
) {
2159 DBG("Clear kernel channel %" PRIu64
", session %s",
2162 ret
= consumer_clear_channel(socket
, chan
->key
);
2168 if (!ksess
->metadata
) {
2170 * Nothing to do for the metadata.
2171 * This is a snapshot session.
2172 * The metadata is genererated on the fly.
2178 * Clear the metadata channel.
2179 * Metadata channel is not cleared per se but we still need to
2180 * perform a rotation operation on it behind the scene.
2182 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2192 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2193 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2196 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2203 enum lttng_error_code
2204 kernel_create_event_notifier_group_notification_fd(int *event_notifier_group_notification_fd
)
2206 int local_fd
= -1, ret
;
2207 enum lttng_error_code error_code_ret
;
2209 LTTNG_ASSERT(event_notifier_group_notification_fd
);
2211 ret
= kernctl_create_event_notifier_group_notification_fd(
2212 kernel_tracer_event_notifier_group_fd
);
2214 PERROR("Failed to create kernel event notifier group notification file descriptor");
2215 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2221 /* Prevent fd duplication after execlp(). */
2222 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2224 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2226 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2230 DBG("Created kernel notifier group notification file descriptor: fd = %d", local_fd
);
2231 error_code_ret
= LTTNG_OK
;
2232 *event_notifier_group_notification_fd
= local_fd
;
2236 if (local_fd
>= 0) {
2237 ret
= close(local_fd
);
2239 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2244 return error_code_ret
;
2247 enum lttng_error_code
2248 kernel_destroy_event_notifier_group_notification_fd(int event_notifier_group_notification_fd
)
2250 enum lttng_error_code ret_code
= LTTNG_OK
;
2252 DBG("Closing event notifier group notification file descriptor: fd = %d",
2253 event_notifier_group_notification_fd
);
2254 if (event_notifier_group_notification_fd
>= 0) {
2255 const int ret
= close(event_notifier_group_notification_fd
);
2257 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2258 event_notifier_group_notification_fd
);
2265 static unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2267 const struct lttng_condition
*condition
= lttng_trigger_get_const_condition(trigger
);
2269 return lttng_condition_hash(condition
);
2272 static int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2274 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2275 const struct lttng_trigger
*trigger
= (lttng_trigger
*) key
;
2277 event_notifier_rule
=
2278 caa_container_of(node
, const struct ltt_kernel_event_notifier_rule
, ht_node
);
2280 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2283 static enum lttng_error_code
kernel_create_event_notifier_rule(
2284 struct lttng_trigger
*trigger
, const struct lttng_credentials
*creds
, uint64_t token
)
2286 int err
, fd
, ret
= 0;
2287 enum lttng_error_code error_code_ret
;
2288 enum lttng_condition_status condition_status
;
2289 enum lttng_condition_type condition_type
;
2290 enum lttng_event_rule_type event_rule_type
;
2291 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2292 struct lttng_kernel_abi_event_notifier kernel_event_notifier
= {};
2293 unsigned int capture_bytecode_count
= 0, i
;
2294 const struct lttng_condition
*condition
= nullptr;
2295 const struct lttng_event_rule
*event_rule
= nullptr;
2296 enum lttng_condition_status cond_status
;
2298 LTTNG_ASSERT(trigger
);
2300 condition
= lttng_trigger_get_const_condition(trigger
);
2301 LTTNG_ASSERT(condition
);
2303 condition_type
= lttng_condition_get_type(condition
);
2304 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2306 /* Does not acquire a reference. */
2307 condition_status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2308 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2309 LTTNG_ASSERT(event_rule
);
2311 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2312 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2314 error_code_ret
= trace_kernel_create_event_notifier_rule(
2317 lttng_condition_event_rule_matches_get_error_counter_index(condition
),
2318 &event_notifier_rule
);
2319 if (error_code_ret
!= LTTNG_OK
) {
2323 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(event_rule
,
2324 &kernel_event_notifier
);
2325 if (error_code_ret
!= LTTNG_OK
) {
2329 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2330 kernel_event_notifier
.error_counter_idx
=
2331 lttng_condition_event_rule_matches_get_error_counter_index(condition
);
2333 fd
= kernctl_create_event_notifier(kernel_tracer_event_notifier_group_fd
,
2334 &kernel_event_notifier
);
2338 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2341 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2342 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2345 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2346 kernel_event_notifier
.event
.name
);
2347 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2350 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2352 kernel_event_notifier
.event
.name
);
2353 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2358 event_notifier_rule
->fd
= fd
;
2359 /* Prevent fd duplication after execlp(). */
2360 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2362 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2364 error_code_ret
= LTTNG_ERR_FATAL
;
2365 goto set_cloexec_error
;
2368 if (event_notifier_rule
->filter
) {
2369 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2373 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2376 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2383 if (lttng_event_rule_get_type(event_rule
) == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
) {
2384 ret
= userspace_probe_event_rule_add_callsites(
2385 event_rule
, creds
, event_notifier_rule
->fd
);
2387 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2388 goto add_callsite_error
;
2392 /* Set the capture bytecode if any. */
2393 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2394 condition
, &capture_bytecode_count
);
2395 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2397 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2398 const struct lttng_bytecode
*capture_bytecode
=
2399 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition
,
2402 if (capture_bytecode
== nullptr) {
2403 ERR("Unexpected NULL capture bytecode on condition");
2404 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2408 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2410 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2411 event_notifier_rule
->fd
);
2412 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2417 err
= kernctl_enable(event_notifier_rule
->fd
);
2421 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2424 PERROR("enable kernel event notifier");
2425 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2431 /* Add trigger to kernel token mapping in the hash table. */
2433 lttng::urcu::read_lock_guard read_lock
;
2434 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
,
2435 hash_trigger(trigger
),
2436 &event_notifier_rule
->ht_node
);
2439 DBG("Created kernel event notifier: name = '%s', fd = %d",
2440 kernel_event_notifier
.event
.name
,
2441 event_notifier_rule
->fd
);
2451 const int close_ret
= close(event_notifier_rule
->fd
);
2454 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2455 event_notifier_rule
->fd
);
2459 free(event_notifier_rule
);
2461 return error_code_ret
;
2464 enum lttng_error_code
kernel_register_event_notifier(struct lttng_trigger
*trigger
,
2465 const struct lttng_credentials
*cmd_creds
)
2467 enum lttng_error_code ret
;
2468 enum lttng_condition_status status
;
2469 enum lttng_domain_type domain_type
;
2470 const struct lttng_event_rule
*event_rule
;
2471 const struct lttng_condition
*const condition
= lttng_trigger_get_const_condition(trigger
);
2472 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2474 LTTNG_ASSERT(condition
);
2476 /* Does not acquire a reference to the event rule. */
2477 status
= lttng_condition_event_rule_matches_get_rule(condition
, &event_rule
);
2478 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2480 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2481 LTTNG_ASSERT(domain_type
== LTTNG_DOMAIN_KERNEL
);
2483 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2484 if (ret
!= LTTNG_OK
) {
2485 ERR("Failed to create kernel event notifier rule");
2491 enum lttng_error_code
kernel_unregister_event_notifier(const struct lttng_trigger
*trigger
)
2493 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2494 struct cds_lfht_node
*node
;
2495 struct cds_lfht_iter iter
;
2496 enum lttng_error_code error_code_ret
;
2499 lttng::urcu::read_lock_guard read_lock
;
2501 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2502 hash_trigger(trigger
),
2507 node
= cds_lfht_iter_get_node(&iter
);
2509 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2513 token_event_rule_element
=
2514 caa_container_of(node
, struct ltt_kernel_event_notifier_rule
, ht_node
);
2516 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2518 error_code_ret
= LTTNG_ERR_FATAL
;
2522 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2523 error_code_ret
= LTTNG_OK
;
2527 return error_code_ret
;
2530 int kernel_get_notification_fd()
2532 return kernel_tracer_event_notifier_group_notification_fd
;