2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
17 #include <common/common.hpp>
18 #include <common/hashtable/utils.hpp>
19 #include <common/trace-chunk.hpp>
20 #include <common/kernel-ctl/kernel-ctl.hpp>
21 #include <common/kernel-ctl/kernel-ioctl.hpp>
22 #include <common/sessiond-comm/sessiond-comm.hpp>
23 #include <common/tracker.hpp>
24 #include <common/utils.hpp>
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.hpp>
31 #include <lttng/condition/event-rule-matches.h>
32 #include <lttng/condition/event-rule-matches-internal.hpp>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.hpp>
35 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
37 #include "event-notifier-error-accounting.hpp"
38 #include "lttng-sessiond.hpp"
39 #include "lttng-syscall.hpp"
40 #include "condition-internal.hpp"
41 #include "consumer.hpp"
43 #include "kernel-consumer.hpp"
44 #include "kern-modules.hpp"
45 #include "sessiond-config.hpp"
48 #include "modprobe.hpp"
49 #include "tracker.hpp"
50 #include "notification-thread-commands.hpp"
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
;
118 LTTNG_ASSERT(session
);
120 /* Allocate data structure */
121 lks
= trace_kernel_create_session(
122 std::const_pointer_cast
<const lttng::trace_format_descriptor
>(
123 session
->trace_format
));
129 /* Kernel tracer session creation */
130 ret
= kernctl_create_session(kernel_tracer_fd
);
132 PERROR("ioctl kernel create session");
137 /* Prevent fd duplication after execlp() */
138 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
140 PERROR("fcntl session fd");
143 lks
->id
= session
->id
;
144 lks
->consumer_fds_sent
= 0;
145 session
->kernel_session
= lks
;
147 DBG("Kernel session created (fd: %d)", lks
->fd
);
150 * This is necessary since the creation time is present in the session
151 * name when it is generated.
153 if (session
->has_auto_generated_name
) {
154 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
156 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
159 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
160 session
->id
, session
->name
);
163 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
165 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
166 session
->id
, session
->name
);
169 ret
= kernctl_session_set_trace_format(lks
->fd
, *session
->trace_format
);
171 WARN("Could not set kernel session trace format %" PRIu64
" name: %s", session
->id
,
178 trace_kernel_destroy_session(lks
);
179 trace_kernel_free_session(lks
);
185 * Create a kernel channel, register it to the kernel tracer and add it to the
188 int kernel_create_channel(struct ltt_kernel_session
*session
,
189 struct lttng_channel
*chan
)
192 struct ltt_kernel_channel
*lkc
;
194 LTTNG_ASSERT(session
);
197 /* Allocate kernel channel */
198 lkc
= trace_kernel_create_channel(chan
);
203 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
204 chan
->name
, lkc
->channel
->attr
.overwrite
,
205 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
206 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
207 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
209 /* Kernel tracer channel creation */
210 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
212 PERROR("ioctl kernel create channel");
216 /* Setup the channel fd */
218 /* Prevent fd duplication after execlp() */
219 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
221 PERROR("fcntl session fd");
224 /* Add channel to session */
225 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
226 session
->channel_count
++;
227 lkc
->session
= session
;
228 lkc
->key
= ++next_kernel_channel_key
;
230 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
231 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
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
297 int extract_userspace_probe_offset_function_elf(
298 const struct lttng_userspace_probe_location
*probe_location
,
299 uid_t uid
, gid_t gid
, uint64_t *offset
)
303 const char *symbol
= NULL
;
304 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
305 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
307 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
308 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
310 lookup
= lttng_userspace_probe_location_get_lookup_method(
318 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
320 LTTNG_ASSERT(lookup_method_type
==
321 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
323 symbol
= lttng_userspace_probe_location_function_get_function_name(
330 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
336 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
338 DBG("userspace probe offset calculation failed for "
339 "function %s", symbol
);
343 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
349 * Compute the offsets of the instrumentation bytes in the binary based on the
350 * tracepoint probe location using the SDT lookup method. This function
351 * allocates the offsets buffer, the caller must free it.
353 * Returns 0 on success and set the offset out parameter to the offsets of the
355 * Returns -1 on error.
358 int extract_userspace_probe_offset_tracepoint_sdt(
359 const struct lttng_userspace_probe_location
*probe_location
,
360 uid_t uid
, gid_t gid
, uint64_t **offsets
,
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
= NULL
;
365 const char *probe_name
= NULL
, *provider_name
= NULL
;
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
);
379 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
381 LTTNG_ASSERT(lookup_method_type
==
382 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
385 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
392 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
394 if (!provider_name
) {
399 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
405 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
406 uid
, gid
, offsets
, offsets_count
);
408 DBG("userspace probe offset calculation failed for sdt "
409 "probe %s:%s", provider_name
, probe_name
);
413 if (*offsets_count
== 0) {
414 DBG("no userspace probe offset found");
418 DBG("%u userspace probe SDT offsets found for %s:%s at:",
419 *offsets_count
, provider_name
, probe_name
);
420 for (i
= 0; i
< *offsets_count
; i
++) {
421 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
428 int userspace_probe_add_callsite(
429 const struct lttng_userspace_probe_location
*location
,
430 uid_t uid
, gid_t gid
, int fd
)
432 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
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
,
452 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
456 callsite
.u
.uprobe
.offset
= offset
;
457 ret
= kernctl_add_callsite(fd
, &callsite
);
459 WARN("Failed to add callsite to ELF userspace probe.");
460 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
465 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
468 uint64_t *offsets
= NULL
;
469 uint32_t offsets_count
;
470 struct lttng_kernel_abi_event_callsite callsite
;
473 * This call allocates the offsets buffer. This buffer must be freed
476 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
477 uid
, gid
, &offsets
, &offsets_count
);
479 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
482 for (i
= 0; i
< offsets_count
; i
++) {
483 callsite
.u
.uprobe
.offset
= offsets
[i
];
484 ret
= kernctl_add_callsite(fd
, &callsite
);
486 WARN("Failed to add callsite to SDT userspace probe");
487 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
496 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
504 * Extract the offsets of the instrumentation point for the different lookup
508 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
509 struct ltt_kernel_session
*session
, int fd
)
512 const struct lttng_userspace_probe_location
*location
= NULL
;
515 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
517 location
= lttng_event_get_userspace_probe_location(ev
);
523 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
526 WARN("Failed to add callsite to userspace probe event '%s'",
535 * Extract the offsets of the instrumentation point for the different look-up
538 static int userspace_probe_event_rule_add_callsites(
539 const struct lttng_event_rule
*rule
,
540 const struct lttng_credentials
*creds
,
544 enum lttng_event_rule_status status
;
545 enum lttng_event_rule_type event_rule_type
;
546 const struct lttng_userspace_probe_location
*location
= NULL
;
551 event_rule_type
= lttng_event_rule_get_type(rule
);
552 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
554 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
555 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
560 ret
= userspace_probe_add_callsite(location
,
561 lttng_credentials_get_uid(creds
),
562 lttng_credentials_get_gid(creds
), fd
);
564 WARN("Failed to add callsite to user space probe object: fd = %d",
573 * Create a kernel event, enable it to the kernel tracer and add it to the
574 * channel event list of the kernel session.
575 * We own filter_expression and filter.
577 int kernel_create_event(struct lttng_event
*ev
,
578 struct ltt_kernel_channel
*channel
,
579 char *filter_expression
,
580 struct lttng_bytecode
*filter
)
583 enum lttng_error_code ret
;
584 struct ltt_kernel_event
*event
;
587 LTTNG_ASSERT(channel
);
589 /* We pass ownership of filter_expression and filter */
590 ret
= trace_kernel_create_event(ev
, filter_expression
,
592 if (ret
!= LTTNG_OK
) {
596 fd
= kernctl_create_event(channel
->fd
, event
->event
);
600 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
603 WARN("Event type not implemented");
604 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
607 WARN("Event %s not found!", ev
->name
);
608 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
611 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
612 PERROR("create event ioctl");
617 event
->type
= ev
->type
;
619 /* Prevent fd duplication after execlp() */
620 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
622 PERROR("fcntl session fd");
626 err
= kernctl_filter(event
->fd
, filter
);
630 ret
= LTTNG_ERR_FILTER_NOMEM
;
633 ret
= LTTNG_ERR_FILTER_INVAL
;
640 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
641 ret
= (lttng_error_code
) userspace_probe_event_add_callsites(ev
, channel
->session
,
644 goto add_callsite_error
;
648 err
= kernctl_enable(event
->fd
);
652 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
655 PERROR("enable kernel event");
656 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
662 /* Add event to event list */
663 cds_list_add(&event
->list
, &channel
->events_list
.head
);
664 channel
->event_count
++;
666 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
676 closeret
= close(event
->fd
);
678 PERROR("close event fd");
688 * Disable a kernel channel.
690 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
696 ret
= kernctl_disable(chan
->fd
);
698 PERROR("disable chan ioctl");
703 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
704 chan
->channel
->name
, chan
->fd
, chan
->key
);
713 * Enable a kernel channel.
715 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
721 ret
= kernctl_enable(chan
->fd
);
722 if (ret
< 0 && ret
!= -EEXIST
) {
723 PERROR("Enable kernel chan");
728 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
729 chan
->channel
->name
, chan
->fd
, chan
->key
);
738 * Enable a kernel event.
740 int kernel_enable_event(struct ltt_kernel_event
*event
)
746 ret
= kernctl_enable(event
->fd
);
750 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
753 PERROR("enable kernel event");
760 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
769 * Disable a kernel event.
771 int kernel_disable_event(struct ltt_kernel_event
*event
)
777 ret
= kernctl_disable(event
->fd
);
779 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
780 event
->event
->name
, event
->fd
);
785 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
794 * Disable a kernel event notifier.
797 int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
804 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
807 ret
= kernctl_disable(event
->fd
);
809 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
810 event
->fd
, event
->token
);
815 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
,
816 event
->fd
, event
->token
);
823 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
824 struct ltt_kernel_session
*session
,
825 enum lttng_process_attr process_attr
)
827 switch (process_attr
) {
828 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
829 return session
->tracker_pid
;
830 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
831 return session
->tracker_vpid
;
832 case LTTNG_PROCESS_ATTR_USER_ID
:
833 return session
->tracker_uid
;
834 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
835 return session
->tracker_vuid
;
836 case LTTNG_PROCESS_ATTR_GROUP_ID
:
837 return session
->tracker_gid
;
838 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
839 return session
->tracker_vgid
;
845 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
846 struct ltt_kernel_session
*session
,
847 enum lttng_process_attr process_attr
)
849 return (const struct process_attr_tracker
*)
850 _kernel_get_process_attr_tracker(session
, process_attr
);
853 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
854 struct ltt_kernel_session
*session
,
855 enum lttng_process_attr process_attr
,
856 enum lttng_tracking_policy policy
)
859 enum lttng_error_code ret_code
= LTTNG_OK
;
860 struct process_attr_tracker
*tracker
=
861 _kernel_get_process_attr_tracker(session
, process_attr
);
862 enum lttng_tracking_policy previous_policy
;
865 ret_code
= LTTNG_ERR_INVALID
;
869 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
870 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
872 ret_code
= LTTNG_ERR_UNK
;
876 if (previous_policy
== policy
) {
881 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
882 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
884 * Maintain a special case for the process ID process
885 * attribute tracker as it was the only supported
886 * attribute prior to 2.12.
888 ret
= kernctl_track_pid(session
->fd
, -1);
890 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
893 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
894 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
896 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
898 * Maintain a special case for the process ID process
899 * attribute tracker as it was the only supported
900 * attribute prior to 2.12.
902 ret
= kernctl_untrack_pid(session
->fd
, -1);
904 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
910 /* kern-ctl error handling */
916 ret_code
= LTTNG_ERR_INVALID
;
919 ret_code
= LTTNG_ERR_NOMEM
;
922 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
925 ret_code
= LTTNG_ERR_UNK
;
932 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
933 struct ltt_kernel_session
*session
,
934 enum lttng_process_attr process_attr
,
935 const struct process_attr_value
*value
)
937 int ret
, integral_value
;
938 enum lttng_error_code ret_code
;
939 struct process_attr_tracker
*tracker
;
940 enum process_attr_tracker_status status
;
943 * Convert process attribute tracker value to the integral
944 * representation required by the kern-ctl API.
946 switch (process_attr
) {
947 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
948 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
949 integral_value
= (int) value
->value
.pid
;
951 case LTTNG_PROCESS_ATTR_USER_ID
:
952 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
953 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
956 ret_code
= utils_user_id_from_name(
957 value
->value
.user_name
, &uid
);
958 if (ret_code
!= LTTNG_OK
) {
961 integral_value
= (int) uid
;
963 integral_value
= (int) value
->value
.uid
;
966 case LTTNG_PROCESS_ATTR_GROUP_ID
:
967 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
968 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
971 ret_code
= utils_group_id_from_name(
972 value
->value
.group_name
, &gid
);
973 if (ret_code
!= LTTNG_OK
) {
976 integral_value
= (int) gid
;
978 integral_value
= (int) value
->value
.gid
;
982 ret_code
= LTTNG_ERR_INVALID
;
986 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
988 ret_code
= LTTNG_ERR_INVALID
;
992 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
993 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
995 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
996 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
998 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
999 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1001 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1003 ret_code
= LTTNG_ERR_UNK
;
1009 DBG("Kernel track %s %d for session id %" PRIu64
,
1010 lttng_process_attr_to_string(process_attr
),
1011 integral_value
, session
->id
);
1012 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1014 * Maintain a special case for the process ID process attribute
1015 * tracker as it was the only supported attribute prior to 2.12.
1017 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1019 ret
= kernctl_track_id(
1020 session
->fd
, process_attr
, integral_value
);
1023 ret_code
= LTTNG_OK
;
1027 kernel_wait_quiescent();
1029 /* kern-ctl error handling */
1032 ret_code
= LTTNG_OK
;
1035 ret_code
= LTTNG_ERR_INVALID
;
1038 ret_code
= LTTNG_ERR_NOMEM
;
1041 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1044 ret_code
= LTTNG_ERR_UNK
;
1048 /* Attempt to remove the value from the tracker. */
1049 status
= process_attr_tracker_inclusion_set_remove_value(
1051 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1052 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1053 lttng_process_attr_to_string(process_attr
),
1060 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1061 struct ltt_kernel_session
*session
,
1062 enum lttng_process_attr process_attr
,
1063 const struct process_attr_value
*value
)
1065 int ret
, integral_value
;
1066 enum lttng_error_code ret_code
;
1067 struct process_attr_tracker
*tracker
;
1068 enum process_attr_tracker_status status
;
1071 * Convert process attribute tracker value to the integral
1072 * representation required by the kern-ctl API.
1074 switch (process_attr
) {
1075 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1076 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1077 integral_value
= (int) value
->value
.pid
;
1079 case LTTNG_PROCESS_ATTR_USER_ID
:
1080 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1081 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1084 ret_code
= utils_user_id_from_name(
1085 value
->value
.user_name
, &uid
);
1086 if (ret_code
!= LTTNG_OK
) {
1089 integral_value
= (int) uid
;
1091 integral_value
= (int) value
->value
.uid
;
1094 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1095 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1096 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1099 ret_code
= utils_group_id_from_name(
1100 value
->value
.group_name
, &gid
);
1101 if (ret_code
!= LTTNG_OK
) {
1104 integral_value
= (int) gid
;
1106 integral_value
= (int) value
->value
.gid
;
1110 ret_code
= LTTNG_ERR_INVALID
;
1114 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1116 ret_code
= LTTNG_ERR_INVALID
;
1120 status
= process_attr_tracker_inclusion_set_remove_value(
1122 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1124 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1125 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1127 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1128 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1130 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1132 ret_code
= LTTNG_ERR_UNK
;
1138 DBG("Kernel track %s %d for session id %" PRIu64
,
1139 lttng_process_attr_to_string(process_attr
),
1140 integral_value
, session
->id
);
1141 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1143 * Maintain a special case for the process ID process attribute
1144 * tracker as it was the only supported attribute prior to 2.12.
1146 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1148 ret
= kernctl_untrack_id(
1149 session
->fd
, process_attr
, integral_value
);
1152 ret_code
= LTTNG_OK
;
1155 kernel_wait_quiescent();
1157 /* kern-ctl error handling */
1160 ret_code
= LTTNG_OK
;
1163 ret_code
= LTTNG_ERR_INVALID
;
1166 ret_code
= LTTNG_ERR_NOMEM
;
1169 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1172 ret_code
= LTTNG_ERR_UNK
;
1176 /* Attempt to add the value to the tracker. */
1177 status
= process_attr_tracker_inclusion_set_add_value(
1179 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1180 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1181 lttng_process_attr_to_string(process_attr
),
1189 * Create kernel metadata, open from the kernel tracer and add it to the
1192 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1195 struct ltt_kernel_metadata
*lkm
= NULL
;
1197 LTTNG_ASSERT(session
);
1199 /* Allocate kernel metadata */
1200 lkm
= trace_kernel_create_metadata();
1205 /* Kernel tracer metadata creation */
1206 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1212 lkm
->key
= ++next_kernel_channel_key
;
1213 /* Prevent fd duplication after execlp() */
1214 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1216 PERROR("fcntl session fd");
1219 session
->metadata
= lkm
;
1221 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1226 trace_kernel_destroy_metadata(lkm
);
1232 * Start tracing session.
1234 int kernel_start_session(struct ltt_kernel_session
*session
)
1238 LTTNG_ASSERT(session
);
1240 ret
= kernctl_start_session(session
->fd
);
1242 PERROR("ioctl start session");
1246 DBG("Kernel session started");
1255 * Make a kernel wait to make sure in-flight probe have completed.
1257 void kernel_wait_quiescent(void)
1260 int fd
= kernel_tracer_fd
;
1262 DBG("Kernel quiescent wait on %d", fd
);
1264 ret
= kernctl_wait_quiescent(fd
);
1266 PERROR("wait quiescent ioctl");
1267 ERR("Kernel quiescent wait failed");
1272 * Force flush buffer of metadata.
1274 int kernel_metadata_flush_buffer(int fd
)
1278 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1280 ret
= kernctl_buffer_flush(fd
);
1282 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1289 * Force flush buffer for channel.
1291 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1294 struct ltt_kernel_stream
*stream
;
1296 LTTNG_ASSERT(channel
);
1298 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1300 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1301 DBG("Flushing channel stream %d", stream
->fd
);
1302 ret
= kernctl_buffer_flush(stream
->fd
);
1305 ERR("Fail to flush buffer for stream %d (ret: %d)",
1314 * Stop tracing session.
1316 int kernel_stop_session(struct ltt_kernel_session
*session
)
1320 LTTNG_ASSERT(session
);
1322 ret
= kernctl_stop_session(session
->fd
);
1327 DBG("Kernel session stopped");
1336 * Open stream of channel, register it to the kernel tracer and add it
1337 * to the stream list of the channel.
1339 * Note: given that the streams may appear in random order wrt CPU
1340 * number (e.g. cpu hotplug), the index value of the stream number in
1341 * the stream name is not necessarily linked to the CPU number.
1343 * Return the number of created stream. Else, a negative value.
1345 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1348 struct ltt_kernel_stream
*lks
;
1350 LTTNG_ASSERT(channel
);
1352 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1353 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1354 channel
->stream_count
);
1364 /* Prevent fd duplication after execlp() */
1365 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1367 PERROR("fcntl session fd");
1370 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1371 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1373 /* Add stream to channel stream list */
1374 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1375 channel
->stream_count
++;
1377 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1381 return channel
->stream_count
;
1388 * Open the metadata stream and set it to the kernel session.
1390 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1394 LTTNG_ASSERT(session
);
1396 ret
= kernctl_create_stream(session
->metadata
->fd
);
1398 PERROR("kernel create metadata stream");
1402 DBG("Kernel metadata stream created (fd: %d)", ret
);
1403 session
->metadata_stream_fd
= ret
;
1404 /* Prevent fd duplication after execlp() */
1405 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1407 PERROR("fcntl session fd");
1417 * Get the event list from the kernel tracer and return the number of elements.
1419 ssize_t
kernel_list_events(struct lttng_event
**events
)
1423 size_t nbmem
, count
= 0;
1425 struct lttng_event
*elist
;
1427 LTTNG_ASSERT(events
);
1429 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1431 PERROR("kernel tracepoint list");
1435 fp
= fdopen(fd
, "r");
1437 PERROR("kernel tracepoint list fdopen");
1442 * Init memory size counter
1443 * See kernel-ctl.h for explanation of this value
1445 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1446 elist
= calloc
<lttng_event
>(nbmem
);
1447 if (elist
== NULL
) {
1448 PERROR("alloc list events");
1453 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1454 if (count
>= nbmem
) {
1455 struct lttng_event
*new_elist
;
1458 new_nbmem
= nbmem
<< 1;
1459 DBG("Reallocating event list from %zu to %zu bytes",
1461 new_elist
= (lttng_event
*) realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1462 if (new_elist
== NULL
) {
1463 PERROR("realloc list events");
1469 /* Zero the new memory */
1470 memset(new_elist
+ nbmem
, 0,
1471 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1475 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1476 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1477 elist
[count
].enabled
= -1;
1483 DBG("Kernel list events done (%zu events)", count
);
1485 ret
= fclose(fp
); /* closes both fp and fd */
1501 * Get kernel version and validate it.
1503 int kernel_validate_version(struct lttng_kernel_abi_tracer_version
*version
,
1504 struct lttng_kernel_abi_tracer_abi_version
*abi_version
)
1508 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1510 ERR("Failed to retrieve the lttng-modules version");
1514 /* Validate version */
1515 if (version
->major
!= VERSION_MAJOR
) {
1516 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1517 version
->major
, VERSION_MAJOR
);
1520 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1522 ERR("Failed to retrieve lttng-modules ABI version");
1525 if (abi_version
->major
!= LTTNG_KERNEL_ABI_MAJOR_VERSION
) {
1526 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1527 abi_version
->major
, abi_version
->minor
,
1528 LTTNG_KERNEL_ABI_MAJOR_VERSION
);
1531 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1532 version
->major
, version
->minor
,
1533 abi_version
->major
, abi_version
->minor
);
1540 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1545 * Kernel work-arounds called at the start of sessiond main().
1547 int init_kernel_workarounds(void)
1553 * boot_id needs to be read once before being used concurrently
1554 * to deal with a Linux kernel race. A fix is proposed for
1555 * upstream, but the work-around is needed for older kernels.
1557 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1564 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1566 /* Ignore error, we don't really care */
1578 * Teardown of a kernel session, keeping data required by destroy notifiers.
1580 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1582 struct lttng_trace_chunk
*trace_chunk
;
1584 if (ksess
== NULL
) {
1585 DBG3("No kernel session when tearing down session");
1589 DBG("Tearing down kernel session");
1590 trace_chunk
= ksess
->current_trace_chunk
;
1593 * Destroy channels on the consumer if at least one FD has been sent and we
1594 * are in no output mode because the streams are in *no* monitor mode so we
1595 * have to send a command to clean them up or else they leaked.
1597 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1599 struct consumer_socket
*socket
;
1600 struct lttng_ht_iter iter
;
1602 /* For each consumer socket. */
1604 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1605 socket
, node
.node
) {
1606 struct ltt_kernel_channel
*chan
;
1608 /* For each channel, ask the consumer to destroy it. */
1609 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1610 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1612 /* Consumer is probably dead. Use next socket. */
1620 /* Close any relayd session */
1621 consumer_output_send_destroy_relayd(ksess
->consumer
);
1623 trace_kernel_destroy_session(ksess
);
1624 lttng_trace_chunk_put(trace_chunk
);
1627 /* Teardown of data required by destroy notifiers. */
1628 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1630 if (ksess
== NULL
) {
1633 trace_kernel_free_session(ksess
);
1637 * Destroy a kernel channel object. It does not do anything on the tracer side.
1639 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1641 struct ltt_kernel_session
*ksess
= NULL
;
1643 LTTNG_ASSERT(kchan
);
1644 LTTNG_ASSERT(kchan
->channel
);
1646 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1648 /* Update channel count of associated session. */
1649 if (kchan
->session
) {
1650 /* Keep pointer reference so we can update it after the destroy. */
1651 ksess
= kchan
->session
;
1654 trace_kernel_destroy_channel(kchan
);
1657 * At this point the kernel channel is not visible anymore. This is safe
1658 * since in order to work on a visible kernel session, the tracing session
1659 * lock (ltt_session.lock) MUST be acquired.
1662 ksess
->channel_count
--;
1667 * Take a snapshot for a given kernel session.
1669 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1671 enum lttng_error_code
kernel_snapshot_record(
1672 struct ltt_kernel_session
*ksess
,
1673 const struct consumer_output
*output
,
1674 uint64_t nb_packets_per_stream
)
1676 int err
, ret
, saved_metadata_fd
;
1677 enum lttng_error_code status
= LTTNG_OK
;
1678 struct consumer_socket
*socket
;
1679 struct lttng_ht_iter iter
;
1680 struct ltt_kernel_metadata
*saved_metadata
;
1681 char *trace_path
= NULL
;
1682 size_t consumer_path_offset
= 0;
1684 LTTNG_ASSERT(ksess
);
1685 LTTNG_ASSERT(ksess
->consumer
);
1686 LTTNG_ASSERT(output
);
1688 DBG("Kernel snapshot record started");
1690 /* Save current metadata since the following calls will change it. */
1691 saved_metadata
= ksess
->metadata
;
1692 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1696 ret
= kernel_open_metadata(ksess
);
1698 status
= LTTNG_ERR_KERN_META_FAIL
;
1702 ret
= kernel_open_metadata_stream(ksess
);
1704 status
= LTTNG_ERR_KERN_META_FAIL
;
1705 goto error_open_stream
;
1708 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1709 "", &consumer_path_offset
);
1711 status
= LTTNG_ERR_INVALID
;
1714 /* Send metadata to consumer and snapshot everything. */
1715 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1716 socket
, node
.node
) {
1717 struct ltt_kernel_channel
*chan
;
1719 pthread_mutex_lock(socket
->lock
);
1720 /* This stream must not be monitored by the consumer. */
1721 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1722 pthread_mutex_unlock(socket
->lock
);
1724 status
= LTTNG_ERR_KERN_META_FAIL
;
1725 goto error_consumer
;
1728 /* For each channel, ask the consumer to snapshot it. */
1729 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1730 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1731 &trace_path
[consumer_path_offset
],
1732 nb_packets_per_stream
);
1733 if (status
!= LTTNG_OK
) {
1734 (void) kernel_consumer_destroy_metadata(socket
,
1736 goto error_consumer
;
1740 /* Snapshot metadata, */
1741 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1742 1, &trace_path
[consumer_path_offset
], 0);
1743 if (status
!= LTTNG_OK
) {
1744 goto error_consumer
;
1748 * The metadata snapshot is done, ask the consumer to destroy it since
1749 * it's not monitored on the consumer side.
1751 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1755 /* Close newly opened metadata stream. It's now on the consumer side. */
1756 err
= close(ksess
->metadata_stream_fd
);
1758 PERROR("close snapshot kernel");
1762 trace_kernel_destroy_metadata(ksess
->metadata
);
1764 /* Restore metadata state.*/
1765 ksess
->metadata
= saved_metadata
;
1766 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1773 * Get the syscall mask array from the kernel tracer.
1775 * Return 0 on success else a negative value. In both case, syscall_mask should
1778 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1780 LTTNG_ASSERT(syscall_mask
);
1781 LTTNG_ASSERT(nr_bits
);
1783 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1787 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1790 struct lttng_kernel_abi_tracer_abi_version abi
;
1792 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1794 ERR("Failed to retrieve lttng-modules ABI version");
1798 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1804 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1807 * Return 1 on success, 0 when feature is not supported, negative value in case
1810 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1813 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1815 return kernel_tracer_abi_greater_or_equal(2, 3);
1819 * Check for the support of the packet sequence number via abi version number.
1821 * Return 1 on success, 0 when feature is not supported, negative value in case
1824 int kernel_supports_ring_buffer_packet_sequence_number(void)
1827 * Packet sequence number was introduced in LTTng 2.8,
1828 * lttng-modules ABI 2.1.
1830 return kernel_tracer_abi_greater_or_equal(2, 1);
1834 * Check for the support of event notifiers via abi version number.
1836 * Return 1 on success, 0 when feature is not supported, negative value in case
1839 int kernel_supports_event_notifiers(void)
1842 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1844 return kernel_tracer_abi_greater_or_equal(2, 6);
1848 * Check for the support of ctf2 via abi version number.
1850 * Return 1 on success, 0 when feature is not supported, negative value in case
1853 int kernel_supports_ctf2(void)
1856 * JORAJ TODO: change this for the actual value when merged upstream
1857 * default to 2.7 ABI for now (modules 2.14)
1858 * CTF2 support was introduced in LTTng 2.1XX, lttng-modules ABI 2.XX.
1860 return kernel_tracer_abi_greater_or_equal(2, 7);
1864 * Rotate a kernel session.
1866 * Return LTTNG_OK on success or else an LTTng error code.
1868 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1871 enum lttng_error_code status
= LTTNG_OK
;
1872 struct consumer_socket
*socket
;
1873 struct lttng_ht_iter iter
;
1874 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1876 LTTNG_ASSERT(ksess
);
1877 LTTNG_ASSERT(ksess
->consumer
);
1879 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1880 session
->name
, session
->id
);
1885 * Note that this loop will end after one iteration given that there is
1886 * only one kernel consumer.
1888 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1889 socket
, node
.node
) {
1890 struct ltt_kernel_channel
*chan
;
1892 /* For each channel, ask the consumer to rotate it. */
1893 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1894 DBG("Rotate kernel channel %" PRIu64
", session %s",
1895 chan
->key
, session
->name
);
1896 ret
= consumer_rotate_channel(socket
, chan
->key
,
1898 /* is_metadata_channel */ false);
1900 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1906 * Rotate the metadata channel.
1908 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1910 /* is_metadata_channel */ true);
1912 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1922 enum lttng_error_code
kernel_create_channel_subdirectories(
1923 const struct ltt_kernel_session
*ksess
)
1925 enum lttng_error_code ret
= LTTNG_OK
;
1926 enum lttng_trace_chunk_status chunk_status
;
1929 LTTNG_ASSERT(ksess
->current_trace_chunk
);
1932 * Create the index subdirectory which will take care
1933 * of implicitly creating the channel's path.
1935 chunk_status
= lttng_trace_chunk_create_subdirectory(
1936 ksess
->current_trace_chunk
,
1937 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1938 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1939 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1948 * Setup necessary data for kernel tracer action.
1950 int init_kernel_tracer(void)
1953 bool is_root
= !getuid();
1955 /* Modprobe lttng kernel modules */
1956 ret
= modprobe_lttng_control();
1961 /* Open debugfs lttng */
1962 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1963 if (kernel_tracer_fd
< 0) {
1964 DBG("Failed to open %s", module_proc_lttng
);
1968 /* Validate kernel version */
1969 ret
= kernel_validate_version(&the_kernel_tracer_version
,
1970 &the_kernel_tracer_abi_version
);
1975 ret
= modprobe_lttng_data();
1980 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1985 WARN("Kernel tracer does not support buffer monitoring. "
1986 "The monitoring timer of channels in the kernel domain "
1987 "will be set to 0 (disabled).");
1990 ret
= kernel_supports_event_notifiers();
1992 ERR("Failed to check for kernel tracer event notifier support");
1995 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1997 /* This is not fatal. */
1998 WARN("Failed to create kernel event notifier group");
1999 kernel_tracer_event_notifier_group_fd
= -1;
2001 enum event_notifier_error_accounting_status error_accounting_status
;
2002 enum lttng_error_code error_code_ret
=
2003 kernel_create_event_notifier_group_notification_fd(
2004 &kernel_tracer_event_notifier_group_notification_fd
);
2006 if (error_code_ret
!= LTTNG_OK
) {
2010 error_accounting_status
= event_notifier_error_accounting_register_kernel(
2011 kernel_tracer_event_notifier_group_fd
);
2012 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2013 ERR("Failed to initialize event notifier error accounting for kernel tracer");
2014 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
2018 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
2019 DEFAULT_HT_SIZE
, 1, 0,
2020 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
2022 if (!kernel_token_to_event_notifier_rule_ht
) {
2023 goto error_token_ht
;
2027 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2028 kernel_tracer_fd
, kernel_tracer_event_notifier_group_fd
,
2029 kernel_tracer_event_notifier_group_notification_fd
);
2031 ret
= syscall_init_table(kernel_tracer_fd
);
2033 ERR("Unable to populate syscall table. Syscall tracing won't "
2034 "work for this session daemon.");
2040 modprobe_remove_lttng_control();
2041 ret
= close(kernel_tracer_fd
);
2043 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2047 kernel_tracer_fd
= -1;
2048 return LTTNG_ERR_KERN_VERSION
;
2052 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2054 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2055 kernel_tracer_event_notifier_group_notification_fd
);
2058 kernel_tracer_event_notifier_group_notification_fd
= -1;
2061 ret
= close(kernel_tracer_event_notifier_group_fd
);
2063 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2064 kernel_tracer_event_notifier_group_fd
);
2067 kernel_tracer_event_notifier_group_fd
= -1;
2069 ret
= close(kernel_tracer_fd
);
2071 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2075 kernel_tracer_fd
= -1;
2078 modprobe_remove_lttng_control();
2081 WARN("No kernel tracer available");
2082 kernel_tracer_fd
= -1;
2084 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2086 return LTTNG_ERR_KERN_NA
;
2090 void cleanup_kernel_tracer(void)
2092 DBG2("Closing kernel event notifier group notification file descriptor");
2093 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2094 int ret
= notification_thread_command_remove_tracer_event_source(
2095 the_notification_thread_handle
,
2096 kernel_tracer_event_notifier_group_notification_fd
);
2097 if (ret
!= LTTNG_OK
) {
2098 ERR("Failed to remove kernel event notifier notification from notification thread");
2101 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2103 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2104 kernel_tracer_event_notifier_group_notification_fd
);
2107 kernel_tracer_event_notifier_group_notification_fd
= -1;
2110 if (kernel_token_to_event_notifier_rule_ht
) {
2111 const int ret
= cds_lfht_destroy(
2112 kernel_token_to_event_notifier_rule_ht
, NULL
);
2113 LTTNG_ASSERT(ret
== 0);
2116 DBG2("Closing kernel event notifier group file descriptor");
2117 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2118 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2121 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2122 kernel_tracer_event_notifier_group_fd
);
2125 kernel_tracer_event_notifier_group_fd
= -1;
2128 DBG2("Closing kernel fd");
2129 if (kernel_tracer_fd
>= 0) {
2130 const int ret
= close(kernel_tracer_fd
);
2133 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2137 kernel_tracer_fd
= -1;
2140 free(syscall_table
);
2143 bool kernel_tracer_is_initialized(void)
2145 return kernel_tracer_fd
>= 0;
2149 * Clear a kernel session.
2151 * Return LTTNG_OK on success or else an LTTng error code.
2153 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2156 enum lttng_error_code status
= LTTNG_OK
;
2157 struct consumer_socket
*socket
;
2158 struct lttng_ht_iter iter
;
2159 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2161 LTTNG_ASSERT(ksess
);
2162 LTTNG_ASSERT(ksess
->consumer
);
2164 DBG("Clear kernel session %s (session %" PRIu64
")",
2165 session
->name
, session
->id
);
2169 if (ksess
->active
) {
2170 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2171 status
= LTTNG_ERR_FATAL
;
2176 * Note that this loop will end after one iteration given that there is
2177 * only one kernel consumer.
2179 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2180 socket
, node
.node
) {
2181 struct ltt_kernel_channel
*chan
;
2183 /* For each channel, ask the consumer to clear it. */
2184 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2185 DBG("Clear kernel channel %" PRIu64
", session %s",
2186 chan
->key
, session
->name
);
2187 ret
= consumer_clear_channel(socket
, chan
->key
);
2193 if (!ksess
->metadata
) {
2195 * Nothing to do for the metadata.
2196 * This is a snapshot session.
2197 * The metadata is genererated on the fly.
2203 * Clear the metadata channel.
2204 * Metadata channel is not cleared per se but we still need to
2205 * perform a rotation operation on it behind the scene.
2207 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2216 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2217 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2220 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2228 enum lttng_error_code
kernel_create_event_notifier_group_notification_fd(
2229 int *event_notifier_group_notification_fd
)
2231 int local_fd
= -1, ret
;
2232 enum lttng_error_code error_code_ret
;
2234 LTTNG_ASSERT(event_notifier_group_notification_fd
);
2236 ret
= kernctl_create_event_notifier_group_notification_fd(
2237 kernel_tracer_event_notifier_group_fd
);
2239 PERROR("Failed to create kernel event notifier group notification file descriptor");
2240 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2246 /* Prevent fd duplication after execlp(). */
2247 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2249 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2251 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2255 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2257 error_code_ret
= LTTNG_OK
;
2258 *event_notifier_group_notification_fd
= local_fd
;
2262 if (local_fd
>= 0) {
2263 ret
= close(local_fd
);
2265 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2270 return error_code_ret
;
2273 enum lttng_error_code
kernel_destroy_event_notifier_group_notification_fd(
2274 int event_notifier_group_notification_fd
)
2276 enum lttng_error_code ret_code
= LTTNG_OK
;
2278 DBG("Closing event notifier group notification file descriptor: fd = %d",
2279 event_notifier_group_notification_fd
);
2280 if (event_notifier_group_notification_fd
>= 0) {
2281 const int ret
= close(event_notifier_group_notification_fd
);
2283 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2284 event_notifier_group_notification_fd
);
2292 unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2294 const struct lttng_condition
*condition
=
2295 lttng_trigger_get_const_condition(trigger
);
2297 return lttng_condition_hash(condition
);
2301 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2303 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2304 const struct lttng_trigger
*trigger
= (lttng_trigger
*) key
;
2306 event_notifier_rule
= caa_container_of(node
,
2307 const struct ltt_kernel_event_notifier_rule
, ht_node
);
2309 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2312 static enum lttng_error_code
kernel_create_event_notifier_rule(
2313 struct lttng_trigger
*trigger
,
2314 const struct lttng_credentials
*creds
, uint64_t token
)
2316 int err
, fd
, ret
= 0;
2317 enum lttng_error_code error_code_ret
;
2318 enum lttng_condition_status condition_status
;
2319 enum lttng_condition_type condition_type
;
2320 enum lttng_event_rule_type event_rule_type
;
2321 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2322 struct lttng_kernel_abi_event_notifier kernel_event_notifier
= {};
2323 unsigned int capture_bytecode_count
= 0, i
;
2324 const struct lttng_condition
*condition
= NULL
;
2325 const struct lttng_event_rule
*event_rule
= NULL
;
2326 enum lttng_condition_status cond_status
;
2328 LTTNG_ASSERT(trigger
);
2330 condition
= lttng_trigger_get_const_condition(trigger
);
2331 LTTNG_ASSERT(condition
);
2333 condition_type
= lttng_condition_get_type(condition
);
2334 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2336 /* Does not acquire a reference. */
2337 condition_status
= lttng_condition_event_rule_matches_get_rule(
2338 condition
, &event_rule
);
2339 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2340 LTTNG_ASSERT(event_rule
);
2342 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2343 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2345 error_code_ret
= trace_kernel_create_event_notifier_rule(trigger
, token
,
2346 lttng_condition_event_rule_matches_get_error_counter_index(
2348 &event_notifier_rule
);
2349 if (error_code_ret
!= LTTNG_OK
) {
2353 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(
2354 event_rule
, &kernel_event_notifier
);
2355 if (error_code_ret
!= LTTNG_OK
) {
2359 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2360 kernel_event_notifier
.error_counter_idx
=
2361 lttng_condition_event_rule_matches_get_error_counter_index(
2364 fd
= kernctl_create_event_notifier(
2365 kernel_tracer_event_notifier_group_fd
,
2366 &kernel_event_notifier
);
2370 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2373 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2374 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2377 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2378 kernel_event_notifier
.event
.name
);
2379 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2382 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2383 fd
, kernel_event_notifier
.event
.name
);
2384 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2389 event_notifier_rule
->fd
= fd
;
2390 /* Prevent fd duplication after execlp(). */
2391 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2393 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2395 error_code_ret
= LTTNG_ERR_FATAL
;
2396 goto set_cloexec_error
;
2399 if (event_notifier_rule
->filter
) {
2400 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2404 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2407 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2414 if (lttng_event_rule_get_type(event_rule
) ==
2415 LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
) {
2416 ret
= userspace_probe_event_rule_add_callsites(
2417 event_rule
, creds
, event_notifier_rule
->fd
);
2419 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2420 goto add_callsite_error
;
2424 /* Set the capture bytecode if any. */
2425 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2426 condition
, &capture_bytecode_count
);
2427 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2429 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2430 const struct lttng_bytecode
*capture_bytecode
=
2431 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2434 if (capture_bytecode
== NULL
) {
2435 ERR("Unexpected NULL capture bytecode on condition");
2436 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2440 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2442 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2443 event_notifier_rule
->fd
);
2444 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2449 err
= kernctl_enable(event_notifier_rule
->fd
);
2453 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2456 PERROR("enable kernel event notifier");
2457 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2463 /* Add trigger to kernel token mapping in the hash table. */
2465 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
, hash_trigger(trigger
),
2466 &event_notifier_rule
->ht_node
);
2469 DBG("Created kernel event notifier: name = '%s', fd = %d",
2470 kernel_event_notifier
.event
.name
,
2471 event_notifier_rule
->fd
);
2481 const int close_ret
= close(event_notifier_rule
->fd
);
2484 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2485 event_notifier_rule
->fd
);
2489 free(event_notifier_rule
);
2491 return error_code_ret
;
2494 enum lttng_error_code
kernel_register_event_notifier(
2495 struct lttng_trigger
*trigger
,
2496 const struct lttng_credentials
*cmd_creds
)
2498 enum lttng_error_code ret
;
2499 enum lttng_condition_status status
;
2500 enum lttng_domain_type domain_type
;
2501 const struct lttng_event_rule
*event_rule
;
2502 const struct lttng_condition
*const condition
=
2503 lttng_trigger_get_const_condition(trigger
);
2504 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2506 LTTNG_ASSERT(condition
);
2508 /* Does not acquire a reference to the event rule. */
2509 status
= lttng_condition_event_rule_matches_get_rule(
2510 condition
, &event_rule
);
2511 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2513 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2514 LTTNG_ASSERT(domain_type
== LTTNG_DOMAIN_KERNEL
);
2516 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2517 if (ret
!= LTTNG_OK
) {
2518 ERR("Failed to create kernel event notifier rule");
2524 enum lttng_error_code
kernel_unregister_event_notifier(
2525 const struct lttng_trigger
*trigger
)
2527 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2528 struct cds_lfht_node
*node
;
2529 struct cds_lfht_iter iter
;
2530 enum lttng_error_code error_code_ret
;
2535 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2536 hash_trigger(trigger
), match_trigger
, trigger
, &iter
);
2538 node
= cds_lfht_iter_get_node(&iter
);
2540 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2544 token_event_rule_element
= caa_container_of(node
,
2545 struct ltt_kernel_event_notifier_rule
, ht_node
);
2547 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2549 error_code_ret
= LTTNG_ERR_FATAL
;
2553 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2554 error_code_ret
= LTTNG_OK
;
2559 return error_code_ret
;
2562 int kernel_get_notification_fd(void)
2564 return kernel_tracer_event_notifier_group_notification_fd
;