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.
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/kernel-ctl/kernel-ioctl.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 * Assumes the ownership of ctx.
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
);
55 /* Exists but not available for this kernel */
56 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
59 /* If EEXIST, we just ignore the error */
63 PERROR("add context ioctl");
64 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
70 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
75 trace_kernel_destroy_context(ctx
);
81 * Create a new kernel session, register it to the kernel tracer and add it to
82 * the session daemon session.
84 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
87 struct ltt_kernel_session
*lks
;
91 /* Allocate data structure */
92 lks
= trace_kernel_create_session();
98 /* Kernel tracer session creation */
99 ret
= kernctl_create_session(tracer_fd
);
101 PERROR("ioctl kernel create session");
106 /* Prevent fd duplication after execlp() */
107 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
109 PERROR("fcntl session fd");
112 lks
->id
= session
->id
;
113 lks
->consumer_fds_sent
= 0;
114 session
->kernel_session
= lks
;
116 DBG("Kernel session created (fd: %d)", lks
->fd
);
122 trace_kernel_destroy_session(lks
);
128 * Create a kernel channel, register it to the kernel tracer and add it to the
131 int kernel_create_channel(struct ltt_kernel_session
*session
,
132 struct lttng_channel
*chan
)
135 struct ltt_kernel_channel
*lkc
;
140 /* Allocate kernel channel */
141 lkc
= trace_kernel_create_channel(chan
);
146 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
147 chan
->name
, lkc
->channel
->attr
.overwrite
,
148 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
149 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
150 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
152 /* Kernel tracer channel creation */
153 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
155 PERROR("ioctl kernel create channel");
159 /* Setup the channel fd */
161 /* Prevent fd duplication after execlp() */
162 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
164 PERROR("fcntl session fd");
167 /* Add channel to session */
168 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
169 session
->channel_count
++;
170 lkc
->session
= session
;
172 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
185 * Create a kernel event, enable it to the kernel tracer and add it to the
186 * channel event list of the kernel session.
187 * We own filter_expression and filter.
189 int kernel_create_event(struct lttng_event
*ev
,
190 struct ltt_kernel_channel
*channel
,
191 char *filter_expression
,
192 struct lttng_filter_bytecode
*filter
)
195 struct ltt_kernel_event
*event
;
200 /* We pass ownership of filter_expression and filter */
201 event
= trace_kernel_create_event(ev
, filter_expression
,
208 ret
= kernctl_create_event(channel
->fd
, event
->event
);
214 WARN("Event type not implemented");
217 WARN("Event %s not found!", ev
->name
);
220 PERROR("create event ioctl");
225 event
->type
= ev
->type
;
227 /* Prevent fd duplication after execlp() */
228 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
230 PERROR("fcntl session fd");
234 ret
= kernctl_filter(event
->fd
, filter
);
240 ret
= kernctl_enable(event
->fd
);
244 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
247 PERROR("enable kernel event");
253 /* Add event to event list */
254 cds_list_add(&event
->list
, &channel
->events_list
.head
);
255 channel
->event_count
++;
257 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
266 closeret
= close(event
->fd
);
268 PERROR("close event fd");
278 * Disable a kernel channel.
280 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
286 ret
= kernctl_disable(chan
->fd
);
288 PERROR("disable chan ioctl");
293 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
302 * Enable a kernel channel.
304 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
310 ret
= kernctl_enable(chan
->fd
);
311 if (ret
< 0 && ret
!= -EEXIST
) {
312 PERROR("Enable kernel chan");
317 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
326 * Enable a kernel event.
328 int kernel_enable_event(struct ltt_kernel_event
*event
)
334 ret
= kernctl_enable(event
->fd
);
338 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
341 PERROR("enable kernel event");
348 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
357 * Disable a kernel event.
359 int kernel_disable_event(struct ltt_kernel_event
*event
)
365 ret
= kernctl_disable(event
->fd
);
369 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
372 PERROR("disable kernel event");
379 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
388 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
392 DBG("Kernel track PID %d for session id %" PRIu64
".",
394 ret
= kernctl_track_pid(session
->fd
, pid
);
400 return LTTNG_ERR_INVALID
;
402 return LTTNG_ERR_NOMEM
;
404 return LTTNG_ERR_PID_TRACKED
;
406 return LTTNG_ERR_UNK
;
410 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
414 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
416 ret
= kernctl_untrack_pid(session
->fd
, pid
);
422 return LTTNG_ERR_INVALID
;
424 return LTTNG_ERR_NOMEM
;
426 return LTTNG_ERR_PID_NOT_TRACKED
;
428 return LTTNG_ERR_UNK
;
432 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
437 ssize_t nbmem
, count
= 0;
441 fd
= kernctl_list_tracker_pids(session
->fd
);
443 PERROR("kernel tracker pids list");
447 fp
= fdopen(fd
, "r");
449 PERROR("kernel tracker pids list fdopen");
453 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
454 pids
= zmalloc(sizeof(*pids
) * nbmem
);
456 PERROR("alloc list pids");
461 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
462 if (count
>= nbmem
) {
466 new_nbmem
= nbmem
<< 1;
467 DBG("Reallocating pids list from %zu to %zu entries",
469 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
470 if (new_pids
== NULL
) {
471 PERROR("realloc list events");
476 /* Zero the new memory */
477 memset(new_pids
+ nbmem
, 0,
478 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
486 DBG("Kernel list tracker pids done (%zd pids)", count
);
488 ret
= fclose(fp
); /* closes both fp and fd */
504 * Create kernel metadata, open from the kernel tracer and add it to the
507 int kernel_open_metadata(struct ltt_kernel_session
*session
)
510 struct ltt_kernel_metadata
*lkm
= NULL
;
514 /* Allocate kernel metadata */
515 lkm
= trace_kernel_create_metadata();
520 /* Kernel tracer metadata creation */
521 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
527 /* Prevent fd duplication after execlp() */
528 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
530 PERROR("fcntl session fd");
533 session
->metadata
= lkm
;
535 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
540 trace_kernel_destroy_metadata(lkm
);
546 * Start tracing session.
548 int kernel_start_session(struct ltt_kernel_session
*session
)
554 ret
= kernctl_start_session(session
->fd
);
556 PERROR("ioctl start session");
560 DBG("Kernel session started");
569 * Make a kernel wait to make sure in-flight probe have completed.
571 void kernel_wait_quiescent(int fd
)
575 DBG("Kernel quiescent wait on %d", fd
);
577 ret
= kernctl_wait_quiescent(fd
);
579 PERROR("wait quiescent ioctl");
580 ERR("Kernel quiescent wait failed");
585 * Force flush buffer of metadata.
587 int kernel_metadata_flush_buffer(int fd
)
591 DBG("Kernel flushing metadata buffer on fd %d", fd
);
593 ret
= kernctl_buffer_flush(fd
);
595 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
602 * Force flush buffer for channel.
604 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
607 struct ltt_kernel_stream
*stream
;
611 DBG("Flush buffer for channel %s", channel
->channel
->name
);
613 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
614 DBG("Flushing channel stream %d", stream
->fd
);
615 ret
= kernctl_buffer_flush(stream
->fd
);
618 ERR("Fail to flush buffer for stream %d (ret: %d)",
627 * Stop tracing session.
629 int kernel_stop_session(struct ltt_kernel_session
*session
)
635 ret
= kernctl_stop_session(session
->fd
);
640 DBG("Kernel session stopped");
649 * Open stream of channel, register it to the kernel tracer and add it
650 * to the stream list of the channel.
652 * Note: given that the streams may appear in random order wrt CPU
653 * number (e.g. cpu hotplug), the index value of the stream number in
654 * the stream name is not necessarily linked to the CPU number.
656 * Return the number of created stream. Else, a negative value.
658 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
661 struct ltt_kernel_stream
*lks
;
665 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
666 lks
= trace_kernel_create_stream(channel
->channel
->name
,
667 channel
->stream_count
);
677 /* Prevent fd duplication after execlp() */
678 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
680 PERROR("fcntl session fd");
683 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
684 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
686 /* Add stream to channel stream list */
687 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
688 channel
->stream_count
++;
690 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
694 return channel
->stream_count
;
701 * Open the metadata stream and set it to the kernel session.
703 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
709 ret
= kernctl_create_stream(session
->metadata
->fd
);
711 PERROR("kernel create metadata stream");
715 DBG("Kernel metadata stream created (fd: %d)", ret
);
716 session
->metadata_stream_fd
= ret
;
717 /* Prevent fd duplication after execlp() */
718 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
720 PERROR("fcntl session fd");
730 * Get the event list from the kernel tracer and return the number of elements.
732 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
736 size_t nbmem
, count
= 0;
738 struct lttng_event
*elist
;
742 fd
= kernctl_tracepoint_list(tracer_fd
);
744 PERROR("kernel tracepoint list");
748 fp
= fdopen(fd
, "r");
750 PERROR("kernel tracepoint list fdopen");
755 * Init memory size counter
756 * See kernel-ctl.h for explanation of this value
758 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
759 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
761 PERROR("alloc list events");
766 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
767 if (count
>= nbmem
) {
768 struct lttng_event
*new_elist
;
771 new_nbmem
= nbmem
<< 1;
772 DBG("Reallocating event list from %zu to %zu bytes",
774 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
775 if (new_elist
== NULL
) {
776 PERROR("realloc list events");
782 /* Zero the new memory */
783 memset(new_elist
+ nbmem
, 0,
784 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
788 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
789 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
790 elist
[count
].enabled
= -1;
796 DBG("Kernel list events done (%zu events)", count
);
798 ret
= fclose(fp
); /* closes both fp and fd */
814 * Get kernel version and validate it.
816 int kernel_validate_version(int tracer_fd
)
819 struct lttng_kernel_tracer_version version
;
820 struct lttng_kernel_tracer_abi_version abi_version
;
822 ret
= kernctl_tracer_version(tracer_fd
, &version
);
824 ERR("Failed to retrieve the lttng-modules version");
828 /* Validate version */
829 if (version
.major
!= VERSION_MAJOR
) {
830 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
831 version
.major
, VERSION_MAJOR
);
834 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
836 ERR("Failed to retrieve lttng-modules ABI version");
839 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
840 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
841 abi_version
.major
, abi_version
.minor
,
842 LTTNG_MODULES_ABI_MAJOR_VERSION
);
845 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
846 version
.major
, version
.minor
,
847 abi_version
.major
, abi_version
.minor
);
854 ERR("Kernel tracer version check failed; kernel tracing will not be available");
859 * Kernel work-arounds called at the start of sessiond main().
861 int init_kernel_workarounds(void)
867 * boot_id needs to be read once before being used concurrently
868 * to deal with a Linux kernel race. A fix is proposed for
869 * upstream, but the work-around is needed for older kernels.
871 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
878 ret
= fread(buf
, 1, sizeof(buf
), fp
);
880 /* Ignore error, we don't really care */
892 * Complete teardown of a kernel session.
894 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
897 DBG3("No kernel session when tearing down session");
901 DBG("Tearing down kernel session");
904 * Destroy channels on the consumer if at least one FD has been sent and we
905 * are in no output mode because the streams are in *no* monitor mode so we
906 * have to send a command to clean them up or else they leaked.
908 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
910 struct consumer_socket
*socket
;
911 struct lttng_ht_iter iter
;
913 /* For each consumer socket. */
915 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
917 struct ltt_kernel_channel
*chan
;
919 /* For each channel, ask the consumer to destroy it. */
920 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
921 ret
= kernel_consumer_destroy_channel(socket
, chan
);
923 /* Consumer is probably dead. Use next socket. */
931 /* Close any relayd session */
932 consumer_output_send_destroy_relayd(ksess
->consumer
);
934 trace_kernel_destroy_session(ksess
);
938 * Destroy a kernel channel object. It does not do anything on the tracer side.
940 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
942 struct ltt_kernel_session
*ksess
= NULL
;
945 assert(kchan
->channel
);
947 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
949 /* Update channel count of associated session. */
950 if (kchan
->session
) {
951 /* Keep pointer reference so we can update it after the destroy. */
952 ksess
= kchan
->session
;
955 trace_kernel_destroy_channel(kchan
);
958 * At this point the kernel channel is not visible anymore. This is safe
959 * since in order to work on a visible kernel session, the tracing session
960 * lock (ltt_session.lock) MUST be acquired.
963 ksess
->channel_count
--;
968 * Take a snapshot for a given kernel session.
970 * Return 0 on success or else return a LTTNG_ERR code.
972 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
973 struct snapshot_output
*output
, int wait
,
974 uint64_t nb_packets_per_stream
)
976 int err
, ret
, saved_metadata_fd
;
977 struct consumer_socket
*socket
;
978 struct lttng_ht_iter iter
;
979 struct ltt_kernel_metadata
*saved_metadata
;
982 assert(ksess
->consumer
);
985 DBG("Kernel snapshot record started");
987 /* Save current metadata since the following calls will change it. */
988 saved_metadata
= ksess
->metadata
;
989 saved_metadata_fd
= ksess
->metadata_stream_fd
;
993 ret
= kernel_open_metadata(ksess
);
995 ret
= LTTNG_ERR_KERN_META_FAIL
;
999 ret
= kernel_open_metadata_stream(ksess
);
1001 ret
= LTTNG_ERR_KERN_META_FAIL
;
1002 goto error_open_stream
;
1005 /* Send metadata to consumer and snapshot everything. */
1006 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1007 socket
, node
.node
) {
1008 struct consumer_output
*saved_output
;
1009 struct ltt_kernel_channel
*chan
;
1012 * Temporarly switch consumer output for our snapshot output. As long
1013 * as the session lock is taken, this is safe.
1015 saved_output
= ksess
->consumer
;
1016 ksess
->consumer
= output
->consumer
;
1018 pthread_mutex_lock(socket
->lock
);
1019 /* This stream must not be monitored by the consumer. */
1020 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1021 pthread_mutex_unlock(socket
->lock
);
1022 /* Put back the saved consumer output into the session. */
1023 ksess
->consumer
= saved_output
;
1025 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1026 goto error_consumer
;
1029 /* For each channel, ask the consumer to snapshot it. */
1030 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1031 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1032 ksess
->uid
, ksess
->gid
,
1033 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1034 nb_packets_per_stream
);
1036 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1037 (void) kernel_consumer_destroy_metadata(socket
,
1039 goto error_consumer
;
1043 /* Snapshot metadata, */
1044 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1045 1, ksess
->uid
, ksess
->gid
,
1046 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1048 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1049 goto error_consumer
;
1053 * The metadata snapshot is done, ask the consumer to destroy it since
1054 * it's not monitored on the consumer side.
1056 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1062 /* Close newly opened metadata stream. It's now on the consumer side. */
1063 err
= close(ksess
->metadata_stream_fd
);
1065 PERROR("close snapshot kernel");
1069 trace_kernel_destroy_metadata(ksess
->metadata
);
1071 /* Restore metadata state.*/
1072 ksess
->metadata
= saved_metadata
;
1073 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1080 * Get the syscall mask array from the kernel tracer.
1082 * Return 0 on success else a negative value. In both case, syscall_mask should
1085 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1087 assert(syscall_mask
);
1090 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);