2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/kernel-ctl/kernel-ioctl.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
33 #include "kernel-consumer.h"
34 #include "kern-modules.h"
39 * Key used to reference a channel between the sessiond and the consumer. This
40 * is only read and updated with the session_list lock held.
42 static uint64_t next_kernel_channel_key
;
44 #include <lttng/userspace-probe.h>
45 #include <lttng/userspace-probe-internal.h>
47 * Add context on a kernel channel.
49 * Assumes the ownership of ctx.
51 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
52 struct ltt_kernel_context
*ctx
)
59 DBG("Adding context to channel %s", chan
->channel
->name
);
60 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
64 /* Exists but not available for this kernel */
65 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
68 /* If EEXIST, we just ignore the error */
72 PERROR("add context ioctl");
73 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
80 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
85 trace_kernel_destroy_context(ctx
);
91 * Create a new kernel session, register it to the kernel tracer and add it to
92 * the session daemon session.
94 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
97 struct ltt_kernel_session
*lks
;
101 /* Allocate data structure */
102 lks
= trace_kernel_create_session();
108 /* Kernel tracer session creation */
109 ret
= kernctl_create_session(tracer_fd
);
111 PERROR("ioctl kernel create session");
116 /* Prevent fd duplication after execlp() */
117 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
119 PERROR("fcntl session fd");
122 lks
->id
= session
->id
;
123 lks
->consumer_fds_sent
= 0;
124 session
->kernel_session
= lks
;
126 DBG("Kernel session created (fd: %d)", lks
->fd
);
132 trace_kernel_destroy_session(lks
);
138 * Create a kernel channel, register it to the kernel tracer and add it to the
141 int kernel_create_channel(struct ltt_kernel_session
*session
,
142 struct lttng_channel
*chan
)
145 struct ltt_kernel_channel
*lkc
;
150 /* Allocate kernel channel */
151 lkc
= trace_kernel_create_channel(chan
);
156 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
157 chan
->name
, lkc
->channel
->attr
.overwrite
,
158 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
159 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
160 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
162 /* Kernel tracer channel creation */
163 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
165 PERROR("ioctl kernel create channel");
169 /* Setup the channel fd */
171 /* Prevent fd duplication after execlp() */
172 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
174 PERROR("fcntl session fd");
177 /* Add channel to session */
178 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
179 session
->channel_count
++;
180 lkc
->session
= session
;
181 lkc
->key
= ++next_kernel_channel_key
;
183 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
184 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
197 * Compute the offset of the instrumentation byte in the binary based on the
198 * function probe location using the ELF lookup method.
200 * Returns 0 on success and set the offset out parameter to the offset of the
202 * Returns -1 on error
205 int extract_userspace_probe_offset_function_elf(
206 struct lttng_userspace_probe_location
*probe_location
,
207 struct ltt_kernel_session
*session
, uint64_t *offset
)
211 const char *symbol
= NULL
;
212 struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
213 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
216 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
217 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
219 lookup
= lttng_userspace_probe_location_get_lookup_method(
227 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
229 assert(lookup_method_type
==
230 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
232 symbol
= lttng_userspace_probe_location_function_get_function_name(
239 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
245 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
246 session
->gid
, offset
);
248 DBG("userspace probe offset calculation failed for "
249 "function %s", symbol
);
253 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
259 * Compute the offsets of the instrumentation bytes in the binary based on the
260 * tracepoint probe location using the SDT lookup method. This function
261 * allocates the offsets buffer, the caller must free it.
263 * Returns 0 on success and set the offset out parameter to the offsets of the
265 * Returns -1 on error.
268 int extract_userspace_probe_offset_tracepoint_sdt(
269 struct lttng_userspace_probe_location
*probe_location
,
270 struct ltt_kernel_session
*session
, uint64_t **offsets
,
271 uint32_t *offsets_count
)
273 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
274 struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
275 const char *probe_name
= NULL
, *provider_name
= NULL
;
279 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
280 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
282 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
289 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
291 assert(lookup_method_type
==
292 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
295 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
302 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
304 if (!provider_name
) {
309 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
315 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
316 session
->uid
, session
->gid
, offsets
, offsets_count
);
318 DBG("userspace probe offset calculation failed for sdt "
319 "probe %s:%s", provider_name
, probe_name
);
323 if (*offsets_count
== 0) {
324 DBG("no userspace probe offset found");
328 DBG("%u userspace probe SDT offsets found for %s:%s at:",
329 *offsets_count
, provider_name
, probe_name
);
330 for (i
= 0; i
< *offsets_count
; i
++) {
331 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
338 * Extract the offsets of the instrumentation point for the different lookup
342 int userspace_probe_add_callsites(struct lttng_event
*ev
,
343 struct ltt_kernel_session
*session
, int fd
)
345 struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
346 enum lttng_userspace_probe_location_lookup_method_type type
;
347 struct lttng_userspace_probe_location
*location
= NULL
;
351 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
353 location
= lttng_event_get_userspace_probe_location(ev
);
359 lttng_userspace_probe_location_get_lookup_method(location
);
360 if (!lookup_method
) {
365 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
367 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
369 struct lttng_kernel_event_callsite callsite
;
372 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
374 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
378 callsite
.u
.uprobe
.offset
= offset
;
379 ret
= kernctl_add_callsite(fd
, &callsite
);
381 WARN("Adding callsite to userspace probe "
382 "event %s failed.", ev
->name
);
383 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
388 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
391 uint64_t *offsets
= NULL
;
392 uint32_t offsets_count
;
393 struct lttng_kernel_event_callsite callsite
;
396 * This call allocates the offsets buffer. This buffer must be freed
399 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
400 &offsets
, &offsets_count
);
402 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
405 for (i
= 0; i
< offsets_count
; i
++) {
406 callsite
.u
.uprobe
.offset
= offsets
[i
];
407 ret
= kernctl_add_callsite(fd
, &callsite
);
409 WARN("Adding callsite to userspace probe "
410 "event %s failed.", ev
->name
);
411 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
420 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
428 * Create a kernel event, enable it to the kernel tracer and add it to the
429 * channel event list of the kernel session.
430 * We own filter_expression and filter.
432 int kernel_create_event(struct lttng_event
*ev
,
433 struct ltt_kernel_channel
*channel
,
434 char *filter_expression
,
435 struct lttng_filter_bytecode
*filter
)
438 enum lttng_error_code ret
;
439 struct ltt_kernel_event
*event
;
444 /* We pass ownership of filter_expression and filter */
445 ret
= trace_kernel_create_event(ev
, filter_expression
,
447 if (ret
!= LTTNG_OK
) {
451 fd
= kernctl_create_event(channel
->fd
, event
->event
);
455 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
458 WARN("Event type not implemented");
459 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
462 WARN("Event %s not found!", ev
->name
);
463 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
466 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
467 PERROR("create event ioctl");
472 event
->type
= ev
->type
;
474 /* Prevent fd duplication after execlp() */
475 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
477 PERROR("fcntl session fd");
481 err
= kernctl_filter(event
->fd
, filter
);
485 ret
= LTTNG_ERR_FILTER_NOMEM
;
488 ret
= LTTNG_ERR_FILTER_INVAL
;
495 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
496 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
498 goto add_callsite_error
;
502 err
= kernctl_enable(event
->fd
);
506 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
509 PERROR("enable kernel event");
510 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
516 /* Add event to event list */
517 cds_list_add(&event
->list
, &channel
->events_list
.head
);
518 channel
->event_count
++;
520 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
530 closeret
= close(event
->fd
);
532 PERROR("close event fd");
542 * Disable a kernel channel.
544 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
550 ret
= kernctl_disable(chan
->fd
);
552 PERROR("disable chan ioctl");
557 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
558 chan
->channel
->name
, chan
->fd
, chan
->key
);
567 * Enable a kernel channel.
569 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
575 ret
= kernctl_enable(chan
->fd
);
576 if (ret
< 0 && ret
!= -EEXIST
) {
577 PERROR("Enable kernel chan");
582 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
583 chan
->channel
->name
, chan
->fd
, chan
->key
);
592 * Enable a kernel event.
594 int kernel_enable_event(struct ltt_kernel_event
*event
)
600 ret
= kernctl_enable(event
->fd
);
604 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
607 PERROR("enable kernel event");
614 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
623 * Disable a kernel event.
625 int kernel_disable_event(struct ltt_kernel_event
*event
)
631 ret
= kernctl_disable(event
->fd
);
635 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
638 PERROR("disable kernel event");
645 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
654 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
658 DBG("Kernel track PID %d for session id %" PRIu64
".",
660 ret
= kernctl_track_pid(session
->fd
, pid
);
666 return LTTNG_ERR_INVALID
;
668 return LTTNG_ERR_NOMEM
;
670 return LTTNG_ERR_PID_TRACKED
;
672 return LTTNG_ERR_UNK
;
676 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
680 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
682 ret
= kernctl_untrack_pid(session
->fd
, pid
);
688 return LTTNG_ERR_INVALID
;
690 return LTTNG_ERR_NOMEM
;
692 return LTTNG_ERR_PID_NOT_TRACKED
;
694 return LTTNG_ERR_UNK
;
698 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
703 ssize_t nbmem
, count
= 0;
707 fd
= kernctl_list_tracker_pids(session
->fd
);
709 PERROR("kernel tracker pids list");
713 fp
= fdopen(fd
, "r");
715 PERROR("kernel tracker pids list fdopen");
719 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
720 pids
= zmalloc(sizeof(*pids
) * nbmem
);
722 PERROR("alloc list pids");
727 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
728 if (count
>= nbmem
) {
732 new_nbmem
= nbmem
<< 1;
733 DBG("Reallocating pids list from %zu to %zu entries",
735 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
736 if (new_pids
== NULL
) {
737 PERROR("realloc list events");
742 /* Zero the new memory */
743 memset(new_pids
+ nbmem
, 0,
744 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
752 DBG("Kernel list tracker pids done (%zd pids)", count
);
754 ret
= fclose(fp
); /* closes both fp and fd */
770 * Create kernel metadata, open from the kernel tracer and add it to the
773 int kernel_open_metadata(struct ltt_kernel_session
*session
)
776 struct ltt_kernel_metadata
*lkm
= NULL
;
780 /* Allocate kernel metadata */
781 lkm
= trace_kernel_create_metadata();
786 /* Kernel tracer metadata creation */
787 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
793 lkm
->key
= ++next_kernel_channel_key
;
794 /* Prevent fd duplication after execlp() */
795 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
797 PERROR("fcntl session fd");
800 session
->metadata
= lkm
;
802 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
807 trace_kernel_destroy_metadata(lkm
);
813 * Start tracing session.
815 int kernel_start_session(struct ltt_kernel_session
*session
)
821 ret
= kernctl_start_session(session
->fd
);
823 PERROR("ioctl start session");
827 DBG("Kernel session started");
836 * Make a kernel wait to make sure in-flight probe have completed.
838 void kernel_wait_quiescent(int fd
)
842 DBG("Kernel quiescent wait on %d", fd
);
844 ret
= kernctl_wait_quiescent(fd
);
846 PERROR("wait quiescent ioctl");
847 ERR("Kernel quiescent wait failed");
852 * Force flush buffer of metadata.
854 int kernel_metadata_flush_buffer(int fd
)
858 DBG("Kernel flushing metadata buffer on fd %d", fd
);
860 ret
= kernctl_buffer_flush(fd
);
862 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
869 * Force flush buffer for channel.
871 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
874 struct ltt_kernel_stream
*stream
;
878 DBG("Flush buffer for channel %s", channel
->channel
->name
);
880 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
881 DBG("Flushing channel stream %d", stream
->fd
);
882 ret
= kernctl_buffer_flush(stream
->fd
);
885 ERR("Fail to flush buffer for stream %d (ret: %d)",
894 * Stop tracing session.
896 int kernel_stop_session(struct ltt_kernel_session
*session
)
902 ret
= kernctl_stop_session(session
->fd
);
907 DBG("Kernel session stopped");
916 * Open stream of channel, register it to the kernel tracer and add it
917 * to the stream list of the channel.
919 * Note: given that the streams may appear in random order wrt CPU
920 * number (e.g. cpu hotplug), the index value of the stream number in
921 * the stream name is not necessarily linked to the CPU number.
923 * Return the number of created stream. Else, a negative value.
925 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
928 struct ltt_kernel_stream
*lks
;
932 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
933 lks
= trace_kernel_create_stream(channel
->channel
->name
,
934 channel
->stream_count
);
944 /* Prevent fd duplication after execlp() */
945 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
947 PERROR("fcntl session fd");
950 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
951 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
953 /* Add stream to channel stream list */
954 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
955 channel
->stream_count
++;
957 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
961 return channel
->stream_count
;
968 * Open the metadata stream and set it to the kernel session.
970 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
976 ret
= kernctl_create_stream(session
->metadata
->fd
);
978 PERROR("kernel create metadata stream");
982 DBG("Kernel metadata stream created (fd: %d)", ret
);
983 session
->metadata_stream_fd
= ret
;
984 /* Prevent fd duplication after execlp() */
985 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
987 PERROR("fcntl session fd");
997 * Get the event list from the kernel tracer and return the number of elements.
999 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
1003 size_t nbmem
, count
= 0;
1005 struct lttng_event
*elist
;
1009 fd
= kernctl_tracepoint_list(tracer_fd
);
1011 PERROR("kernel tracepoint list");
1015 fp
= fdopen(fd
, "r");
1017 PERROR("kernel tracepoint list fdopen");
1022 * Init memory size counter
1023 * See kernel-ctl.h for explanation of this value
1025 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1026 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1027 if (elist
== NULL
) {
1028 PERROR("alloc list events");
1033 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1034 if (count
>= nbmem
) {
1035 struct lttng_event
*new_elist
;
1038 new_nbmem
= nbmem
<< 1;
1039 DBG("Reallocating event list from %zu to %zu bytes",
1041 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1042 if (new_elist
== NULL
) {
1043 PERROR("realloc list events");
1049 /* Zero the new memory */
1050 memset(new_elist
+ nbmem
, 0,
1051 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1055 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1056 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1057 elist
[count
].enabled
= -1;
1063 DBG("Kernel list events done (%zu events)", count
);
1065 ret
= fclose(fp
); /* closes both fp and fd */
1081 * Get kernel version and validate it.
1083 int kernel_validate_version(int tracer_fd
,
1084 struct lttng_kernel_tracer_version
*version
,
1085 struct lttng_kernel_tracer_abi_version
*abi_version
)
1089 ret
= kernctl_tracer_version(tracer_fd
, version
);
1091 ERR("Failed to retrieve the lttng-modules version");
1095 /* Validate version */
1096 if (version
->major
!= VERSION_MAJOR
) {
1097 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1098 version
->major
, VERSION_MAJOR
);
1101 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1103 ERR("Failed to retrieve lttng-modules ABI version");
1106 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1107 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1108 abi_version
->major
, abi_version
->minor
,
1109 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1112 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1113 version
->major
, version
->minor
,
1114 abi_version
->major
, abi_version
->minor
);
1121 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1126 * Kernel work-arounds called at the start of sessiond main().
1128 int init_kernel_workarounds(void)
1134 * boot_id needs to be read once before being used concurrently
1135 * to deal with a Linux kernel race. A fix is proposed for
1136 * upstream, but the work-around is needed for older kernels.
1138 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1145 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1147 /* Ignore error, we don't really care */
1159 * Complete teardown of a kernel session.
1161 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1163 if (ksess
== NULL
) {
1164 DBG3("No kernel session when tearing down session");
1168 DBG("Tearing down kernel session");
1171 * Destroy channels on the consumer if at least one FD has been sent and we
1172 * are in no output mode because the streams are in *no* monitor mode so we
1173 * have to send a command to clean them up or else they leaked.
1175 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1177 struct consumer_socket
*socket
;
1178 struct lttng_ht_iter iter
;
1180 /* For each consumer socket. */
1182 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1183 socket
, node
.node
) {
1184 struct ltt_kernel_channel
*chan
;
1186 /* For each channel, ask the consumer to destroy it. */
1187 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1188 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1190 /* Consumer is probably dead. Use next socket. */
1198 /* Close any relayd session */
1199 consumer_output_send_destroy_relayd(ksess
->consumer
);
1201 trace_kernel_destroy_session(ksess
);
1205 * Destroy a kernel channel object. It does not do anything on the tracer side.
1207 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1209 struct ltt_kernel_session
*ksess
= NULL
;
1212 assert(kchan
->channel
);
1214 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1216 /* Update channel count of associated session. */
1217 if (kchan
->session
) {
1218 /* Keep pointer reference so we can update it after the destroy. */
1219 ksess
= kchan
->session
;
1222 trace_kernel_destroy_channel(kchan
);
1225 * At this point the kernel channel is not visible anymore. This is safe
1226 * since in order to work on a visible kernel session, the tracing session
1227 * lock (ltt_session.lock) MUST be acquired.
1230 ksess
->channel_count
--;
1235 * Take a snapshot for a given kernel session.
1237 * Return 0 on success or else return a LTTNG_ERR code.
1239 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1240 struct snapshot_output
*output
, int wait
,
1241 uint64_t nb_packets_per_stream
)
1243 int err
, ret
, saved_metadata_fd
;
1244 struct consumer_socket
*socket
;
1245 struct lttng_ht_iter iter
;
1246 struct ltt_kernel_metadata
*saved_metadata
;
1247 struct ltt_session
*session
;
1248 uint64_t trace_archive_id
;
1251 assert(ksess
->consumer
);
1254 DBG("Kernel snapshot record started");
1256 session
= session_find_by_id(ksess
->id
);
1258 assert(pthread_mutex_trylock(&session
->lock
));
1259 assert(session_trylock_list());
1260 trace_archive_id
= session
->current_archive_id
;
1262 /* Save current metadata since the following calls will change it. */
1263 saved_metadata
= ksess
->metadata
;
1264 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1268 ret
= kernel_open_metadata(ksess
);
1270 ret
= LTTNG_ERR_KERN_META_FAIL
;
1274 ret
= kernel_open_metadata_stream(ksess
);
1276 ret
= LTTNG_ERR_KERN_META_FAIL
;
1277 goto error_open_stream
;
1280 /* Send metadata to consumer and snapshot everything. */
1281 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1282 socket
, node
.node
) {
1283 struct consumer_output
*saved_output
;
1284 struct ltt_kernel_channel
*chan
;
1287 * Temporarly switch consumer output for our snapshot output. As long
1288 * as the session lock is taken, this is safe.
1290 saved_output
= ksess
->consumer
;
1291 ksess
->consumer
= output
->consumer
;
1293 pthread_mutex_lock(socket
->lock
);
1294 /* This stream must not be monitored by the consumer. */
1295 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1296 pthread_mutex_unlock(socket
->lock
);
1297 /* Put back the saved consumer output into the session. */
1298 ksess
->consumer
= saved_output
;
1300 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1301 goto error_consumer
;
1304 /* For each channel, ask the consumer to snapshot it. */
1305 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1306 ret
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1307 ksess
->uid
, ksess
->gid
,
1308 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1309 nb_packets_per_stream
,
1312 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1313 (void) kernel_consumer_destroy_metadata(socket
,
1315 goto error_consumer
;
1319 /* Snapshot metadata, */
1320 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1321 1, ksess
->uid
, ksess
->gid
,
1322 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1325 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1326 goto error_consumer
;
1330 * The metadata snapshot is done, ask the consumer to destroy it since
1331 * it's not monitored on the consumer side.
1333 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1339 /* Close newly opened metadata stream. It's now on the consumer side. */
1340 err
= close(ksess
->metadata_stream_fd
);
1342 PERROR("close snapshot kernel");
1346 trace_kernel_destroy_metadata(ksess
->metadata
);
1348 /* Restore metadata state.*/
1349 ksess
->metadata
= saved_metadata
;
1350 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1357 * Get the syscall mask array from the kernel tracer.
1359 * Return 0 on success else a negative value. In both case, syscall_mask should
1362 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1364 assert(syscall_mask
);
1367 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1371 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1374 * Return 1 on success, 0 when feature is not supported, negative value in case
1377 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1379 int ret
= 0; // Not supported by default
1380 struct lttng_kernel_tracer_abi_version abi
;
1382 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1384 ERR("Failed to retrieve lttng-modules ABI version");
1389 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1391 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1403 * Rotate a kernel session.
1405 * Return 0 on success or else return a LTTNG_ERR code.
1407 int kernel_rotate_session(struct ltt_session
*session
)
1410 struct consumer_socket
*socket
;
1411 struct lttng_ht_iter iter
;
1412 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1415 assert(ksess
->consumer
);
1417 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1418 session
->name
, session
->id
);
1423 * Note that this loop will end after one iteration given that there is
1424 * only one kernel consumer.
1426 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1427 socket
, node
.node
) {
1428 struct ltt_kernel_channel
*chan
;
1431 * Account the metadata channel first to make sure the
1432 * number of channels waiting for a rotation cannot
1433 * reach 0 before we complete the iteration over all
1436 ret
= rotate_add_channel_pending(ksess
->metadata
->key
,
1437 LTTNG_DOMAIN_KERNEL
, session
);
1439 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1443 /* For each channel, ask the consumer to rotate it. */
1444 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1445 ret
= rotate_add_channel_pending(chan
->key
,
1446 LTTNG_DOMAIN_KERNEL
, session
);
1448 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1452 DBG("Rotate channel %" PRIu64
", session %s", chan
->key
, session
->name
);
1453 ret
= consumer_rotate_channel(socket
, chan
->key
,
1454 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1455 ksess
->consumer
->subdir
,
1456 /* is_metadata_channel */ false,
1457 session
->current_archive_id
,
1458 &session
->rotate_pending_relay
);
1460 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1466 * Rotate the metadata channel.
1468 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1469 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1470 ksess
->consumer
->subdir
,
1471 /* is_metadata_channel */ true,
1472 session
->current_archive_id
,
1473 &session
->rotate_pending_relay
);
1475 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;