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.
27 #include <common/common.h>
28 #include <common/kernel-ctl/kernel-ctl.h>
29 #include <common/kernel-ctl/kernel-ioctl.h>
30 #include <common/sessiond-comm/sessiond-comm.h>
34 #include "kernel-consumer.h"
35 #include "kern-modules.h"
39 * Add context on a kernel channel.
41 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
42 struct ltt_kernel_context
*ctx
)
49 DBG("Adding context to channel %s", chan
->channel
->name
);
50 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
52 if (errno
!= EEXIST
) {
53 PERROR("add context ioctl");
55 /* If EEXIST, we just ignore the error */
61 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
73 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
76 struct ltt_kernel_session
*lks
;
80 /* Allocate data structure */
81 lks
= trace_kernel_create_session();
87 /* Kernel tracer session creation */
88 ret
= kernctl_create_session(tracer_fd
);
90 PERROR("ioctl kernel create session");
95 /* Prevent fd duplication after execlp() */
96 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
98 PERROR("fcntl session fd");
101 lks
->id
= session
->id
;
102 lks
->consumer_fds_sent
= 0;
103 session
->kernel_session
= lks
;
105 DBG("Kernel session created (fd: %d)", lks
->fd
);
111 trace_kernel_destroy_session(lks
);
117 * Create a kernel channel, register it to the kernel tracer and add it to the
120 int kernel_create_channel(struct ltt_kernel_session
*session
,
121 struct lttng_channel
*chan
)
124 struct ltt_kernel_channel
*lkc
;
129 /* Allocate kernel channel */
130 lkc
= trace_kernel_create_channel(chan
);
135 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
136 chan
->name
, lkc
->channel
->attr
.overwrite
,
137 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
138 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
139 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
141 /* Kernel tracer channel creation */
142 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
144 PERROR("ioctl kernel create channel");
148 /* Setup the channel fd */
150 /* Prevent fd duplication after execlp() */
151 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
153 PERROR("fcntl session fd");
156 /* Add channel to session */
157 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
158 session
->channel_count
++;
159 lkc
->session
= session
;
161 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
174 * Create a kernel event, enable it to the kernel tracer and add it to the
175 * channel event list of the kernel session.
176 * We own filter_expression and filter.
178 int kernel_create_event(struct lttng_event
*ev
,
179 struct ltt_kernel_channel
*channel
,
180 char *filter_expression
,
181 struct lttng_filter_bytecode
*filter
)
184 struct ltt_kernel_event
*event
;
189 /* We pass ownership of filter_expression and filter */
190 event
= trace_kernel_create_event(ev
, filter_expression
,
197 ret
= kernctl_create_event(channel
->fd
, event
->event
);
203 WARN("Event type not implemented");
206 WARN("Event %s not found!", ev
->name
);
209 PERROR("create event ioctl");
215 event
->type
= ev
->type
;
217 /* Prevent fd duplication after execlp() */
218 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
220 PERROR("fcntl session fd");
224 ret
= kernctl_filter(event
->fd
, filter
);
230 ret
= kernctl_enable(event
->fd
);
234 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
237 PERROR("enable kernel event");
243 /* Add event to event list */
244 cds_list_add(&event
->list
, &channel
->events_list
.head
);
245 channel
->event_count
++;
247 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
256 closeret
= close(event
->fd
);
258 PERROR("close event fd");
268 * Disable a kernel channel.
270 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
276 ret
= kernctl_disable(chan
->fd
);
278 PERROR("disable chan ioctl");
284 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
293 * Enable a kernel channel.
295 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
301 ret
= kernctl_enable(chan
->fd
);
302 if (ret
< 0 && errno
!= EEXIST
) {
303 PERROR("Enable kernel chan");
308 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
317 * Enable a kernel event.
319 int kernel_enable_event(struct ltt_kernel_event
*event
)
325 ret
= kernctl_enable(event
->fd
);
329 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
332 PERROR("enable kernel event");
339 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
348 * Disable a kernel event.
350 int kernel_disable_event(struct ltt_kernel_event
*event
)
356 ret
= kernctl_disable(event
->fd
);
360 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
363 PERROR("disable kernel event");
370 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
379 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
383 DBG("Kernel track PID %d for session id %" PRIu64
".",
385 ret
= kernctl_track_pid(session
->fd
, pid
);
391 return LTTNG_ERR_INVALID
;
393 return LTTNG_ERR_NOMEM
;
395 return LTTNG_ERR_PID_TRACKED
;
397 return LTTNG_ERR_UNK
;
401 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
405 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
407 ret
= kernctl_untrack_pid(session
->fd
, pid
);
413 return LTTNG_ERR_INVALID
;
415 return LTTNG_ERR_NOMEM
;
417 return LTTNG_ERR_PID_NOT_TRACKED
;
419 return LTTNG_ERR_UNK
;
423 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
428 ssize_t nbmem
, count
= 0;
432 fd
= kernctl_list_tracker_pids(session
->fd
);
434 PERROR("kernel tracker pids list");
438 fp
= fdopen(fd
, "r");
440 PERROR("kernel tracker pids list fdopen");
444 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
445 pids
= zmalloc(sizeof(*pids
) * nbmem
);
447 PERROR("alloc list pids");
452 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
453 if (count
>= nbmem
) {
457 new_nbmem
= nbmem
<< 1;
458 DBG("Reallocating pids list from %zu to %zu entries",
460 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
461 if (new_pids
== NULL
) {
462 PERROR("realloc list events");
467 /* Zero the new memory */
468 memset(new_pids
+ nbmem
, 0,
469 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
477 DBG("Kernel list tracker pids done (%zd pids)", count
);
479 ret
= fclose(fp
); /* closes both fp and fd */
495 * Create kernel metadata, open from the kernel tracer and add it to the
498 int kernel_open_metadata(struct ltt_kernel_session
*session
)
501 struct ltt_kernel_metadata
*lkm
= NULL
;
505 /* Allocate kernel metadata */
506 lkm
= trace_kernel_create_metadata();
511 /* Kernel tracer metadata creation */
512 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
518 /* Prevent fd duplication after execlp() */
519 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
521 PERROR("fcntl session fd");
524 session
->metadata
= lkm
;
526 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
531 trace_kernel_destroy_metadata(lkm
);
537 * Start tracing session.
539 int kernel_start_session(struct ltt_kernel_session
*session
)
545 ret
= kernctl_start_session(session
->fd
);
547 PERROR("ioctl start session");
551 DBG("Kernel session started");
560 * Make a kernel wait to make sure in-flight probe have completed.
562 void kernel_wait_quiescent(int fd
)
566 DBG("Kernel quiescent wait on %d", fd
);
568 ret
= kernctl_wait_quiescent(fd
);
570 PERROR("wait quiescent ioctl");
571 ERR("Kernel quiescent wait failed");
578 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
584 ret
= kernctl_calibrate(fd
, calibrate
);
586 PERROR("calibrate ioctl");
595 * Force flush buffer of metadata.
597 int kernel_metadata_flush_buffer(int fd
)
601 DBG("Kernel flushing metadata buffer on fd %d", fd
);
603 ret
= kernctl_buffer_flush(fd
);
605 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
612 * Force flush buffer for channel.
614 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
617 struct ltt_kernel_stream
*stream
;
621 DBG("Flush buffer for channel %s", channel
->channel
->name
);
623 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
624 DBG("Flushing channel stream %d", stream
->fd
);
625 ret
= kernctl_buffer_flush(stream
->fd
);
628 ERR("Fail to flush buffer for stream %d (ret: %d)",
637 * Stop tracing session.
639 int kernel_stop_session(struct ltt_kernel_session
*session
)
645 ret
= kernctl_stop_session(session
->fd
);
650 DBG("Kernel session stopped");
659 * Open stream of channel, register it to the kernel tracer and add it
660 * to the stream list of the channel.
662 * Return the number of created stream. Else, a negative value.
664 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
667 struct ltt_kernel_stream
*lks
;
671 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
672 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
682 /* Prevent fd duplication after execlp() */
683 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
685 PERROR("fcntl session fd");
688 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
689 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
691 /* Add stream to channe stream list */
692 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
693 channel
->stream_count
++;
695 /* Increment counter which represent CPU number. */
698 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
702 return channel
->stream_count
;
709 * Open the metadata stream and set it to the kernel session.
711 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
717 ret
= kernctl_create_stream(session
->metadata
->fd
);
719 PERROR("kernel create metadata stream");
723 DBG("Kernel metadata stream created (fd: %d)", ret
);
724 session
->metadata_stream_fd
= ret
;
725 /* Prevent fd duplication after execlp() */
726 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
728 PERROR("fcntl session fd");
738 * Get the event list from the kernel tracer and return the number of elements.
740 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
744 size_t nbmem
, count
= 0;
746 struct lttng_event
*elist
;
750 fd
= kernctl_tracepoint_list(tracer_fd
);
752 PERROR("kernel tracepoint list");
756 fp
= fdopen(fd
, "r");
758 PERROR("kernel tracepoint list fdopen");
763 * Init memory size counter
764 * See kernel-ctl.h for explanation of this value
766 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
767 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
769 PERROR("alloc list events");
774 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
775 if (count
>= nbmem
) {
776 struct lttng_event
*new_elist
;
779 new_nbmem
= nbmem
<< 1;
780 DBG("Reallocating event list from %zu to %zu bytes",
782 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
783 if (new_elist
== NULL
) {
784 PERROR("realloc list events");
790 /* Zero the new memory */
791 memset(new_elist
+ nbmem
, 0,
792 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
796 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
797 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
798 elist
[count
].enabled
= -1;
804 DBG("Kernel list events done (%zu events)", count
);
806 ret
= fclose(fp
); /* closes both fp and fd */
822 * Get kernel version and validate it.
824 int kernel_validate_version(int tracer_fd
)
827 struct lttng_kernel_tracer_version version
;
828 struct lttng_kernel_tracer_abi_version abi_version
;
830 ret
= kernctl_tracer_version(tracer_fd
, &version
);
832 ERR("Failed at getting the lttng-modules version");
836 /* Validate version */
837 if (version
.major
!= VERSION_MAJOR
) {
838 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
839 version
.major
, VERSION_MAJOR
);
842 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
844 ERR("Failed at getting lttng-modules ABI version");
847 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
848 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
849 abi_version
.major
, abi_version
.minor
,
850 LTTNG_MODULES_ABI_MAJOR_VERSION
);
853 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
854 version
.major
, version
.minor
,
855 abi_version
.major
, abi_version
.minor
);
866 * Kernel work-arounds called at the start of sessiond main().
868 int init_kernel_workarounds(void)
874 * boot_id needs to be read once before being used concurrently
875 * to deal with a Linux kernel race. A fix is proposed for
876 * upstream, but the work-around is needed for older kernels.
878 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
885 ret
= fread(buf
, 1, sizeof(buf
), fp
);
887 /* Ignore error, we don't really care */
899 * Complete teardown of a kernel session.
901 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
904 DBG3("No kernel session when tearing down session");
908 DBG("Tearing down kernel session");
911 * Destroy channels on the consumer if at least one FD has been sent and we
912 * are in no output mode because the streams are in *no* monitor mode so we
913 * have to send a command to clean them up or else they leaked.
915 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
917 struct consumer_socket
*socket
;
918 struct lttng_ht_iter iter
;
920 /* For each consumer socket. */
922 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
924 struct ltt_kernel_channel
*chan
;
926 /* For each channel, ask the consumer to destroy it. */
927 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
928 ret
= kernel_consumer_destroy_channel(socket
, chan
);
930 /* Consumer is probably dead. Use next socket. */
938 /* Close any relayd session */
939 consumer_output_send_destroy_relayd(ksess
->consumer
);
941 trace_kernel_destroy_session(ksess
);
945 * Destroy a kernel channel object. It does not do anything on the tracer side.
947 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
949 struct ltt_kernel_session
*ksess
= NULL
;
952 assert(kchan
->channel
);
954 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
956 /* Update channel count of associated session. */
957 if (kchan
->session
) {
958 /* Keep pointer reference so we can update it after the destroy. */
959 ksess
= kchan
->session
;
962 trace_kernel_destroy_channel(kchan
);
965 * At this point the kernel channel is not visible anymore. This is safe
966 * since in order to work on a visible kernel session, the tracing session
967 * lock (ltt_session.lock) MUST be acquired.
970 ksess
->channel_count
--;
975 * Take a snapshot for a given kernel session.
977 * Return 0 on success or else return a LTTNG_ERR code.
979 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
980 struct snapshot_output
*output
, int wait
,
981 uint64_t nb_packets_per_stream
)
983 int err
, ret
, saved_metadata_fd
;
984 struct consumer_socket
*socket
;
985 struct lttng_ht_iter iter
;
986 struct ltt_kernel_metadata
*saved_metadata
;
989 assert(ksess
->consumer
);
992 DBG("Kernel snapshot record started");
994 /* Save current metadata since the following calls will change it. */
995 saved_metadata
= ksess
->metadata
;
996 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1000 ret
= kernel_open_metadata(ksess
);
1002 ret
= LTTNG_ERR_KERN_META_FAIL
;
1006 ret
= kernel_open_metadata_stream(ksess
);
1008 ret
= LTTNG_ERR_KERN_META_FAIL
;
1009 goto error_open_stream
;
1012 /* Send metadata to consumer and snapshot everything. */
1013 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1014 socket
, node
.node
) {
1015 struct consumer_output
*saved_output
;
1016 struct ltt_kernel_channel
*chan
;
1019 * Temporarly switch consumer output for our snapshot output. As long
1020 * as the session lock is taken, this is safe.
1022 saved_output
= ksess
->consumer
;
1023 ksess
->consumer
= output
->consumer
;
1025 pthread_mutex_lock(socket
->lock
);
1026 /* This stream must not be monitored by the consumer. */
1027 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1028 pthread_mutex_unlock(socket
->lock
);
1029 /* Put back the saved consumer output into the session. */
1030 ksess
->consumer
= saved_output
;
1032 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1033 goto error_consumer
;
1036 /* For each channel, ask the consumer to snapshot it. */
1037 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1038 pthread_mutex_lock(socket
->lock
);
1039 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1040 ksess
->uid
, ksess
->gid
,
1041 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1042 nb_packets_per_stream
);
1043 pthread_mutex_unlock(socket
->lock
);
1045 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1046 (void) kernel_consumer_destroy_metadata(socket
,
1048 goto error_consumer
;
1052 /* Snapshot metadata, */
1053 pthread_mutex_lock(socket
->lock
);
1054 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1055 1, ksess
->uid
, ksess
->gid
,
1056 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1057 pthread_mutex_unlock(socket
->lock
);
1059 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1060 goto error_consumer
;
1064 * The metadata snapshot is done, ask the consumer to destroy it since
1065 * it's not monitored on the consumer side.
1067 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1073 /* Close newly opened metadata stream. It's now on the consumer side. */
1074 err
= close(ksess
->metadata_stream_fd
);
1076 PERROR("close snapshot kernel");
1080 trace_kernel_destroy_metadata(ksess
->metadata
);
1082 /* Restore metadata state.*/
1083 ksess
->metadata
= saved_metadata
;
1084 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1091 * Get the syscall mask array from the kernel tracer.
1093 * Return 0 on success else a negative value. In both case, syscall_mask should
1096 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1098 assert(syscall_mask
);
1101 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);