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 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
);
53 /* Exists but not available for this kernel */
54 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
57 /* If EEXIST, we just ignore the error */
61 PERROR("add context ioctl");
62 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
68 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
76 * Create a new kernel session, register it to the kernel tracer and add it to
77 * the session daemon session.
79 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
82 struct ltt_kernel_session
*lks
;
86 /* Allocate data structure */
87 lks
= trace_kernel_create_session();
93 /* Kernel tracer session creation */
94 ret
= kernctl_create_session(tracer_fd
);
96 PERROR("ioctl kernel create session");
101 /* Prevent fd duplication after execlp() */
102 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
104 PERROR("fcntl session fd");
107 lks
->id
= session
->id
;
108 lks
->consumer_fds_sent
= 0;
109 session
->kernel_session
= lks
;
111 DBG("Kernel session created (fd: %d)", lks
->fd
);
117 trace_kernel_destroy_session(lks
);
123 * Create a kernel channel, register it to the kernel tracer and add it to the
126 int kernel_create_channel(struct ltt_kernel_session
*session
,
127 struct lttng_channel
*chan
)
130 struct ltt_kernel_channel
*lkc
;
135 /* Allocate kernel channel */
136 lkc
= trace_kernel_create_channel(chan
);
141 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
142 chan
->name
, lkc
->channel
->attr
.overwrite
,
143 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
144 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
145 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
147 /* Kernel tracer channel creation */
148 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
150 PERROR("ioctl kernel create channel");
154 /* Setup the channel fd */
156 /* Prevent fd duplication after execlp() */
157 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
159 PERROR("fcntl session fd");
162 /* Add channel to session */
163 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
164 session
->channel_count
++;
165 lkc
->session
= session
;
167 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
180 * Create a kernel event, enable it to the kernel tracer and add it to the
181 * channel event list of the kernel session.
182 * We own filter_expression and filter.
184 int kernel_create_event(struct lttng_event
*ev
,
185 struct ltt_kernel_channel
*channel
,
186 char *filter_expression
,
187 struct lttng_filter_bytecode
*filter
)
190 struct ltt_kernel_event
*event
;
195 /* We pass ownership of filter_expression and filter */
196 event
= trace_kernel_create_event(ev
, filter_expression
,
203 ret
= kernctl_create_event(channel
->fd
, event
->event
);
209 WARN("Event type not implemented");
212 WARN("Event %s not found!", ev
->name
);
215 PERROR("create event ioctl");
220 event
->type
= ev
->type
;
222 /* Prevent fd duplication after execlp() */
223 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
225 PERROR("fcntl session fd");
229 ret
= kernctl_filter(event
->fd
, filter
);
235 ret
= kernctl_enable(event
->fd
);
239 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
242 PERROR("enable kernel event");
248 /* Add event to event list */
249 cds_list_add(&event
->list
, &channel
->events_list
.head
);
250 channel
->event_count
++;
252 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
261 closeret
= close(event
->fd
);
263 PERROR("close event fd");
273 * Disable a kernel channel.
275 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
281 ret
= kernctl_disable(chan
->fd
);
283 PERROR("disable chan ioctl");
288 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
297 * Enable a kernel channel.
299 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
305 ret
= kernctl_enable(chan
->fd
);
306 if (ret
< 0 && ret
!= -EEXIST
) {
307 PERROR("Enable kernel chan");
312 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
321 * Enable a kernel event.
323 int kernel_enable_event(struct ltt_kernel_event
*event
)
329 ret
= kernctl_enable(event
->fd
);
333 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
336 PERROR("enable kernel event");
343 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
352 * Disable a kernel event.
354 int kernel_disable_event(struct ltt_kernel_event
*event
)
360 ret
= kernctl_disable(event
->fd
);
364 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
367 PERROR("disable kernel event");
374 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
383 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
387 DBG("Kernel track PID %d for session id %" PRIu64
".",
389 ret
= kernctl_track_pid(session
->fd
, pid
);
395 return LTTNG_ERR_INVALID
;
397 return LTTNG_ERR_NOMEM
;
399 return LTTNG_ERR_PID_TRACKED
;
401 return LTTNG_ERR_UNK
;
405 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
409 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
411 ret
= kernctl_untrack_pid(session
->fd
, pid
);
417 return LTTNG_ERR_INVALID
;
419 return LTTNG_ERR_NOMEM
;
421 return LTTNG_ERR_PID_NOT_TRACKED
;
423 return LTTNG_ERR_UNK
;
427 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
432 ssize_t nbmem
, count
= 0;
436 fd
= kernctl_list_tracker_pids(session
->fd
);
438 PERROR("kernel tracker pids list");
442 fp
= fdopen(fd
, "r");
444 PERROR("kernel tracker pids list fdopen");
448 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
449 pids
= zmalloc(sizeof(*pids
) * nbmem
);
451 PERROR("alloc list pids");
456 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
457 if (count
>= nbmem
) {
461 new_nbmem
= nbmem
<< 1;
462 DBG("Reallocating pids list from %zu to %zu entries",
464 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
465 if (new_pids
== NULL
) {
466 PERROR("realloc list events");
471 /* Zero the new memory */
472 memset(new_pids
+ nbmem
, 0,
473 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
481 DBG("Kernel list tracker pids done (%zd pids)", count
);
483 ret
= fclose(fp
); /* closes both fp and fd */
499 * Create kernel metadata, open from the kernel tracer and add it to the
502 int kernel_open_metadata(struct ltt_kernel_session
*session
)
505 struct ltt_kernel_metadata
*lkm
= NULL
;
509 /* Allocate kernel metadata */
510 lkm
= trace_kernel_create_metadata();
515 /* Kernel tracer metadata creation */
516 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
522 /* Prevent fd duplication after execlp() */
523 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
525 PERROR("fcntl session fd");
528 session
->metadata
= lkm
;
530 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
535 trace_kernel_destroy_metadata(lkm
);
541 * Start tracing session.
543 int kernel_start_session(struct ltt_kernel_session
*session
)
549 ret
= kernctl_start_session(session
->fd
);
551 PERROR("ioctl start session");
555 DBG("Kernel session started");
564 * Make a kernel wait to make sure in-flight probe have completed.
566 void kernel_wait_quiescent(int fd
)
570 DBG("Kernel quiescent wait on %d", fd
);
572 ret
= kernctl_wait_quiescent(fd
);
574 PERROR("wait quiescent ioctl");
575 ERR("Kernel quiescent wait failed");
580 * Force flush buffer of metadata.
582 int kernel_metadata_flush_buffer(int fd
)
586 DBG("Kernel flushing metadata buffer on fd %d", fd
);
588 ret
= kernctl_buffer_flush(fd
);
590 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
597 * Force flush buffer for channel.
599 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
602 struct ltt_kernel_stream
*stream
;
606 DBG("Flush buffer for channel %s", channel
->channel
->name
);
608 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
609 DBG("Flushing channel stream %d", stream
->fd
);
610 ret
= kernctl_buffer_flush(stream
->fd
);
613 ERR("Fail to flush buffer for stream %d (ret: %d)",
622 * Stop tracing session.
624 int kernel_stop_session(struct ltt_kernel_session
*session
)
630 ret
= kernctl_stop_session(session
->fd
);
635 DBG("Kernel session stopped");
644 * Open stream of channel, register it to the kernel tracer and add it
645 * to the stream list of the channel.
647 * Note: given that the streams may appear in random order wrt CPU
648 * number (e.g. cpu hotplug), the index value of the stream number in
649 * the stream name is not necessarily linked to the CPU number.
651 * Return the number of created stream. Else, a negative value.
653 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
656 struct ltt_kernel_stream
*lks
;
660 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
661 lks
= trace_kernel_create_stream(channel
->channel
->name
,
662 channel
->stream_count
);
672 /* Prevent fd duplication after execlp() */
673 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
675 PERROR("fcntl session fd");
678 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
679 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
681 /* Add stream to channel stream list */
682 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
683 channel
->stream_count
++;
685 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
689 return channel
->stream_count
;
696 * Open the metadata stream and set it to the kernel session.
698 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
704 ret
= kernctl_create_stream(session
->metadata
->fd
);
706 PERROR("kernel create metadata stream");
710 DBG("Kernel metadata stream created (fd: %d)", ret
);
711 session
->metadata_stream_fd
= ret
;
712 /* Prevent fd duplication after execlp() */
713 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
715 PERROR("fcntl session fd");
725 * Get the event list from the kernel tracer and return the number of elements.
727 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
731 size_t nbmem
, count
= 0;
733 struct lttng_event
*elist
;
737 fd
= kernctl_tracepoint_list(tracer_fd
);
739 PERROR("kernel tracepoint list");
743 fp
= fdopen(fd
, "r");
745 PERROR("kernel tracepoint list fdopen");
750 * Init memory size counter
751 * See kernel-ctl.h for explanation of this value
753 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
754 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
756 PERROR("alloc list events");
761 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
762 if (count
>= nbmem
) {
763 struct lttng_event
*new_elist
;
766 new_nbmem
= nbmem
<< 1;
767 DBG("Reallocating event list from %zu to %zu bytes",
769 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
770 if (new_elist
== NULL
) {
771 PERROR("realloc list events");
777 /* Zero the new memory */
778 memset(new_elist
+ nbmem
, 0,
779 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
783 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
784 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
785 elist
[count
].enabled
= -1;
791 DBG("Kernel list events done (%zu events)", count
);
793 ret
= fclose(fp
); /* closes both fp and fd */
809 * Get kernel version and validate it.
811 int kernel_validate_version(int tracer_fd
)
814 struct lttng_kernel_tracer_version version
;
815 struct lttng_kernel_tracer_abi_version abi_version
;
817 ret
= kernctl_tracer_version(tracer_fd
, &version
);
819 ERR("Failed to retrieve the lttng-modules version");
823 /* Validate version */
824 if (version
.major
!= VERSION_MAJOR
) {
825 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
826 version
.major
, VERSION_MAJOR
);
829 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
831 ERR("Failed to retrieve lttng-modules ABI version");
834 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
835 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
836 abi_version
.major
, abi_version
.minor
,
837 LTTNG_MODULES_ABI_MAJOR_VERSION
);
840 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
841 version
.major
, version
.minor
,
842 abi_version
.major
, abi_version
.minor
);
849 ERR("Kernel tracer version check failed; kernel tracing will not be available");
854 * Kernel work-arounds called at the start of sessiond main().
856 int init_kernel_workarounds(void)
862 * boot_id needs to be read once before being used concurrently
863 * to deal with a Linux kernel race. A fix is proposed for
864 * upstream, but the work-around is needed for older kernels.
866 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
873 ret
= fread(buf
, 1, sizeof(buf
), fp
);
875 /* Ignore error, we don't really care */
887 * Complete teardown of a kernel session.
889 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
892 DBG3("No kernel session when tearing down session");
896 DBG("Tearing down kernel session");
899 * Destroy channels on the consumer if at least one FD has been sent and we
900 * are in no output mode because the streams are in *no* monitor mode so we
901 * have to send a command to clean them up or else they leaked.
903 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
905 struct consumer_socket
*socket
;
906 struct lttng_ht_iter iter
;
908 /* For each consumer socket. */
910 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
912 struct ltt_kernel_channel
*chan
;
914 /* For each channel, ask the consumer to destroy it. */
915 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
916 ret
= kernel_consumer_destroy_channel(socket
, chan
);
918 /* Consumer is probably dead. Use next socket. */
926 /* Close any relayd session */
927 consumer_output_send_destroy_relayd(ksess
->consumer
);
929 trace_kernel_destroy_session(ksess
);
933 * Destroy a kernel channel object. It does not do anything on the tracer side.
935 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
937 struct ltt_kernel_session
*ksess
= NULL
;
940 assert(kchan
->channel
);
942 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
944 /* Update channel count of associated session. */
945 if (kchan
->session
) {
946 /* Keep pointer reference so we can update it after the destroy. */
947 ksess
= kchan
->session
;
950 trace_kernel_destroy_channel(kchan
);
953 * At this point the kernel channel is not visible anymore. This is safe
954 * since in order to work on a visible kernel session, the tracing session
955 * lock (ltt_session.lock) MUST be acquired.
958 ksess
->channel_count
--;
963 * Take a snapshot for a given kernel session.
965 * Return 0 on success or else return a LTTNG_ERR code.
967 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
968 struct snapshot_output
*output
, int wait
,
969 uint64_t nb_packets_per_stream
)
971 int err
, ret
, saved_metadata_fd
;
972 struct consumer_socket
*socket
;
973 struct lttng_ht_iter iter
;
974 struct ltt_kernel_metadata
*saved_metadata
;
977 assert(ksess
->consumer
);
980 DBG("Kernel snapshot record started");
982 /* Save current metadata since the following calls will change it. */
983 saved_metadata
= ksess
->metadata
;
984 saved_metadata_fd
= ksess
->metadata_stream_fd
;
988 ret
= kernel_open_metadata(ksess
);
990 ret
= LTTNG_ERR_KERN_META_FAIL
;
994 ret
= kernel_open_metadata_stream(ksess
);
996 ret
= LTTNG_ERR_KERN_META_FAIL
;
997 goto error_open_stream
;
1000 /* Send metadata to consumer and snapshot everything. */
1001 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1002 socket
, node
.node
) {
1003 struct consumer_output
*saved_output
;
1004 struct ltt_kernel_channel
*chan
;
1007 * Temporarly switch consumer output for our snapshot output. As long
1008 * as the session lock is taken, this is safe.
1010 saved_output
= ksess
->consumer
;
1011 ksess
->consumer
= output
->consumer
;
1013 pthread_mutex_lock(socket
->lock
);
1014 /* This stream must not be monitored by the consumer. */
1015 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1016 pthread_mutex_unlock(socket
->lock
);
1017 /* Put back the saved consumer output into the session. */
1018 ksess
->consumer
= saved_output
;
1020 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1021 goto error_consumer
;
1024 /* For each channel, ask the consumer to snapshot it. */
1025 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1026 pthread_mutex_lock(socket
->lock
);
1027 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1028 ksess
->uid
, ksess
->gid
,
1029 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1030 nb_packets_per_stream
);
1031 pthread_mutex_unlock(socket
->lock
);
1033 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1034 (void) kernel_consumer_destroy_metadata(socket
,
1036 goto error_consumer
;
1040 /* Snapshot metadata, */
1041 pthread_mutex_lock(socket
->lock
);
1042 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1043 1, ksess
->uid
, ksess
->gid
,
1044 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1045 pthread_mutex_unlock(socket
->lock
);
1047 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1048 goto error_consumer
;
1052 * The metadata snapshot is done, ask the consumer to destroy it since
1053 * it's not monitored on the consumer side.
1055 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1061 /* Close newly opened metadata stream. It's now on the consumer side. */
1062 err
= close(ksess
->metadata_stream_fd
);
1064 PERROR("close snapshot kernel");
1068 trace_kernel_destroy_metadata(ksess
->metadata
);
1070 /* Restore metadata state.*/
1071 ksess
->metadata
= saved_metadata
;
1072 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1079 * Get the syscall mask array from the kernel tracer.
1081 * Return 0 on success else a negative value. In both case, syscall_mask should
1084 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1086 assert(syscall_mask
);
1089 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);