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 const struct lttng_userspace_probe_location
*probe_location
,
207 struct ltt_kernel_session
*session
, uint64_t *offset
)
211 const char *symbol
= NULL
;
212 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
213 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
215 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
216 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
218 lookup
= lttng_userspace_probe_location_get_lookup_method(
226 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
228 assert(lookup_method_type
==
229 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
231 symbol
= lttng_userspace_probe_location_function_get_function_name(
238 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
244 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
245 session
->gid
, offset
);
247 DBG("userspace probe offset calculation failed for "
248 "function %s", symbol
);
252 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
258 * Compute the offsets of the instrumentation bytes in the binary based on the
259 * tracepoint probe location using the SDT lookup method. This function
260 * allocates the offsets buffer, the caller must free it.
262 * Returns 0 on success and set the offset out parameter to the offsets of the
264 * Returns -1 on error.
267 int extract_userspace_probe_offset_tracepoint_sdt(
268 const struct lttng_userspace_probe_location
*probe_location
,
269 struct ltt_kernel_session
*session
, uint64_t **offsets
,
270 uint32_t *offsets_count
)
272 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
273 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
274 const char *probe_name
= NULL
, *provider_name
= NULL
;
278 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
279 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
281 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
288 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
290 assert(lookup_method_type
==
291 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
294 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
301 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
303 if (!provider_name
) {
308 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
314 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
315 session
->uid
, session
->gid
, offsets
, offsets_count
);
317 DBG("userspace probe offset calculation failed for sdt "
318 "probe %s:%s", provider_name
, probe_name
);
322 if (*offsets_count
== 0) {
323 DBG("no userspace probe offset found");
327 DBG("%u userspace probe SDT offsets found for %s:%s at:",
328 *offsets_count
, provider_name
, probe_name
);
329 for (i
= 0; i
< *offsets_count
; i
++) {
330 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
337 * Extract the offsets of the instrumentation point for the different lookup
341 int userspace_probe_add_callsites(struct lttng_event
*ev
,
342 struct ltt_kernel_session
*session
, int fd
)
344 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
345 enum lttng_userspace_probe_location_lookup_method_type type
;
346 const struct lttng_userspace_probe_location
*location
= NULL
;
350 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
352 location
= lttng_event_get_userspace_probe_location(ev
);
358 lttng_userspace_probe_location_get_lookup_method(location
);
359 if (!lookup_method
) {
364 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
366 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
368 struct lttng_kernel_event_callsite callsite
;
371 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
373 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
377 callsite
.u
.uprobe
.offset
= offset
;
378 ret
= kernctl_add_callsite(fd
, &callsite
);
380 WARN("Adding callsite to userspace probe "
381 "event %s failed.", ev
->name
);
382 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
387 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
390 uint64_t *offsets
= NULL
;
391 uint32_t offsets_count
;
392 struct lttng_kernel_event_callsite callsite
;
395 * This call allocates the offsets buffer. This buffer must be freed
398 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
399 &offsets
, &offsets_count
);
401 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
404 for (i
= 0; i
< offsets_count
; i
++) {
405 callsite
.u
.uprobe
.offset
= offsets
[i
];
406 ret
= kernctl_add_callsite(fd
, &callsite
);
408 WARN("Adding callsite to userspace probe "
409 "event %s failed.", ev
->name
);
410 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
419 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
427 * Create a kernel event, enable it to the kernel tracer and add it to the
428 * channel event list of the kernel session.
429 * We own filter_expression and filter.
431 int kernel_create_event(struct lttng_event
*ev
,
432 struct ltt_kernel_channel
*channel
,
433 char *filter_expression
,
434 struct lttng_filter_bytecode
*filter
)
437 enum lttng_error_code ret
;
438 struct ltt_kernel_event
*event
;
443 /* We pass ownership of filter_expression and filter */
444 ret
= trace_kernel_create_event(ev
, filter_expression
,
446 if (ret
!= LTTNG_OK
) {
450 fd
= kernctl_create_event(channel
->fd
, event
->event
);
454 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
457 WARN("Event type not implemented");
458 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
461 WARN("Event %s not found!", ev
->name
);
462 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
465 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
466 PERROR("create event ioctl");
471 event
->type
= ev
->type
;
473 /* Prevent fd duplication after execlp() */
474 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
476 PERROR("fcntl session fd");
480 err
= kernctl_filter(event
->fd
, filter
);
484 ret
= LTTNG_ERR_FILTER_NOMEM
;
487 ret
= LTTNG_ERR_FILTER_INVAL
;
494 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
495 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
497 goto add_callsite_error
;
501 err
= kernctl_enable(event
->fd
);
505 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
508 PERROR("enable kernel event");
509 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
515 /* Add event to event list */
516 cds_list_add(&event
->list
, &channel
->events_list
.head
);
517 channel
->event_count
++;
519 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
529 closeret
= close(event
->fd
);
531 PERROR("close event fd");
541 * Disable a kernel channel.
543 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
549 ret
= kernctl_disable(chan
->fd
);
551 PERROR("disable chan ioctl");
556 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
557 chan
->channel
->name
, chan
->fd
, chan
->key
);
566 * Enable a kernel channel.
568 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
574 ret
= kernctl_enable(chan
->fd
);
575 if (ret
< 0 && ret
!= -EEXIST
) {
576 PERROR("Enable kernel chan");
581 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
582 chan
->channel
->name
, chan
->fd
, chan
->key
);
591 * Enable a kernel event.
593 int kernel_enable_event(struct ltt_kernel_event
*event
)
599 ret
= kernctl_enable(event
->fd
);
603 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
606 PERROR("enable kernel event");
613 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
622 * Disable a kernel event.
624 int kernel_disable_event(struct ltt_kernel_event
*event
)
630 ret
= kernctl_disable(event
->fd
);
634 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
637 PERROR("disable kernel event");
644 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
653 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
657 DBG("Kernel track PID %d for session id %" PRIu64
".",
659 ret
= kernctl_track_pid(session
->fd
, pid
);
665 return LTTNG_ERR_INVALID
;
667 return LTTNG_ERR_NOMEM
;
669 return LTTNG_ERR_PID_TRACKED
;
671 return LTTNG_ERR_UNK
;
675 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
679 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
681 ret
= kernctl_untrack_pid(session
->fd
, pid
);
687 return LTTNG_ERR_INVALID
;
689 return LTTNG_ERR_NOMEM
;
691 return LTTNG_ERR_PID_NOT_TRACKED
;
693 return LTTNG_ERR_UNK
;
697 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
702 ssize_t nbmem
, count
= 0;
706 fd
= kernctl_list_tracker_pids(session
->fd
);
708 PERROR("kernel tracker pids list");
712 fp
= fdopen(fd
, "r");
714 PERROR("kernel tracker pids list fdopen");
718 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
719 pids
= zmalloc(sizeof(*pids
) * nbmem
);
721 PERROR("alloc list pids");
726 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
727 if (count
>= nbmem
) {
731 new_nbmem
= nbmem
<< 1;
732 DBG("Reallocating pids list from %zu to %zu entries",
734 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
735 if (new_pids
== NULL
) {
736 PERROR("realloc list events");
741 /* Zero the new memory */
742 memset(new_pids
+ nbmem
, 0,
743 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
751 DBG("Kernel list tracker pids done (%zd pids)", count
);
753 ret
= fclose(fp
); /* closes both fp and fd */
769 * Create kernel metadata, open from the kernel tracer and add it to the
772 int kernel_open_metadata(struct ltt_kernel_session
*session
)
775 struct ltt_kernel_metadata
*lkm
= NULL
;
779 /* Allocate kernel metadata */
780 lkm
= trace_kernel_create_metadata();
785 /* Kernel tracer metadata creation */
786 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
792 lkm
->key
= ++next_kernel_channel_key
;
793 /* Prevent fd duplication after execlp() */
794 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
796 PERROR("fcntl session fd");
799 session
->metadata
= lkm
;
801 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
806 trace_kernel_destroy_metadata(lkm
);
812 * Start tracing session.
814 int kernel_start_session(struct ltt_kernel_session
*session
)
820 ret
= kernctl_start_session(session
->fd
);
822 PERROR("ioctl start session");
826 DBG("Kernel session started");
835 * Make a kernel wait to make sure in-flight probe have completed.
837 void kernel_wait_quiescent(int fd
)
841 DBG("Kernel quiescent wait on %d", fd
);
843 ret
= kernctl_wait_quiescent(fd
);
845 PERROR("wait quiescent ioctl");
846 ERR("Kernel quiescent wait failed");
851 * Force flush buffer of metadata.
853 int kernel_metadata_flush_buffer(int fd
)
857 DBG("Kernel flushing metadata buffer on fd %d", fd
);
859 ret
= kernctl_buffer_flush(fd
);
861 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
868 * Force flush buffer for channel.
870 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
873 struct ltt_kernel_stream
*stream
;
877 DBG("Flush buffer for channel %s", channel
->channel
->name
);
879 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
880 DBG("Flushing channel stream %d", stream
->fd
);
881 ret
= kernctl_buffer_flush(stream
->fd
);
884 ERR("Fail to flush buffer for stream %d (ret: %d)",
893 * Stop tracing session.
895 int kernel_stop_session(struct ltt_kernel_session
*session
)
901 ret
= kernctl_stop_session(session
->fd
);
906 DBG("Kernel session stopped");
915 * Open stream of channel, register it to the kernel tracer and add it
916 * to the stream list of the channel.
918 * Note: given that the streams may appear in random order wrt CPU
919 * number (e.g. cpu hotplug), the index value of the stream number in
920 * the stream name is not necessarily linked to the CPU number.
922 * Return the number of created stream. Else, a negative value.
924 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
927 struct ltt_kernel_stream
*lks
;
931 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
932 lks
= trace_kernel_create_stream(channel
->channel
->name
,
933 channel
->stream_count
);
943 /* Prevent fd duplication after execlp() */
944 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
946 PERROR("fcntl session fd");
949 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
950 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
952 /* Add stream to channel stream list */
953 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
954 channel
->stream_count
++;
956 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
960 return channel
->stream_count
;
967 * Open the metadata stream and set it to the kernel session.
969 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
975 ret
= kernctl_create_stream(session
->metadata
->fd
);
977 PERROR("kernel create metadata stream");
981 DBG("Kernel metadata stream created (fd: %d)", ret
);
982 session
->metadata_stream_fd
= ret
;
983 /* Prevent fd duplication after execlp() */
984 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
986 PERROR("fcntl session fd");
996 * Get the event list from the kernel tracer and return the number of elements.
998 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
1002 size_t nbmem
, count
= 0;
1004 struct lttng_event
*elist
;
1008 fd
= kernctl_tracepoint_list(tracer_fd
);
1010 PERROR("kernel tracepoint list");
1014 fp
= fdopen(fd
, "r");
1016 PERROR("kernel tracepoint list fdopen");
1021 * Init memory size counter
1022 * See kernel-ctl.h for explanation of this value
1024 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1025 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1026 if (elist
== NULL
) {
1027 PERROR("alloc list events");
1032 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1033 if (count
>= nbmem
) {
1034 struct lttng_event
*new_elist
;
1037 new_nbmem
= nbmem
<< 1;
1038 DBG("Reallocating event list from %zu to %zu bytes",
1040 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1041 if (new_elist
== NULL
) {
1042 PERROR("realloc list events");
1048 /* Zero the new memory */
1049 memset(new_elist
+ nbmem
, 0,
1050 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1054 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1055 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1056 elist
[count
].enabled
= -1;
1062 DBG("Kernel list events done (%zu events)", count
);
1064 ret
= fclose(fp
); /* closes both fp and fd */
1080 * Get kernel version and validate it.
1082 int kernel_validate_version(int tracer_fd
,
1083 struct lttng_kernel_tracer_version
*version
,
1084 struct lttng_kernel_tracer_abi_version
*abi_version
)
1088 ret
= kernctl_tracer_version(tracer_fd
, version
);
1090 ERR("Failed to retrieve the lttng-modules version");
1094 /* Validate version */
1095 if (version
->major
!= VERSION_MAJOR
) {
1096 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1097 version
->major
, VERSION_MAJOR
);
1100 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1102 ERR("Failed to retrieve lttng-modules ABI version");
1105 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1106 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1107 abi_version
->major
, abi_version
->minor
,
1108 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1111 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1112 version
->major
, version
->minor
,
1113 abi_version
->major
, abi_version
->minor
);
1120 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1125 * Kernel work-arounds called at the start of sessiond main().
1127 int init_kernel_workarounds(void)
1133 * boot_id needs to be read once before being used concurrently
1134 * to deal with a Linux kernel race. A fix is proposed for
1135 * upstream, but the work-around is needed for older kernels.
1137 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1144 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1146 /* Ignore error, we don't really care */
1158 * Complete teardown of a kernel session.
1160 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1162 if (ksess
== NULL
) {
1163 DBG3("No kernel session when tearing down session");
1167 DBG("Tearing down kernel session");
1170 * Destroy channels on the consumer if at least one FD has been sent and we
1171 * are in no output mode because the streams are in *no* monitor mode so we
1172 * have to send a command to clean them up or else they leaked.
1174 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1176 struct consumer_socket
*socket
;
1177 struct lttng_ht_iter iter
;
1179 /* For each consumer socket. */
1181 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1182 socket
, node
.node
) {
1183 struct ltt_kernel_channel
*chan
;
1185 /* For each channel, ask the consumer to destroy it. */
1186 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1187 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1189 /* Consumer is probably dead. Use next socket. */
1197 /* Close any relayd session */
1198 consumer_output_send_destroy_relayd(ksess
->consumer
);
1200 trace_kernel_destroy_session(ksess
);
1204 * Destroy a kernel channel object. It does not do anything on the tracer side.
1206 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1208 struct ltt_kernel_session
*ksess
= NULL
;
1211 assert(kchan
->channel
);
1213 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1215 /* Update channel count of associated session. */
1216 if (kchan
->session
) {
1217 /* Keep pointer reference so we can update it after the destroy. */
1218 ksess
= kchan
->session
;
1221 trace_kernel_destroy_channel(kchan
);
1224 * At this point the kernel channel is not visible anymore. This is safe
1225 * since in order to work on a visible kernel session, the tracing session
1226 * lock (ltt_session.lock) MUST be acquired.
1229 ksess
->channel_count
--;
1234 * Take a snapshot for a given kernel session.
1236 * Return 0 on success or else return a LTTNG_ERR code.
1238 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1239 struct snapshot_output
*output
, int wait
,
1240 uint64_t nb_packets_per_stream
)
1242 int err
, ret
, saved_metadata_fd
;
1243 struct consumer_socket
*socket
;
1244 struct lttng_ht_iter iter
;
1245 struct ltt_kernel_metadata
*saved_metadata
;
1246 struct ltt_session
*session
;
1247 uint64_t trace_archive_id
;
1250 assert(ksess
->consumer
);
1253 DBG("Kernel snapshot record started");
1255 session
= session_find_by_id(ksess
->id
);
1257 assert(pthread_mutex_trylock(&session
->lock
));
1258 assert(session_trylock_list());
1259 trace_archive_id
= session
->current_archive_id
;
1261 /* Save current metadata since the following calls will change it. */
1262 saved_metadata
= ksess
->metadata
;
1263 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1267 ret
= kernel_open_metadata(ksess
);
1269 ret
= LTTNG_ERR_KERN_META_FAIL
;
1273 ret
= kernel_open_metadata_stream(ksess
);
1275 ret
= LTTNG_ERR_KERN_META_FAIL
;
1276 goto error_open_stream
;
1279 /* Send metadata to consumer and snapshot everything. */
1280 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1281 socket
, node
.node
) {
1282 struct consumer_output
*saved_output
;
1283 struct ltt_kernel_channel
*chan
;
1286 * Temporarly switch consumer output for our snapshot output. As long
1287 * as the session lock is taken, this is safe.
1289 saved_output
= ksess
->consumer
;
1290 ksess
->consumer
= output
->consumer
;
1292 pthread_mutex_lock(socket
->lock
);
1293 /* This stream must not be monitored by the consumer. */
1294 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1295 pthread_mutex_unlock(socket
->lock
);
1296 /* Put back the saved consumer output into the session. */
1297 ksess
->consumer
= saved_output
;
1299 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1300 goto error_consumer
;
1303 /* For each channel, ask the consumer to snapshot it. */
1304 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1305 ret
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1306 ksess
->uid
, ksess
->gid
,
1307 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1308 nb_packets_per_stream
,
1311 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1312 (void) kernel_consumer_destroy_metadata(socket
,
1314 goto error_consumer
;
1318 /* Snapshot metadata, */
1319 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1320 1, ksess
->uid
, ksess
->gid
,
1321 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1324 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1325 goto error_consumer
;
1329 * The metadata snapshot is done, ask the consumer to destroy it since
1330 * it's not monitored on the consumer side.
1332 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1338 /* Close newly opened metadata stream. It's now on the consumer side. */
1339 err
= close(ksess
->metadata_stream_fd
);
1341 PERROR("close snapshot kernel");
1345 trace_kernel_destroy_metadata(ksess
->metadata
);
1347 /* Restore metadata state.*/
1348 ksess
->metadata
= saved_metadata
;
1349 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1356 * Get the syscall mask array from the kernel tracer.
1358 * Return 0 on success else a negative value. In both case, syscall_mask should
1361 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1363 assert(syscall_mask
);
1366 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1370 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1373 * Return 1 on success, 0 when feature is not supported, negative value in case
1376 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1378 int ret
= 0; // Not supported by default
1379 struct lttng_kernel_tracer_abi_version abi
;
1381 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1383 ERR("Failed to retrieve lttng-modules ABI version");
1388 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1390 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1402 * Rotate a kernel session.
1404 * Return 0 on success or else return a LTTNG_ERR code.
1406 int kernel_rotate_session(struct ltt_session
*session
)
1409 struct consumer_socket
*socket
;
1410 struct lttng_ht_iter iter
;
1411 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1414 assert(ksess
->consumer
);
1416 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1417 session
->name
, session
->id
);
1422 * Note that this loop will end after one iteration given that there is
1423 * only one kernel consumer.
1425 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1426 socket
, node
.node
) {
1427 struct ltt_kernel_channel
*chan
;
1429 /* For each channel, ask the consumer to rotate it. */
1430 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1431 DBG("Rotate kernel channel %" PRIu64
", session %s",
1432 chan
->key
, session
->name
);
1433 ret
= consumer_rotate_channel(socket
, chan
->key
,
1434 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1435 ksess
->consumer
->subdir
,
1436 /* is_metadata_channel */ false,
1437 session
->current_archive_id
);
1439 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1445 * Rotate the metadata channel.
1447 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1448 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1449 ksess
->consumer
->subdir
,
1450 /* is_metadata_channel */ true,
1451 session
->current_archive_id
);
1453 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;