2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
17 #include <common/common.h>
18 #include <common/hashtable/utils.h>
19 #include <common/trace-chunk.h>
20 #include <common/kernel-ctl/kernel-ctl.h>
21 #include <common/kernel-ctl/kernel-ioctl.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
23 #include <common/tracker.h>
24 #include <common/utils.h>
25 #include <lttng/event.h>
26 #include <lttng/lttng-error.h>
27 #include <lttng/tracker.h>
29 #include <lttng/userspace-probe.h>
30 #include <lttng/userspace-probe-internal.h>
31 #include <lttng/condition/event-rule.h>
32 #include <lttng/condition/event-rule-internal.h>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.h>
35 #include <lttng/event-rule/uprobe-internal.h>
37 #include "lttng-sessiond.h"
38 #include "lttng-syscall.h"
39 #include "condition-internal.h"
42 #include "kernel-consumer.h"
43 #include "kern-modules.h"
44 #include "sessiond-config.h"
49 #include "trigger-error-accounting.h"
50 #include "notification-thread-commands.h"
53 * Key used to reference a channel between the sessiond and the consumer. This
54 * is only read and updated with the session_list lock held.
56 static uint64_t next_kernel_channel_key
;
58 static const char *module_proc_lttng
= "/proc/lttng";
60 static int kernel_tracer_fd
= -1;
61 static int kernel_tracer_trigger_group_fd
= -1;
62 static int kernel_tracer_trigger_group_notification_fd
= -1;
63 static struct cds_lfht
*kernel_tracer_token_ht
;
66 * Add context on a kernel channel.
68 * Assumes the ownership of ctx.
70 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
71 struct ltt_kernel_context
*ctx
)
78 DBG("Adding context to channel %s", chan
->channel
->name
);
79 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
83 /* Exists but not available for this kernel */
84 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
87 /* If EEXIST, we just ignore the error */
91 PERROR("add context ioctl");
92 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
99 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
104 trace_kernel_destroy_context(ctx
);
110 * Create a new kernel session, register it to the kernel tracer and add it to
111 * the session daemon session.
113 int kernel_create_session(struct ltt_session
*session
)
116 struct ltt_kernel_session
*lks
;
120 /* Allocate data structure */
121 lks
= trace_kernel_create_session();
127 /* Kernel tracer session creation */
128 ret
= kernctl_create_session(kernel_tracer_fd
);
130 PERROR("ioctl kernel create session");
135 /* Prevent fd duplication after execlp() */
136 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
138 PERROR("fcntl session fd");
141 lks
->id
= session
->id
;
142 lks
->consumer_fds_sent
= 0;
143 session
->kernel_session
= lks
;
145 DBG("Kernel session created (fd: %d)", lks
->fd
);
148 * This is necessary since the creation time is present in the session
149 * name when it is generated.
151 if (session
->has_auto_generated_name
) {
152 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
154 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
157 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
158 session
->id
, session
->name
);
161 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
163 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
164 session
->id
, session
->name
);
171 trace_kernel_destroy_session(lks
);
172 trace_kernel_free_session(lks
);
178 * Create a kernel channel, register it to the kernel tracer and add it to the
181 int kernel_create_channel(struct ltt_kernel_session
*session
,
182 struct lttng_channel
*chan
)
185 struct ltt_kernel_channel
*lkc
;
190 /* Allocate kernel channel */
191 lkc
= trace_kernel_create_channel(chan
);
196 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
197 chan
->name
, lkc
->channel
->attr
.overwrite
,
198 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
199 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
200 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
202 /* Kernel tracer channel creation */
203 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
205 PERROR("ioctl kernel create channel");
209 /* Setup the channel fd */
211 /* Prevent fd duplication after execlp() */
212 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
214 PERROR("fcntl session fd");
217 /* Add channel to session */
218 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
219 session
->channel_count
++;
220 lkc
->session
= session
;
221 lkc
->key
= ++next_kernel_channel_key
;
223 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
224 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
237 * Create a kernel channel, register it to the kernel tracer and add it to the
241 int kernel_create_trigger_group(int *trigger_group_fd
)
246 assert(trigger_group_fd
);
248 /* Kernel tracer channel creation */
249 ret
= kernctl_create_trigger_group(kernel_tracer_fd
);
251 PERROR("ioctl kernel create trigger group");
259 /* Prevent fd duplication after execlp() */
260 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
262 PERROR("fcntl session fd");
265 DBG("Kernel trigger group created (fd: %d)",
270 *trigger_group_fd
= local_fd
;
275 * Compute the offset of the instrumentation byte in the binary based on the
276 * function probe location using the ELF lookup method.
278 * Returns 0 on success and set the offset out parameter to the offset of the
280 * Returns -1 on error
283 int extract_userspace_probe_offset_function_elf(
284 const struct lttng_userspace_probe_location
*probe_location
,
285 uid_t uid
, gid_t gid
, uint64_t *offset
)
289 const char *symbol
= NULL
;
290 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
291 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
293 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
294 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
296 lookup
= lttng_userspace_probe_location_get_lookup_method(
304 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
306 assert(lookup_method_type
==
307 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
309 symbol
= lttng_userspace_probe_location_function_get_function_name(
316 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
322 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
324 DBG("userspace probe offset calculation failed for "
325 "function %s", symbol
);
329 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
335 * Compute the offsets of the instrumentation bytes in the binary based on the
336 * tracepoint probe location using the SDT lookup method. This function
337 * allocates the offsets buffer, the caller must free it.
339 * Returns 0 on success and set the offset out parameter to the offsets of the
341 * Returns -1 on error.
344 int extract_userspace_probe_offset_tracepoint_sdt(
345 const struct lttng_userspace_probe_location
*probe_location
,
346 uid_t uid
, gid_t gid
, uint64_t **offsets
,
347 uint32_t *offsets_count
)
349 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
350 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
351 const char *probe_name
= NULL
, *provider_name
= NULL
;
355 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
356 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
358 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
365 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
367 assert(lookup_method_type
==
368 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
371 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
378 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
380 if (!provider_name
) {
385 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
391 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
392 uid
, gid
, offsets
, offsets_count
);
394 DBG("userspace probe offset calculation failed for sdt "
395 "probe %s:%s", provider_name
, probe_name
);
399 if (*offsets_count
== 0) {
400 DBG("no userspace probe offset found");
404 DBG("%u userspace probe SDT offsets found for %s:%s at:",
405 *offsets_count
, provider_name
, probe_name
);
406 for (i
= 0; i
< *offsets_count
; i
++) {
407 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
414 int userspace_probe_add_callsite(
415 const struct lttng_userspace_probe_location
*location
,
416 uid_t uid
, gid_t gid
, int fd
)
418 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
419 enum lttng_userspace_probe_location_lookup_method_type type
;
422 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
423 if (!lookup_method
) {
428 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
430 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
432 struct lttng_kernel_event_callsite callsite
;
435 ret
= extract_userspace_probe_offset_function_elf(location
,
438 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
442 callsite
.u
.uprobe
.offset
= offset
;
443 ret
= kernctl_add_callsite(fd
, &callsite
);
445 WARN("Adding callsite to ELF userspace probe failed.");
446 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
451 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
454 uint64_t *offsets
= NULL
;
455 uint32_t offsets_count
;
456 struct lttng_kernel_event_callsite callsite
;
459 * This call allocates the offsets buffer. This buffer must be freed
462 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
463 uid
, gid
, &offsets
, &offsets_count
);
465 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
468 for (i
= 0; i
< offsets_count
; i
++) {
469 callsite
.u
.uprobe
.offset
= offsets
[i
];
470 ret
= kernctl_add_callsite(fd
, &callsite
);
472 WARN("Adding callsite to SDT userspace probe "
474 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
483 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
491 * Extract the offsets of the instrumentation point for the different lookup
495 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
496 struct ltt_kernel_session
*session
, int fd
)
498 const struct lttng_userspace_probe_location
*location
= NULL
;
502 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
504 location
= lttng_event_get_userspace_probe_location(ev
);
510 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
513 WARN("Adding callsite to userspace probe event \"%s\" "
514 "failed.", ev
->name
);
522 * Extract the offsets of the instrumentation point for the different lookup
525 static int userspace_probe_event_rule_add_callsites(
526 const struct lttng_event_rule
*rule
,
527 const struct lttng_credentials
*creds
,
530 const struct lttng_userspace_probe_location
*location
= NULL
;
531 enum lttng_event_rule_status status
;
536 assert(lttng_event_rule_get_type(rule
) == LTTNG_EVENT_RULE_TYPE_UPROBE
);
538 status
= lttng_event_rule_uprobe_get_location(rule
, &location
);
539 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
544 ret
= userspace_probe_add_callsite(
545 location
, lttng_credentials_get_uid(creds
), lttng_credentials_get_gid(creds
), fd
);
547 WARN("Adding callsite to userspace probe object %d"
556 * Create a kernel event, enable it to the kernel tracer and add it to the
557 * channel event list of the kernel session.
558 * We own filter_expression and filter.
560 int kernel_create_event(struct lttng_event
*ev
,
561 struct ltt_kernel_channel
*channel
,
562 char *filter_expression
,
563 struct lttng_bytecode
*filter
)
566 enum lttng_error_code ret
;
567 struct ltt_kernel_event
*event
;
572 /* We pass ownership of filter_expression and filter */
573 ret
= trace_kernel_create_event(ev
, filter_expression
,
575 if (ret
!= LTTNG_OK
) {
579 fd
= kernctl_create_event(channel
->fd
, event
->event
);
583 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
586 WARN("Event type not implemented");
587 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
590 WARN("Event %s not found!", ev
->name
);
591 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
594 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
595 PERROR("create event ioctl");
600 event
->type
= ev
->type
;
602 /* Prevent fd duplication after execlp() */
603 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
605 PERROR("fcntl session fd");
609 err
= kernctl_filter(event
->fd
, filter
);
613 ret
= LTTNG_ERR_FILTER_NOMEM
;
616 ret
= LTTNG_ERR_FILTER_INVAL
;
623 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
624 ret
= userspace_probe_event_add_callsites(ev
, channel
->session
,
627 goto add_callsite_error
;
631 err
= kernctl_enable(event
->fd
);
635 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
638 PERROR("enable kernel event");
639 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
645 /* Add event to event list */
646 cds_list_add(&event
->list
, &channel
->events_list
.head
);
647 channel
->event_count
++;
649 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
659 closeret
= close(event
->fd
);
661 PERROR("close event fd");
671 * Disable a kernel channel.
673 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
679 ret
= kernctl_disable(chan
->fd
);
681 PERROR("disable chan ioctl");
686 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
687 chan
->channel
->name
, chan
->fd
, chan
->key
);
696 * Enable a kernel channel.
698 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
704 ret
= kernctl_enable(chan
->fd
);
705 if (ret
< 0 && ret
!= -EEXIST
) {
706 PERROR("Enable kernel chan");
711 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
712 chan
->channel
->name
, chan
->fd
, chan
->key
);
721 * Enable a kernel event.
723 int kernel_enable_event(struct ltt_kernel_event
*event
)
729 ret
= kernctl_enable(event
->fd
);
733 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
736 PERROR("enable kernel event");
743 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
752 * Disable a kernel event.
754 int kernel_disable_event(struct ltt_kernel_event
*event
)
760 ret
= kernctl_disable(event
->fd
);
764 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
767 PERROR("disable kernel event");
774 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
783 * Disable a kernel trigger.
786 int kernel_disable_token_event_rule(struct ltt_kernel_token_event_rule
*event
)
793 cds_lfht_del(kernel_tracer_token_ht
, &event
->ht_node
);
796 ret
= kernctl_disable(event
->fd
);
800 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
803 PERROR("disable kernel event");
810 DBG("Kernel trigger token %" PRIu64
" disabled (fd: %d)", event
->token
, event
->fd
);
819 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
820 struct ltt_kernel_session
*session
,
821 enum lttng_process_attr process_attr
)
823 switch (process_attr
) {
824 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
825 return session
->tracker_pid
;
826 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
827 return session
->tracker_vpid
;
828 case LTTNG_PROCESS_ATTR_USER_ID
:
829 return session
->tracker_uid
;
830 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
831 return session
->tracker_vuid
;
832 case LTTNG_PROCESS_ATTR_GROUP_ID
:
833 return session
->tracker_gid
;
834 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
835 return session
->tracker_vgid
;
841 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
842 struct ltt_kernel_session
*session
,
843 enum lttng_process_attr process_attr
)
845 return (const struct process_attr_tracker
*)
846 _kernel_get_process_attr_tracker(session
, process_attr
);
849 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
850 struct ltt_kernel_session
*session
,
851 enum lttng_process_attr process_attr
,
852 enum lttng_tracking_policy policy
)
855 enum lttng_error_code ret_code
= LTTNG_OK
;
856 struct process_attr_tracker
*tracker
=
857 _kernel_get_process_attr_tracker(session
, process_attr
);
858 enum lttng_tracking_policy previous_policy
;
861 ret_code
= LTTNG_ERR_INVALID
;
865 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
866 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
868 ret_code
= LTTNG_ERR_UNK
;
872 if (previous_policy
== policy
) {
877 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
878 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
880 * Maintain a special case for the process ID process
881 * attribute tracker as it was the only supported
882 * attribute prior to 2.12.
884 ret
= kernctl_track_pid(session
->fd
, -1);
886 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
889 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
890 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
892 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
894 * Maintain a special case for the process ID process
895 * attribute tracker as it was the only supported
896 * attribute prior to 2.12.
898 ret
= kernctl_untrack_pid(session
->fd
, -1);
900 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
906 /* kern-ctl error handling */
912 ret_code
= LTTNG_ERR_INVALID
;
915 ret_code
= LTTNG_ERR_NOMEM
;
918 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
921 ret_code
= LTTNG_ERR_UNK
;
928 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
929 struct ltt_kernel_session
*session
,
930 enum lttng_process_attr process_attr
,
931 const struct process_attr_value
*value
)
933 int ret
, integral_value
;
934 enum lttng_error_code ret_code
;
935 struct process_attr_tracker
*tracker
;
936 enum process_attr_tracker_status status
;
939 * Convert process attribute tracker value to the integral
940 * representation required by the kern-ctl API.
942 switch (process_attr
) {
943 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
944 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
945 integral_value
= (int) value
->value
.pid
;
947 case LTTNG_PROCESS_ATTR_USER_ID
:
948 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
949 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
952 ret_code
= utils_user_id_from_name(
953 value
->value
.user_name
, &uid
);
954 if (ret_code
!= LTTNG_OK
) {
957 integral_value
= (int) uid
;
959 integral_value
= (int) value
->value
.uid
;
962 case LTTNG_PROCESS_ATTR_GROUP_ID
:
963 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
964 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
967 ret_code
= utils_group_id_from_name(
968 value
->value
.group_name
, &gid
);
969 if (ret_code
!= LTTNG_OK
) {
972 integral_value
= (int) gid
;
974 integral_value
= (int) value
->value
.gid
;
978 ret_code
= LTTNG_ERR_INVALID
;
982 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
984 ret_code
= LTTNG_ERR_INVALID
;
988 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
989 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
991 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
992 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
994 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
995 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
997 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
999 ret_code
= LTTNG_ERR_UNK
;
1005 DBG("Kernel track %s %d for session id %" PRIu64
,
1006 lttng_process_attr_to_string(process_attr
),
1007 integral_value
, session
->id
);
1008 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1010 * Maintain a special case for the process ID process attribute
1011 * tracker as it was the only supported attribute prior to 2.12.
1013 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1015 ret
= kernctl_track_id(
1016 session
->fd
, process_attr
, integral_value
);
1019 ret_code
= LTTNG_OK
;
1023 kernel_wait_quiescent();
1025 /* kern-ctl error handling */
1028 ret_code
= LTTNG_OK
;
1031 ret_code
= LTTNG_ERR_INVALID
;
1034 ret_code
= LTTNG_ERR_NOMEM
;
1037 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1040 ret_code
= LTTNG_ERR_UNK
;
1044 /* Attempt to remove the value from the tracker. */
1045 status
= process_attr_tracker_inclusion_set_remove_value(
1047 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1048 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1049 lttng_process_attr_to_string(process_attr
),
1056 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1057 struct ltt_kernel_session
*session
,
1058 enum lttng_process_attr process_attr
,
1059 const struct process_attr_value
*value
)
1061 int ret
, integral_value
;
1062 enum lttng_error_code ret_code
;
1063 struct process_attr_tracker
*tracker
;
1064 enum process_attr_tracker_status status
;
1067 * Convert process attribute tracker value to the integral
1068 * representation required by the kern-ctl API.
1070 switch (process_attr
) {
1071 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1072 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1073 integral_value
= (int) value
->value
.pid
;
1075 case LTTNG_PROCESS_ATTR_USER_ID
:
1076 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1077 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1080 ret_code
= utils_user_id_from_name(
1081 value
->value
.user_name
, &uid
);
1082 if (ret_code
!= LTTNG_OK
) {
1085 integral_value
= (int) uid
;
1087 integral_value
= (int) value
->value
.uid
;
1090 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1091 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1092 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1095 ret_code
= utils_group_id_from_name(
1096 value
->value
.group_name
, &gid
);
1097 if (ret_code
!= LTTNG_OK
) {
1100 integral_value
= (int) gid
;
1102 integral_value
= (int) value
->value
.gid
;
1106 ret_code
= LTTNG_ERR_INVALID
;
1110 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1112 ret_code
= LTTNG_ERR_INVALID
;
1116 status
= process_attr_tracker_inclusion_set_remove_value(
1118 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1120 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1121 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1123 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1124 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1126 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1128 ret_code
= LTTNG_ERR_UNK
;
1134 DBG("Kernel track %s %d for session id %" PRIu64
,
1135 lttng_process_attr_to_string(process_attr
),
1136 integral_value
, session
->id
);
1137 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1139 * Maintain a special case for the process ID process attribute
1140 * tracker as it was the only supported attribute prior to 2.12.
1142 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1144 ret
= kernctl_untrack_id(
1145 session
->fd
, process_attr
, integral_value
);
1148 ret_code
= LTTNG_OK
;
1151 kernel_wait_quiescent();
1153 /* kern-ctl error handling */
1156 ret_code
= LTTNG_OK
;
1159 ret_code
= LTTNG_ERR_INVALID
;
1162 ret_code
= LTTNG_ERR_NOMEM
;
1165 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1168 ret_code
= LTTNG_ERR_UNK
;
1172 /* Attempt to add the value to the tracker. */
1173 status
= process_attr_tracker_inclusion_set_add_value(
1175 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1176 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1177 lttng_process_attr_to_string(process_attr
),
1185 * Create kernel metadata, open from the kernel tracer and add it to the
1188 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1191 struct ltt_kernel_metadata
*lkm
= NULL
;
1195 /* Allocate kernel metadata */
1196 lkm
= trace_kernel_create_metadata();
1201 /* Kernel tracer metadata creation */
1202 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1208 lkm
->key
= ++next_kernel_channel_key
;
1209 /* Prevent fd duplication after execlp() */
1210 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1212 PERROR("fcntl session fd");
1215 session
->metadata
= lkm
;
1217 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1222 trace_kernel_destroy_metadata(lkm
);
1228 * Start tracing session.
1230 int kernel_start_session(struct ltt_kernel_session
*session
)
1236 ret
= kernctl_start_session(session
->fd
);
1238 PERROR("ioctl start session");
1242 DBG("Kernel session started");
1251 * Make a kernel wait to make sure in-flight probe have completed.
1253 void kernel_wait_quiescent(void)
1256 int fd
= kernel_tracer_fd
;
1258 DBG("Kernel quiescent wait on %d", fd
);
1260 ret
= kernctl_wait_quiescent(fd
);
1262 PERROR("wait quiescent ioctl");
1263 ERR("Kernel quiescent wait failed");
1268 * Force flush buffer of metadata.
1270 int kernel_metadata_flush_buffer(int fd
)
1274 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1276 ret
= kernctl_buffer_flush(fd
);
1278 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1285 * Force flush buffer for channel.
1287 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1290 struct ltt_kernel_stream
*stream
;
1294 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1296 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1297 DBG("Flushing channel stream %d", stream
->fd
);
1298 ret
= kernctl_buffer_flush(stream
->fd
);
1301 ERR("Fail to flush buffer for stream %d (ret: %d)",
1310 * Stop tracing session.
1312 int kernel_stop_session(struct ltt_kernel_session
*session
)
1318 ret
= kernctl_stop_session(session
->fd
);
1323 DBG("Kernel session stopped");
1332 * Open stream of channel, register it to the kernel tracer and add it
1333 * to the stream list of the channel.
1335 * Note: given that the streams may appear in random order wrt CPU
1336 * number (e.g. cpu hotplug), the index value of the stream number in
1337 * the stream name is not necessarily linked to the CPU number.
1339 * Return the number of created stream. Else, a negative value.
1341 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1344 struct ltt_kernel_stream
*lks
;
1348 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1349 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1350 channel
->stream_count
);
1360 /* Prevent fd duplication after execlp() */
1361 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1363 PERROR("fcntl session fd");
1366 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1367 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1369 /* Add stream to channel stream list */
1370 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1371 channel
->stream_count
++;
1373 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1377 return channel
->stream_count
;
1384 * Open the metadata stream and set it to the kernel session.
1386 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1392 ret
= kernctl_create_stream(session
->metadata
->fd
);
1394 PERROR("kernel create metadata stream");
1398 DBG("Kernel metadata stream created (fd: %d)", ret
);
1399 session
->metadata_stream_fd
= ret
;
1400 /* Prevent fd duplication after execlp() */
1401 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1403 PERROR("fcntl session fd");
1413 * Get the event list from the kernel tracer and return the number of elements.
1415 ssize_t
kernel_list_events(struct lttng_event
**events
)
1419 size_t nbmem
, count
= 0;
1421 struct lttng_event
*elist
;
1425 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1427 PERROR("kernel tracepoint list");
1431 fp
= fdopen(fd
, "r");
1433 PERROR("kernel tracepoint list fdopen");
1438 * Init memory size counter
1439 * See kernel-ctl.h for explanation of this value
1441 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1442 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1443 if (elist
== NULL
) {
1444 PERROR("alloc list events");
1449 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1450 if (count
>= nbmem
) {
1451 struct lttng_event
*new_elist
;
1454 new_nbmem
= nbmem
<< 1;
1455 DBG("Reallocating event list from %zu to %zu bytes",
1457 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1458 if (new_elist
== NULL
) {
1459 PERROR("realloc list events");
1465 /* Zero the new memory */
1466 memset(new_elist
+ nbmem
, 0,
1467 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1471 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1472 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1473 elist
[count
].enabled
= -1;
1479 DBG("Kernel list events done (%zu events)", count
);
1481 ret
= fclose(fp
); /* closes both fp and fd */
1497 * Get kernel version and validate it.
1499 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1500 struct lttng_kernel_tracer_abi_version
*abi_version
)
1504 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1506 ERR("Failed to retrieve the lttng-modules version");
1510 /* Validate version */
1511 if (version
->major
!= VERSION_MAJOR
) {
1512 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1513 version
->major
, VERSION_MAJOR
);
1516 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1518 ERR("Failed to retrieve lttng-modules ABI version");
1521 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1522 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1523 abi_version
->major
, abi_version
->minor
,
1524 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1527 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1528 version
->major
, version
->minor
,
1529 abi_version
->major
, abi_version
->minor
);
1536 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1541 * Kernel work-arounds called at the start of sessiond main().
1543 int init_kernel_workarounds(void)
1549 * boot_id needs to be read once before being used concurrently
1550 * to deal with a Linux kernel race. A fix is proposed for
1551 * upstream, but the work-around is needed for older kernels.
1553 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1560 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1562 /* Ignore error, we don't really care */
1574 * Teardown of a kernel session, keeping data required by destroy notifiers.
1576 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1578 struct lttng_trace_chunk
*trace_chunk
;
1580 if (ksess
== NULL
) {
1581 DBG3("No kernel session when tearing down session");
1585 DBG("Tearing down kernel session");
1586 trace_chunk
= ksess
->current_trace_chunk
;
1589 * Destroy channels on the consumer if at least one FD has been sent and we
1590 * are in no output mode because the streams are in *no* monitor mode so we
1591 * have to send a command to clean them up or else they leaked.
1593 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1595 struct consumer_socket
*socket
;
1596 struct lttng_ht_iter iter
;
1598 /* For each consumer socket. */
1600 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1601 socket
, node
.node
) {
1602 struct ltt_kernel_channel
*chan
;
1604 /* For each channel, ask the consumer to destroy it. */
1605 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1606 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1608 /* Consumer is probably dead. Use next socket. */
1616 /* Close any relayd session */
1617 consumer_output_send_destroy_relayd(ksess
->consumer
);
1619 trace_kernel_destroy_session(ksess
);
1620 lttng_trace_chunk_put(trace_chunk
);
1623 /* Teardown of data required by destroy notifiers. */
1624 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1626 if (ksess
== NULL
) {
1629 trace_kernel_free_session(ksess
);
1633 * Destroy a kernel channel object. It does not do anything on the tracer side.
1635 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1637 struct ltt_kernel_session
*ksess
= NULL
;
1640 assert(kchan
->channel
);
1642 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1644 /* Update channel count of associated session. */
1645 if (kchan
->session
) {
1646 /* Keep pointer reference so we can update it after the destroy. */
1647 ksess
= kchan
->session
;
1650 trace_kernel_destroy_channel(kchan
);
1653 * At this point the kernel channel is not visible anymore. This is safe
1654 * since in order to work on a visible kernel session, the tracing session
1655 * lock (ltt_session.lock) MUST be acquired.
1658 ksess
->channel_count
--;
1663 * Take a snapshot for a given kernel session.
1665 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1667 enum lttng_error_code
kernel_snapshot_record(
1668 struct ltt_kernel_session
*ksess
,
1669 const struct consumer_output
*output
, int wait
,
1670 uint64_t nb_packets_per_stream
)
1672 int err
, ret
, saved_metadata_fd
;
1673 enum lttng_error_code status
= LTTNG_OK
;
1674 struct consumer_socket
*socket
;
1675 struct lttng_ht_iter iter
;
1676 struct ltt_kernel_metadata
*saved_metadata
;
1677 char *trace_path
= NULL
;
1678 size_t consumer_path_offset
= 0;
1681 assert(ksess
->consumer
);
1684 DBG("Kernel snapshot record started");
1686 /* Save current metadata since the following calls will change it. */
1687 saved_metadata
= ksess
->metadata
;
1688 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1692 ret
= kernel_open_metadata(ksess
);
1694 status
= LTTNG_ERR_KERN_META_FAIL
;
1698 ret
= kernel_open_metadata_stream(ksess
);
1700 status
= LTTNG_ERR_KERN_META_FAIL
;
1701 goto error_open_stream
;
1704 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1705 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1707 status
= LTTNG_ERR_INVALID
;
1710 /* Send metadata to consumer and snapshot everything. */
1711 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1712 socket
, node
.node
) {
1713 struct ltt_kernel_channel
*chan
;
1715 pthread_mutex_lock(socket
->lock
);
1716 /* This stream must not be monitored by the consumer. */
1717 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1718 pthread_mutex_unlock(socket
->lock
);
1720 status
= LTTNG_ERR_KERN_META_FAIL
;
1721 goto error_consumer
;
1724 /* For each channel, ask the consumer to snapshot it. */
1725 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1726 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1727 ksess
->uid
, ksess
->gid
,
1728 &trace_path
[consumer_path_offset
], wait
,
1729 nb_packets_per_stream
);
1730 if (status
!= LTTNG_OK
) {
1731 (void) kernel_consumer_destroy_metadata(socket
,
1733 goto error_consumer
;
1737 /* Snapshot metadata, */
1738 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1739 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1741 if (status
!= LTTNG_OK
) {
1742 goto error_consumer
;
1746 * The metadata snapshot is done, ask the consumer to destroy it since
1747 * it's not monitored on the consumer side.
1749 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1753 /* Close newly opened metadata stream. It's now on the consumer side. */
1754 err
= close(ksess
->metadata_stream_fd
);
1756 PERROR("close snapshot kernel");
1760 trace_kernel_destroy_metadata(ksess
->metadata
);
1762 /* Restore metadata state.*/
1763 ksess
->metadata
= saved_metadata
;
1764 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1771 * Get the syscall mask array from the kernel tracer.
1773 * Return 0 on success else a negative value. In both case, syscall_mask should
1776 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1778 assert(syscall_mask
);
1781 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1785 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1788 * Return 1 on success, 0 when feature is not supported, negative value in case
1791 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1793 int ret
= 0; // Not supported by default
1794 struct lttng_kernel_tracer_abi_version abi
;
1796 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1798 ERR("Failed to retrieve lttng-modules ABI version");
1803 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1805 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1817 * Check for the support of the packet sequence number via abi version number.
1819 * Return 1 on success, 0 when feature is not supported, negative value in case
1822 int kernel_supports_ring_buffer_packet_sequence_number(void)
1824 int ret
= 0; // Not supported by default
1825 struct lttng_kernel_tracer_abi_version abi
;
1827 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1829 ERR("Failed to retrieve lttng-modules ABI version");
1834 * Packet sequence number was introduced in LTTng 2.8,
1835 * lttng-modules ABI 2.1.
1837 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1849 * Rotate a kernel session.
1851 * Return LTTNG_OK on success or else an LTTng error code.
1853 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1856 enum lttng_error_code status
= LTTNG_OK
;
1857 struct consumer_socket
*socket
;
1858 struct lttng_ht_iter iter
;
1859 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1862 assert(ksess
->consumer
);
1864 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1865 session
->name
, session
->id
);
1870 * Note that this loop will end after one iteration given that there is
1871 * only one kernel consumer.
1873 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1874 socket
, node
.node
) {
1875 struct ltt_kernel_channel
*chan
;
1877 /* For each channel, ask the consumer to rotate it. */
1878 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1879 DBG("Rotate kernel channel %" PRIu64
", session %s",
1880 chan
->key
, session
->name
);
1881 ret
= consumer_rotate_channel(socket
, chan
->key
,
1882 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1883 /* is_metadata_channel */ false);
1885 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1891 * Rotate the metadata channel.
1893 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1894 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1895 /* is_metadata_channel */ true);
1897 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1907 enum lttng_error_code
kernel_create_channel_subdirectories(
1908 const struct ltt_kernel_session
*ksess
)
1910 enum lttng_error_code ret
= LTTNG_OK
;
1911 enum lttng_trace_chunk_status chunk_status
;
1914 assert(ksess
->current_trace_chunk
);
1917 * Create the index subdirectory which will take care
1918 * of implicitly creating the channel's path.
1920 chunk_status
= lttng_trace_chunk_create_subdirectory(
1921 ksess
->current_trace_chunk
,
1922 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1923 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1924 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1933 * Setup necessary data for kernel tracer action.
1936 int init_kernel_tracer(void)
1939 enum lttng_error_code error_code_ret
;
1940 bool is_root
= !getuid();
1942 /* Modprobe lttng kernel modules */
1943 ret
= modprobe_lttng_control();
1948 /* Open debugfs lttng */
1949 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1950 if (kernel_tracer_fd
< 0) {
1951 DBG("Failed to open %s", module_proc_lttng
);
1955 /* Validate kernel version */
1956 ret
= kernel_validate_version(&kernel_tracer_version
,
1957 &kernel_tracer_abi_version
);
1962 ret
= modprobe_lttng_data();
1967 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1972 WARN("Kernel tracer does not support buffer monitoring. "
1973 "The monitoring timer of channels in the kernel domain "
1974 "will be set to 0 (disabled).");
1977 ret
= kernel_create_trigger_group(&kernel_tracer_trigger_group_fd
);
1979 /* TODO: error handling if it is not supported etc. */
1980 WARN("Failed trigger group creation");
1981 kernel_tracer_trigger_group_fd
= -1;
1982 /* This is not fatal */
1985 error_code_ret
= kernel_create_trigger_group_notification_fd(
1986 &kernel_tracer_trigger_group_notification_fd
);
1987 if (error_code_ret
!= LTTNG_OK
) {
1991 trigger_error_accounting_register_kernel(kernel_tracer_trigger_group_fd
);
1997 kernel_tracer_token_ht
= cds_lfht_new(DEFAULT_HT_SIZE
, 1, 0,
1998 CDS_LFHT_AUTO_RESIZE
|CDS_LFHT_ACCOUNTING
, NULL
);
1999 if (!kernel_tracer_token_ht
) {
2003 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
2004 DBG("Kernel tracer trigger group fd %d", kernel_tracer_trigger_group_fd
);
2005 DBG("Kernel tracer trigger group notification fd %d", kernel_tracer_trigger_group_notification_fd
);
2007 ret
= syscall_init_table(kernel_tracer_fd
);
2009 ERR("Unable to populate syscall table. Syscall tracing won't "
2010 "work for this session daemon.");
2016 modprobe_remove_lttng_control();
2017 ret
= close(kernel_tracer_fd
);
2021 kernel_tracer_fd
= -1;
2022 return LTTNG_ERR_KERN_VERSION
;
2025 ret
= close(kernel_tracer_fd
);
2031 modprobe_remove_lttng_control();
2034 WARN("No kernel tracer available");
2035 kernel_tracer_fd
= -1;
2037 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2039 return LTTNG_ERR_KERN_NA
;
2044 void cleanup_kernel_tracer(void)
2047 struct cds_lfht_iter iter
;
2049 struct ltt_kernel_token_event_rule
*rule
= NULL
;
2050 cds_lfht_for_each_entry(kernel_tracer_token_ht
, &iter
, rule
, ht_node
) {
2051 kernel_disable_token_event_rule(rule
);
2052 trace_kernel_destroy_token_event_rule(rule
);
2053 cds_lfht_del(kernel_tracer_token_ht
, &rule
->ht_node
);
2056 DBG2("Closing kernel trigger group notification fd");
2057 if (kernel_tracer_trigger_group_notification_fd
>= 0) {
2059 ret
= notification_thread_command_remove_application(
2060 notification_thread_handle
,
2061 kernel_tracer_trigger_group_notification_fd
);
2062 if (ret
!= LTTNG_OK
) {
2063 ERR("Failed to remove kernel trigger notification from notification thread");
2065 ret
= close(kernel_tracer_trigger_group_notification_fd
);
2069 kernel_tracer_trigger_group_notification_fd
= -1;
2072 /* TODO: do we iterate over the list to remove all token? */
2073 DBG2("Closing kernel trigger group fd");
2074 if (kernel_tracer_trigger_group_fd
>= 0) {
2075 ret
= close(kernel_tracer_trigger_group_fd
);
2079 kernel_tracer_trigger_group_fd
= -1;
2082 DBG2("Closing kernel fd");
2083 if (kernel_tracer_fd
>= 0) {
2084 ret
= close(kernel_tracer_fd
);
2088 kernel_tracer_fd
= -1;
2091 DBG("Unloading kernel modules");
2092 modprobe_remove_lttng_all();
2093 free(syscall_table
);
2097 bool kernel_tracer_is_initialized(void)
2099 return kernel_tracer_fd
>= 0;
2103 * Clear a kernel session.
2105 * Return LTTNG_OK on success or else an LTTng error code.
2107 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2110 enum lttng_error_code status
= LTTNG_OK
;
2111 struct consumer_socket
*socket
;
2112 struct lttng_ht_iter iter
;
2113 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2116 assert(ksess
->consumer
);
2118 DBG("Clear kernel session %s (session %" PRIu64
")",
2119 session
->name
, session
->id
);
2123 if (ksess
->active
) {
2124 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2125 status
= LTTNG_ERR_FATAL
;
2130 * Note that this loop will end after one iteration given that there is
2131 * only one kernel consumer.
2133 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2134 socket
, node
.node
) {
2135 struct ltt_kernel_channel
*chan
;
2137 /* For each channel, ask the consumer to clear it. */
2138 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2139 DBG("Clear kernel channel %" PRIu64
", session %s",
2140 chan
->key
, session
->name
);
2141 ret
= consumer_clear_channel(socket
, chan
->key
);
2147 if (!ksess
->metadata
) {
2149 * Nothing to do for the metadata.
2150 * This is a snapshot session.
2151 * The metadata is genererated on the fly.
2157 * Clear the metadata channel.
2158 * Metadata channel is not cleared per se but we still need to
2159 * perform a rotation operation on it behind the scene.
2161 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2170 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2171 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2174 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2182 enum lttng_error_code
kernel_create_trigger_group_notification_fd(
2183 int *trigger_group_notification_fd
)
2185 enum lttng_error_code error_code_ret
;
2186 int local_fd
= -1, ret
;
2188 assert(trigger_group_notification_fd
);
2190 ret
= kernctl_create_trigger_group_notification_fd(kernel_tracer_trigger_group_fd
);
2192 PERROR("ioctl kernel create trigger group");
2193 error_code_ret
= LTTNG_ERR_TRIGGER_GROUP_NOTIFICATION_FD
;
2200 /* Prevent fd duplication after execlp() */
2201 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2203 PERROR("fcntl session fd");
2204 error_code_ret
= LTTNG_ERR_TRIGGER_GROUP_NOTIFICATION_FD
;
2208 DBG("Kernel trigger group notification created (fd: %d)",
2210 error_code_ret
= LTTNG_OK
;
2211 *trigger_group_notification_fd
= local_fd
;
2214 return error_code_ret
;
2217 enum lttng_error_code
kernel_create_trigger_group_error_counter(
2218 int *error_counter_fd
, size_t nb_bucket
)
2220 enum lttng_error_code error_code_ret
;
2221 int local_fd
= -1, ret
;
2222 struct lttng_kernel_counter_conf error_counter_conf
;
2224 assert(error_counter_fd
);
2226 error_counter_conf
.arithmetic
= LTTNG_KERNEL_COUNTER_ARITHMETIC_MODULAR
;
2227 error_counter_conf
.bitness
= LTTNG_KERNEL_COUNTER_BITNESS_64BITS
;
2228 error_counter_conf
.global_sum_step
= 0;
2229 error_counter_conf
.number_dimensions
= 1;
2230 error_counter_conf
.dimensions
[0].size
= nb_bucket
;
2231 error_counter_conf
.dimensions
[0].has_underflow
= false;
2232 error_counter_conf
.dimensions
[0].has_overflow
= false;
2234 ret
= kernctl_create_trigger_group_error_counter(
2235 kernel_tracer_trigger_group_fd
, &error_counter_conf
);
2237 PERROR("ioctl kernel create trigger group error counter");
2238 error_code_ret
= LTTNG_ERR_TRIGGER_GROUP_ERROR_COUNTER
;
2245 /* Prevent fd duplication after execlp() */
2246 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2248 error_code_ret
= LTTNG_ERR_TRIGGER_GROUP_ERROR_COUNTER
;
2249 PERROR("fcntl session fd");
2252 DBG("Kernel trigger group error counter (fd: %d)",
2254 error_code_ret
= LTTNG_OK
;
2257 *error_counter_fd
= local_fd
;
2258 return error_code_ret
;
2261 enum lttng_error_code
kernel_destroy_trigger_group_notification_fd(
2262 int trigger_group_notification_fd
)
2264 enum lttng_error_code ret
= LTTNG_OK
;
2265 DBG("Closing trigger group notification fd %d", trigger_group_notification_fd
);
2266 if (trigger_group_notification_fd
>= 0) {
2267 ret
= close(trigger_group_notification_fd
);
2276 unsigned long hash_trigger(struct lttng_trigger
*trigger
)
2278 const struct lttng_condition
*condition
=
2279 lttng_trigger_get_const_condition(trigger
);
2280 return lttng_condition_hash(condition
);
2284 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2286 struct ltt_kernel_token_event_rule
*token
;
2287 const struct lttng_trigger
*trigger
= key
;
2289 token
= caa_container_of(node
, struct ltt_kernel_token_event_rule
, ht_node
);
2291 return lttng_trigger_is_equal(trigger
, token
->trigger
);
2294 static enum lttng_error_code
kernel_create_token_event_rule(struct lttng_trigger
*trigger
,
2295 const struct lttng_credentials
*creds
, uint64_t token
)
2297 int err
, fd
, ret
= 0;
2298 enum lttng_error_code error_code_ret
;
2299 struct ltt_kernel_token_event_rule
*event
;
2300 struct lttng_kernel_trigger kernel_trigger
= {};
2301 unsigned int capture_bytecode_count
= 0;
2302 struct lttng_condition
*condition
= NULL
;
2303 struct lttng_event_rule
*event_rule
= NULL
;
2307 condition
= lttng_trigger_get_condition(trigger
);
2309 assert(lttng_condition_get_type(condition
) == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT
);
2311 lttng_condition_event_rule_get_rule_mutable(condition
, &event_rule
);
2313 assert(lttng_event_rule_get_type(event_rule
) != LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2315 error_code_ret
= trace_kernel_create_token_event_rule(trigger
, token
,
2316 lttng_trigger_get_error_counter_index(trigger
), &event
);
2317 if (error_code_ret
!= LTTNG_OK
) {
2321 trace_kernel_init_trigger_from_event_rule(event_rule
, &kernel_trigger
);
2322 kernel_trigger
.id
= event
->token
;
2323 kernel_trigger
.error_counter_idx
= lttng_trigger_get_error_counter_index(trigger
);
2325 fd
= kernctl_create_trigger(kernel_tracer_trigger_group_fd
, &kernel_trigger
);
2329 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2332 WARN("Trigger type not implemented");
2333 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2336 WARN("Event %s not found!", kernel_trigger
.name
);
2337 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2340 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2341 PERROR("create trigger ioctl");
2347 /* Prevent fd duplication after execlp() */
2348 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
2350 PERROR("fcntl session fd");
2353 if (event
->filter
) {
2354 err
= kernctl_filter(event
->fd
, event
->filter
);
2358 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2361 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2368 if (lttng_event_rule_get_type(event_rule
) ==
2369 LTTNG_EVENT_RULE_TYPE_UPROBE
) {
2370 ret
= userspace_probe_event_rule_add_callsites(
2371 event_rule
, creds
, event
->fd
);
2373 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2374 goto add_callsite_error
;
2378 /* Set the capture bytecode */
2379 capture_bytecode_count
= lttng_trigger_get_capture_bytecode_count(trigger
);
2380 for (unsigned int i
= 0; i
< capture_bytecode_count
; i
++) {
2381 const struct lttng_bytecode
*capture_bytecode
= lttng_trigger_get_capture_bytecode_at_index(trigger
, i
);
2382 ret
= kernctl_capture(event
->fd
, capture_bytecode
);
2384 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2389 err
= kernctl_enable(event
->fd
);
2393 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2396 PERROR("enable kernel trigger");
2397 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2403 /* Add trigger to kernel token mapping in the hashtable. */
2404 cds_lfht_add(kernel_tracer_token_ht
, hash_trigger(trigger
),
2407 DBG("Trigger %s created (fd: %d)", kernel_trigger
.name
, event
->fd
);
2417 closeret
= close(event
->fd
);
2419 PERROR("close event fd");
2425 return error_code_ret
;
2428 enum lttng_error_code
kernel_register_trigger(struct lttng_trigger
*trigger
, const struct lttng_credentials
*cmd_creds
)
2430 enum lttng_error_code ret
;
2431 struct lttng_condition
*condition
;
2432 struct lttng_event_rule
*event_rule
;
2435 /* TODO error handling */
2438 pthread_mutex_lock(¬ification_trigger_tokens_ht_lock
);
2440 /* TODO: error checking and type checking */
2441 token
= lttng_trigger_get_tracer_token(trigger
);
2442 condition
= lttng_trigger_get_condition(trigger
);
2443 (void) lttng_condition_event_rule_get_rule_mutable(condition
, &event_rule
);
2445 assert(lttng_event_rule_get_domain_type(event_rule
) == LTTNG_DOMAIN_KERNEL
);
2447 ret
= kernel_create_token_event_rule(trigger
, cmd_creds
, token
);
2448 if (ret
!= LTTNG_OK
) {
2449 ERR("Failed to create kernel trigger token.");
2453 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
2457 enum lttng_error_code
kernel_unregister_trigger(struct lttng_trigger
*trigger
)
2459 struct ltt_kernel_token_event_rule
*token_event_rule_element
;
2460 struct cds_lfht_node
*node
;
2461 struct cds_lfht_iter iter
;
2462 enum lttng_error_code error_code_ret
;
2466 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
2468 cds_lfht_lookup(kernel_tracer_token_ht
, hash_trigger(trigger
),
2469 match_trigger
, trigger
, &iter
);
2470 node
= cds_lfht_iter_get_node(&iter
);
2472 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2476 token_event_rule_element
= caa_container_of(node
,
2477 struct ltt_kernel_token_event_rule
, ht_node
);
2479 ret
= kernel_disable_token_event_rule(token_event_rule_element
);
2481 error_code_ret
= LTTNG_ERR_FATAL
;
2485 trace_kernel_destroy_token_event_rule(token_event_rule_element
);
2488 pthread_mutex_unlock(¬ification_trigger_tokens_ht_lock
);
2490 error_code_ret
= LTTNG_OK
;
2493 return error_code_ret
;
2496 int kernel_get_notification_fd(void)
2498 return kernel_tracer_trigger_group_notification_fd
;