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 <sys/types.h>
28 #include <common/common.h>
29 #include <common/trace-chunk.h>
30 #include <common/kernel-ctl/kernel-ctl.h>
31 #include <common/kernel-ctl/kernel-ioctl.h>
32 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "lttng-sessiond.h"
35 #include "lttng-syscall.h"
38 #include "kernel-consumer.h"
39 #include "kern-modules.h"
45 * Key used to reference a channel between the sessiond and the consumer. This
46 * is only read and updated with the session_list lock held.
48 static uint64_t next_kernel_channel_key
;
50 static const char *module_proc_lttng
= "/proc/lttng";
52 static int kernel_tracer_fd
= -1;
54 #include <lttng/userspace-probe.h>
55 #include <lttng/userspace-probe-internal.h>
57 * Add context on a kernel channel.
59 * Assumes the ownership of ctx.
61 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
62 struct ltt_kernel_context
*ctx
)
69 DBG("Adding context to channel %s", chan
->channel
->name
);
70 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
74 /* Exists but not available for this kernel */
75 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
78 /* If EEXIST, we just ignore the error */
82 PERROR("add context ioctl");
83 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
90 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
95 trace_kernel_destroy_context(ctx
);
101 * Create a new kernel session, register it to the kernel tracer and add it to
102 * the session daemon session.
104 int kernel_create_session(struct ltt_session
*session
)
107 struct ltt_kernel_session
*lks
;
111 /* Allocate data structure */
112 lks
= trace_kernel_create_session();
118 /* Kernel tracer session creation */
119 ret
= kernctl_create_session(kernel_tracer_fd
);
121 PERROR("ioctl kernel create session");
126 /* Prevent fd duplication after execlp() */
127 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
129 PERROR("fcntl session fd");
132 lks
->id
= session
->id
;
133 lks
->consumer_fds_sent
= 0;
134 session
->kernel_session
= lks
;
136 DBG("Kernel session created (fd: %d)", lks
->fd
);
139 * This is necessary since the creation time is present in the session
140 * name when it is generated.
142 if (session
->has_auto_generated_name
) {
143 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
145 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
148 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
149 session
->id
, session
->name
);
152 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
154 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
155 session
->id
, session
->name
);
162 trace_kernel_destroy_session(lks
);
163 trace_kernel_free_session(lks
);
169 * Create a kernel channel, register it to the kernel tracer and add it to the
172 int kernel_create_channel(struct ltt_kernel_session
*session
,
173 struct lttng_channel
*chan
)
176 struct ltt_kernel_channel
*lkc
;
181 /* Allocate kernel channel */
182 lkc
= trace_kernel_create_channel(chan
);
187 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
188 chan
->name
, lkc
->channel
->attr
.overwrite
,
189 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
190 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
191 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
193 /* Kernel tracer channel creation */
194 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
196 PERROR("ioctl kernel create channel");
200 /* Setup the channel fd */
202 /* Prevent fd duplication after execlp() */
203 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
205 PERROR("fcntl session fd");
208 /* Add channel to session */
209 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
210 session
->channel_count
++;
211 lkc
->session
= session
;
212 lkc
->key
= ++next_kernel_channel_key
;
214 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
215 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
228 * Compute the offset of the instrumentation byte in the binary based on the
229 * function probe location using the ELF lookup method.
231 * Returns 0 on success and set the offset out parameter to the offset of the
233 * Returns -1 on error
236 int extract_userspace_probe_offset_function_elf(
237 const struct lttng_userspace_probe_location
*probe_location
,
238 struct ltt_kernel_session
*session
, uint64_t *offset
)
242 const char *symbol
= NULL
;
243 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
244 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
246 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
247 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
249 lookup
= lttng_userspace_probe_location_get_lookup_method(
257 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
259 assert(lookup_method_type
==
260 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
262 symbol
= lttng_userspace_probe_location_function_get_function_name(
269 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
275 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
276 session
->gid
, offset
);
278 DBG("userspace probe offset calculation failed for "
279 "function %s", symbol
);
283 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
289 * Compute the offsets of the instrumentation bytes in the binary based on the
290 * tracepoint probe location using the SDT lookup method. This function
291 * allocates the offsets buffer, the caller must free it.
293 * Returns 0 on success and set the offset out parameter to the offsets of the
295 * Returns -1 on error.
298 int extract_userspace_probe_offset_tracepoint_sdt(
299 const struct lttng_userspace_probe_location
*probe_location
,
300 struct ltt_kernel_session
*session
, uint64_t **offsets
,
301 uint32_t *offsets_count
)
303 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
304 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
305 const char *probe_name
= NULL
, *provider_name
= NULL
;
309 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
310 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
312 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
319 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
321 assert(lookup_method_type
==
322 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
325 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
332 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
334 if (!provider_name
) {
339 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
345 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
346 session
->uid
, session
->gid
, offsets
, offsets_count
);
348 DBG("userspace probe offset calculation failed for sdt "
349 "probe %s:%s", provider_name
, probe_name
);
353 if (*offsets_count
== 0) {
354 DBG("no userspace probe offset found");
358 DBG("%u userspace probe SDT offsets found for %s:%s at:",
359 *offsets_count
, provider_name
, probe_name
);
360 for (i
= 0; i
< *offsets_count
; i
++) {
361 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
368 * Extract the offsets of the instrumentation point for the different lookup
372 int userspace_probe_add_callsites(struct lttng_event
*ev
,
373 struct ltt_kernel_session
*session
, int fd
)
375 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
376 enum lttng_userspace_probe_location_lookup_method_type type
;
377 const struct lttng_userspace_probe_location
*location
= NULL
;
381 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
383 location
= lttng_event_get_userspace_probe_location(ev
);
389 lttng_userspace_probe_location_get_lookup_method(location
);
390 if (!lookup_method
) {
395 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
397 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
399 struct lttng_kernel_event_callsite callsite
;
402 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
404 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
408 callsite
.u
.uprobe
.offset
= offset
;
409 ret
= kernctl_add_callsite(fd
, &callsite
);
411 WARN("Adding callsite to userspace probe "
412 "event %s failed.", ev
->name
);
413 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
418 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
421 uint64_t *offsets
= NULL
;
422 uint32_t offsets_count
;
423 struct lttng_kernel_event_callsite callsite
;
426 * This call allocates the offsets buffer. This buffer must be freed
429 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
430 &offsets
, &offsets_count
);
432 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
435 for (i
= 0; i
< offsets_count
; i
++) {
436 callsite
.u
.uprobe
.offset
= offsets
[i
];
437 ret
= kernctl_add_callsite(fd
, &callsite
);
439 WARN("Adding callsite to userspace probe "
440 "event %s failed.", ev
->name
);
441 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
450 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
458 * Create a kernel event, enable it to the kernel tracer and add it to the
459 * channel event list of the kernel session.
460 * We own filter_expression and filter.
462 int kernel_create_event(struct lttng_event
*ev
,
463 struct ltt_kernel_channel
*channel
,
464 char *filter_expression
,
465 struct lttng_filter_bytecode
*filter
)
468 enum lttng_error_code ret
;
469 struct ltt_kernel_event
*event
;
474 /* We pass ownership of filter_expression and filter */
475 ret
= trace_kernel_create_event(ev
, filter_expression
,
477 if (ret
!= LTTNG_OK
) {
481 fd
= kernctl_create_event(channel
->fd
, event
->event
);
485 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
488 WARN("Event type not implemented");
489 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
492 WARN("Event %s not found!", ev
->name
);
493 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
496 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
497 PERROR("create event ioctl");
502 event
->type
= ev
->type
;
504 /* Prevent fd duplication after execlp() */
505 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
507 PERROR("fcntl session fd");
511 err
= kernctl_filter(event
->fd
, filter
);
515 ret
= LTTNG_ERR_FILTER_NOMEM
;
518 ret
= LTTNG_ERR_FILTER_INVAL
;
525 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
526 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
528 goto add_callsite_error
;
532 err
= kernctl_enable(event
->fd
);
536 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
539 PERROR("enable kernel event");
540 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
546 /* Add event to event list */
547 cds_list_add(&event
->list
, &channel
->events_list
.head
);
548 channel
->event_count
++;
550 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
560 closeret
= close(event
->fd
);
562 PERROR("close event fd");
572 * Disable a kernel channel.
574 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
580 ret
= kernctl_disable(chan
->fd
);
582 PERROR("disable chan ioctl");
587 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
588 chan
->channel
->name
, chan
->fd
, chan
->key
);
597 * Enable a kernel channel.
599 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
605 ret
= kernctl_enable(chan
->fd
);
606 if (ret
< 0 && ret
!= -EEXIST
) {
607 PERROR("Enable kernel chan");
612 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
613 chan
->channel
->name
, chan
->fd
, chan
->key
);
622 * Enable a kernel event.
624 int kernel_enable_event(struct ltt_kernel_event
*event
)
630 ret
= kernctl_enable(event
->fd
);
634 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
637 PERROR("enable kernel event");
644 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
653 * Disable a kernel event.
655 int kernel_disable_event(struct ltt_kernel_event
*event
)
661 ret
= kernctl_disable(event
->fd
);
663 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
664 event
->event
->name
, event
->fd
);
669 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
678 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
682 DBG("Kernel track PID %d for session id %" PRIu64
".",
684 ret
= kernctl_track_pid(session
->fd
, pid
);
690 return LTTNG_ERR_INVALID
;
692 return LTTNG_ERR_NOMEM
;
694 return LTTNG_ERR_PID_TRACKED
;
696 return LTTNG_ERR_UNK
;
700 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
704 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
706 ret
= kernctl_untrack_pid(session
->fd
, pid
);
712 return LTTNG_ERR_INVALID
;
714 return LTTNG_ERR_NOMEM
;
716 return LTTNG_ERR_PID_NOT_TRACKED
;
718 return LTTNG_ERR_UNK
;
722 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
727 ssize_t nbmem
, count
= 0;
731 fd
= kernctl_list_tracker_pids(session
->fd
);
733 PERROR("kernel tracker pids list");
737 fp
= fdopen(fd
, "r");
739 PERROR("kernel tracker pids list fdopen");
743 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
744 pids
= zmalloc(sizeof(*pids
) * nbmem
);
746 PERROR("alloc list pids");
751 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
752 if (count
>= nbmem
) {
756 new_nbmem
= nbmem
<< 1;
757 DBG("Reallocating pids list from %zu to %zu entries",
759 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
760 if (new_pids
== NULL
) {
761 PERROR("realloc list events");
766 /* Zero the new memory */
767 memset(new_pids
+ nbmem
, 0,
768 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
776 DBG("Kernel list tracker pids done (%zd pids)", count
);
778 ret
= fclose(fp
); /* closes both fp and fd */
794 * Create kernel metadata, open from the kernel tracer and add it to the
797 int kernel_open_metadata(struct ltt_kernel_session
*session
)
800 struct ltt_kernel_metadata
*lkm
= NULL
;
804 /* Allocate kernel metadata */
805 lkm
= trace_kernel_create_metadata();
810 /* Kernel tracer metadata creation */
811 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
817 lkm
->key
= ++next_kernel_channel_key
;
818 /* Prevent fd duplication after execlp() */
819 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
821 PERROR("fcntl session fd");
824 session
->metadata
= lkm
;
826 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
831 trace_kernel_destroy_metadata(lkm
);
837 * Start tracing session.
839 int kernel_start_session(struct ltt_kernel_session
*session
)
845 ret
= kernctl_start_session(session
->fd
);
847 PERROR("ioctl start session");
851 DBG("Kernel session started");
860 * Make a kernel wait to make sure in-flight probe have completed.
862 void kernel_wait_quiescent(void)
865 int fd
= kernel_tracer_fd
;
867 DBG("Kernel quiescent wait on %d", fd
);
869 ret
= kernctl_wait_quiescent(fd
);
871 PERROR("wait quiescent ioctl");
872 ERR("Kernel quiescent wait failed");
877 * Force flush buffer of metadata.
879 int kernel_metadata_flush_buffer(int fd
)
883 DBG("Kernel flushing metadata buffer on fd %d", fd
);
885 ret
= kernctl_buffer_flush(fd
);
887 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
894 * Force flush buffer for channel.
896 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
899 struct ltt_kernel_stream
*stream
;
903 DBG("Flush buffer for channel %s", channel
->channel
->name
);
905 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
906 DBG("Flushing channel stream %d", stream
->fd
);
907 ret
= kernctl_buffer_flush(stream
->fd
);
910 ERR("Fail to flush buffer for stream %d (ret: %d)",
919 * Stop tracing session.
921 int kernel_stop_session(struct ltt_kernel_session
*session
)
927 ret
= kernctl_stop_session(session
->fd
);
932 DBG("Kernel session stopped");
941 * Open stream of channel, register it to the kernel tracer and add it
942 * to the stream list of the channel.
944 * Note: given that the streams may appear in random order wrt CPU
945 * number (e.g. cpu hotplug), the index value of the stream number in
946 * the stream name is not necessarily linked to the CPU number.
948 * Return the number of created stream. Else, a negative value.
950 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
953 struct ltt_kernel_stream
*lks
;
957 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
958 lks
= trace_kernel_create_stream(channel
->channel
->name
,
959 channel
->stream_count
);
969 /* Prevent fd duplication after execlp() */
970 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
972 PERROR("fcntl session fd");
975 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
976 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
978 /* Add stream to channel stream list */
979 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
980 channel
->stream_count
++;
982 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
986 return channel
->stream_count
;
993 * Open the metadata stream and set it to the kernel session.
995 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1001 ret
= kernctl_create_stream(session
->metadata
->fd
);
1003 PERROR("kernel create metadata stream");
1007 DBG("Kernel metadata stream created (fd: %d)", ret
);
1008 session
->metadata_stream_fd
= ret
;
1009 /* Prevent fd duplication after execlp() */
1010 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1012 PERROR("fcntl session fd");
1022 * Get the event list from the kernel tracer and return the number of elements.
1024 ssize_t
kernel_list_events(struct lttng_event
**events
)
1028 size_t nbmem
, count
= 0;
1030 struct lttng_event
*elist
;
1034 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1036 PERROR("kernel tracepoint list");
1040 fp
= fdopen(fd
, "r");
1042 PERROR("kernel tracepoint list fdopen");
1047 * Init memory size counter
1048 * See kernel-ctl.h for explanation of this value
1050 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1051 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1052 if (elist
== NULL
) {
1053 PERROR("alloc list events");
1058 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1059 if (count
>= nbmem
) {
1060 struct lttng_event
*new_elist
;
1063 new_nbmem
= nbmem
<< 1;
1064 DBG("Reallocating event list from %zu to %zu bytes",
1066 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1067 if (new_elist
== NULL
) {
1068 PERROR("realloc list events");
1074 /* Zero the new memory */
1075 memset(new_elist
+ nbmem
, 0,
1076 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1080 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1081 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1082 elist
[count
].enabled
= -1;
1088 DBG("Kernel list events done (%zu events)", count
);
1090 ret
= fclose(fp
); /* closes both fp and fd */
1106 * Get kernel version and validate it.
1108 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1109 struct lttng_kernel_tracer_abi_version
*abi_version
)
1113 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1115 ERR("Failed to retrieve the lttng-modules version");
1119 /* Validate version */
1120 if (version
->major
!= VERSION_MAJOR
) {
1121 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1122 version
->major
, VERSION_MAJOR
);
1125 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1127 ERR("Failed to retrieve lttng-modules ABI version");
1130 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1131 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1132 abi_version
->major
, abi_version
->minor
,
1133 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1136 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1137 version
->major
, version
->minor
,
1138 abi_version
->major
, abi_version
->minor
);
1145 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1150 * Kernel work-arounds called at the start of sessiond main().
1152 int init_kernel_workarounds(void)
1158 * boot_id needs to be read once before being used concurrently
1159 * to deal with a Linux kernel race. A fix is proposed for
1160 * upstream, but the work-around is needed for older kernels.
1162 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1169 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1171 /* Ignore error, we don't really care */
1183 * Teardown of a kernel session, keeping data required by destroy notifiers.
1185 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1187 struct lttng_trace_chunk
*trace_chunk
;
1189 if (ksess
== NULL
) {
1190 DBG3("No kernel session when tearing down session");
1194 DBG("Tearing down kernel session");
1195 trace_chunk
= ksess
->current_trace_chunk
;
1198 * Destroy channels on the consumer if at least one FD has been sent and we
1199 * are in no output mode because the streams are in *no* monitor mode so we
1200 * have to send a command to clean them up or else they leaked.
1202 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1204 struct consumer_socket
*socket
;
1205 struct lttng_ht_iter iter
;
1207 /* For each consumer socket. */
1209 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1210 socket
, node
.node
) {
1211 struct ltt_kernel_channel
*chan
;
1213 /* For each channel, ask the consumer to destroy it. */
1214 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1215 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1217 /* Consumer is probably dead. Use next socket. */
1225 /* Close any relayd session */
1226 consumer_output_send_destroy_relayd(ksess
->consumer
);
1228 trace_kernel_destroy_session(ksess
);
1229 lttng_trace_chunk_put(trace_chunk
);
1232 /* Teardown of data required by destroy notifiers. */
1233 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1235 if (ksess
== NULL
) {
1238 trace_kernel_free_session(ksess
);
1242 * Destroy a kernel channel object. It does not do anything on the tracer side.
1244 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1246 struct ltt_kernel_session
*ksess
= NULL
;
1249 assert(kchan
->channel
);
1251 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1253 /* Update channel count of associated session. */
1254 if (kchan
->session
) {
1255 /* Keep pointer reference so we can update it after the destroy. */
1256 ksess
= kchan
->session
;
1259 trace_kernel_destroy_channel(kchan
);
1262 * At this point the kernel channel is not visible anymore. This is safe
1263 * since in order to work on a visible kernel session, the tracing session
1264 * lock (ltt_session.lock) MUST be acquired.
1267 ksess
->channel_count
--;
1272 * Take a snapshot for a given kernel session.
1274 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1276 enum lttng_error_code
kernel_snapshot_record(
1277 struct ltt_kernel_session
*ksess
,
1278 const struct consumer_output
*output
, int wait
,
1279 uint64_t nb_packets_per_stream
)
1281 int err
, ret
, saved_metadata_fd
;
1282 enum lttng_error_code status
= LTTNG_OK
;
1283 struct consumer_socket
*socket
;
1284 struct lttng_ht_iter iter
;
1285 struct ltt_kernel_metadata
*saved_metadata
;
1286 char *trace_path
= NULL
;
1289 assert(ksess
->consumer
);
1292 DBG("Kernel snapshot record started");
1294 /* Save current metadata since the following calls will change it. */
1295 saved_metadata
= ksess
->metadata
;
1296 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1300 ret
= kernel_open_metadata(ksess
);
1302 status
= LTTNG_ERR_KERN_META_FAIL
;
1306 ret
= kernel_open_metadata_stream(ksess
);
1308 status
= LTTNG_ERR_KERN_META_FAIL
;
1309 goto error_open_stream
;
1312 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1313 DEFAULT_KERNEL_TRACE_DIR
);
1315 status
= LTTNG_ERR_INVALID
;
1318 /* Send metadata to consumer and snapshot everything. */
1319 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1320 socket
, node
.node
) {
1321 struct ltt_kernel_channel
*chan
;
1323 pthread_mutex_lock(socket
->lock
);
1324 /* This stream must not be monitored by the consumer. */
1325 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1326 pthread_mutex_unlock(socket
->lock
);
1328 status
= LTTNG_ERR_KERN_META_FAIL
;
1329 goto error_consumer
;
1332 /* For each channel, ask the consumer to snapshot it. */
1333 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1334 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1335 ksess
->uid
, ksess
->gid
,
1337 nb_packets_per_stream
);
1338 if (status
!= LTTNG_OK
) {
1339 (void) kernel_consumer_destroy_metadata(socket
,
1341 goto error_consumer
;
1345 /* Snapshot metadata, */
1346 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1347 1, ksess
->uid
, ksess
->gid
, trace_path
, wait
, 0);
1348 if (status
!= LTTNG_OK
) {
1349 goto error_consumer
;
1353 * The metadata snapshot is done, ask the consumer to destroy it since
1354 * it's not monitored on the consumer side.
1356 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1360 /* Close newly opened metadata stream. It's now on the consumer side. */
1361 err
= close(ksess
->metadata_stream_fd
);
1363 PERROR("close snapshot kernel");
1367 trace_kernel_destroy_metadata(ksess
->metadata
);
1369 /* Restore metadata state.*/
1370 ksess
->metadata
= saved_metadata
;
1371 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1378 * Get the syscall mask array from the kernel tracer.
1380 * Return 0 on success else a negative value. In both case, syscall_mask should
1383 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1385 assert(syscall_mask
);
1388 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1392 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1395 * Return 1 on success, 0 when feature is not supported, negative value in case
1398 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1400 int ret
= 0; // Not supported by default
1401 struct lttng_kernel_tracer_abi_version abi
;
1403 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1405 ERR("Failed to retrieve lttng-modules ABI version");
1410 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1412 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1424 * Check for the support of the packet sequence number via abi version number.
1426 * Return 1 on success, 0 when feature is not supported, negative value in case
1429 int kernel_supports_ring_buffer_packet_sequence_number(void)
1431 int ret
= 0; // Not supported by default
1432 struct lttng_kernel_tracer_abi_version abi
;
1434 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1436 ERR("Failed to retrieve lttng-modules ABI version");
1441 * Packet sequence number was introduced in LTTng 2.8,
1442 * lttng-modules ABI 2.1.
1444 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1456 * Rotate a kernel session.
1458 * Return LTTNG_OK on success or else an LTTng error code.
1460 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1463 enum lttng_error_code status
= LTTNG_OK
;
1464 struct consumer_socket
*socket
;
1465 struct lttng_ht_iter iter
;
1466 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1469 assert(ksess
->consumer
);
1471 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1472 session
->name
, session
->id
);
1477 * Note that this loop will end after one iteration given that there is
1478 * only one kernel consumer.
1480 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1481 socket
, node
.node
) {
1482 struct ltt_kernel_channel
*chan
;
1484 /* For each channel, ask the consumer to rotate it. */
1485 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1486 DBG("Rotate kernel channel %" PRIu64
", session %s",
1487 chan
->key
, session
->name
);
1488 ret
= consumer_rotate_channel(socket
, chan
->key
,
1489 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1490 /* is_metadata_channel */ false);
1492 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1498 * Rotate the metadata channel.
1500 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1501 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1502 /* is_metadata_channel */ true);
1504 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1514 enum lttng_error_code
kernel_create_channel_subdirectories(
1515 const struct ltt_kernel_session
*ksess
)
1517 enum lttng_error_code ret
= LTTNG_OK
;
1518 enum lttng_trace_chunk_status chunk_status
;
1521 assert(ksess
->current_trace_chunk
);
1524 * Create the index subdirectory which will take care
1525 * of implicitly creating the channel's path.
1527 chunk_status
= lttng_trace_chunk_create_subdirectory(
1528 ksess
->current_trace_chunk
,
1529 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1530 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1531 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1540 * Setup necessary data for kernel tracer action.
1543 int init_kernel_tracer(void)
1546 bool is_root
= !getuid();
1548 /* Modprobe lttng kernel modules */
1549 ret
= modprobe_lttng_control();
1554 /* Open debugfs lttng */
1555 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1556 if (kernel_tracer_fd
< 0) {
1557 DBG("Failed to open %s", module_proc_lttng
);
1561 /* Validate kernel version */
1562 ret
= kernel_validate_version(&kernel_tracer_version
,
1563 &kernel_tracer_abi_version
);
1568 ret
= modprobe_lttng_data();
1573 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1579 WARN("Kernel tracer does not support buffer monitoring. "
1580 "The monitoring timer of channels in the kernel domain "
1581 "will be set to 0 (disabled).");
1584 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1586 ret
= syscall_init_table(kernel_tracer_fd
);
1588 ERR("Unable to populate syscall table. Syscall tracing won't "
1589 "work for this session daemon.");
1594 modprobe_remove_lttng_control();
1595 ret
= close(kernel_tracer_fd
);
1599 kernel_tracer_fd
= -1;
1600 return LTTNG_ERR_KERN_VERSION
;
1603 ret
= close(kernel_tracer_fd
);
1609 modprobe_remove_lttng_control();
1612 WARN("No kernel tracer available");
1613 kernel_tracer_fd
= -1;
1615 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1617 return LTTNG_ERR_KERN_NA
;
1622 void cleanup_kernel_tracer(void)
1626 DBG2("Closing kernel fd");
1627 if (kernel_tracer_fd
>= 0) {
1628 ret
= close(kernel_tracer_fd
);
1632 kernel_tracer_fd
= -1;
1634 DBG("Unloading kernel modules");
1635 modprobe_remove_lttng_all();
1636 free(syscall_table
);
1640 bool kernel_tracer_is_initialized(void)
1642 return kernel_tracer_fd
>= 0;