2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
17 #include <common/common.h>
18 #include <common/hashtable/utils.h>
19 #include <common/trace-chunk.h>
20 #include <common/kernel-ctl/kernel-ctl.h>
21 #include <common/kernel-ctl/kernel-ioctl.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/tracker.h>
24 #include <common/utils.h>
25 #include <lttng/event.h>
26 #include <lttng/lttng-error.h>
27 #include <lttng/tracker.h>
29 #include <lttng/userspace-probe.h>
30 #include <lttng/userspace-probe-internal.h>
31 #include <lttng/condition/event-rule.h>
32 #include <lttng/condition/event-rule-internal.h>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.h>
35 #include <lttng/event-rule/uprobe-internal.h>
37 #include "lttng-sessiond.h"
38 #include "lttng-syscall.h"
39 #include "condition-internal.h"
42 #include "kernel-consumer.h"
43 #include "kern-modules.h"
48 #include "notification-thread-commands.h"
51 * Key used to reference a channel between the sessiond and the consumer. This
52 * is only read and updated with the session_list lock held.
54 static uint64_t next_kernel_channel_key
;
56 static const char *module_proc_lttng
= "/proc/lttng";
58 static int kernel_tracer_fd
= -1;
59 static int kernel_tracer_event_notifier_group_fd
= -1;
60 static int kernel_tracer_event_notifier_group_notification_fd
= -1;
61 static struct cds_lfht
*kernel_token_to_event_notifier_rule_ht
;
64 * Add context on a kernel channel.
66 * Assumes the ownership of ctx.
68 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
69 struct ltt_kernel_context
*ctx
)
76 DBG("Adding context to channel %s", chan
->channel
->name
);
77 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
81 /* Exists but not available for this kernel */
82 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
85 /* If EEXIST, we just ignore the error */
89 PERROR("add context ioctl");
90 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
97 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
102 trace_kernel_destroy_context(ctx
);
108 * Create a new kernel session, register it to the kernel tracer and add it to
109 * the session daemon session.
111 int kernel_create_session(struct ltt_session
*session
)
114 struct ltt_kernel_session
*lks
;
118 /* Allocate data structure */
119 lks
= trace_kernel_create_session();
125 /* Kernel tracer session creation */
126 ret
= kernctl_create_session(kernel_tracer_fd
);
128 PERROR("ioctl kernel create session");
133 /* Prevent fd duplication after execlp() */
134 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
136 PERROR("fcntl session fd");
139 lks
->id
= session
->id
;
140 lks
->consumer_fds_sent
= 0;
141 session
->kernel_session
= lks
;
143 DBG("Kernel session created (fd: %d)", lks
->fd
);
146 * This is necessary since the creation time is present in the session
147 * name when it is generated.
149 if (session
->has_auto_generated_name
) {
150 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
152 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
155 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
156 session
->id
, session
->name
);
159 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
161 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
162 session
->id
, session
->name
);
169 trace_kernel_destroy_session(lks
);
170 trace_kernel_free_session(lks
);
176 * Create a kernel channel, register it to the kernel tracer and add it to the
179 int kernel_create_channel(struct ltt_kernel_session
*session
,
180 struct lttng_channel
*chan
)
183 struct ltt_kernel_channel
*lkc
;
188 /* Allocate kernel channel */
189 lkc
= trace_kernel_create_channel(chan
);
194 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
195 chan
->name
, lkc
->channel
->attr
.overwrite
,
196 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
197 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
198 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
200 /* Kernel tracer channel creation */
201 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
203 PERROR("ioctl kernel create channel");
207 /* Setup the channel fd */
209 /* Prevent fd duplication after execlp() */
210 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
212 PERROR("fcntl session fd");
215 /* Add channel to session */
216 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
217 session
->channel_count
++;
218 lkc
->session
= session
;
219 lkc
->key
= ++next_kernel_channel_key
;
221 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
222 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
235 * Create a kernel event notifier group, register it to the kernel tracer and
236 * add it to the kernel session.
238 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
243 assert(event_notifier_group_fd
);
245 /* Kernel event notifier group creation. */
246 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
248 PERROR("Failed to create kernel event notifier group");
255 /* Prevent fd duplication after execlp(). */
256 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
258 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
263 DBG("Created kernel event notifier group: fd = %d", local_fd
);
264 *event_notifier_group_fd
= local_fd
;
269 ret
= close(local_fd
);
271 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
280 * Compute the offset of the instrumentation byte in the binary based on the
281 * function probe location using the ELF lookup method.
283 * Returns 0 on success and set the offset out parameter to the offset of the
285 * Returns -1 on error
288 int extract_userspace_probe_offset_function_elf(
289 const struct lttng_userspace_probe_location
*probe_location
,
290 uid_t uid
, gid_t gid
, uint64_t *offset
)
294 const char *symbol
= NULL
;
295 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
296 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
298 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
299 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
301 lookup
= lttng_userspace_probe_location_get_lookup_method(
309 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
311 assert(lookup_method_type
==
312 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
314 symbol
= lttng_userspace_probe_location_function_get_function_name(
321 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
327 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
329 DBG("userspace probe offset calculation failed for "
330 "function %s", symbol
);
334 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
340 * Compute the offsets of the instrumentation bytes in the binary based on the
341 * tracepoint probe location using the SDT lookup method. This function
342 * allocates the offsets buffer, the caller must free it.
344 * Returns 0 on success and set the offset out parameter to the offsets of the
346 * Returns -1 on error.
349 int extract_userspace_probe_offset_tracepoint_sdt(
350 const struct lttng_userspace_probe_location
*probe_location
,
351 uid_t uid
, gid_t gid
, uint64_t **offsets
,
352 uint32_t *offsets_count
)
354 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
355 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
356 const char *probe_name
= NULL
, *provider_name
= NULL
;
360 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
361 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
363 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
370 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
372 assert(lookup_method_type
==
373 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
376 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
383 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
385 if (!provider_name
) {
390 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
396 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
397 uid
, gid
, offsets
, offsets_count
);
399 DBG("userspace probe offset calculation failed for sdt "
400 "probe %s:%s", provider_name
, probe_name
);
404 if (*offsets_count
== 0) {
405 DBG("no userspace probe offset found");
409 DBG("%u userspace probe SDT offsets found for %s:%s at:",
410 *offsets_count
, provider_name
, probe_name
);
411 for (i
= 0; i
< *offsets_count
; i
++) {
412 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
419 int userspace_probe_add_callsite(
420 const struct lttng_userspace_probe_location
*location
,
421 uid_t uid
, gid_t gid
, int fd
)
423 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
424 enum lttng_userspace_probe_location_lookup_method_type type
;
427 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
428 if (!lookup_method
) {
433 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
435 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
437 struct lttng_kernel_event_callsite callsite
;
440 ret
= extract_userspace_probe_offset_function_elf(location
,
443 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
447 callsite
.u
.uprobe
.offset
= offset
;
448 ret
= kernctl_add_callsite(fd
, &callsite
);
450 WARN("Failed to add callsite to ELF userspace probe.");
451 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
456 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
459 uint64_t *offsets
= NULL
;
460 uint32_t offsets_count
;
461 struct lttng_kernel_event_callsite callsite
;
464 * This call allocates the offsets buffer. This buffer must be freed
467 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
468 uid
, gid
, &offsets
, &offsets_count
);
470 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
473 for (i
= 0; i
< offsets_count
; i
++) {
474 callsite
.u
.uprobe
.offset
= offsets
[i
];
475 ret
= kernctl_add_callsite(fd
, &callsite
);
477 WARN("Failed to add callsite to SDT userspace probe");
478 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
487 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
495 * Extract the offsets of the instrumentation point for the different lookup
499 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
500 struct ltt_kernel_session
*session
, int fd
)
503 const struct lttng_userspace_probe_location
*location
= NULL
;
506 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
508 location
= lttng_event_get_userspace_probe_location(ev
);
514 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
517 WARN("Failed to add callsite to userspace probe event '%s'",
526 * Extract the offsets of the instrumentation point for the different look-up
529 static int userspace_probe_event_rule_add_callsites(
530 const struct lttng_event_rule
*rule
,
531 const struct lttng_credentials
*creds
,
535 enum lttng_event_rule_status status
;
536 enum lttng_event_rule_type event_rule_type
;
537 const struct lttng_userspace_probe_location
*location
= NULL
;
542 event_rule_type
= lttng_event_rule_get_type(rule
);
543 assert(event_rule_type
== LTTNG_EVENT_RULE_TYPE_UPROBE
);
545 status
= lttng_event_rule_uprobe_get_location(rule
, &location
);
546 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
551 ret
= userspace_probe_add_callsite(location
,
552 lttng_credentials_get_uid(creds
),
553 lttng_credentials_get_gid(creds
), fd
);
555 WARN("Failed to add callsite to user space probe object: fd = %d",
564 * Create a kernel event, enable it to the kernel tracer and add it to the
565 * channel event list of the kernel session.
566 * We own filter_expression and filter.
568 int kernel_create_event(struct lttng_event
*ev
,
569 struct ltt_kernel_channel
*channel
,
570 char *filter_expression
,
571 struct lttng_filter_bytecode
*filter
)
574 enum lttng_error_code ret
;
575 struct ltt_kernel_event
*event
;
580 /* We pass ownership of filter_expression and filter */
581 ret
= trace_kernel_create_event(ev
, filter_expression
,
583 if (ret
!= LTTNG_OK
) {
587 fd
= kernctl_create_event(channel
->fd
, event
->event
);
591 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
594 WARN("Event type not implemented");
595 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
598 WARN("Event %s not found!", ev
->name
);
599 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
602 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
603 PERROR("create event ioctl");
608 event
->type
= ev
->type
;
610 /* Prevent fd duplication after execlp() */
611 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
613 PERROR("fcntl session fd");
617 err
= kernctl_filter(event
->fd
, filter
);
621 ret
= LTTNG_ERR_FILTER_NOMEM
;
624 ret
= LTTNG_ERR_FILTER_INVAL
;
631 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
632 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
635 goto add_callsite_error
;
639 err
= kernctl_enable(event
->fd
);
643 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
646 PERROR("enable kernel event");
647 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
653 /* Add event to event list */
654 cds_list_add(&event
->list
, &channel
->events_list
.head
);
655 channel
->event_count
++;
657 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
667 closeret
= close(event
->fd
);
669 PERROR("close event fd");
679 * Disable a kernel channel.
681 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
687 ret
= kernctl_disable(chan
->fd
);
689 PERROR("disable chan ioctl");
694 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
695 chan
->channel
->name
, chan
->fd
, chan
->key
);
704 * Enable a kernel channel.
706 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
712 ret
= kernctl_enable(chan
->fd
);
713 if (ret
< 0 && ret
!= -EEXIST
) {
714 PERROR("Enable kernel chan");
719 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
720 chan
->channel
->name
, chan
->fd
, chan
->key
);
729 * Enable a kernel event.
731 int kernel_enable_event(struct ltt_kernel_event
*event
)
737 ret
= kernctl_enable(event
->fd
);
741 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
744 PERROR("enable kernel event");
751 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
760 * Disable a kernel event.
762 int kernel_disable_event(struct ltt_kernel_event
*event
)
768 ret
= kernctl_disable(event
->fd
);
772 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
775 PERROR("disable kernel event");
782 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
791 * Disable a kernel event notifier.
794 int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
801 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
804 ret
= kernctl_disable(event
->fd
);
808 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
811 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
812 event
->fd
, event
->token
);
819 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
,
820 event
->fd
, event
->token
);
827 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
828 struct ltt_kernel_session
*session
,
829 enum lttng_process_attr process_attr
)
831 switch (process_attr
) {
832 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
833 return session
->tracker_pid
;
834 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
835 return session
->tracker_vpid
;
836 case LTTNG_PROCESS_ATTR_USER_ID
:
837 return session
->tracker_uid
;
838 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
839 return session
->tracker_vuid
;
840 case LTTNG_PROCESS_ATTR_GROUP_ID
:
841 return session
->tracker_gid
;
842 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
843 return session
->tracker_vgid
;
849 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
850 struct ltt_kernel_session
*session
,
851 enum lttng_process_attr process_attr
)
853 return (const struct process_attr_tracker
*)
854 _kernel_get_process_attr_tracker(session
, process_attr
);
857 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
858 struct ltt_kernel_session
*session
,
859 enum lttng_process_attr process_attr
,
860 enum lttng_tracking_policy policy
)
863 enum lttng_error_code ret_code
= LTTNG_OK
;
864 struct process_attr_tracker
*tracker
=
865 _kernel_get_process_attr_tracker(session
, process_attr
);
866 enum lttng_tracking_policy previous_policy
;
869 ret_code
= LTTNG_ERR_INVALID
;
873 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
874 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
876 ret_code
= LTTNG_ERR_UNK
;
880 if (previous_policy
== policy
) {
885 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
886 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
888 * Maintain a special case for the process ID process
889 * attribute tracker as it was the only supported
890 * attribute prior to 2.12.
892 ret
= kernctl_track_pid(session
->fd
, -1);
894 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
897 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
898 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
900 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
902 * Maintain a special case for the process ID process
903 * attribute tracker as it was the only supported
904 * attribute prior to 2.12.
906 ret
= kernctl_untrack_pid(session
->fd
, -1);
908 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
914 /* kern-ctl error handling */
920 ret_code
= LTTNG_ERR_INVALID
;
923 ret_code
= LTTNG_ERR_NOMEM
;
926 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
929 ret_code
= LTTNG_ERR_UNK
;
936 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
937 struct ltt_kernel_session
*session
,
938 enum lttng_process_attr process_attr
,
939 const struct process_attr_value
*value
)
941 int ret
, integral_value
;
942 enum lttng_error_code ret_code
;
943 struct process_attr_tracker
*tracker
;
944 enum process_attr_tracker_status status
;
947 * Convert process attribute tracker value to the integral
948 * representation required by the kern-ctl API.
950 switch (process_attr
) {
951 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
952 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
953 integral_value
= (int) value
->value
.pid
;
955 case LTTNG_PROCESS_ATTR_USER_ID
:
956 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
957 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
960 ret_code
= utils_user_id_from_name(
961 value
->value
.user_name
, &uid
);
962 if (ret_code
!= LTTNG_OK
) {
965 integral_value
= (int) uid
;
967 integral_value
= (int) value
->value
.uid
;
970 case LTTNG_PROCESS_ATTR_GROUP_ID
:
971 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
972 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
975 ret_code
= utils_group_id_from_name(
976 value
->value
.group_name
, &gid
);
977 if (ret_code
!= LTTNG_OK
) {
980 integral_value
= (int) gid
;
982 integral_value
= (int) value
->value
.gid
;
986 ret_code
= LTTNG_ERR_INVALID
;
990 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
992 ret_code
= LTTNG_ERR_INVALID
;
996 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
997 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
999 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
1000 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1002 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1003 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1005 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1007 ret_code
= LTTNG_ERR_UNK
;
1013 DBG("Kernel track %s %d for session id %" PRIu64
,
1014 lttng_process_attr_to_string(process_attr
),
1015 integral_value
, session
->id
);
1016 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1018 * Maintain a special case for the process ID process attribute
1019 * tracker as it was the only supported attribute prior to 2.12.
1021 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1023 ret
= kernctl_track_id(
1024 session
->fd
, process_attr
, integral_value
);
1027 ret_code
= LTTNG_OK
;
1031 kernel_wait_quiescent();
1033 /* kern-ctl error handling */
1036 ret_code
= LTTNG_OK
;
1039 ret_code
= LTTNG_ERR_INVALID
;
1042 ret_code
= LTTNG_ERR_NOMEM
;
1045 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1048 ret_code
= LTTNG_ERR_UNK
;
1052 /* Attempt to remove the value from the tracker. */
1053 status
= process_attr_tracker_inclusion_set_remove_value(
1055 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1056 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1057 lttng_process_attr_to_string(process_attr
),
1064 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1065 struct ltt_kernel_session
*session
,
1066 enum lttng_process_attr process_attr
,
1067 const struct process_attr_value
*value
)
1069 int ret
, integral_value
;
1070 enum lttng_error_code ret_code
;
1071 struct process_attr_tracker
*tracker
;
1072 enum process_attr_tracker_status status
;
1075 * Convert process attribute tracker value to the integral
1076 * representation required by the kern-ctl API.
1078 switch (process_attr
) {
1079 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1080 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1081 integral_value
= (int) value
->value
.pid
;
1083 case LTTNG_PROCESS_ATTR_USER_ID
:
1084 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1085 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1088 ret_code
= utils_user_id_from_name(
1089 value
->value
.user_name
, &uid
);
1090 if (ret_code
!= LTTNG_OK
) {
1093 integral_value
= (int) uid
;
1095 integral_value
= (int) value
->value
.uid
;
1098 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1099 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1100 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1103 ret_code
= utils_group_id_from_name(
1104 value
->value
.group_name
, &gid
);
1105 if (ret_code
!= LTTNG_OK
) {
1108 integral_value
= (int) gid
;
1110 integral_value
= (int) value
->value
.gid
;
1114 ret_code
= LTTNG_ERR_INVALID
;
1118 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1120 ret_code
= LTTNG_ERR_INVALID
;
1124 status
= process_attr_tracker_inclusion_set_remove_value(
1126 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1128 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1129 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1131 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1132 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1134 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1136 ret_code
= LTTNG_ERR_UNK
;
1142 DBG("Kernel track %s %d for session id %" PRIu64
,
1143 lttng_process_attr_to_string(process_attr
),
1144 integral_value
, session
->id
);
1145 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1147 * Maintain a special case for the process ID process attribute
1148 * tracker as it was the only supported attribute prior to 2.12.
1150 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1152 ret
= kernctl_untrack_id(
1153 session
->fd
, process_attr
, integral_value
);
1156 ret_code
= LTTNG_OK
;
1159 kernel_wait_quiescent();
1161 /* kern-ctl error handling */
1164 ret_code
= LTTNG_OK
;
1167 ret_code
= LTTNG_ERR_INVALID
;
1170 ret_code
= LTTNG_ERR_NOMEM
;
1173 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1176 ret_code
= LTTNG_ERR_UNK
;
1180 /* Attempt to add the value to the tracker. */
1181 status
= process_attr_tracker_inclusion_set_add_value(
1183 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1184 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1185 lttng_process_attr_to_string(process_attr
),
1193 * Create kernel metadata, open from the kernel tracer and add it to the
1196 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1199 struct ltt_kernel_metadata
*lkm
= NULL
;
1203 /* Allocate kernel metadata */
1204 lkm
= trace_kernel_create_metadata();
1209 /* Kernel tracer metadata creation */
1210 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1216 lkm
->key
= ++next_kernel_channel_key
;
1217 /* Prevent fd duplication after execlp() */
1218 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1220 PERROR("fcntl session fd");
1223 session
->metadata
= lkm
;
1225 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1230 trace_kernel_destroy_metadata(lkm
);
1236 * Start tracing session.
1238 int kernel_start_session(struct ltt_kernel_session
*session
)
1244 ret
= kernctl_start_session(session
->fd
);
1246 PERROR("ioctl start session");
1250 DBG("Kernel session started");
1259 * Make a kernel wait to make sure in-flight probe have completed.
1261 void kernel_wait_quiescent(void)
1264 int fd
= kernel_tracer_fd
;
1266 DBG("Kernel quiescent wait on %d", fd
);
1268 ret
= kernctl_wait_quiescent(fd
);
1270 PERROR("wait quiescent ioctl");
1271 ERR("Kernel quiescent wait failed");
1276 * Force flush buffer of metadata.
1278 int kernel_metadata_flush_buffer(int fd
)
1282 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1284 ret
= kernctl_buffer_flush(fd
);
1286 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1293 * Force flush buffer for channel.
1295 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1298 struct ltt_kernel_stream
*stream
;
1302 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1304 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1305 DBG("Flushing channel stream %d", stream
->fd
);
1306 ret
= kernctl_buffer_flush(stream
->fd
);
1309 ERR("Fail to flush buffer for stream %d (ret: %d)",
1318 * Stop tracing session.
1320 int kernel_stop_session(struct ltt_kernel_session
*session
)
1326 ret
= kernctl_stop_session(session
->fd
);
1331 DBG("Kernel session stopped");
1340 * Open stream of channel, register it to the kernel tracer and add it
1341 * to the stream list of the channel.
1343 * Note: given that the streams may appear in random order wrt CPU
1344 * number (e.g. cpu hotplug), the index value of the stream number in
1345 * the stream name is not necessarily linked to the CPU number.
1347 * Return the number of created stream. Else, a negative value.
1349 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1352 struct ltt_kernel_stream
*lks
;
1356 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1357 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1358 channel
->stream_count
);
1368 /* Prevent fd duplication after execlp() */
1369 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1371 PERROR("fcntl session fd");
1374 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1375 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1377 /* Add stream to channel stream list */
1378 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1379 channel
->stream_count
++;
1381 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1385 return channel
->stream_count
;
1392 * Open the metadata stream and set it to the kernel session.
1394 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1400 ret
= kernctl_create_stream(session
->metadata
->fd
);
1402 PERROR("kernel create metadata stream");
1406 DBG("Kernel metadata stream created (fd: %d)", ret
);
1407 session
->metadata_stream_fd
= ret
;
1408 /* Prevent fd duplication after execlp() */
1409 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1411 PERROR("fcntl session fd");
1421 * Get the event list from the kernel tracer and return the number of elements.
1423 ssize_t
kernel_list_events(struct lttng_event
**events
)
1427 size_t nbmem
, count
= 0;
1429 struct lttng_event
*elist
;
1433 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1435 PERROR("kernel tracepoint list");
1439 fp
= fdopen(fd
, "r");
1441 PERROR("kernel tracepoint list fdopen");
1446 * Init memory size counter
1447 * See kernel-ctl.h for explanation of this value
1449 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1450 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1451 if (elist
== NULL
) {
1452 PERROR("alloc list events");
1457 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1458 if (count
>= nbmem
) {
1459 struct lttng_event
*new_elist
;
1462 new_nbmem
= nbmem
<< 1;
1463 DBG("Reallocating event list from %zu to %zu bytes",
1465 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1466 if (new_elist
== NULL
) {
1467 PERROR("realloc list events");
1473 /* Zero the new memory */
1474 memset(new_elist
+ nbmem
, 0,
1475 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1479 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1480 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1481 elist
[count
].enabled
= -1;
1487 DBG("Kernel list events done (%zu events)", count
);
1489 ret
= fclose(fp
); /* closes both fp and fd */
1505 * Get kernel version and validate it.
1507 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1508 struct lttng_kernel_tracer_abi_version
*abi_version
)
1512 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1514 ERR("Failed to retrieve the lttng-modules version");
1518 /* Validate version */
1519 if (version
->major
!= VERSION_MAJOR
) {
1520 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1521 version
->major
, VERSION_MAJOR
);
1524 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1526 ERR("Failed to retrieve lttng-modules ABI version");
1529 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1530 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1531 abi_version
->major
, abi_version
->minor
,
1532 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1535 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1536 version
->major
, version
->minor
,
1537 abi_version
->major
, abi_version
->minor
);
1544 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1549 * Kernel work-arounds called at the start of sessiond main().
1551 int init_kernel_workarounds(void)
1557 * boot_id needs to be read once before being used concurrently
1558 * to deal with a Linux kernel race. A fix is proposed for
1559 * upstream, but the work-around is needed for older kernels.
1561 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1568 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1570 /* Ignore error, we don't really care */
1582 * Teardown of a kernel session, keeping data required by destroy notifiers.
1584 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1586 struct lttng_trace_chunk
*trace_chunk
;
1588 if (ksess
== NULL
) {
1589 DBG3("No kernel session when tearing down session");
1593 DBG("Tearing down kernel session");
1594 trace_chunk
= ksess
->current_trace_chunk
;
1597 * Destroy channels on the consumer if at least one FD has been sent and we
1598 * are in no output mode because the streams are in *no* monitor mode so we
1599 * have to send a command to clean them up or else they leaked.
1601 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1603 struct consumer_socket
*socket
;
1604 struct lttng_ht_iter iter
;
1606 /* For each consumer socket. */
1608 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1609 socket
, node
.node
) {
1610 struct ltt_kernel_channel
*chan
;
1612 /* For each channel, ask the consumer to destroy it. */
1613 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1614 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1616 /* Consumer is probably dead. Use next socket. */
1624 /* Close any relayd session */
1625 consumer_output_send_destroy_relayd(ksess
->consumer
);
1627 trace_kernel_destroy_session(ksess
);
1628 lttng_trace_chunk_put(trace_chunk
);
1631 /* Teardown of data required by destroy notifiers. */
1632 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1634 if (ksess
== NULL
) {
1637 trace_kernel_free_session(ksess
);
1641 * Destroy a kernel channel object. It does not do anything on the tracer side.
1643 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1645 struct ltt_kernel_session
*ksess
= NULL
;
1648 assert(kchan
->channel
);
1650 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1652 /* Update channel count of associated session. */
1653 if (kchan
->session
) {
1654 /* Keep pointer reference so we can update it after the destroy. */
1655 ksess
= kchan
->session
;
1658 trace_kernel_destroy_channel(kchan
);
1661 * At this point the kernel channel is not visible anymore. This is safe
1662 * since in order to work on a visible kernel session, the tracing session
1663 * lock (ltt_session.lock) MUST be acquired.
1666 ksess
->channel_count
--;
1671 * Take a snapshot for a given kernel session.
1673 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1675 enum lttng_error_code
kernel_snapshot_record(
1676 struct ltt_kernel_session
*ksess
,
1677 const struct consumer_output
*output
, int wait
,
1678 uint64_t nb_packets_per_stream
)
1680 int err
, ret
, saved_metadata_fd
;
1681 enum lttng_error_code status
= LTTNG_OK
;
1682 struct consumer_socket
*socket
;
1683 struct lttng_ht_iter iter
;
1684 struct ltt_kernel_metadata
*saved_metadata
;
1685 char *trace_path
= NULL
;
1686 size_t consumer_path_offset
= 0;
1689 assert(ksess
->consumer
);
1692 DBG("Kernel snapshot record started");
1694 /* Save current metadata since the following calls will change it. */
1695 saved_metadata
= ksess
->metadata
;
1696 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1700 ret
= kernel_open_metadata(ksess
);
1702 status
= LTTNG_ERR_KERN_META_FAIL
;
1706 ret
= kernel_open_metadata_stream(ksess
);
1708 status
= LTTNG_ERR_KERN_META_FAIL
;
1709 goto error_open_stream
;
1712 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1713 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1715 status
= LTTNG_ERR_INVALID
;
1718 /* Send metadata to consumer and snapshot everything. */
1719 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1720 socket
, node
.node
) {
1721 struct ltt_kernel_channel
*chan
;
1723 pthread_mutex_lock(socket
->lock
);
1724 /* This stream must not be monitored by the consumer. */
1725 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1726 pthread_mutex_unlock(socket
->lock
);
1728 status
= LTTNG_ERR_KERN_META_FAIL
;
1729 goto error_consumer
;
1732 /* For each channel, ask the consumer to snapshot it. */
1733 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1734 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1735 ksess
->uid
, ksess
->gid
,
1736 &trace_path
[consumer_path_offset
], wait
,
1737 nb_packets_per_stream
);
1738 if (status
!= LTTNG_OK
) {
1739 (void) kernel_consumer_destroy_metadata(socket
,
1741 goto error_consumer
;
1745 /* Snapshot metadata, */
1746 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1747 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1749 if (status
!= LTTNG_OK
) {
1750 goto error_consumer
;
1754 * The metadata snapshot is done, ask the consumer to destroy it since
1755 * it's not monitored on the consumer side.
1757 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1761 /* Close newly opened metadata stream. It's now on the consumer side. */
1762 err
= close(ksess
->metadata_stream_fd
);
1764 PERROR("close snapshot kernel");
1768 trace_kernel_destroy_metadata(ksess
->metadata
);
1770 /* Restore metadata state.*/
1771 ksess
->metadata
= saved_metadata
;
1772 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1779 * Get the syscall mask array from the kernel tracer.
1781 * Return 0 on success else a negative value. In both case, syscall_mask should
1784 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1786 assert(syscall_mask
);
1789 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1793 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1796 struct lttng_kernel_tracer_abi_version abi
;
1798 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1800 ERR("Failed to retrieve lttng-modules ABI version");
1804 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1810 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1813 * Return 1 on success, 0 when feature is not supported, negative value in case
1816 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1819 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1821 return kernel_tracer_abi_greater_or_equal(2, 3);
1825 * Check for the support of the packet sequence number via abi version number.
1827 * Return 1 on success, 0 when feature is not supported, negative value in case
1830 int kernel_supports_ring_buffer_packet_sequence_number(void)
1833 * Packet sequence number was introduced in LTTng 2.8,
1834 * lttng-modules ABI 2.1.
1836 return kernel_tracer_abi_greater_or_equal(2, 1);
1840 * Check for the support of event notifiers via abi version number.
1842 * Return 1 on success, 0 when feature is not supported, negative value in case
1845 int kernel_supports_event_notifiers(void)
1848 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1850 return kernel_tracer_abi_greater_or_equal(2, 6);
1854 * Rotate a kernel session.
1856 * Return LTTNG_OK on success or else an LTTng error code.
1858 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1861 enum lttng_error_code status
= LTTNG_OK
;
1862 struct consumer_socket
*socket
;
1863 struct lttng_ht_iter iter
;
1864 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1867 assert(ksess
->consumer
);
1869 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1870 session
->name
, session
->id
);
1875 * Note that this loop will end after one iteration given that there is
1876 * only one kernel consumer.
1878 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1879 socket
, node
.node
) {
1880 struct ltt_kernel_channel
*chan
;
1882 /* For each channel, ask the consumer to rotate it. */
1883 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1884 DBG("Rotate kernel channel %" PRIu64
", session %s",
1885 chan
->key
, session
->name
);
1886 ret
= consumer_rotate_channel(socket
, chan
->key
,
1887 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1888 /* is_metadata_channel */ false);
1890 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1896 * Rotate the metadata channel.
1898 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1899 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1900 /* is_metadata_channel */ true);
1902 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1912 enum lttng_error_code
kernel_create_channel_subdirectories(
1913 const struct ltt_kernel_session
*ksess
)
1915 enum lttng_error_code ret
= LTTNG_OK
;
1916 enum lttng_trace_chunk_status chunk_status
;
1919 assert(ksess
->current_trace_chunk
);
1922 * Create the index subdirectory which will take care
1923 * of implicitly creating the channel's path.
1925 chunk_status
= lttng_trace_chunk_create_subdirectory(
1926 ksess
->current_trace_chunk
,
1927 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1928 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1929 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1938 * Setup necessary data for kernel tracer action.
1941 int init_kernel_tracer(void)
1944 bool is_root
= !getuid();
1946 /* Modprobe lttng kernel modules */
1947 ret
= modprobe_lttng_control();
1952 /* Open debugfs lttng */
1953 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1954 if (kernel_tracer_fd
< 0) {
1955 DBG("Failed to open %s", module_proc_lttng
);
1959 /* Validate kernel version */
1960 ret
= kernel_validate_version(&kernel_tracer_version
,
1961 &kernel_tracer_abi_version
);
1966 ret
= modprobe_lttng_data();
1971 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1976 WARN("Kernel tracer does not support buffer monitoring. "
1977 "The monitoring timer of channels in the kernel domain "
1978 "will be set to 0 (disabled).");
1981 ret
= kernel_supports_event_notifiers();
1983 ERR("Failed to check for kernel tracer event notifier support");
1986 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1988 /* This is not fatal. */
1989 WARN("Failed to create kernel event notifier group");
1990 kernel_tracer_event_notifier_group_fd
= -1;
1992 const enum lttng_error_code error_code_ret
=
1993 kernel_create_event_notifier_group_notification_fd(
1994 &kernel_tracer_event_notifier_group_notification_fd
);
1996 if (error_code_ret
!= LTTNG_OK
) {
2000 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
2001 DEFAULT_HT_SIZE
, 1, 0,
2002 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
2004 if (!kernel_token_to_event_notifier_rule_ht
) {
2005 goto error_token_ht
;
2009 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2010 kernel_tracer_fd
, kernel_tracer_event_notifier_group_fd
,
2011 kernel_tracer_event_notifier_group_notification_fd
);
2013 ret
= syscall_init_table(kernel_tracer_fd
);
2015 ERR("Unable to populate syscall table. Syscall tracing won't "
2016 "work for this session daemon.");
2022 modprobe_remove_lttng_control();
2023 ret
= close(kernel_tracer_fd
);
2025 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2028 kernel_tracer_fd
= -1;
2029 return LTTNG_ERR_KERN_VERSION
;
2033 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2035 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2036 kernel_tracer_event_notifier_group_notification_fd
);
2040 ret
= close(kernel_tracer_event_notifier_group_fd
);
2042 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2043 kernel_tracer_event_notifier_group_fd
);
2046 ret
= close(kernel_tracer_fd
);
2048 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
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
;
2066 void cleanup_kernel_tracer(void)
2068 DBG2("Closing kernel event notifier group notification file descriptor");
2069 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2070 int ret
= notification_thread_command_remove_tracer_event_source(
2071 notification_thread_handle
,
2072 kernel_tracer_event_notifier_group_notification_fd
);
2073 if (ret
!= LTTNG_OK
) {
2074 ERR("Failed to remove kernel event notifier notification from notification thread");
2077 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2079 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2080 kernel_tracer_event_notifier_group_notification_fd
);
2083 kernel_tracer_event_notifier_group_notification_fd
= -1;
2086 if (kernel_token_to_event_notifier_rule_ht
) {
2087 const int ret
= cds_lfht_destroy(
2088 kernel_token_to_event_notifier_rule_ht
, NULL
);
2092 DBG2("Closing kernel event notifier group file descriptor");
2093 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2094 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2097 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2098 kernel_tracer_event_notifier_group_fd
);
2101 kernel_tracer_event_notifier_group_fd
= -1;
2104 DBG2("Closing kernel fd");
2105 if (kernel_tracer_fd
>= 0) {
2106 const int ret
= close(kernel_tracer_fd
);
2109 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2113 kernel_tracer_fd
= -1;
2116 DBG("Unloading kernel modules");
2117 modprobe_remove_lttng_all();
2118 free(syscall_table
);
2122 bool kernel_tracer_is_initialized(void)
2124 return kernel_tracer_fd
>= 0;
2128 * Clear a kernel session.
2130 * Return LTTNG_OK on success or else an LTTng error code.
2132 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2135 enum lttng_error_code status
= LTTNG_OK
;
2136 struct consumer_socket
*socket
;
2137 struct lttng_ht_iter iter
;
2138 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2141 assert(ksess
->consumer
);
2143 DBG("Clear kernel session %s (session %" PRIu64
")",
2144 session
->name
, session
->id
);
2148 if (ksess
->active
) {
2149 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2150 status
= LTTNG_ERR_FATAL
;
2155 * Note that this loop will end after one iteration given that there is
2156 * only one kernel consumer.
2158 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2159 socket
, node
.node
) {
2160 struct ltt_kernel_channel
*chan
;
2162 /* For each channel, ask the consumer to clear it. */
2163 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2164 DBG("Clear kernel channel %" PRIu64
", session %s",
2165 chan
->key
, session
->name
);
2166 ret
= consumer_clear_channel(socket
, chan
->key
);
2172 if (!ksess
->metadata
) {
2174 * Nothing to do for the metadata.
2175 * This is a snapshot session.
2176 * The metadata is genererated on the fly.
2182 * Clear the metadata channel.
2183 * Metadata channel is not cleared per se but we still need to
2184 * perform a rotation operation on it behind the scene.
2186 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2195 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2196 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2199 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2207 enum lttng_error_code
kernel_create_event_notifier_group_notification_fd(
2208 int *event_notifier_group_notification_fd
)
2210 int local_fd
= -1, ret
;
2211 enum lttng_error_code error_code_ret
;
2213 assert(event_notifier_group_notification_fd
);
2215 ret
= kernctl_create_event_notifier_group_notification_fd(
2216 kernel_tracer_event_notifier_group_fd
);
2218 PERROR("Failed to create kernel event notifier group notification file descriptor");
2219 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2225 /* Prevent fd duplication after execlp(). */
2226 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2228 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2230 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2234 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2236 error_code_ret
= LTTNG_OK
;
2237 *event_notifier_group_notification_fd
= local_fd
;
2241 if (local_fd
>= 0) {
2242 ret
= close(local_fd
);
2244 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2249 return error_code_ret
;
2252 enum lttng_error_code
kernel_destroy_event_notifier_group_notification_fd(
2253 int event_notifier_group_notification_fd
)
2255 enum lttng_error_code ret_code
= LTTNG_OK
;
2257 DBG("Closing event notifier group notification file descriptor: fd = %d",
2258 event_notifier_group_notification_fd
);
2259 if (event_notifier_group_notification_fd
>= 0) {
2260 const int ret
= close(event_notifier_group_notification_fd
);
2262 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2263 event_notifier_group_notification_fd
);
2271 unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2273 const struct lttng_condition
*condition
=
2274 lttng_trigger_get_const_condition(trigger
);
2276 return lttng_condition_hash(condition
);
2280 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2282 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2283 const struct lttng_trigger
*trigger
= key
;
2285 event_notifier_rule
= caa_container_of(node
,
2286 const struct ltt_kernel_event_notifier_rule
, ht_node
);
2288 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2291 static enum lttng_error_code
kernel_create_event_notifier_rule(
2292 struct lttng_trigger
*trigger
,
2293 const struct lttng_credentials
*creds
, uint64_t token
)
2295 int err
, fd
, ret
= 0;
2296 enum lttng_error_code error_code_ret
;
2297 enum lttng_condition_status condition_status
;
2298 enum lttng_condition_type condition_type
;
2299 enum lttng_event_rule_type event_rule_type
;
2300 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2301 struct lttng_kernel_event_notifier kernel_event_notifier
= {};
2302 const struct lttng_condition
*condition
= NULL
;
2303 const struct lttng_event_rule
*event_rule
= NULL
;
2307 condition
= lttng_trigger_get_const_condition(trigger
);
2310 condition_type
= lttng_condition_get_type(condition
);
2311 assert(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2313 /* Does not acquire a reference. */
2314 condition_status
= lttng_condition_event_rule_get_rule(
2315 condition
, &event_rule
);
2316 assert(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2319 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2320 assert(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2322 error_code_ret
= trace_kernel_create_event_notifier_rule(trigger
, token
,
2323 &event_notifier_rule
);
2324 if (error_code_ret
!= LTTNG_OK
) {
2328 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(
2329 event_rule
, &kernel_event_notifier
);
2330 if (error_code_ret
!= LTTNG_OK
) {
2334 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2336 fd
= kernctl_create_event_notifier(
2337 kernel_tracer_event_notifier_group_fd
,
2338 &kernel_event_notifier
);
2342 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2345 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2346 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2349 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2350 kernel_event_notifier
.event
.name
);
2351 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2354 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2355 fd
, kernel_event_notifier
.event
.name
);
2356 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2361 event_notifier_rule
->fd
= fd
;
2362 /* Prevent fd duplication after execlp(). */
2363 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2365 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2367 error_code_ret
= LTTNG_ERR_FATAL
;
2368 goto set_cloexec_error
;
2371 if (event_notifier_rule
->filter
) {
2372 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2376 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2379 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2386 if (lttng_event_rule_get_type(event_rule
) ==
2387 LTTNG_EVENT_RULE_TYPE_UPROBE
) {
2388 ret
= userspace_probe_event_rule_add_callsites(
2389 event_rule
, creds
, event_notifier_rule
->fd
);
2391 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2392 goto add_callsite_error
;
2396 err
= kernctl_enable(event_notifier_rule
->fd
);
2400 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2403 PERROR("enable kernel event notifier");
2404 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2410 /* Add trigger to kernel token mapping in the hash table. */
2412 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
, hash_trigger(trigger
),
2413 &event_notifier_rule
->ht_node
);
2416 DBG("Created kernel event notifier: name = '%s', fd = %d",
2417 kernel_event_notifier
.event
.name
,
2418 event_notifier_rule
->fd
);
2427 const int close_ret
= close(event_notifier_rule
->fd
);
2430 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2431 event_notifier_rule
->fd
);
2435 free(event_notifier_rule
);
2437 return error_code_ret
;
2440 enum lttng_error_code
kernel_register_event_notifier(
2441 struct lttng_trigger
*trigger
,
2442 const struct lttng_credentials
*cmd_creds
)
2444 enum lttng_error_code ret
;
2445 enum lttng_condition_status status
;
2446 enum lttng_domain_type domain_type
;
2447 const struct lttng_event_rule
*event_rule
;
2448 const struct lttng_condition
*const condition
=
2449 lttng_trigger_get_const_condition(trigger
);
2450 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2454 /* Does not acquire a reference to the event rule. */
2455 status
= lttng_condition_event_rule_get_rule(
2456 condition
, &event_rule
);
2457 assert(status
== LTTNG_CONDITION_STATUS_OK
);
2459 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2460 assert(domain_type
== LTTNG_DOMAIN_KERNEL
);
2462 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2463 if (ret
!= LTTNG_OK
) {
2464 ERR("Failed to create kernel event notifier rule");
2470 enum lttng_error_code
kernel_unregister_event_notifier(
2471 const struct lttng_trigger
*trigger
)
2473 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2474 struct cds_lfht_node
*node
;
2475 struct cds_lfht_iter iter
;
2476 enum lttng_error_code error_code_ret
;
2481 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2482 hash_trigger(trigger
), match_trigger
, trigger
, &iter
);
2484 node
= cds_lfht_iter_get_node(&iter
);
2486 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2490 token_event_rule_element
= caa_container_of(node
,
2491 struct ltt_kernel_event_notifier_rule
, ht_node
);
2493 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2495 error_code_ret
= LTTNG_ERR_FATAL
;
2499 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2500 error_code_ret
= LTTNG_OK
;
2505 return error_code_ret
;
2508 int kernel_get_notification_fd(void)
2510 return kernel_tracer_event_notifier_group_notification_fd
;