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/trace-chunk.h>
28 #include <common/kernel-ctl/kernel-ctl.h>
29 #include <common/kernel-ctl/kernel-ioctl.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "kernel-consumer.h"
35 #include "kern-modules.h"
40 * Key used to reference a channel between the sessiond and the consumer. This
41 * is only read and updated with the session_list lock held.
43 static uint64_t next_kernel_channel_key
;
45 #include <lttng/userspace-probe.h>
46 #include <lttng/userspace-probe-internal.h>
48 * Add context on a kernel channel.
50 * Assumes the ownership of ctx.
52 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
53 struct ltt_kernel_context
*ctx
)
60 DBG("Adding context to channel %s", chan
->channel
->name
);
61 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
65 /* Exists but not available for this kernel */
66 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
69 /* If EEXIST, we just ignore the error */
73 PERROR("add context ioctl");
74 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
81 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
86 trace_kernel_destroy_context(ctx
);
92 * Create a new kernel session, register it to the kernel tracer and add it to
93 * the session daemon session.
95 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
98 struct ltt_kernel_session
*lks
;
102 /* Allocate data structure */
103 lks
= trace_kernel_create_session();
109 /* Kernel tracer session creation */
110 ret
= kernctl_create_session(tracer_fd
);
112 PERROR("ioctl kernel create session");
117 /* Prevent fd duplication after execlp() */
118 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
120 PERROR("fcntl session fd");
123 lks
->id
= session
->id
;
124 lks
->consumer_fds_sent
= 0;
125 session
->kernel_session
= lks
;
127 DBG("Kernel session created (fd: %d)", lks
->fd
);
133 trace_kernel_destroy_session(lks
);
134 trace_kernel_free_session(lks
);
140 * Create a kernel channel, register it to the kernel tracer and add it to the
143 int kernel_create_channel(struct ltt_kernel_session
*session
,
144 struct lttng_channel
*chan
)
147 struct ltt_kernel_channel
*lkc
;
152 /* Allocate kernel channel */
153 lkc
= trace_kernel_create_channel(chan
);
158 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
159 chan
->name
, lkc
->channel
->attr
.overwrite
,
160 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
161 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
162 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
164 /* Kernel tracer channel creation */
165 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
167 PERROR("ioctl kernel create channel");
171 /* Setup the channel fd */
173 /* Prevent fd duplication after execlp() */
174 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
176 PERROR("fcntl session fd");
179 /* Add channel to session */
180 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
181 session
->channel_count
++;
182 lkc
->session
= session
;
183 lkc
->key
= ++next_kernel_channel_key
;
185 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
186 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
199 * Compute the offset of the instrumentation byte in the binary based on the
200 * function probe location using the ELF lookup method.
202 * Returns 0 on success and set the offset out parameter to the offset of the
204 * Returns -1 on error
207 int extract_userspace_probe_offset_function_elf(
208 const struct lttng_userspace_probe_location
*probe_location
,
209 struct ltt_kernel_session
*session
, uint64_t *offset
)
213 const char *symbol
= NULL
;
214 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
215 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
217 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
218 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
220 lookup
= lttng_userspace_probe_location_get_lookup_method(
228 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
230 assert(lookup_method_type
==
231 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
233 symbol
= lttng_userspace_probe_location_function_get_function_name(
240 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
246 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
247 session
->gid
, offset
);
249 DBG("userspace probe offset calculation failed for "
250 "function %s", symbol
);
254 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
260 * Compute the offsets of the instrumentation bytes in the binary based on the
261 * tracepoint probe location using the SDT lookup method. This function
262 * allocates the offsets buffer, the caller must free it.
264 * Returns 0 on success and set the offset out parameter to the offsets of the
266 * Returns -1 on error.
269 int extract_userspace_probe_offset_tracepoint_sdt(
270 const struct lttng_userspace_probe_location
*probe_location
,
271 struct ltt_kernel_session
*session
, uint64_t **offsets
,
272 uint32_t *offsets_count
)
274 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
275 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
276 const char *probe_name
= NULL
, *provider_name
= NULL
;
280 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
281 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
283 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
290 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
292 assert(lookup_method_type
==
293 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
296 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
303 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
305 if (!provider_name
) {
310 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
316 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
317 session
->uid
, session
->gid
, offsets
, offsets_count
);
319 DBG("userspace probe offset calculation failed for sdt "
320 "probe %s:%s", provider_name
, probe_name
);
324 if (*offsets_count
== 0) {
325 DBG("no userspace probe offset found");
329 DBG("%u userspace probe SDT offsets found for %s:%s at:",
330 *offsets_count
, provider_name
, probe_name
);
331 for (i
= 0; i
< *offsets_count
; i
++) {
332 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
339 * Extract the offsets of the instrumentation point for the different lookup
343 int userspace_probe_add_callsites(struct lttng_event
*ev
,
344 struct ltt_kernel_session
*session
, int fd
)
346 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
347 enum lttng_userspace_probe_location_lookup_method_type type
;
348 const struct lttng_userspace_probe_location
*location
= NULL
;
352 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
354 location
= lttng_event_get_userspace_probe_location(ev
);
360 lttng_userspace_probe_location_get_lookup_method(location
);
361 if (!lookup_method
) {
366 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
368 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
370 struct lttng_kernel_event_callsite callsite
;
373 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
375 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
379 callsite
.u
.uprobe
.offset
= offset
;
380 ret
= kernctl_add_callsite(fd
, &callsite
);
382 WARN("Adding callsite to userspace probe "
383 "event %s failed.", ev
->name
);
384 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
389 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
392 uint64_t *offsets
= NULL
;
393 uint32_t offsets_count
;
394 struct lttng_kernel_event_callsite callsite
;
397 * This call allocates the offsets buffer. This buffer must be freed
400 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
401 &offsets
, &offsets_count
);
403 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
406 for (i
= 0; i
< offsets_count
; i
++) {
407 callsite
.u
.uprobe
.offset
= offsets
[i
];
408 ret
= kernctl_add_callsite(fd
, &callsite
);
410 WARN("Adding callsite to userspace probe "
411 "event %s failed.", ev
->name
);
412 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
421 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
429 * Create a kernel event, enable it to the kernel tracer and add it to the
430 * channel event list of the kernel session.
431 * We own filter_expression and filter.
433 int kernel_create_event(struct lttng_event
*ev
,
434 struct ltt_kernel_channel
*channel
,
435 char *filter_expression
,
436 struct lttng_filter_bytecode
*filter
)
439 enum lttng_error_code ret
;
440 struct ltt_kernel_event
*event
;
445 /* We pass ownership of filter_expression and filter */
446 ret
= trace_kernel_create_event(ev
, filter_expression
,
448 if (ret
!= LTTNG_OK
) {
452 fd
= kernctl_create_event(channel
->fd
, event
->event
);
456 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
459 WARN("Event type not implemented");
460 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
463 WARN("Event %s not found!", ev
->name
);
464 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
467 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
468 PERROR("create event ioctl");
473 event
->type
= ev
->type
;
475 /* Prevent fd duplication after execlp() */
476 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
478 PERROR("fcntl session fd");
482 err
= kernctl_filter(event
->fd
, filter
);
486 ret
= LTTNG_ERR_FILTER_NOMEM
;
489 ret
= LTTNG_ERR_FILTER_INVAL
;
496 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
497 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
499 goto add_callsite_error
;
503 err
= kernctl_enable(event
->fd
);
507 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
510 PERROR("enable kernel event");
511 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
517 /* Add event to event list */
518 cds_list_add(&event
->list
, &channel
->events_list
.head
);
519 channel
->event_count
++;
521 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
531 closeret
= close(event
->fd
);
533 PERROR("close event fd");
543 * Disable a kernel channel.
545 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
551 ret
= kernctl_disable(chan
->fd
);
553 PERROR("disable chan ioctl");
558 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
559 chan
->channel
->name
, chan
->fd
, chan
->key
);
568 * Enable a kernel channel.
570 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
576 ret
= kernctl_enable(chan
->fd
);
577 if (ret
< 0 && ret
!= -EEXIST
) {
578 PERROR("Enable kernel chan");
583 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
584 chan
->channel
->name
, chan
->fd
, chan
->key
);
593 * Enable a kernel event.
595 int kernel_enable_event(struct ltt_kernel_event
*event
)
601 ret
= kernctl_enable(event
->fd
);
605 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
608 PERROR("enable kernel event");
615 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
624 * Disable a kernel event.
626 int kernel_disable_event(struct ltt_kernel_event
*event
)
632 ret
= kernctl_disable(event
->fd
);
636 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
639 PERROR("disable kernel event");
646 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
655 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
659 DBG("Kernel track PID %d for session id %" PRIu64
".",
661 ret
= kernctl_track_pid(session
->fd
, pid
);
667 return LTTNG_ERR_INVALID
;
669 return LTTNG_ERR_NOMEM
;
671 return LTTNG_ERR_PID_TRACKED
;
673 return LTTNG_ERR_UNK
;
677 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
681 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
683 ret
= kernctl_untrack_pid(session
->fd
, pid
);
689 return LTTNG_ERR_INVALID
;
691 return LTTNG_ERR_NOMEM
;
693 return LTTNG_ERR_PID_NOT_TRACKED
;
695 return LTTNG_ERR_UNK
;
699 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
704 ssize_t nbmem
, count
= 0;
708 fd
= kernctl_list_tracker_pids(session
->fd
);
710 PERROR("kernel tracker pids list");
714 fp
= fdopen(fd
, "r");
716 PERROR("kernel tracker pids list fdopen");
720 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
721 pids
= zmalloc(sizeof(*pids
) * nbmem
);
723 PERROR("alloc list pids");
728 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
729 if (count
>= nbmem
) {
733 new_nbmem
= nbmem
<< 1;
734 DBG("Reallocating pids list from %zu to %zu entries",
736 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
737 if (new_pids
== NULL
) {
738 PERROR("realloc list events");
743 /* Zero the new memory */
744 memset(new_pids
+ nbmem
, 0,
745 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
753 DBG("Kernel list tracker pids done (%zd pids)", count
);
755 ret
= fclose(fp
); /* closes both fp and fd */
771 * Create kernel metadata, open from the kernel tracer and add it to the
774 int kernel_open_metadata(struct ltt_kernel_session
*session
)
777 struct ltt_kernel_metadata
*lkm
= NULL
;
781 /* Allocate kernel metadata */
782 lkm
= trace_kernel_create_metadata();
787 /* Kernel tracer metadata creation */
788 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
794 lkm
->key
= ++next_kernel_channel_key
;
795 /* Prevent fd duplication after execlp() */
796 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
798 PERROR("fcntl session fd");
801 session
->metadata
= lkm
;
803 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
808 trace_kernel_destroy_metadata(lkm
);
814 * Start tracing session.
816 int kernel_start_session(struct ltt_kernel_session
*session
)
822 ret
= kernctl_start_session(session
->fd
);
824 PERROR("ioctl start session");
828 DBG("Kernel session started");
837 * Make a kernel wait to make sure in-flight probe have completed.
839 void kernel_wait_quiescent(int fd
)
843 DBG("Kernel quiescent wait on %d", fd
);
845 ret
= kernctl_wait_quiescent(fd
);
847 PERROR("wait quiescent ioctl");
848 ERR("Kernel quiescent wait failed");
853 * Force flush buffer of metadata.
855 int kernel_metadata_flush_buffer(int fd
)
859 DBG("Kernel flushing metadata buffer on fd %d", fd
);
861 ret
= kernctl_buffer_flush(fd
);
863 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
870 * Force flush buffer for channel.
872 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
875 struct ltt_kernel_stream
*stream
;
879 DBG("Flush buffer for channel %s", channel
->channel
->name
);
881 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
882 DBG("Flushing channel stream %d", stream
->fd
);
883 ret
= kernctl_buffer_flush(stream
->fd
);
886 ERR("Fail to flush buffer for stream %d (ret: %d)",
895 * Stop tracing session.
897 int kernel_stop_session(struct ltt_kernel_session
*session
)
903 ret
= kernctl_stop_session(session
->fd
);
908 DBG("Kernel session stopped");
917 * Open stream of channel, register it to the kernel tracer and add it
918 * to the stream list of the channel.
920 * Note: given that the streams may appear in random order wrt CPU
921 * number (e.g. cpu hotplug), the index value of the stream number in
922 * the stream name is not necessarily linked to the CPU number.
924 * Return the number of created stream. Else, a negative value.
926 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
929 struct ltt_kernel_stream
*lks
;
933 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
934 lks
= trace_kernel_create_stream(channel
->channel
->name
,
935 channel
->stream_count
);
945 /* Prevent fd duplication after execlp() */
946 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
948 PERROR("fcntl session fd");
951 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
952 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
954 /* Add stream to channel stream list */
955 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
956 channel
->stream_count
++;
958 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
962 return channel
->stream_count
;
969 * Open the metadata stream and set it to the kernel session.
971 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
977 ret
= kernctl_create_stream(session
->metadata
->fd
);
979 PERROR("kernel create metadata stream");
983 DBG("Kernel metadata stream created (fd: %d)", ret
);
984 session
->metadata_stream_fd
= ret
;
985 /* Prevent fd duplication after execlp() */
986 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
988 PERROR("fcntl session fd");
998 * Get the event list from the kernel tracer and return the number of elements.
1000 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
1004 size_t nbmem
, count
= 0;
1006 struct lttng_event
*elist
;
1010 fd
= kernctl_tracepoint_list(tracer_fd
);
1012 PERROR("kernel tracepoint list");
1016 fp
= fdopen(fd
, "r");
1018 PERROR("kernel tracepoint list fdopen");
1023 * Init memory size counter
1024 * See kernel-ctl.h for explanation of this value
1026 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1027 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1028 if (elist
== NULL
) {
1029 PERROR("alloc list events");
1034 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1035 if (count
>= nbmem
) {
1036 struct lttng_event
*new_elist
;
1039 new_nbmem
= nbmem
<< 1;
1040 DBG("Reallocating event list from %zu to %zu bytes",
1042 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1043 if (new_elist
== NULL
) {
1044 PERROR("realloc list events");
1050 /* Zero the new memory */
1051 memset(new_elist
+ nbmem
, 0,
1052 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1056 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1057 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1058 elist
[count
].enabled
= -1;
1064 DBG("Kernel list events done (%zu events)", count
);
1066 ret
= fclose(fp
); /* closes both fp and fd */
1082 * Get kernel version and validate it.
1084 int kernel_validate_version(int tracer_fd
,
1085 struct lttng_kernel_tracer_version
*version
,
1086 struct lttng_kernel_tracer_abi_version
*abi_version
)
1090 ret
= kernctl_tracer_version(tracer_fd
, version
);
1092 ERR("Failed to retrieve the lttng-modules version");
1096 /* Validate version */
1097 if (version
->major
!= VERSION_MAJOR
) {
1098 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1099 version
->major
, VERSION_MAJOR
);
1102 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
1104 ERR("Failed to retrieve lttng-modules ABI version");
1107 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1108 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1109 abi_version
->major
, abi_version
->minor
,
1110 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1113 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1114 version
->major
, version
->minor
,
1115 abi_version
->major
, abi_version
->minor
);
1122 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1127 * Kernel work-arounds called at the start of sessiond main().
1129 int init_kernel_workarounds(void)
1135 * boot_id needs to be read once before being used concurrently
1136 * to deal with a Linux kernel race. A fix is proposed for
1137 * upstream, but the work-around is needed for older kernels.
1139 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1146 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1148 /* Ignore error, we don't really care */
1160 * Teardown of a kernel session, keeping data required by destroy notifiers.
1162 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1164 struct lttng_trace_chunk
*trace_chunk
;
1166 if (ksess
== NULL
) {
1167 DBG3("No kernel session when tearing down session");
1171 DBG("Tearing down kernel session");
1172 trace_chunk
= ksess
->current_trace_chunk
;
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
);
1206 lttng_trace_chunk_put(trace_chunk
);
1209 /* Teardown of data required by destroy notifiers. */
1210 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1212 if (ksess
== NULL
) {
1215 trace_kernel_free_session(ksess
);
1219 * Destroy a kernel channel object. It does not do anything on the tracer side.
1221 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1223 struct ltt_kernel_session
*ksess
= NULL
;
1226 assert(kchan
->channel
);
1228 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1230 /* Update channel count of associated session. */
1231 if (kchan
->session
) {
1232 /* Keep pointer reference so we can update it after the destroy. */
1233 ksess
= kchan
->session
;
1236 trace_kernel_destroy_channel(kchan
);
1239 * At this point the kernel channel is not visible anymore. This is safe
1240 * since in order to work on a visible kernel session, the tracing session
1241 * lock (ltt_session.lock) MUST be acquired.
1244 ksess
->channel_count
--;
1249 * Take a snapshot for a given kernel session.
1251 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1253 enum lttng_error_code
kernel_snapshot_record(
1254 struct ltt_kernel_session
*ksess
,
1255 const struct consumer_output
*output
, int wait
,
1256 uint64_t nb_packets_per_stream
)
1258 int err
, ret
, saved_metadata_fd
;
1259 enum lttng_error_code status
= LTTNG_OK
;
1260 struct consumer_socket
*socket
;
1261 struct lttng_ht_iter iter
;
1262 struct ltt_kernel_metadata
*saved_metadata
;
1265 assert(ksess
->consumer
);
1268 DBG("Kernel snapshot record started");
1270 /* Save current metadata since the following calls will change it. */
1271 saved_metadata
= ksess
->metadata
;
1272 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1276 ret
= kernel_open_metadata(ksess
);
1278 status
= LTTNG_ERR_KERN_META_FAIL
;
1282 ret
= kernel_open_metadata_stream(ksess
);
1284 status
= LTTNG_ERR_KERN_META_FAIL
;
1285 goto error_open_stream
;
1288 /* Send metadata to consumer and snapshot everything. */
1289 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1290 socket
, node
.node
) {
1291 struct ltt_kernel_channel
*chan
;
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
);
1298 status
= LTTNG_ERR_KERN_META_FAIL
;
1299 goto error_consumer
;
1302 /* For each channel, ask the consumer to snapshot it. */
1303 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1304 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1305 ksess
->uid
, ksess
->gid
,
1306 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1307 nb_packets_per_stream
);
1308 if (status
!= LTTNG_OK
) {
1309 (void) kernel_consumer_destroy_metadata(socket
,
1311 goto error_consumer
;
1315 /* Snapshot metadata, */
1316 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1317 1, ksess
->uid
, ksess
->gid
,
1318 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1319 if (status
!= LTTNG_OK
) {
1320 goto error_consumer
;
1324 * The metadata snapshot is done, ask the consumer to destroy it since
1325 * it's not monitored on the consumer side.
1327 (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
;
1348 * Get the syscall mask array from the kernel tracer.
1350 * Return 0 on success else a negative value. In both case, syscall_mask should
1353 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1355 assert(syscall_mask
);
1358 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1362 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1365 * Return 1 on success, 0 when feature is not supported, negative value in case
1368 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1370 int ret
= 0; // Not supported by default
1371 struct lttng_kernel_tracer_abi_version abi
;
1373 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1375 ERR("Failed to retrieve lttng-modules ABI version");
1380 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1382 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1394 * Rotate a kernel session.
1396 * Return LTTNG_OK on success or else an LTTng error code.
1398 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1401 enum lttng_error_code status
= LTTNG_OK
;
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
;
1422 /* For each channel, ask the consumer to rotate it. */
1423 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1424 DBG("Rotate kernel channel %" PRIu64
", session %s",
1425 chan
->key
, session
->name
);
1426 ret
= consumer_rotate_channel(socket
, chan
->key
,
1427 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1428 /* is_metadata_channel */ false);
1430 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1436 * Rotate the metadata channel.
1438 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1439 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1440 /* is_metadata_channel */ true);
1442 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1452 enum lttng_error_code
kernel_create_channel_subdirectories(
1453 const struct ltt_kernel_session
*ksess
)
1455 enum lttng_error_code ret
= LTTNG_OK
;
1456 enum lttng_trace_chunk_status chunk_status
;
1459 assert(ksess
->current_trace_chunk
);
1462 * Create the index subdirectory which will take care
1463 * of implicitly creating the channel's path.
1465 chunk_status
= lttng_trace_chunk_create_subdirectory(
1466 ksess
->current_trace_chunk
,
1467 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1468 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1469 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;