2 * Copyright (C) 2011 EfficiOS Inc.
4 * SPDX-License-Identifier: GPL-2.0-only
15 #include <sys/types.h>
17 #include <common/common.hpp>
18 #include <common/hashtable/utils.hpp>
19 #include <common/trace-chunk.hpp>
20 #include <common/kernel-ctl/kernel-ctl.hpp>
21 #include <common/kernel-ctl/kernel-ioctl.hpp>
22 #include <common/sessiond-comm/sessiond-comm.hpp>
23 #include <common/tracker.hpp>
24 #include <common/utils.hpp>
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.hpp>
31 #include <lttng/condition/event-rule-matches.h>
32 #include <lttng/condition/event-rule-matches-internal.hpp>
33 #include <lttng/event-rule/event-rule.h>
34 #include <lttng/event-rule/event-rule-internal.hpp>
35 #include <lttng/event-rule/kernel-uprobe-internal.hpp>
37 #include "event-notifier-error-accounting.hpp"
38 #include "lttng-sessiond.hpp"
39 #include "lttng-syscall.hpp"
40 #include "condition-internal.hpp"
41 #include "consumer.hpp"
43 #include "kernel-consumer.hpp"
44 #include "kern-modules.hpp"
45 #include "sessiond-config.hpp"
48 #include "modprobe.hpp"
49 #include "tracker.hpp"
50 #include "notification-thread-commands.hpp"
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_event_notifier_group_fd
= -1;
62 static int kernel_tracer_event_notifier_group_notification_fd
= -1;
63 static struct cds_lfht
*kernel_token_to_event_notifier_rule_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
;
118 LTTNG_ASSERT(session
);
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
);
167 ret
= kernctl_session_set_trace_format(lks
->fd
, *session
->trace_format
);
169 WARN("Could not set kernel session trace format %" PRIu64
" name: %s", session
->id
,
176 trace_kernel_destroy_session(lks
);
177 trace_kernel_free_session(lks
);
183 * Create a kernel channel, register it to the kernel tracer and add it to the
186 int kernel_create_channel(struct ltt_kernel_session
*session
,
187 struct lttng_channel
*chan
)
190 struct ltt_kernel_channel
*lkc
;
192 LTTNG_ASSERT(session
);
195 /* Allocate kernel channel */
196 lkc
= trace_kernel_create_channel(chan
);
201 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
202 chan
->name
, lkc
->channel
->attr
.overwrite
,
203 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
204 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
205 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
207 /* Kernel tracer channel creation */
208 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
210 PERROR("ioctl kernel create channel");
214 /* Setup the channel fd */
216 /* Prevent fd duplication after execlp() */
217 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
219 PERROR("fcntl session fd");
222 /* Add channel to session */
223 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
224 session
->channel_count
++;
225 lkc
->session
= session
;
226 lkc
->key
= ++next_kernel_channel_key
;
228 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
229 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
242 * Create a kernel event notifier group, register it to the kernel tracer and
243 * add it to the kernel session.
245 static int kernel_create_event_notifier_group(int *event_notifier_group_fd
)
250 LTTNG_ASSERT(event_notifier_group_fd
);
252 /* Kernel event notifier group creation. */
253 ret
= kernctl_create_event_notifier_group(kernel_tracer_fd
);
255 PERROR("Failed to create kernel event notifier group");
262 /* Prevent fd duplication after execlp(). */
263 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
265 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
270 DBG("Created kernel event notifier group: fd = %d", local_fd
);
271 *event_notifier_group_fd
= local_fd
;
276 ret
= close(local_fd
);
278 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
287 * Compute the offset of the instrumentation byte in the binary based on the
288 * function probe location using the ELF lookup method.
290 * Returns 0 on success and set the offset out parameter to the offset of the
292 * Returns -1 on error
295 int extract_userspace_probe_offset_function_elf(
296 const struct lttng_userspace_probe_location
*probe_location
,
297 uid_t uid
, gid_t gid
, uint64_t *offset
)
301 const char *symbol
= NULL
;
302 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
303 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
305 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
306 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
308 lookup
= lttng_userspace_probe_location_get_lookup_method(
316 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
318 LTTNG_ASSERT(lookup_method_type
==
319 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
321 symbol
= lttng_userspace_probe_location_function_get_function_name(
328 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
334 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, uid
, gid
, offset
);
336 DBG("userspace probe offset calculation failed for "
337 "function %s", symbol
);
341 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
347 * Compute the offsets of the instrumentation bytes in the binary based on the
348 * tracepoint probe location using the SDT lookup method. This function
349 * allocates the offsets buffer, the caller must free it.
351 * Returns 0 on success and set the offset out parameter to the offsets of the
353 * Returns -1 on error.
356 int extract_userspace_probe_offset_tracepoint_sdt(
357 const struct lttng_userspace_probe_location
*probe_location
,
358 uid_t uid
, gid_t gid
, uint64_t **offsets
,
359 uint32_t *offsets_count
)
361 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
362 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
363 const char *probe_name
= NULL
, *provider_name
= NULL
;
367 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location
) ==
368 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
370 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
377 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
379 LTTNG_ASSERT(lookup_method_type
==
380 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
383 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
390 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
392 if (!provider_name
) {
397 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
403 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
404 uid
, gid
, offsets
, offsets_count
);
406 DBG("userspace probe offset calculation failed for sdt "
407 "probe %s:%s", provider_name
, probe_name
);
411 if (*offsets_count
== 0) {
412 DBG("no userspace probe offset found");
416 DBG("%u userspace probe SDT offsets found for %s:%s at:",
417 *offsets_count
, provider_name
, probe_name
);
418 for (i
= 0; i
< *offsets_count
; i
++) {
419 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
426 int userspace_probe_add_callsite(
427 const struct lttng_userspace_probe_location
*location
,
428 uid_t uid
, gid_t gid
, int fd
)
430 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
431 enum lttng_userspace_probe_location_lookup_method_type type
;
434 lookup_method
= lttng_userspace_probe_location_get_lookup_method(location
);
435 if (!lookup_method
) {
440 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
442 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
444 struct lttng_kernel_abi_event_callsite callsite
;
447 ret
= extract_userspace_probe_offset_function_elf(location
,
450 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
454 callsite
.u
.uprobe
.offset
= offset
;
455 ret
= kernctl_add_callsite(fd
, &callsite
);
457 WARN("Failed to add callsite to ELF userspace probe.");
458 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
463 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
466 uint64_t *offsets
= NULL
;
467 uint32_t offsets_count
;
468 struct lttng_kernel_abi_event_callsite callsite
;
471 * This call allocates the offsets buffer. This buffer must be freed
474 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
,
475 uid
, gid
, &offsets
, &offsets_count
);
477 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
480 for (i
= 0; i
< offsets_count
; i
++) {
481 callsite
.u
.uprobe
.offset
= offsets
[i
];
482 ret
= kernctl_add_callsite(fd
, &callsite
);
484 WARN("Failed to add callsite to SDT userspace probe");
485 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
494 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
502 * Extract the offsets of the instrumentation point for the different lookup
506 int userspace_probe_event_add_callsites(struct lttng_event
*ev
,
507 struct ltt_kernel_session
*session
, int fd
)
510 const struct lttng_userspace_probe_location
*location
= NULL
;
513 LTTNG_ASSERT(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
515 location
= lttng_event_get_userspace_probe_location(ev
);
521 ret
= userspace_probe_add_callsite(location
, session
->uid
, session
->gid
,
524 WARN("Failed to add callsite to userspace probe event '%s'",
533 * Extract the offsets of the instrumentation point for the different look-up
536 static int userspace_probe_event_rule_add_callsites(
537 const struct lttng_event_rule
*rule
,
538 const struct lttng_credentials
*creds
,
542 enum lttng_event_rule_status status
;
543 enum lttng_event_rule_type event_rule_type
;
544 const struct lttng_userspace_probe_location
*location
= NULL
;
549 event_rule_type
= lttng_event_rule_get_type(rule
);
550 LTTNG_ASSERT(event_rule_type
== LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
);
552 status
= lttng_event_rule_kernel_uprobe_get_location(rule
, &location
);
553 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
|| !location
) {
558 ret
= userspace_probe_add_callsite(location
,
559 lttng_credentials_get_uid(creds
),
560 lttng_credentials_get_gid(creds
), fd
);
562 WARN("Failed to add callsite to user space probe object: fd = %d",
571 * Create a kernel event, enable it to the kernel tracer and add it to the
572 * channel event list of the kernel session.
573 * We own filter_expression and filter.
575 int kernel_create_event(struct lttng_event
*ev
,
576 struct ltt_kernel_channel
*channel
,
577 char *filter_expression
,
578 struct lttng_bytecode
*filter
)
581 enum lttng_error_code ret
;
582 struct ltt_kernel_event
*event
;
585 LTTNG_ASSERT(channel
);
587 /* We pass ownership of filter_expression and filter */
588 ret
= trace_kernel_create_event(ev
, filter_expression
,
590 if (ret
!= LTTNG_OK
) {
594 fd
= kernctl_create_event(channel
->fd
, event
->event
);
598 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
601 WARN("Event type not implemented");
602 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
605 WARN("Event %s not found!", ev
->name
);
606 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
609 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
610 PERROR("create event ioctl");
615 event
->type
= ev
->type
;
617 /* Prevent fd duplication after execlp() */
618 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
620 PERROR("fcntl session fd");
624 err
= kernctl_filter(event
->fd
, filter
);
628 ret
= LTTNG_ERR_FILTER_NOMEM
;
631 ret
= LTTNG_ERR_FILTER_INVAL
;
638 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
639 ret
= (lttng_error_code
) userspace_probe_event_add_callsites(ev
, channel
->session
,
642 goto add_callsite_error
;
646 err
= kernctl_enable(event
->fd
);
650 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
653 PERROR("enable kernel event");
654 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
660 /* Add event to event list */
661 cds_list_add(&event
->list
, &channel
->events_list
.head
);
662 channel
->event_count
++;
664 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
674 closeret
= close(event
->fd
);
676 PERROR("close event fd");
686 * Disable a kernel channel.
688 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
694 ret
= kernctl_disable(chan
->fd
);
696 PERROR("disable chan ioctl");
701 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
702 chan
->channel
->name
, chan
->fd
, chan
->key
);
711 * Enable a kernel channel.
713 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
719 ret
= kernctl_enable(chan
->fd
);
720 if (ret
< 0 && ret
!= -EEXIST
) {
721 PERROR("Enable kernel chan");
726 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
727 chan
->channel
->name
, chan
->fd
, chan
->key
);
736 * Enable a kernel event.
738 int kernel_enable_event(struct ltt_kernel_event
*event
)
744 ret
= kernctl_enable(event
->fd
);
748 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
751 PERROR("enable kernel event");
758 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
767 * Disable a kernel event.
769 int kernel_disable_event(struct ltt_kernel_event
*event
)
775 ret
= kernctl_disable(event
->fd
);
777 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
778 event
->event
->name
, event
->fd
);
783 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
792 * Disable a kernel event notifier.
795 int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule
*event
)
802 cds_lfht_del(kernel_token_to_event_notifier_rule_ht
, &event
->ht_node
);
805 ret
= kernctl_disable(event
->fd
);
807 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64
,
808 event
->fd
, event
->token
);
813 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64
,
814 event
->fd
, event
->token
);
821 struct process_attr_tracker
*_kernel_get_process_attr_tracker(
822 struct ltt_kernel_session
*session
,
823 enum lttng_process_attr process_attr
)
825 switch (process_attr
) {
826 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
827 return session
->tracker_pid
;
828 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
829 return session
->tracker_vpid
;
830 case LTTNG_PROCESS_ATTR_USER_ID
:
831 return session
->tracker_uid
;
832 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
833 return session
->tracker_vuid
;
834 case LTTNG_PROCESS_ATTR_GROUP_ID
:
835 return session
->tracker_gid
;
836 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
837 return session
->tracker_vgid
;
843 const struct process_attr_tracker
*kernel_get_process_attr_tracker(
844 struct ltt_kernel_session
*session
,
845 enum lttng_process_attr process_attr
)
847 return (const struct process_attr_tracker
*)
848 _kernel_get_process_attr_tracker(session
, process_attr
);
851 enum lttng_error_code
kernel_process_attr_tracker_set_tracking_policy(
852 struct ltt_kernel_session
*session
,
853 enum lttng_process_attr process_attr
,
854 enum lttng_tracking_policy policy
)
857 enum lttng_error_code ret_code
= LTTNG_OK
;
858 struct process_attr_tracker
*tracker
=
859 _kernel_get_process_attr_tracker(session
, process_attr
);
860 enum lttng_tracking_policy previous_policy
;
863 ret_code
= LTTNG_ERR_INVALID
;
867 previous_policy
= process_attr_tracker_get_tracking_policy(tracker
);
868 ret
= process_attr_tracker_set_tracking_policy(tracker
, policy
);
870 ret_code
= LTTNG_ERR_UNK
;
874 if (previous_policy
== policy
) {
879 case LTTNG_TRACKING_POLICY_INCLUDE_ALL
:
880 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
882 * Maintain a special case for the process ID process
883 * attribute tracker as it was the only supported
884 * attribute prior to 2.12.
886 ret
= kernctl_track_pid(session
->fd
, -1);
888 ret
= kernctl_track_id(session
->fd
, process_attr
, -1);
891 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL
:
892 case LTTNG_TRACKING_POLICY_INCLUDE_SET
:
894 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
896 * Maintain a special case for the process ID process
897 * attribute tracker as it was the only supported
898 * attribute prior to 2.12.
900 ret
= kernctl_untrack_pid(session
->fd
, -1);
902 ret
= kernctl_untrack_id(session
->fd
, process_attr
, -1);
908 /* kern-ctl error handling */
914 ret_code
= LTTNG_ERR_INVALID
;
917 ret_code
= LTTNG_ERR_NOMEM
;
920 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
923 ret_code
= LTTNG_ERR_UNK
;
930 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_add_value(
931 struct ltt_kernel_session
*session
,
932 enum lttng_process_attr process_attr
,
933 const struct process_attr_value
*value
)
935 int ret
, integral_value
;
936 enum lttng_error_code ret_code
;
937 struct process_attr_tracker
*tracker
;
938 enum process_attr_tracker_status status
;
941 * Convert process attribute tracker value to the integral
942 * representation required by the kern-ctl API.
944 switch (process_attr
) {
945 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
946 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
947 integral_value
= (int) value
->value
.pid
;
949 case LTTNG_PROCESS_ATTR_USER_ID
:
950 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
951 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
954 ret_code
= utils_user_id_from_name(
955 value
->value
.user_name
, &uid
);
956 if (ret_code
!= LTTNG_OK
) {
959 integral_value
= (int) uid
;
961 integral_value
= (int) value
->value
.uid
;
964 case LTTNG_PROCESS_ATTR_GROUP_ID
:
965 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
966 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
969 ret_code
= utils_group_id_from_name(
970 value
->value
.group_name
, &gid
);
971 if (ret_code
!= LTTNG_OK
) {
974 integral_value
= (int) gid
;
976 integral_value
= (int) value
->value
.gid
;
980 ret_code
= LTTNG_ERR_INVALID
;
984 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
986 ret_code
= LTTNG_ERR_INVALID
;
990 status
= process_attr_tracker_inclusion_set_add_value(tracker
, value
);
991 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
993 case PROCESS_ATTR_TRACKER_STATUS_EXISTS
:
994 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
996 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
997 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
999 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1001 ret_code
= LTTNG_ERR_UNK
;
1007 DBG("Kernel track %s %d for session id %" PRIu64
,
1008 lttng_process_attr_to_string(process_attr
),
1009 integral_value
, session
->id
);
1010 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1012 * Maintain a special case for the process ID process attribute
1013 * tracker as it was the only supported attribute prior to 2.12.
1015 ret
= kernctl_track_pid(session
->fd
, integral_value
);
1017 ret
= kernctl_track_id(
1018 session
->fd
, process_attr
, integral_value
);
1021 ret_code
= LTTNG_OK
;
1025 kernel_wait_quiescent();
1027 /* kern-ctl error handling */
1030 ret_code
= LTTNG_OK
;
1033 ret_code
= LTTNG_ERR_INVALID
;
1036 ret_code
= LTTNG_ERR_NOMEM
;
1039 ret_code
= LTTNG_ERR_PROCESS_ATTR_EXISTS
;
1042 ret_code
= LTTNG_ERR_UNK
;
1046 /* Attempt to remove the value from the tracker. */
1047 status
= process_attr_tracker_inclusion_set_remove_value(
1049 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1050 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1051 lttng_process_attr_to_string(process_attr
),
1058 enum lttng_error_code
kernel_process_attr_tracker_inclusion_set_remove_value(
1059 struct ltt_kernel_session
*session
,
1060 enum lttng_process_attr process_attr
,
1061 const struct process_attr_value
*value
)
1063 int ret
, integral_value
;
1064 enum lttng_error_code ret_code
;
1065 struct process_attr_tracker
*tracker
;
1066 enum process_attr_tracker_status status
;
1069 * Convert process attribute tracker value to the integral
1070 * representation required by the kern-ctl API.
1072 switch (process_attr
) {
1073 case LTTNG_PROCESS_ATTR_PROCESS_ID
:
1074 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID
:
1075 integral_value
= (int) value
->value
.pid
;
1077 case LTTNG_PROCESS_ATTR_USER_ID
:
1078 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID
:
1079 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME
) {
1082 ret_code
= utils_user_id_from_name(
1083 value
->value
.user_name
, &uid
);
1084 if (ret_code
!= LTTNG_OK
) {
1087 integral_value
= (int) uid
;
1089 integral_value
= (int) value
->value
.uid
;
1092 case LTTNG_PROCESS_ATTR_GROUP_ID
:
1093 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID
:
1094 if (value
->type
== LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME
) {
1097 ret_code
= utils_group_id_from_name(
1098 value
->value
.group_name
, &gid
);
1099 if (ret_code
!= LTTNG_OK
) {
1102 integral_value
= (int) gid
;
1104 integral_value
= (int) value
->value
.gid
;
1108 ret_code
= LTTNG_ERR_INVALID
;
1112 tracker
= _kernel_get_process_attr_tracker(session
, process_attr
);
1114 ret_code
= LTTNG_ERR_INVALID
;
1118 status
= process_attr_tracker_inclusion_set_remove_value(
1120 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1122 case PROCESS_ATTR_TRACKER_STATUS_MISSING
:
1123 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1125 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY
:
1126 ret_code
= LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY
;
1128 case PROCESS_ATTR_TRACKER_STATUS_ERROR
:
1130 ret_code
= LTTNG_ERR_UNK
;
1136 DBG("Kernel track %s %d for session id %" PRIu64
,
1137 lttng_process_attr_to_string(process_attr
),
1138 integral_value
, session
->id
);
1139 if (process_attr
== LTTNG_PROCESS_ATTR_PROCESS_ID
) {
1141 * Maintain a special case for the process ID process attribute
1142 * tracker as it was the only supported attribute prior to 2.12.
1144 ret
= kernctl_untrack_pid(session
->fd
, integral_value
);
1146 ret
= kernctl_untrack_id(
1147 session
->fd
, process_attr
, integral_value
);
1150 ret_code
= LTTNG_OK
;
1153 kernel_wait_quiescent();
1155 /* kern-ctl error handling */
1158 ret_code
= LTTNG_OK
;
1161 ret_code
= LTTNG_ERR_INVALID
;
1164 ret_code
= LTTNG_ERR_NOMEM
;
1167 ret_code
= LTTNG_ERR_PROCESS_ATTR_MISSING
;
1170 ret_code
= LTTNG_ERR_UNK
;
1174 /* Attempt to add the value to the tracker. */
1175 status
= process_attr_tracker_inclusion_set_add_value(
1177 if (status
!= PROCESS_ATTR_TRACKER_STATUS_OK
) {
1178 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
1179 lttng_process_attr_to_string(process_attr
),
1187 * Create kernel metadata, open from the kernel tracer and add it to the
1190 int kernel_open_metadata(struct ltt_kernel_session
*session
)
1193 struct ltt_kernel_metadata
*lkm
= NULL
;
1195 LTTNG_ASSERT(session
);
1197 /* Allocate kernel metadata */
1198 lkm
= trace_kernel_create_metadata();
1203 /* Kernel tracer metadata creation */
1204 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
1210 lkm
->key
= ++next_kernel_channel_key
;
1211 /* Prevent fd duplication after execlp() */
1212 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
1214 PERROR("fcntl session fd");
1217 session
->metadata
= lkm
;
1219 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
1224 trace_kernel_destroy_metadata(lkm
);
1230 * Start tracing session.
1232 int kernel_start_session(struct ltt_kernel_session
*session
)
1236 LTTNG_ASSERT(session
);
1238 ret
= kernctl_start_session(session
->fd
);
1240 PERROR("ioctl start session");
1244 DBG("Kernel session started");
1253 * Make a kernel wait to make sure in-flight probe have completed.
1255 void kernel_wait_quiescent(void)
1258 int fd
= kernel_tracer_fd
;
1260 DBG("Kernel quiescent wait on %d", fd
);
1262 ret
= kernctl_wait_quiescent(fd
);
1264 PERROR("wait quiescent ioctl");
1265 ERR("Kernel quiescent wait failed");
1270 * Force flush buffer of metadata.
1272 int kernel_metadata_flush_buffer(int fd
)
1276 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1278 ret
= kernctl_buffer_flush(fd
);
1280 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1287 * Force flush buffer for channel.
1289 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1292 struct ltt_kernel_stream
*stream
;
1294 LTTNG_ASSERT(channel
);
1296 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1298 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1299 DBG("Flushing channel stream %d", stream
->fd
);
1300 ret
= kernctl_buffer_flush(stream
->fd
);
1303 ERR("Fail to flush buffer for stream %d (ret: %d)",
1312 * Stop tracing session.
1314 int kernel_stop_session(struct ltt_kernel_session
*session
)
1318 LTTNG_ASSERT(session
);
1320 ret
= kernctl_stop_session(session
->fd
);
1325 DBG("Kernel session stopped");
1334 * Open stream of channel, register it to the kernel tracer and add it
1335 * to the stream list of the channel.
1337 * Note: given that the streams may appear in random order wrt CPU
1338 * number (e.g. cpu hotplug), the index value of the stream number in
1339 * the stream name is not necessarily linked to the CPU number.
1341 * Return the number of created stream. Else, a negative value.
1343 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1346 struct ltt_kernel_stream
*lks
;
1348 LTTNG_ASSERT(channel
);
1350 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1351 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1352 channel
->stream_count
);
1362 /* Prevent fd duplication after execlp() */
1363 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1365 PERROR("fcntl session fd");
1368 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1369 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1371 /* Add stream to channel stream list */
1372 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1373 channel
->stream_count
++;
1375 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1379 return channel
->stream_count
;
1386 * Open the metadata stream and set it to the kernel session.
1388 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1392 LTTNG_ASSERT(session
);
1394 ret
= kernctl_create_stream(session
->metadata
->fd
);
1396 PERROR("kernel create metadata stream");
1400 DBG("Kernel metadata stream created (fd: %d)", ret
);
1401 session
->metadata_stream_fd
= ret
;
1402 /* Prevent fd duplication after execlp() */
1403 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1405 PERROR("fcntl session fd");
1415 * Get the event list from the kernel tracer and return the number of elements.
1417 ssize_t
kernel_list_events(struct lttng_event
**events
)
1421 size_t nbmem
, count
= 0;
1423 struct lttng_event
*elist
;
1425 LTTNG_ASSERT(events
);
1427 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1429 PERROR("kernel tracepoint list");
1433 fp
= fdopen(fd
, "r");
1435 PERROR("kernel tracepoint list fdopen");
1440 * Init memory size counter
1441 * See kernel-ctl.h for explanation of this value
1443 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1444 elist
= calloc
<lttng_event
>(nbmem
);
1445 if (elist
== NULL
) {
1446 PERROR("alloc list events");
1451 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1452 if (count
>= nbmem
) {
1453 struct lttng_event
*new_elist
;
1456 new_nbmem
= nbmem
<< 1;
1457 DBG("Reallocating event list from %zu to %zu bytes",
1459 new_elist
= (lttng_event
*) realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1460 if (new_elist
== NULL
) {
1461 PERROR("realloc list events");
1467 /* Zero the new memory */
1468 memset(new_elist
+ nbmem
, 0,
1469 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1473 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1474 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1475 elist
[count
].enabled
= -1;
1481 DBG("Kernel list events done (%zu events)", count
);
1483 ret
= fclose(fp
); /* closes both fp and fd */
1499 * Get kernel version and validate it.
1501 int kernel_validate_version(struct lttng_kernel_abi_tracer_version
*version
,
1502 struct lttng_kernel_abi_tracer_abi_version
*abi_version
)
1506 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1508 ERR("Failed to retrieve the lttng-modules version");
1512 /* Validate version */
1513 if (version
->major
!= VERSION_MAJOR
) {
1514 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1515 version
->major
, VERSION_MAJOR
);
1518 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1520 ERR("Failed to retrieve lttng-modules ABI version");
1523 if (abi_version
->major
!= LTTNG_KERNEL_ABI_MAJOR_VERSION
) {
1524 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1525 abi_version
->major
, abi_version
->minor
,
1526 LTTNG_KERNEL_ABI_MAJOR_VERSION
);
1529 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1530 version
->major
, version
->minor
,
1531 abi_version
->major
, abi_version
->minor
);
1538 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1543 * Kernel work-arounds called at the start of sessiond main().
1545 int init_kernel_workarounds(void)
1551 * boot_id needs to be read once before being used concurrently
1552 * to deal with a Linux kernel race. A fix is proposed for
1553 * upstream, but the work-around is needed for older kernels.
1555 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1562 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1564 /* Ignore error, we don't really care */
1576 * Teardown of a kernel session, keeping data required by destroy notifiers.
1578 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1580 struct lttng_trace_chunk
*trace_chunk
;
1582 if (ksess
== NULL
) {
1583 DBG3("No kernel session when tearing down session");
1587 DBG("Tearing down kernel session");
1588 trace_chunk
= ksess
->current_trace_chunk
;
1591 * Destroy channels on the consumer if at least one FD has been sent and we
1592 * are in no output mode because the streams are in *no* monitor mode so we
1593 * have to send a command to clean them up or else they leaked.
1595 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1597 struct consumer_socket
*socket
;
1598 struct lttng_ht_iter iter
;
1600 /* For each consumer socket. */
1602 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1603 socket
, node
.node
) {
1604 struct ltt_kernel_channel
*chan
;
1606 /* For each channel, ask the consumer to destroy it. */
1607 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1608 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1610 /* Consumer is probably dead. Use next socket. */
1618 /* Close any relayd session */
1619 consumer_output_send_destroy_relayd(ksess
->consumer
);
1621 trace_kernel_destroy_session(ksess
);
1622 lttng_trace_chunk_put(trace_chunk
);
1625 /* Teardown of data required by destroy notifiers. */
1626 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1628 if (ksess
== NULL
) {
1631 trace_kernel_free_session(ksess
);
1635 * Destroy a kernel channel object. It does not do anything on the tracer side.
1637 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1639 struct ltt_kernel_session
*ksess
= NULL
;
1641 LTTNG_ASSERT(kchan
);
1642 LTTNG_ASSERT(kchan
->channel
);
1644 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1646 /* Update channel count of associated session. */
1647 if (kchan
->session
) {
1648 /* Keep pointer reference so we can update it after the destroy. */
1649 ksess
= kchan
->session
;
1652 trace_kernel_destroy_channel(kchan
);
1655 * At this point the kernel channel is not visible anymore. This is safe
1656 * since in order to work on a visible kernel session, the tracing session
1657 * lock (ltt_session.lock) MUST be acquired.
1660 ksess
->channel_count
--;
1665 * Take a snapshot for a given kernel session.
1667 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1669 enum lttng_error_code
kernel_snapshot_record(
1670 struct ltt_kernel_session
*ksess
,
1671 const struct consumer_output
*output
,
1672 uint64_t nb_packets_per_stream
)
1674 int err
, ret
, saved_metadata_fd
;
1675 enum lttng_error_code status
= LTTNG_OK
;
1676 struct consumer_socket
*socket
;
1677 struct lttng_ht_iter iter
;
1678 struct ltt_kernel_metadata
*saved_metadata
;
1679 char *trace_path
= NULL
;
1680 size_t consumer_path_offset
= 0;
1682 LTTNG_ASSERT(ksess
);
1683 LTTNG_ASSERT(ksess
->consumer
);
1684 LTTNG_ASSERT(output
);
1686 DBG("Kernel snapshot record started");
1688 /* Save current metadata since the following calls will change it. */
1689 saved_metadata
= ksess
->metadata
;
1690 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1694 ret
= kernel_open_metadata(ksess
);
1696 status
= LTTNG_ERR_KERN_META_FAIL
;
1700 ret
= kernel_open_metadata_stream(ksess
);
1702 status
= LTTNG_ERR_KERN_META_FAIL
;
1703 goto error_open_stream
;
1706 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1707 "", &consumer_path_offset
);
1709 status
= LTTNG_ERR_INVALID
;
1712 /* Send metadata to consumer and snapshot everything. */
1713 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1714 socket
, node
.node
) {
1715 struct ltt_kernel_channel
*chan
;
1717 pthread_mutex_lock(socket
->lock
);
1718 /* This stream must not be monitored by the consumer. */
1719 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1720 pthread_mutex_unlock(socket
->lock
);
1722 status
= LTTNG_ERR_KERN_META_FAIL
;
1723 goto error_consumer
;
1726 /* For each channel, ask the consumer to snapshot it. */
1727 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1728 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1729 &trace_path
[consumer_path_offset
],
1730 nb_packets_per_stream
);
1731 if (status
!= LTTNG_OK
) {
1732 (void) kernel_consumer_destroy_metadata(socket
,
1734 goto error_consumer
;
1738 /* Snapshot metadata, */
1739 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1740 1, &trace_path
[consumer_path_offset
], 0);
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 LTTNG_ASSERT(syscall_mask
);
1779 LTTNG_ASSERT(nr_bits
);
1781 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1785 int kernel_tracer_abi_greater_or_equal(unsigned int major
, unsigned int minor
)
1788 struct lttng_kernel_abi_tracer_abi_version abi
;
1790 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1792 ERR("Failed to retrieve lttng-modules ABI version");
1796 ret
= abi
.major
> major
|| (abi
.major
== major
&& abi
.minor
>= minor
);
1802 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1805 * Return 1 on success, 0 when feature is not supported, negative value in case
1808 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1811 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1813 return kernel_tracer_abi_greater_or_equal(2, 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)
1825 * Packet sequence number was introduced in LTTng 2.8,
1826 * lttng-modules ABI 2.1.
1828 return kernel_tracer_abi_greater_or_equal(2, 1);
1832 * Check for the support of event notifiers via abi version number.
1834 * Return 1 on success, 0 when feature is not supported, negative value in case
1837 int kernel_supports_event_notifiers(void)
1840 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1842 return kernel_tracer_abi_greater_or_equal(2, 6);
1846 * Check for the support of ctf2 via abi version number.
1848 * Return 1 on success, 0 when feature is not supported, negative value in case
1851 int kernel_supports_ctf2(void)
1854 * JORAJ TODO: change this for the actual value when merged upstream
1855 * default to 2.7 ABI for now (modules 2.14)
1856 * CTF2 support was introduced in LTTng 2.1XX, lttng-modules ABI 2.XX.
1858 return kernel_tracer_abi_greater_or_equal(2, 7);
1862 * Rotate a kernel session.
1864 * Return LTTNG_OK on success or else an LTTng error code.
1866 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1869 enum lttng_error_code status
= LTTNG_OK
;
1870 struct consumer_socket
*socket
;
1871 struct lttng_ht_iter iter
;
1872 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1874 LTTNG_ASSERT(ksess
);
1875 LTTNG_ASSERT(ksess
->consumer
);
1877 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1878 session
->name
, session
->id
);
1883 * Note that this loop will end after one iteration given that there is
1884 * only one kernel consumer.
1886 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1887 socket
, node
.node
) {
1888 struct ltt_kernel_channel
*chan
;
1890 /* For each channel, ask the consumer to rotate it. */
1891 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1892 DBG("Rotate kernel channel %" PRIu64
", session %s",
1893 chan
->key
, session
->name
);
1894 ret
= consumer_rotate_channel(socket
, chan
->key
,
1896 /* is_metadata_channel */ false);
1898 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1904 * Rotate the metadata channel.
1906 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1908 /* is_metadata_channel */ true);
1910 status
= LTTNG_ERR_ROTATION_FAIL_CONSUMER
;
1920 enum lttng_error_code
kernel_create_channel_subdirectories(
1921 const struct ltt_kernel_session
*ksess
)
1923 enum lttng_error_code ret
= LTTNG_OK
;
1924 enum lttng_trace_chunk_status chunk_status
;
1927 LTTNG_ASSERT(ksess
->current_trace_chunk
);
1930 * Create the index subdirectory which will take care
1931 * of implicitly creating the channel's path.
1933 chunk_status
= lttng_trace_chunk_create_subdirectory(
1934 ksess
->current_trace_chunk
,
1935 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1936 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1937 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1946 * Setup necessary data for kernel tracer action.
1948 int init_kernel_tracer(void)
1951 bool is_root
= !getuid();
1953 /* Modprobe lttng kernel modules */
1954 ret
= modprobe_lttng_control();
1959 /* Open debugfs lttng */
1960 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1961 if (kernel_tracer_fd
< 0) {
1962 DBG("Failed to open %s", module_proc_lttng
);
1966 /* Validate kernel version */
1967 ret
= kernel_validate_version(&the_kernel_tracer_version
,
1968 &the_kernel_tracer_abi_version
);
1973 ret
= modprobe_lttng_data();
1978 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1983 WARN("Kernel tracer does not support buffer monitoring. "
1984 "The monitoring timer of channels in the kernel domain "
1985 "will be set to 0 (disabled).");
1988 ret
= kernel_supports_event_notifiers();
1990 ERR("Failed to check for kernel tracer event notifier support");
1993 ret
= kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd
);
1995 /* This is not fatal. */
1996 WARN("Failed to create kernel event notifier group");
1997 kernel_tracer_event_notifier_group_fd
= -1;
1999 enum event_notifier_error_accounting_status error_accounting_status
;
2000 enum lttng_error_code error_code_ret
=
2001 kernel_create_event_notifier_group_notification_fd(
2002 &kernel_tracer_event_notifier_group_notification_fd
);
2004 if (error_code_ret
!= LTTNG_OK
) {
2008 error_accounting_status
= event_notifier_error_accounting_register_kernel(
2009 kernel_tracer_event_notifier_group_fd
);
2010 if (error_accounting_status
!= EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK
) {
2011 ERR("Failed to initialize event notifier error accounting for kernel tracer");
2012 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING
;
2016 kernel_token_to_event_notifier_rule_ht
= cds_lfht_new(
2017 DEFAULT_HT_SIZE
, 1, 0,
2018 CDS_LFHT_AUTO_RESIZE
| CDS_LFHT_ACCOUNTING
,
2020 if (!kernel_token_to_event_notifier_rule_ht
) {
2021 goto error_token_ht
;
2025 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
2026 kernel_tracer_fd
, kernel_tracer_event_notifier_group_fd
,
2027 kernel_tracer_event_notifier_group_notification_fd
);
2029 ret
= syscall_init_table(kernel_tracer_fd
);
2031 ERR("Unable to populate syscall table. Syscall tracing won't "
2032 "work for this session daemon.");
2038 modprobe_remove_lttng_control();
2039 ret
= close(kernel_tracer_fd
);
2041 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2045 kernel_tracer_fd
= -1;
2046 return LTTNG_ERR_KERN_VERSION
;
2050 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2052 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
2053 kernel_tracer_event_notifier_group_notification_fd
);
2056 kernel_tracer_event_notifier_group_notification_fd
= -1;
2059 ret
= close(kernel_tracer_event_notifier_group_fd
);
2061 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
2062 kernel_tracer_event_notifier_group_fd
);
2065 kernel_tracer_event_notifier_group_fd
= -1;
2067 ret
= close(kernel_tracer_fd
);
2069 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2073 kernel_tracer_fd
= -1;
2076 modprobe_remove_lttng_control();
2079 WARN("No kernel tracer available");
2080 kernel_tracer_fd
= -1;
2082 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
2084 return LTTNG_ERR_KERN_NA
;
2088 void cleanup_kernel_tracer(void)
2090 DBG2("Closing kernel event notifier group notification file descriptor");
2091 if (kernel_tracer_event_notifier_group_notification_fd
>= 0) {
2092 int ret
= notification_thread_command_remove_tracer_event_source(
2093 the_notification_thread_handle
,
2094 kernel_tracer_event_notifier_group_notification_fd
);
2095 if (ret
!= LTTNG_OK
) {
2096 ERR("Failed to remove kernel event notifier notification from notification thread");
2099 ret
= close(kernel_tracer_event_notifier_group_notification_fd
);
2101 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2102 kernel_tracer_event_notifier_group_notification_fd
);
2105 kernel_tracer_event_notifier_group_notification_fd
= -1;
2108 if (kernel_token_to_event_notifier_rule_ht
) {
2109 const int ret
= cds_lfht_destroy(
2110 kernel_token_to_event_notifier_rule_ht
, NULL
);
2111 LTTNG_ASSERT(ret
== 0);
2114 DBG2("Closing kernel event notifier group file descriptor");
2115 if (kernel_tracer_event_notifier_group_fd
>= 0) {
2116 const int ret
= close(kernel_tracer_event_notifier_group_fd
);
2119 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2120 kernel_tracer_event_notifier_group_fd
);
2123 kernel_tracer_event_notifier_group_fd
= -1;
2126 DBG2("Closing kernel fd");
2127 if (kernel_tracer_fd
>= 0) {
2128 const int ret
= close(kernel_tracer_fd
);
2131 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2135 kernel_tracer_fd
= -1;
2138 free(syscall_table
);
2141 bool kernel_tracer_is_initialized(void)
2143 return kernel_tracer_fd
>= 0;
2147 * Clear a kernel session.
2149 * Return LTTNG_OK on success or else an LTTng error code.
2151 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
2154 enum lttng_error_code status
= LTTNG_OK
;
2155 struct consumer_socket
*socket
;
2156 struct lttng_ht_iter iter
;
2157 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
2159 LTTNG_ASSERT(ksess
);
2160 LTTNG_ASSERT(ksess
->consumer
);
2162 DBG("Clear kernel session %s (session %" PRIu64
")",
2163 session
->name
, session
->id
);
2167 if (ksess
->active
) {
2168 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
2169 status
= LTTNG_ERR_FATAL
;
2174 * Note that this loop will end after one iteration given that there is
2175 * only one kernel consumer.
2177 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
2178 socket
, node
.node
) {
2179 struct ltt_kernel_channel
*chan
;
2181 /* For each channel, ask the consumer to clear it. */
2182 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
2183 DBG("Clear kernel channel %" PRIu64
", session %s",
2184 chan
->key
, session
->name
);
2185 ret
= consumer_clear_channel(socket
, chan
->key
);
2191 if (!ksess
->metadata
) {
2193 * Nothing to do for the metadata.
2194 * This is a snapshot session.
2195 * The metadata is genererated on the fly.
2201 * Clear the metadata channel.
2202 * Metadata channel is not cleared per se but we still need to
2203 * perform a rotation operation on it behind the scene.
2205 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
2214 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
2215 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
2218 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;
2226 enum lttng_error_code
kernel_create_event_notifier_group_notification_fd(
2227 int *event_notifier_group_notification_fd
)
2229 int local_fd
= -1, ret
;
2230 enum lttng_error_code error_code_ret
;
2232 LTTNG_ASSERT(event_notifier_group_notification_fd
);
2234 ret
= kernctl_create_event_notifier_group_notification_fd(
2235 kernel_tracer_event_notifier_group_fd
);
2237 PERROR("Failed to create kernel event notifier group notification file descriptor");
2238 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2244 /* Prevent fd duplication after execlp(). */
2245 ret
= fcntl(local_fd
, F_SETFD
, FD_CLOEXEC
);
2247 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2249 error_code_ret
= LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD
;
2253 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2255 error_code_ret
= LTTNG_OK
;
2256 *event_notifier_group_notification_fd
= local_fd
;
2260 if (local_fd
>= 0) {
2261 ret
= close(local_fd
);
2263 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2268 return error_code_ret
;
2271 enum lttng_error_code
kernel_destroy_event_notifier_group_notification_fd(
2272 int event_notifier_group_notification_fd
)
2274 enum lttng_error_code ret_code
= LTTNG_OK
;
2276 DBG("Closing event notifier group notification file descriptor: fd = %d",
2277 event_notifier_group_notification_fd
);
2278 if (event_notifier_group_notification_fd
>= 0) {
2279 const int ret
= close(event_notifier_group_notification_fd
);
2281 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2282 event_notifier_group_notification_fd
);
2290 unsigned long hash_trigger(const struct lttng_trigger
*trigger
)
2292 const struct lttng_condition
*condition
=
2293 lttng_trigger_get_const_condition(trigger
);
2295 return lttng_condition_hash(condition
);
2299 int match_trigger(struct cds_lfht_node
*node
, const void *key
)
2301 const struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2302 const struct lttng_trigger
*trigger
= (lttng_trigger
*) key
;
2304 event_notifier_rule
= caa_container_of(node
,
2305 const struct ltt_kernel_event_notifier_rule
, ht_node
);
2307 return lttng_trigger_is_equal(trigger
, event_notifier_rule
->trigger
);
2310 static enum lttng_error_code
kernel_create_event_notifier_rule(
2311 struct lttng_trigger
*trigger
,
2312 const struct lttng_credentials
*creds
, uint64_t token
)
2314 int err
, fd
, ret
= 0;
2315 enum lttng_error_code error_code_ret
;
2316 enum lttng_condition_status condition_status
;
2317 enum lttng_condition_type condition_type
;
2318 enum lttng_event_rule_type event_rule_type
;
2319 struct ltt_kernel_event_notifier_rule
*event_notifier_rule
;
2320 struct lttng_kernel_abi_event_notifier kernel_event_notifier
= {};
2321 unsigned int capture_bytecode_count
= 0, i
;
2322 const struct lttng_condition
*condition
= NULL
;
2323 const struct lttng_event_rule
*event_rule
= NULL
;
2324 enum lttng_condition_status cond_status
;
2326 LTTNG_ASSERT(trigger
);
2328 condition
= lttng_trigger_get_const_condition(trigger
);
2329 LTTNG_ASSERT(condition
);
2331 condition_type
= lttng_condition_get_type(condition
);
2332 LTTNG_ASSERT(condition_type
== LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES
);
2334 /* Does not acquire a reference. */
2335 condition_status
= lttng_condition_event_rule_matches_get_rule(
2336 condition
, &event_rule
);
2337 LTTNG_ASSERT(condition_status
== LTTNG_CONDITION_STATUS_OK
);
2338 LTTNG_ASSERT(event_rule
);
2340 event_rule_type
= lttng_event_rule_get_type(event_rule
);
2341 LTTNG_ASSERT(event_rule_type
!= LTTNG_EVENT_RULE_TYPE_UNKNOWN
);
2343 error_code_ret
= trace_kernel_create_event_notifier_rule(trigger
, token
,
2344 lttng_condition_event_rule_matches_get_error_counter_index(
2346 &event_notifier_rule
);
2347 if (error_code_ret
!= LTTNG_OK
) {
2351 error_code_ret
= trace_kernel_init_event_notifier_from_event_rule(
2352 event_rule
, &kernel_event_notifier
);
2353 if (error_code_ret
!= LTTNG_OK
) {
2357 kernel_event_notifier
.event
.token
= event_notifier_rule
->token
;
2358 kernel_event_notifier
.error_counter_idx
=
2359 lttng_condition_event_rule_matches_get_error_counter_index(
2362 fd
= kernctl_create_event_notifier(
2363 kernel_tracer_event_notifier_group_fd
,
2364 &kernel_event_notifier
);
2368 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2371 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2372 error_code_ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
2375 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2376 kernel_event_notifier
.event
.name
);
2377 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2380 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2381 fd
, kernel_event_notifier
.event
.name
);
2382 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2387 event_notifier_rule
->fd
= fd
;
2388 /* Prevent fd duplication after execlp(). */
2389 err
= fcntl(event_notifier_rule
->fd
, F_SETFD
, FD_CLOEXEC
);
2391 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2393 error_code_ret
= LTTNG_ERR_FATAL
;
2394 goto set_cloexec_error
;
2397 if (event_notifier_rule
->filter
) {
2398 err
= kernctl_filter(event_notifier_rule
->fd
, event_notifier_rule
->filter
);
2402 error_code_ret
= LTTNG_ERR_FILTER_NOMEM
;
2405 error_code_ret
= LTTNG_ERR_FILTER_INVAL
;
2412 if (lttng_event_rule_get_type(event_rule
) ==
2413 LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE
) {
2414 ret
= userspace_probe_event_rule_add_callsites(
2415 event_rule
, creds
, event_notifier_rule
->fd
);
2417 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2418 goto add_callsite_error
;
2422 /* Set the capture bytecode if any. */
2423 cond_status
= lttng_condition_event_rule_matches_get_capture_descriptor_count(
2424 condition
, &capture_bytecode_count
);
2425 LTTNG_ASSERT(cond_status
== LTTNG_CONDITION_STATUS_OK
);
2427 for (i
= 0; i
< capture_bytecode_count
; i
++) {
2428 const struct lttng_bytecode
*capture_bytecode
=
2429 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(
2432 if (capture_bytecode
== NULL
) {
2433 ERR("Unexpected NULL capture bytecode on condition");
2434 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2438 ret
= kernctl_capture(event_notifier_rule
->fd
, capture_bytecode
);
2440 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
2441 event_notifier_rule
->fd
);
2442 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2447 err
= kernctl_enable(event_notifier_rule
->fd
);
2451 error_code_ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
2454 PERROR("enable kernel event notifier");
2455 error_code_ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
2461 /* Add trigger to kernel token mapping in the hash table. */
2463 cds_lfht_add(kernel_token_to_event_notifier_rule_ht
, hash_trigger(trigger
),
2464 &event_notifier_rule
->ht_node
);
2467 DBG("Created kernel event notifier: name = '%s', fd = %d",
2468 kernel_event_notifier
.event
.name
,
2469 event_notifier_rule
->fd
);
2479 const int close_ret
= close(event_notifier_rule
->fd
);
2482 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2483 event_notifier_rule
->fd
);
2487 free(event_notifier_rule
);
2489 return error_code_ret
;
2492 enum lttng_error_code
kernel_register_event_notifier(
2493 struct lttng_trigger
*trigger
,
2494 const struct lttng_credentials
*cmd_creds
)
2496 enum lttng_error_code ret
;
2497 enum lttng_condition_status status
;
2498 enum lttng_domain_type domain_type
;
2499 const struct lttng_event_rule
*event_rule
;
2500 const struct lttng_condition
*const condition
=
2501 lttng_trigger_get_const_condition(trigger
);
2502 const uint64_t token
= lttng_trigger_get_tracer_token(trigger
);
2504 LTTNG_ASSERT(condition
);
2506 /* Does not acquire a reference to the event rule. */
2507 status
= lttng_condition_event_rule_matches_get_rule(
2508 condition
, &event_rule
);
2509 LTTNG_ASSERT(status
== LTTNG_CONDITION_STATUS_OK
);
2511 domain_type
= lttng_event_rule_get_domain_type(event_rule
);
2512 LTTNG_ASSERT(domain_type
== LTTNG_DOMAIN_KERNEL
);
2514 ret
= kernel_create_event_notifier_rule(trigger
, cmd_creds
, token
);
2515 if (ret
!= LTTNG_OK
) {
2516 ERR("Failed to create kernel event notifier rule");
2522 enum lttng_error_code
kernel_unregister_event_notifier(
2523 const struct lttng_trigger
*trigger
)
2525 struct ltt_kernel_event_notifier_rule
*token_event_rule_element
;
2526 struct cds_lfht_node
*node
;
2527 struct cds_lfht_iter iter
;
2528 enum lttng_error_code error_code_ret
;
2533 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht
,
2534 hash_trigger(trigger
), match_trigger
, trigger
, &iter
);
2536 node
= cds_lfht_iter_get_node(&iter
);
2538 error_code_ret
= LTTNG_ERR_TRIGGER_NOT_FOUND
;
2542 token_event_rule_element
= caa_container_of(node
,
2543 struct ltt_kernel_event_notifier_rule
, ht_node
);
2545 ret
= kernel_disable_event_notifier_rule(token_event_rule_element
);
2547 error_code_ret
= LTTNG_ERR_FATAL
;
2551 trace_kernel_destroy_event_notifier_rule(token_event_rule_element
);
2552 error_code_ret
= LTTNG_OK
;
2557 return error_code_ret
;
2560 int kernel_get_notification_fd(void)
2562 return kernel_tracer_event_notifier_group_notification_fd
;