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
);
665 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
668 PERROR("disable kernel event");
675 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
684 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
688 DBG("Kernel track PID %d for session id %" PRIu64
".",
690 ret
= kernctl_track_pid(session
->fd
, pid
);
696 return LTTNG_ERR_INVALID
;
698 return LTTNG_ERR_NOMEM
;
700 return LTTNG_ERR_PID_TRACKED
;
702 return LTTNG_ERR_UNK
;
706 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
710 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
712 ret
= kernctl_untrack_pid(session
->fd
, pid
);
718 return LTTNG_ERR_INVALID
;
720 return LTTNG_ERR_NOMEM
;
722 return LTTNG_ERR_PID_NOT_TRACKED
;
724 return LTTNG_ERR_UNK
;
728 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
733 ssize_t nbmem
, count
= 0;
737 fd
= kernctl_list_tracker_pids(session
->fd
);
739 PERROR("kernel tracker pids list");
743 fp
= fdopen(fd
, "r");
745 PERROR("kernel tracker pids list fdopen");
749 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
750 pids
= zmalloc(sizeof(*pids
) * nbmem
);
752 PERROR("alloc list pids");
757 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
758 if (count
>= nbmem
) {
762 new_nbmem
= nbmem
<< 1;
763 DBG("Reallocating pids list from %zu to %zu entries",
765 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
766 if (new_pids
== NULL
) {
767 PERROR("realloc list events");
772 /* Zero the new memory */
773 memset(new_pids
+ nbmem
, 0,
774 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
782 DBG("Kernel list tracker pids done (%zd pids)", count
);
784 ret
= fclose(fp
); /* closes both fp and fd */
800 * Create kernel metadata, open from the kernel tracer and add it to the
803 int kernel_open_metadata(struct ltt_kernel_session
*session
)
806 struct ltt_kernel_metadata
*lkm
= NULL
;
810 /* Allocate kernel metadata */
811 lkm
= trace_kernel_create_metadata();
816 /* Kernel tracer metadata creation */
817 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
823 lkm
->key
= ++next_kernel_channel_key
;
824 /* Prevent fd duplication after execlp() */
825 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
827 PERROR("fcntl session fd");
830 session
->metadata
= lkm
;
832 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
837 trace_kernel_destroy_metadata(lkm
);
843 * Start tracing session.
845 int kernel_start_session(struct ltt_kernel_session
*session
)
851 ret
= kernctl_start_session(session
->fd
);
853 PERROR("ioctl start session");
857 DBG("Kernel session started");
866 * Make a kernel wait to make sure in-flight probe have completed.
868 void kernel_wait_quiescent(void)
871 int fd
= kernel_tracer_fd
;
873 DBG("Kernel quiescent wait on %d", fd
);
875 ret
= kernctl_wait_quiescent(fd
);
877 PERROR("wait quiescent ioctl");
878 ERR("Kernel quiescent wait failed");
883 * Force flush buffer of metadata.
885 int kernel_metadata_flush_buffer(int fd
)
889 DBG("Kernel flushing metadata buffer on fd %d", fd
);
891 ret
= kernctl_buffer_flush(fd
);
893 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
900 * Force flush buffer for channel.
902 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
905 struct ltt_kernel_stream
*stream
;
909 DBG("Flush buffer for channel %s", channel
->channel
->name
);
911 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
912 DBG("Flushing channel stream %d", stream
->fd
);
913 ret
= kernctl_buffer_flush(stream
->fd
);
916 ERR("Fail to flush buffer for stream %d (ret: %d)",
925 * Stop tracing session.
927 int kernel_stop_session(struct ltt_kernel_session
*session
)
933 ret
= kernctl_stop_session(session
->fd
);
938 DBG("Kernel session stopped");
947 * Open stream of channel, register it to the kernel tracer and add it
948 * to the stream list of the channel.
950 * Note: given that the streams may appear in random order wrt CPU
951 * number (e.g. cpu hotplug), the index value of the stream number in
952 * the stream name is not necessarily linked to the CPU number.
954 * Return the number of created stream. Else, a negative value.
956 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
959 struct ltt_kernel_stream
*lks
;
963 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
964 lks
= trace_kernel_create_stream(channel
->channel
->name
,
965 channel
->stream_count
);
975 /* Prevent fd duplication after execlp() */
976 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
978 PERROR("fcntl session fd");
981 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
982 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
984 /* Add stream to channel stream list */
985 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
986 channel
->stream_count
++;
988 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
992 return channel
->stream_count
;
999 * Open the metadata stream and set it to the kernel session.
1001 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1007 ret
= kernctl_create_stream(session
->metadata
->fd
);
1009 PERROR("kernel create metadata stream");
1013 DBG("Kernel metadata stream created (fd: %d)", ret
);
1014 session
->metadata_stream_fd
= ret
;
1015 /* Prevent fd duplication after execlp() */
1016 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1018 PERROR("fcntl session fd");
1028 * Get the event list from the kernel tracer and return the number of elements.
1030 ssize_t
kernel_list_events(struct lttng_event
**events
)
1034 size_t nbmem
, count
= 0;
1036 struct lttng_event
*elist
;
1040 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1042 PERROR("kernel tracepoint list");
1046 fp
= fdopen(fd
, "r");
1048 PERROR("kernel tracepoint list fdopen");
1053 * Init memory size counter
1054 * See kernel-ctl.h for explanation of this value
1056 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1057 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1058 if (elist
== NULL
) {
1059 PERROR("alloc list events");
1064 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1065 if (count
>= nbmem
) {
1066 struct lttng_event
*new_elist
;
1069 new_nbmem
= nbmem
<< 1;
1070 DBG("Reallocating event list from %zu to %zu bytes",
1072 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1073 if (new_elist
== NULL
) {
1074 PERROR("realloc list events");
1080 /* Zero the new memory */
1081 memset(new_elist
+ nbmem
, 0,
1082 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1086 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1087 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1088 elist
[count
].enabled
= -1;
1094 DBG("Kernel list events done (%zu events)", count
);
1096 ret
= fclose(fp
); /* closes both fp and fd */
1112 * Get kernel version and validate it.
1114 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1115 struct lttng_kernel_tracer_abi_version
*abi_version
)
1119 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1121 ERR("Failed to retrieve the lttng-modules version");
1125 /* Validate version */
1126 if (version
->major
!= VERSION_MAJOR
) {
1127 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1128 version
->major
, VERSION_MAJOR
);
1131 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1133 ERR("Failed to retrieve lttng-modules ABI version");
1136 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1137 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1138 abi_version
->major
, abi_version
->minor
,
1139 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1142 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1143 version
->major
, version
->minor
,
1144 abi_version
->major
, abi_version
->minor
);
1151 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1156 * Kernel work-arounds called at the start of sessiond main().
1158 int init_kernel_workarounds(void)
1164 * boot_id needs to be read once before being used concurrently
1165 * to deal with a Linux kernel race. A fix is proposed for
1166 * upstream, but the work-around is needed for older kernels.
1168 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1175 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1177 /* Ignore error, we don't really care */
1189 * Teardown of a kernel session, keeping data required by destroy notifiers.
1191 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1193 struct lttng_trace_chunk
*trace_chunk
;
1195 if (ksess
== NULL
) {
1196 DBG3("No kernel session when tearing down session");
1200 DBG("Tearing down kernel session");
1201 trace_chunk
= ksess
->current_trace_chunk
;
1204 * Destroy channels on the consumer if at least one FD has been sent and we
1205 * are in no output mode because the streams are in *no* monitor mode so we
1206 * have to send a command to clean them up or else they leaked.
1208 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1210 struct consumer_socket
*socket
;
1211 struct lttng_ht_iter iter
;
1213 /* For each consumer socket. */
1215 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1216 socket
, node
.node
) {
1217 struct ltt_kernel_channel
*chan
;
1219 /* For each channel, ask the consumer to destroy it. */
1220 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1221 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1223 /* Consumer is probably dead. Use next socket. */
1231 /* Close any relayd session */
1232 consumer_output_send_destroy_relayd(ksess
->consumer
);
1234 trace_kernel_destroy_session(ksess
);
1235 lttng_trace_chunk_put(trace_chunk
);
1238 /* Teardown of data required by destroy notifiers. */
1239 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1241 if (ksess
== NULL
) {
1244 trace_kernel_free_session(ksess
);
1248 * Destroy a kernel channel object. It does not do anything on the tracer side.
1250 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1252 struct ltt_kernel_session
*ksess
= NULL
;
1255 assert(kchan
->channel
);
1257 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1259 /* Update channel count of associated session. */
1260 if (kchan
->session
) {
1261 /* Keep pointer reference so we can update it after the destroy. */
1262 ksess
= kchan
->session
;
1265 trace_kernel_destroy_channel(kchan
);
1268 * At this point the kernel channel is not visible anymore. This is safe
1269 * since in order to work on a visible kernel session, the tracing session
1270 * lock (ltt_session.lock) MUST be acquired.
1273 ksess
->channel_count
--;
1278 * Take a snapshot for a given kernel session.
1280 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1282 enum lttng_error_code
kernel_snapshot_record(
1283 struct ltt_kernel_session
*ksess
,
1284 const struct consumer_output
*output
, int wait
,
1285 uint64_t nb_packets_per_stream
)
1287 int err
, ret
, saved_metadata_fd
;
1288 enum lttng_error_code status
= LTTNG_OK
;
1289 struct consumer_socket
*socket
;
1290 struct lttng_ht_iter iter
;
1291 struct ltt_kernel_metadata
*saved_metadata
;
1292 char *trace_path
= NULL
;
1295 assert(ksess
->consumer
);
1298 DBG("Kernel snapshot record started");
1300 /* Save current metadata since the following calls will change it. */
1301 saved_metadata
= ksess
->metadata
;
1302 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1306 ret
= kernel_open_metadata(ksess
);
1308 status
= LTTNG_ERR_KERN_META_FAIL
;
1312 ret
= kernel_open_metadata_stream(ksess
);
1314 status
= LTTNG_ERR_KERN_META_FAIL
;
1315 goto error_open_stream
;
1318 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1319 DEFAULT_KERNEL_TRACE_DIR
);
1321 status
= LTTNG_ERR_INVALID
;
1324 /* Send metadata to consumer and snapshot everything. */
1325 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1326 socket
, node
.node
) {
1327 struct ltt_kernel_channel
*chan
;
1329 pthread_mutex_lock(socket
->lock
);
1330 /* This stream must not be monitored by the consumer. */
1331 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1332 pthread_mutex_unlock(socket
->lock
);
1334 status
= LTTNG_ERR_KERN_META_FAIL
;
1335 goto error_consumer
;
1338 /* For each channel, ask the consumer to snapshot it. */
1339 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1340 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1341 ksess
->uid
, ksess
->gid
,
1343 nb_packets_per_stream
);
1344 if (status
!= LTTNG_OK
) {
1345 (void) kernel_consumer_destroy_metadata(socket
,
1347 goto error_consumer
;
1351 /* Snapshot metadata, */
1352 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1353 1, ksess
->uid
, ksess
->gid
, trace_path
, wait
, 0);
1354 if (status
!= LTTNG_OK
) {
1355 goto error_consumer
;
1359 * The metadata snapshot is done, ask the consumer to destroy it since
1360 * it's not monitored on the consumer side.
1362 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1366 /* Close newly opened metadata stream. It's now on the consumer side. */
1367 err
= close(ksess
->metadata_stream_fd
);
1369 PERROR("close snapshot kernel");
1373 trace_kernel_destroy_metadata(ksess
->metadata
);
1375 /* Restore metadata state.*/
1376 ksess
->metadata
= saved_metadata
;
1377 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1384 * Get the syscall mask array from the kernel tracer.
1386 * Return 0 on success else a negative value. In both case, syscall_mask should
1389 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1391 assert(syscall_mask
);
1394 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1398 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1401 * Return 1 on success, 0 when feature is not supported, negative value in case
1404 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1406 int ret
= 0; // Not supported by default
1407 struct lttng_kernel_tracer_abi_version abi
;
1409 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1411 ERR("Failed to retrieve lttng-modules ABI version");
1416 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1418 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1430 * Check for the support of the packet sequence number via abi version number.
1432 * Return 1 on success, 0 when feature is not supported, negative value in case
1435 int kernel_supports_ring_buffer_packet_sequence_number(void)
1437 int ret
= 0; // Not supported by default
1438 struct lttng_kernel_tracer_abi_version abi
;
1440 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1442 ERR("Failed to retrieve lttng-modules ABI version");
1447 * Packet sequence number was introduced in LTTng 2.8,
1448 * lttng-modules ABI 2.1.
1450 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1462 * Rotate a kernel session.
1464 * Return LTTNG_OK on success or else an LTTng error code.
1466 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1469 enum lttng_error_code status
= LTTNG_OK
;
1470 struct consumer_socket
*socket
;
1471 struct lttng_ht_iter iter
;
1472 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1475 assert(ksess
->consumer
);
1477 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1478 session
->name
, session
->id
);
1483 * Note that this loop will end after one iteration given that there is
1484 * only one kernel consumer.
1486 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1487 socket
, node
.node
) {
1488 struct ltt_kernel_channel
*chan
;
1490 /* For each channel, ask the consumer to rotate it. */
1491 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1492 DBG("Rotate kernel channel %" PRIu64
", session %s",
1493 chan
->key
, session
->name
);
1494 ret
= consumer_rotate_channel(socket
, chan
->key
,
1495 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1496 /* is_metadata_channel */ false);
1498 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1504 * Rotate the metadata channel.
1506 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1507 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1508 /* is_metadata_channel */ true);
1510 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1520 enum lttng_error_code
kernel_create_channel_subdirectories(
1521 const struct ltt_kernel_session
*ksess
)
1523 enum lttng_error_code ret
= LTTNG_OK
;
1524 enum lttng_trace_chunk_status chunk_status
;
1527 assert(ksess
->current_trace_chunk
);
1530 * Create the index subdirectory which will take care
1531 * of implicitly creating the channel's path.
1533 chunk_status
= lttng_trace_chunk_create_subdirectory(
1534 ksess
->current_trace_chunk
,
1535 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1536 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1537 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1546 * Setup necessary data for kernel tracer action.
1549 int init_kernel_tracer(void)
1552 bool is_root
= !getuid();
1554 /* Modprobe lttng kernel modules */
1555 ret
= modprobe_lttng_control();
1560 /* Open debugfs lttng */
1561 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1562 if (kernel_tracer_fd
< 0) {
1563 DBG("Failed to open %s", module_proc_lttng
);
1567 /* Validate kernel version */
1568 ret
= kernel_validate_version(&kernel_tracer_version
,
1569 &kernel_tracer_abi_version
);
1574 ret
= modprobe_lttng_data();
1579 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1585 WARN("Kernel tracer does not support buffer monitoring. "
1586 "The monitoring timer of channels in the kernel domain "
1587 "will be set to 0 (disabled).");
1590 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1592 ret
= syscall_init_table(kernel_tracer_fd
);
1594 ERR("Unable to populate syscall table. Syscall tracing won't "
1595 "work for this session daemon.");
1600 modprobe_remove_lttng_control();
1601 ret
= close(kernel_tracer_fd
);
1605 kernel_tracer_fd
= -1;
1606 return LTTNG_ERR_KERN_VERSION
;
1609 ret
= close(kernel_tracer_fd
);
1615 modprobe_remove_lttng_control();
1618 WARN("No kernel tracer available");
1619 kernel_tracer_fd
= -1;
1621 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1623 return LTTNG_ERR_KERN_NA
;
1628 void cleanup_kernel_tracer(void)
1632 DBG2("Closing kernel fd");
1633 if (kernel_tracer_fd
>= 0) {
1634 ret
= close(kernel_tracer_fd
);
1638 kernel_tracer_fd
= -1;
1640 DBG("Unloading kernel modules");
1641 modprobe_remove_lttng_all();
1642 free(syscall_table
);
1646 bool kernel_tracer_is_initialized(void)
1648 return kernel_tracer_fd
>= 0;