2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/kernel-ctl/kernel-ioctl.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
33 #include "kernel-consumer.h"
34 #include "kern-modules.h"
39 * Key used to reference a channel between the sessiond and the consumer. This
40 * is only read and updated with the session_list lock held.
42 static uint64_t next_kernel_channel_key
;
44 #include <lttng/userspace-probe.h>
45 #include <lttng/userspace-probe-internal.h>
47 * Add context on a kernel channel.
49 * Assumes the ownership of ctx.
51 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
52 struct ltt_kernel_context
*ctx
)
59 DBG("Adding context to channel %s", chan
->channel
->name
);
60 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
64 /* Exists but not available for this kernel */
65 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
68 /* If EEXIST, we just ignore the error */
72 PERROR("add context ioctl");
73 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
80 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
85 trace_kernel_destroy_context(ctx
);
91 * Create a new kernel session, register it to the kernel tracer and add it to
92 * the session daemon session.
94 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
97 struct ltt_kernel_session
*lks
;
101 /* Allocate data structure */
102 lks
= trace_kernel_create_session();
108 /* Kernel tracer session creation */
109 ret
= kernctl_create_session(tracer_fd
);
111 PERROR("ioctl kernel create session");
116 /* Prevent fd duplication after execlp() */
117 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
119 PERROR("fcntl session fd");
122 lks
->id
= session
->id
;
123 lks
->consumer_fds_sent
= 0;
124 session
->kernel_session
= lks
;
126 DBG("Kernel session created (fd: %d)", lks
->fd
);
129 * This is necessary since the creation time is present in the session
130 * name when it is generated.
132 if (session
->has_auto_generated_name
) {
133 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
135 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
138 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
139 session
->id
, session
->name
);
142 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
144 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
145 session
->id
, session
->name
);
152 trace_kernel_destroy_session(lks
);
158 * Create a kernel channel, register it to the kernel tracer and add it to the
161 int kernel_create_channel(struct ltt_kernel_session
*session
,
162 struct lttng_channel
*chan
)
165 struct ltt_kernel_channel
*lkc
;
170 /* Allocate kernel channel */
171 lkc
= trace_kernel_create_channel(chan
);
176 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
177 chan
->name
, lkc
->channel
->attr
.overwrite
,
178 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
179 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
180 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
182 /* Kernel tracer channel creation */
183 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
185 PERROR("ioctl kernel create channel");
189 /* Setup the channel fd */
191 /* Prevent fd duplication after execlp() */
192 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
194 PERROR("fcntl session fd");
197 /* Add channel to session */
198 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
199 session
->channel_count
++;
200 lkc
->session
= session
;
201 lkc
->key
= ++next_kernel_channel_key
;
203 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
204 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
217 * Compute the offset of the instrumentation byte in the binary based on the
218 * function probe location using the ELF lookup method.
220 * Returns 0 on success and set the offset out parameter to the offset of the
222 * Returns -1 on error
225 int extract_userspace_probe_offset_function_elf(
226 const struct lttng_userspace_probe_location
*probe_location
,
227 struct ltt_kernel_session
*session
, uint64_t *offset
)
231 const char *symbol
= NULL
;
232 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
233 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
235 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
236 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
238 lookup
= lttng_userspace_probe_location_get_lookup_method(
246 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
248 assert(lookup_method_type
==
249 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
251 symbol
= lttng_userspace_probe_location_function_get_function_name(
258 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
264 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
265 session
->gid
, offset
);
267 DBG("userspace probe offset calculation failed for "
268 "function %s", symbol
);
272 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
278 * Compute the offsets of the instrumentation bytes in the binary based on the
279 * tracepoint probe location using the SDT lookup method. This function
280 * allocates the offsets buffer, the caller must free it.
282 * Returns 0 on success and set the offset out parameter to the offsets of the
284 * Returns -1 on error.
287 int extract_userspace_probe_offset_tracepoint_sdt(
288 const struct lttng_userspace_probe_location
*probe_location
,
289 struct ltt_kernel_session
*session
, uint64_t **offsets
,
290 uint32_t *offsets_count
)
292 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
293 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
294 const char *probe_name
= NULL
, *provider_name
= NULL
;
298 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
299 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
301 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
308 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
310 assert(lookup_method_type
==
311 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
314 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
321 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
323 if (!provider_name
) {
328 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
334 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
335 session
->uid
, session
->gid
, offsets
, offsets_count
);
337 DBG("userspace probe offset calculation failed for sdt "
338 "probe %s:%s", provider_name
, probe_name
);
342 if (*offsets_count
== 0) {
343 DBG("no userspace probe offset found");
347 DBG("%u userspace probe SDT offsets found for %s:%s at:",
348 *offsets_count
, provider_name
, probe_name
);
349 for (i
= 0; i
< *offsets_count
; i
++) {
350 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
357 * Extract the offsets of the instrumentation point for the different lookup
361 int userspace_probe_add_callsites(struct lttng_event
*ev
,
362 struct ltt_kernel_session
*session
, int fd
)
364 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
365 enum lttng_userspace_probe_location_lookup_method_type type
;
366 const struct lttng_userspace_probe_location
*location
= NULL
;
370 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
372 location
= lttng_event_get_userspace_probe_location(ev
);
378 lttng_userspace_probe_location_get_lookup_method(location
);
379 if (!lookup_method
) {
384 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
386 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
388 struct lttng_kernel_event_callsite callsite
;
391 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
393 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
397 callsite
.u
.uprobe
.offset
= offset
;
398 ret
= kernctl_add_callsite(fd
, &callsite
);
400 WARN("Adding callsite to userspace probe "
401 "event %s failed.", ev
->name
);
402 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
407 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
410 uint64_t *offsets
= NULL
;
411 uint32_t offsets_count
;
412 struct lttng_kernel_event_callsite callsite
;
415 * This call allocates the offsets buffer. This buffer must be freed
418 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
419 &offsets
, &offsets_count
);
421 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
424 for (i
= 0; i
< offsets_count
; i
++) {
425 callsite
.u
.uprobe
.offset
= offsets
[i
];
426 ret
= kernctl_add_callsite(fd
, &callsite
);
428 WARN("Adding callsite to userspace probe "
429 "event %s failed.", ev
->name
);
430 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
439 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
447 * Create a kernel event, enable it to the kernel tracer and add it to the
448 * channel event list of the kernel session.
449 * We own filter_expression and filter.
451 int kernel_create_event(struct lttng_event
*ev
,
452 struct ltt_kernel_channel
*channel
,
453 char *filter_expression
,
454 struct lttng_filter_bytecode
*filter
)
457 enum lttng_error_code ret
;
458 struct ltt_kernel_event
*event
;
463 /* We pass ownership of filter_expression and filter */
464 ret
= trace_kernel_create_event(ev
, filter_expression
,
466 if (ret
!= LTTNG_OK
) {
470 fd
= kernctl_create_event(channel
->fd
, event
->event
);
474 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
477 WARN("Event type not implemented");
478 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
481 WARN("Event %s not found!", ev
->name
);
482 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
485 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
486 PERROR("create event ioctl");
491 event
->type
= ev
->type
;
493 /* Prevent fd duplication after execlp() */
494 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
496 PERROR("fcntl session fd");
500 err
= kernctl_filter(event
->fd
, filter
);
504 ret
= LTTNG_ERR_FILTER_NOMEM
;
507 ret
= LTTNG_ERR_FILTER_INVAL
;
514 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
515 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
517 goto add_callsite_error
;
521 err
= kernctl_enable(event
->fd
);
525 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
528 PERROR("enable kernel event");
529 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
535 /* Add event to event list */
536 cds_list_add(&event
->list
, &channel
->events_list
.head
);
537 channel
->event_count
++;
539 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
549 closeret
= close(event
->fd
);
551 PERROR("close event fd");
561 * Disable a kernel channel.
563 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
569 ret
= kernctl_disable(chan
->fd
);
571 PERROR("disable chan ioctl");
576 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
577 chan
->channel
->name
, chan
->fd
, chan
->key
);
586 * Enable a kernel channel.
588 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
594 ret
= kernctl_enable(chan
->fd
);
595 if (ret
< 0 && ret
!= -EEXIST
) {
596 PERROR("Enable kernel chan");
601 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
602 chan
->channel
->name
, chan
->fd
, chan
->key
);
611 * Enable a kernel event.
613 int kernel_enable_event(struct ltt_kernel_event
*event
)
619 ret
= kernctl_enable(event
->fd
);
623 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
626 PERROR("enable kernel event");
633 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
642 * Disable a kernel event.
644 int kernel_disable_event(struct ltt_kernel_event
*event
)
650 ret
= kernctl_disable(event
->fd
);
654 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
657 PERROR("disable kernel event");
664 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
673 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
677 DBG("Kernel track PID %d for session id %" PRIu64
".",
679 ret
= kernctl_track_pid(session
->fd
, pid
);
685 return LTTNG_ERR_INVALID
;
687 return LTTNG_ERR_NOMEM
;
689 return LTTNG_ERR_PID_TRACKED
;
691 return LTTNG_ERR_UNK
;
695 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
699 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
701 ret
= kernctl_untrack_pid(session
->fd
, pid
);
707 return LTTNG_ERR_INVALID
;
709 return LTTNG_ERR_NOMEM
;
711 return LTTNG_ERR_PID_NOT_TRACKED
;
713 return LTTNG_ERR_UNK
;
717 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
722 ssize_t nbmem
, count
= 0;
726 fd
= kernctl_list_tracker_pids(session
->fd
);
728 PERROR("kernel tracker pids list");
732 fp
= fdopen(fd
, "r");
734 PERROR("kernel tracker pids list fdopen");
738 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
739 pids
= zmalloc(sizeof(*pids
) * nbmem
);
741 PERROR("alloc list pids");
746 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
747 if (count
>= nbmem
) {
751 new_nbmem
= nbmem
<< 1;
752 DBG("Reallocating pids list from %zu to %zu entries",
754 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
755 if (new_pids
== NULL
) {
756 PERROR("realloc list events");
761 /* Zero the new memory */
762 memset(new_pids
+ nbmem
, 0,
763 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
771 DBG("Kernel list tracker pids done (%zd pids)", count
);
773 ret
= fclose(fp
); /* closes both fp and fd */
789 * Create kernel metadata, open from the kernel tracer and add it to the
792 int kernel_open_metadata(struct ltt_kernel_session
*session
)
795 struct ltt_kernel_metadata
*lkm
= NULL
;
799 /* Allocate kernel metadata */
800 lkm
= trace_kernel_create_metadata();
805 /* Kernel tracer metadata creation */
806 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
812 lkm
->key
= ++next_kernel_channel_key
;
813 /* Prevent fd duplication after execlp() */
814 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
816 PERROR("fcntl session fd");
819 session
->metadata
= lkm
;
821 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
826 trace_kernel_destroy_metadata(lkm
);
832 * Start tracing session.
834 int kernel_start_session(struct ltt_kernel_session
*session
)
840 ret
= kernctl_start_session(session
->fd
);
842 PERROR("ioctl start session");
846 DBG("Kernel session started");
855 * Make a kernel wait to make sure in-flight probe have completed.
857 void kernel_wait_quiescent(int fd
)
861 DBG("Kernel quiescent wait on %d", fd
);
863 ret
= kernctl_wait_quiescent(fd
);
865 PERROR("wait quiescent ioctl");
866 ERR("Kernel quiescent wait failed");
871 * Force flush buffer of metadata.
873 int kernel_metadata_flush_buffer(int fd
)
877 DBG("Kernel flushing metadata buffer on fd %d", fd
);
879 ret
= kernctl_buffer_flush(fd
);
881 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
888 * Force flush buffer for channel.
890 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
893 struct ltt_kernel_stream
*stream
;
897 DBG("Flush buffer for channel %s", channel
->channel
->name
);
899 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
900 DBG("Flushing channel stream %d", stream
->fd
);
901 ret
= kernctl_buffer_flush(stream
->fd
);
904 ERR("Fail to flush buffer for stream %d (ret: %d)",
913 * Stop tracing session.
915 int kernel_stop_session(struct ltt_kernel_session
*session
)
921 ret
= kernctl_stop_session(session
->fd
);
926 DBG("Kernel session stopped");
935 * Open stream of channel, register it to the kernel tracer and add it
936 * to the stream list of the channel.
938 * Note: given that the streams may appear in random order wrt CPU
939 * number (e.g. cpu hotplug), the index value of the stream number in
940 * the stream name is not necessarily linked to the CPU number.
942 * Return the number of created stream. Else, a negative value.
944 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
947 struct ltt_kernel_stream
*lks
;
951 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
952 lks
= trace_kernel_create_stream(channel
->channel
->name
,
953 channel
->stream_count
);
963 /* Prevent fd duplication after execlp() */
964 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
966 PERROR("fcntl session fd");
969 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
970 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
972 /* Add stream to channel stream list */
973 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
974 channel
->stream_count
++;
976 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
980 return channel
->stream_count
;
987 * Open the metadata stream and set it to the kernel session.
989 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
995 ret
= kernctl_create_stream(session
->metadata
->fd
);
997 PERROR("kernel create metadata stream");
1001 DBG("Kernel metadata stream created (fd: %d)", ret
);
1002 session
->metadata_stream_fd
= ret
;
1003 /* Prevent fd duplication after execlp() */
1004 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1006 PERROR("fcntl session fd");
1016 * Get the event list from the kernel tracer and return the number of elements.
1018 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
1022 size_t nbmem
, count
= 0;
1024 struct lttng_event
*elist
;
1028 fd
= kernctl_tracepoint_list(tracer_fd
);
1030 PERROR("kernel tracepoint list");
1034 fp
= fdopen(fd
, "r");
1036 PERROR("kernel tracepoint list fdopen");
1041 * Init memory size counter
1042 * See kernel-ctl.h for explanation of this value
1044 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1045 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1046 if (elist
== NULL
) {
1047 PERROR("alloc list events");
1052 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1053 if (count
>= nbmem
) {
1054 struct lttng_event
*new_elist
;
1057 new_nbmem
= nbmem
<< 1;
1058 DBG("Reallocating event list from %zu to %zu bytes",
1060 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1061 if (new_elist
== NULL
) {
1062 PERROR("realloc list events");
1068 /* Zero the new memory */
1069 memset(new_elist
+ nbmem
, 0,
1070 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1074 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1075 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1076 elist
[count
].enabled
= -1;
1082 DBG("Kernel list events done (%zu events)", count
);
1084 ret
= fclose(fp
); /* closes both fp and fd */
1100 * Get kernel version and validate it.
1102 int kernel_validate_version(int tracer_fd
,
1103 struct lttng_kernel_tracer_version
*version
,
1104 struct lttng_kernel_tracer_abi_version
*abi_version
)
1108 ret
= kernctl_tracer_version(tracer_fd
, version
);
1110 ERR("Failed to retrieve the lttng-modules version");
1114 /* Validate version */
1115 if (version
->major
!= VERSION_MAJOR
) {
1116 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1117 version
->major
, VERSION_MAJOR
);
1120 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1122 ERR("Failed to retrieve lttng-modules ABI version");
1125 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1126 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1127 abi_version
->major
, abi_version
->minor
,
1128 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1131 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1132 version
->major
, version
->minor
,
1133 abi_version
->major
, abi_version
->minor
);
1140 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1145 * Kernel work-arounds called at the start of sessiond main().
1147 int init_kernel_workarounds(void)
1153 * boot_id needs to be read once before being used concurrently
1154 * to deal with a Linux kernel race. A fix is proposed for
1155 * upstream, but the work-around is needed for older kernels.
1157 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1164 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1166 /* Ignore error, we don't really care */
1178 * Complete teardown of a kernel session.
1180 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1182 if (ksess
== NULL
) {
1183 DBG3("No kernel session when tearing down session");
1187 DBG("Tearing down kernel session");
1190 * Destroy channels on the consumer if at least one FD has been sent and we
1191 * are in no output mode because the streams are in *no* monitor mode so we
1192 * have to send a command to clean them up or else they leaked.
1194 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1196 struct consumer_socket
*socket
;
1197 struct lttng_ht_iter iter
;
1199 /* For each consumer socket. */
1201 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1202 socket
, node
.node
) {
1203 struct ltt_kernel_channel
*chan
;
1205 /* For each channel, ask the consumer to destroy it. */
1206 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1207 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1209 /* Consumer is probably dead. Use next socket. */
1217 /* Close any relayd session */
1218 consumer_output_send_destroy_relayd(ksess
->consumer
);
1220 trace_kernel_destroy_session(ksess
);
1224 * Destroy a kernel channel object. It does not do anything on the tracer side.
1226 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1228 struct ltt_kernel_session
*ksess
= NULL
;
1231 assert(kchan
->channel
);
1233 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1235 /* Update channel count of associated session. */
1236 if (kchan
->session
) {
1237 /* Keep pointer reference so we can update it after the destroy. */
1238 ksess
= kchan
->session
;
1241 trace_kernel_destroy_channel(kchan
);
1244 * At this point the kernel channel is not visible anymore. This is safe
1245 * since in order to work on a visible kernel session, the tracing session
1246 * lock (ltt_session.lock) MUST be acquired.
1249 ksess
->channel_count
--;
1254 * Take a snapshot for a given kernel session.
1256 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1258 enum lttng_error_code
kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1259 struct snapshot_output
*output
, int wait
,
1260 uint64_t nb_packets_per_stream
)
1262 int err
, ret
, saved_metadata_fd
;
1263 enum lttng_error_code status
= LTTNG_OK
;
1264 struct consumer_socket
*socket
;
1265 struct lttng_ht_iter iter
;
1266 struct ltt_kernel_metadata
*saved_metadata
;
1267 struct ltt_session
*session
= NULL
;
1268 uint64_t trace_archive_id
;
1271 assert(ksess
->consumer
);
1274 DBG("Kernel snapshot record started");
1276 session
= session_find_by_id(ksess
->id
);
1278 assert(pthread_mutex_trylock(&session
->lock
));
1279 assert(session_trylock_list());
1280 trace_archive_id
= session
->current_archive_id
;
1282 /* Save current metadata since the following calls will change it. */
1283 saved_metadata
= ksess
->metadata
;
1284 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1288 ret
= kernel_open_metadata(ksess
);
1290 status
= LTTNG_ERR_KERN_META_FAIL
;
1294 ret
= kernel_open_metadata_stream(ksess
);
1296 status
= LTTNG_ERR_KERN_META_FAIL
;
1297 goto error_open_stream
;
1300 /* Send metadata to consumer and snapshot everything. */
1301 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1302 socket
, node
.node
) {
1303 struct consumer_output
*saved_output
;
1304 struct ltt_kernel_channel
*chan
;
1307 * Temporarly switch consumer output for our snapshot output. As long
1308 * as the session lock is taken, this is safe.
1310 saved_output
= ksess
->consumer
;
1311 ksess
->consumer
= output
->consumer
;
1313 pthread_mutex_lock(socket
->lock
);
1314 /* This stream must not be monitored by the consumer. */
1315 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1316 pthread_mutex_unlock(socket
->lock
);
1317 /* Put back the saved consumer output into the session. */
1318 ksess
->consumer
= saved_output
;
1320 status
= LTTNG_ERR_KERN_META_FAIL
;
1321 goto error_consumer
;
1324 /* For each channel, ask the consumer to snapshot it. */
1325 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1326 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1327 ksess
->uid
, ksess
->gid
,
1328 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1329 nb_packets_per_stream
,
1331 if (status
!= LTTNG_OK
) {
1332 (void) kernel_consumer_destroy_metadata(socket
,
1334 goto error_consumer
;
1338 /* Snapshot metadata, */
1339 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1340 1, ksess
->uid
, ksess
->gid
,
1341 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1343 if (status
!= LTTNG_OK
) {
1344 goto error_consumer
;
1348 * The metadata snapshot is done, ask the consumer to destroy it since
1349 * it's not monitored on the consumer side.
1351 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1355 /* Close newly opened metadata stream. It's now on the consumer side. */
1356 err
= close(ksess
->metadata_stream_fd
);
1358 PERROR("close snapshot kernel");
1362 trace_kernel_destroy_metadata(ksess
->metadata
);
1364 /* Restore metadata state.*/
1365 ksess
->metadata
= saved_metadata
;
1366 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1368 session_put(session
);
1375 * Get the syscall mask array from the kernel tracer.
1377 * Return 0 on success else a negative value. In both case, syscall_mask should
1380 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1382 assert(syscall_mask
);
1385 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1389 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1392 * Return 1 on success, 0 when feature is not supported, negative value in case
1395 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1397 int ret
= 0; // Not supported by default
1398 struct lttng_kernel_tracer_abi_version abi
;
1400 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1402 ERR("Failed to retrieve lttng-modules ABI version");
1407 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1409 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1421 * Rotate a kernel session.
1423 * Return LTTNG_OK on success or else an LTTng error code.
1425 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1428 enum lttng_error_code status
= LTTNG_OK
;
1429 struct consumer_socket
*socket
;
1430 struct lttng_ht_iter iter
;
1431 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1434 assert(ksess
->consumer
);
1436 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1437 session
->name
, session
->id
);
1442 * Note that this loop will end after one iteration given that there is
1443 * only one kernel consumer.
1445 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1446 socket
, node
.node
) {
1447 struct ltt_kernel_channel
*chan
;
1449 /* For each channel, ask the consumer to rotate it. */
1450 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1451 DBG("Rotate kernel channel %" PRIu64
", session %s",
1452 chan
->key
, session
->name
);
1453 ret
= consumer_rotate_channel(socket
, chan
->key
,
1454 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1455 ksess
->consumer
->domain_subdir
,
1456 /* is_metadata_channel */ false,
1457 session
->current_archive_id
);
1459 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1465 * Rotate the metadata channel.
1467 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1468 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1469 ksess
->consumer
->domain_subdir
,
1470 /* is_metadata_channel */ true,
1471 session
->current_archive_id
);
1473 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;