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/event.h"
12 #include "lttng/lttng-error.h"
13 #include "lttng/tracker.h"
21 #include <sys/types.h>
23 #include <common/common.h>
24 #include <common/hashtable/utils.h>
25 #include <common/trace-chunk.h>
26 #include <common/kernel-ctl/kernel-ctl.h>
27 #include <common/kernel-ctl/kernel-ioctl.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
30 #include <lttng/userspace-probe.h>
31 #include <lttng/userspace-probe-internal.h>
32 #include <lttng/condition/event-rule.h>
33 #include <lttng/condition/event-rule-internal.h>
34 #include <lttng/event-rule/event-rule.h>
35 #include <lttng/event-rule/event-rule-internal.h>
36 #include <lttng/event-rule/uprobe-internal.h>
38 #include "lttng-sessiond.h"
39 #include "lttng-syscall.h"
42 #include "kernel-consumer.h"
43 #include "kern-modules.h"
47 #include "notification-thread-commands.h"
50 * Key used to reference a channel between the sessiond and the consumer. This
51 * is only read and updated with the session_list lock held.
53 static uint64_t next_kernel_channel_key
;
55 static const char *module_proc_lttng
= "/proc/lttng";
57 static int kernel_tracer_fd
= -1;
58 static int kernel_tracer_trigger_group_fd
= -1;
59 static int kernel_tracer_trigger_group_notification_fd
= -1;
60 static struct ltt_kernel_token_event_rule_list kernel_tracer_token_list
;
63 * Add context on a kernel channel.
65 * Assumes the ownership of ctx.
67 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
68 struct ltt_kernel_context
*ctx
)
75 DBG("Adding context to channel %s", chan
->channel
->name
);
76 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
80 /* Exists but not available for this kernel */
81 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
84 /* If EEXIST, we just ignore the error */
88 PERROR("add context ioctl");
89 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
96 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
101 trace_kernel_destroy_context(ctx
);
107 * Create a new kernel session, register it to the kernel tracer and add it to
108 * the session daemon session.
110 int kernel_create_session(struct ltt_session
*session
)
113 struct ltt_kernel_session
*lks
;
117 /* Allocate data structure */
118 lks
= trace_kernel_create_session();
124 /* Kernel tracer session creation */
125 ret
= kernctl_create_session(kernel_tracer_fd
);
127 PERROR("ioctl kernel create session");
132 /* Prevent fd duplication after execlp() */
133 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
135 PERROR("fcntl session fd");
138 lks
->id
= session
->id
;
139 lks
->consumer_fds_sent
= 0;
140 session
->kernel_session
= lks
;
142 DBG("Kernel session created (fd: %d)", lks
->fd
);
145 * This is necessary since the creation time is present in the session
146 * name when it is generated.
148 if (session
->has_auto_generated_name
) {
149 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
151 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
154 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
155 session
->id
, session
->name
);
158 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
160 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
161 session
->id
, session
->name
);
168 trace_kernel_destroy_session(lks
);
169 trace_kernel_free_session(lks
);
175 * Create a kernel channel, register it to the kernel tracer and add it to the
178 int kernel_create_channel(struct ltt_kernel_session
*session
,
179 struct lttng_channel
*chan
)
182 struct ltt_kernel_channel
*lkc
;
187 /* Allocate kernel channel */
188 lkc
= trace_kernel_create_channel(chan
);
193 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
194 chan
->name
, lkc
->channel
->attr
.overwrite
,
195 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
196 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
197 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
199 /* Kernel tracer channel creation */
200 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
202 PERROR("ioctl kernel create channel");
206 /* Setup the channel fd */
208 /* Prevent fd duplication after execlp() */
209 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
211 PERROR("fcntl session fd");
214 /* Add channel to session */
215 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
216 session
->channel_count
++;
217 lkc
->session
= session
;
218 lkc
->key
= ++next_kernel_channel_key
;
220 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
221 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
234 * Create a kernel channel, register it to the kernel tracer and add it to the
238 int kernel_create_trigger_group(int *trigger_group_fd
)
243 assert(trigger_group_fd
);
245 /* Kernel tracer channel creation */
246 ret
= kernctl_create_trigger_group(kernel_tracer_fd
);
248 PERROR("ioctl kernel create trigger group");
256 /* Prevent fd duplication after execlp() */
257 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
259 PERROR("fcntl session fd");
262 DBG("Kernel trigger group created (fd: %d)",
267 *trigger_group_fd
= local_fd
;
272 * Compute the offset of the instrumentation byte in the binary based on the
273 * function probe location using the ELF lookup method.
275 * Returns 0 on success and set the offset out parameter to the offset of the
277 * Returns -1 on error
280 int extract_userspace_probe_offset_function_elf(
281 const struct lttng_userspace_probe_location
*probe_location
,
282 uid_t uid
, gid_t gid
, uint64_t *offset
)
286 const char *symbol
= NULL
;
287 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
288 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
290 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
291 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
293 lookup
= lttng_userspace_probe_location_get_lookup_method(
301 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
303 assert(lookup_method_type
==
304 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
306 symbol
= lttng_userspace_probe_location_function_get_function_name(
313 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
319 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
321 DBG("userspace probe offset calculation failed for "
322 "function %s", symbol
);
326 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
332 * Compute the offsets of the instrumentation bytes in the binary based on the
333 * tracepoint probe location using the SDT lookup method. This function
334 * allocates the offsets buffer, the caller must free it.
336 * Returns 0 on success and set the offset out parameter to the offsets of the
338 * Returns -1 on error.
341 int extract_userspace_probe_offset_tracepoint_sdt(
342 const struct lttng_userspace_probe_location
*probe_location
,
343 uid_t uid
, gid_t gid
, uint64_t **offsets
,
344 uint32_t *offsets_count
)
346 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
347 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
348 const char *probe_name
= NULL
, *provider_name
= NULL
;
352 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
353 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
355 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
362 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
364 assert(lookup_method_type
==
365 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
368 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
375 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
377 if (!provider_name
) {
382 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
388 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
389 uid
, gid
, offsets
, offsets_count
);
391 DBG("userspace probe offset calculation failed for sdt "
392 "probe %s:%s", provider_name
, probe_name
);
396 if (*offsets_count
== 0) {
397 DBG("no userspace probe offset found");
401 DBG("%u userspace probe SDT offsets found for %s:%s at:",
402 *offsets_count
, provider_name
, probe_name
);
403 for (i
= 0; i
< *offsets_count
; i
++) {
404 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
411 int userspace_probe_add_callsite(
412 const struct lttng_userspace_probe_location
*location
,
413 uid_t uid
, gid_t gid
, int fd
)
415 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
416 enum lttng_userspace_probe_location_lookup_method_type type
;
419 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
420 if (!lookup_method
) {
425 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
427 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
429 struct lttng_kernel_event_callsite callsite
;
432 ret
= extract_userspace_probe_offset_function_elf(location
,
435 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
439 callsite
.u
.uprobe
.offset
= offset
;
440 ret
= kernctl_add_callsite(fd
, &callsite
);
442 WARN("Adding callsite to ELF userspace probe failed.");
443 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
448 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
451 uint64_t *offsets
= NULL
;
452 uint32_t offsets_count
;
453 struct lttng_kernel_event_callsite callsite
;
456 * This call allocates the offsets buffer. This buffer must be freed
459 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
460 uid
, gid
, &offsets
, &offsets_count
);
462 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
465 for (i
= 0; i
< offsets_count
; i
++) {
466 callsite
.u
.uprobe
.offset
= offsets
[i
];
467 ret
= kernctl_add_callsite(fd
, &callsite
);
469 WARN("Adding callsite to SDT userspace probe "
471 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
480 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
488 * Extract the offsets of the instrumentation point for the different lookup
492 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
493 struct ltt_kernel_session
*session
, int fd
)
495 const struct lttng_userspace_probe_location
*location
= NULL
;
499 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
501 location
= lttng_event_get_userspace_probe_location(ev
);
507 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
510 WARN("Adding callsite to userspace probe event \"%s\" "
511 "failed.", ev
->name
);
519 * Extract the offsets of the instrumentation point for the different lookup
522 static int userspace_probe_event_rule_add_callsites(
523 const struct lttng_event_rule
*rule
,
524 const struct lttng_credentials
*creds
,
527 const struct lttng_userspace_probe_location
*location
= NULL
;
528 enum lttng_event_rule_status status
;
533 assert(lttng_event_rule_get_type(rule
) == LTTNG_EVENT_RULE_TYPE_UPROBE
);
535 status
= lttng_event_rule_uprobe_get_location(rule
, &location
);
536 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
541 ret
= userspace_probe_add_callsite(location
, creds
->uid
, creds
->gid
,
544 WARN("Adding callsite to userspace probe object %d"
553 * Create a kernel event, enable it to the kernel tracer and add it to the
554 * channel event list of the kernel session.
555 * We own filter_expression and filter.
557 int kernel_create_event(struct lttng_event
*ev
,
558 struct ltt_kernel_channel
*channel
,
559 char *filter_expression
,
560 struct lttng_bytecode
*filter
)
563 enum lttng_error_code ret
;
564 struct ltt_kernel_event
*event
;
569 /* We pass ownership of filter_expression and filter */
570 ret
= trace_kernel_create_event(ev
, filter_expression
,
572 if (ret
!= LTTNG_OK
) {
576 fd
= kernctl_create_event(channel
->fd
, event
->event
);
580 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
583 WARN("Event type not implemented");
584 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
587 WARN("Event %s not found!", ev
->name
);
588 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
591 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
592 PERROR("create event ioctl");
597 event
->type
= ev
->type
;
599 /* Prevent fd duplication after execlp() */
600 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
602 PERROR("fcntl session fd");
606 err
= kernctl_filter(event
->fd
, filter
);
610 ret
= LTTNG_ERR_FILTER_NOMEM
;
613 ret
= LTTNG_ERR_FILTER_INVAL
;
620 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
621 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
624 goto add_callsite_error
;
628 err
= kernctl_enable(event
->fd
);
632 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
635 PERROR("enable kernel event");
636 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
642 /* Add event to event list */
643 cds_list_add(&event
->list
, &channel
->events_list
.head
);
644 channel
->event_count
++;
646 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
656 closeret
= close(event
->fd
);
658 PERROR("close event fd");
668 * Disable a kernel channel.
670 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
676 ret
= kernctl_disable(chan
->fd
);
678 PERROR("disable chan ioctl");
683 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
684 chan
->channel
->name
, chan
->fd
, chan
->key
);
693 * Enable a kernel channel.
695 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
701 ret
= kernctl_enable(chan
->fd
);
702 if (ret
< 0 && ret
!= -EEXIST
) {
703 PERROR("Enable kernel chan");
708 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
709 chan
->channel
->name
, chan
->fd
, chan
->key
);
718 * Enable a kernel event.
720 int kernel_enable_event(struct ltt_kernel_event
*event
)
726 ret
= kernctl_enable(event
->fd
);
730 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
733 PERROR("enable kernel event");
740 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
749 * Disable a kernel event.
751 int kernel_disable_event(struct ltt_kernel_event
*event
)
757 ret
= kernctl_disable(event
->fd
);
761 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
764 PERROR("disable kernel event");
771 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
780 * Disable a kernel trigger.
783 int kernel_disable_token_event_rule(struct ltt_kernel_token_event_rule
*event
)
789 ret
= kernctl_disable(event
->fd
);
793 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
796 PERROR("disable kernel event");
803 DBG("Kernel trigger token %" PRIu64
" disabled (fd: %d)", event
->token
, event
->fd
);
812 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
813 struct ltt_kernel_session
*session
,
814 enum lttng_process_attr process_attr
)
816 switch (process_attr
) {
817 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
818 return session
->tracker_pid
;
819 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
820 return session
->tracker_vpid
;
821 case LTTNG_PROCESS_ATTR_USER_ID
:
822 return session
->tracker_uid
;
823 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
824 return session
->tracker_vuid
;
825 case LTTNG_PROCESS_ATTR_GROUP_ID
:
826 return session
->tracker_gid
;
827 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
828 return session
->tracker_vgid
;
834 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
835 struct ltt_kernel_session
*session
,
836 enum lttng_process_attr process_attr
)
838 return (const struct process_attr_tracker
*)
839 _kernel_get_process_attr_tracker(session
, process_attr
);
842 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
843 struct ltt_kernel_session
*session
,
844 enum lttng_process_attr process_attr
,
845 enum lttng_tracking_policy policy
)
848 enum lttng_error_code ret_code
= LTTNG_OK
;
849 struct process_attr_tracker
*tracker
=
850 _kernel_get_process_attr_tracker(session
, process_attr
);
851 enum lttng_tracking_policy previous_policy
;
854 ret_code
= LTTNG_ERR_INVALID
;
858 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
859 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
861 ret_code
= LTTNG_ERR_UNK
;
865 if (previous_policy
== policy
) {
870 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
871 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
873 * Maintain a special case for the process ID process
874 * attribute tracker as it was the only supported
875 * attribute prior to 2.12.
877 ret
= kernctl_track_pid(session
->fd
, -1);
879 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
882 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
883 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
885 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
887 * Maintain a special case for the process ID process
888 * attribute tracker as it was the only supported
889 * attribute prior to 2.12.
891 ret
= kernctl_untrack_pid(session
->fd
, -1);
893 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
899 /* kern-ctl error handling */
905 ret_code
= LTTNG_ERR_INVALID
;
908 ret_code
= LTTNG_ERR_NOMEM
;
911 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
914 ret_code
= LTTNG_ERR_UNK
;
921 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
922 struct ltt_kernel_session
*session
,
923 enum lttng_process_attr process_attr
,
924 const struct process_attr_value
*value
)
926 int ret
, integral_value
;
927 enum lttng_error_code ret_code
;
928 struct process_attr_tracker
*tracker
;
929 enum process_attr_tracker_status status
;
932 * Convert process attribute tracker value to the integral
933 * representation required by the kern-ctl API.
935 switch (process_attr
) {
936 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
937 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
938 integral_value
= (int) value
->value
.pid
;
940 case LTTNG_PROCESS_ATTR_USER_ID
:
941 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
942 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
945 ret_code
= utils_user_id_from_name(
946 value
->value
.user_name
, &uid
);
947 if (ret_code
!= LTTNG_OK
) {
950 integral_value
= (int) uid
;
952 integral_value
= (int) value
->value
.uid
;
955 case LTTNG_PROCESS_ATTR_GROUP_ID
:
956 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
957 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
960 ret_code
= utils_group_id_from_name(
961 value
->value
.group_name
, &gid
);
962 if (ret_code
!= LTTNG_OK
) {
965 integral_value
= (int) gid
;
967 integral_value
= (int) value
->value
.gid
;
971 ret_code
= LTTNG_ERR_INVALID
;
975 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
977 ret_code
= LTTNG_ERR_INVALID
;
981 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
982 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
984 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
985 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
987 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
988 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
990 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
992 ret_code
= LTTNG_ERR_UNK
;
998 DBG("Kernel track %s %d for session id %" PRIu64
,
999 lttng_process_attr_to_string(process_attr
),
1000 integral_value
, session
->id
);
1001 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1003 * Maintain a special case for the process ID process attribute
1004 * tracker as it was the only supported attribute prior to 2.12.
1006 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1008 ret
= kernctl_track_id(
1009 session
->fd
, process_attr
, integral_value
);
1012 ret_code
= LTTNG_OK
;
1016 kernel_wait_quiescent();
1018 /* kern-ctl error handling */
1021 ret_code
= LTTNG_OK
;
1024 ret_code
= LTTNG_ERR_INVALID
;
1027 ret_code
= LTTNG_ERR_NOMEM
;
1030 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1033 ret_code
= LTTNG_ERR_UNK
;
1037 /* Attempt to remove the value from the tracker. */
1038 status
= process_attr_tracker_inclusion_set_remove_value(
1040 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1041 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1042 lttng_process_attr_to_string(process_attr
),
1049 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1050 struct ltt_kernel_session
*session
,
1051 enum lttng_process_attr process_attr
,
1052 const struct process_attr_value
*value
)
1054 int ret
, integral_value
;
1055 enum lttng_error_code ret_code
;
1056 struct process_attr_tracker
*tracker
;
1057 enum process_attr_tracker_status status
;
1060 * Convert process attribute tracker value to the integral
1061 * representation required by the kern-ctl API.
1063 switch (process_attr
) {
1064 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1065 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1066 integral_value
= (int) value
->value
.pid
;
1068 case LTTNG_PROCESS_ATTR_USER_ID
:
1069 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1070 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1073 ret_code
= utils_user_id_from_name(
1074 value
->value
.user_name
, &uid
);
1075 if (ret_code
!= LTTNG_OK
) {
1078 integral_value
= (int) uid
;
1080 integral_value
= (int) value
->value
.uid
;
1083 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1084 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1085 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1088 ret_code
= utils_group_id_from_name(
1089 value
->value
.group_name
, &gid
);
1090 if (ret_code
!= LTTNG_OK
) {
1093 integral_value
= (int) gid
;
1095 integral_value
= (int) value
->value
.gid
;
1099 ret_code
= LTTNG_ERR_INVALID
;
1103 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1105 ret_code
= LTTNG_ERR_INVALID
;
1109 status
= process_attr_tracker_inclusion_set_remove_value(
1111 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1113 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1114 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1116 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1117 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1119 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1121 ret_code
= LTTNG_ERR_UNK
;
1127 DBG("Kernel track %s %d for session id %" PRIu64
,
1128 lttng_process_attr_to_string(process_attr
),
1129 integral_value
, session
->id
);
1130 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1132 * Maintain a special case for the process ID process attribute
1133 * tracker as it was the only supported attribute prior to 2.12.
1135 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1137 ret
= kernctl_untrack_id(
1138 session
->fd
, process_attr
, integral_value
);
1141 ret_code
= LTTNG_OK
;
1144 kernel_wait_quiescent();
1146 /* kern-ctl error handling */
1149 ret_code
= LTTNG_OK
;
1152 ret_code
= LTTNG_ERR_INVALID
;
1155 ret_code
= LTTNG_ERR_NOMEM
;
1158 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1161 ret_code
= LTTNG_ERR_UNK
;
1165 /* Attempt to add the value to the tracker. */
1166 status
= process_attr_tracker_inclusion_set_add_value(
1168 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1169 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1170 lttng_process_attr_to_string(process_attr
),
1178 * Create kernel metadata, open from the kernel tracer and add it to the
1181 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1184 struct ltt_kernel_metadata
*lkm
= NULL
;
1188 /* Allocate kernel metadata */
1189 lkm
= trace_kernel_create_metadata();
1194 /* Kernel tracer metadata creation */
1195 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1201 lkm
->key
= ++next_kernel_channel_key
;
1202 /* Prevent fd duplication after execlp() */
1203 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1205 PERROR("fcntl session fd");
1208 session
->metadata
= lkm
;
1210 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1215 trace_kernel_destroy_metadata(lkm
);
1221 * Start tracing session.
1223 int kernel_start_session(struct ltt_kernel_session
*session
)
1229 ret
= kernctl_start_session(session
->fd
);
1231 PERROR("ioctl start session");
1235 DBG("Kernel session started");
1244 * Make a kernel wait to make sure in-flight probe have completed.
1246 void kernel_wait_quiescent(void)
1249 int fd
= kernel_tracer_fd
;
1251 DBG("Kernel quiescent wait on %d", fd
);
1253 ret
= kernctl_wait_quiescent(fd
);
1255 PERROR("wait quiescent ioctl");
1256 ERR("Kernel quiescent wait failed");
1261 * Force flush buffer of metadata.
1263 int kernel_metadata_flush_buffer(int fd
)
1267 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1269 ret
= kernctl_buffer_flush(fd
);
1271 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1278 * Force flush buffer for channel.
1280 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1283 struct ltt_kernel_stream
*stream
;
1287 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1289 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1290 DBG("Flushing channel stream %d", stream
->fd
);
1291 ret
= kernctl_buffer_flush(stream
->fd
);
1294 ERR("Fail to flush buffer for stream %d (ret: %d)",
1303 * Stop tracing session.
1305 int kernel_stop_session(struct ltt_kernel_session
*session
)
1311 ret
= kernctl_stop_session(session
->fd
);
1316 DBG("Kernel session stopped");
1325 * Open stream of channel, register it to the kernel tracer and add it
1326 * to the stream list of the channel.
1328 * Note: given that the streams may appear in random order wrt CPU
1329 * number (e.g. cpu hotplug), the index value of the stream number in
1330 * the stream name is not necessarily linked to the CPU number.
1332 * Return the number of created stream. Else, a negative value.
1334 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1337 struct ltt_kernel_stream
*lks
;
1341 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1342 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1343 channel
->stream_count
);
1353 /* Prevent fd duplication after execlp() */
1354 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1356 PERROR("fcntl session fd");
1359 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1360 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1362 /* Add stream to channel stream list */
1363 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1364 channel
->stream_count
++;
1366 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1370 return channel
->stream_count
;
1377 * Open the metadata stream and set it to the kernel session.
1379 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1385 ret
= kernctl_create_stream(session
->metadata
->fd
);
1387 PERROR("kernel create metadata stream");
1391 DBG("Kernel metadata stream created (fd: %d)", ret
);
1392 session
->metadata_stream_fd
= ret
;
1393 /* Prevent fd duplication after execlp() */
1394 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1396 PERROR("fcntl session fd");
1406 * Get the event list from the kernel tracer and return the number of elements.
1408 ssize_t
kernel_list_events(struct lttng_event
**events
)
1412 size_t nbmem
, count
= 0;
1414 struct lttng_event
*elist
;
1418 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1420 PERROR("kernel tracepoint list");
1424 fp
= fdopen(fd
, "r");
1426 PERROR("kernel tracepoint list fdopen");
1431 * Init memory size counter
1432 * See kernel-ctl.h for explanation of this value
1434 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1435 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1436 if (elist
== NULL
) {
1437 PERROR("alloc list events");
1442 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1443 if (count
>= nbmem
) {
1444 struct lttng_event
*new_elist
;
1447 new_nbmem
= nbmem
<< 1;
1448 DBG("Reallocating event list from %zu to %zu bytes",
1450 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1451 if (new_elist
== NULL
) {
1452 PERROR("realloc list events");
1458 /* Zero the new memory */
1459 memset(new_elist
+ nbmem
, 0,
1460 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1464 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1465 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1466 elist
[count
].enabled
= -1;
1472 DBG("Kernel list events done (%zu events)", count
);
1474 ret
= fclose(fp
); /* closes both fp and fd */
1490 * Get kernel version and validate it.
1492 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1493 struct lttng_kernel_tracer_abi_version
*abi_version
)
1497 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1499 ERR("Failed to retrieve the lttng-modules version");
1503 /* Validate version */
1504 if (version
->major
!= VERSION_MAJOR
) {
1505 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1506 version
->major
, VERSION_MAJOR
);
1509 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1511 ERR("Failed to retrieve lttng-modules ABI version");
1514 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1515 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1516 abi_version
->major
, abi_version
->minor
,
1517 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1520 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1521 version
->major
, version
->minor
,
1522 abi_version
->major
, abi_version
->minor
);
1529 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1534 * Kernel work-arounds called at the start of sessiond main().
1536 int init_kernel_workarounds(void)
1542 * boot_id needs to be read once before being used concurrently
1543 * to deal with a Linux kernel race. A fix is proposed for
1544 * upstream, but the work-around is needed for older kernels.
1546 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1553 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1555 /* Ignore error, we don't really care */
1567 * Teardown of a kernel session, keeping data required by destroy notifiers.
1569 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1571 struct lttng_trace_chunk
*trace_chunk
;
1573 if (ksess
== NULL
) {
1574 DBG3("No kernel session when tearing down session");
1578 DBG("Tearing down kernel session");
1579 trace_chunk
= ksess
->current_trace_chunk
;
1582 * Destroy channels on the consumer if at least one FD has been sent and we
1583 * are in no output mode because the streams are in *no* monitor mode so we
1584 * have to send a command to clean them up or else they leaked.
1586 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1588 struct consumer_socket
*socket
;
1589 struct lttng_ht_iter iter
;
1591 /* For each consumer socket. */
1593 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1594 socket
, node
.node
) {
1595 struct ltt_kernel_channel
*chan
;
1597 /* For each channel, ask the consumer to destroy it. */
1598 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1599 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1601 /* Consumer is probably dead. Use next socket. */
1609 /* Close any relayd session */
1610 consumer_output_send_destroy_relayd(ksess
->consumer
);
1612 trace_kernel_destroy_session(ksess
);
1613 lttng_trace_chunk_put(trace_chunk
);
1616 /* Teardown of data required by destroy notifiers. */
1617 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1619 if (ksess
== NULL
) {
1622 trace_kernel_free_session(ksess
);
1626 * Destroy a kernel channel object. It does not do anything on the tracer side.
1628 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1630 struct ltt_kernel_session
*ksess
= NULL
;
1633 assert(kchan
->channel
);
1635 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1637 /* Update channel count of associated session. */
1638 if (kchan
->session
) {
1639 /* Keep pointer reference so we can update it after the destroy. */
1640 ksess
= kchan
->session
;
1643 trace_kernel_destroy_channel(kchan
);
1646 * At this point the kernel channel is not visible anymore. This is safe
1647 * since in order to work on a visible kernel session, the tracing session
1648 * lock (ltt_session.lock) MUST be acquired.
1651 ksess
->channel_count
--;
1656 * Take a snapshot for a given kernel session.
1658 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1660 enum lttng_error_code
kernel_snapshot_record(
1661 struct ltt_kernel_session
*ksess
,
1662 const struct consumer_output
*output
, int wait
,
1663 uint64_t nb_packets_per_stream
)
1665 int err
, ret
, saved_metadata_fd
;
1666 enum lttng_error_code status
= LTTNG_OK
;
1667 struct consumer_socket
*socket
;
1668 struct lttng_ht_iter iter
;
1669 struct ltt_kernel_metadata
*saved_metadata
;
1670 char *trace_path
= NULL
;
1671 size_t consumer_path_offset
= 0;
1674 assert(ksess
->consumer
);
1677 DBG("Kernel snapshot record started");
1679 /* Save current metadata since the following calls will change it. */
1680 saved_metadata
= ksess
->metadata
;
1681 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1685 ret
= kernel_open_metadata(ksess
);
1687 status
= LTTNG_ERR_KERN_META_FAIL
;
1691 ret
= kernel_open_metadata_stream(ksess
);
1693 status
= LTTNG_ERR_KERN_META_FAIL
;
1694 goto error_open_stream
;
1697 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1698 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1700 status
= LTTNG_ERR_INVALID
;
1703 /* Send metadata to consumer and snapshot everything. */
1704 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1705 socket
, node
.node
) {
1706 struct ltt_kernel_channel
*chan
;
1708 pthread_mutex_lock(socket
->lock
);
1709 /* This stream must not be monitored by the consumer. */
1710 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1711 pthread_mutex_unlock(socket
->lock
);
1713 status
= LTTNG_ERR_KERN_META_FAIL
;
1714 goto error_consumer
;
1717 /* For each channel, ask the consumer to snapshot it. */
1718 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1719 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1720 ksess
->uid
, ksess
->gid
,
1721 &trace_path
[consumer_path_offset
], wait
,
1722 nb_packets_per_stream
);
1723 if (status
!= LTTNG_OK
) {
1724 (void) kernel_consumer_destroy_metadata(socket
,
1726 goto error_consumer
;
1730 /* Snapshot metadata, */
1731 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1732 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1734 if (status
!= LTTNG_OK
) {
1735 goto error_consumer
;
1739 * The metadata snapshot is done, ask the consumer to destroy it since
1740 * it's not monitored on the consumer side.
1742 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1746 /* Close newly opened metadata stream. It's now on the consumer side. */
1747 err
= close(ksess
->metadata_stream_fd
);
1749 PERROR("close snapshot kernel");
1753 trace_kernel_destroy_metadata(ksess
->metadata
);
1755 /* Restore metadata state.*/
1756 ksess
->metadata
= saved_metadata
;
1757 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1764 * Get the syscall mask array from the kernel tracer.
1766 * Return 0 on success else a negative value. In both case, syscall_mask should
1769 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1771 assert(syscall_mask
);
1774 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1778 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1781 * Return 1 on success, 0 when feature is not supported, negative value in case
1784 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1786 int ret
= 0; // Not supported by default
1787 struct lttng_kernel_tracer_abi_version abi
;
1789 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1791 ERR("Failed to retrieve lttng-modules ABI version");
1796 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1798 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1810 * Check for the support of the packet sequence number via abi version number.
1812 * Return 1 on success, 0 when feature is not supported, negative value in case
1815 int kernel_supports_ring_buffer_packet_sequence_number(void)
1817 int ret
= 0; // Not supported by default
1818 struct lttng_kernel_tracer_abi_version abi
;
1820 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1822 ERR("Failed to retrieve lttng-modules ABI version");
1827 * Packet sequence number was introduced in LTTng 2.8,
1828 * lttng-modules ABI 2.1.
1830 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1842 * Rotate a kernel session.
1844 * Return LTTNG_OK on success or else an LTTng error code.
1846 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1849 enum lttng_error_code status
= LTTNG_OK
;
1850 struct consumer_socket
*socket
;
1851 struct lttng_ht_iter iter
;
1852 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1855 assert(ksess
->consumer
);
1857 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1858 session
->name
, session
->id
);
1863 * Note that this loop will end after one iteration given that there is
1864 * only one kernel consumer.
1866 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1867 socket
, node
.node
) {
1868 struct ltt_kernel_channel
*chan
;
1870 /* For each channel, ask the consumer to rotate it. */
1871 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1872 DBG("Rotate kernel channel %" PRIu64
", session %s",
1873 chan
->key
, session
->name
);
1874 ret
= consumer_rotate_channel(socket
, chan
->key
,
1875 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1876 /* is_metadata_channel */ false);
1878 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1884 * Rotate the metadata channel.
1886 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1887 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1888 /* is_metadata_channel */ true);
1890 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1900 enum lttng_error_code
kernel_create_channel_subdirectories(
1901 const struct ltt_kernel_session
*ksess
)
1903 enum lttng_error_code ret
= LTTNG_OK
;
1904 enum lttng_trace_chunk_status chunk_status
;
1907 assert(ksess
->current_trace_chunk
);
1910 * Create the index subdirectory which will take care
1911 * of implicitly creating the channel's path.
1913 chunk_status
= lttng_trace_chunk_create_subdirectory(
1914 ksess
->current_trace_chunk
,
1915 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1916 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1917 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1926 * Setup necessary data for kernel tracer action.
1929 int init_kernel_tracer(void)
1932 bool is_root
= !getuid();
1934 /* Modprobe lttng kernel modules */
1935 ret
= modprobe_lttng_control();
1940 /* Open debugfs lttng */
1941 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1942 if (kernel_tracer_fd
< 0) {
1943 DBG("Failed to open %s", module_proc_lttng
);
1947 /* Validate kernel version */
1948 ret
= kernel_validate_version(&kernel_tracer_version
,
1949 &kernel_tracer_abi_version
);
1954 ret
= modprobe_lttng_data();
1959 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1964 WARN("Kernel tracer does not support buffer monitoring. "
1965 "The monitoring timer of channels in the kernel domain "
1966 "will be set to 0 (disabled).");
1969 ret
= kernel_create_trigger_group(&kernel_tracer_trigger_group_fd
);
1971 /* TODO: error handling if it is not supported etc. */
1972 WARN("Failed trigger group creation");
1973 kernel_tracer_trigger_group_fd
= -1;
1974 /* This is not fatal */
1976 ret
= kernel_create_trigger_group_notification_fd(&kernel_tracer_trigger_group_notification_fd
);
1982 CDS_INIT_LIST_HEAD(&kernel_tracer_token_list
.head
);
1984 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1985 DBG("Kernel tracer trigger group fd %d", kernel_tracer_trigger_group_fd
);
1986 DBG("Kernel tracer trigger group notificationi fd %d", kernel_tracer_trigger_group_notification_fd
);
1988 ret
= syscall_init_table(kernel_tracer_fd
);
1990 ERR("Unable to populate syscall table. Syscall tracing won't "
1991 "work for this session daemon.");
1997 modprobe_remove_lttng_control();
1998 ret
= close(kernel_tracer_fd
);
2002 kernel_tracer_fd
= -1;
2003 return LTTNG_ERR_KERN_VERSION
;
2006 ret
= close(kernel_tracer_fd
);
2012 modprobe_remove_lttng_control();
2015 WARN("No kernel tracer available");
2016 kernel_tracer_fd
= -1;
2018 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2020 return LTTNG_ERR_KERN_NA
;
2025 void cleanup_kernel_tracer(void)
2029 struct ltt_kernel_token_event_rule
*rule
, *rtmp
;
2030 cds_list_for_each_entry_safe(rule
, rtmp
, &kernel_tracer_token_list
.head
, list
) {
2031 kernel_disable_token_event_rule(rule
);
2032 trace_kernel_destroy_token_event_rule(rule
);
2035 DBG2("Closing kernel trigger group notification fd");
2036 if (kernel_tracer_trigger_group_notification_fd
>= 0) {
2037 ret
= close(kernel_tracer_trigger_group_notification_fd
);
2041 kernel_tracer_trigger_group_notification_fd
= -1;
2044 /* TODO: do we iterate over the list to remove all token? */
2045 DBG2("Closing kernel trigger group fd");
2046 if (kernel_tracer_trigger_group_fd
>= 0) {
2047 ret
= close(kernel_tracer_trigger_group_fd
);
2051 kernel_tracer_trigger_group_fd
= -1;
2054 DBG2("Closing kernel fd");
2055 if (kernel_tracer_fd
>= 0) {
2056 ret
= close(kernel_tracer_fd
);
2060 kernel_tracer_fd
= -1;
2063 DBG("Unloading kernel modules");
2064 modprobe_remove_lttng_all();
2065 free(syscall_table
);
2069 bool kernel_tracer_is_initialized(void)
2071 return kernel_tracer_fd
>= 0;
2075 * Clear a kernel session.
2077 * Return LTTNG_OK on success or else an LTTng error code.
2079 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2082 enum lttng_error_code status
= LTTNG_OK
;
2083 struct consumer_socket
*socket
;
2084 struct lttng_ht_iter iter
;
2085 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2088 assert(ksess
->consumer
);
2090 DBG("Clear kernel session %s (session %" PRIu64
")",
2091 session
->name
, session
->id
);
2095 if (ksess
->active
) {
2096 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2097 status
= LTTNG_ERR_FATAL
;
2102 * Note that this loop will end after one iteration given that there is
2103 * only one kernel consumer.
2105 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2106 socket
, node
.node
) {
2107 struct ltt_kernel_channel
*chan
;
2109 /* For each channel, ask the consumer to clear it. */
2110 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2111 DBG("Clear kernel channel %" PRIu64
", session %s",
2112 chan
->key
, session
->name
);
2113 ret
= consumer_clear_channel(socket
, chan
->key
);
2119 if (!ksess
->metadata
) {
2121 * Nothing to do for the metadata.
2122 * This is a snapshot session.
2123 * The metadata is genererated on the fly.
2129 * Clear the metadata channel.
2130 * Metadata channel is not cleared per se but we still need to
2131 * perform a rotation operation on it behind the scene.
2133 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2142 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2143 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2146 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2154 enum lttng_error_code
kernel_create_trigger_group_notification_fd(
2155 int *trigger_group_notification_fd
)
2157 enum lttng_error_code ret
= LTTNG_OK
;
2160 assert(trigger_group_notification_fd
);
2162 ret
= kernctl_create_trigger_group_notification_fd(kernel_tracer_trigger_group_fd
);
2164 PERROR("ioctl kernel create trigger group");
2172 /* Prevent fd duplication after execlp() */
2173 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2175 PERROR("fcntl session fd");
2178 DBG("Kernel trigger group notification created (fd: %d)",
2183 *trigger_group_notification_fd
= local_fd
;
2187 enum lttng_error_code
kernel_destroy_trigger_group_notification_fd(
2188 int trigger_group_notification_fd
)
2190 enum lttng_error_code ret
= LTTNG_OK
;
2191 DBG("Closing trigger group notification fd %d", trigger_group_notification_fd
);
2192 if (trigger_group_notification_fd
>= 0) {
2193 ret
= close(trigger_group_notification_fd
);
2201 static int kernel_create_token_event_rule(struct lttng_trigger
*trigger
,
2202 const struct lttng_credentials
*creds
, uint64_t token
)
2205 enum lttng_error_code ret
;
2206 struct ltt_kernel_token_event_rule
*event
;
2207 struct lttng_kernel_trigger kernel_trigger
;
2208 unsigned int capture_bytecode_count
= 0;
2209 struct lttng_condition
*condition
= NULL
;
2210 struct lttng_event_rule
*event_rule
= NULL
;
2214 condition
= lttng_trigger_get_condition(trigger
);
2216 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2218 lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
);
2220 assert(lttng_event_rule_get_type(event_rule
) != LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2222 /* TODO: Should we check that the event-rule is kernel oriented here? */
2224 ret
= trace_kernel_create_token_event_rule(event_rule
, token
, &event
);
2225 if (ret
!= LTTNG_OK
) {
2229 trace_kernel_init_trigger_from_event_rule(event
->event_rule
, &kernel_trigger
);
2230 kernel_trigger
.id
= event
->token
;
2232 fd
= kernctl_create_trigger(kernel_tracer_trigger_group_fd
, &kernel_trigger
);
2236 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2239 WARN("Trigger type not implemented");
2240 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2243 WARN("Event %s not found!", kernel_trigger
.name
);
2244 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2247 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2248 PERROR("create trigger ioctl");
2254 /* Prevent fd duplication after execlp() */
2255 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
2257 PERROR("fcntl session fd");
2260 if (event
->filter
) {
2261 err
= kernctl_filter(event
->fd
, event
->filter
);
2265 ret
= LTTNG_ERR_FILTER_NOMEM
;
2268 ret
= LTTNG_ERR_FILTER_INVAL
;
2275 if (lttng_event_rule_get_type(event
->event_rule
) ==
2276 LTTNG_EVENT_RULE_TYPE_UPROBE
) {
2277 ret
= userspace_probe_event_rule_add_callsites(
2278 event
->event_rule
, creds
, event
->fd
);
2280 goto add_callsite_error
;
2284 /* Set the capture bytecode */
2285 capture_bytecode_count
= lttng_trigger_get_capture_bytecode_count(trigger
);
2286 for (unsigned int i
= 0; i
< capture_bytecode_count
; i
++) {
2287 const struct lttng_bytecode
*capture_bytecode
= lttng_trigger_get_capture_bytecode_at_index(trigger
, i
);
2288 ret
= kernctl_capture(event
->fd
, capture_bytecode
);
2294 err
= kernctl_enable(event
->fd
);
2298 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2301 PERROR("enable kernel trigger");
2302 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2308 /* Add event to event list */
2309 cds_list_add(&event
->list
, &kernel_tracer_token_list
.head
);
2311 DBG("Trigger %s created (fd: %d)", kernel_trigger
.name
, event
->fd
);
2321 closeret
= close(event
->fd
);
2323 PERROR("close event fd");
2332 enum lttng_error_code
kernel_update_tokens(void)
2334 enum lttng_error_code ret
= LTTNG_OK
;
2335 enum lttng_trigger_status t_status
;
2336 struct ltt_kernel_token_event_rule
*token_event_rule_element
;
2337 struct lttng_triggers
*triggers
;
2340 /* TODO error handling */
2342 /* Get list of token trigger from the notification thread here */
2344 pthread_mutex_lock(¬ification_trigger_tokens_ht_lock
);
2345 ret
= notification_thread_command_get_tokens(notification_thread_handle
, &triggers
);
2346 if (ret
!= LTTNG_OK
) {
2353 t_status
= lttng_triggers_get_count(triggers
, &count
);
2354 if (t_status
!= LTTNG_TRIGGER_STATUS_OK
) {
2359 for (unsigned int i
= 0; i
< count
; i
++) {
2360 struct lttng_condition
*condition
;
2361 struct lttng_event_rule
*event_rule
;
2362 struct lttng_trigger
*trigger
;
2363 struct ltt_kernel_token_event_rule
*k_token
;
2364 const struct lttng_credentials
*creds
;
2367 trigger
= lttng_triggers_get_pointer_of_index(triggers
, i
);
2370 /* TODO: error checking and type checking */
2371 token
= lttng_trigger_get_key(trigger
);
2372 condition
= lttng_trigger_get_condition(trigger
);
2373 (void) lttng_condition_event_rule_get_rule_no_const(condition
, &event_rule
);
2375 if (lttng_event_rule_get_domain_type(event_rule
) != LTTNG_DOMAIN_KERNEL
) {
2376 /* Skip ust related trigger */
2380 creds
= lttng_trigger_get_credentials(trigger
);
2381 /* Iterate over all known token trigger */
2382 k_token
= trace_kernel_find_trigger_by_token(&kernel_tracer_token_list
, token
);
2384 ret
= kernel_create_token_event_rule(trigger
, creds
, token
);
2391 /* Remove all unknown trigger from the app
2392 * TODO find a way better way then this, do it on the unregister command
2393 * and be specific on the token to remove instead of going over all
2394 * trigger known to the app. This is sub optimal.
2396 cds_list_for_each_entry (token_event_rule_element
, &kernel_tracer_token_list
.head
,
2401 token
= token_event_rule_element
->token
;
2404 * Check if the app event trigger still exists on the
2405 * notification side.
2406 * TODO: might want to change the backing data struct of the
2407 * lttng_triggers object to allow quick lookup?
2408 * For kernel mostly all of this can be removed once we delete
2409 * on a per trigger basis.
2412 for (unsigned int i
= 0; i
< count
; i
++) {
2413 struct lttng_trigger
*trigger
;
2414 uint64_t inner_token
;
2416 trigger
= lttng_triggers_get_pointer_of_index(
2420 inner_token
= lttng_trigger_get_key(trigger
);
2422 if (inner_token
== token
) {
2433 kernel_disable_token_event_rule(token_event_rule_element
);
2434 trace_kernel_destroy_token_event_rule(token_event_rule_element
);
2437 lttng_triggers_destroy(triggers
);
2439 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
2444 int kernel_get_notification_fd(void)
2446 return kernel_tracer_trigger_group_notification_fd
;