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 err
= kernctl_enable(event
->fd
);
499 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
502 PERROR("enable kernel event");
503 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
509 /* Add event to event list */
510 cds_list_add(&event
->list
, &channel
->events_list
.head
);
511 channel
->event_count
++;
513 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
522 closeret
= close(event
->fd
);
524 PERROR("close event fd");
534 * Disable a kernel channel.
536 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
542 ret
= kernctl_disable(chan
->fd
);
544 PERROR("disable chan ioctl");
549 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
550 chan
->channel
->name
, chan
->fd
, chan
->key
);
559 * Enable a kernel channel.
561 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
567 ret
= kernctl_enable(chan
->fd
);
568 if (ret
< 0 && ret
!= -EEXIST
) {
569 PERROR("Enable kernel chan");
574 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
575 chan
->channel
->name
, chan
->fd
, chan
->key
);
584 * Enable a kernel event.
586 int kernel_enable_event(struct ltt_kernel_event
*event
)
592 ret
= kernctl_enable(event
->fd
);
596 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
599 PERROR("enable kernel event");
606 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
615 * Disable a kernel event.
617 int kernel_disable_event(struct ltt_kernel_event
*event
)
623 ret
= kernctl_disable(event
->fd
);
627 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
630 PERROR("disable kernel event");
637 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
646 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
650 DBG("Kernel track PID %d for session id %" PRIu64
".",
652 ret
= kernctl_track_pid(session
->fd
, pid
);
658 return LTTNG_ERR_INVALID
;
660 return LTTNG_ERR_NOMEM
;
662 return LTTNG_ERR_PID_TRACKED
;
664 return LTTNG_ERR_UNK
;
668 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
672 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
674 ret
= kernctl_untrack_pid(session
->fd
, pid
);
680 return LTTNG_ERR_INVALID
;
682 return LTTNG_ERR_NOMEM
;
684 return LTTNG_ERR_PID_NOT_TRACKED
;
686 return LTTNG_ERR_UNK
;
690 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
695 ssize_t nbmem
, count
= 0;
699 fd
= kernctl_list_tracker_pids(session
->fd
);
701 PERROR("kernel tracker pids list");
705 fp
= fdopen(fd
, "r");
707 PERROR("kernel tracker pids list fdopen");
711 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
712 pids
= zmalloc(sizeof(*pids
) * nbmem
);
714 PERROR("alloc list pids");
719 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
720 if (count
>= nbmem
) {
724 new_nbmem
= nbmem
<< 1;
725 DBG("Reallocating pids list from %zu to %zu entries",
727 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
728 if (new_pids
== NULL
) {
729 PERROR("realloc list events");
734 /* Zero the new memory */
735 memset(new_pids
+ nbmem
, 0,
736 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
744 DBG("Kernel list tracker pids done (%zd pids)", count
);
746 ret
= fclose(fp
); /* closes both fp and fd */
762 * Create kernel metadata, open from the kernel tracer and add it to the
765 int kernel_open_metadata(struct ltt_kernel_session
*session
)
768 struct ltt_kernel_metadata
*lkm
= NULL
;
772 /* Allocate kernel metadata */
773 lkm
= trace_kernel_create_metadata();
778 /* Kernel tracer metadata creation */
779 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
785 lkm
->key
= ++next_kernel_channel_key
;
786 /* Prevent fd duplication after execlp() */
787 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
789 PERROR("fcntl session fd");
792 session
->metadata
= lkm
;
794 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
799 trace_kernel_destroy_metadata(lkm
);
805 * Start tracing session.
807 int kernel_start_session(struct ltt_kernel_session
*session
)
813 ret
= kernctl_start_session(session
->fd
);
815 PERROR("ioctl start session");
819 DBG("Kernel session started");
828 * Make a kernel wait to make sure in-flight probe have completed.
830 void kernel_wait_quiescent(int fd
)
834 DBG("Kernel quiescent wait on %d", fd
);
836 ret
= kernctl_wait_quiescent(fd
);
838 PERROR("wait quiescent ioctl");
839 ERR("Kernel quiescent wait failed");
844 * Force flush buffer of metadata.
846 int kernel_metadata_flush_buffer(int fd
)
850 DBG("Kernel flushing metadata buffer on fd %d", fd
);
852 ret
= kernctl_buffer_flush(fd
);
854 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
861 * Force flush buffer for channel.
863 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
866 struct ltt_kernel_stream
*stream
;
870 DBG("Flush buffer for channel %s", channel
->channel
->name
);
872 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
873 DBG("Flushing channel stream %d", stream
->fd
);
874 ret
= kernctl_buffer_flush(stream
->fd
);
877 ERR("Fail to flush buffer for stream %d (ret: %d)",
886 * Stop tracing session.
888 int kernel_stop_session(struct ltt_kernel_session
*session
)
894 ret
= kernctl_stop_session(session
->fd
);
899 DBG("Kernel session stopped");
908 * Open stream of channel, register it to the kernel tracer and add it
909 * to the stream list of the channel.
911 * Note: given that the streams may appear in random order wrt CPU
912 * number (e.g. cpu hotplug), the index value of the stream number in
913 * the stream name is not necessarily linked to the CPU number.
915 * Return the number of created stream. Else, a negative value.
917 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
920 struct ltt_kernel_stream
*lks
;
924 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
925 lks
= trace_kernel_create_stream(channel
->channel
->name
,
926 channel
->stream_count
);
936 /* Prevent fd duplication after execlp() */
937 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
939 PERROR("fcntl session fd");
942 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
943 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
945 /* Add stream to channel stream list */
946 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
947 channel
->stream_count
++;
949 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
953 return channel
->stream_count
;
960 * Open the metadata stream and set it to the kernel session.
962 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
968 ret
= kernctl_create_stream(session
->metadata
->fd
);
970 PERROR("kernel create metadata stream");
974 DBG("Kernel metadata stream created (fd: %d)", ret
);
975 session
->metadata_stream_fd
= ret
;
976 /* Prevent fd duplication after execlp() */
977 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
979 PERROR("fcntl session fd");
989 * Get the event list from the kernel tracer and return the number of elements.
991 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
995 size_t nbmem
, count
= 0;
997 struct lttng_event
*elist
;
1001 fd
= kernctl_tracepoint_list(tracer_fd
);
1003 PERROR("kernel tracepoint list");
1007 fp
= fdopen(fd
, "r");
1009 PERROR("kernel tracepoint list fdopen");
1014 * Init memory size counter
1015 * See kernel-ctl.h for explanation of this value
1017 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1018 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1019 if (elist
== NULL
) {
1020 PERROR("alloc list events");
1025 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1026 if (count
>= nbmem
) {
1027 struct lttng_event
*new_elist
;
1030 new_nbmem
= nbmem
<< 1;
1031 DBG("Reallocating event list from %zu to %zu bytes",
1033 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1034 if (new_elist
== NULL
) {
1035 PERROR("realloc list events");
1041 /* Zero the new memory */
1042 memset(new_elist
+ nbmem
, 0,
1043 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1047 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1048 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1049 elist
[count
].enabled
= -1;
1055 DBG("Kernel list events done (%zu events)", count
);
1057 ret
= fclose(fp
); /* closes both fp and fd */
1073 * Get kernel version and validate it.
1075 int kernel_validate_version(int tracer_fd
,
1076 struct lttng_kernel_tracer_version
*version
,
1077 struct lttng_kernel_tracer_abi_version
*abi_version
)
1081 ret
= kernctl_tracer_version(tracer_fd
, version
);
1083 ERR("Failed to retrieve the lttng-modules version");
1087 /* Validate version */
1088 if (version
->major
!= VERSION_MAJOR
) {
1089 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1090 version
->major
, VERSION_MAJOR
);
1093 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1095 ERR("Failed to retrieve lttng-modules ABI version");
1098 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1099 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1100 abi_version
->major
, abi_version
->minor
,
1101 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1104 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1105 version
->major
, version
->minor
,
1106 abi_version
->major
, abi_version
->minor
);
1113 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1118 * Kernel work-arounds called at the start of sessiond main().
1120 int init_kernel_workarounds(void)
1126 * boot_id needs to be read once before being used concurrently
1127 * to deal with a Linux kernel race. A fix is proposed for
1128 * upstream, but the work-around is needed for older kernels.
1130 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1137 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1139 /* Ignore error, we don't really care */
1151 * Complete teardown of a kernel session.
1153 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1155 if (ksess
== NULL
) {
1156 DBG3("No kernel session when tearing down session");
1160 DBG("Tearing down kernel session");
1163 * Destroy channels on the consumer if at least one FD has been sent and we
1164 * are in no output mode because the streams are in *no* monitor mode so we
1165 * have to send a command to clean them up or else they leaked.
1167 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1169 struct consumer_socket
*socket
;
1170 struct lttng_ht_iter iter
;
1172 /* For each consumer socket. */
1174 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1175 socket
, node
.node
) {
1176 struct ltt_kernel_channel
*chan
;
1178 /* For each channel, ask the consumer to destroy it. */
1179 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1180 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1182 /* Consumer is probably dead. Use next socket. */
1190 /* Close any relayd session */
1191 consumer_output_send_destroy_relayd(ksess
->consumer
);
1193 trace_kernel_destroy_session(ksess
);
1197 * Destroy a kernel channel object. It does not do anything on the tracer side.
1199 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1201 struct ltt_kernel_session
*ksess
= NULL
;
1204 assert(kchan
->channel
);
1206 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1208 /* Update channel count of associated session. */
1209 if (kchan
->session
) {
1210 /* Keep pointer reference so we can update it after the destroy. */
1211 ksess
= kchan
->session
;
1214 trace_kernel_destroy_channel(kchan
);
1217 * At this point the kernel channel is not visible anymore. This is safe
1218 * since in order to work on a visible kernel session, the tracing session
1219 * lock (ltt_session.lock) MUST be acquired.
1222 ksess
->channel_count
--;
1227 * Take a snapshot for a given kernel session.
1229 * Return 0 on success or else return a LTTNG_ERR code.
1231 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1232 struct snapshot_output
*output
, int wait
,
1233 uint64_t nb_packets_per_stream
)
1235 int err
, ret
, saved_metadata_fd
;
1236 struct consumer_socket
*socket
;
1237 struct lttng_ht_iter iter
;
1238 struct ltt_kernel_metadata
*saved_metadata
;
1239 struct ltt_session
*session
;
1240 uint64_t trace_archive_id
;
1243 assert(ksess
->consumer
);
1246 DBG("Kernel snapshot record started");
1248 session
= session_find_by_id(ksess
->id
);
1250 assert(pthread_mutex_trylock(&session
->lock
));
1251 assert(session_trylock_list());
1252 trace_archive_id
= session
->current_archive_id
;
1254 /* Save current metadata since the following calls will change it. */
1255 saved_metadata
= ksess
->metadata
;
1256 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1260 ret
= kernel_open_metadata(ksess
);
1262 ret
= LTTNG_ERR_KERN_META_FAIL
;
1266 ret
= kernel_open_metadata_stream(ksess
);
1268 ret
= LTTNG_ERR_KERN_META_FAIL
;
1269 goto error_open_stream
;
1272 /* Send metadata to consumer and snapshot everything. */
1273 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1274 socket
, node
.node
) {
1275 struct consumer_output
*saved_output
;
1276 struct ltt_kernel_channel
*chan
;
1279 * Temporarly switch consumer output for our snapshot output. As long
1280 * as the session lock is taken, this is safe.
1282 saved_output
= ksess
->consumer
;
1283 ksess
->consumer
= output
->consumer
;
1285 pthread_mutex_lock(socket
->lock
);
1286 /* This stream must not be monitored by the consumer. */
1287 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1288 pthread_mutex_unlock(socket
->lock
);
1289 /* Put back the saved consumer output into the session. */
1290 ksess
->consumer
= saved_output
;
1292 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1293 goto error_consumer
;
1296 /* For each channel, ask the consumer to snapshot it. */
1297 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1298 ret
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1299 ksess
->uid
, ksess
->gid
,
1300 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1301 nb_packets_per_stream
,
1304 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1305 (void) kernel_consumer_destroy_metadata(socket
,
1307 goto error_consumer
;
1311 /* Snapshot metadata, */
1312 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1313 1, ksess
->uid
, ksess
->gid
,
1314 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1317 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1318 goto error_consumer
;
1322 * The metadata snapshot is done, ask the consumer to destroy it since
1323 * it's not monitored on the consumer side.
1325 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1331 /* Close newly opened metadata stream. It's now on the consumer side. */
1332 err
= close(ksess
->metadata_stream_fd
);
1334 PERROR("close snapshot kernel");
1338 trace_kernel_destroy_metadata(ksess
->metadata
);
1340 /* Restore metadata state.*/
1341 ksess
->metadata
= saved_metadata
;
1342 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1349 * Get the syscall mask array from the kernel tracer.
1351 * Return 0 on success else a negative value. In both case, syscall_mask should
1354 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1356 assert(syscall_mask
);
1359 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1363 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1366 * Return 1 on success, 0 when feature is not supported, negative value in case
1369 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1371 int ret
= 0; // Not supported by default
1372 struct lttng_kernel_tracer_abi_version abi
;
1374 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1376 ERR("Failed to retrieve lttng-modules ABI version");
1381 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1383 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1395 * Rotate a kernel session.
1397 * Return 0 on success or else return a LTTNG_ERR code.
1399 int kernel_rotate_session(struct ltt_session
*session
)
1402 struct consumer_socket
*socket
;
1403 struct lttng_ht_iter iter
;
1404 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1407 assert(ksess
->consumer
);
1409 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1410 session
->name
, session
->id
);
1415 * Note that this loop will end after one iteration given that there is
1416 * only one kernel consumer.
1418 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1419 socket
, node
.node
) {
1420 struct ltt_kernel_channel
*chan
;
1423 * Account the metadata channel first to make sure the
1424 * number of channels waiting for a rotation cannot
1425 * reach 0 before we complete the iteration over all
1428 ret
= rotate_add_channel_pending(ksess
->metadata
->key
,
1429 LTTNG_DOMAIN_KERNEL
, session
);
1431 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1435 /* For each channel, ask the consumer to rotate it. */
1436 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1437 ret
= rotate_add_channel_pending(chan
->key
,
1438 LTTNG_DOMAIN_KERNEL
, session
);
1440 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1444 DBG("Rotate channel %" PRIu64
", session %s", chan
->key
, session
->name
);
1445 ret
= consumer_rotate_channel(socket
, chan
->key
,
1446 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1447 ksess
->consumer
->subdir
,
1448 /* is_metadata_channel */ false,
1449 session
->current_archive_id
,
1450 &session
->rotate_pending_relay
);
1452 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1458 * Rotate the metadata channel.
1460 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1461 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1462 ksess
->consumer
->subdir
,
1463 /* is_metadata_channel */ true,
1464 session
->current_archive_id
,
1465 &session
->rotate_pending_relay
);
1467 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;