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"
38 * Add context on a kernel channel.
40 * Assumes the ownership of ctx.
42 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
43 struct ltt_kernel_context
*ctx
)
50 DBG("Adding context to channel %s", chan
->channel
->name
);
51 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
55 /* Exists but not available for this kernel */
56 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
59 /* If EEXIST, we just ignore the error */
63 PERROR("add context ioctl");
64 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
71 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
76 trace_kernel_destroy_context(ctx
);
82 * Create a new kernel session, register it to the kernel tracer and add it to
83 * the session daemon session.
85 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
88 struct ltt_kernel_session
*lks
;
92 /* Allocate data structure */
93 lks
= trace_kernel_create_session();
99 /* Kernel tracer session creation */
100 ret
= kernctl_create_session(tracer_fd
);
102 PERROR("ioctl kernel create session");
107 /* Prevent fd duplication after execlp() */
108 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
110 PERROR("fcntl session fd");
113 lks
->id
= session
->id
;
114 lks
->consumer_fds_sent
= 0;
115 session
->kernel_session
= lks
;
117 DBG("Kernel session created (fd: %d)", lks
->fd
);
123 trace_kernel_destroy_session(lks
);
129 * Create a kernel channel, register it to the kernel tracer and add it to the
132 int kernel_create_channel(struct ltt_kernel_session
*session
,
133 struct lttng_channel
*chan
)
136 struct ltt_kernel_channel
*lkc
;
141 /* Allocate kernel channel */
142 lkc
= trace_kernel_create_channel(chan
);
147 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
148 chan
->name
, lkc
->channel
->attr
.overwrite
,
149 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
150 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
151 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
153 /* Kernel tracer channel creation */
154 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
156 PERROR("ioctl kernel create channel");
160 /* Setup the channel fd */
162 /* Prevent fd duplication after execlp() */
163 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
165 PERROR("fcntl session fd");
168 /* Add channel to session */
169 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
170 session
->channel_count
++;
171 lkc
->session
= session
;
173 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
186 * Create a kernel event, enable it to the kernel tracer and add it to the
187 * channel event list of the kernel session.
188 * We own filter_expression and filter.
190 int kernel_create_event(struct lttng_event
*ev
,
191 struct ltt_kernel_channel
*channel
,
192 char *filter_expression
,
193 struct lttng_filter_bytecode
*filter
)
196 struct ltt_kernel_event
*event
;
201 /* We pass ownership of filter_expression and filter */
202 event
= trace_kernel_create_event(ev
, filter_expression
,
209 ret
= kernctl_create_event(channel
->fd
, event
->event
);
215 WARN("Event type not implemented");
218 WARN("Event %s not found!", ev
->name
);
221 PERROR("create event ioctl");
226 event
->type
= ev
->type
;
228 /* Prevent fd duplication after execlp() */
229 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
231 PERROR("fcntl session fd");
235 ret
= kernctl_filter(event
->fd
, filter
);
241 ret
= kernctl_enable(event
->fd
);
245 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
248 PERROR("enable kernel event");
254 /* Add event to event list */
255 cds_list_add(&event
->list
, &channel
->events_list
.head
);
256 channel
->event_count
++;
258 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
267 closeret
= close(event
->fd
);
269 PERROR("close event fd");
279 * Disable a kernel channel.
281 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
287 ret
= kernctl_disable(chan
->fd
);
289 PERROR("disable chan ioctl");
294 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
303 * Enable a kernel channel.
305 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
311 ret
= kernctl_enable(chan
->fd
);
312 if (ret
< 0 && ret
!= -EEXIST
) {
313 PERROR("Enable kernel chan");
318 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
327 * Enable a kernel event.
329 int kernel_enable_event(struct ltt_kernel_event
*event
)
335 ret
= kernctl_enable(event
->fd
);
339 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
342 PERROR("enable kernel event");
349 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
358 * Disable a kernel event.
360 int kernel_disable_event(struct ltt_kernel_event
*event
)
366 ret
= kernctl_disable(event
->fd
);
370 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
373 PERROR("disable kernel event");
380 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
389 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
393 DBG("Kernel track PID %d for session id %" PRIu64
".",
395 ret
= kernctl_track_pid(session
->fd
, pid
);
401 return LTTNG_ERR_INVALID
;
403 return LTTNG_ERR_NOMEM
;
405 return LTTNG_ERR_PID_TRACKED
;
407 return LTTNG_ERR_UNK
;
411 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
415 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
417 ret
= kernctl_untrack_pid(session
->fd
, pid
);
423 return LTTNG_ERR_INVALID
;
425 return LTTNG_ERR_NOMEM
;
427 return LTTNG_ERR_PID_NOT_TRACKED
;
429 return LTTNG_ERR_UNK
;
433 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
438 ssize_t nbmem
, count
= 0;
442 fd
= kernctl_list_tracker_pids(session
->fd
);
444 PERROR("kernel tracker pids list");
448 fp
= fdopen(fd
, "r");
450 PERROR("kernel tracker pids list fdopen");
454 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
455 pids
= zmalloc(sizeof(*pids
) * nbmem
);
457 PERROR("alloc list pids");
462 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
463 if (count
>= nbmem
) {
467 new_nbmem
= nbmem
<< 1;
468 DBG("Reallocating pids list from %zu to %zu entries",
470 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
471 if (new_pids
== NULL
) {
472 PERROR("realloc list events");
477 /* Zero the new memory */
478 memset(new_pids
+ nbmem
, 0,
479 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
487 DBG("Kernel list tracker pids done (%zd pids)", count
);
489 ret
= fclose(fp
); /* closes both fp and fd */
505 * Create kernel metadata, open from the kernel tracer and add it to the
508 int kernel_open_metadata(struct ltt_kernel_session
*session
)
511 struct ltt_kernel_metadata
*lkm
= NULL
;
515 /* Allocate kernel metadata */
516 lkm
= trace_kernel_create_metadata();
521 /* Kernel tracer metadata creation */
522 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
528 /* Prevent fd duplication after execlp() */
529 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
531 PERROR("fcntl session fd");
534 session
->metadata
= lkm
;
536 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
541 trace_kernel_destroy_metadata(lkm
);
547 * Start tracing session.
549 int kernel_start_session(struct ltt_kernel_session
*session
)
555 ret
= kernctl_start_session(session
->fd
);
557 PERROR("ioctl start session");
561 DBG("Kernel session started");
570 * Make a kernel wait to make sure in-flight probe have completed.
572 void kernel_wait_quiescent(int fd
)
576 DBG("Kernel quiescent wait on %d", fd
);
578 ret
= kernctl_wait_quiescent(fd
);
580 PERROR("wait quiescent ioctl");
581 ERR("Kernel quiescent wait failed");
586 * Force flush buffer of metadata.
588 int kernel_metadata_flush_buffer(int fd
)
592 DBG("Kernel flushing metadata buffer on fd %d", fd
);
594 ret
= kernctl_buffer_flush(fd
);
596 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
603 * Force flush buffer for channel.
605 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
608 struct ltt_kernel_stream
*stream
;
612 DBG("Flush buffer for channel %s", channel
->channel
->name
);
614 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
615 DBG("Flushing channel stream %d", stream
->fd
);
616 ret
= kernctl_buffer_flush(stream
->fd
);
619 ERR("Fail to flush buffer for stream %d (ret: %d)",
628 * Stop tracing session.
630 int kernel_stop_session(struct ltt_kernel_session
*session
)
636 ret
= kernctl_stop_session(session
->fd
);
641 DBG("Kernel session stopped");
650 * Open stream of channel, register it to the kernel tracer and add it
651 * to the stream list of the channel.
653 * Note: given that the streams may appear in random order wrt CPU
654 * number (e.g. cpu hotplug), the index value of the stream number in
655 * the stream name is not necessarily linked to the CPU number.
657 * Return the number of created stream. Else, a negative value.
659 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
662 struct ltt_kernel_stream
*lks
;
666 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
667 lks
= trace_kernel_create_stream(channel
->channel
->name
,
668 channel
->stream_count
);
678 /* Prevent fd duplication after execlp() */
679 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
681 PERROR("fcntl session fd");
684 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
685 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
687 /* Add stream to channel stream list */
688 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
689 channel
->stream_count
++;
691 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
695 return channel
->stream_count
;
702 * Open the metadata stream and set it to the kernel session.
704 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
710 ret
= kernctl_create_stream(session
->metadata
->fd
);
712 PERROR("kernel create metadata stream");
716 DBG("Kernel metadata stream created (fd: %d)", ret
);
717 session
->metadata_stream_fd
= ret
;
718 /* Prevent fd duplication after execlp() */
719 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
721 PERROR("fcntl session fd");
731 * Get the event list from the kernel tracer and return the number of elements.
733 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
737 size_t nbmem
, count
= 0;
739 struct lttng_event
*elist
;
743 fd
= kernctl_tracepoint_list(tracer_fd
);
745 PERROR("kernel tracepoint list");
749 fp
= fdopen(fd
, "r");
751 PERROR("kernel tracepoint list fdopen");
756 * Init memory size counter
757 * See kernel-ctl.h for explanation of this value
759 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
760 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
762 PERROR("alloc list events");
767 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
768 if (count
>= nbmem
) {
769 struct lttng_event
*new_elist
;
772 new_nbmem
= nbmem
<< 1;
773 DBG("Reallocating event list from %zu to %zu bytes",
775 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
776 if (new_elist
== NULL
) {
777 PERROR("realloc list events");
783 /* Zero the new memory */
784 memset(new_elist
+ nbmem
, 0,
785 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
789 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
790 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
791 elist
[count
].enabled
= -1;
797 DBG("Kernel list events done (%zu events)", count
);
799 ret
= fclose(fp
); /* closes both fp and fd */
815 * Get kernel version and validate it.
817 int kernel_validate_version(int tracer_fd
)
820 struct lttng_kernel_tracer_version version
;
821 struct lttng_kernel_tracer_abi_version abi_version
;
823 ret
= kernctl_tracer_version(tracer_fd
, &version
);
825 ERR("Failed to retrieve the lttng-modules version");
829 /* Validate version */
830 if (version
.major
!= VERSION_MAJOR
) {
831 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
832 version
.major
, VERSION_MAJOR
);
835 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
837 ERR("Failed to retrieve lttng-modules ABI version");
840 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
841 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
842 abi_version
.major
, abi_version
.minor
,
843 LTTNG_MODULES_ABI_MAJOR_VERSION
);
846 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
847 version
.major
, version
.minor
,
848 abi_version
.major
, abi_version
.minor
);
855 ERR("Kernel tracer version check failed; kernel tracing will not be available");
860 * Kernel work-arounds called at the start of sessiond main().
862 int init_kernel_workarounds(void)
868 * boot_id needs to be read once before being used concurrently
869 * to deal with a Linux kernel race. A fix is proposed for
870 * upstream, but the work-around is needed for older kernels.
872 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
879 ret
= fread(buf
, 1, sizeof(buf
), fp
);
881 /* Ignore error, we don't really care */
893 * Complete teardown of a kernel session.
895 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
898 DBG3("No kernel session when tearing down session");
902 DBG("Tearing down kernel session");
905 * Destroy channels on the consumer if at least one FD has been sent and we
906 * are in no output mode because the streams are in *no* monitor mode so we
907 * have to send a command to clean them up or else they leaked.
909 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
911 struct consumer_socket
*socket
;
912 struct lttng_ht_iter iter
;
914 /* For each consumer socket. */
916 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
918 struct ltt_kernel_channel
*chan
;
920 /* For each channel, ask the consumer to destroy it. */
921 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
922 ret
= kernel_consumer_destroy_channel(socket
, chan
);
924 /* Consumer is probably dead. Use next socket. */
932 /* Close any relayd session */
933 consumer_output_send_destroy_relayd(ksess
->consumer
);
935 trace_kernel_destroy_session(ksess
);
939 * Destroy a kernel channel object. It does not do anything on the tracer side.
941 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
943 struct ltt_kernel_session
*ksess
= NULL
;
946 assert(kchan
->channel
);
948 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
950 /* Update channel count of associated session. */
951 if (kchan
->session
) {
952 /* Keep pointer reference so we can update it after the destroy. */
953 ksess
= kchan
->session
;
956 trace_kernel_destroy_channel(kchan
);
959 * At this point the kernel channel is not visible anymore. This is safe
960 * since in order to work on a visible kernel session, the tracing session
961 * lock (ltt_session.lock) MUST be acquired.
964 ksess
->channel_count
--;
969 * Take a snapshot for a given kernel session.
971 * Return 0 on success or else return a LTTNG_ERR code.
973 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
974 struct snapshot_output
*output
, int wait
,
975 uint64_t nb_packets_per_stream
)
977 int err
, ret
, saved_metadata_fd
;
978 struct consumer_socket
*socket
;
979 struct lttng_ht_iter iter
;
980 struct ltt_kernel_metadata
*saved_metadata
;
983 assert(ksess
->consumer
);
986 DBG("Kernel snapshot record started");
988 /* Save current metadata since the following calls will change it. */
989 saved_metadata
= ksess
->metadata
;
990 saved_metadata_fd
= ksess
->metadata_stream_fd
;
994 ret
= kernel_open_metadata(ksess
);
996 ret
= LTTNG_ERR_KERN_META_FAIL
;
1000 ret
= kernel_open_metadata_stream(ksess
);
1002 ret
= LTTNG_ERR_KERN_META_FAIL
;
1003 goto error_open_stream
;
1006 /* Send metadata to consumer and snapshot everything. */
1007 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1008 socket
, node
.node
) {
1009 struct consumer_output
*saved_output
;
1010 struct ltt_kernel_channel
*chan
;
1013 * Temporarly switch consumer output for our snapshot output. As long
1014 * as the session lock is taken, this is safe.
1016 saved_output
= ksess
->consumer
;
1017 ksess
->consumer
= output
->consumer
;
1019 pthread_mutex_lock(socket
->lock
);
1020 /* This stream must not be monitored by the consumer. */
1021 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1022 pthread_mutex_unlock(socket
->lock
);
1023 /* Put back the saved consumer output into the session. */
1024 ksess
->consumer
= saved_output
;
1026 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1027 goto error_consumer
;
1030 /* For each channel, ask the consumer to snapshot it. */
1031 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1032 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1033 ksess
->uid
, ksess
->gid
,
1034 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1035 nb_packets_per_stream
);
1037 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1038 (void) kernel_consumer_destroy_metadata(socket
,
1040 goto error_consumer
;
1044 /* Snapshot metadata, */
1045 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1046 1, ksess
->uid
, ksess
->gid
,
1047 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1049 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1050 goto error_consumer
;
1054 * The metadata snapshot is done, ask the consumer to destroy it since
1055 * it's not monitored on the consumer side.
1057 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1063 /* Close newly opened metadata stream. It's now on the consumer side. */
1064 err
= close(ksess
->metadata_stream_fd
);
1066 PERROR("close snapshot kernel");
1070 trace_kernel_destroy_metadata(ksess
->metadata
);
1072 /* Restore metadata state.*/
1073 ksess
->metadata
= saved_metadata
;
1074 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1081 * Get the syscall mask array from the kernel tracer.
1083 * Return 0 on success else a negative value. In both case, syscall_mask should
1086 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1088 assert(syscall_mask
);
1091 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1095 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1098 * Return 1 on success, 0 when feature is not supported, negative value in case
1101 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1103 int ret
= 0; // Not supported by default
1104 struct lttng_kernel_tracer_abi_version abi
;
1106 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1108 ERR("Failed to retrieve lttng-modules ABI version");
1113 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1115 if (abi
.major
>= 2 && abi
.minor
>= 3) {