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 * Key used to reference a channel between the sessiond and the consumer. This
39 * is only read and updated with the session_list lock held.
41 static uint64_t next_kernel_channel_key
;
44 * Add context on a kernel channel.
46 * Assumes the ownership of ctx.
48 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
49 struct ltt_kernel_context
*ctx
)
56 DBG("Adding context to channel %s", chan
->channel
->name
);
57 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
61 /* Exists but not available for this kernel */
62 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
65 /* If EEXIST, we just ignore the error */
69 PERROR("add context ioctl");
70 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
77 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
82 trace_kernel_destroy_context(ctx
);
88 * Create a new kernel session, register it to the kernel tracer and add it to
89 * the session daemon session.
91 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
94 struct ltt_kernel_session
*lks
;
98 /* Allocate data structure */
99 lks
= trace_kernel_create_session();
105 /* Kernel tracer session creation */
106 ret
= kernctl_create_session(tracer_fd
);
108 PERROR("ioctl kernel create session");
113 /* Prevent fd duplication after execlp() */
114 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
116 PERROR("fcntl session fd");
119 lks
->id
= session
->id
;
120 lks
->consumer_fds_sent
= 0;
121 session
->kernel_session
= lks
;
123 DBG("Kernel session created (fd: %d)", lks
->fd
);
129 trace_kernel_destroy_session(lks
);
135 * Create a kernel channel, register it to the kernel tracer and add it to the
138 int kernel_create_channel(struct ltt_kernel_session
*session
,
139 struct lttng_channel
*chan
)
142 struct ltt_kernel_channel
*lkc
;
147 /* Allocate kernel channel */
148 lkc
= trace_kernel_create_channel(chan
);
153 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
154 chan
->name
, lkc
->channel
->attr
.overwrite
,
155 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
156 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
157 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
159 /* Kernel tracer channel creation */
160 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
162 PERROR("ioctl kernel create channel");
166 /* Setup the channel fd */
168 /* Prevent fd duplication after execlp() */
169 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
171 PERROR("fcntl session fd");
174 /* Add channel to session */
175 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
176 session
->channel_count
++;
177 lkc
->session
= session
;
178 lkc
->key
= ++next_kernel_channel_key
;
180 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
181 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
194 * Create a kernel event, enable it to the kernel tracer and add it to the
195 * channel event list of the kernel session.
196 * We own filter_expression and filter.
198 int kernel_create_event(struct lttng_event
*ev
,
199 struct ltt_kernel_channel
*channel
,
200 char *filter_expression
,
201 struct lttng_filter_bytecode
*filter
)
204 struct ltt_kernel_event
*event
;
209 /* We pass ownership of filter_expression and filter */
210 event
= trace_kernel_create_event(ev
, filter_expression
,
217 ret
= kernctl_create_event(channel
->fd
, event
->event
);
223 WARN("Event type not implemented");
226 WARN("Event %s not found!", ev
->name
);
229 PERROR("create event ioctl");
234 event
->type
= ev
->type
;
236 /* Prevent fd duplication after execlp() */
237 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
239 PERROR("fcntl session fd");
243 ret
= kernctl_filter(event
->fd
, filter
);
249 ret
= kernctl_enable(event
->fd
);
253 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
256 PERROR("enable kernel event");
262 /* Add event to event list */
263 cds_list_add(&event
->list
, &channel
->events_list
.head
);
264 channel
->event_count
++;
266 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
275 closeret
= close(event
->fd
);
277 PERROR("close event fd");
287 * Disable a kernel channel.
289 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
295 ret
= kernctl_disable(chan
->fd
);
297 PERROR("disable chan ioctl");
302 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
303 chan
->channel
->name
, chan
->fd
, chan
->key
);
312 * Enable a kernel channel.
314 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
320 ret
= kernctl_enable(chan
->fd
);
321 if (ret
< 0 && ret
!= -EEXIST
) {
322 PERROR("Enable kernel chan");
327 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
328 chan
->channel
->name
, chan
->fd
, chan
->key
);
337 * Enable a kernel event.
339 int kernel_enable_event(struct ltt_kernel_event
*event
)
345 ret
= kernctl_enable(event
->fd
);
349 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
352 PERROR("enable kernel event");
359 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
368 * Disable a kernel event.
370 int kernel_disable_event(struct ltt_kernel_event
*event
)
376 ret
= kernctl_disable(event
->fd
);
380 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
383 PERROR("disable kernel event");
390 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
399 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
403 DBG("Kernel track PID %d for session id %" PRIu64
".",
405 ret
= kernctl_track_pid(session
->fd
, pid
);
411 return LTTNG_ERR_INVALID
;
413 return LTTNG_ERR_NOMEM
;
415 return LTTNG_ERR_PID_TRACKED
;
417 return LTTNG_ERR_UNK
;
421 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
425 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
427 ret
= kernctl_untrack_pid(session
->fd
, pid
);
433 return LTTNG_ERR_INVALID
;
435 return LTTNG_ERR_NOMEM
;
437 return LTTNG_ERR_PID_NOT_TRACKED
;
439 return LTTNG_ERR_UNK
;
443 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
448 ssize_t nbmem
, count
= 0;
452 fd
= kernctl_list_tracker_pids(session
->fd
);
454 PERROR("kernel tracker pids list");
458 fp
= fdopen(fd
, "r");
460 PERROR("kernel tracker pids list fdopen");
464 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
465 pids
= zmalloc(sizeof(*pids
) * nbmem
);
467 PERROR("alloc list pids");
472 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
473 if (count
>= nbmem
) {
477 new_nbmem
= nbmem
<< 1;
478 DBG("Reallocating pids list from %zu to %zu entries",
480 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
481 if (new_pids
== NULL
) {
482 PERROR("realloc list events");
487 /* Zero the new memory */
488 memset(new_pids
+ nbmem
, 0,
489 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
497 DBG("Kernel list tracker pids done (%zd pids)", count
);
499 ret
= fclose(fp
); /* closes both fp and fd */
515 * Create kernel metadata, open from the kernel tracer and add it to the
518 int kernel_open_metadata(struct ltt_kernel_session
*session
)
521 struct ltt_kernel_metadata
*lkm
= NULL
;
525 /* Allocate kernel metadata */
526 lkm
= trace_kernel_create_metadata();
531 /* Kernel tracer metadata creation */
532 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
538 lkm
->key
= ++next_kernel_channel_key
;
539 /* Prevent fd duplication after execlp() */
540 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
542 PERROR("fcntl session fd");
545 session
->metadata
= lkm
;
547 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
552 trace_kernel_destroy_metadata(lkm
);
558 * Start tracing session.
560 int kernel_start_session(struct ltt_kernel_session
*session
)
566 ret
= kernctl_start_session(session
->fd
);
568 PERROR("ioctl start session");
572 DBG("Kernel session started");
581 * Make a kernel wait to make sure in-flight probe have completed.
583 void kernel_wait_quiescent(int fd
)
587 DBG("Kernel quiescent wait on %d", fd
);
589 ret
= kernctl_wait_quiescent(fd
);
591 PERROR("wait quiescent ioctl");
592 ERR("Kernel quiescent wait failed");
597 * Force flush buffer of metadata.
599 int kernel_metadata_flush_buffer(int fd
)
603 DBG("Kernel flushing metadata buffer on fd %d", fd
);
605 ret
= kernctl_buffer_flush(fd
);
607 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
614 * Force flush buffer for channel.
616 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
619 struct ltt_kernel_stream
*stream
;
623 DBG("Flush buffer for channel %s", channel
->channel
->name
);
625 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
626 DBG("Flushing channel stream %d", stream
->fd
);
627 ret
= kernctl_buffer_flush(stream
->fd
);
630 ERR("Fail to flush buffer for stream %d (ret: %d)",
639 * Stop tracing session.
641 int kernel_stop_session(struct ltt_kernel_session
*session
)
647 ret
= kernctl_stop_session(session
->fd
);
652 DBG("Kernel session stopped");
661 * Open stream of channel, register it to the kernel tracer and add it
662 * to the stream list of the channel.
664 * Note: given that the streams may appear in random order wrt CPU
665 * number (e.g. cpu hotplug), the index value of the stream number in
666 * the stream name is not necessarily linked to the CPU number.
668 * Return the number of created stream. Else, a negative value.
670 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
673 struct ltt_kernel_stream
*lks
;
677 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
678 lks
= trace_kernel_create_stream(channel
->channel
->name
,
679 channel
->stream_count
);
689 /* Prevent fd duplication after execlp() */
690 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
692 PERROR("fcntl session fd");
695 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
696 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
698 /* Add stream to channel stream list */
699 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
700 channel
->stream_count
++;
702 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
706 return channel
->stream_count
;
713 * Open the metadata stream and set it to the kernel session.
715 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
721 ret
= kernctl_create_stream(session
->metadata
->fd
);
723 PERROR("kernel create metadata stream");
727 DBG("Kernel metadata stream created (fd: %d)", ret
);
728 session
->metadata_stream_fd
= ret
;
729 /* Prevent fd duplication after execlp() */
730 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
732 PERROR("fcntl session fd");
742 * Get the event list from the kernel tracer and return the number of elements.
744 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
748 size_t nbmem
, count
= 0;
750 struct lttng_event
*elist
;
754 fd
= kernctl_tracepoint_list(tracer_fd
);
756 PERROR("kernel tracepoint list");
760 fp
= fdopen(fd
, "r");
762 PERROR("kernel tracepoint list fdopen");
767 * Init memory size counter
768 * See kernel-ctl.h for explanation of this value
770 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
771 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
773 PERROR("alloc list events");
778 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
779 if (count
>= nbmem
) {
780 struct lttng_event
*new_elist
;
783 new_nbmem
= nbmem
<< 1;
784 DBG("Reallocating event list from %zu to %zu bytes",
786 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
787 if (new_elist
== NULL
) {
788 PERROR("realloc list events");
794 /* Zero the new memory */
795 memset(new_elist
+ nbmem
, 0,
796 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
800 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
801 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
802 elist
[count
].enabled
= -1;
808 DBG("Kernel list events done (%zu events)", count
);
810 ret
= fclose(fp
); /* closes both fp and fd */
826 * Get kernel version and validate it.
828 int kernel_validate_version(int tracer_fd
,
829 struct lttng_kernel_tracer_version
*version
,
830 struct lttng_kernel_tracer_abi_version
*abi_version
)
834 ret
= kernctl_tracer_version(tracer_fd
, version
);
836 ERR("Failed to retrieve the lttng-modules version");
840 /* Validate version */
841 if (version
->major
!= VERSION_MAJOR
) {
842 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
843 version
->major
, VERSION_MAJOR
);
846 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
848 ERR("Failed to retrieve lttng-modules ABI version");
851 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
852 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
853 abi_version
->major
, abi_version
->minor
,
854 LTTNG_MODULES_ABI_MAJOR_VERSION
);
857 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
858 version
->major
, version
->minor
,
859 abi_version
->major
, abi_version
->minor
);
866 ERR("Kernel tracer version check failed; kernel tracing will not be available");
871 * Kernel work-arounds called at the start of sessiond main().
873 int init_kernel_workarounds(void)
879 * boot_id needs to be read once before being used concurrently
880 * to deal with a Linux kernel race. A fix is proposed for
881 * upstream, but the work-around is needed for older kernels.
883 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
890 ret
= fread(buf
, 1, sizeof(buf
), fp
);
892 /* Ignore error, we don't really care */
904 * Complete teardown of a kernel session.
906 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
909 DBG3("No kernel session when tearing down session");
913 DBG("Tearing down kernel session");
916 * Destroy channels on the consumer if at least one FD has been sent and we
917 * are in no output mode because the streams are in *no* monitor mode so we
918 * have to send a command to clean them up or else they leaked.
920 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
922 struct consumer_socket
*socket
;
923 struct lttng_ht_iter iter
;
925 /* For each consumer socket. */
927 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
929 struct ltt_kernel_channel
*chan
;
931 /* For each channel, ask the consumer to destroy it. */
932 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
933 ret
= kernel_consumer_destroy_channel(socket
, chan
);
935 /* Consumer is probably dead. Use next socket. */
943 /* Close any relayd session */
944 consumer_output_send_destroy_relayd(ksess
->consumer
);
946 trace_kernel_destroy_session(ksess
);
950 * Destroy a kernel channel object. It does not do anything on the tracer side.
952 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
954 struct ltt_kernel_session
*ksess
= NULL
;
957 assert(kchan
->channel
);
959 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
961 /* Update channel count of associated session. */
962 if (kchan
->session
) {
963 /* Keep pointer reference so we can update it after the destroy. */
964 ksess
= kchan
->session
;
967 trace_kernel_destroy_channel(kchan
);
970 * At this point the kernel channel is not visible anymore. This is safe
971 * since in order to work on a visible kernel session, the tracing session
972 * lock (ltt_session.lock) MUST be acquired.
975 ksess
->channel_count
--;
980 * Take a snapshot for a given kernel session.
982 * Return 0 on success or else return a LTTNG_ERR code.
984 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
985 struct snapshot_output
*output
, int wait
,
986 uint64_t nb_packets_per_stream
)
988 int err
, ret
, saved_metadata_fd
;
989 struct consumer_socket
*socket
;
990 struct lttng_ht_iter iter
;
991 struct ltt_kernel_metadata
*saved_metadata
;
994 assert(ksess
->consumer
);
997 DBG("Kernel snapshot record started");
999 /* Save current metadata since the following calls will change it. */
1000 saved_metadata
= ksess
->metadata
;
1001 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1005 ret
= kernel_open_metadata(ksess
);
1007 ret
= LTTNG_ERR_KERN_META_FAIL
;
1011 ret
= kernel_open_metadata_stream(ksess
);
1013 ret
= LTTNG_ERR_KERN_META_FAIL
;
1014 goto error_open_stream
;
1017 /* Send metadata to consumer and snapshot everything. */
1018 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1019 socket
, node
.node
) {
1020 struct consumer_output
*saved_output
;
1021 struct ltt_kernel_channel
*chan
;
1024 * Temporarly switch consumer output for our snapshot output. As long
1025 * as the session lock is taken, this is safe.
1027 saved_output
= ksess
->consumer
;
1028 ksess
->consumer
= output
->consumer
;
1030 pthread_mutex_lock(socket
->lock
);
1031 /* This stream must not be monitored by the consumer. */
1032 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1033 pthread_mutex_unlock(socket
->lock
);
1034 /* Put back the saved consumer output into the session. */
1035 ksess
->consumer
= saved_output
;
1037 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1038 goto error_consumer
;
1041 /* For each channel, ask the consumer to snapshot it. */
1042 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1043 ret
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1044 ksess
->uid
, ksess
->gid
,
1045 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1046 nb_packets_per_stream
);
1048 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1049 (void) kernel_consumer_destroy_metadata(socket
,
1051 goto error_consumer
;
1055 /* Snapshot metadata, */
1056 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1057 1, ksess
->uid
, ksess
->gid
,
1058 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1060 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1061 goto error_consumer
;
1065 * The metadata snapshot is done, ask the consumer to destroy it since
1066 * it's not monitored on the consumer side.
1068 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1074 /* Close newly opened metadata stream. It's now on the consumer side. */
1075 err
= close(ksess
->metadata_stream_fd
);
1077 PERROR("close snapshot kernel");
1081 trace_kernel_destroy_metadata(ksess
->metadata
);
1083 /* Restore metadata state.*/
1084 ksess
->metadata
= saved_metadata
;
1085 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1092 * Get the syscall mask array from the kernel tracer.
1094 * Return 0 on success else a negative value. In both case, syscall_mask should
1097 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1099 assert(syscall_mask
);
1102 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1106 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1109 * Return 1 on success, 0 when feature is not supported, negative value in case
1112 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1114 int ret
= 0; // Not supported by default
1115 struct lttng_kernel_tracer_abi_version abi
;
1117 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1119 ERR("Failed to retrieve lttng-modules ABI version");
1124 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1126 if (abi
.major
>= 2 && abi
.minor
>= 3) {