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"
38 * Add context on a kernel channel.
40 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
41 struct ltt_kernel_context
*ctx
)
48 DBG("Adding context to channel %s", chan
->channel
->name
);
49 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
51 if (errno
!= EEXIST
) {
52 PERROR("add context ioctl");
54 /* If EEXIST, we just ignore the error */
60 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
69 * Create a new kernel session, register it to the kernel tracer and add it to
70 * the session daemon session.
72 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
75 struct ltt_kernel_session
*lks
;
79 /* Allocate data structure */
80 lks
= trace_kernel_create_session();
86 /* Kernel tracer session creation */
87 ret
= kernctl_create_session(tracer_fd
);
89 PERROR("ioctl kernel create session");
94 /* Prevent fd duplication after execlp() */
95 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
97 PERROR("fcntl session fd");
100 lks
->id
= session
->id
;
101 lks
->consumer_fds_sent
= 0;
102 session
->kernel_session
= lks
;
104 DBG("Kernel session created (fd: %d)", lks
->fd
);
110 trace_kernel_destroy_session(lks
);
116 * Create a kernel channel, register it to the kernel tracer and add it to the
119 int kernel_create_channel(struct ltt_kernel_session
*session
,
120 struct lttng_channel
*chan
)
123 struct ltt_kernel_channel
*lkc
;
128 /* Allocate kernel channel */
129 lkc
= trace_kernel_create_channel(chan
);
134 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
135 chan
->name
, lkc
->channel
->attr
.overwrite
,
136 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
137 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
138 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
140 /* Kernel tracer channel creation */
141 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
143 PERROR("ioctl kernel create channel");
147 /* Setup the channel fd */
149 /* Prevent fd duplication after execlp() */
150 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
152 PERROR("fcntl session fd");
155 /* Add channel to session */
156 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
157 session
->channel_count
++;
158 lkc
->session
= session
;
160 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
173 * Create a kernel event, enable it to the kernel tracer and add it to the
174 * channel event list of the kernel session.
176 int kernel_create_event(struct lttng_event
*ev
,
177 struct ltt_kernel_channel
*channel
)
180 struct ltt_kernel_event
*event
;
185 event
= trace_kernel_create_event(ev
);
191 ret
= kernctl_create_event(channel
->fd
, event
->event
);
197 WARN("Event type not implemented");
200 WARN("Event %s not found!", ev
->name
);
203 PERROR("create event ioctl");
210 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
212 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
213 DBG2("Kernel event syscall creation success");
215 * We use fd == -1 to ensure that we never trigger a close of fd
223 /* Prevent fd duplication after execlp() */
224 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
226 PERROR("fcntl session fd");
230 /* Add event to event list */
231 cds_list_add(&event
->list
, &channel
->events_list
.head
);
232 channel
->event_count
++;
234 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
245 * Disable a kernel channel.
247 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
253 ret
= kernctl_disable(chan
->fd
);
255 PERROR("disable chan ioctl");
261 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
270 * Enable a kernel channel.
272 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
278 ret
= kernctl_enable(chan
->fd
);
279 if (ret
< 0 && errno
!= EEXIST
) {
280 PERROR("Enable kernel chan");
285 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
294 * Enable a kernel event.
296 int kernel_enable_event(struct ltt_kernel_event
*event
)
302 ret
= kernctl_enable(event
->fd
);
306 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
309 PERROR("enable kernel event");
316 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
325 * Disable a kernel event.
327 int kernel_disable_event(struct ltt_kernel_event
*event
)
333 ret
= kernctl_disable(event
->fd
);
337 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
340 PERROR("disable kernel event");
347 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
355 int kernel_enable_syscall(const char *syscall_name
,
356 struct ltt_kernel_channel
*channel
)
358 return kernctl_enable_syscall(channel
->fd
, syscall_name
);
361 int kernel_disable_syscall(const char *syscall_name
,
362 struct ltt_kernel_channel
*channel
)
364 return kernctl_disable_syscall(channel
->fd
, syscall_name
);
368 * Create kernel metadata, open from the kernel tracer and add it to the
371 int kernel_open_metadata(struct ltt_kernel_session
*session
)
374 struct ltt_kernel_metadata
*lkm
= NULL
;
378 /* Allocate kernel metadata */
379 lkm
= trace_kernel_create_metadata();
384 /* Kernel tracer metadata creation */
385 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
391 /* Prevent fd duplication after execlp() */
392 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
394 PERROR("fcntl session fd");
397 session
->metadata
= lkm
;
399 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
404 trace_kernel_destroy_metadata(lkm
);
410 * Start tracing session.
412 int kernel_start_session(struct ltt_kernel_session
*session
)
418 ret
= kernctl_start_session(session
->fd
);
420 PERROR("ioctl start session");
424 DBG("Kernel session started");
433 * Make a kernel wait to make sure in-flight probe have completed.
435 void kernel_wait_quiescent(int fd
)
439 DBG("Kernel quiescent wait on %d", fd
);
441 ret
= kernctl_wait_quiescent(fd
);
443 PERROR("wait quiescent ioctl");
444 ERR("Kernel quiescent wait failed");
451 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
457 ret
= kernctl_calibrate(fd
, calibrate
);
459 PERROR("calibrate ioctl");
468 * Force flush buffer of metadata.
470 int kernel_metadata_flush_buffer(int fd
)
474 DBG("Kernel flushing metadata buffer on fd %d", fd
);
476 ret
= kernctl_buffer_flush(fd
);
478 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
485 * Force flush buffer for channel.
487 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
490 struct ltt_kernel_stream
*stream
;
494 DBG("Flush buffer for channel %s", channel
->channel
->name
);
496 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
497 DBG("Flushing channel stream %d", stream
->fd
);
498 ret
= kernctl_buffer_flush(stream
->fd
);
501 ERR("Fail to flush buffer for stream %d (ret: %d)",
510 * Stop tracing session.
512 int kernel_stop_session(struct ltt_kernel_session
*session
)
518 ret
= kernctl_stop_session(session
->fd
);
523 DBG("Kernel session stopped");
532 * Open stream of channel, register it to the kernel tracer and add it
533 * to the stream list of the channel.
535 * Return the number of created stream. Else, a negative value.
537 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
540 struct ltt_kernel_stream
*lks
;
544 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
545 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
555 /* Prevent fd duplication after execlp() */
556 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
558 PERROR("fcntl session fd");
561 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
562 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
564 /* Add stream to channe stream list */
565 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
566 channel
->stream_count
++;
568 /* Increment counter which represent CPU number. */
571 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
575 return channel
->stream_count
;
582 * Open the metadata stream and set it to the kernel session.
584 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
590 ret
= kernctl_create_stream(session
->metadata
->fd
);
592 PERROR("kernel create metadata stream");
596 DBG("Kernel metadata stream created (fd: %d)", ret
);
597 session
->metadata_stream_fd
= ret
;
598 /* Prevent fd duplication after execlp() */
599 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
601 PERROR("fcntl session fd");
611 * Get the event list from the kernel tracer and return the number of elements.
613 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
617 size_t nbmem
, count
= 0;
619 struct lttng_event
*elist
;
623 fd
= kernctl_tracepoint_list(tracer_fd
);
625 PERROR("kernel tracepoint list");
629 fp
= fdopen(fd
, "r");
631 PERROR("kernel tracepoint list fdopen");
636 * Init memory size counter
637 * See kernel-ctl.h for explanation of this value
639 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
640 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
642 PERROR("alloc list events");
647 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
648 if (count
>= nbmem
) {
649 struct lttng_event
*new_elist
;
652 new_nbmem
= nbmem
<< 1;
653 DBG("Reallocating event list from %zu to %zu bytes",
655 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
656 if (new_elist
== NULL
) {
657 PERROR("realloc list events");
663 /* Zero the new memory */
664 memset(new_elist
+ nbmem
, 0,
665 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
669 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
670 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
671 elist
[count
].enabled
= -1;
677 DBG("Kernel list events done (%zu events)", count
);
679 ret
= fclose(fp
); /* closes both fp and fd */
695 * Get kernel version and validate it.
697 int kernel_validate_version(int tracer_fd
)
700 struct lttng_kernel_tracer_version version
;
702 ret
= kernctl_tracer_version(tracer_fd
, &version
);
704 ERR("Failed at getting the lttng-modules version");
708 /* Validate version */
709 if (version
.major
!= KERN_MODULES_PRE_MAJOR
710 && version
.major
!= KERN_MODULES_MAJOR
) {
714 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
718 ERR("Kernel major version %d is not compatible (supporting <= %d)",
719 version
.major
, KERN_MODULES_MAJOR
)
727 * Kernel work-arounds called at the start of sessiond main().
729 int init_kernel_workarounds(void)
735 * boot_id needs to be read once before being used concurrently
736 * to deal with a Linux kernel race. A fix is proposed for
737 * upstream, but the work-around is needed for older kernels.
739 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
746 ret
= fread(buf
, 1, sizeof(buf
), fp
);
748 /* Ignore error, we don't really care */
760 * Complete teardown of a kernel session.
762 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
765 DBG3("No kernel session when tearing down session");
769 DBG("Tearing down kernel session");
772 * Destroy channels on the consumer if at least one FD has been sent and we
773 * are in no output mode because the streams are in *no* monitor mode so we
774 * have to send a command to clean them up or else they leaked.
776 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
778 struct consumer_socket
*socket
;
779 struct lttng_ht_iter iter
;
781 /* For each consumer socket. */
782 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
784 struct ltt_kernel_channel
*chan
;
786 /* For each channel, ask the consumer to destroy it. */
787 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
788 ret
= kernel_consumer_destroy_channel(socket
, chan
);
790 /* Consumer is probably dead. Use next socket. */
797 /* Close any relayd session */
798 consumer_output_send_destroy_relayd(ksess
->consumer
);
800 trace_kernel_destroy_session(ksess
);
804 * Destroy a kernel channel object. It does not do anything on the tracer side.
806 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
808 struct ltt_kernel_session
*ksess
= NULL
;
811 assert(kchan
->channel
);
813 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
815 /* Update channel count of associated session. */
816 if (kchan
->session
) {
817 /* Keep pointer reference so we can update it after the destroy. */
818 ksess
= kchan
->session
;
821 trace_kernel_destroy_channel(kchan
);
824 * At this point the kernel channel is not visible anymore. This is safe
825 * since in order to work on a visible kernel session, the tracing session
826 * lock (ltt_session.lock) MUST be acquired.
829 ksess
->channel_count
--;
834 * Take a snapshot for a given kernel session.
836 * Return 0 on success or else return a LTTNG_ERR code.
838 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
839 struct snapshot_output
*output
, int wait
, uint64_t max_size_per_stream
)
841 int err
, ret
, saved_metadata_fd
;
842 struct consumer_socket
*socket
;
843 struct lttng_ht_iter iter
;
844 struct ltt_kernel_metadata
*saved_metadata
;
847 assert(ksess
->consumer
);
850 DBG("Kernel snapshot record started");
852 /* Save current metadata since the following calls will change it. */
853 saved_metadata
= ksess
->metadata
;
854 saved_metadata_fd
= ksess
->metadata_stream_fd
;
858 ret
= kernel_open_metadata(ksess
);
860 ret
= LTTNG_ERR_KERN_META_FAIL
;
864 ret
= kernel_open_metadata_stream(ksess
);
866 ret
= LTTNG_ERR_KERN_META_FAIL
;
867 goto error_open_stream
;
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 pthread_mutex_lock(socket
->lock
);
897 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
898 ksess
->uid
, ksess
->gid
,
899 DEFAULT_KERNEL_TRACE_DIR
, wait
,
900 max_size_per_stream
);
901 pthread_mutex_unlock(socket
->lock
);
903 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
904 (void) kernel_consumer_destroy_metadata(socket
,
910 /* Snapshot metadata, */
911 pthread_mutex_lock(socket
->lock
);
912 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
913 1, ksess
->uid
, ksess
->gid
,
914 DEFAULT_KERNEL_TRACE_DIR
, wait
, max_size_per_stream
);
915 pthread_mutex_unlock(socket
->lock
);
917 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
922 * The metadata snapshot is done, ask the consumer to destroy it since
923 * it's not monitored on the consumer side.
925 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
931 /* Close newly opened metadata stream. It's now on the consumer side. */
932 err
= close(ksess
->metadata_stream_fd
);
934 PERROR("close snapshot kernel");
938 trace_kernel_destroy_metadata(ksess
->metadata
);
940 /* Restore metadata state.*/
941 ksess
->metadata
= saved_metadata
;
942 ksess
->metadata_stream_fd
= saved_metadata_fd
;
949 * Get the syscall mask array from the kernel tracer.
951 * Return 0 on success else a negative value. In both case, syscall_mask should
954 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
956 assert(syscall_mask
);
959 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);