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/kernel-ctl/kernel-ioctl.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "kernel-consumer.h"
35 #include "kern-modules.h"
39 * Add context on a kernel channel.
41 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
42 struct ltt_kernel_context
*ctx
)
49 DBG("Adding context to channel %s", chan
->channel
->name
);
50 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
52 if (errno
!= EEXIST
) {
53 PERROR("add context ioctl");
55 /* If EEXIST, we just ignore the error */
61 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
73 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
76 struct ltt_kernel_session
*lks
;
80 /* Allocate data structure */
81 lks
= trace_kernel_create_session();
87 /* Kernel tracer session creation */
88 ret
= kernctl_create_session(tracer_fd
);
90 PERROR("ioctl kernel create session");
95 /* Prevent fd duplication after execlp() */
96 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
98 PERROR("fcntl session fd");
101 lks
->id
= session
->id
;
102 lks
->consumer_fds_sent
= 0;
103 session
->kernel_session
= lks
;
105 DBG("Kernel session created (fd: %d)", lks
->fd
);
111 trace_kernel_destroy_session(lks
);
117 * Create a kernel channel, register it to the kernel tracer and add it to the
120 int kernel_create_channel(struct ltt_kernel_session
*session
,
121 struct lttng_channel
*chan
)
124 struct ltt_kernel_channel
*lkc
;
129 /* Allocate kernel channel */
130 lkc
= trace_kernel_create_channel(chan
);
135 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
136 chan
->name
, lkc
->channel
->attr
.overwrite
,
137 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
138 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
139 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
141 /* Kernel tracer channel creation */
142 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
144 PERROR("ioctl kernel create channel");
148 /* Setup the channel fd */
150 /* Prevent fd duplication after execlp() */
151 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
153 PERROR("fcntl session fd");
156 /* Add channel to session */
157 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
158 session
->channel_count
++;
159 lkc
->session
= session
;
161 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
174 * Create a kernel event, enable it to the kernel tracer and add it to the
175 * channel event list of the kernel session.
176 * We own filter_expression and filter.
178 int kernel_create_event(struct lttng_event
*ev
,
179 struct ltt_kernel_channel
*channel
)
182 struct ltt_kernel_event
*event
;
187 event
= trace_kernel_create_event(ev
);
193 ret
= kernctl_create_event(channel
->fd
, event
->event
);
199 WARN("Event type not implemented");
202 WARN("Event %s not found!", ev
->name
);
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
);
357 int kernel_enable_syscall(const char *syscall_name
,
358 struct ltt_kernel_channel
*channel
)
360 return kernctl_enable_syscall(channel
->fd
, syscall_name
);
363 int kernel_disable_syscall(const char *syscall_name
,
364 struct ltt_kernel_channel
*channel
)
366 return kernctl_disable_syscall(channel
->fd
, syscall_name
);
370 * Create kernel metadata, open from the kernel tracer and add it to the
373 int kernel_open_metadata(struct ltt_kernel_session
*session
)
376 struct ltt_kernel_metadata
*lkm
= NULL
;
380 /* Allocate kernel metadata */
381 lkm
= trace_kernel_create_metadata();
386 /* Kernel tracer metadata creation */
387 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
393 /* Prevent fd duplication after execlp() */
394 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
396 PERROR("fcntl session fd");
399 session
->metadata
= lkm
;
401 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
406 trace_kernel_destroy_metadata(lkm
);
412 * Start tracing session.
414 int kernel_start_session(struct ltt_kernel_session
*session
)
420 ret
= kernctl_start_session(session
->fd
);
422 PERROR("ioctl start session");
426 DBG("Kernel session started");
435 * Make a kernel wait to make sure in-flight probe have completed.
437 void kernel_wait_quiescent(int fd
)
441 DBG("Kernel quiescent wait on %d", fd
);
443 ret
= kernctl_wait_quiescent(fd
);
445 PERROR("wait quiescent ioctl");
446 ERR("Kernel quiescent wait failed");
453 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
459 ret
= kernctl_calibrate(fd
, calibrate
);
461 PERROR("calibrate ioctl");
470 * Force flush buffer of metadata.
472 int kernel_metadata_flush_buffer(int fd
)
476 DBG("Kernel flushing metadata buffer on fd %d", fd
);
478 ret
= kernctl_buffer_flush(fd
);
480 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
487 * Force flush buffer for channel.
489 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
492 struct ltt_kernel_stream
*stream
;
496 DBG("Flush buffer for channel %s", channel
->channel
->name
);
498 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
499 DBG("Flushing channel stream %d", stream
->fd
);
500 ret
= kernctl_buffer_flush(stream
->fd
);
503 ERR("Fail to flush buffer for stream %d (ret: %d)",
512 * Stop tracing session.
514 int kernel_stop_session(struct ltt_kernel_session
*session
)
520 ret
= kernctl_stop_session(session
->fd
);
525 DBG("Kernel session stopped");
534 * Open stream of channel, register it to the kernel tracer and add it
535 * to the stream list of the channel.
537 * Return the number of created stream. Else, a negative value.
539 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
542 struct ltt_kernel_stream
*lks
;
546 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
547 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
557 /* Prevent fd duplication after execlp() */
558 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
560 PERROR("fcntl session fd");
563 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
564 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
566 /* Add stream to channe stream list */
567 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
568 channel
->stream_count
++;
570 /* Increment counter which represent CPU number. */
573 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
577 return channel
->stream_count
;
584 * Open the metadata stream and set it to the kernel session.
586 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
592 ret
= kernctl_create_stream(session
->metadata
->fd
);
594 PERROR("kernel create metadata stream");
598 DBG("Kernel metadata stream created (fd: %d)", ret
);
599 session
->metadata_stream_fd
= ret
;
600 /* Prevent fd duplication after execlp() */
601 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
603 PERROR("fcntl session fd");
613 * Get the event list from the kernel tracer and return the number of elements.
615 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
619 size_t nbmem
, count
= 0;
621 struct lttng_event
*elist
;
625 fd
= kernctl_tracepoint_list(tracer_fd
);
627 PERROR("kernel tracepoint list");
631 fp
= fdopen(fd
, "r");
633 PERROR("kernel tracepoint list fdopen");
638 * Init memory size counter
639 * See kernel-ctl.h for explanation of this value
641 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
642 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
644 PERROR("alloc list events");
649 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
650 if (count
>= nbmem
) {
651 struct lttng_event
*new_elist
;
654 new_nbmem
= nbmem
<< 1;
655 DBG("Reallocating event list from %zu to %zu bytes",
657 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
658 if (new_elist
== NULL
) {
659 PERROR("realloc list events");
665 /* Zero the new memory */
666 memset(new_elist
+ nbmem
, 0,
667 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
671 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
672 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
673 elist
[count
].enabled
= -1;
679 DBG("Kernel list events done (%zu events)", count
);
681 ret
= fclose(fp
); /* closes both fp and fd */
697 * Get kernel version and validate it.
699 int kernel_validate_version(int tracer_fd
)
702 struct lttng_kernel_tracer_version version
;
703 struct lttng_kernel_tracer_abi_version abi_version
;
705 ret
= kernctl_tracer_version(tracer_fd
, &version
);
707 ERR("Failed at getting the lttng-modules version");
711 /* Validate version */
712 if (version
.major
!= VERSION_MAJOR
) {
713 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
714 version
.major
, VERSION_MAJOR
);
717 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
719 ERR("Failed at getting lttng-modules ABI version");
722 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
723 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
724 abi_version
.major
, abi_version
.minor
,
725 LTTNG_MODULES_ABI_MAJOR_VERSION
);
728 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
729 version
.major
, version
.minor
,
730 abi_version
.major
, abi_version
.minor
);
741 * Kernel work-arounds called at the start of sessiond main().
743 int init_kernel_workarounds(void)
749 * boot_id needs to be read once before being used concurrently
750 * to deal with a Linux kernel race. A fix is proposed for
751 * upstream, but the work-around is needed for older kernels.
753 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
760 ret
= fread(buf
, 1, sizeof(buf
), fp
);
762 /* Ignore error, we don't really care */
774 * Complete teardown of a kernel session.
776 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
779 DBG3("No kernel session when tearing down session");
783 DBG("Tearing down kernel session");
786 * Destroy channels on the consumer if at least one FD has been sent and we
787 * are in no output mode because the streams are in *no* monitor mode so we
788 * have to send a command to clean them up or else they leaked.
790 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
792 struct consumer_socket
*socket
;
793 struct lttng_ht_iter iter
;
795 /* For each consumer socket. */
797 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
799 struct ltt_kernel_channel
*chan
;
801 /* For each channel, ask the consumer to destroy it. */
802 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
803 ret
= kernel_consumer_destroy_channel(socket
, chan
);
805 /* Consumer is probably dead. Use next socket. */
813 /* Close any relayd session */
814 consumer_output_send_destroy_relayd(ksess
->consumer
);
816 trace_kernel_destroy_session(ksess
);
820 * Destroy a kernel channel object. It does not do anything on the tracer side.
822 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
824 struct ltt_kernel_session
*ksess
= NULL
;
827 assert(kchan
->channel
);
829 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
831 /* Update channel count of associated session. */
832 if (kchan
->session
) {
833 /* Keep pointer reference so we can update it after the destroy. */
834 ksess
= kchan
->session
;
837 trace_kernel_destroy_channel(kchan
);
840 * At this point the kernel channel is not visible anymore. This is safe
841 * since in order to work on a visible kernel session, the tracing session
842 * lock (ltt_session.lock) MUST be acquired.
845 ksess
->channel_count
--;
850 * Take a snapshot for a given kernel session.
852 * Return 0 on success or else return a LTTNG_ERR code.
854 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
855 struct snapshot_output
*output
, int wait
, uint64_t max_size_per_stream
)
857 int err
, ret
, saved_metadata_fd
;
858 struct consumer_socket
*socket
;
859 struct lttng_ht_iter iter
;
860 struct ltt_kernel_metadata
*saved_metadata
;
863 assert(ksess
->consumer
);
866 DBG("Kernel snapshot record started");
868 /* Save current metadata since the following calls will change it. */
869 saved_metadata
= ksess
->metadata
;
870 saved_metadata_fd
= ksess
->metadata_stream_fd
;
874 ret
= kernel_open_metadata(ksess
);
876 ret
= LTTNG_ERR_KERN_META_FAIL
;
880 ret
= kernel_open_metadata_stream(ksess
);
882 ret
= LTTNG_ERR_KERN_META_FAIL
;
883 goto error_open_stream
;
886 /* Send metadata to consumer and snapshot everything. */
887 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
889 struct consumer_output
*saved_output
;
890 struct ltt_kernel_channel
*chan
;
893 * Temporarly switch consumer output for our snapshot output. As long
894 * as the session lock is taken, this is safe.
896 saved_output
= ksess
->consumer
;
897 ksess
->consumer
= output
->consumer
;
899 pthread_mutex_lock(socket
->lock
);
900 /* This stream must not be monitored by the consumer. */
901 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
902 pthread_mutex_unlock(socket
->lock
);
903 /* Put back the saved consumer output into the session. */
904 ksess
->consumer
= saved_output
;
906 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
910 /* For each channel, ask the consumer to snapshot it. */
911 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
912 pthread_mutex_lock(socket
->lock
);
913 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
914 ksess
->uid
, ksess
->gid
,
915 DEFAULT_KERNEL_TRACE_DIR
, wait
,
916 max_size_per_stream
);
917 pthread_mutex_unlock(socket
->lock
);
919 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
920 (void) kernel_consumer_destroy_metadata(socket
,
926 /* Snapshot metadata, */
927 pthread_mutex_lock(socket
->lock
);
928 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
929 1, ksess
->uid
, ksess
->gid
,
930 DEFAULT_KERNEL_TRACE_DIR
, wait
, max_size_per_stream
);
931 pthread_mutex_unlock(socket
->lock
);
933 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
938 * The metadata snapshot is done, ask the consumer to destroy it since
939 * it's not monitored on the consumer side.
941 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
947 /* Close newly opened metadata stream. It's now on the consumer side. */
948 err
= close(ksess
->metadata_stream_fd
);
950 PERROR("close snapshot kernel");
954 trace_kernel_destroy_metadata(ksess
->metadata
);
956 /* Restore metadata state.*/
957 ksess
->metadata
= saved_metadata
;
958 ksess
->metadata_stream_fd
= saved_metadata_fd
;
965 * Get the syscall mask array from the kernel tracer.
967 * Return 0 on success else a negative value. In both case, syscall_mask should
970 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
972 assert(syscall_mask
);
975 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);