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.
27 #include <common/common.h>
28 #include <common/kernel-ctl/kernel-ctl.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
33 #include "kernel-consumer.h"
34 #include "kern-modules.h"
37 * Add context on a kernel channel.
39 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
40 struct lttng_kernel_context
*ctx
)
47 DBG("Adding context to channel %s", chan
->channel
->name
);
48 ret
= kernctl_add_context(chan
->fd
, ctx
);
50 if (errno
!= EEXIST
) {
51 PERROR("add context ioctl");
53 /* If EEXIST, we just ignore the error */
59 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
60 if (chan
->ctx
== NULL
) {
61 PERROR("zmalloc event context");
65 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
74 * Create a new kernel session, register it to the kernel tracer and add it to
75 * the session daemon session.
77 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
80 struct ltt_kernel_session
*lks
;
84 /* Allocate data structure */
85 lks
= trace_kernel_create_session();
91 /* Kernel tracer session creation */
92 ret
= kernctl_create_session(tracer_fd
);
94 PERROR("ioctl kernel create session");
99 /* Prevent fd duplication after execlp() */
100 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
102 PERROR("fcntl session fd");
105 lks
->id
= session
->id
;
106 lks
->consumer_fds_sent
= 0;
107 session
->kernel_session
= lks
;
109 DBG("Kernel session created (fd: %d)", lks
->fd
);
115 trace_kernel_destroy_session(lks
);
121 * Create a kernel channel, register it to the kernel tracer and add it to the
124 int kernel_create_channel(struct ltt_kernel_session
*session
,
125 struct lttng_channel
*chan
)
128 struct ltt_kernel_channel
*lkc
;
133 /* Allocate kernel channel */
134 lkc
= trace_kernel_create_channel(chan
);
139 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
140 chan
->name
, lkc
->channel
->attr
.overwrite
,
141 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
142 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
143 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
145 /* Kernel tracer channel creation */
146 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
148 PERROR("ioctl kernel create channel");
152 /* Setup the channel fd */
154 /* Prevent fd duplication after execlp() */
155 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
157 PERROR("fcntl session fd");
160 /* Add channel to session */
161 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
162 session
->channel_count
++;
163 lkc
->session
= session
;
165 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
178 * Create a kernel event, enable it to the kernel tracer and add it to the
179 * channel event list of the kernel session.
181 int kernel_create_event(struct lttng_event
*ev
,
182 struct ltt_kernel_channel
*channel
)
185 struct ltt_kernel_event
*event
;
190 event
= trace_kernel_create_event(ev
);
196 ret
= kernctl_create_event(channel
->fd
, event
->event
);
202 WARN("Event type not implemented");
205 PERROR("create event ioctl");
212 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
214 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
215 DBG2("Kernel event syscall creation success");
217 * We use fd == -1 to ensure that we never trigger a close of fd
225 /* Prevent fd duplication after execlp() */
226 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
228 PERROR("fcntl session fd");
232 /* Add event to event list */
233 cds_list_add(&event
->list
, &channel
->events_list
.head
);
234 channel
->event_count
++;
236 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
247 * Disable a kernel channel.
249 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
255 ret
= kernctl_disable(chan
->fd
);
257 PERROR("disable chan ioctl");
263 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
272 * Enable a kernel channel.
274 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
280 ret
= kernctl_enable(chan
->fd
);
281 if (ret
< 0 && errno
!= EEXIST
) {
282 PERROR("Enable kernel chan");
287 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
296 * Enable a kernel event.
298 int kernel_enable_event(struct ltt_kernel_event
*event
)
304 ret
= kernctl_enable(event
->fd
);
308 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
311 PERROR("enable kernel event");
318 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
327 * Disable a kernel event.
329 int kernel_disable_event(struct ltt_kernel_event
*event
)
335 ret
= kernctl_disable(event
->fd
);
339 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
342 PERROR("disable kernel event");
349 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
358 * Create kernel metadata, open from the kernel tracer and add it to the
361 int kernel_open_metadata(struct ltt_kernel_session
*session
)
364 struct ltt_kernel_metadata
*lkm
= NULL
;
368 /* Allocate kernel metadata */
369 lkm
= trace_kernel_create_metadata();
374 /* Kernel tracer metadata creation */
375 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
381 /* Prevent fd duplication after execlp() */
382 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
384 PERROR("fcntl session fd");
387 session
->metadata
= lkm
;
389 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
394 trace_kernel_destroy_metadata(lkm
);
400 * Start tracing session.
402 int kernel_start_session(struct ltt_kernel_session
*session
)
408 ret
= kernctl_start_session(session
->fd
);
410 PERROR("ioctl start session");
414 DBG("Kernel session started");
423 * Make a kernel wait to make sure in-flight probe have completed.
425 void kernel_wait_quiescent(int fd
)
429 DBG("Kernel quiescent wait on %d", fd
);
431 ret
= kernctl_wait_quiescent(fd
);
433 PERROR("wait quiescent ioctl");
434 ERR("Kernel quiescent wait failed");
441 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
447 ret
= kernctl_calibrate(fd
, calibrate
);
449 PERROR("calibrate ioctl");
458 * Force flush buffer of metadata.
460 int kernel_metadata_flush_buffer(int fd
)
464 DBG("Kernel flushing metadata buffer on fd %d", fd
);
466 ret
= kernctl_buffer_flush(fd
);
468 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
475 * Force flush buffer for channel.
477 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
480 struct ltt_kernel_stream
*stream
;
484 DBG("Flush buffer for channel %s", channel
->channel
->name
);
486 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
487 DBG("Flushing channel stream %d", stream
->fd
);
488 ret
= kernctl_buffer_flush(stream
->fd
);
491 ERR("Fail to flush buffer for stream %d (ret: %d)",
500 * Stop tracing session.
502 int kernel_stop_session(struct ltt_kernel_session
*session
)
508 ret
= kernctl_stop_session(session
->fd
);
513 DBG("Kernel session stopped");
522 * Open stream of channel, register it to the kernel tracer and add it
523 * to the stream list of the channel.
525 * Return the number of created stream. Else, a negative value.
527 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
530 struct ltt_kernel_stream
*lks
;
534 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
535 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
545 /* Prevent fd duplication after execlp() */
546 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
548 PERROR("fcntl session fd");
551 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
552 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
554 /* Add stream to channe stream list */
555 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
556 channel
->stream_count
++;
558 /* Increment counter which represent CPU number. */
561 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
565 return channel
->stream_count
;
572 * Open the metadata stream and set it to the kernel session.
574 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
580 ret
= kernctl_create_stream(session
->metadata
->fd
);
582 PERROR("kernel create metadata stream");
586 DBG("Kernel metadata stream created (fd: %d)", ret
);
587 session
->metadata_stream_fd
= ret
;
588 /* Prevent fd duplication after execlp() */
589 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
591 PERROR("fcntl session fd");
601 * Get the event list from the kernel tracer and return the number of elements.
603 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
607 size_t nbmem
, count
= 0;
609 struct lttng_event
*elist
;
613 fd
= kernctl_tracepoint_list(tracer_fd
);
615 PERROR("kernel tracepoint list");
619 fp
= fdopen(fd
, "r");
621 PERROR("kernel tracepoint list fdopen");
626 * Init memory size counter
627 * See kernel-ctl.h for explanation of this value
629 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
630 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
632 PERROR("alloc list events");
637 while (fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
) == 1) {
638 if (count
>= nbmem
) {
639 struct lttng_event
*new_elist
;
641 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
643 /* Double the size */
645 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
646 if (new_elist
== NULL
) {
647 PERROR("realloc list events");
655 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
656 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
657 elist
[count
].enabled
= -1;
663 DBG("Kernel list events done (%zu events)", count
);
665 ret
= fclose(fp
); /* closes both fp and fd */
681 * Get kernel version and validate it.
683 int kernel_validate_version(int tracer_fd
)
686 struct lttng_kernel_tracer_version version
;
688 ret
= kernctl_tracer_version(tracer_fd
, &version
);
690 ERR("Failed at getting the lttng-modules version");
694 /* Validate version */
695 if (version
.major
!= KERN_MODULES_PRE_MAJOR
696 && version
.major
!= KERN_MODULES_MAJOR
) {
700 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
704 ERR("Kernel major version %d is not compatible (supporting <= %d)",
705 version
.major
, KERN_MODULES_MAJOR
)
713 * Kernel work-arounds called at the start of sessiond main().
715 int init_kernel_workarounds(void)
721 * boot_id needs to be read once before being used concurrently
722 * to deal with a Linux kernel race. A fix is proposed for
723 * upstream, but the work-around is needed for older kernels.
725 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
732 ret
= fread(buf
, 1, sizeof(buf
), fp
);
734 /* Ignore error, we don't really care */
746 * Complete teardown of a kernel session.
748 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
751 DBG3("No kernel session when tearing down session");
755 DBG("Tearing down kernel session");
758 * Destroy channels on the consumer if in no output mode because the
759 * streams are in *no* monitor mode so we have to send a command to clean
760 * them up or else they leaked.
762 if (!ksess
->output_traces
) {
764 struct consumer_socket
*socket
;
765 struct lttng_ht_iter iter
;
767 /* For each consumer socket. */
768 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
770 struct ltt_kernel_channel
*chan
;
772 /* For each channel, ask the consumer to destroy it. */
773 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
774 ret
= kernel_consumer_destroy_channel(socket
, chan
);
776 /* Consumer is probably dead. Use next socket. */
783 /* Close any relayd session */
784 consumer_output_send_destroy_relayd(ksess
->consumer
);
786 trace_kernel_destroy_session(ksess
);
790 * Destroy a kernel channel object. It does not do anything on the tracer side.
792 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
794 struct ltt_kernel_session
*ksess
= NULL
;
797 assert(kchan
->channel
);
799 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
801 /* Update channel count of associated session. */
802 if (kchan
->session
) {
803 /* Keep pointer reference so we can update it after the destroy. */
804 ksess
= kchan
->session
;
807 trace_kernel_destroy_channel(kchan
);
810 * At this point the kernel channel is not visible anymore. This is safe
811 * since in order to work on a visible kernel session, the tracing session
812 * lock (ltt_session.lock) MUST be acquired.
815 ksess
->channel_count
--;
820 * Take a snapshot for a given kernel session.
822 * Return 0 on success or else return a LTTNG_ERR code.
824 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
825 struct snapshot_output
*output
, int wait
, unsigned int nb_streams
)
827 int err
, ret
, saved_metadata_fd
;
828 struct consumer_socket
*socket
;
829 struct lttng_ht_iter iter
;
830 struct ltt_kernel_metadata
*saved_metadata
;
831 uint64_t max_size_per_stream
= 0;
834 assert(ksess
->consumer
);
837 DBG("Kernel snapshot record started");
839 /* Save current metadata since the following calls will change it. */
840 saved_metadata
= ksess
->metadata
;
841 saved_metadata_fd
= ksess
->metadata_stream_fd
;
845 ret
= kernel_open_metadata(ksess
);
847 ret
= LTTNG_ERR_KERN_META_FAIL
;
851 ret
= kernel_open_metadata_stream(ksess
);
853 ret
= LTTNG_ERR_KERN_META_FAIL
;
854 goto error_open_stream
;
857 if (output
->max_size
> 0 && nb_streams
> 0) {
858 max_size_per_stream
= output
->max_size
/ nb_streams
;
861 /* Send metadata to consumer and snapshot everything. */
862 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
864 struct consumer_output
*saved_output
;
865 struct ltt_kernel_channel
*chan
;
868 * Temporarly switch consumer output for our snapshot output. As long
869 * as the session lock is taken, this is safe.
871 saved_output
= ksess
->consumer
;
872 ksess
->consumer
= output
->consumer
;
874 pthread_mutex_lock(socket
->lock
);
875 /* This stream must not be monitored by the consumer. */
876 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
877 pthread_mutex_unlock(socket
->lock
);
878 /* Put back the saved consumer output into the session. */
879 ksess
->consumer
= saved_output
;
881 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
885 /* For each channel, ask the consumer to snapshot it. */
886 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
887 if (max_size_per_stream
&&
888 chan
->channel
->attr
.subbuf_size
> max_size_per_stream
) {
889 ret
= LTTNG_ERR_INVALID
;
890 DBG3("Kernel snapshot record maximum stream size %" PRIu64
891 " is smaller than subbuffer size of %" PRIu64
,
892 max_size_per_stream
, chan
->channel
->attr
.subbuf_size
);
893 (void) kernel_consumer_destroy_metadata(socket
,
898 pthread_mutex_lock(socket
->lock
);
899 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
900 ksess
->uid
, ksess
->gid
,
901 DEFAULT_KERNEL_TRACE_DIR
, wait
,
902 max_size_per_stream
);
903 pthread_mutex_unlock(socket
->lock
);
905 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
906 (void) kernel_consumer_destroy_metadata(socket
,
912 /* Snapshot metadata, */
913 pthread_mutex_lock(socket
->lock
);
914 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
915 1, ksess
->uid
, ksess
->gid
,
916 DEFAULT_KERNEL_TRACE_DIR
, wait
, max_size_per_stream
);
917 pthread_mutex_unlock(socket
->lock
);
919 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
924 * The metadata snapshot is done, ask the consumer to destroy it since
925 * it's not monitored on the consumer side.
927 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
933 /* Close newly opened metadata stream. It's now on the consumer side. */
934 err
= close(ksess
->metadata_stream_fd
);
936 PERROR("close snapshot kernel");
940 trace_kernel_destroy_metadata(ksess
->metadata
);
942 /* Restore metadata state.*/
943 ksess
->metadata
= saved_metadata
;
944 ksess
->metadata_stream_fd
= saved_metadata_fd
;