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 WARN("Event %s not found!", ev
->name
);
208 PERROR("create event ioctl");
215 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
217 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
218 DBG2("Kernel event syscall creation success");
220 * We use fd == -1 to ensure that we never trigger a close of fd
228 /* Prevent fd duplication after execlp() */
229 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
231 PERROR("fcntl session fd");
235 /* Add event to event list */
236 cds_list_add(&event
->list
, &channel
->events_list
.head
);
237 channel
->event_count
++;
239 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
250 * Disable a kernel channel.
252 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
258 ret
= kernctl_disable(chan
->fd
);
260 PERROR("disable chan ioctl");
266 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
275 * Enable a kernel channel.
277 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
283 ret
= kernctl_enable(chan
->fd
);
284 if (ret
< 0 && errno
!= EEXIST
) {
285 PERROR("Enable kernel chan");
290 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
299 * Enable a kernel event.
301 int kernel_enable_event(struct ltt_kernel_event
*event
)
307 ret
= kernctl_enable(event
->fd
);
311 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
314 PERROR("enable kernel event");
321 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
330 * Disable a kernel event.
332 int kernel_disable_event(struct ltt_kernel_event
*event
)
338 ret
= kernctl_disable(event
->fd
);
342 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
345 PERROR("disable kernel event");
352 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
361 * Create kernel metadata, open from the kernel tracer and add it to the
364 int kernel_open_metadata(struct ltt_kernel_session
*session
)
367 struct ltt_kernel_metadata
*lkm
= NULL
;
371 /* Allocate kernel metadata */
372 lkm
= trace_kernel_create_metadata();
377 /* Kernel tracer metadata creation */
378 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
384 /* Prevent fd duplication after execlp() */
385 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
387 PERROR("fcntl session fd");
390 session
->metadata
= lkm
;
392 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
397 trace_kernel_destroy_metadata(lkm
);
403 * Start tracing session.
405 int kernel_start_session(struct ltt_kernel_session
*session
)
411 ret
= kernctl_start_session(session
->fd
);
413 PERROR("ioctl start session");
417 DBG("Kernel session started");
426 * Make a kernel wait to make sure in-flight probe have completed.
428 void kernel_wait_quiescent(int fd
)
432 DBG("Kernel quiescent wait on %d", fd
);
434 ret
= kernctl_wait_quiescent(fd
);
436 PERROR("wait quiescent ioctl");
437 ERR("Kernel quiescent wait failed");
444 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
450 ret
= kernctl_calibrate(fd
, calibrate
);
452 PERROR("calibrate ioctl");
461 * Force flush buffer of metadata.
463 int kernel_metadata_flush_buffer(int fd
)
467 DBG("Kernel flushing metadata buffer on fd %d", fd
);
469 ret
= kernctl_buffer_flush(fd
);
471 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
478 * Force flush buffer for channel.
480 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
483 struct ltt_kernel_stream
*stream
;
487 DBG("Flush buffer for channel %s", channel
->channel
->name
);
489 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
490 DBG("Flushing channel stream %d", stream
->fd
);
491 ret
= kernctl_buffer_flush(stream
->fd
);
494 ERR("Fail to flush buffer for stream %d (ret: %d)",
503 * Stop tracing session.
505 int kernel_stop_session(struct ltt_kernel_session
*session
)
511 ret
= kernctl_stop_session(session
->fd
);
516 DBG("Kernel session stopped");
525 * Open stream of channel, register it to the kernel tracer and add it
526 * to the stream list of the channel.
528 * Return the number of created stream. Else, a negative value.
530 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
533 struct ltt_kernel_stream
*lks
;
537 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
538 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
548 /* Prevent fd duplication after execlp() */
549 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
551 PERROR("fcntl session fd");
554 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
555 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
557 /* Add stream to channe stream list */
558 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
559 channel
->stream_count
++;
561 /* Increment counter which represent CPU number. */
564 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
568 return channel
->stream_count
;
575 * Open the metadata stream and set it to the kernel session.
577 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
583 ret
= kernctl_create_stream(session
->metadata
->fd
);
585 PERROR("kernel create metadata stream");
589 DBG("Kernel metadata stream created (fd: %d)", ret
);
590 session
->metadata_stream_fd
= ret
;
591 /* Prevent fd duplication after execlp() */
592 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
594 PERROR("fcntl session fd");
604 * Get the event list from the kernel tracer and return the number of elements.
606 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
610 size_t nbmem
, count
= 0;
612 struct lttng_event
*elist
;
616 fd
= kernctl_tracepoint_list(tracer_fd
);
618 PERROR("kernel tracepoint list");
622 fp
= fdopen(fd
, "r");
624 PERROR("kernel tracepoint list fdopen");
629 * Init memory size counter
630 * See kernel-ctl.h for explanation of this value
632 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
633 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
635 PERROR("alloc list events");
640 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
641 if (count
>= nbmem
) {
642 struct lttng_event
*new_elist
;
645 new_nbmem
= nbmem
<< 1;
646 DBG("Reallocating event list from %zu to %zu bytes",
648 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
649 if (new_elist
== NULL
) {
650 PERROR("realloc list events");
656 /* Zero the new memory */
657 memset(new_elist
+ nbmem
, 0,
658 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
662 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
663 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
664 elist
[count
].enabled
= -1;
670 DBG("Kernel list events done (%zu events)", count
);
672 ret
= fclose(fp
); /* closes both fp and fd */
688 * Get kernel version and validate it.
690 int kernel_validate_version(int tracer_fd
)
693 struct lttng_kernel_tracer_version version
;
695 ret
= kernctl_tracer_version(tracer_fd
, &version
);
697 ERR("Failed at getting the lttng-modules version");
701 /* Validate version */
702 if (version
.major
!= KERN_MODULES_PRE_MAJOR
703 && version
.major
!= KERN_MODULES_MAJOR
) {
707 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
711 ERR("Kernel major version %d is not compatible (supporting <= %d)",
712 version
.major
, KERN_MODULES_MAJOR
)
720 * Kernel work-arounds called at the start of sessiond main().
722 int init_kernel_workarounds(void)
728 * boot_id needs to be read once before being used concurrently
729 * to deal with a Linux kernel race. A fix is proposed for
730 * upstream, but the work-around is needed for older kernels.
732 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
739 ret
= fread(buf
, 1, sizeof(buf
), fp
);
741 /* Ignore error, we don't really care */
753 * Complete teardown of a kernel session.
755 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
758 DBG3("No kernel session when tearing down session");
762 DBG("Tearing down kernel session");
765 * Destroy channels on the consumer if at least one FD has been sent and we
766 * are in no output mode because the streams are in *no* monitor mode so we
767 * have to send a command to clean them up or else they leaked.
769 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
771 struct consumer_socket
*socket
;
772 struct lttng_ht_iter iter
;
774 /* For each consumer socket. */
776 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
778 struct ltt_kernel_channel
*chan
;
780 /* For each channel, ask the consumer to destroy it. */
781 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
782 ret
= kernel_consumer_destroy_channel(socket
, chan
);
784 /* Consumer is probably dead. Use next socket. */
792 /* Close any relayd session */
793 consumer_output_send_destroy_relayd(ksess
->consumer
);
795 trace_kernel_destroy_session(ksess
);
799 * Destroy a kernel channel object. It does not do anything on the tracer side.
801 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
803 struct ltt_kernel_session
*ksess
= NULL
;
806 assert(kchan
->channel
);
808 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
810 /* Update channel count of associated session. */
811 if (kchan
->session
) {
812 /* Keep pointer reference so we can update it after the destroy. */
813 ksess
= kchan
->session
;
816 trace_kernel_destroy_channel(kchan
);
819 * At this point the kernel channel is not visible anymore. This is safe
820 * since in order to work on a visible kernel session, the tracing session
821 * lock (ltt_session.lock) MUST be acquired.
824 ksess
->channel_count
--;
829 * Take a snapshot for a given kernel session.
831 * Return 0 on success or else return a LTTNG_ERR code.
833 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
834 struct snapshot_output
*output
, int wait
, unsigned int nb_streams
)
836 int err
, ret
, saved_metadata_fd
;
837 struct consumer_socket
*socket
;
838 struct lttng_ht_iter iter
;
839 struct ltt_kernel_metadata
*saved_metadata
;
840 uint64_t max_size_per_stream
= 0;
843 assert(ksess
->consumer
);
846 DBG("Kernel snapshot record started");
848 /* Save current metadata since the following calls will change it. */
849 saved_metadata
= ksess
->metadata
;
850 saved_metadata_fd
= ksess
->metadata_stream_fd
;
854 ret
= kernel_open_metadata(ksess
);
856 ret
= LTTNG_ERR_KERN_META_FAIL
;
860 ret
= kernel_open_metadata_stream(ksess
);
862 ret
= LTTNG_ERR_KERN_META_FAIL
;
863 goto error_open_stream
;
866 if (output
->max_size
> 0 && nb_streams
> 0) {
867 max_size_per_stream
= output
->max_size
/ nb_streams
;
870 /* Send metadata to consumer and snapshot everything. */
871 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
873 struct consumer_output
*saved_output
;
874 struct ltt_kernel_channel
*chan
;
877 * Temporarly switch consumer output for our snapshot output. As long
878 * as the session lock is taken, this is safe.
880 saved_output
= ksess
->consumer
;
881 ksess
->consumer
= output
->consumer
;
883 pthread_mutex_lock(socket
->lock
);
884 /* This stream must not be monitored by the consumer. */
885 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
886 pthread_mutex_unlock(socket
->lock
);
887 /* Put back the saved consumer output into the session. */
888 ksess
->consumer
= saved_output
;
890 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
894 /* For each channel, ask the consumer to snapshot it. */
895 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
896 if (max_size_per_stream
&&
897 chan
->channel
->attr
.subbuf_size
> max_size_per_stream
) {
898 ret
= LTTNG_ERR_INVALID
;
899 DBG3("Kernel snapshot record maximum stream size %" PRIu64
900 " is smaller than subbuffer size of %" PRIu64
,
901 max_size_per_stream
, chan
->channel
->attr
.subbuf_size
);
902 (void) kernel_consumer_destroy_metadata(socket
,
907 pthread_mutex_lock(socket
->lock
);
908 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
909 ksess
->uid
, ksess
->gid
,
910 DEFAULT_KERNEL_TRACE_DIR
, wait
,
911 max_size_per_stream
);
912 pthread_mutex_unlock(socket
->lock
);
914 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
915 (void) kernel_consumer_destroy_metadata(socket
,
921 /* Snapshot metadata, */
922 pthread_mutex_lock(socket
->lock
);
923 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
924 1, ksess
->uid
, ksess
->gid
,
925 DEFAULT_KERNEL_TRACE_DIR
, wait
, max_size_per_stream
);
926 pthread_mutex_unlock(socket
->lock
);
928 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
933 * The metadata snapshot is done, ask the consumer to destroy it since
934 * it's not monitored on the consumer side.
936 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
942 /* Close newly opened metadata stream. It's now on the consumer side. */
943 err
= close(ksess
->metadata_stream_fd
);
945 PERROR("close snapshot kernel");
949 trace_kernel_destroy_metadata(ksess
->metadata
);
951 /* Restore metadata state.*/
952 ksess
->metadata
= saved_metadata
;
953 ksess
->metadata_stream_fd
= saved_metadata_fd
;