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
);
128 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
130 WARN("Could not set kernel session name");
137 trace_kernel_destroy_session(lks
);
143 * Create a kernel channel, register it to the kernel tracer and add it to the
146 int kernel_create_channel(struct ltt_kernel_session
*session
,
147 struct lttng_channel
*chan
)
150 struct ltt_kernel_channel
*lkc
;
155 /* Allocate kernel channel */
156 lkc
= trace_kernel_create_channel(chan
);
161 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
162 chan
->name
, lkc
->channel
->attr
.overwrite
,
163 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
164 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
165 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
167 /* Kernel tracer channel creation */
168 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
170 PERROR("ioctl kernel create channel");
174 /* Setup the channel fd */
176 /* Prevent fd duplication after execlp() */
177 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
179 PERROR("fcntl session fd");
182 /* Add channel to session */
183 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
184 session
->channel_count
++;
185 lkc
->session
= session
;
186 lkc
->key
= ++next_kernel_channel_key
;
188 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
189 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
202 * Compute the offset of the instrumentation byte in the binary based on the
203 * function probe location using the ELF lookup method.
205 * Returns 0 on success and set the offset out parameter to the offset of the
207 * Returns -1 on error
210 int extract_userspace_probe_offset_function_elf(
211 const struct lttng_userspace_probe_location
*probe_location
,
212 struct ltt_kernel_session
*session
, uint64_t *offset
)
216 const char *symbol
= NULL
;
217 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
218 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
220 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
221 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
223 lookup
= lttng_userspace_probe_location_get_lookup_method(
231 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
233 assert(lookup_method_type
==
234 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
236 symbol
= lttng_userspace_probe_location_function_get_function_name(
243 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
249 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
250 session
->gid
, offset
);
252 DBG("userspace probe offset calculation failed for "
253 "function %s", symbol
);
257 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
263 * Compute the offsets of the instrumentation bytes in the binary based on the
264 * tracepoint probe location using the SDT lookup method. This function
265 * allocates the offsets buffer, the caller must free it.
267 * Returns 0 on success and set the offset out parameter to the offsets of the
269 * Returns -1 on error.
272 int extract_userspace_probe_offset_tracepoint_sdt(
273 const struct lttng_userspace_probe_location
*probe_location
,
274 struct ltt_kernel_session
*session
, uint64_t **offsets
,
275 uint32_t *offsets_count
)
277 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
278 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
279 const char *probe_name
= NULL
, *provider_name
= NULL
;
283 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
284 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
286 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
293 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
295 assert(lookup_method_type
==
296 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
299 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
306 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
308 if (!provider_name
) {
313 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
319 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
320 session
->uid
, session
->gid
, offsets
, offsets_count
);
322 DBG("userspace probe offset calculation failed for sdt "
323 "probe %s:%s", provider_name
, probe_name
);
327 if (*offsets_count
== 0) {
328 DBG("no userspace probe offset found");
332 DBG("%u userspace probe SDT offsets found for %s:%s at:",
333 *offsets_count
, provider_name
, probe_name
);
334 for (i
= 0; i
< *offsets_count
; i
++) {
335 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
342 * Extract the offsets of the instrumentation point for the different lookup
346 int userspace_probe_add_callsites(struct lttng_event
*ev
,
347 struct ltt_kernel_session
*session
, int fd
)
349 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
350 enum lttng_userspace_probe_location_lookup_method_type type
;
351 const struct lttng_userspace_probe_location
*location
= NULL
;
355 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
357 location
= lttng_event_get_userspace_probe_location(ev
);
363 lttng_userspace_probe_location_get_lookup_method(location
);
364 if (!lookup_method
) {
369 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
371 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
373 struct lttng_kernel_event_callsite callsite
;
376 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
378 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
382 callsite
.u
.uprobe
.offset
= offset
;
383 ret
= kernctl_add_callsite(fd
, &callsite
);
385 WARN("Adding callsite to userspace probe "
386 "event %s failed.", ev
->name
);
387 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
392 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
395 uint64_t *offsets
= NULL
;
396 uint32_t offsets_count
;
397 struct lttng_kernel_event_callsite callsite
;
400 * This call allocates the offsets buffer. This buffer must be freed
403 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
404 &offsets
, &offsets_count
);
406 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
409 for (i
= 0; i
< offsets_count
; i
++) {
410 callsite
.u
.uprobe
.offset
= offsets
[i
];
411 ret
= kernctl_add_callsite(fd
, &callsite
);
413 WARN("Adding callsite to userspace probe "
414 "event %s failed.", ev
->name
);
415 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
424 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
432 * Create a kernel event, enable it to the kernel tracer and add it to the
433 * channel event list of the kernel session.
434 * We own filter_expression and filter.
436 int kernel_create_event(struct lttng_event
*ev
,
437 struct ltt_kernel_channel
*channel
,
438 char *filter_expression
,
439 struct lttng_filter_bytecode
*filter
)
442 enum lttng_error_code ret
;
443 struct ltt_kernel_event
*event
;
448 /* We pass ownership of filter_expression and filter */
449 ret
= trace_kernel_create_event(ev
, filter_expression
,
451 if (ret
!= LTTNG_OK
) {
455 fd
= kernctl_create_event(channel
->fd
, event
->event
);
459 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
462 WARN("Event type not implemented");
463 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
466 WARN("Event %s not found!", ev
->name
);
467 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
470 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
471 PERROR("create event ioctl");
476 event
->type
= ev
->type
;
478 /* Prevent fd duplication after execlp() */
479 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
481 PERROR("fcntl session fd");
485 err
= kernctl_filter(event
->fd
, filter
);
489 ret
= LTTNG_ERR_FILTER_NOMEM
;
492 ret
= LTTNG_ERR_FILTER_INVAL
;
499 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
500 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
502 goto add_callsite_error
;
506 err
= kernctl_enable(event
->fd
);
510 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
513 PERROR("enable kernel event");
514 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
520 /* Add event to event list */
521 cds_list_add(&event
->list
, &channel
->events_list
.head
);
522 channel
->event_count
++;
524 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
534 closeret
= close(event
->fd
);
536 PERROR("close event fd");
546 * Disable a kernel channel.
548 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
554 ret
= kernctl_disable(chan
->fd
);
556 PERROR("disable chan ioctl");
561 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
562 chan
->channel
->name
, chan
->fd
, chan
->key
);
571 * Enable a kernel channel.
573 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
579 ret
= kernctl_enable(chan
->fd
);
580 if (ret
< 0 && ret
!= -EEXIST
) {
581 PERROR("Enable kernel chan");
586 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
587 chan
->channel
->name
, chan
->fd
, chan
->key
);
596 * Enable a kernel event.
598 int kernel_enable_event(struct ltt_kernel_event
*event
)
604 ret
= kernctl_enable(event
->fd
);
608 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
611 PERROR("enable kernel event");
618 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
627 * Disable a kernel event.
629 int kernel_disable_event(struct ltt_kernel_event
*event
)
635 ret
= kernctl_disable(event
->fd
);
639 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
642 PERROR("disable kernel event");
649 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
658 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
662 DBG("Kernel track PID %d for session id %" PRIu64
".",
664 ret
= kernctl_track_pid(session
->fd
, pid
);
670 return LTTNG_ERR_INVALID
;
672 return LTTNG_ERR_NOMEM
;
674 return LTTNG_ERR_PID_TRACKED
;
676 return LTTNG_ERR_UNK
;
680 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
684 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
686 ret
= kernctl_untrack_pid(session
->fd
, pid
);
692 return LTTNG_ERR_INVALID
;
694 return LTTNG_ERR_NOMEM
;
696 return LTTNG_ERR_PID_NOT_TRACKED
;
698 return LTTNG_ERR_UNK
;
702 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
707 ssize_t nbmem
, count
= 0;
711 fd
= kernctl_list_tracker_pids(session
->fd
);
713 PERROR("kernel tracker pids list");
717 fp
= fdopen(fd
, "r");
719 PERROR("kernel tracker pids list fdopen");
723 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
724 pids
= zmalloc(sizeof(*pids
) * nbmem
);
726 PERROR("alloc list pids");
731 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
732 if (count
>= nbmem
) {
736 new_nbmem
= nbmem
<< 1;
737 DBG("Reallocating pids list from %zu to %zu entries",
739 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
740 if (new_pids
== NULL
) {
741 PERROR("realloc list events");
746 /* Zero the new memory */
747 memset(new_pids
+ nbmem
, 0,
748 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
756 DBG("Kernel list tracker pids done (%zd pids)", count
);
758 ret
= fclose(fp
); /* closes both fp and fd */
774 * Create kernel metadata, open from the kernel tracer and add it to the
777 int kernel_open_metadata(struct ltt_kernel_session
*session
)
780 struct ltt_kernel_metadata
*lkm
= NULL
;
784 /* Allocate kernel metadata */
785 lkm
= trace_kernel_create_metadata();
790 /* Kernel tracer metadata creation */
791 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
797 lkm
->key
= ++next_kernel_channel_key
;
798 /* Prevent fd duplication after execlp() */
799 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
801 PERROR("fcntl session fd");
804 session
->metadata
= lkm
;
806 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
811 trace_kernel_destroy_metadata(lkm
);
817 * Start tracing session.
819 int kernel_start_session(struct ltt_kernel_session
*session
)
825 ret
= kernctl_start_session(session
->fd
);
827 PERROR("ioctl start session");
831 DBG("Kernel session started");
840 * Make a kernel wait to make sure in-flight probe have completed.
842 void kernel_wait_quiescent(int fd
)
846 DBG("Kernel quiescent wait on %d", fd
);
848 ret
= kernctl_wait_quiescent(fd
);
850 PERROR("wait quiescent ioctl");
851 ERR("Kernel quiescent wait failed");
856 * Force flush buffer of metadata.
858 int kernel_metadata_flush_buffer(int fd
)
862 DBG("Kernel flushing metadata buffer on fd %d", fd
);
864 ret
= kernctl_buffer_flush(fd
);
866 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
873 * Force flush buffer for channel.
875 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
878 struct ltt_kernel_stream
*stream
;
882 DBG("Flush buffer for channel %s", channel
->channel
->name
);
884 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
885 DBG("Flushing channel stream %d", stream
->fd
);
886 ret
= kernctl_buffer_flush(stream
->fd
);
889 ERR("Fail to flush buffer for stream %d (ret: %d)",
898 * Stop tracing session.
900 int kernel_stop_session(struct ltt_kernel_session
*session
)
906 ret
= kernctl_stop_session(session
->fd
);
911 DBG("Kernel session stopped");
920 * Open stream of channel, register it to the kernel tracer and add it
921 * to the stream list of the channel.
923 * Note: given that the streams may appear in random order wrt CPU
924 * number (e.g. cpu hotplug), the index value of the stream number in
925 * the stream name is not necessarily linked to the CPU number.
927 * Return the number of created stream. Else, a negative value.
929 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
932 struct ltt_kernel_stream
*lks
;
936 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
937 lks
= trace_kernel_create_stream(channel
->channel
->name
,
938 channel
->stream_count
);
948 /* Prevent fd duplication after execlp() */
949 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
951 PERROR("fcntl session fd");
954 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
955 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
957 /* Add stream to channel stream list */
958 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
959 channel
->stream_count
++;
961 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
965 return channel
->stream_count
;
972 * Open the metadata stream and set it to the kernel session.
974 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
980 ret
= kernctl_create_stream(session
->metadata
->fd
);
982 PERROR("kernel create metadata stream");
986 DBG("Kernel metadata stream created (fd: %d)", ret
);
987 session
->metadata_stream_fd
= ret
;
988 /* Prevent fd duplication after execlp() */
989 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
991 PERROR("fcntl session fd");
1001 * Get the event list from the kernel tracer and return the number of elements.
1003 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
1007 size_t nbmem
, count
= 0;
1009 struct lttng_event
*elist
;
1013 fd
= kernctl_tracepoint_list(tracer_fd
);
1015 PERROR("kernel tracepoint list");
1019 fp
= fdopen(fd
, "r");
1021 PERROR("kernel tracepoint list fdopen");
1026 * Init memory size counter
1027 * See kernel-ctl.h for explanation of this value
1029 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1030 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1031 if (elist
== NULL
) {
1032 PERROR("alloc list events");
1037 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1038 if (count
>= nbmem
) {
1039 struct lttng_event
*new_elist
;
1042 new_nbmem
= nbmem
<< 1;
1043 DBG("Reallocating event list from %zu to %zu bytes",
1045 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1046 if (new_elist
== NULL
) {
1047 PERROR("realloc list events");
1053 /* Zero the new memory */
1054 memset(new_elist
+ nbmem
, 0,
1055 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1059 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1060 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1061 elist
[count
].enabled
= -1;
1067 DBG("Kernel list events done (%zu events)", count
);
1069 ret
= fclose(fp
); /* closes both fp and fd */
1085 * Get kernel version and validate it.
1087 int kernel_validate_version(int tracer_fd
,
1088 struct lttng_kernel_tracer_version
*version
,
1089 struct lttng_kernel_tracer_abi_version
*abi_version
)
1093 ret
= kernctl_tracer_version(tracer_fd
, version
);
1095 ERR("Failed to retrieve the lttng-modules version");
1099 /* Validate version */
1100 if (version
->major
!= VERSION_MAJOR
) {
1101 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1102 version
->major
, VERSION_MAJOR
);
1105 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1107 ERR("Failed to retrieve lttng-modules ABI version");
1110 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1111 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1112 abi_version
->major
, abi_version
->minor
,
1113 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1116 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1117 version
->major
, version
->minor
,
1118 abi_version
->major
, abi_version
->minor
);
1125 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1130 * Kernel work-arounds called at the start of sessiond main().
1132 int init_kernel_workarounds(void)
1138 * boot_id needs to be read once before being used concurrently
1139 * to deal with a Linux kernel race. A fix is proposed for
1140 * upstream, but the work-around is needed for older kernels.
1142 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1149 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1151 /* Ignore error, we don't really care */
1163 * Complete teardown of a kernel session.
1165 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1167 if (ksess
== NULL
) {
1168 DBG3("No kernel session when tearing down session");
1172 DBG("Tearing down kernel session");
1175 * Destroy channels on the consumer if at least one FD has been sent and we
1176 * are in no output mode because the streams are in *no* monitor mode so we
1177 * have to send a command to clean them up or else they leaked.
1179 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1181 struct consumer_socket
*socket
;
1182 struct lttng_ht_iter iter
;
1184 /* For each consumer socket. */
1186 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1187 socket
, node
.node
) {
1188 struct ltt_kernel_channel
*chan
;
1190 /* For each channel, ask the consumer to destroy it. */
1191 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1192 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1194 /* Consumer is probably dead. Use next socket. */
1202 /* Close any relayd session */
1203 consumer_output_send_destroy_relayd(ksess
->consumer
);
1205 trace_kernel_destroy_session(ksess
);
1209 * Destroy a kernel channel object. It does not do anything on the tracer side.
1211 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1213 struct ltt_kernel_session
*ksess
= NULL
;
1216 assert(kchan
->channel
);
1218 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1220 /* Update channel count of associated session. */
1221 if (kchan
->session
) {
1222 /* Keep pointer reference so we can update it after the destroy. */
1223 ksess
= kchan
->session
;
1226 trace_kernel_destroy_channel(kchan
);
1229 * At this point the kernel channel is not visible anymore. This is safe
1230 * since in order to work on a visible kernel session, the tracing session
1231 * lock (ltt_session.lock) MUST be acquired.
1234 ksess
->channel_count
--;
1239 * Take a snapshot for a given kernel session.
1241 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1243 enum lttng_error_code
kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1244 struct snapshot_output
*output
, int wait
,
1245 uint64_t nb_packets_per_stream
)
1247 int err
, ret
, saved_metadata_fd
;
1248 enum lttng_error_code status
= LTTNG_OK
;
1249 struct consumer_socket
*socket
;
1250 struct lttng_ht_iter iter
;
1251 struct ltt_kernel_metadata
*saved_metadata
;
1252 struct ltt_session
*session
= NULL
;
1253 uint64_t trace_archive_id
;
1256 assert(ksess
->consumer
);
1259 DBG("Kernel snapshot record started");
1261 session
= session_find_by_id(ksess
->id
);
1263 assert(pthread_mutex_trylock(&session
->lock
));
1264 assert(session_trylock_list());
1265 trace_archive_id
= session
->current_archive_id
;
1267 /* Save current metadata since the following calls will change it. */
1268 saved_metadata
= ksess
->metadata
;
1269 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1273 ret
= kernel_open_metadata(ksess
);
1275 status
= LTTNG_ERR_KERN_META_FAIL
;
1279 ret
= kernel_open_metadata_stream(ksess
);
1281 status
= LTTNG_ERR_KERN_META_FAIL
;
1282 goto error_open_stream
;
1285 /* Send metadata to consumer and snapshot everything. */
1286 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1287 socket
, node
.node
) {
1288 struct consumer_output
*saved_output
;
1289 struct ltt_kernel_channel
*chan
;
1292 * Temporarly switch consumer output for our snapshot output. As long
1293 * as the session lock is taken, this is safe.
1295 saved_output
= ksess
->consumer
;
1296 ksess
->consumer
= output
->consumer
;
1298 pthread_mutex_lock(socket
->lock
);
1299 /* This stream must not be monitored by the consumer. */
1300 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1301 pthread_mutex_unlock(socket
->lock
);
1302 /* Put back the saved consumer output into the session. */
1303 ksess
->consumer
= saved_output
;
1305 status
= LTTNG_ERR_KERN_META_FAIL
;
1306 goto error_consumer
;
1309 /* For each channel, ask the consumer to snapshot it. */
1310 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1311 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1312 ksess
->uid
, ksess
->gid
,
1313 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1314 nb_packets_per_stream
,
1316 if (status
!= LTTNG_OK
) {
1317 (void) kernel_consumer_destroy_metadata(socket
,
1319 goto error_consumer
;
1323 /* Snapshot metadata, */
1324 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1325 1, ksess
->uid
, ksess
->gid
,
1326 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1328 if (status
!= LTTNG_OK
) {
1329 goto error_consumer
;
1333 * The metadata snapshot is done, ask the consumer to destroy it since
1334 * it's not monitored on the consumer side.
1336 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1340 /* Close newly opened metadata stream. It's now on the consumer side. */
1341 err
= close(ksess
->metadata_stream_fd
);
1343 PERROR("close snapshot kernel");
1347 trace_kernel_destroy_metadata(ksess
->metadata
);
1349 /* Restore metadata state.*/
1350 ksess
->metadata
= saved_metadata
;
1351 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1353 session_put(session
);
1360 * Get the syscall mask array from the kernel tracer.
1362 * Return 0 on success else a negative value. In both case, syscall_mask should
1365 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1367 assert(syscall_mask
);
1370 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1374 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1377 * Return 1 on success, 0 when feature is not supported, negative value in case
1380 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1382 int ret
= 0; // Not supported by default
1383 struct lttng_kernel_tracer_abi_version abi
;
1385 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1387 ERR("Failed to retrieve lttng-modules ABI version");
1392 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1394 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1406 * Rotate a kernel session.
1408 * Return LTTNG_OK on success or else an LTTng error code.
1410 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1413 enum lttng_error_code status
= LTTNG_OK
;
1414 struct consumer_socket
*socket
;
1415 struct lttng_ht_iter iter
;
1416 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1419 assert(ksess
->consumer
);
1421 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1422 session
->name
, session
->id
);
1427 * Note that this loop will end after one iteration given that there is
1428 * only one kernel consumer.
1430 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1431 socket
, node
.node
) {
1432 struct ltt_kernel_channel
*chan
;
1434 /* For each channel, ask the consumer to rotate it. */
1435 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1436 DBG("Rotate kernel channel %" PRIu64
", session %s",
1437 chan
->key
, session
->name
);
1438 ret
= consumer_rotate_channel(socket
, chan
->key
,
1439 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1440 ksess
->consumer
->domain_subdir
,
1441 /* is_metadata_channel */ false,
1442 session
->current_archive_id
);
1444 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1450 * Rotate the metadata channel.
1452 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1453 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1454 ksess
->consumer
->domain_subdir
,
1455 /* is_metadata_channel */ true,
1456 session
->current_archive_id
);
1458 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;