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.
25 #include <sys/types.h>
27 #include <common/common.h>
28 #include <common/trace-chunk.h>
29 #include <common/kernel-ctl/kernel-ctl.h>
30 #include <common/kernel-ctl/kernel-ioctl.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
33 #include "lttng-sessiond.h"
34 #include "lttng-syscall.h"
37 #include "kernel-consumer.h"
38 #include "kern-modules.h"
44 * Key used to reference a channel between the sessiond and the consumer. This
45 * is only read and updated with the session_list lock held.
47 static uint64_t next_kernel_channel_key
;
49 static const char *module_proc_lttng
= "/proc/lttng";
51 static int kernel_tracer_fd
= -1;
53 #include <lttng/userspace-probe.h>
54 #include <lttng/userspace-probe-internal.h>
56 * Add context on a kernel channel.
58 * Assumes the ownership of ctx.
60 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
61 struct ltt_kernel_context
*ctx
)
68 DBG("Adding context to channel %s", chan
->channel
->name
);
69 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
73 /* Exists but not available for this kernel */
74 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
77 /* If EEXIST, we just ignore the error */
81 PERROR("add context ioctl");
82 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
89 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
94 trace_kernel_destroy_context(ctx
);
100 * Create a new kernel session, register it to the kernel tracer and add it to
101 * the session daemon session.
103 int kernel_create_session(struct ltt_session
*session
)
106 struct ltt_kernel_session
*lks
;
110 /* Allocate data structure */
111 lks
= trace_kernel_create_session();
117 /* Kernel tracer session creation */
118 ret
= kernctl_create_session(kernel_tracer_fd
);
120 PERROR("ioctl kernel create session");
125 /* Prevent fd duplication after execlp() */
126 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
128 PERROR("fcntl session fd");
131 lks
->id
= session
->id
;
132 lks
->consumer_fds_sent
= 0;
133 session
->kernel_session
= lks
;
135 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * This is necessary since the creation time is present in the session
139 * name when it is generated.
141 if (session
->has_auto_generated_name
) {
142 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
144 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
147 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
148 session
->id
, session
->name
);
155 trace_kernel_destroy_session(lks
);
156 trace_kernel_free_session(lks
);
162 * Create a kernel channel, register it to the kernel tracer and add it to the
165 int kernel_create_channel(struct ltt_kernel_session
*session
,
166 struct lttng_channel
*chan
)
169 struct ltt_kernel_channel
*lkc
;
174 /* Allocate kernel channel */
175 lkc
= trace_kernel_create_channel(chan
);
180 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
181 chan
->name
, lkc
->channel
->attr
.overwrite
,
182 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
183 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
184 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
186 /* Kernel tracer channel creation */
187 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
189 PERROR("ioctl kernel create channel");
193 /* Setup the channel fd */
195 /* Prevent fd duplication after execlp() */
196 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
198 PERROR("fcntl session fd");
201 /* Add channel to session */
202 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
203 session
->channel_count
++;
204 lkc
->session
= session
;
205 lkc
->key
= ++next_kernel_channel_key
;
207 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
208 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
221 * Compute the offset of the instrumentation byte in the binary based on the
222 * function probe location using the ELF lookup method.
224 * Returns 0 on success and set the offset out parameter to the offset of the
226 * Returns -1 on error
229 int extract_userspace_probe_offset_function_elf(
230 const struct lttng_userspace_probe_location
*probe_location
,
231 struct ltt_kernel_session
*session
, uint64_t *offset
)
235 const char *symbol
= NULL
;
236 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
237 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
239 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
240 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
242 lookup
= lttng_userspace_probe_location_get_lookup_method(
250 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
252 assert(lookup_method_type
==
253 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
255 symbol
= lttng_userspace_probe_location_function_get_function_name(
262 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
268 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
269 session
->gid
, offset
);
271 DBG("userspace probe offset calculation failed for "
272 "function %s", symbol
);
276 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
282 * Compute the offsets of the instrumentation bytes in the binary based on the
283 * tracepoint probe location using the SDT lookup method. This function
284 * allocates the offsets buffer, the caller must free it.
286 * Returns 0 on success and set the offset out parameter to the offsets of the
288 * Returns -1 on error.
291 int extract_userspace_probe_offset_tracepoint_sdt(
292 const struct lttng_userspace_probe_location
*probe_location
,
293 struct ltt_kernel_session
*session
, uint64_t **offsets
,
294 uint32_t *offsets_count
)
296 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
297 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
298 const char *probe_name
= NULL
, *provider_name
= NULL
;
302 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
303 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
305 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
312 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
314 assert(lookup_method_type
==
315 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
318 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
325 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
327 if (!provider_name
) {
332 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
338 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
339 session
->uid
, session
->gid
, offsets
, offsets_count
);
341 DBG("userspace probe offset calculation failed for sdt "
342 "probe %s:%s", provider_name
, probe_name
);
346 if (*offsets_count
== 0) {
347 DBG("no userspace probe offset found");
351 DBG("%u userspace probe SDT offsets found for %s:%s at:",
352 *offsets_count
, provider_name
, probe_name
);
353 for (i
= 0; i
< *offsets_count
; i
++) {
354 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
361 * Extract the offsets of the instrumentation point for the different lookup
365 int userspace_probe_add_callsites(struct lttng_event
*ev
,
366 struct ltt_kernel_session
*session
, int fd
)
368 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
369 enum lttng_userspace_probe_location_lookup_method_type type
;
370 const struct lttng_userspace_probe_location
*location
= NULL
;
374 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
376 location
= lttng_event_get_userspace_probe_location(ev
);
382 lttng_userspace_probe_location_get_lookup_method(location
);
383 if (!lookup_method
) {
388 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
390 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
392 struct lttng_kernel_event_callsite callsite
;
395 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
397 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
401 callsite
.u
.uprobe
.offset
= offset
;
402 ret
= kernctl_add_callsite(fd
, &callsite
);
404 WARN("Adding callsite to userspace probe "
405 "event %s failed.", ev
->name
);
406 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
411 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
414 uint64_t *offsets
= NULL
;
415 uint32_t offsets_count
;
416 struct lttng_kernel_event_callsite callsite
;
419 * This call allocates the offsets buffer. This buffer must be freed
422 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
423 &offsets
, &offsets_count
);
425 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
428 for (i
= 0; i
< offsets_count
; i
++) {
429 callsite
.u
.uprobe
.offset
= offsets
[i
];
430 ret
= kernctl_add_callsite(fd
, &callsite
);
432 WARN("Adding callsite to userspace probe "
433 "event %s failed.", ev
->name
);
434 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
443 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
451 * Create a kernel event, enable it to the kernel tracer and add it to the
452 * channel event list of the kernel session.
453 * We own filter_expression and filter.
455 int kernel_create_event(struct lttng_event
*ev
,
456 struct ltt_kernel_channel
*channel
,
457 char *filter_expression
,
458 struct lttng_filter_bytecode
*filter
)
461 enum lttng_error_code ret
;
462 struct ltt_kernel_event
*event
;
467 /* We pass ownership of filter_expression and filter */
468 ret
= trace_kernel_create_event(ev
, filter_expression
,
470 if (ret
!= LTTNG_OK
) {
474 fd
= kernctl_create_event(channel
->fd
, event
->event
);
478 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
481 WARN("Event type not implemented");
482 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
485 WARN("Event %s not found!", ev
->name
);
486 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
489 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
490 PERROR("create event ioctl");
495 event
->type
= ev
->type
;
497 /* Prevent fd duplication after execlp() */
498 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
500 PERROR("fcntl session fd");
504 err
= kernctl_filter(event
->fd
, filter
);
508 ret
= LTTNG_ERR_FILTER_NOMEM
;
511 ret
= LTTNG_ERR_FILTER_INVAL
;
518 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
519 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
521 goto add_callsite_error
;
525 err
= kernctl_enable(event
->fd
);
529 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
532 PERROR("enable kernel event");
533 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
539 /* Add event to event list */
540 cds_list_add(&event
->list
, &channel
->events_list
.head
);
541 channel
->event_count
++;
543 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
553 closeret
= close(event
->fd
);
555 PERROR("close event fd");
565 * Disable a kernel channel.
567 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
573 ret
= kernctl_disable(chan
->fd
);
575 PERROR("disable chan ioctl");
580 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
581 chan
->channel
->name
, chan
->fd
, chan
->key
);
590 * Enable a kernel channel.
592 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
598 ret
= kernctl_enable(chan
->fd
);
599 if (ret
< 0 && ret
!= -EEXIST
) {
600 PERROR("Enable kernel chan");
605 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
606 chan
->channel
->name
, chan
->fd
, chan
->key
);
615 * Enable a kernel event.
617 int kernel_enable_event(struct ltt_kernel_event
*event
)
623 ret
= kernctl_enable(event
->fd
);
627 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
630 PERROR("enable kernel event");
637 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
646 * Disable a kernel event.
648 int kernel_disable_event(struct ltt_kernel_event
*event
)
654 ret
= kernctl_disable(event
->fd
);
658 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
661 PERROR("disable kernel event");
668 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
677 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
681 DBG("Kernel track PID %d for session id %" PRIu64
".",
683 ret
= kernctl_track_pid(session
->fd
, pid
);
689 return LTTNG_ERR_INVALID
;
691 return LTTNG_ERR_NOMEM
;
693 return LTTNG_ERR_PID_TRACKED
;
695 return LTTNG_ERR_UNK
;
699 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
703 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
705 ret
= kernctl_untrack_pid(session
->fd
, pid
);
711 return LTTNG_ERR_INVALID
;
713 return LTTNG_ERR_NOMEM
;
715 return LTTNG_ERR_PID_NOT_TRACKED
;
717 return LTTNG_ERR_UNK
;
721 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
726 ssize_t nbmem
, count
= 0;
730 fd
= kernctl_list_tracker_pids(session
->fd
);
732 PERROR("kernel tracker pids list");
736 fp
= fdopen(fd
, "r");
738 PERROR("kernel tracker pids list fdopen");
742 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
743 pids
= zmalloc(sizeof(*pids
) * nbmem
);
745 PERROR("alloc list pids");
750 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
751 if (count
>= nbmem
) {
755 new_nbmem
= nbmem
<< 1;
756 DBG("Reallocating pids list from %zu to %zu entries",
758 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
759 if (new_pids
== NULL
) {
760 PERROR("realloc list events");
765 /* Zero the new memory */
766 memset(new_pids
+ nbmem
, 0,
767 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
775 DBG("Kernel list tracker pids done (%zd pids)", count
);
777 ret
= fclose(fp
); /* closes both fp and fd */
793 * Create kernel metadata, open from the kernel tracer and add it to the
796 int kernel_open_metadata(struct ltt_kernel_session
*session
)
799 struct ltt_kernel_metadata
*lkm
= NULL
;
803 /* Allocate kernel metadata */
804 lkm
= trace_kernel_create_metadata();
809 /* Kernel tracer metadata creation */
810 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
816 lkm
->key
= ++next_kernel_channel_key
;
817 /* Prevent fd duplication after execlp() */
818 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
820 PERROR("fcntl session fd");
823 session
->metadata
= lkm
;
825 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
830 trace_kernel_destroy_metadata(lkm
);
836 * Start tracing session.
838 int kernel_start_session(struct ltt_kernel_session
*session
)
844 ret
= kernctl_start_session(session
->fd
);
846 PERROR("ioctl start session");
850 DBG("Kernel session started");
859 * Make a kernel wait to make sure in-flight probe have completed.
861 void kernel_wait_quiescent(void)
864 int fd
= kernel_tracer_fd
;
866 DBG("Kernel quiescent wait on %d", fd
);
868 ret
= kernctl_wait_quiescent(fd
);
870 PERROR("wait quiescent ioctl");
871 ERR("Kernel quiescent wait failed");
876 * Force flush buffer of metadata.
878 int kernel_metadata_flush_buffer(int fd
)
882 DBG("Kernel flushing metadata buffer on fd %d", fd
);
884 ret
= kernctl_buffer_flush(fd
);
886 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
893 * Force flush buffer for channel.
895 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
898 struct ltt_kernel_stream
*stream
;
902 DBG("Flush buffer for channel %s", channel
->channel
->name
);
904 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
905 DBG("Flushing channel stream %d", stream
->fd
);
906 ret
= kernctl_buffer_flush(stream
->fd
);
909 ERR("Fail to flush buffer for stream %d (ret: %d)",
918 * Stop tracing session.
920 int kernel_stop_session(struct ltt_kernel_session
*session
)
926 ret
= kernctl_stop_session(session
->fd
);
931 DBG("Kernel session stopped");
940 * Open stream of channel, register it to the kernel tracer and add it
941 * to the stream list of the channel.
943 * Note: given that the streams may appear in random order wrt CPU
944 * number (e.g. cpu hotplug), the index value of the stream number in
945 * the stream name is not necessarily linked to the CPU number.
947 * Return the number of created stream. Else, a negative value.
949 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
952 struct ltt_kernel_stream
*lks
;
956 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
957 lks
= trace_kernel_create_stream(channel
->channel
->name
,
958 channel
->stream_count
);
968 /* Prevent fd duplication after execlp() */
969 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
971 PERROR("fcntl session fd");
974 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
975 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
977 /* Add stream to channel stream list */
978 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
979 channel
->stream_count
++;
981 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
985 return channel
->stream_count
;
992 * Open the metadata stream and set it to the kernel session.
994 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1000 ret
= kernctl_create_stream(session
->metadata
->fd
);
1002 PERROR("kernel create metadata stream");
1006 DBG("Kernel metadata stream created (fd: %d)", ret
);
1007 session
->metadata_stream_fd
= ret
;
1008 /* Prevent fd duplication after execlp() */
1009 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1011 PERROR("fcntl session fd");
1021 * Get the event list from the kernel tracer and return the number of elements.
1023 ssize_t
kernel_list_events(struct lttng_event
**events
)
1027 size_t nbmem
, count
= 0;
1029 struct lttng_event
*elist
;
1033 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1035 PERROR("kernel tracepoint list");
1039 fp
= fdopen(fd
, "r");
1041 PERROR("kernel tracepoint list fdopen");
1046 * Init memory size counter
1047 * See kernel-ctl.h for explanation of this value
1049 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1050 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1051 if (elist
== NULL
) {
1052 PERROR("alloc list events");
1057 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1058 if (count
>= nbmem
) {
1059 struct lttng_event
*new_elist
;
1062 new_nbmem
= nbmem
<< 1;
1063 DBG("Reallocating event list from %zu to %zu bytes",
1065 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1066 if (new_elist
== NULL
) {
1067 PERROR("realloc list events");
1073 /* Zero the new memory */
1074 memset(new_elist
+ nbmem
, 0,
1075 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1079 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1080 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1081 elist
[count
].enabled
= -1;
1087 DBG("Kernel list events done (%zu events)", count
);
1089 ret
= fclose(fp
); /* closes both fp and fd */
1105 * Get kernel version and validate it.
1107 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1108 struct lttng_kernel_tracer_abi_version
*abi_version
)
1112 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1114 ERR("Failed to retrieve the lttng-modules version");
1118 /* Validate version */
1119 if (version
->major
!= VERSION_MAJOR
) {
1120 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1121 version
->major
, VERSION_MAJOR
);
1124 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1126 ERR("Failed to retrieve lttng-modules ABI version");
1129 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1130 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1131 abi_version
->major
, abi_version
->minor
,
1132 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1135 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1136 version
->major
, version
->minor
,
1137 abi_version
->major
, abi_version
->minor
);
1144 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1149 * Kernel work-arounds called at the start of sessiond main().
1151 int init_kernel_workarounds(void)
1157 * boot_id needs to be read once before being used concurrently
1158 * to deal with a Linux kernel race. A fix is proposed for
1159 * upstream, but the work-around is needed for older kernels.
1161 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1168 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1170 /* Ignore error, we don't really care */
1182 * Teardown of a kernel session, keeping data required by destroy notifiers.
1184 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1186 struct lttng_trace_chunk
*trace_chunk
;
1188 if (ksess
== NULL
) {
1189 DBG3("No kernel session when tearing down session");
1193 DBG("Tearing down kernel session");
1194 trace_chunk
= ksess
->current_trace_chunk
;
1197 * Destroy channels on the consumer if at least one FD has been sent and we
1198 * are in no output mode because the streams are in *no* monitor mode so we
1199 * have to send a command to clean them up or else they leaked.
1201 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1203 struct consumer_socket
*socket
;
1204 struct lttng_ht_iter iter
;
1206 /* For each consumer socket. */
1208 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1209 socket
, node
.node
) {
1210 struct ltt_kernel_channel
*chan
;
1212 /* For each channel, ask the consumer to destroy it. */
1213 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1214 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1216 /* Consumer is probably dead. Use next socket. */
1224 /* Close any relayd session */
1225 consumer_output_send_destroy_relayd(ksess
->consumer
);
1227 trace_kernel_destroy_session(ksess
);
1228 lttng_trace_chunk_put(trace_chunk
);
1231 /* Teardown of data required by destroy notifiers. */
1232 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1234 if (ksess
== NULL
) {
1237 trace_kernel_free_session(ksess
);
1241 * Destroy a kernel channel object. It does not do anything on the tracer side.
1243 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1245 struct ltt_kernel_session
*ksess
= NULL
;
1248 assert(kchan
->channel
);
1250 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1252 /* Update channel count of associated session. */
1253 if (kchan
->session
) {
1254 /* Keep pointer reference so we can update it after the destroy. */
1255 ksess
= kchan
->session
;
1258 trace_kernel_destroy_channel(kchan
);
1261 * At this point the kernel channel is not visible anymore. This is safe
1262 * since in order to work on a visible kernel session, the tracing session
1263 * lock (ltt_session.lock) MUST be acquired.
1266 ksess
->channel_count
--;
1271 * Take a snapshot for a given kernel session.
1273 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1275 enum lttng_error_code
kernel_snapshot_record(
1276 struct ltt_kernel_session
*ksess
,
1277 const struct consumer_output
*output
, int wait
,
1278 uint64_t nb_packets_per_stream
)
1280 int err
, ret
, saved_metadata_fd
;
1281 enum lttng_error_code status
= LTTNG_OK
;
1282 struct consumer_socket
*socket
;
1283 struct lttng_ht_iter iter
;
1284 struct ltt_kernel_metadata
*saved_metadata
;
1285 char *trace_path
= NULL
;
1288 assert(ksess
->consumer
);
1291 DBG("Kernel snapshot record started");
1293 /* Save current metadata since the following calls will change it. */
1294 saved_metadata
= ksess
->metadata
;
1295 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1299 ret
= kernel_open_metadata(ksess
);
1301 status
= LTTNG_ERR_KERN_META_FAIL
;
1305 ret
= kernel_open_metadata_stream(ksess
);
1307 status
= LTTNG_ERR_KERN_META_FAIL
;
1308 goto error_open_stream
;
1311 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1312 DEFAULT_KERNEL_TRACE_DIR
);
1314 status
= LTTNG_ERR_INVALID
;
1317 /* Send metadata to consumer and snapshot everything. */
1318 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1319 socket
, node
.node
) {
1320 struct ltt_kernel_channel
*chan
;
1322 pthread_mutex_lock(socket
->lock
);
1323 /* This stream must not be monitored by the consumer. */
1324 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1325 pthread_mutex_unlock(socket
->lock
);
1327 status
= LTTNG_ERR_KERN_META_FAIL
;
1328 goto error_consumer
;
1331 /* For each channel, ask the consumer to snapshot it. */
1332 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1333 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1334 ksess
->uid
, ksess
->gid
,
1336 nb_packets_per_stream
);
1337 if (status
!= LTTNG_OK
) {
1338 (void) kernel_consumer_destroy_metadata(socket
,
1340 goto error_consumer
;
1344 /* Snapshot metadata, */
1345 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1346 1, ksess
->uid
, ksess
->gid
, trace_path
, wait
, 0);
1347 if (status
!= LTTNG_OK
) {
1348 goto error_consumer
;
1352 * The metadata snapshot is done, ask the consumer to destroy it since
1353 * it's not monitored on the consumer side.
1355 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1359 /* Close newly opened metadata stream. It's now on the consumer side. */
1360 err
= close(ksess
->metadata_stream_fd
);
1362 PERROR("close snapshot kernel");
1366 trace_kernel_destroy_metadata(ksess
->metadata
);
1368 /* Restore metadata state.*/
1369 ksess
->metadata
= saved_metadata
;
1370 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1377 * Get the syscall mask array from the kernel tracer.
1379 * Return 0 on success else a negative value. In both case, syscall_mask should
1382 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1384 assert(syscall_mask
);
1387 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1391 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1394 * Return 1 on success, 0 when feature is not supported, negative value in case
1397 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1399 int ret
= 0; // Not supported by default
1400 struct lttng_kernel_tracer_abi_version abi
;
1402 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1404 ERR("Failed to retrieve lttng-modules ABI version");
1409 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1411 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1423 * Rotate a kernel session.
1425 * Return LTTNG_OK on success or else an LTTng error code.
1427 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1430 enum lttng_error_code status
= LTTNG_OK
;
1431 struct consumer_socket
*socket
;
1432 struct lttng_ht_iter iter
;
1433 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1436 assert(ksess
->consumer
);
1438 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1439 session
->name
, session
->id
);
1444 * Note that this loop will end after one iteration given that there is
1445 * only one kernel consumer.
1447 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1448 socket
, node
.node
) {
1449 struct ltt_kernel_channel
*chan
;
1451 /* For each channel, ask the consumer to rotate it. */
1452 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1453 DBG("Rotate kernel channel %" PRIu64
", session %s",
1454 chan
->key
, session
->name
);
1455 ret
= consumer_rotate_channel(socket
, chan
->key
,
1456 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1457 /* is_metadata_channel */ false);
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 /* is_metadata_channel */ true);
1471 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1481 enum lttng_error_code
kernel_create_channel_subdirectories(
1482 const struct ltt_kernel_session
*ksess
)
1484 enum lttng_error_code ret
= LTTNG_OK
;
1485 enum lttng_trace_chunk_status chunk_status
;
1488 assert(ksess
->current_trace_chunk
);
1491 * Create the index subdirectory which will take care
1492 * of implicitly creating the channel's path.
1494 chunk_status
= lttng_trace_chunk_create_subdirectory(
1495 ksess
->current_trace_chunk
,
1496 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1497 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1498 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1507 * Setup necessary data for kernel tracer action.
1510 int init_kernel_tracer(void)
1513 bool is_root
= !getuid();
1515 /* Modprobe lttng kernel modules */
1516 ret
= modprobe_lttng_control();
1521 /* Open debugfs lttng */
1522 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1523 if (kernel_tracer_fd
< 0) {
1524 DBG("Failed to open %s", module_proc_lttng
);
1528 /* Validate kernel version */
1529 ret
= kernel_validate_version(&kernel_tracer_version
,
1530 &kernel_tracer_abi_version
);
1535 ret
= modprobe_lttng_data();
1540 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1546 WARN("Kernel tracer does not support buffer monitoring. "
1547 "The monitoring timer of channels in the kernel domain "
1548 "will be set to 0 (disabled).");
1551 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1553 ret
= syscall_init_table(kernel_tracer_fd
);
1555 ERR("Unable to populate syscall table. Syscall tracing won't "
1556 "work for this session daemon.");
1561 modprobe_remove_lttng_control();
1562 ret
= close(kernel_tracer_fd
);
1566 kernel_tracer_fd
= -1;
1567 return LTTNG_ERR_KERN_VERSION
;
1570 ret
= close(kernel_tracer_fd
);
1576 modprobe_remove_lttng_control();
1579 WARN("No kernel tracer available");
1580 kernel_tracer_fd
= -1;
1582 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1584 return LTTNG_ERR_KERN_NA
;
1589 void cleanup_kernel_tracer(void)
1593 DBG2("Closing kernel fd");
1594 if (kernel_tracer_fd
>= 0) {
1595 ret
= close(kernel_tracer_fd
);
1599 kernel_tracer_fd
= -1;
1601 DBG("Unloading kernel modules");
1602 modprobe_remove_lttng_all();
1603 free(syscall_table
);
1607 bool kernel_tracer_is_initialized(void)
1609 return kernel_tracer_fd
>= 0;