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
)
183 struct ltt_kernel_event
*event
;
188 event
= trace_kernel_create_event(ev
);
194 ret
= kernctl_create_event(channel
->fd
, event
->event
);
200 WARN("Event type not implemented");
203 WARN("Event %s not found!", ev
->name
);
206 PERROR("create event ioctl");
212 event
->type
= ev
->type
;
214 /* Prevent fd duplication after execlp() */
215 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
217 PERROR("fcntl session fd");
220 /* Add event to event list */
221 cds_list_add(&event
->list
, &channel
->events_list
.head
);
222 channel
->event_count
++;
224 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
235 * Disable a kernel channel.
237 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
243 ret
= kernctl_disable(chan
->fd
);
245 PERROR("disable chan ioctl");
251 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
260 * Enable a kernel channel.
262 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
268 ret
= kernctl_enable(chan
->fd
);
269 if (ret
< 0 && errno
!= EEXIST
) {
270 PERROR("Enable kernel chan");
275 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
284 * Enable a kernel event.
286 int kernel_enable_event(struct ltt_kernel_event
*event
)
292 ret
= kernctl_enable(event
->fd
);
296 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
299 PERROR("enable kernel event");
306 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
315 * Disable a kernel event.
317 int kernel_disable_event(struct ltt_kernel_event
*event
)
323 ret
= kernctl_disable(event
->fd
);
327 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
330 PERROR("disable kernel event");
337 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
346 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
348 DBG("Kernel track PID %d for session id %" PRIu64
".",
350 return kernctl_track_pid(session
->fd
, pid
);
353 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
355 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
357 return kernctl_untrack_pid(session
->fd
, pid
);
360 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
365 ssize_t nbmem
, count
= 0;
369 fd
= kernctl_list_tracker_pids(session
->fd
);
371 PERROR("kernel tracker pids list");
375 fp
= fdopen(fd
, "r");
377 PERROR("kernel tracker pids list fdopen");
381 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
382 pids
= zmalloc(sizeof(*pids
) * nbmem
);
384 PERROR("alloc list pids");
389 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
390 if (count
>= nbmem
) {
394 new_nbmem
= nbmem
<< 1;
395 DBG("Reallocating pids list from %zu to %zu entries",
397 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
398 if (new_pids
== NULL
) {
399 PERROR("realloc list events");
404 /* Zero the new memory */
405 memset(new_pids
+ nbmem
, 0,
406 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
414 DBG("Kernel list tracker pids done (%zd pids)", count
);
416 ret
= fclose(fp
); /* closes both fp and fd */
432 * Create kernel metadata, open from the kernel tracer and add it to the
435 int kernel_open_metadata(struct ltt_kernel_session
*session
)
438 struct ltt_kernel_metadata
*lkm
= NULL
;
442 /* Allocate kernel metadata */
443 lkm
= trace_kernel_create_metadata();
448 /* Kernel tracer metadata creation */
449 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
455 /* Prevent fd duplication after execlp() */
456 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
458 PERROR("fcntl session fd");
461 session
->metadata
= lkm
;
463 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
468 trace_kernel_destroy_metadata(lkm
);
474 * Start tracing session.
476 int kernel_start_session(struct ltt_kernel_session
*session
)
482 ret
= kernctl_start_session(session
->fd
);
484 PERROR("ioctl start session");
488 DBG("Kernel session started");
497 * Make a kernel wait to make sure in-flight probe have completed.
499 void kernel_wait_quiescent(int fd
)
503 DBG("Kernel quiescent wait on %d", fd
);
505 ret
= kernctl_wait_quiescent(fd
);
507 PERROR("wait quiescent ioctl");
508 ERR("Kernel quiescent wait failed");
515 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
521 ret
= kernctl_calibrate(fd
, calibrate
);
523 PERROR("calibrate ioctl");
532 * Force flush buffer of metadata.
534 int kernel_metadata_flush_buffer(int fd
)
538 DBG("Kernel flushing metadata buffer on fd %d", fd
);
540 ret
= kernctl_buffer_flush(fd
);
542 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
549 * Force flush buffer for channel.
551 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
554 struct ltt_kernel_stream
*stream
;
558 DBG("Flush buffer for channel %s", channel
->channel
->name
);
560 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
561 DBG("Flushing channel stream %d", stream
->fd
);
562 ret
= kernctl_buffer_flush(stream
->fd
);
565 ERR("Fail to flush buffer for stream %d (ret: %d)",
574 * Stop tracing session.
576 int kernel_stop_session(struct ltt_kernel_session
*session
)
582 ret
= kernctl_stop_session(session
->fd
);
587 DBG("Kernel session stopped");
596 * Open stream of channel, register it to the kernel tracer and add it
597 * to the stream list of the channel.
599 * Return the number of created stream. Else, a negative value.
601 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
604 struct ltt_kernel_stream
*lks
;
608 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
609 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
619 /* Prevent fd duplication after execlp() */
620 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
622 PERROR("fcntl session fd");
625 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
626 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
628 /* Add stream to channe stream list */
629 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
630 channel
->stream_count
++;
632 /* Increment counter which represent CPU number. */
635 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
639 return channel
->stream_count
;
646 * Open the metadata stream and set it to the kernel session.
648 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
654 ret
= kernctl_create_stream(session
->metadata
->fd
);
656 PERROR("kernel create metadata stream");
660 DBG("Kernel metadata stream created (fd: %d)", ret
);
661 session
->metadata_stream_fd
= ret
;
662 /* Prevent fd duplication after execlp() */
663 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
665 PERROR("fcntl session fd");
675 * Get the event list from the kernel tracer and return the number of elements.
677 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
681 size_t nbmem
, count
= 0;
683 struct lttng_event
*elist
;
687 fd
= kernctl_tracepoint_list(tracer_fd
);
689 PERROR("kernel tracepoint list");
693 fp
= fdopen(fd
, "r");
695 PERROR("kernel tracepoint list fdopen");
700 * Init memory size counter
701 * See kernel-ctl.h for explanation of this value
703 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
704 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
706 PERROR("alloc list events");
711 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
712 if (count
>= nbmem
) {
713 struct lttng_event
*new_elist
;
716 new_nbmem
= nbmem
<< 1;
717 DBG("Reallocating event list from %zu to %zu bytes",
719 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
720 if (new_elist
== NULL
) {
721 PERROR("realloc list events");
727 /* Zero the new memory */
728 memset(new_elist
+ nbmem
, 0,
729 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
733 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
734 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
735 elist
[count
].enabled
= -1;
741 DBG("Kernel list events done (%zu events)", count
);
743 ret
= fclose(fp
); /* closes both fp and fd */
759 * Get kernel version and validate it.
761 int kernel_validate_version(int tracer_fd
)
764 struct lttng_kernel_tracer_version version
;
765 struct lttng_kernel_tracer_abi_version abi_version
;
767 ret
= kernctl_tracer_version(tracer_fd
, &version
);
769 ERR("Failed at getting the lttng-modules version");
773 /* Validate version */
774 if (version
.major
!= VERSION_MAJOR
) {
775 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
776 version
.major
, VERSION_MAJOR
);
779 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
781 ERR("Failed at getting lttng-modules ABI version");
784 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
785 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
786 abi_version
.major
, abi_version
.minor
,
787 LTTNG_MODULES_ABI_MAJOR_VERSION
);
790 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
791 version
.major
, version
.minor
,
792 abi_version
.major
, abi_version
.minor
);
803 * Kernel work-arounds called at the start of sessiond main().
805 int init_kernel_workarounds(void)
811 * boot_id needs to be read once before being used concurrently
812 * to deal with a Linux kernel race. A fix is proposed for
813 * upstream, but the work-around is needed for older kernels.
815 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
822 ret
= fread(buf
, 1, sizeof(buf
), fp
);
824 /* Ignore error, we don't really care */
836 * Complete teardown of a kernel session.
838 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
841 DBG3("No kernel session when tearing down session");
845 DBG("Tearing down kernel session");
848 * Destroy channels on the consumer if at least one FD has been sent and we
849 * are in no output mode because the streams are in *no* monitor mode so we
850 * have to send a command to clean them up or else they leaked.
852 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
854 struct consumer_socket
*socket
;
855 struct lttng_ht_iter iter
;
857 /* For each consumer socket. */
859 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
861 struct ltt_kernel_channel
*chan
;
863 /* For each channel, ask the consumer to destroy it. */
864 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
865 ret
= kernel_consumer_destroy_channel(socket
, chan
);
867 /* Consumer is probably dead. Use next socket. */
875 /* Close any relayd session */
876 consumer_output_send_destroy_relayd(ksess
->consumer
);
878 trace_kernel_destroy_session(ksess
);
882 * Destroy a kernel channel object. It does not do anything on the tracer side.
884 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
886 struct ltt_kernel_session
*ksess
= NULL
;
889 assert(kchan
->channel
);
891 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
893 /* Update channel count of associated session. */
894 if (kchan
->session
) {
895 /* Keep pointer reference so we can update it after the destroy. */
896 ksess
= kchan
->session
;
899 trace_kernel_destroy_channel(kchan
);
902 * At this point the kernel channel is not visible anymore. This is safe
903 * since in order to work on a visible kernel session, the tracing session
904 * lock (ltt_session.lock) MUST be acquired.
907 ksess
->channel_count
--;
912 * Take a snapshot for a given kernel session.
914 * Return 0 on success or else return a LTTNG_ERR code.
916 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
917 struct snapshot_output
*output
, int wait
,
918 uint64_t nb_packets_per_stream
)
920 int err
, ret
, saved_metadata_fd
;
921 struct consumer_socket
*socket
;
922 struct lttng_ht_iter iter
;
923 struct ltt_kernel_metadata
*saved_metadata
;
926 assert(ksess
->consumer
);
929 DBG("Kernel snapshot record started");
931 /* Save current metadata since the following calls will change it. */
932 saved_metadata
= ksess
->metadata
;
933 saved_metadata_fd
= ksess
->metadata_stream_fd
;
937 ret
= kernel_open_metadata(ksess
);
939 ret
= LTTNG_ERR_KERN_META_FAIL
;
943 ret
= kernel_open_metadata_stream(ksess
);
945 ret
= LTTNG_ERR_KERN_META_FAIL
;
946 goto error_open_stream
;
949 /* Send metadata to consumer and snapshot everything. */
950 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
952 struct consumer_output
*saved_output
;
953 struct ltt_kernel_channel
*chan
;
956 * Temporarly switch consumer output for our snapshot output. As long
957 * as the session lock is taken, this is safe.
959 saved_output
= ksess
->consumer
;
960 ksess
->consumer
= output
->consumer
;
962 pthread_mutex_lock(socket
->lock
);
963 /* This stream must not be monitored by the consumer. */
964 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
965 pthread_mutex_unlock(socket
->lock
);
966 /* Put back the saved consumer output into the session. */
967 ksess
->consumer
= saved_output
;
969 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
973 /* For each channel, ask the consumer to snapshot it. */
974 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
975 pthread_mutex_lock(socket
->lock
);
976 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
977 ksess
->uid
, ksess
->gid
,
978 DEFAULT_KERNEL_TRACE_DIR
, wait
,
979 nb_packets_per_stream
);
980 pthread_mutex_unlock(socket
->lock
);
982 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
983 (void) kernel_consumer_destroy_metadata(socket
,
989 /* Snapshot metadata, */
990 pthread_mutex_lock(socket
->lock
);
991 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
992 1, ksess
->uid
, ksess
->gid
,
993 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
994 pthread_mutex_unlock(socket
->lock
);
996 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1001 * The metadata snapshot is done, ask the consumer to destroy it since
1002 * it's not monitored on the consumer side.
1004 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1010 /* Close newly opened metadata stream. It's now on the consumer side. */
1011 err
= close(ksess
->metadata_stream_fd
);
1013 PERROR("close snapshot kernel");
1017 trace_kernel_destroy_metadata(ksess
->metadata
);
1019 /* Restore metadata state.*/
1020 ksess
->metadata
= saved_metadata
;
1021 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1028 * Get the syscall mask array from the kernel tracer.
1030 * Return 0 on success else a negative value. In both case, syscall_mask should
1033 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1035 assert(syscall_mask
);
1038 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);