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.
28 #include <common/common.h>
29 #include <common/kernel-ctl/kernel-ctl.h>
30 #include <common/kernel-ctl/kernel-ioctl.h>
31 #include <common/sessiond-comm/sessiond-comm.h>
35 #include "kernel-consumer.h"
36 #include "kern-modules.h"
40 * Add context on a kernel channel.
42 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
43 struct ltt_kernel_context
*ctx
)
50 DBG("Adding context to channel %s", chan
->channel
->name
);
51 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
53 if (errno
!= EEXIST
) {
54 PERROR("add context ioctl");
56 /* If EEXIST, we just ignore the error */
62 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
71 * Create a new kernel session, register it to the kernel tracer and add it to
72 * the session daemon session.
74 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
77 struct ltt_kernel_session
*lks
;
81 /* Allocate data structure */
82 lks
= trace_kernel_create_session();
88 /* Kernel tracer session creation */
89 ret
= kernctl_create_session(tracer_fd
);
91 PERROR("ioctl kernel create session");
96 /* Prevent fd duplication after execlp() */
97 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
99 PERROR("fcntl session fd");
102 lks
->id
= session
->id
;
103 lks
->consumer_fds_sent
= 0;
104 session
->kernel_session
= lks
;
106 DBG("Kernel session created (fd: %d)", lks
->fd
);
112 trace_kernel_destroy_session(lks
);
118 * Create a kernel channel, register it to the kernel tracer and add it to the
121 int kernel_create_channel(struct ltt_kernel_session
*session
,
122 struct lttng_channel
*chan
)
125 struct ltt_kernel_channel
*lkc
;
130 /* Allocate kernel channel */
131 lkc
= trace_kernel_create_channel(chan
);
136 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
137 chan
->name
, lkc
->channel
->attr
.overwrite
,
138 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
139 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
140 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
142 /* Kernel tracer channel creation */
143 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
145 PERROR("ioctl kernel create channel");
149 /* Setup the channel fd */
151 /* Prevent fd duplication after execlp() */
152 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
154 PERROR("fcntl session fd");
157 /* Add channel to session */
158 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
159 session
->channel_count
++;
160 lkc
->session
= session
;
162 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
175 * Create a kernel event, enable it to the kernel tracer and add it to the
176 * channel event list of the kernel session.
177 * We own filter_expression and filter.
179 int kernel_create_event(struct lttng_event
*ev
,
180 struct ltt_kernel_channel
*channel
,
181 char *filter_expression
,
182 struct lttng_filter_bytecode
*filter
)
185 struct ltt_kernel_event
*event
;
190 /* We pass ownership of filter_expression and filter */
191 event
= trace_kernel_create_event(ev
, filter_expression
,
198 ret
= kernctl_create_event(channel
->fd
, event
->event
);
204 WARN("Event type not implemented");
207 WARN("Event %s not found!", ev
->name
);
210 PERROR("create event ioctl");
216 event
->type
= ev
->type
;
218 /* Prevent fd duplication after execlp() */
219 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
221 PERROR("fcntl session fd");
225 ret
= kernctl_filter(event
->fd
, filter
);
231 ret
= kernctl_enable(event
->fd
);
235 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
238 PERROR("enable kernel event");
244 /* Add event to event list */
245 cds_list_add(&event
->list
, &channel
->events_list
.head
);
246 channel
->event_count
++;
248 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
257 closeret
= close(event
->fd
);
259 PERROR("close event fd");
269 * Disable a kernel channel.
271 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
277 ret
= kernctl_disable(chan
->fd
);
279 PERROR("disable chan ioctl");
285 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
294 * Enable a kernel channel.
296 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
302 ret
= kernctl_enable(chan
->fd
);
303 if (ret
< 0 && errno
!= EEXIST
) {
304 PERROR("Enable kernel chan");
309 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
318 * Enable a kernel event.
320 int kernel_enable_event(struct ltt_kernel_event
*event
)
326 ret
= kernctl_enable(event
->fd
);
330 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
333 PERROR("enable kernel event");
340 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
349 * Disable a kernel event.
351 int kernel_disable_event(struct ltt_kernel_event
*event
)
357 ret
= kernctl_disable(event
->fd
);
361 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
364 PERROR("disable kernel event");
371 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
380 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
382 DBG("Kernel track PID %d for session id %" PRIu64
".",
384 return kernctl_track_pid(session
->fd
, pid
);
387 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
389 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
391 return kernctl_untrack_pid(session
->fd
, pid
);
394 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
399 ssize_t nbmem
, count
= 0;
403 fd
= kernctl_list_tracker_pids(session
->fd
);
405 PERROR("kernel tracker pids list");
409 fp
= fdopen(fd
, "r");
411 PERROR("kernel tracker pids list fdopen");
415 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
416 pids
= zmalloc(sizeof(*pids
) * nbmem
);
418 PERROR("alloc list pids");
423 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
424 if (count
>= nbmem
) {
428 new_nbmem
= nbmem
<< 1;
429 DBG("Reallocating pids list from %zu to %zu entries",
431 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
432 if (new_pids
== NULL
) {
433 PERROR("realloc list events");
438 /* Zero the new memory */
439 memset(new_pids
+ nbmem
, 0,
440 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
448 DBG("Kernel list tracker pids done (%zd pids)", count
);
450 ret
= fclose(fp
); /* closes both fp and fd */
466 * Create kernel metadata, open from the kernel tracer and add it to the
469 int kernel_open_metadata(struct ltt_kernel_session
*session
)
472 struct ltt_kernel_metadata
*lkm
= NULL
;
476 /* Allocate kernel metadata */
477 lkm
= trace_kernel_create_metadata();
482 /* Kernel tracer metadata creation */
483 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
489 /* Prevent fd duplication after execlp() */
490 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
492 PERROR("fcntl session fd");
495 session
->metadata
= lkm
;
497 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
502 trace_kernel_destroy_metadata(lkm
);
508 * Start tracing session.
510 int kernel_start_session(struct ltt_kernel_session
*session
)
516 ret
= kernctl_start_session(session
->fd
);
518 PERROR("ioctl start session");
522 DBG("Kernel session started");
531 * Make a kernel wait to make sure in-flight probe have completed.
533 void kernel_wait_quiescent(int fd
)
537 DBG("Kernel quiescent wait on %d", fd
);
539 ret
= kernctl_wait_quiescent(fd
);
541 PERROR("wait quiescent ioctl");
542 ERR("Kernel quiescent wait failed");
549 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
555 ret
= kernctl_calibrate(fd
, calibrate
);
557 PERROR("calibrate ioctl");
566 * Force flush buffer of metadata.
568 int kernel_metadata_flush_buffer(int fd
)
572 DBG("Kernel flushing metadata buffer on fd %d", fd
);
574 ret
= kernctl_buffer_flush(fd
);
576 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
583 * Force flush buffer for channel.
585 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
588 struct ltt_kernel_stream
*stream
;
592 DBG("Flush buffer for channel %s", channel
->channel
->name
);
594 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
595 DBG("Flushing channel stream %d", stream
->fd
);
596 ret
= kernctl_buffer_flush(stream
->fd
);
599 ERR("Fail to flush buffer for stream %d (ret: %d)",
608 * Stop tracing session.
610 int kernel_stop_session(struct ltt_kernel_session
*session
)
616 ret
= kernctl_stop_session(session
->fd
);
621 DBG("Kernel session stopped");
630 * Open stream of channel, register it to the kernel tracer and add it
631 * to the stream list of the channel.
633 * Return the number of created stream. Else, a negative value.
635 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
638 struct ltt_kernel_stream
*lks
;
642 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
643 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
653 /* Prevent fd duplication after execlp() */
654 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
656 PERROR("fcntl session fd");
659 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
660 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
662 /* Add stream to channe stream list */
663 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
664 channel
->stream_count
++;
666 /* Increment counter which represent CPU number. */
669 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
673 return channel
->stream_count
;
680 * Open the metadata stream and set it to the kernel session.
682 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
688 ret
= kernctl_create_stream(session
->metadata
->fd
);
690 PERROR("kernel create metadata stream");
694 DBG("Kernel metadata stream created (fd: %d)", ret
);
695 session
->metadata_stream_fd
= ret
;
696 /* Prevent fd duplication after execlp() */
697 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
699 PERROR("fcntl session fd");
709 * Get the event list from the kernel tracer and return the number of elements.
711 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
715 size_t nbmem
, count
= 0;
717 struct lttng_event
*elist
;
721 fd
= kernctl_tracepoint_list(tracer_fd
);
723 PERROR("kernel tracepoint list");
727 fp
= fdopen(fd
, "r");
729 PERROR("kernel tracepoint list fdopen");
734 * Init memory size counter
735 * See kernel-ctl.h for explanation of this value
737 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
738 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
740 PERROR("alloc list events");
745 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
746 if (count
>= nbmem
) {
747 struct lttng_event
*new_elist
;
750 new_nbmem
= nbmem
<< 1;
751 DBG("Reallocating event list from %zu to %zu bytes",
753 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
754 if (new_elist
== NULL
) {
755 PERROR("realloc list events");
761 /* Zero the new memory */
762 memset(new_elist
+ nbmem
, 0,
763 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
767 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
768 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
769 elist
[count
].enabled
= -1;
775 DBG("Kernel list events done (%zu events)", count
);
777 ret
= fclose(fp
); /* closes both fp and fd */
793 * Get kernel version and validate it.
795 int kernel_validate_version(int tracer_fd
)
798 struct lttng_kernel_tracer_version version
;
799 struct lttng_kernel_tracer_abi_version abi_version
;
801 ret
= kernctl_tracer_version(tracer_fd
, &version
);
803 ERR("Failed at getting the lttng-modules version");
807 /* Validate version */
808 if (version
.major
!= VERSION_MAJOR
) {
809 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
810 version
.major
, VERSION_MAJOR
);
813 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
815 ERR("Failed at getting lttng-modules ABI version");
818 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
819 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
820 abi_version
.major
, abi_version
.minor
,
821 LTTNG_MODULES_ABI_MAJOR_VERSION
);
824 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
825 version
.major
, version
.minor
,
826 abi_version
.major
, abi_version
.minor
);
837 * Kernel work-arounds called at the start of sessiond main().
839 int init_kernel_workarounds(void)
845 * boot_id needs to be read once before being used concurrently
846 * to deal with a Linux kernel race. A fix is proposed for
847 * upstream, but the work-around is needed for older kernels.
849 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
856 ret
= fread(buf
, 1, sizeof(buf
), fp
);
858 /* Ignore error, we don't really care */
870 * Complete teardown of a kernel session.
872 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
875 DBG3("No kernel session when tearing down session");
879 DBG("Tearing down kernel session");
882 * Destroy channels on the consumer if at least one FD has been sent and we
883 * are in no output mode because the streams are in *no* monitor mode so we
884 * have to send a command to clean them up or else they leaked.
886 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
888 struct consumer_socket
*socket
;
889 struct lttng_ht_iter iter
;
891 /* For each consumer socket. */
893 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
895 struct ltt_kernel_channel
*chan
;
897 /* For each channel, ask the consumer to destroy it. */
898 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
899 ret
= kernel_consumer_destroy_channel(socket
, chan
);
901 /* Consumer is probably dead. Use next socket. */
909 /* Close any relayd session */
910 consumer_output_send_destroy_relayd(ksess
->consumer
);
912 trace_kernel_destroy_session(ksess
);
916 * Destroy a kernel channel object. It does not do anything on the tracer side.
918 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
920 struct ltt_kernel_session
*ksess
= NULL
;
923 assert(kchan
->channel
);
925 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
927 /* Update channel count of associated session. */
928 if (kchan
->session
) {
929 /* Keep pointer reference so we can update it after the destroy. */
930 ksess
= kchan
->session
;
933 trace_kernel_destroy_channel(kchan
);
936 * At this point the kernel channel is not visible anymore. This is safe
937 * since in order to work on a visible kernel session, the tracing session
938 * lock (ltt_session.lock) MUST be acquired.
941 ksess
->channel_count
--;
946 * Take a snapshot for a given kernel session.
948 * Return 0 on success or else return a LTTNG_ERR code.
950 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
951 struct snapshot_output
*output
, int wait
,
952 uint64_t nb_packets_per_stream
)
954 int err
, ret
, saved_metadata_fd
;
955 struct consumer_socket
*socket
;
956 struct lttng_ht_iter iter
;
957 struct ltt_kernel_metadata
*saved_metadata
;
960 assert(ksess
->consumer
);
963 DBG("Kernel snapshot record started");
965 /* Save current metadata since the following calls will change it. */
966 saved_metadata
= ksess
->metadata
;
967 saved_metadata_fd
= ksess
->metadata_stream_fd
;
971 ret
= kernel_open_metadata(ksess
);
973 ret
= LTTNG_ERR_KERN_META_FAIL
;
977 ret
= kernel_open_metadata_stream(ksess
);
979 ret
= LTTNG_ERR_KERN_META_FAIL
;
980 goto error_open_stream
;
983 /* Send metadata to consumer and snapshot everything. */
984 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
986 struct consumer_output
*saved_output
;
987 struct ltt_kernel_channel
*chan
;
990 * Temporarly switch consumer output for our snapshot output. As long
991 * as the session lock is taken, this is safe.
993 saved_output
= ksess
->consumer
;
994 ksess
->consumer
= output
->consumer
;
996 pthread_mutex_lock(socket
->lock
);
997 /* This stream must not be monitored by the consumer. */
998 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
999 pthread_mutex_unlock(socket
->lock
);
1000 /* Put back the saved consumer output into the session. */
1001 ksess
->consumer
= saved_output
;
1003 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1004 goto error_consumer
;
1007 /* For each channel, ask the consumer to snapshot it. */
1008 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1009 pthread_mutex_lock(socket
->lock
);
1010 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1011 ksess
->uid
, ksess
->gid
,
1012 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1013 nb_packets_per_stream
);
1014 pthread_mutex_unlock(socket
->lock
);
1016 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1017 (void) kernel_consumer_destroy_metadata(socket
,
1019 goto error_consumer
;
1023 /* Snapshot metadata, */
1024 pthread_mutex_lock(socket
->lock
);
1025 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1026 1, ksess
->uid
, ksess
->gid
,
1027 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1028 pthread_mutex_unlock(socket
->lock
);
1030 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1031 goto error_consumer
;
1035 * The metadata snapshot is done, ask the consumer to destroy it since
1036 * it's not monitored on the consumer side.
1038 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1044 /* Close newly opened metadata stream. It's now on the consumer side. */
1045 err
= close(ksess
->metadata_stream_fd
);
1047 PERROR("close snapshot kernel");
1051 trace_kernel_destroy_metadata(ksess
->metadata
);
1053 /* Restore metadata state.*/
1054 ksess
->metadata
= saved_metadata
;
1055 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1062 * Get the syscall mask array from the kernel tracer.
1064 * Return 0 on success else a negative value. In both case, syscall_mask should
1067 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1069 assert(syscall_mask
);
1072 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);