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 struct ltt_kernel_session
*session
, 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
, session
->uid
,
271 session
->gid
, offset
);
273 DBG("userspace probe offset calculation failed for "
274 "function %s", symbol
);
278 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
284 * Compute the offsets of the instrumentation bytes in the binary based on the
285 * tracepoint probe location using the SDT lookup method. This function
286 * allocates the offsets buffer, the caller must free it.
288 * Returns 0 on success and set the offset out parameter to the offsets of the
290 * Returns -1 on error.
293 int extract_userspace_probe_offset_tracepoint_sdt(
294 const struct lttng_userspace_probe_location
*probe_location
,
295 struct ltt_kernel_session
*session
, uint64_t **offsets
,
296 uint32_t *offsets_count
)
298 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
299 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
300 const char *probe_name
= NULL
, *provider_name
= NULL
;
304 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
305 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
307 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
314 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
316 assert(lookup_method_type
==
317 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
320 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
327 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
329 if (!provider_name
) {
334 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
340 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
341 session
->uid
, session
->gid
, offsets
, offsets_count
);
343 DBG("userspace probe offset calculation failed for sdt "
344 "probe %s:%s", provider_name
, probe_name
);
348 if (*offsets_count
== 0) {
349 DBG("no userspace probe offset found");
353 DBG("%u userspace probe SDT offsets found for %s:%s at:",
354 *offsets_count
, provider_name
, probe_name
);
355 for (i
= 0; i
< *offsets_count
; i
++) {
356 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
363 * Extract the offsets of the instrumentation point for the different lookup
367 int userspace_probe_add_callsites(struct lttng_event
*ev
,
368 struct ltt_kernel_session
*session
, int fd
)
370 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
371 enum lttng_userspace_probe_location_lookup_method_type type
;
372 const struct lttng_userspace_probe_location
*location
= NULL
;
376 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
378 location
= lttng_event_get_userspace_probe_location(ev
);
384 lttng_userspace_probe_location_get_lookup_method(location
);
385 if (!lookup_method
) {
390 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
392 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
394 struct lttng_kernel_event_callsite callsite
;
397 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
399 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
403 callsite
.u
.uprobe
.offset
= offset
;
404 ret
= kernctl_add_callsite(fd
, &callsite
);
406 WARN("Adding callsite to userspace probe "
407 "event %s failed.", ev
->name
);
408 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
413 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
416 uint64_t *offsets
= NULL
;
417 uint32_t offsets_count
;
418 struct lttng_kernel_event_callsite callsite
;
421 * This call allocates the offsets buffer. This buffer must be freed
424 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
425 &offsets
, &offsets_count
);
427 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
430 for (i
= 0; i
< offsets_count
; i
++) {
431 callsite
.u
.uprobe
.offset
= offsets
[i
];
432 ret
= kernctl_add_callsite(fd
, &callsite
);
434 WARN("Adding callsite to userspace probe "
435 "event %s failed.", ev
->name
);
436 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
445 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
453 * Create a kernel event, enable it to the kernel tracer and add it to the
454 * channel event list of the kernel session.
455 * We own filter_expression and filter.
457 int kernel_create_event(struct lttng_event
*ev
,
458 struct ltt_kernel_channel
*channel
,
459 char *filter_expression
,
460 struct lttng_filter_bytecode
*filter
)
463 enum lttng_error_code ret
;
464 struct ltt_kernel_event
*event
;
469 /* We pass ownership of filter_expression and filter */
470 ret
= trace_kernel_create_event(ev
, filter_expression
,
472 if (ret
!= LTTNG_OK
) {
476 fd
= kernctl_create_event(channel
->fd
, event
->event
);
480 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
483 WARN("Event type not implemented");
484 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
487 WARN("Event %s not found!", ev
->name
);
488 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
491 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
492 PERROR("create event ioctl");
497 event
->type
= ev
->type
;
499 /* Prevent fd duplication after execlp() */
500 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
502 PERROR("fcntl session fd");
506 err
= kernctl_filter(event
->fd
, filter
);
510 ret
= LTTNG_ERR_FILTER_NOMEM
;
513 ret
= LTTNG_ERR_FILTER_INVAL
;
520 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
521 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
523 goto add_callsite_error
;
527 err
= kernctl_enable(event
->fd
);
531 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
534 PERROR("enable kernel event");
535 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
541 /* Add event to event list */
542 cds_list_add(&event
->list
, &channel
->events_list
.head
);
543 channel
->event_count
++;
545 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
555 closeret
= close(event
->fd
);
557 PERROR("close event fd");
567 * Disable a kernel channel.
569 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
575 ret
= kernctl_disable(chan
->fd
);
577 PERROR("disable chan ioctl");
582 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
583 chan
->channel
->name
, chan
->fd
, chan
->key
);
592 * Enable a kernel channel.
594 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
600 ret
= kernctl_enable(chan
->fd
);
601 if (ret
< 0 && ret
!= -EEXIST
) {
602 PERROR("Enable kernel chan");
607 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
608 chan
->channel
->name
, chan
->fd
, chan
->key
);
617 * Enable a kernel event.
619 int kernel_enable_event(struct ltt_kernel_event
*event
)
625 ret
= kernctl_enable(event
->fd
);
629 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
632 PERROR("enable kernel event");
639 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
648 * Disable a kernel event.
650 int kernel_disable_event(struct ltt_kernel_event
*event
)
656 ret
= kernctl_disable(event
->fd
);
660 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
663 PERROR("disable kernel event");
670 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
679 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
680 struct ltt_kernel_session
*session
,
681 enum lttng_process_attr process_attr
)
683 switch (process_attr
) {
684 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
685 return session
->tracker_pid
;
686 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
687 return session
->tracker_vpid
;
688 case LTTNG_PROCESS_ATTR_USER_ID
:
689 return session
->tracker_uid
;
690 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
691 return session
->tracker_vuid
;
692 case LTTNG_PROCESS_ATTR_GROUP_ID
:
693 return session
->tracker_gid
;
694 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
695 return session
->tracker_vgid
;
701 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
702 struct ltt_kernel_session
*session
,
703 enum lttng_process_attr process_attr
)
705 return (const struct process_attr_tracker
*)
706 _kernel_get_process_attr_tracker(session
, process_attr
);
709 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
710 struct ltt_kernel_session
*session
,
711 enum lttng_process_attr process_attr
,
712 enum lttng_tracking_policy policy
)
715 enum lttng_error_code ret_code
= LTTNG_OK
;
716 struct process_attr_tracker
*tracker
=
717 _kernel_get_process_attr_tracker(session
, process_attr
);
718 enum lttng_tracking_policy previous_policy
;
721 ret_code
= LTTNG_ERR_INVALID
;
725 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
726 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
728 ret_code
= LTTNG_ERR_UNK
;
732 if (previous_policy
== policy
) {
737 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
738 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
740 * Maintain a special case for the process ID process
741 * attribute tracker as it was the only supported
742 * attribute prior to 2.12.
744 ret
= kernctl_track_pid(session
->fd
, -1);
746 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
749 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
750 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
752 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
754 * Maintain a special case for the process ID process
755 * attribute tracker as it was the only supported
756 * attribute prior to 2.12.
758 ret
= kernctl_untrack_pid(session
->fd
, -1);
760 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
766 /* kern-ctl error handling */
772 ret_code
= LTTNG_ERR_INVALID
;
775 ret_code
= LTTNG_ERR_NOMEM
;
778 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
781 ret_code
= LTTNG_ERR_UNK
;
788 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
789 struct ltt_kernel_session
*session
,
790 enum lttng_process_attr process_attr
,
791 const struct process_attr_value
*value
)
793 int ret
, integral_value
;
794 enum lttng_error_code ret_code
;
795 struct process_attr_tracker
*tracker
;
796 enum process_attr_tracker_status status
;
799 * Convert process attribute tracker value to the integral
800 * representation required by the kern-ctl API.
802 switch (process_attr
) {
803 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
804 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
805 integral_value
= (int) value
->value
.pid
;
807 case LTTNG_PROCESS_ATTR_USER_ID
:
808 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
809 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
812 ret_code
= utils_user_id_from_name(
813 value
->value
.user_name
, &uid
);
814 if (ret_code
!= LTTNG_OK
) {
817 integral_value
= (int) uid
;
819 integral_value
= (int) value
->value
.uid
;
822 case LTTNG_PROCESS_ATTR_GROUP_ID
:
823 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
824 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
827 ret_code
= utils_group_id_from_name(
828 value
->value
.group_name
, &gid
);
829 if (ret_code
!= LTTNG_OK
) {
832 integral_value
= (int) gid
;
834 integral_value
= (int) value
->value
.gid
;
838 ret_code
= LTTNG_ERR_INVALID
;
842 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
844 ret_code
= LTTNG_ERR_INVALID
;
848 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
849 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
851 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
852 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
854 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
855 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
857 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
859 ret_code
= LTTNG_ERR_UNK
;
865 DBG("Kernel track %s %d for session id %" PRIu64
,
866 lttng_process_attr_to_string(process_attr
),
867 integral_value
, session
->id
);
868 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
870 * Maintain a special case for the process ID process attribute
871 * tracker as it was the only supported attribute prior to 2.12.
873 ret
= kernctl_track_pid(session
->fd
, integral_value
);
875 ret
= kernctl_track_id(
876 session
->fd
, process_attr
, integral_value
);
883 kernel_wait_quiescent();
885 /* kern-ctl error handling */
891 ret_code
= LTTNG_ERR_INVALID
;
894 ret_code
= LTTNG_ERR_NOMEM
;
897 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
900 ret_code
= LTTNG_ERR_UNK
;
904 /* Attempt to remove the value from the tracker. */
905 status
= process_attr_tracker_inclusion_set_remove_value(
907 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
908 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
909 lttng_process_attr_to_string(process_attr
),
916 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
917 struct ltt_kernel_session
*session
,
918 enum lttng_process_attr process_attr
,
919 const struct process_attr_value
*value
)
921 int ret
, integral_value
;
922 enum lttng_error_code ret_code
;
923 struct process_attr_tracker
*tracker
;
924 enum process_attr_tracker_status status
;
927 * Convert process attribute tracker value to the integral
928 * representation required by the kern-ctl API.
930 switch (process_attr
) {
931 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
932 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
933 integral_value
= (int) value
->value
.pid
;
935 case LTTNG_PROCESS_ATTR_USER_ID
:
936 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
937 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
940 ret_code
= utils_user_id_from_name(
941 value
->value
.user_name
, &uid
);
942 if (ret_code
!= LTTNG_OK
) {
945 integral_value
= (int) uid
;
947 integral_value
= (int) value
->value
.uid
;
950 case LTTNG_PROCESS_ATTR_GROUP_ID
:
951 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
952 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
955 ret_code
= utils_group_id_from_name(
956 value
->value
.group_name
, &gid
);
957 if (ret_code
!= LTTNG_OK
) {
960 integral_value
= (int) gid
;
962 integral_value
= (int) value
->value
.gid
;
966 ret_code
= LTTNG_ERR_INVALID
;
970 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
972 ret_code
= LTTNG_ERR_INVALID
;
976 status
= process_attr_tracker_inclusion_set_remove_value(
978 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
980 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
981 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
983 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
984 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
986 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
988 ret_code
= LTTNG_ERR_UNK
;
994 DBG("Kernel track %s %d for session id %" PRIu64
,
995 lttng_process_attr_to_string(process_attr
),
996 integral_value
, session
->id
);
997 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
999 * Maintain a special case for the process ID process attribute
1000 * tracker as it was the only supported attribute prior to 2.12.
1002 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1004 ret
= kernctl_untrack_id(
1005 session
->fd
, process_attr
, integral_value
);
1008 ret_code
= LTTNG_OK
;
1011 kernel_wait_quiescent();
1013 /* kern-ctl error handling */
1016 ret_code
= LTTNG_OK
;
1019 ret_code
= LTTNG_ERR_INVALID
;
1022 ret_code
= LTTNG_ERR_NOMEM
;
1025 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1028 ret_code
= LTTNG_ERR_UNK
;
1032 /* Attempt to add the value to the tracker. */
1033 status
= process_attr_tracker_inclusion_set_add_value(
1035 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1036 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1037 lttng_process_attr_to_string(process_attr
),
1045 * Create kernel metadata, open from the kernel tracer and add it to the
1048 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1051 struct ltt_kernel_metadata
*lkm
= NULL
;
1055 /* Allocate kernel metadata */
1056 lkm
= trace_kernel_create_metadata();
1061 /* Kernel tracer metadata creation */
1062 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1068 lkm
->key
= ++next_kernel_channel_key
;
1069 /* Prevent fd duplication after execlp() */
1070 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1072 PERROR("fcntl session fd");
1075 session
->metadata
= lkm
;
1077 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1082 trace_kernel_destroy_metadata(lkm
);
1088 * Start tracing session.
1090 int kernel_start_session(struct ltt_kernel_session
*session
)
1096 ret
= kernctl_start_session(session
->fd
);
1098 PERROR("ioctl start session");
1102 DBG("Kernel session started");
1111 * Make a kernel wait to make sure in-flight probe have completed.
1113 void kernel_wait_quiescent(void)
1116 int fd
= kernel_tracer_fd
;
1118 DBG("Kernel quiescent wait on %d", fd
);
1120 ret
= kernctl_wait_quiescent(fd
);
1122 PERROR("wait quiescent ioctl");
1123 ERR("Kernel quiescent wait failed");
1128 * Force flush buffer of metadata.
1130 int kernel_metadata_flush_buffer(int fd
)
1134 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1136 ret
= kernctl_buffer_flush(fd
);
1138 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1145 * Force flush buffer for channel.
1147 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1150 struct ltt_kernel_stream
*stream
;
1154 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1156 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1157 DBG("Flushing channel stream %d", stream
->fd
);
1158 ret
= kernctl_buffer_flush(stream
->fd
);
1161 ERR("Fail to flush buffer for stream %d (ret: %d)",
1170 * Stop tracing session.
1172 int kernel_stop_session(struct ltt_kernel_session
*session
)
1178 ret
= kernctl_stop_session(session
->fd
);
1183 DBG("Kernel session stopped");
1192 * Open stream of channel, register it to the kernel tracer and add it
1193 * to the stream list of the channel.
1195 * Note: given that the streams may appear in random order wrt CPU
1196 * number (e.g. cpu hotplug), the index value of the stream number in
1197 * the stream name is not necessarily linked to the CPU number.
1199 * Return the number of created stream. Else, a negative value.
1201 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1204 struct ltt_kernel_stream
*lks
;
1208 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1209 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1210 channel
->stream_count
);
1220 /* Prevent fd duplication after execlp() */
1221 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1223 PERROR("fcntl session fd");
1226 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1227 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1229 /* Add stream to channel stream list */
1230 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1231 channel
->stream_count
++;
1233 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1237 return channel
->stream_count
;
1244 * Open the metadata stream and set it to the kernel session.
1246 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1252 ret
= kernctl_create_stream(session
->metadata
->fd
);
1254 PERROR("kernel create metadata stream");
1258 DBG("Kernel metadata stream created (fd: %d)", ret
);
1259 session
->metadata_stream_fd
= ret
;
1260 /* Prevent fd duplication after execlp() */
1261 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1263 PERROR("fcntl session fd");
1273 * Get the event list from the kernel tracer and return the number of elements.
1275 ssize_t
kernel_list_events(struct lttng_event
**events
)
1279 size_t nbmem
, count
= 0;
1281 struct lttng_event
*elist
;
1285 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1287 PERROR("kernel tracepoint list");
1291 fp
= fdopen(fd
, "r");
1293 PERROR("kernel tracepoint list fdopen");
1298 * Init memory size counter
1299 * See kernel-ctl.h for explanation of this value
1301 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1302 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1303 if (elist
== NULL
) {
1304 PERROR("alloc list events");
1309 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1310 if (count
>= nbmem
) {
1311 struct lttng_event
*new_elist
;
1314 new_nbmem
= nbmem
<< 1;
1315 DBG("Reallocating event list from %zu to %zu bytes",
1317 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1318 if (new_elist
== NULL
) {
1319 PERROR("realloc list events");
1325 /* Zero the new memory */
1326 memset(new_elist
+ nbmem
, 0,
1327 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1331 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1332 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1333 elist
[count
].enabled
= -1;
1339 DBG("Kernel list events done (%zu events)", count
);
1341 ret
= fclose(fp
); /* closes both fp and fd */
1357 * Get kernel version and validate it.
1359 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1360 struct lttng_kernel_tracer_abi_version
*abi_version
)
1364 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1366 ERR("Failed to retrieve the lttng-modules version");
1370 /* Validate version */
1371 if (version
->major
!= VERSION_MAJOR
) {
1372 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1373 version
->major
, VERSION_MAJOR
);
1376 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1378 ERR("Failed to retrieve lttng-modules ABI version");
1381 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1382 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1383 abi_version
->major
, abi_version
->minor
,
1384 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1387 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1388 version
->major
, version
->minor
,
1389 abi_version
->major
, abi_version
->minor
);
1396 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1401 * Kernel work-arounds called at the start of sessiond main().
1403 int init_kernel_workarounds(void)
1409 * boot_id needs to be read once before being used concurrently
1410 * to deal with a Linux kernel race. A fix is proposed for
1411 * upstream, but the work-around is needed for older kernels.
1413 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1420 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1422 /* Ignore error, we don't really care */
1434 * Teardown of a kernel session, keeping data required by destroy notifiers.
1436 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1438 struct lttng_trace_chunk
*trace_chunk
;
1440 if (ksess
== NULL
) {
1441 DBG3("No kernel session when tearing down session");
1445 DBG("Tearing down kernel session");
1446 trace_chunk
= ksess
->current_trace_chunk
;
1449 * Destroy channels on the consumer if at least one FD has been sent and we
1450 * are in no output mode because the streams are in *no* monitor mode so we
1451 * have to send a command to clean them up or else they leaked.
1453 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1455 struct consumer_socket
*socket
;
1456 struct lttng_ht_iter iter
;
1458 /* For each consumer socket. */
1460 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1461 socket
, node
.node
) {
1462 struct ltt_kernel_channel
*chan
;
1464 /* For each channel, ask the consumer to destroy it. */
1465 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1466 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1468 /* Consumer is probably dead. Use next socket. */
1476 /* Close any relayd session */
1477 consumer_output_send_destroy_relayd(ksess
->consumer
);
1479 trace_kernel_destroy_session(ksess
);
1480 lttng_trace_chunk_put(trace_chunk
);
1483 /* Teardown of data required by destroy notifiers. */
1484 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1486 if (ksess
== NULL
) {
1489 trace_kernel_free_session(ksess
);
1493 * Destroy a kernel channel object. It does not do anything on the tracer side.
1495 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1497 struct ltt_kernel_session
*ksess
= NULL
;
1500 assert(kchan
->channel
);
1502 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1504 /* Update channel count of associated session. */
1505 if (kchan
->session
) {
1506 /* Keep pointer reference so we can update it after the destroy. */
1507 ksess
= kchan
->session
;
1510 trace_kernel_destroy_channel(kchan
);
1513 * At this point the kernel channel is not visible anymore. This is safe
1514 * since in order to work on a visible kernel session, the tracing session
1515 * lock (ltt_session.lock) MUST be acquired.
1518 ksess
->channel_count
--;
1523 * Take a snapshot for a given kernel session.
1525 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1527 enum lttng_error_code
kernel_snapshot_record(
1528 struct ltt_kernel_session
*ksess
,
1529 const struct consumer_output
*output
, int wait
,
1530 uint64_t nb_packets_per_stream
)
1532 int err
, ret
, saved_metadata_fd
;
1533 enum lttng_error_code status
= LTTNG_OK
;
1534 struct consumer_socket
*socket
;
1535 struct lttng_ht_iter iter
;
1536 struct ltt_kernel_metadata
*saved_metadata
;
1537 char *trace_path
= NULL
;
1538 size_t consumer_path_offset
= 0;
1541 assert(ksess
->consumer
);
1544 DBG("Kernel snapshot record started");
1546 /* Save current metadata since the following calls will change it. */
1547 saved_metadata
= ksess
->metadata
;
1548 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1552 ret
= kernel_open_metadata(ksess
);
1554 status
= LTTNG_ERR_KERN_META_FAIL
;
1558 ret
= kernel_open_metadata_stream(ksess
);
1560 status
= LTTNG_ERR_KERN_META_FAIL
;
1561 goto error_open_stream
;
1564 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1565 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1567 status
= LTTNG_ERR_INVALID
;
1570 /* Send metadata to consumer and snapshot everything. */
1571 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1572 socket
, node
.node
) {
1573 struct ltt_kernel_channel
*chan
;
1575 pthread_mutex_lock(socket
->lock
);
1576 /* This stream must not be monitored by the consumer. */
1577 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1578 pthread_mutex_unlock(socket
->lock
);
1580 status
= LTTNG_ERR_KERN_META_FAIL
;
1581 goto error_consumer
;
1584 /* For each channel, ask the consumer to snapshot it. */
1585 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1586 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1587 ksess
->uid
, ksess
->gid
,
1588 &trace_path
[consumer_path_offset
], wait
,
1589 nb_packets_per_stream
);
1590 if (status
!= LTTNG_OK
) {
1591 (void) kernel_consumer_destroy_metadata(socket
,
1593 goto error_consumer
;
1597 /* Snapshot metadata, */
1598 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1599 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1601 if (status
!= LTTNG_OK
) {
1602 goto error_consumer
;
1606 * The metadata snapshot is done, ask the consumer to destroy it since
1607 * it's not monitored on the consumer side.
1609 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1613 /* Close newly opened metadata stream. It's now on the consumer side. */
1614 err
= close(ksess
->metadata_stream_fd
);
1616 PERROR("close snapshot kernel");
1620 trace_kernel_destroy_metadata(ksess
->metadata
);
1622 /* Restore metadata state.*/
1623 ksess
->metadata
= saved_metadata
;
1624 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1631 * Get the syscall mask array from the kernel tracer.
1633 * Return 0 on success else a negative value. In both case, syscall_mask should
1636 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1638 assert(syscall_mask
);
1641 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1645 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1648 * Return 1 on success, 0 when feature is not supported, negative value in case
1651 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1653 int ret
= 0; // Not supported by default
1654 struct lttng_kernel_tracer_abi_version abi
;
1656 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1658 ERR("Failed to retrieve lttng-modules ABI version");
1663 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1665 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1677 * Check for the support of the packet sequence number via abi version number.
1679 * Return 1 on success, 0 when feature is not supported, negative value in case
1682 int kernel_supports_ring_buffer_packet_sequence_number(void)
1684 int ret
= 0; // Not supported by default
1685 struct lttng_kernel_tracer_abi_version abi
;
1687 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1689 ERR("Failed to retrieve lttng-modules ABI version");
1694 * Packet sequence number was introduced in LTTng 2.8,
1695 * lttng-modules ABI 2.1.
1697 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1709 * Rotate a kernel session.
1711 * Return LTTNG_OK on success or else an LTTng error code.
1713 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1716 enum lttng_error_code status
= LTTNG_OK
;
1717 struct consumer_socket
*socket
;
1718 struct lttng_ht_iter iter
;
1719 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1722 assert(ksess
->consumer
);
1724 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1725 session
->name
, session
->id
);
1730 * Note that this loop will end after one iteration given that there is
1731 * only one kernel consumer.
1733 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1734 socket
, node
.node
) {
1735 struct ltt_kernel_channel
*chan
;
1737 /* For each channel, ask the consumer to rotate it. */
1738 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1739 DBG("Rotate kernel channel %" PRIu64
", session %s",
1740 chan
->key
, session
->name
);
1741 ret
= consumer_rotate_channel(socket
, chan
->key
,
1742 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1743 /* is_metadata_channel */ false);
1745 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1751 * Rotate the metadata channel.
1753 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1754 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1755 /* is_metadata_channel */ true);
1757 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1767 enum lttng_error_code
kernel_create_channel_subdirectories(
1768 const struct ltt_kernel_session
*ksess
)
1770 enum lttng_error_code ret
= LTTNG_OK
;
1771 enum lttng_trace_chunk_status chunk_status
;
1774 assert(ksess
->current_trace_chunk
);
1777 * Create the index subdirectory which will take care
1778 * of implicitly creating the channel's path.
1780 chunk_status
= lttng_trace_chunk_create_subdirectory(
1781 ksess
->current_trace_chunk
,
1782 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1783 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1784 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1793 * Setup necessary data for kernel tracer action.
1796 int init_kernel_tracer(void)
1799 bool is_root
= !getuid();
1801 /* Modprobe lttng kernel modules */
1802 ret
= modprobe_lttng_control();
1807 /* Open debugfs lttng */
1808 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1809 if (kernel_tracer_fd
< 0) {
1810 DBG("Failed to open %s", module_proc_lttng
);
1814 /* Validate kernel version */
1815 ret
= kernel_validate_version(&kernel_tracer_version
,
1816 &kernel_tracer_abi_version
);
1821 ret
= modprobe_lttng_data();
1826 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1832 WARN("Kernel tracer does not support buffer monitoring. "
1833 "The monitoring timer of channels in the kernel domain "
1834 "will be set to 0 (disabled).");
1837 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1839 ret
= syscall_init_table(kernel_tracer_fd
);
1841 ERR("Unable to populate syscall table. Syscall tracing won't "
1842 "work for this session daemon.");
1847 modprobe_remove_lttng_control();
1848 ret
= close(kernel_tracer_fd
);
1852 kernel_tracer_fd
= -1;
1853 return LTTNG_ERR_KERN_VERSION
;
1856 ret
= close(kernel_tracer_fd
);
1862 modprobe_remove_lttng_control();
1865 WARN("No kernel tracer available");
1866 kernel_tracer_fd
= -1;
1868 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1870 return LTTNG_ERR_KERN_NA
;
1875 void cleanup_kernel_tracer(void)
1879 DBG2("Closing kernel fd");
1880 if (kernel_tracer_fd
>= 0) {
1881 ret
= close(kernel_tracer_fd
);
1885 kernel_tracer_fd
= -1;
1887 DBG("Unloading kernel modules");
1888 modprobe_remove_lttng_all();
1889 free(syscall_table
);
1893 bool kernel_tracer_is_initialized(void)
1895 return kernel_tracer_fd
>= 0;
1899 * Clear a kernel session.
1901 * Return LTTNG_OK on success or else an LTTng error code.
1903 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
1906 enum lttng_error_code status
= LTTNG_OK
;
1907 struct consumer_socket
*socket
;
1908 struct lttng_ht_iter iter
;
1909 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1912 assert(ksess
->consumer
);
1914 DBG("Clear kernel session %s (session %" PRIu64
")",
1915 session
->name
, session
->id
);
1919 if (ksess
->active
) {
1920 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
1921 status
= LTTNG_ERR_FATAL
;
1926 * Note that this loop will end after one iteration given that there is
1927 * only one kernel consumer.
1929 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1930 socket
, node
.node
) {
1931 struct ltt_kernel_channel
*chan
;
1933 /* For each channel, ask the consumer to clear it. */
1934 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1935 DBG("Clear kernel channel %" PRIu64
", session %s",
1936 chan
->key
, session
->name
);
1937 ret
= consumer_clear_channel(socket
, chan
->key
);
1943 if (!ksess
->metadata
) {
1945 * Nothing to do for the metadata.
1946 * This is a snapshot session.
1947 * The metadata is genererated on the fly.
1953 * Clear the metadata channel.
1954 * Metadata channel is not cleared per se but we still need to
1955 * perform a rotation operation on it behind the scene.
1957 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
1966 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
1967 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
1970 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;