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 ltt_kernel_context
*ctx
)
47 DBG("Adding context to channel %s", chan
->channel
->name
);
48 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
50 if (errno
!= EEXIST
) {
51 PERROR("add context ioctl");
53 /* If EEXIST, we just ignore the error */
59 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
68 * Create a new kernel session, register it to the kernel tracer and add it to
69 * the session daemon session.
71 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
74 struct ltt_kernel_session
*lks
;
78 /* Allocate data structure */
79 lks
= trace_kernel_create_session();
85 /* Kernel tracer session creation */
86 ret
= kernctl_create_session(tracer_fd
);
88 PERROR("ioctl kernel create session");
93 /* Prevent fd duplication after execlp() */
94 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
96 PERROR("fcntl session fd");
99 lks
->id
= session
->id
;
100 lks
->consumer_fds_sent
= 0;
101 session
->kernel_session
= lks
;
103 DBG("Kernel session created (fd: %d)", lks
->fd
);
109 trace_kernel_destroy_session(lks
);
115 * Create a kernel channel, register it to the kernel tracer and add it to the
118 int kernel_create_channel(struct ltt_kernel_session
*session
,
119 struct lttng_channel
*chan
)
122 struct ltt_kernel_channel
*lkc
;
127 /* Allocate kernel channel */
128 lkc
= trace_kernel_create_channel(chan
);
133 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
134 chan
->name
, lkc
->channel
->attr
.overwrite
,
135 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
136 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
137 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
139 /* Kernel tracer channel creation */
140 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
142 PERROR("ioctl kernel create channel");
146 /* Setup the channel fd */
148 /* Prevent fd duplication after execlp() */
149 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
151 PERROR("fcntl session fd");
154 /* Add channel to session */
155 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
156 session
->channel_count
++;
157 lkc
->session
= session
;
159 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
172 * Create a kernel event, enable it to the kernel tracer and add it to the
173 * channel event list of the kernel session.
175 int kernel_create_event(struct lttng_event
*ev
,
176 struct ltt_kernel_channel
*channel
)
179 struct ltt_kernel_event
*event
;
184 event
= trace_kernel_create_event(ev
);
190 ret
= kernctl_create_event(channel
->fd
, event
->event
);
196 WARN("Event type not implemented");
199 WARN("Event %s not found!", ev
->name
);
202 PERROR("create event ioctl");
209 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
211 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
212 DBG2("Kernel event syscall creation success");
214 * We use fd == -1 to ensure that we never trigger a close of fd
222 /* Prevent fd duplication after execlp() */
223 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
225 PERROR("fcntl session fd");
229 /* Add event to event list */
230 cds_list_add(&event
->list
, &channel
->events_list
.head
);
231 channel
->event_count
++;
233 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
244 * Disable a kernel channel.
246 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
252 ret
= kernctl_disable(chan
->fd
);
254 PERROR("disable chan ioctl");
260 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
269 * Enable a kernel channel.
271 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
277 ret
= kernctl_enable(chan
->fd
);
278 if (ret
< 0 && errno
!= EEXIST
) {
279 PERROR("Enable kernel chan");
284 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
293 * Enable a kernel event.
295 int kernel_enable_event(struct ltt_kernel_event
*event
)
301 ret
= kernctl_enable(event
->fd
);
305 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
308 PERROR("enable kernel event");
315 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
324 * Disable a kernel event.
326 int kernel_disable_event(struct ltt_kernel_event
*event
)
332 ret
= kernctl_disable(event
->fd
);
336 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
339 PERROR("disable kernel event");
346 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
355 * Create kernel metadata, open from the kernel tracer and add it to the
358 int kernel_open_metadata(struct ltt_kernel_session
*session
)
361 struct ltt_kernel_metadata
*lkm
= NULL
;
365 /* Allocate kernel metadata */
366 lkm
= trace_kernel_create_metadata();
371 /* Kernel tracer metadata creation */
372 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
378 /* Prevent fd duplication after execlp() */
379 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
381 PERROR("fcntl session fd");
384 session
->metadata
= lkm
;
386 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
391 trace_kernel_destroy_metadata(lkm
);
397 * Start tracing session.
399 int kernel_start_session(struct ltt_kernel_session
*session
)
405 ret
= kernctl_start_session(session
->fd
);
407 PERROR("ioctl start session");
411 DBG("Kernel session started");
420 * Make a kernel wait to make sure in-flight probe have completed.
422 void kernel_wait_quiescent(int fd
)
426 DBG("Kernel quiescent wait on %d", fd
);
428 ret
= kernctl_wait_quiescent(fd
);
430 PERROR("wait quiescent ioctl");
431 ERR("Kernel quiescent wait failed");
438 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
444 ret
= kernctl_calibrate(fd
, calibrate
);
446 PERROR("calibrate ioctl");
455 * Force flush buffer of metadata.
457 int kernel_metadata_flush_buffer(int fd
)
461 DBG("Kernel flushing metadata buffer on fd %d", fd
);
463 ret
= kernctl_buffer_flush(fd
);
465 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
472 * Force flush buffer for channel.
474 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
477 struct ltt_kernel_stream
*stream
;
481 DBG("Flush buffer for channel %s", channel
->channel
->name
);
483 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
484 DBG("Flushing channel stream %d", stream
->fd
);
485 ret
= kernctl_buffer_flush(stream
->fd
);
488 ERR("Fail to flush buffer for stream %d (ret: %d)",
497 * Stop tracing session.
499 int kernel_stop_session(struct ltt_kernel_session
*session
)
505 ret
= kernctl_stop_session(session
->fd
);
510 DBG("Kernel session stopped");
519 * Open stream of channel, register it to the kernel tracer and add it
520 * to the stream list of the channel.
522 * Return the number of created stream. Else, a negative value.
524 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
527 struct ltt_kernel_stream
*lks
;
531 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
532 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
542 /* Prevent fd duplication after execlp() */
543 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
545 PERROR("fcntl session fd");
548 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
549 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
551 /* Add stream to channe stream list */
552 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
553 channel
->stream_count
++;
555 /* Increment counter which represent CPU number. */
558 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
562 return channel
->stream_count
;
569 * Open the metadata stream and set it to the kernel session.
571 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
577 ret
= kernctl_create_stream(session
->metadata
->fd
);
579 PERROR("kernel create metadata stream");
583 DBG("Kernel metadata stream created (fd: %d)", ret
);
584 session
->metadata_stream_fd
= ret
;
585 /* Prevent fd duplication after execlp() */
586 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
588 PERROR("fcntl session fd");
598 * Get the event list from the kernel tracer and return the number of elements.
600 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
604 size_t nbmem
, count
= 0;
606 struct lttng_event
*elist
;
610 fd
= kernctl_tracepoint_list(tracer_fd
);
612 PERROR("kernel tracepoint list");
616 fp
= fdopen(fd
, "r");
618 PERROR("kernel tracepoint list fdopen");
623 * Init memory size counter
624 * See kernel-ctl.h for explanation of this value
626 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
627 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
629 PERROR("alloc list events");
634 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
635 if (count
>= nbmem
) {
636 struct lttng_event
*new_elist
;
639 new_nbmem
= nbmem
<< 1;
640 DBG("Reallocating event list from %zu to %zu bytes",
642 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
643 if (new_elist
== NULL
) {
644 PERROR("realloc list events");
650 /* Zero the new memory */
651 memset(new_elist
+ nbmem
, 0,
652 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
656 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
657 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
658 elist
[count
].enabled
= -1;
664 DBG("Kernel list events done (%zu events)", count
);
666 ret
= fclose(fp
); /* closes both fp and fd */
682 * Get kernel version and validate it.
684 int kernel_validate_version(int tracer_fd
)
687 struct lttng_kernel_tracer_version version
;
689 ret
= kernctl_tracer_version(tracer_fd
, &version
);
691 ERR("Failed at getting the lttng-modules version");
695 /* Validate version */
696 if (version
.major
!= KERN_MODULES_PRE_MAJOR
697 && version
.major
!= KERN_MODULES_MAJOR
) {
701 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
705 ERR("Kernel major version %d is not compatible (supporting <= %d)",
706 version
.major
, KERN_MODULES_MAJOR
)
714 * Kernel work-arounds called at the start of sessiond main().
716 int init_kernel_workarounds(void)
722 * boot_id needs to be read once before being used concurrently
723 * to deal with a Linux kernel race. A fix is proposed for
724 * upstream, but the work-around is needed for older kernels.
726 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
733 ret
= fread(buf
, 1, sizeof(buf
), fp
);
735 /* Ignore error, we don't really care */
747 * Complete teardown of a kernel session.
749 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
752 DBG3("No kernel session when tearing down session");
756 DBG("Tearing down kernel session");
759 * Destroy channels on the consumer if at least one FD has been sent and we
760 * are in no output mode because the streams are in *no* monitor mode so we
761 * have to send a command to clean them up or else they leaked.
763 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
765 struct consumer_socket
*socket
;
766 struct lttng_ht_iter iter
;
768 /* For each consumer socket. */
769 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
771 struct ltt_kernel_channel
*chan
;
773 /* For each channel, ask the consumer to destroy it. */
774 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
775 ret
= kernel_consumer_destroy_channel(socket
, chan
);
777 /* Consumer is probably dead. Use next socket. */
784 /* Close any relayd session */
785 consumer_output_send_destroy_relayd(ksess
->consumer
);
787 trace_kernel_destroy_session(ksess
);
791 * Destroy a kernel channel object. It does not do anything on the tracer side.
793 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
795 struct ltt_kernel_session
*ksess
= NULL
;
798 assert(kchan
->channel
);
800 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
802 /* Update channel count of associated session. */
803 if (kchan
->session
) {
804 /* Keep pointer reference so we can update it after the destroy. */
805 ksess
= kchan
->session
;
808 trace_kernel_destroy_channel(kchan
);
811 * At this point the kernel channel is not visible anymore. This is safe
812 * since in order to work on a visible kernel session, the tracing session
813 * lock (ltt_session.lock) MUST be acquired.
816 ksess
->channel_count
--;
821 * Take a snapshot for a given kernel session.
823 * Return 0 on success or else return a LTTNG_ERR code.
825 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
826 struct snapshot_output
*output
, int wait
, unsigned int nb_streams
)
828 int err
, ret
, saved_metadata_fd
;
829 struct consumer_socket
*socket
;
830 struct lttng_ht_iter iter
;
831 struct ltt_kernel_metadata
*saved_metadata
;
832 uint64_t max_size_per_stream
= 0;
835 assert(ksess
->consumer
);
838 DBG("Kernel snapshot record started");
840 /* Save current metadata since the following calls will change it. */
841 saved_metadata
= ksess
->metadata
;
842 saved_metadata_fd
= ksess
->metadata_stream_fd
;
846 ret
= kernel_open_metadata(ksess
);
848 ret
= LTTNG_ERR_KERN_META_FAIL
;
852 ret
= kernel_open_metadata_stream(ksess
);
854 ret
= LTTNG_ERR_KERN_META_FAIL
;
855 goto error_open_stream
;
858 if (output
->max_size
> 0 && nb_streams
> 0) {
859 max_size_per_stream
= output
->max_size
/ nb_streams
;
862 /* Send metadata to consumer and snapshot everything. */
863 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
865 struct consumer_output
*saved_output
;
866 struct ltt_kernel_channel
*chan
;
869 * Temporarly switch consumer output for our snapshot output. As long
870 * as the session lock is taken, this is safe.
872 saved_output
= ksess
->consumer
;
873 ksess
->consumer
= output
->consumer
;
875 pthread_mutex_lock(socket
->lock
);
876 /* This stream must not be monitored by the consumer. */
877 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
878 pthread_mutex_unlock(socket
->lock
);
879 /* Put back the saved consumer output into the session. */
880 ksess
->consumer
= saved_output
;
882 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
886 /* For each channel, ask the consumer to snapshot it. */
887 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
888 if (max_size_per_stream
&&
889 chan
->channel
->attr
.subbuf_size
> max_size_per_stream
) {
890 ret
= LTTNG_ERR_INVALID
;
891 DBG3("Kernel snapshot record maximum stream size %" PRIu64
892 " is smaller than subbuffer size of %" PRIu64
,
893 max_size_per_stream
, chan
->channel
->attr
.subbuf_size
);
894 (void) kernel_consumer_destroy_metadata(socket
,
899 pthread_mutex_lock(socket
->lock
);
900 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
901 ksess
->uid
, ksess
->gid
,
902 DEFAULT_KERNEL_TRACE_DIR
, wait
,
903 max_size_per_stream
);
904 pthread_mutex_unlock(socket
->lock
);
906 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
907 (void) kernel_consumer_destroy_metadata(socket
,
913 /* Snapshot metadata, */
914 pthread_mutex_lock(socket
->lock
);
915 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
916 1, ksess
->uid
, ksess
->gid
,
917 DEFAULT_KERNEL_TRACE_DIR
, wait
, max_size_per_stream
);
918 pthread_mutex_unlock(socket
->lock
);
920 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
925 * The metadata snapshot is done, ask the consumer to destroy it since
926 * it's not monitored on the consumer side.
928 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
934 /* Close newly opened metadata stream. It's now on the consumer side. */
935 err
= close(ksess
->metadata_stream_fd
);
937 PERROR("close snapshot kernel");
941 trace_kernel_destroy_metadata(ksess
->metadata
);
943 /* Restore metadata state.*/
944 ksess
->metadata
= saved_metadata
;
945 ksess
->metadata_stream_fd
= saved_metadata_fd
;