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/trace-chunk.h>
19 #include <common/kernel-ctl/kernel-ctl.h>
20 #include <common/kernel-ctl/kernel-ioctl.h>
21 #include <common/sessiond-comm/sessiond-comm.h>
22 #include <common/tracker.h>
23 #include <common/utils.h>
24 #include <lttng/event.h>
25 #include <lttng/lttng-error.h>
26 #include <lttng/tracker.h>
28 #include "lttng-sessiond.h"
29 #include "lttng-syscall.h"
32 #include "kernel-consumer.h"
33 #include "kern-modules.h"
40 * Key used to reference a channel between the sessiond and the consumer. This
41 * is only read and updated with the session_list lock held.
43 static uint64_t next_kernel_channel_key
;
45 static const char *module_proc_lttng
= "/proc/lttng";
47 static int kernel_tracer_fd
= -1;
49 #include <lttng/userspace-probe.h>
50 #include <lttng/userspace-probe-internal.h>
52 * Add context on a kernel channel.
54 * Assumes the ownership of ctx.
56 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
57 struct ltt_kernel_context
*ctx
)
64 DBG("Adding context to channel %s", chan
->channel
->name
);
65 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
69 /* Exists but not available for this kernel */
70 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
73 /* If EEXIST, we just ignore the error */
77 PERROR("add context ioctl");
78 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
85 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
90 trace_kernel_destroy_context(ctx
);
96 * Create a new kernel session, register it to the kernel tracer and add it to
97 * the session daemon session.
99 int kernel_create_session(struct ltt_session
*session
)
102 struct ltt_kernel_session
*lks
;
106 /* Allocate data structure */
107 lks
= trace_kernel_create_session();
113 /* Kernel tracer session creation */
114 ret
= kernctl_create_session(kernel_tracer_fd
);
116 PERROR("ioctl kernel create session");
121 /* Prevent fd duplication after execlp() */
122 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
124 PERROR("fcntl session fd");
127 lks
->id
= session
->id
;
128 lks
->consumer_fds_sent
= 0;
129 session
->kernel_session
= lks
;
131 DBG("Kernel session created (fd: %d)", lks
->fd
);
134 * This is necessary since the creation time is present in the session
135 * name when it is generated.
137 if (session
->has_auto_generated_name
) {
138 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
140 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
143 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
144 session
->id
, session
->name
);
147 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
149 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
150 session
->id
, session
->name
);
157 trace_kernel_destroy_session(lks
);
158 trace_kernel_free_session(lks
);
164 * Create a kernel channel, register it to the kernel tracer and add it to the
167 int kernel_create_channel(struct ltt_kernel_session
*session
,
168 struct lttng_channel
*chan
)
171 struct ltt_kernel_channel
*lkc
;
176 /* Allocate kernel channel */
177 lkc
= trace_kernel_create_channel(chan
);
182 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
183 chan
->name
, lkc
->channel
->attr
.overwrite
,
184 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
185 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
186 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
188 /* Kernel tracer channel creation */
189 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
191 PERROR("ioctl kernel create channel");
195 /* Setup the channel fd */
197 /* Prevent fd duplication after execlp() */
198 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
200 PERROR("fcntl session fd");
203 /* Add channel to session */
204 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
205 session
->channel_count
++;
206 lkc
->session
= session
;
207 lkc
->key
= ++next_kernel_channel_key
;
209 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
210 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
223 * Compute the offset of the instrumentation byte in the binary based on the
224 * function probe location using the ELF lookup method.
226 * Returns 0 on success and set the offset out parameter to the offset of the
228 * Returns -1 on error
231 int extract_userspace_probe_offset_function_elf(
232 const struct lttng_userspace_probe_location
*probe_location
,
233 uid_t uid
, gid_t gid
, uint64_t *offset
)
237 const char *symbol
= NULL
;
238 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
239 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
241 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
242 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
244 lookup
= lttng_userspace_probe_location_get_lookup_method(
252 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
254 assert(lookup_method_type
==
255 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
257 symbol
= lttng_userspace_probe_location_function_get_function_name(
264 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
270 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, 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 uid_t uid
, gid_t gid
, 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 uid
, 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 int userspace_probe_add_callsite(
363 const struct lttng_userspace_probe_location
*location
,
364 uid_t uid
, gid_t gid
, int fd
)
366 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
367 enum lttng_userspace_probe_location_lookup_method_type type
;
370 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
371 if (!lookup_method
) {
376 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
378 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
380 struct lttng_kernel_event_callsite callsite
;
383 ret
= extract_userspace_probe_offset_function_elf(location
,
386 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
390 callsite
.u
.uprobe
.offset
= offset
;
391 ret
= kernctl_add_callsite(fd
, &callsite
);
393 WARN("Failed to add callsite to ELF userspace probe.");
394 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
399 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
402 uint64_t *offsets
= NULL
;
403 uint32_t offsets_count
;
404 struct lttng_kernel_event_callsite callsite
;
407 * This call allocates the offsets buffer. This buffer must be freed
410 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
411 uid
, gid
, &offsets
, &offsets_count
);
413 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
416 for (i
= 0; i
< offsets_count
; i
++) {
417 callsite
.u
.uprobe
.offset
= offsets
[i
];
418 ret
= kernctl_add_callsite(fd
, &callsite
);
420 WARN("Failed to add callsite to SDT userspace probe");
421 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
430 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
438 * Extract the offsets of the instrumentation point for the different lookup
442 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
443 struct ltt_kernel_session
*session
, int fd
)
446 const struct lttng_userspace_probe_location
*location
= NULL
;
449 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
451 location
= lttng_event_get_userspace_probe_location(ev
);
457 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
460 WARN("Failed to add callsite to userspace probe event '%s'",
469 * Create a kernel event, enable it to the kernel tracer and add it to the
470 * channel event list of the kernel session.
471 * We own filter_expression and filter.
473 int kernel_create_event(struct lttng_event
*ev
,
474 struct ltt_kernel_channel
*channel
,
475 char *filter_expression
,
476 struct lttng_filter_bytecode
*filter
)
479 enum lttng_error_code ret
;
480 struct ltt_kernel_event
*event
;
485 /* We pass ownership of filter_expression and filter */
486 ret
= trace_kernel_create_event(ev
, filter_expression
,
488 if (ret
!= LTTNG_OK
) {
492 fd
= kernctl_create_event(channel
->fd
, event
->event
);
496 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
499 WARN("Event type not implemented");
500 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
503 WARN("Event %s not found!", ev
->name
);
504 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
507 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
508 PERROR("create event ioctl");
513 event
->type
= ev
->type
;
515 /* Prevent fd duplication after execlp() */
516 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
518 PERROR("fcntl session fd");
522 err
= kernctl_filter(event
->fd
, filter
);
526 ret
= LTTNG_ERR_FILTER_NOMEM
;
529 ret
= LTTNG_ERR_FILTER_INVAL
;
536 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
537 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
540 goto add_callsite_error
;
544 err
= kernctl_enable(event
->fd
);
548 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
551 PERROR("enable kernel event");
552 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
558 /* Add event to event list */
559 cds_list_add(&event
->list
, &channel
->events_list
.head
);
560 channel
->event_count
++;
562 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
572 closeret
= close(event
->fd
);
574 PERROR("close event fd");
584 * Disable a kernel channel.
586 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
592 ret
= kernctl_disable(chan
->fd
);
594 PERROR("disable chan ioctl");
599 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
600 chan
->channel
->name
, chan
->fd
, chan
->key
);
609 * Enable a kernel channel.
611 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
617 ret
= kernctl_enable(chan
->fd
);
618 if (ret
< 0 && ret
!= -EEXIST
) {
619 PERROR("Enable kernel chan");
624 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
625 chan
->channel
->name
, chan
->fd
, chan
->key
);
634 * Enable a kernel event.
636 int kernel_enable_event(struct ltt_kernel_event
*event
)
642 ret
= kernctl_enable(event
->fd
);
646 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
649 PERROR("enable kernel event");
656 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
665 * Disable a kernel event.
667 int kernel_disable_event(struct ltt_kernel_event
*event
)
673 ret
= kernctl_disable(event
->fd
);
677 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
680 PERROR("disable kernel event");
687 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
696 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
697 struct ltt_kernel_session
*session
,
698 enum lttng_process_attr process_attr
)
700 switch (process_attr
) {
701 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
702 return session
->tracker_pid
;
703 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
704 return session
->tracker_vpid
;
705 case LTTNG_PROCESS_ATTR_USER_ID
:
706 return session
->tracker_uid
;
707 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
708 return session
->tracker_vuid
;
709 case LTTNG_PROCESS_ATTR_GROUP_ID
:
710 return session
->tracker_gid
;
711 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
712 return session
->tracker_vgid
;
718 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
719 struct ltt_kernel_session
*session
,
720 enum lttng_process_attr process_attr
)
722 return (const struct process_attr_tracker
*)
723 _kernel_get_process_attr_tracker(session
, process_attr
);
726 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
727 struct ltt_kernel_session
*session
,
728 enum lttng_process_attr process_attr
,
729 enum lttng_tracking_policy policy
)
732 enum lttng_error_code ret_code
= LTTNG_OK
;
733 struct process_attr_tracker
*tracker
=
734 _kernel_get_process_attr_tracker(session
, process_attr
);
735 enum lttng_tracking_policy previous_policy
;
738 ret_code
= LTTNG_ERR_INVALID
;
742 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
743 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
745 ret_code
= LTTNG_ERR_UNK
;
749 if (previous_policy
== policy
) {
754 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
755 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
757 * Maintain a special case for the process ID process
758 * attribute tracker as it was the only supported
759 * attribute prior to 2.12.
761 ret
= kernctl_track_pid(session
->fd
, -1);
763 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
766 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
767 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
769 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
771 * Maintain a special case for the process ID process
772 * attribute tracker as it was the only supported
773 * attribute prior to 2.12.
775 ret
= kernctl_untrack_pid(session
->fd
, -1);
777 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
783 /* kern-ctl error handling */
789 ret_code
= LTTNG_ERR_INVALID
;
792 ret_code
= LTTNG_ERR_NOMEM
;
795 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
798 ret_code
= LTTNG_ERR_UNK
;
805 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
806 struct ltt_kernel_session
*session
,
807 enum lttng_process_attr process_attr
,
808 const struct process_attr_value
*value
)
810 int ret
, integral_value
;
811 enum lttng_error_code ret_code
;
812 struct process_attr_tracker
*tracker
;
813 enum process_attr_tracker_status status
;
816 * Convert process attribute tracker value to the integral
817 * representation required by the kern-ctl API.
819 switch (process_attr
) {
820 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
821 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
822 integral_value
= (int) value
->value
.pid
;
824 case LTTNG_PROCESS_ATTR_USER_ID
:
825 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
826 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
829 ret_code
= utils_user_id_from_name(
830 value
->value
.user_name
, &uid
);
831 if (ret_code
!= LTTNG_OK
) {
834 integral_value
= (int) uid
;
836 integral_value
= (int) value
->value
.uid
;
839 case LTTNG_PROCESS_ATTR_GROUP_ID
:
840 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
841 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
844 ret_code
= utils_group_id_from_name(
845 value
->value
.group_name
, &gid
);
846 if (ret_code
!= LTTNG_OK
) {
849 integral_value
= (int) gid
;
851 integral_value
= (int) value
->value
.gid
;
855 ret_code
= LTTNG_ERR_INVALID
;
859 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
861 ret_code
= LTTNG_ERR_INVALID
;
865 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
866 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
868 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
869 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
871 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
872 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
874 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
876 ret_code
= LTTNG_ERR_UNK
;
882 DBG("Kernel track %s %d for session id %" PRIu64
,
883 lttng_process_attr_to_string(process_attr
),
884 integral_value
, session
->id
);
885 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
887 * Maintain a special case for the process ID process attribute
888 * tracker as it was the only supported attribute prior to 2.12.
890 ret
= kernctl_track_pid(session
->fd
, integral_value
);
892 ret
= kernctl_track_id(
893 session
->fd
, process_attr
, integral_value
);
900 kernel_wait_quiescent();
902 /* kern-ctl error handling */
908 ret_code
= LTTNG_ERR_INVALID
;
911 ret_code
= LTTNG_ERR_NOMEM
;
914 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
917 ret_code
= LTTNG_ERR_UNK
;
921 /* Attempt to remove the value from the tracker. */
922 status
= process_attr_tracker_inclusion_set_remove_value(
924 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
925 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
926 lttng_process_attr_to_string(process_attr
),
933 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
934 struct ltt_kernel_session
*session
,
935 enum lttng_process_attr process_attr
,
936 const struct process_attr_value
*value
)
938 int ret
, integral_value
;
939 enum lttng_error_code ret_code
;
940 struct process_attr_tracker
*tracker
;
941 enum process_attr_tracker_status status
;
944 * Convert process attribute tracker value to the integral
945 * representation required by the kern-ctl API.
947 switch (process_attr
) {
948 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
949 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
950 integral_value
= (int) value
->value
.pid
;
952 case LTTNG_PROCESS_ATTR_USER_ID
:
953 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
954 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
957 ret_code
= utils_user_id_from_name(
958 value
->value
.user_name
, &uid
);
959 if (ret_code
!= LTTNG_OK
) {
962 integral_value
= (int) uid
;
964 integral_value
= (int) value
->value
.uid
;
967 case LTTNG_PROCESS_ATTR_GROUP_ID
:
968 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
969 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
972 ret_code
= utils_group_id_from_name(
973 value
->value
.group_name
, &gid
);
974 if (ret_code
!= LTTNG_OK
) {
977 integral_value
= (int) gid
;
979 integral_value
= (int) value
->value
.gid
;
983 ret_code
= LTTNG_ERR_INVALID
;
987 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
989 ret_code
= LTTNG_ERR_INVALID
;
993 status
= process_attr_tracker_inclusion_set_remove_value(
995 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
997 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
998 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1000 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1001 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1003 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1005 ret_code
= LTTNG_ERR_UNK
;
1011 DBG("Kernel track %s %d for session id %" PRIu64
,
1012 lttng_process_attr_to_string(process_attr
),
1013 integral_value
, session
->id
);
1014 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1016 * Maintain a special case for the process ID process attribute
1017 * tracker as it was the only supported attribute prior to 2.12.
1019 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1021 ret
= kernctl_untrack_id(
1022 session
->fd
, process_attr
, integral_value
);
1025 ret_code
= LTTNG_OK
;
1028 kernel_wait_quiescent();
1030 /* kern-ctl error handling */
1033 ret_code
= LTTNG_OK
;
1036 ret_code
= LTTNG_ERR_INVALID
;
1039 ret_code
= LTTNG_ERR_NOMEM
;
1042 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1045 ret_code
= LTTNG_ERR_UNK
;
1049 /* Attempt to add the value to the tracker. */
1050 status
= process_attr_tracker_inclusion_set_add_value(
1052 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1053 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1054 lttng_process_attr_to_string(process_attr
),
1062 * Create kernel metadata, open from the kernel tracer and add it to the
1065 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1068 struct ltt_kernel_metadata
*lkm
= NULL
;
1072 /* Allocate kernel metadata */
1073 lkm
= trace_kernel_create_metadata();
1078 /* Kernel tracer metadata creation */
1079 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1085 lkm
->key
= ++next_kernel_channel_key
;
1086 /* Prevent fd duplication after execlp() */
1087 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1089 PERROR("fcntl session fd");
1092 session
->metadata
= lkm
;
1094 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1099 trace_kernel_destroy_metadata(lkm
);
1105 * Start tracing session.
1107 int kernel_start_session(struct ltt_kernel_session
*session
)
1113 ret
= kernctl_start_session(session
->fd
);
1115 PERROR("ioctl start session");
1119 DBG("Kernel session started");
1128 * Make a kernel wait to make sure in-flight probe have completed.
1130 void kernel_wait_quiescent(void)
1133 int fd
= kernel_tracer_fd
;
1135 DBG("Kernel quiescent wait on %d", fd
);
1137 ret
= kernctl_wait_quiescent(fd
);
1139 PERROR("wait quiescent ioctl");
1140 ERR("Kernel quiescent wait failed");
1145 * Force flush buffer of metadata.
1147 int kernel_metadata_flush_buffer(int fd
)
1151 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1153 ret
= kernctl_buffer_flush(fd
);
1155 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1162 * Force flush buffer for channel.
1164 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1167 struct ltt_kernel_stream
*stream
;
1171 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1173 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1174 DBG("Flushing channel stream %d", stream
->fd
);
1175 ret
= kernctl_buffer_flush(stream
->fd
);
1178 ERR("Fail to flush buffer for stream %d (ret: %d)",
1187 * Stop tracing session.
1189 int kernel_stop_session(struct ltt_kernel_session
*session
)
1195 ret
= kernctl_stop_session(session
->fd
);
1200 DBG("Kernel session stopped");
1209 * Open stream of channel, register it to the kernel tracer and add it
1210 * to the stream list of the channel.
1212 * Note: given that the streams may appear in random order wrt CPU
1213 * number (e.g. cpu hotplug), the index value of the stream number in
1214 * the stream name is not necessarily linked to the CPU number.
1216 * Return the number of created stream. Else, a negative value.
1218 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1221 struct ltt_kernel_stream
*lks
;
1225 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1226 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1227 channel
->stream_count
);
1237 /* Prevent fd duplication after execlp() */
1238 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1240 PERROR("fcntl session fd");
1243 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1244 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1246 /* Add stream to channel stream list */
1247 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1248 channel
->stream_count
++;
1250 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1254 return channel
->stream_count
;
1261 * Open the metadata stream and set it to the kernel session.
1263 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1269 ret
= kernctl_create_stream(session
->metadata
->fd
);
1271 PERROR("kernel create metadata stream");
1275 DBG("Kernel metadata stream created (fd: %d)", ret
);
1276 session
->metadata_stream_fd
= ret
;
1277 /* Prevent fd duplication after execlp() */
1278 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1280 PERROR("fcntl session fd");
1290 * Get the event list from the kernel tracer and return the number of elements.
1292 ssize_t
kernel_list_events(struct lttng_event
**events
)
1296 size_t nbmem
, count
= 0;
1298 struct lttng_event
*elist
;
1302 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1304 PERROR("kernel tracepoint list");
1308 fp
= fdopen(fd
, "r");
1310 PERROR("kernel tracepoint list fdopen");
1315 * Init memory size counter
1316 * See kernel-ctl.h for explanation of this value
1318 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1319 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1320 if (elist
== NULL
) {
1321 PERROR("alloc list events");
1326 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1327 if (count
>= nbmem
) {
1328 struct lttng_event
*new_elist
;
1331 new_nbmem
= nbmem
<< 1;
1332 DBG("Reallocating event list from %zu to %zu bytes",
1334 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1335 if (new_elist
== NULL
) {
1336 PERROR("realloc list events");
1342 /* Zero the new memory */
1343 memset(new_elist
+ nbmem
, 0,
1344 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1348 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1349 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1350 elist
[count
].enabled
= -1;
1356 DBG("Kernel list events done (%zu events)", count
);
1358 ret
= fclose(fp
); /* closes both fp and fd */
1374 * Get kernel version and validate it.
1376 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1377 struct lttng_kernel_tracer_abi_version
*abi_version
)
1381 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1383 ERR("Failed to retrieve the lttng-modules version");
1387 /* Validate version */
1388 if (version
->major
!= VERSION_MAJOR
) {
1389 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1390 version
->major
, VERSION_MAJOR
);
1393 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1395 ERR("Failed to retrieve lttng-modules ABI version");
1398 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1399 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1400 abi_version
->major
, abi_version
->minor
,
1401 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1404 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1405 version
->major
, version
->minor
,
1406 abi_version
->major
, abi_version
->minor
);
1413 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1418 * Kernel work-arounds called at the start of sessiond main().
1420 int init_kernel_workarounds(void)
1426 * boot_id needs to be read once before being used concurrently
1427 * to deal with a Linux kernel race. A fix is proposed for
1428 * upstream, but the work-around is needed for older kernels.
1430 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1437 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1439 /* Ignore error, we don't really care */
1451 * Teardown of a kernel session, keeping data required by destroy notifiers.
1453 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1455 struct lttng_trace_chunk
*trace_chunk
;
1457 if (ksess
== NULL
) {
1458 DBG3("No kernel session when tearing down session");
1462 DBG("Tearing down kernel session");
1463 trace_chunk
= ksess
->current_trace_chunk
;
1466 * Destroy channels on the consumer if at least one FD has been sent and we
1467 * are in no output mode because the streams are in *no* monitor mode so we
1468 * have to send a command to clean them up or else they leaked.
1470 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1472 struct consumer_socket
*socket
;
1473 struct lttng_ht_iter iter
;
1475 /* For each consumer socket. */
1477 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1478 socket
, node
.node
) {
1479 struct ltt_kernel_channel
*chan
;
1481 /* For each channel, ask the consumer to destroy it. */
1482 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1483 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1485 /* Consumer is probably dead. Use next socket. */
1493 /* Close any relayd session */
1494 consumer_output_send_destroy_relayd(ksess
->consumer
);
1496 trace_kernel_destroy_session(ksess
);
1497 lttng_trace_chunk_put(trace_chunk
);
1500 /* Teardown of data required by destroy notifiers. */
1501 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1503 if (ksess
== NULL
) {
1506 trace_kernel_free_session(ksess
);
1510 * Destroy a kernel channel object. It does not do anything on the tracer side.
1512 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1514 struct ltt_kernel_session
*ksess
= NULL
;
1517 assert(kchan
->channel
);
1519 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1521 /* Update channel count of associated session. */
1522 if (kchan
->session
) {
1523 /* Keep pointer reference so we can update it after the destroy. */
1524 ksess
= kchan
->session
;
1527 trace_kernel_destroy_channel(kchan
);
1530 * At this point the kernel channel is not visible anymore. This is safe
1531 * since in order to work on a visible kernel session, the tracing session
1532 * lock (ltt_session.lock) MUST be acquired.
1535 ksess
->channel_count
--;
1540 * Take a snapshot for a given kernel session.
1542 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1544 enum lttng_error_code
kernel_snapshot_record(
1545 struct ltt_kernel_session
*ksess
,
1546 const struct consumer_output
*output
, int wait
,
1547 uint64_t nb_packets_per_stream
)
1549 int err
, ret
, saved_metadata_fd
;
1550 enum lttng_error_code status
= LTTNG_OK
;
1551 struct consumer_socket
*socket
;
1552 struct lttng_ht_iter iter
;
1553 struct ltt_kernel_metadata
*saved_metadata
;
1554 char *trace_path
= NULL
;
1555 size_t consumer_path_offset
= 0;
1558 assert(ksess
->consumer
);
1561 DBG("Kernel snapshot record started");
1563 /* Save current metadata since the following calls will change it. */
1564 saved_metadata
= ksess
->metadata
;
1565 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1569 ret
= kernel_open_metadata(ksess
);
1571 status
= LTTNG_ERR_KERN_META_FAIL
;
1575 ret
= kernel_open_metadata_stream(ksess
);
1577 status
= LTTNG_ERR_KERN_META_FAIL
;
1578 goto error_open_stream
;
1581 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1582 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1584 status
= LTTNG_ERR_INVALID
;
1587 /* Send metadata to consumer and snapshot everything. */
1588 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1589 socket
, node
.node
) {
1590 struct ltt_kernel_channel
*chan
;
1592 pthread_mutex_lock(socket
->lock
);
1593 /* This stream must not be monitored by the consumer. */
1594 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1595 pthread_mutex_unlock(socket
->lock
);
1597 status
= LTTNG_ERR_KERN_META_FAIL
;
1598 goto error_consumer
;
1601 /* For each channel, ask the consumer to snapshot it. */
1602 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1603 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1604 ksess
->uid
, ksess
->gid
,
1605 &trace_path
[consumer_path_offset
], wait
,
1606 nb_packets_per_stream
);
1607 if (status
!= LTTNG_OK
) {
1608 (void) kernel_consumer_destroy_metadata(socket
,
1610 goto error_consumer
;
1614 /* Snapshot metadata, */
1615 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1616 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1618 if (status
!= LTTNG_OK
) {
1619 goto error_consumer
;
1623 * The metadata snapshot is done, ask the consumer to destroy it since
1624 * it's not monitored on the consumer side.
1626 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1630 /* Close newly opened metadata stream. It's now on the consumer side. */
1631 err
= close(ksess
->metadata_stream_fd
);
1633 PERROR("close snapshot kernel");
1637 trace_kernel_destroy_metadata(ksess
->metadata
);
1639 /* Restore metadata state.*/
1640 ksess
->metadata
= saved_metadata
;
1641 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1648 * Get the syscall mask array from the kernel tracer.
1650 * Return 0 on success else a negative value. In both case, syscall_mask should
1653 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1655 assert(syscall_mask
);
1658 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1662 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1665 struct lttng_kernel_tracer_abi_version abi
;
1667 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1669 ERR("Failed to retrieve lttng-modules ABI version");
1673 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1679 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1682 * Return 1 on success, 0 when feature is not supported, negative value in case
1685 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1688 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1690 return kernel_tracer_abi_greater_or_equal(2, 3);
1694 * Check for the support of the packet sequence number via abi version number.
1696 * Return 1 on success, 0 when feature is not supported, negative value in case
1699 int kernel_supports_ring_buffer_packet_sequence_number(void)
1702 * Packet sequence number was introduced in LTTng 2.8,
1703 * lttng-modules ABI 2.1.
1705 return kernel_tracer_abi_greater_or_equal(2, 1);
1709 * Check for the support of event notifiers via abi version number.
1711 * Return 1 on success, 0 when feature is not supported, negative value in case
1714 int kernel_supports_event_notifiers(void)
1717 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1719 return kernel_tracer_abi_greater_or_equal(2, 6);
1723 * Rotate a kernel session.
1725 * Return LTTNG_OK on success or else an LTTng error code.
1727 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1730 enum lttng_error_code status
= LTTNG_OK
;
1731 struct consumer_socket
*socket
;
1732 struct lttng_ht_iter iter
;
1733 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1736 assert(ksess
->consumer
);
1738 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1739 session
->name
, session
->id
);
1744 * Note that this loop will end after one iteration given that there is
1745 * only one kernel consumer.
1747 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1748 socket
, node
.node
) {
1749 struct ltt_kernel_channel
*chan
;
1751 /* For each channel, ask the consumer to rotate it. */
1752 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1753 DBG("Rotate kernel channel %" PRIu64
", session %s",
1754 chan
->key
, session
->name
);
1755 ret
= consumer_rotate_channel(socket
, chan
->key
,
1756 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1757 /* is_metadata_channel */ false);
1759 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1765 * Rotate the metadata channel.
1767 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1768 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1769 /* is_metadata_channel */ true);
1771 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1781 enum lttng_error_code
kernel_create_channel_subdirectories(
1782 const struct ltt_kernel_session
*ksess
)
1784 enum lttng_error_code ret
= LTTNG_OK
;
1785 enum lttng_trace_chunk_status chunk_status
;
1788 assert(ksess
->current_trace_chunk
);
1791 * Create the index subdirectory which will take care
1792 * of implicitly creating the channel's path.
1794 chunk_status
= lttng_trace_chunk_create_subdirectory(
1795 ksess
->current_trace_chunk
,
1796 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1797 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1798 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1807 * Setup necessary data for kernel tracer action.
1810 int init_kernel_tracer(void)
1813 bool is_root
= !getuid();
1815 /* Modprobe lttng kernel modules */
1816 ret
= modprobe_lttng_control();
1821 /* Open debugfs lttng */
1822 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1823 if (kernel_tracer_fd
< 0) {
1824 DBG("Failed to open %s", module_proc_lttng
);
1828 /* Validate kernel version */
1829 ret
= kernel_validate_version(&kernel_tracer_version
,
1830 &kernel_tracer_abi_version
);
1835 ret
= modprobe_lttng_data();
1840 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1846 WARN("Kernel tracer does not support buffer monitoring. "
1847 "The monitoring timer of channels in the kernel domain "
1848 "will be set to 0 (disabled).");
1851 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1853 ret
= syscall_init_table(kernel_tracer_fd
);
1855 ERR("Unable to populate syscall table. Syscall tracing won't "
1856 "work for this session daemon.");
1861 modprobe_remove_lttng_control();
1862 ret
= close(kernel_tracer_fd
);
1866 kernel_tracer_fd
= -1;
1867 return LTTNG_ERR_KERN_VERSION
;
1870 ret
= close(kernel_tracer_fd
);
1876 modprobe_remove_lttng_control();
1879 WARN("No kernel tracer available");
1880 kernel_tracer_fd
= -1;
1882 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1884 return LTTNG_ERR_KERN_NA
;
1889 void cleanup_kernel_tracer(void)
1893 DBG2("Closing kernel fd");
1894 if (kernel_tracer_fd
>= 0) {
1895 ret
= close(kernel_tracer_fd
);
1899 kernel_tracer_fd
= -1;
1901 DBG("Unloading kernel modules");
1902 modprobe_remove_lttng_all();
1903 free(syscall_table
);
1907 bool kernel_tracer_is_initialized(void)
1909 return kernel_tracer_fd
>= 0;
1913 * Clear a kernel session.
1915 * Return LTTNG_OK on success or else an LTTng error code.
1917 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
1920 enum lttng_error_code status
= LTTNG_OK
;
1921 struct consumer_socket
*socket
;
1922 struct lttng_ht_iter iter
;
1923 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1926 assert(ksess
->consumer
);
1928 DBG("Clear kernel session %s (session %" PRIu64
")",
1929 session
->name
, session
->id
);
1933 if (ksess
->active
) {
1934 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
1935 status
= LTTNG_ERR_FATAL
;
1940 * Note that this loop will end after one iteration given that there is
1941 * only one kernel consumer.
1943 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1944 socket
, node
.node
) {
1945 struct ltt_kernel_channel
*chan
;
1947 /* For each channel, ask the consumer to clear it. */
1948 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1949 DBG("Clear kernel channel %" PRIu64
", session %s",
1950 chan
->key
, session
->name
);
1951 ret
= consumer_clear_channel(socket
, chan
->key
);
1957 if (!ksess
->metadata
) {
1959 * Nothing to do for the metadata.
1960 * This is a snapshot session.
1961 * The metadata is genererated on the fly.
1967 * Clear the metadata channel.
1968 * Metadata channel is not cleared per se but we still need to
1969 * perform a rotation operation on it behind the scene.
1971 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
1980 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
1981 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
1984 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;