2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "bin/lttng-sessiond/tracker.h"
9 #include "common/tracker.h"
10 #include "common/utils.h"
11 #include "lttng/lttng-error.h"
12 #include "lttng/tracker.h"
20 #include <sys/types.h>
22 #include <common/common.h>
23 #include <common/trace-chunk.h>
24 #include <common/kernel-ctl/kernel-ctl.h>
25 #include <common/kernel-ctl/kernel-ioctl.h>
26 #include <common/sessiond-comm/sessiond-comm.h>
28 #include "lttng-sessiond.h"
29 #include "lttng-syscall.h"
32 #include "kernel-consumer.h"
33 #include "kern-modules.h"
39 * Key used to reference a channel between the sessiond and the consumer. This
40 * is only read and updated with the session_list lock held.
42 static uint64_t next_kernel_channel_key
;
44 static const char *module_proc_lttng
= "/proc/lttng";
46 static int kernel_tracer_fd
= -1;
48 #include <lttng/userspace-probe.h>
49 #include <lttng/userspace-probe-internal.h>
51 * Add context on a kernel channel.
53 * Assumes the ownership of ctx.
55 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
56 struct ltt_kernel_context
*ctx
)
63 DBG("Adding context to channel %s", chan
->channel
->name
);
64 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
68 /* Exists but not available for this kernel */
69 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
72 /* If EEXIST, we just ignore the error */
76 PERROR("add context ioctl");
77 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
84 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
89 trace_kernel_destroy_context(ctx
);
95 * Create a new kernel session, register it to the kernel tracer and add it to
96 * the session daemon session.
98 int kernel_create_session(struct ltt_session
*session
)
101 struct ltt_kernel_session
*lks
;
105 /* Allocate data structure */
106 lks
= trace_kernel_create_session();
112 /* Kernel tracer session creation */
113 ret
= kernctl_create_session(kernel_tracer_fd
);
115 PERROR("ioctl kernel create session");
120 /* Prevent fd duplication after execlp() */
121 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
123 PERROR("fcntl session fd");
126 lks
->id
= session
->id
;
127 lks
->consumer_fds_sent
= 0;
128 session
->kernel_session
= lks
;
130 DBG("Kernel session created (fd: %d)", lks
->fd
);
133 * This is necessary since the creation time is present in the session
134 * name when it is generated.
136 if (session
->has_auto_generated_name
) {
137 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
139 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
142 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
143 session
->id
, session
->name
);
146 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
148 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
149 session
->id
, session
->name
);
156 trace_kernel_destroy_session(lks
);
157 trace_kernel_free_session(lks
);
163 * Create a kernel channel, register it to the kernel tracer and add it to the
166 int kernel_create_channel(struct ltt_kernel_session
*session
,
167 struct lttng_channel
*chan
)
170 struct ltt_kernel_channel
*lkc
;
175 /* Allocate kernel channel */
176 lkc
= trace_kernel_create_channel(chan
);
181 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
182 chan
->name
, lkc
->channel
->attr
.overwrite
,
183 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
184 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
185 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
187 /* Kernel tracer channel creation */
188 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
190 PERROR("ioctl kernel create channel");
194 /* Setup the channel fd */
196 /* Prevent fd duplication after execlp() */
197 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
199 PERROR("fcntl session fd");
202 /* Add channel to session */
203 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
204 session
->channel_count
++;
205 lkc
->session
= session
;
206 lkc
->key
= ++next_kernel_channel_key
;
208 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
209 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
222 * Compute the offset of the instrumentation byte in the binary based on the
223 * function probe location using the ELF lookup method.
225 * Returns 0 on success and set the offset out parameter to the offset of the
227 * Returns -1 on error
230 int extract_userspace_probe_offset_function_elf(
231 const struct lttng_userspace_probe_location
*probe_location
,
232 struct ltt_kernel_session
*session
, uint64_t *offset
)
236 const char *symbol
= NULL
;
237 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
238 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
240 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
241 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
243 lookup
= lttng_userspace_probe_location_get_lookup_method(
251 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
253 assert(lookup_method_type
==
254 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
256 symbol
= lttng_userspace_probe_location_function_get_function_name(
263 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
269 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
270 session
->gid
, offset
);
272 DBG("userspace probe offset calculation failed for "
273 "function %s", symbol
);
277 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
283 * Compute the offsets of the instrumentation bytes in the binary based on the
284 * tracepoint probe location using the SDT lookup method. This function
285 * allocates the offsets buffer, the caller must free it.
287 * Returns 0 on success and set the offset out parameter to the offsets of the
289 * Returns -1 on error.
292 int extract_userspace_probe_offset_tracepoint_sdt(
293 const struct lttng_userspace_probe_location
*probe_location
,
294 struct ltt_kernel_session
*session
, uint64_t **offsets
,
295 uint32_t *offsets_count
)
297 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
298 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
299 const char *probe_name
= NULL
, *provider_name
= NULL
;
303 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
304 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
306 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
313 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
315 assert(lookup_method_type
==
316 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
319 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
326 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
328 if (!provider_name
) {
333 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
339 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
340 session
->uid
, session
->gid
, offsets
, offsets_count
);
342 DBG("userspace probe offset calculation failed for sdt "
343 "probe %s:%s", provider_name
, probe_name
);
347 if (*offsets_count
== 0) {
348 DBG("no userspace probe offset found");
352 DBG("%u userspace probe SDT offsets found for %s:%s at:",
353 *offsets_count
, provider_name
, probe_name
);
354 for (i
= 0; i
< *offsets_count
; i
++) {
355 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
362 * Extract the offsets of the instrumentation point for the different lookup
366 int userspace_probe_add_callsites(struct lttng_event
*ev
,
367 struct ltt_kernel_session
*session
, int fd
)
369 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
370 enum lttng_userspace_probe_location_lookup_method_type type
;
371 const struct lttng_userspace_probe_location
*location
= NULL
;
375 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
377 location
= lttng_event_get_userspace_probe_location(ev
);
383 lttng_userspace_probe_location_get_lookup_method(location
);
384 if (!lookup_method
) {
389 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
391 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
393 struct lttng_kernel_event_callsite callsite
;
396 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
398 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
402 callsite
.u
.uprobe
.offset
= offset
;
403 ret
= kernctl_add_callsite(fd
, &callsite
);
405 WARN("Adding callsite to userspace probe "
406 "event %s failed.", ev
->name
);
407 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
412 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
415 uint64_t *offsets
= NULL
;
416 uint32_t offsets_count
;
417 struct lttng_kernel_event_callsite callsite
;
420 * This call allocates the offsets buffer. This buffer must be freed
423 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
424 &offsets
, &offsets_count
);
426 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
429 for (i
= 0; i
< offsets_count
; i
++) {
430 callsite
.u
.uprobe
.offset
= offsets
[i
];
431 ret
= kernctl_add_callsite(fd
, &callsite
);
433 WARN("Adding callsite to userspace probe "
434 "event %s failed.", ev
->name
);
435 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
444 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
452 * Create a kernel event, enable it to the kernel tracer and add it to the
453 * channel event list of the kernel session.
454 * We own filter_expression and filter.
456 int kernel_create_event(struct lttng_event
*ev
,
457 struct ltt_kernel_channel
*channel
,
458 char *filter_expression
,
459 struct lttng_filter_bytecode
*filter
)
462 enum lttng_error_code ret
;
463 struct ltt_kernel_event
*event
;
468 /* We pass ownership of filter_expression and filter */
469 ret
= trace_kernel_create_event(ev
, filter_expression
,
471 if (ret
!= LTTNG_OK
) {
475 fd
= kernctl_create_event(channel
->fd
, event
->event
);
479 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
482 WARN("Event type not implemented");
483 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
486 WARN("Event %s not found!", ev
->name
);
487 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
490 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
491 PERROR("create event ioctl");
496 event
->type
= ev
->type
;
498 /* Prevent fd duplication after execlp() */
499 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
501 PERROR("fcntl session fd");
505 err
= kernctl_filter(event
->fd
, filter
);
509 ret
= LTTNG_ERR_FILTER_NOMEM
;
512 ret
= LTTNG_ERR_FILTER_INVAL
;
519 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
520 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
522 goto add_callsite_error
;
526 err
= kernctl_enable(event
->fd
);
530 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
533 PERROR("enable kernel event");
534 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
540 /* Add event to event list */
541 cds_list_add(&event
->list
, &channel
->events_list
.head
);
542 channel
->event_count
++;
544 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
554 closeret
= close(event
->fd
);
556 PERROR("close event fd");
566 * Disable a kernel channel.
568 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
574 ret
= kernctl_disable(chan
->fd
);
576 PERROR("disable chan ioctl");
581 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
582 chan
->channel
->name
, chan
->fd
, chan
->key
);
591 * Enable a kernel channel.
593 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
599 ret
= kernctl_enable(chan
->fd
);
600 if (ret
< 0 && ret
!= -EEXIST
) {
601 PERROR("Enable kernel chan");
606 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
607 chan
->channel
->name
, chan
->fd
, chan
->key
);
616 * Enable a kernel event.
618 int kernel_enable_event(struct ltt_kernel_event
*event
)
624 ret
= kernctl_enable(event
->fd
);
628 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
631 PERROR("enable kernel event");
638 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
647 * Disable a kernel event.
649 int kernel_disable_event(struct ltt_kernel_event
*event
)
655 ret
= kernctl_disable(event
->fd
);
659 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
662 PERROR("disable kernel event");
669 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
678 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
679 struct ltt_kernel_session
*session
,
680 enum lttng_process_attr process_attr
)
682 switch (process_attr
) {
683 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
684 return session
->tracker_pid
;
685 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
686 return session
->tracker_vpid
;
687 case LTTNG_PROCESS_ATTR_USER_ID
:
688 return session
->tracker_uid
;
689 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
690 return session
->tracker_vuid
;
691 case LTTNG_PROCESS_ATTR_GROUP_ID
:
692 return session
->tracker_gid
;
693 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
694 return session
->tracker_vgid
;
700 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
701 struct ltt_kernel_session
*session
,
702 enum lttng_process_attr process_attr
)
704 return (const struct process_attr_tracker
*)
705 _kernel_get_process_attr_tracker(session
, process_attr
);
708 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
709 struct ltt_kernel_session
*session
,
710 enum lttng_process_attr process_attr
,
711 enum lttng_tracking_policy policy
)
714 enum lttng_error_code ret_code
= LTTNG_OK
;
715 struct process_attr_tracker
*tracker
=
716 _kernel_get_process_attr_tracker(session
, process_attr
);
717 enum lttng_tracking_policy previous_policy
;
720 ret_code
= LTTNG_ERR_INVALID
;
724 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
725 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
727 ret_code
= LTTNG_ERR_UNK
;
731 if (previous_policy
== policy
) {
736 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
737 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
739 * Maintain a special case for the process ID process
740 * attribute tracker as it was the only supported
741 * attribute prior to 2.12.
743 ret
= kernctl_track_pid(session
->fd
, -1);
745 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
748 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
749 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
751 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
753 * Maintain a special case for the process ID process
754 * attribute tracker as it was the only supported
755 * attribute prior to 2.12.
757 ret
= kernctl_untrack_pid(session
->fd
, -1);
759 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
765 /* kern-ctl error handling */
771 ret_code
= LTTNG_ERR_INVALID
;
774 ret_code
= LTTNG_ERR_NOMEM
;
777 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
780 ret_code
= LTTNG_ERR_UNK
;
787 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
788 struct ltt_kernel_session
*session
,
789 enum lttng_process_attr process_attr
,
790 const struct process_attr_value
*value
)
792 int ret
, integral_value
;
793 enum lttng_error_code ret_code
;
794 struct process_attr_tracker
*tracker
;
795 enum process_attr_tracker_status status
;
798 * Convert process attribute tracker value to the integral
799 * representation required by the kern-ctl API.
801 switch (process_attr
) {
802 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
803 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
804 integral_value
= (int) value
->value
.pid
;
806 case LTTNG_PROCESS_ATTR_USER_ID
:
807 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
808 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
811 ret_code
= utils_user_id_from_name(
812 value
->value
.user_name
, &uid
);
813 if (ret_code
!= LTTNG_OK
) {
816 integral_value
= (int) uid
;
818 integral_value
= (int) value
->value
.uid
;
821 case LTTNG_PROCESS_ATTR_GROUP_ID
:
822 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
823 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
826 ret_code
= utils_group_id_from_name(
827 value
->value
.group_name
, &gid
);
828 if (ret_code
!= LTTNG_OK
) {
831 integral_value
= (int) gid
;
833 integral_value
= (int) value
->value
.gid
;
837 ret_code
= LTTNG_ERR_INVALID
;
841 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
843 ret_code
= LTTNG_ERR_INVALID
;
847 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
848 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
850 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
851 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
853 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
854 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
856 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
858 ret_code
= LTTNG_ERR_UNK
;
864 DBG("Kernel track %s %d for session id %" PRIu64
,
865 lttng_process_attr_to_string(process_attr
),
866 integral_value
, session
->id
);
867 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
869 * Maintain a special case for the process ID process attribute
870 * tracker as it was the only supported attribute prior to 2.12.
872 ret
= kernctl_track_pid(session
->fd
, integral_value
);
874 ret
= kernctl_track_id(
875 session
->fd
, process_attr
, integral_value
);
882 kernel_wait_quiescent();
884 /* kern-ctl error handling */
890 ret_code
= LTTNG_ERR_INVALID
;
893 ret_code
= LTTNG_ERR_NOMEM
;
896 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
899 ret_code
= LTTNG_ERR_UNK
;
903 /* Attempt to remove the value from the tracker. */
904 status
= process_attr_tracker_inclusion_set_remove_value(
906 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
907 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
908 lttng_process_attr_to_string(process_attr
),
915 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
916 struct ltt_kernel_session
*session
,
917 enum lttng_process_attr process_attr
,
918 const struct process_attr_value
*value
)
920 int ret
, integral_value
;
921 enum lttng_error_code ret_code
;
922 struct process_attr_tracker
*tracker
;
923 enum process_attr_tracker_status status
;
926 * Convert process attribute tracker value to the integral
927 * representation required by the kern-ctl API.
929 switch (process_attr
) {
930 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
931 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
932 integral_value
= (int) value
->value
.pid
;
934 case LTTNG_PROCESS_ATTR_USER_ID
:
935 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
936 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
939 ret_code
= utils_user_id_from_name(
940 value
->value
.user_name
, &uid
);
941 if (ret_code
!= LTTNG_OK
) {
944 integral_value
= (int) uid
;
946 integral_value
= (int) value
->value
.uid
;
949 case LTTNG_PROCESS_ATTR_GROUP_ID
:
950 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
951 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
954 ret_code
= utils_group_id_from_name(
955 value
->value
.group_name
, &gid
);
956 if (ret_code
!= LTTNG_OK
) {
959 integral_value
= (int) gid
;
961 integral_value
= (int) value
->value
.gid
;
965 ret_code
= LTTNG_ERR_INVALID
;
969 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
971 ret_code
= LTTNG_ERR_INVALID
;
975 status
= process_attr_tracker_inclusion_set_remove_value(
977 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
979 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
980 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
982 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
983 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
985 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
987 ret_code
= LTTNG_ERR_UNK
;
993 DBG("Kernel track %s %d for session id %" PRIu64
,
994 lttng_process_attr_to_string(process_attr
),
995 integral_value
, session
->id
);
996 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
998 * Maintain a special case for the process ID process attribute
999 * tracker as it was the only supported attribute prior to 2.12.
1001 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1003 ret
= kernctl_untrack_id(
1004 session
->fd
, process_attr
, integral_value
);
1007 ret_code
= LTTNG_OK
;
1010 kernel_wait_quiescent();
1012 /* kern-ctl error handling */
1015 ret_code
= LTTNG_OK
;
1018 ret_code
= LTTNG_ERR_INVALID
;
1021 ret_code
= LTTNG_ERR_NOMEM
;
1024 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1027 ret_code
= LTTNG_ERR_UNK
;
1031 /* Attempt to add the value to the tracker. */
1032 status
= process_attr_tracker_inclusion_set_add_value(
1034 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1035 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1036 lttng_process_attr_to_string(process_attr
),
1044 * Create kernel metadata, open from the kernel tracer and add it to the
1047 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1050 struct ltt_kernel_metadata
*lkm
= NULL
;
1054 /* Allocate kernel metadata */
1055 lkm
= trace_kernel_create_metadata();
1060 /* Kernel tracer metadata creation */
1061 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1067 lkm
->key
= ++next_kernel_channel_key
;
1068 /* Prevent fd duplication after execlp() */
1069 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1071 PERROR("fcntl session fd");
1074 session
->metadata
= lkm
;
1076 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1081 trace_kernel_destroy_metadata(lkm
);
1087 * Start tracing session.
1089 int kernel_start_session(struct ltt_kernel_session
*session
)
1095 ret
= kernctl_start_session(session
->fd
);
1097 PERROR("ioctl start session");
1101 DBG("Kernel session started");
1110 * Make a kernel wait to make sure in-flight probe have completed.
1112 void kernel_wait_quiescent(void)
1115 int fd
= kernel_tracer_fd
;
1117 DBG("Kernel quiescent wait on %d", fd
);
1119 ret
= kernctl_wait_quiescent(fd
);
1121 PERROR("wait quiescent ioctl");
1122 ERR("Kernel quiescent wait failed");
1127 * Force flush buffer of metadata.
1129 int kernel_metadata_flush_buffer(int fd
)
1133 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1135 ret
= kernctl_buffer_flush(fd
);
1137 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1144 * Force flush buffer for channel.
1146 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1149 struct ltt_kernel_stream
*stream
;
1153 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1155 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1156 DBG("Flushing channel stream %d", stream
->fd
);
1157 ret
= kernctl_buffer_flush(stream
->fd
);
1160 ERR("Fail to flush buffer for stream %d (ret: %d)",
1169 * Stop tracing session.
1171 int kernel_stop_session(struct ltt_kernel_session
*session
)
1177 ret
= kernctl_stop_session(session
->fd
);
1182 DBG("Kernel session stopped");
1191 * Open stream of channel, register it to the kernel tracer and add it
1192 * to the stream list of the channel.
1194 * Note: given that the streams may appear in random order wrt CPU
1195 * number (e.g. cpu hotplug), the index value of the stream number in
1196 * the stream name is not necessarily linked to the CPU number.
1198 * Return the number of created stream. Else, a negative value.
1200 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1203 struct ltt_kernel_stream
*lks
;
1207 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1208 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1209 channel
->stream_count
);
1219 /* Prevent fd duplication after execlp() */
1220 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1222 PERROR("fcntl session fd");
1225 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1226 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1228 /* Add stream to channel stream list */
1229 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1230 channel
->stream_count
++;
1232 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1236 return channel
->stream_count
;
1243 * Open the metadata stream and set it to the kernel session.
1245 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1251 ret
= kernctl_create_stream(session
->metadata
->fd
);
1253 PERROR("kernel create metadata stream");
1257 DBG("Kernel metadata stream created (fd: %d)", ret
);
1258 session
->metadata_stream_fd
= ret
;
1259 /* Prevent fd duplication after execlp() */
1260 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1262 PERROR("fcntl session fd");
1272 * Get the event list from the kernel tracer and return the number of elements.
1274 ssize_t
kernel_list_events(struct lttng_event
**events
)
1278 size_t nbmem
, count
= 0;
1280 struct lttng_event
*elist
;
1284 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1286 PERROR("kernel tracepoint list");
1290 fp
= fdopen(fd
, "r");
1292 PERROR("kernel tracepoint list fdopen");
1297 * Init memory size counter
1298 * See kernel-ctl.h for explanation of this value
1300 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1301 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1302 if (elist
== NULL
) {
1303 PERROR("alloc list events");
1308 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1309 if (count
>= nbmem
) {
1310 struct lttng_event
*new_elist
;
1313 new_nbmem
= nbmem
<< 1;
1314 DBG("Reallocating event list from %zu to %zu bytes",
1316 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1317 if (new_elist
== NULL
) {
1318 PERROR("realloc list events");
1324 /* Zero the new memory */
1325 memset(new_elist
+ nbmem
, 0,
1326 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1330 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1331 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1332 elist
[count
].enabled
= -1;
1338 DBG("Kernel list events done (%zu events)", count
);
1340 ret
= fclose(fp
); /* closes both fp and fd */
1356 * Get kernel version and validate it.
1358 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1359 struct lttng_kernel_tracer_abi_version
*abi_version
)
1363 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1365 ERR("Failed to retrieve the lttng-modules version");
1369 /* Validate version */
1370 if (version
->major
!= VERSION_MAJOR
) {
1371 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1372 version
->major
, VERSION_MAJOR
);
1375 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1377 ERR("Failed to retrieve lttng-modules ABI version");
1380 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1381 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1382 abi_version
->major
, abi_version
->minor
,
1383 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1386 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1387 version
->major
, version
->minor
,
1388 abi_version
->major
, abi_version
->minor
);
1395 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1400 * Kernel work-arounds called at the start of sessiond main().
1402 int init_kernel_workarounds(void)
1408 * boot_id needs to be read once before being used concurrently
1409 * to deal with a Linux kernel race. A fix is proposed for
1410 * upstream, but the work-around is needed for older kernels.
1412 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1419 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1421 /* Ignore error, we don't really care */
1433 * Teardown of a kernel session, keeping data required by destroy notifiers.
1435 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1437 struct lttng_trace_chunk
*trace_chunk
;
1439 if (ksess
== NULL
) {
1440 DBG3("No kernel session when tearing down session");
1444 DBG("Tearing down kernel session");
1445 trace_chunk
= ksess
->current_trace_chunk
;
1448 * Destroy channels on the consumer if at least one FD has been sent and we
1449 * are in no output mode because the streams are in *no* monitor mode so we
1450 * have to send a command to clean them up or else they leaked.
1452 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1454 struct consumer_socket
*socket
;
1455 struct lttng_ht_iter iter
;
1457 /* For each consumer socket. */
1459 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1460 socket
, node
.node
) {
1461 struct ltt_kernel_channel
*chan
;
1463 /* For each channel, ask the consumer to destroy it. */
1464 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1465 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1467 /* Consumer is probably dead. Use next socket. */
1475 /* Close any relayd session */
1476 consumer_output_send_destroy_relayd(ksess
->consumer
);
1478 trace_kernel_destroy_session(ksess
);
1479 lttng_trace_chunk_put(trace_chunk
);
1482 /* Teardown of data required by destroy notifiers. */
1483 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1485 if (ksess
== NULL
) {
1488 trace_kernel_free_session(ksess
);
1492 * Destroy a kernel channel object. It does not do anything on the tracer side.
1494 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1496 struct ltt_kernel_session
*ksess
= NULL
;
1499 assert(kchan
->channel
);
1501 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1503 /* Update channel count of associated session. */
1504 if (kchan
->session
) {
1505 /* Keep pointer reference so we can update it after the destroy. */
1506 ksess
= kchan
->session
;
1509 trace_kernel_destroy_channel(kchan
);
1512 * At this point the kernel channel is not visible anymore. This is safe
1513 * since in order to work on a visible kernel session, the tracing session
1514 * lock (ltt_session.lock) MUST be acquired.
1517 ksess
->channel_count
--;
1522 * Take a snapshot for a given kernel session.
1524 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1526 enum lttng_error_code
kernel_snapshot_record(
1527 struct ltt_kernel_session
*ksess
,
1528 const struct consumer_output
*output
, int wait
,
1529 uint64_t nb_packets_per_stream
)
1531 int err
, ret
, saved_metadata_fd
;
1532 enum lttng_error_code status
= LTTNG_OK
;
1533 struct consumer_socket
*socket
;
1534 struct lttng_ht_iter iter
;
1535 struct ltt_kernel_metadata
*saved_metadata
;
1536 char *trace_path
= NULL
;
1537 size_t consumer_path_offset
= 0;
1540 assert(ksess
->consumer
);
1543 DBG("Kernel snapshot record started");
1545 /* Save current metadata since the following calls will change it. */
1546 saved_metadata
= ksess
->metadata
;
1547 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1551 ret
= kernel_open_metadata(ksess
);
1553 status
= LTTNG_ERR_KERN_META_FAIL
;
1557 ret
= kernel_open_metadata_stream(ksess
);
1559 status
= LTTNG_ERR_KERN_META_FAIL
;
1560 goto error_open_stream
;
1563 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1564 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1566 status
= LTTNG_ERR_INVALID
;
1569 /* Send metadata to consumer and snapshot everything. */
1570 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1571 socket
, node
.node
) {
1572 struct ltt_kernel_channel
*chan
;
1574 pthread_mutex_lock(socket
->lock
);
1575 /* This stream must not be monitored by the consumer. */
1576 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1577 pthread_mutex_unlock(socket
->lock
);
1579 status
= LTTNG_ERR_KERN_META_FAIL
;
1580 goto error_consumer
;
1583 /* For each channel, ask the consumer to snapshot it. */
1584 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1585 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1586 ksess
->uid
, ksess
->gid
,
1587 &trace_path
[consumer_path_offset
], wait
,
1588 nb_packets_per_stream
);
1589 if (status
!= LTTNG_OK
) {
1590 (void) kernel_consumer_destroy_metadata(socket
,
1592 goto error_consumer
;
1596 /* Snapshot metadata, */
1597 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1598 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1600 if (status
!= LTTNG_OK
) {
1601 goto error_consumer
;
1605 * The metadata snapshot is done, ask the consumer to destroy it since
1606 * it's not monitored on the consumer side.
1608 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1612 /* Close newly opened metadata stream. It's now on the consumer side. */
1613 err
= close(ksess
->metadata_stream_fd
);
1615 PERROR("close snapshot kernel");
1619 trace_kernel_destroy_metadata(ksess
->metadata
);
1621 /* Restore metadata state.*/
1622 ksess
->metadata
= saved_metadata
;
1623 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1630 * Get the syscall mask array from the kernel tracer.
1632 * Return 0 on success else a negative value. In both case, syscall_mask should
1635 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1637 assert(syscall_mask
);
1640 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1644 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1647 * Return 1 on success, 0 when feature is not supported, negative value in case
1650 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1652 int ret
= 0; // Not supported by default
1653 struct lttng_kernel_tracer_abi_version abi
;
1655 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1657 ERR("Failed to retrieve lttng-modules ABI version");
1662 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1664 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1676 * Check for the support of the packet sequence number via abi version number.
1678 * Return 1 on success, 0 when feature is not supported, negative value in case
1681 int kernel_supports_ring_buffer_packet_sequence_number(void)
1683 int ret
= 0; // Not supported by default
1684 struct lttng_kernel_tracer_abi_version abi
;
1686 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1688 ERR("Failed to retrieve lttng-modules ABI version");
1693 * Packet sequence number was introduced in LTTng 2.8,
1694 * lttng-modules ABI 2.1.
1696 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1708 * Rotate a kernel session.
1710 * Return LTTNG_OK on success or else an LTTng error code.
1712 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1715 enum lttng_error_code status
= LTTNG_OK
;
1716 struct consumer_socket
*socket
;
1717 struct lttng_ht_iter iter
;
1718 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1721 assert(ksess
->consumer
);
1723 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1724 session
->name
, session
->id
);
1729 * Note that this loop will end after one iteration given that there is
1730 * only one kernel consumer.
1732 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1733 socket
, node
.node
) {
1734 struct ltt_kernel_channel
*chan
;
1736 /* For each channel, ask the consumer to rotate it. */
1737 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1738 DBG("Rotate kernel channel %" PRIu64
", session %s",
1739 chan
->key
, session
->name
);
1740 ret
= consumer_rotate_channel(socket
, chan
->key
,
1741 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1742 /* is_metadata_channel */ false);
1744 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1750 * Rotate the metadata channel.
1752 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1753 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1754 /* is_metadata_channel */ true);
1756 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1766 enum lttng_error_code
kernel_create_channel_subdirectories(
1767 const struct ltt_kernel_session
*ksess
)
1769 enum lttng_error_code ret
= LTTNG_OK
;
1770 enum lttng_trace_chunk_status chunk_status
;
1773 assert(ksess
->current_trace_chunk
);
1776 * Create the index subdirectory which will take care
1777 * of implicitly creating the channel's path.
1779 chunk_status
= lttng_trace_chunk_create_subdirectory(
1780 ksess
->current_trace_chunk
,
1781 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1782 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1783 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1792 * Setup necessary data for kernel tracer action.
1795 int init_kernel_tracer(void)
1798 bool is_root
= !getuid();
1800 /* Modprobe lttng kernel modules */
1801 ret
= modprobe_lttng_control();
1806 /* Open debugfs lttng */
1807 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1808 if (kernel_tracer_fd
< 0) {
1809 DBG("Failed to open %s", module_proc_lttng
);
1813 /* Validate kernel version */
1814 ret
= kernel_validate_version(&kernel_tracer_version
,
1815 &kernel_tracer_abi_version
);
1820 ret
= modprobe_lttng_data();
1825 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1831 WARN("Kernel tracer does not support buffer monitoring. "
1832 "The monitoring timer of channels in the kernel domain "
1833 "will be set to 0 (disabled).");
1836 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1838 ret
= syscall_init_table(kernel_tracer_fd
);
1840 ERR("Unable to populate syscall table. Syscall tracing won't "
1841 "work for this session daemon.");
1846 modprobe_remove_lttng_control();
1847 ret
= close(kernel_tracer_fd
);
1851 kernel_tracer_fd
= -1;
1852 return LTTNG_ERR_KERN_VERSION
;
1855 ret
= close(kernel_tracer_fd
);
1861 modprobe_remove_lttng_control();
1864 WARN("No kernel tracer available");
1865 kernel_tracer_fd
= -1;
1867 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1869 return LTTNG_ERR_KERN_NA
;
1874 void cleanup_kernel_tracer(void)
1878 DBG2("Closing kernel fd");
1879 if (kernel_tracer_fd
>= 0) {
1880 ret
= close(kernel_tracer_fd
);
1884 kernel_tracer_fd
= -1;
1886 DBG("Unloading kernel modules");
1887 modprobe_remove_lttng_all();
1888 free(syscall_table
);
1892 bool kernel_tracer_is_initialized(void)
1894 return kernel_tracer_fd
>= 0;
1898 * Clear a kernel session.
1900 * Return LTTNG_OK on success or else an LTTng error code.
1902 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
1905 enum lttng_error_code status
= LTTNG_OK
;
1906 struct consumer_socket
*socket
;
1907 struct lttng_ht_iter iter
;
1908 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1911 assert(ksess
->consumer
);
1913 DBG("Clear kernel session %s (session %" PRIu64
")",
1914 session
->name
, session
->id
);
1918 if (ksess
->active
) {
1919 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
1920 status
= LTTNG_ERR_FATAL
;
1925 * Note that this loop will end after one iteration given that there is
1926 * only one kernel consumer.
1928 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1929 socket
, node
.node
) {
1930 struct ltt_kernel_channel
*chan
;
1932 /* For each channel, ask the consumer to clear it. */
1933 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1934 DBG("Clear kernel channel %" PRIu64
", session %s",
1935 chan
->key
, session
->name
);
1936 ret
= consumer_clear_channel(socket
, chan
->key
);
1942 if (!ksess
->metadata
) {
1944 * Nothing to do for the metadata.
1945 * This is a snapshot session.
1946 * The metadata is genererated on the fly.
1952 * Clear the metadata channel.
1953 * Metadata channel is not cleared per se but we still need to
1954 * perform a rotation operation on it behind the scene.
1956 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
1965 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
1966 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
1969 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;