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");
213 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
215 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
216 DBG2("Kernel event syscall creation success");
218 * We use fd == -1 to ensure that we never trigger a close of fd
226 /* Prevent fd duplication after execlp() */
227 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
229 PERROR("fcntl session fd");
233 /* Add event to event list */
234 cds_list_add(&event
->list
, &channel
->events_list
.head
);
235 channel
->event_count
++;
237 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
248 * Disable a kernel channel.
250 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
256 ret
= kernctl_disable(chan
->fd
);
258 PERROR("disable chan ioctl");
264 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
273 * Enable a kernel channel.
275 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
281 ret
= kernctl_enable(chan
->fd
);
282 if (ret
< 0 && errno
!= EEXIST
) {
283 PERROR("Enable kernel chan");
288 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
297 * Enable a kernel event.
299 int kernel_enable_event(struct ltt_kernel_event
*event
)
305 ret
= kernctl_enable(event
->fd
);
309 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
312 PERROR("enable kernel event");
319 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
328 * Disable a kernel event.
330 int kernel_disable_event(struct ltt_kernel_event
*event
)
336 ret
= kernctl_disable(event
->fd
);
340 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
343 PERROR("disable kernel event");
350 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
358 int kernel_enable_syscall(const char *syscall_name
,
359 struct ltt_kernel_channel
*channel
)
361 return kernctl_enable_syscall(channel
->fd
, syscall_name
);
364 int kernel_disable_syscall(const char *syscall_name
,
365 struct ltt_kernel_channel
*channel
)
367 return kernctl_disable_syscall(channel
->fd
, syscall_name
);
370 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
372 DBG("Kernel track PID %d for session id %" PRIu64
".",
374 return kernctl_track_pid(session
->fd
, pid
);
377 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
379 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
381 return kernctl_untrack_pid(session
->fd
, pid
);
384 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
389 ssize_t nbmem
, count
= 0;
393 fd
= kernctl_list_tracker_pids(session
->fd
);
395 PERROR("kernel tracker pids list");
399 fp
= fdopen(fd
, "r");
401 PERROR("kernel tracker pids list fdopen");
405 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
406 pids
= zmalloc(sizeof(*pids
) * nbmem
);
408 PERROR("alloc list pids");
413 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
414 if (count
>= nbmem
) {
418 new_nbmem
= nbmem
<< 1;
419 DBG("Reallocating pids list from %zu to %zu entries",
421 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
422 if (new_pids
== NULL
) {
423 PERROR("realloc list events");
428 /* Zero the new memory */
429 memset(new_pids
+ nbmem
, 0,
430 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
438 DBG("Kernel list tracker pids done (%zd pids)", count
);
440 ret
= fclose(fp
); /* closes both fp and fd */
456 * Create kernel metadata, open from the kernel tracer and add it to the
459 int kernel_open_metadata(struct ltt_kernel_session
*session
)
462 struct ltt_kernel_metadata
*lkm
= NULL
;
466 /* Allocate kernel metadata */
467 lkm
= trace_kernel_create_metadata();
472 /* Kernel tracer metadata creation */
473 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
479 /* Prevent fd duplication after execlp() */
480 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
482 PERROR("fcntl session fd");
485 session
->metadata
= lkm
;
487 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
492 trace_kernel_destroy_metadata(lkm
);
498 * Start tracing session.
500 int kernel_start_session(struct ltt_kernel_session
*session
)
506 ret
= kernctl_start_session(session
->fd
);
508 PERROR("ioctl start session");
512 DBG("Kernel session started");
521 * Make a kernel wait to make sure in-flight probe have completed.
523 void kernel_wait_quiescent(int fd
)
527 DBG("Kernel quiescent wait on %d", fd
);
529 ret
= kernctl_wait_quiescent(fd
);
531 PERROR("wait quiescent ioctl");
532 ERR("Kernel quiescent wait failed");
539 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
545 ret
= kernctl_calibrate(fd
, calibrate
);
547 PERROR("calibrate ioctl");
556 * Force flush buffer of metadata.
558 int kernel_metadata_flush_buffer(int fd
)
562 DBG("Kernel flushing metadata buffer on fd %d", fd
);
564 ret
= kernctl_buffer_flush(fd
);
566 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
573 * Force flush buffer for channel.
575 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
578 struct ltt_kernel_stream
*stream
;
582 DBG("Flush buffer for channel %s", channel
->channel
->name
);
584 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
585 DBG("Flushing channel stream %d", stream
->fd
);
586 ret
= kernctl_buffer_flush(stream
->fd
);
589 ERR("Fail to flush buffer for stream %d (ret: %d)",
598 * Stop tracing session.
600 int kernel_stop_session(struct ltt_kernel_session
*session
)
606 ret
= kernctl_stop_session(session
->fd
);
611 DBG("Kernel session stopped");
620 * Open stream of channel, register it to the kernel tracer and add it
621 * to the stream list of the channel.
623 * Return the number of created stream. Else, a negative value.
625 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
628 struct ltt_kernel_stream
*lks
;
632 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
633 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
643 /* Prevent fd duplication after execlp() */
644 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
646 PERROR("fcntl session fd");
649 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
650 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
652 /* Add stream to channe stream list */
653 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
654 channel
->stream_count
++;
656 /* Increment counter which represent CPU number. */
659 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
663 return channel
->stream_count
;
670 * Open the metadata stream and set it to the kernel session.
672 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
678 ret
= kernctl_create_stream(session
->metadata
->fd
);
680 PERROR("kernel create metadata stream");
684 DBG("Kernel metadata stream created (fd: %d)", ret
);
685 session
->metadata_stream_fd
= ret
;
686 /* Prevent fd duplication after execlp() */
687 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
689 PERROR("fcntl session fd");
699 * Get the event list from the kernel tracer and return the number of elements.
701 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
705 size_t nbmem
, count
= 0;
707 struct lttng_event
*elist
;
711 fd
= kernctl_tracepoint_list(tracer_fd
);
713 PERROR("kernel tracepoint list");
717 fp
= fdopen(fd
, "r");
719 PERROR("kernel tracepoint list fdopen");
724 * Init memory size counter
725 * See kernel-ctl.h for explanation of this value
727 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
728 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
730 PERROR("alloc list events");
735 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
736 if (count
>= nbmem
) {
737 struct lttng_event
*new_elist
;
740 new_nbmem
= nbmem
<< 1;
741 DBG("Reallocating event list from %zu to %zu bytes",
743 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
744 if (new_elist
== NULL
) {
745 PERROR("realloc list events");
751 /* Zero the new memory */
752 memset(new_elist
+ nbmem
, 0,
753 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
757 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
758 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
759 elist
[count
].enabled
= -1;
765 DBG("Kernel list events done (%zu events)", count
);
767 ret
= fclose(fp
); /* closes both fp and fd */
783 * Get kernel version and validate it.
785 int kernel_validate_version(int tracer_fd
)
788 struct lttng_kernel_tracer_version version
;
789 struct lttng_kernel_tracer_abi_version abi_version
;
791 ret
= kernctl_tracer_version(tracer_fd
, &version
);
793 ERR("Failed at getting the lttng-modules version");
797 /* Validate version */
798 if (version
.major
!= VERSION_MAJOR
) {
799 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
800 version
.major
, VERSION_MAJOR
);
803 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
805 ERR("Failed at getting lttng-modules ABI version");
808 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
809 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
810 abi_version
.major
, abi_version
.minor
,
811 LTTNG_MODULES_ABI_MAJOR_VERSION
);
814 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
815 version
.major
, version
.minor
,
816 abi_version
.major
, abi_version
.minor
);
827 * Kernel work-arounds called at the start of sessiond main().
829 int init_kernel_workarounds(void)
835 * boot_id needs to be read once before being used concurrently
836 * to deal with a Linux kernel race. A fix is proposed for
837 * upstream, but the work-around is needed for older kernels.
839 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
846 ret
= fread(buf
, 1, sizeof(buf
), fp
);
848 /* Ignore error, we don't really care */
860 * Complete teardown of a kernel session.
862 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
865 DBG3("No kernel session when tearing down session");
869 DBG("Tearing down kernel session");
872 * Destroy channels on the consumer if at least one FD has been sent and we
873 * are in no output mode because the streams are in *no* monitor mode so we
874 * have to send a command to clean them up or else they leaked.
876 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
878 struct consumer_socket
*socket
;
879 struct lttng_ht_iter iter
;
881 /* For each consumer socket. */
883 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
885 struct ltt_kernel_channel
*chan
;
887 /* For each channel, ask the consumer to destroy it. */
888 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
889 ret
= kernel_consumer_destroy_channel(socket
, chan
);
891 /* Consumer is probably dead. Use next socket. */
899 /* Close any relayd session */
900 consumer_output_send_destroy_relayd(ksess
->consumer
);
902 trace_kernel_destroy_session(ksess
);
906 * Destroy a kernel channel object. It does not do anything on the tracer side.
908 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
910 struct ltt_kernel_session
*ksess
= NULL
;
913 assert(kchan
->channel
);
915 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
917 /* Update channel count of associated session. */
918 if (kchan
->session
) {
919 /* Keep pointer reference so we can update it after the destroy. */
920 ksess
= kchan
->session
;
923 trace_kernel_destroy_channel(kchan
);
926 * At this point the kernel channel is not visible anymore. This is safe
927 * since in order to work on a visible kernel session, the tracing session
928 * lock (ltt_session.lock) MUST be acquired.
931 ksess
->channel_count
--;
936 * Take a snapshot for a given kernel session.
938 * Return 0 on success or else return a LTTNG_ERR code.
940 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
941 struct snapshot_output
*output
, int wait
,
942 uint64_t nb_packets_per_stream
)
944 int err
, ret
, saved_metadata_fd
;
945 struct consumer_socket
*socket
;
946 struct lttng_ht_iter iter
;
947 struct ltt_kernel_metadata
*saved_metadata
;
950 assert(ksess
->consumer
);
953 DBG("Kernel snapshot record started");
955 /* Save current metadata since the following calls will change it. */
956 saved_metadata
= ksess
->metadata
;
957 saved_metadata_fd
= ksess
->metadata_stream_fd
;
961 ret
= kernel_open_metadata(ksess
);
963 ret
= LTTNG_ERR_KERN_META_FAIL
;
967 ret
= kernel_open_metadata_stream(ksess
);
969 ret
= LTTNG_ERR_KERN_META_FAIL
;
970 goto error_open_stream
;
973 /* Send metadata to consumer and snapshot everything. */
974 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
976 struct consumer_output
*saved_output
;
977 struct ltt_kernel_channel
*chan
;
980 * Temporarly switch consumer output for our snapshot output. As long
981 * as the session lock is taken, this is safe.
983 saved_output
= ksess
->consumer
;
984 ksess
->consumer
= output
->consumer
;
986 pthread_mutex_lock(socket
->lock
);
987 /* This stream must not be monitored by the consumer. */
988 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
989 pthread_mutex_unlock(socket
->lock
);
990 /* Put back the saved consumer output into the session. */
991 ksess
->consumer
= saved_output
;
993 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
997 /* For each channel, ask the consumer to snapshot it. */
998 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
999 pthread_mutex_lock(socket
->lock
);
1000 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1001 ksess
->uid
, ksess
->gid
,
1002 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1003 nb_packets_per_stream
);
1004 pthread_mutex_unlock(socket
->lock
);
1006 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1007 (void) kernel_consumer_destroy_metadata(socket
,
1009 goto error_consumer
;
1013 /* Snapshot metadata, */
1014 pthread_mutex_lock(socket
->lock
);
1015 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1016 1, ksess
->uid
, ksess
->gid
,
1017 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1018 pthread_mutex_unlock(socket
->lock
);
1020 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1021 goto error_consumer
;
1025 * The metadata snapshot is done, ask the consumer to destroy it since
1026 * it's not monitored on the consumer side.
1028 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1034 /* Close newly opened metadata stream. It's now on the consumer side. */
1035 err
= close(ksess
->metadata_stream_fd
);
1037 PERROR("close snapshot kernel");
1041 trace_kernel_destroy_metadata(ksess
->metadata
);
1043 /* Restore metadata state.*/
1044 ksess
->metadata
= saved_metadata
;
1045 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1052 * Get the syscall mask array from the kernel tracer.
1054 * Return 0 on success else a negative value. In both case, syscall_mask should
1057 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1059 assert(syscall_mask
);
1062 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);