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
,
181 char *filter_expression
,
182 struct lttng_filter_bytecode
*filter
)
185 struct ltt_kernel_event
*event
;
190 /* We pass ownership of filter_expression and filter */
191 event
= trace_kernel_create_event(ev
, filter_expression
,
198 ret
= kernctl_create_event(channel
->fd
, event
->event
);
204 WARN("Event type not implemented");
207 WARN("Event %s not found!", ev
->name
);
210 PERROR("create event ioctl");
216 event
->type
= ev
->type
;
218 /* Prevent fd duplication after execlp() */
219 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
221 PERROR("fcntl session fd");
225 ret
= kernctl_filter(event
->fd
, filter
);
231 ret
= kernctl_enable(event
->fd
);
235 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
238 PERROR("enable kernel event");
244 /* Add event to event list */
245 cds_list_add(&event
->list
, &channel
->events_list
.head
);
246 channel
->event_count
++;
248 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
257 closeret
= close(event
->fd
);
259 PERROR("close event fd");
269 * Disable a kernel channel.
271 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
277 ret
= kernctl_disable(chan
->fd
);
279 PERROR("disable chan ioctl");
285 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
294 * Enable a kernel channel.
296 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
302 ret
= kernctl_enable(chan
->fd
);
303 if (ret
< 0 && errno
!= EEXIST
) {
304 PERROR("Enable kernel chan");
309 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
318 * Enable a kernel event.
320 int kernel_enable_event(struct ltt_kernel_event
*event
)
326 ret
= kernctl_enable(event
->fd
);
330 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
333 PERROR("enable kernel event");
340 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
349 * Disable a kernel event.
351 int kernel_disable_event(struct ltt_kernel_event
*event
)
357 ret
= kernctl_disable(event
->fd
);
361 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
364 PERROR("disable kernel event");
371 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
380 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
384 DBG("Kernel track PID %d for session id %" PRIu64
".",
386 ret
= kernctl_track_pid(session
->fd
, pid
);
392 return LTTNG_ERR_INVALID
;
394 return LTTNG_ERR_NOMEM
;
396 return LTTNG_ERR_PID_TRACKED
;
398 return LTTNG_ERR_UNK
;
402 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
406 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
408 ret
= kernctl_untrack_pid(session
->fd
, pid
);
414 return LTTNG_ERR_INVALID
;
416 return LTTNG_ERR_NOMEM
;
418 return LTTNG_ERR_PID_NOT_TRACKED
;
420 return LTTNG_ERR_UNK
;
424 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
429 ssize_t nbmem
, count
= 0;
433 fd
= kernctl_list_tracker_pids(session
->fd
);
435 PERROR("kernel tracker pids list");
439 fp
= fdopen(fd
, "r");
441 PERROR("kernel tracker pids list fdopen");
445 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
446 pids
= zmalloc(sizeof(*pids
) * nbmem
);
448 PERROR("alloc list pids");
453 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
454 if (count
>= nbmem
) {
458 new_nbmem
= nbmem
<< 1;
459 DBG("Reallocating pids list from %zu to %zu entries",
461 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
462 if (new_pids
== NULL
) {
463 PERROR("realloc list events");
468 /* Zero the new memory */
469 memset(new_pids
+ nbmem
, 0,
470 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
478 DBG("Kernel list tracker pids done (%zd pids)", count
);
480 ret
= fclose(fp
); /* closes both fp and fd */
496 * Create kernel metadata, open from the kernel tracer and add it to the
499 int kernel_open_metadata(struct ltt_kernel_session
*session
)
502 struct ltt_kernel_metadata
*lkm
= NULL
;
506 /* Allocate kernel metadata */
507 lkm
= trace_kernel_create_metadata();
512 /* Kernel tracer metadata creation */
513 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
519 /* Prevent fd duplication after execlp() */
520 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
522 PERROR("fcntl session fd");
525 session
->metadata
= lkm
;
527 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
532 trace_kernel_destroy_metadata(lkm
);
538 * Start tracing session.
540 int kernel_start_session(struct ltt_kernel_session
*session
)
546 ret
= kernctl_start_session(session
->fd
);
548 PERROR("ioctl start session");
552 DBG("Kernel session started");
561 * Make a kernel wait to make sure in-flight probe have completed.
563 void kernel_wait_quiescent(int fd
)
567 DBG("Kernel quiescent wait on %d", fd
);
569 ret
= kernctl_wait_quiescent(fd
);
571 PERROR("wait quiescent ioctl");
572 ERR("Kernel quiescent wait failed");
579 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
585 ret
= kernctl_calibrate(fd
, calibrate
);
587 PERROR("calibrate ioctl");
596 * Force flush buffer of metadata.
598 int kernel_metadata_flush_buffer(int fd
)
602 DBG("Kernel flushing metadata buffer on fd %d", fd
);
604 ret
= kernctl_buffer_flush(fd
);
606 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
613 * Force flush buffer for channel.
615 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
618 struct ltt_kernel_stream
*stream
;
622 DBG("Flush buffer for channel %s", channel
->channel
->name
);
624 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
625 DBG("Flushing channel stream %d", stream
->fd
);
626 ret
= kernctl_buffer_flush(stream
->fd
);
629 ERR("Fail to flush buffer for stream %d (ret: %d)",
638 * Stop tracing session.
640 int kernel_stop_session(struct ltt_kernel_session
*session
)
646 ret
= kernctl_stop_session(session
->fd
);
651 DBG("Kernel session stopped");
660 * Open stream of channel, register it to the kernel tracer and add it
661 * to the stream list of the channel.
663 * Return the number of created stream. Else, a negative value.
665 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
668 struct ltt_kernel_stream
*lks
;
672 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
673 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
683 /* Prevent fd duplication after execlp() */
684 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
686 PERROR("fcntl session fd");
689 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
690 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
692 /* Add stream to channe stream list */
693 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
694 channel
->stream_count
++;
696 /* Increment counter which represent CPU number. */
699 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
703 return channel
->stream_count
;
710 * Open the metadata stream and set it to the kernel session.
712 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
718 ret
= kernctl_create_stream(session
->metadata
->fd
);
720 PERROR("kernel create metadata stream");
724 DBG("Kernel metadata stream created (fd: %d)", ret
);
725 session
->metadata_stream_fd
= ret
;
726 /* Prevent fd duplication after execlp() */
727 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
729 PERROR("fcntl session fd");
739 * Get the event list from the kernel tracer and return the number of elements.
741 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
745 size_t nbmem
, count
= 0;
747 struct lttng_event
*elist
;
751 fd
= kernctl_tracepoint_list(tracer_fd
);
753 PERROR("kernel tracepoint list");
757 fp
= fdopen(fd
, "r");
759 PERROR("kernel tracepoint list fdopen");
764 * Init memory size counter
765 * See kernel-ctl.h for explanation of this value
767 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
768 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
770 PERROR("alloc list events");
775 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
776 if (count
>= nbmem
) {
777 struct lttng_event
*new_elist
;
780 new_nbmem
= nbmem
<< 1;
781 DBG("Reallocating event list from %zu to %zu bytes",
783 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
784 if (new_elist
== NULL
) {
785 PERROR("realloc list events");
791 /* Zero the new memory */
792 memset(new_elist
+ nbmem
, 0,
793 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
797 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
798 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
799 elist
[count
].enabled
= -1;
805 DBG("Kernel list events done (%zu events)", count
);
807 ret
= fclose(fp
); /* closes both fp and fd */
823 * Get kernel version and validate it.
825 int kernel_validate_version(int tracer_fd
)
828 struct lttng_kernel_tracer_version version
;
829 struct lttng_kernel_tracer_abi_version abi_version
;
831 ret
= kernctl_tracer_version(tracer_fd
, &version
);
833 ERR("Failed at getting the lttng-modules version");
837 /* Validate version */
838 if (version
.major
!= VERSION_MAJOR
) {
839 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
840 version
.major
, VERSION_MAJOR
);
843 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
845 ERR("Failed at getting lttng-modules ABI version");
848 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
849 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
850 abi_version
.major
, abi_version
.minor
,
851 LTTNG_MODULES_ABI_MAJOR_VERSION
);
854 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
855 version
.major
, version
.minor
,
856 abi_version
.major
, abi_version
.minor
);
867 * Kernel work-arounds called at the start of sessiond main().
869 int init_kernel_workarounds(void)
875 * boot_id needs to be read once before being used concurrently
876 * to deal with a Linux kernel race. A fix is proposed for
877 * upstream, but the work-around is needed for older kernels.
879 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
886 ret
= fread(buf
, 1, sizeof(buf
), fp
);
888 /* Ignore error, we don't really care */
900 * Complete teardown of a kernel session.
902 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
905 DBG3("No kernel session when tearing down session");
909 DBG("Tearing down kernel session");
912 * Destroy channels on the consumer if at least one FD has been sent and we
913 * are in no output mode because the streams are in *no* monitor mode so we
914 * have to send a command to clean them up or else they leaked.
916 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
918 struct consumer_socket
*socket
;
919 struct lttng_ht_iter iter
;
921 /* For each consumer socket. */
923 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
925 struct ltt_kernel_channel
*chan
;
927 /* For each channel, ask the consumer to destroy it. */
928 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
929 ret
= kernel_consumer_destroy_channel(socket
, chan
);
931 /* Consumer is probably dead. Use next socket. */
939 /* Close any relayd session */
940 consumer_output_send_destroy_relayd(ksess
->consumer
);
942 trace_kernel_destroy_session(ksess
);
946 * Destroy a kernel channel object. It does not do anything on the tracer side.
948 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
950 struct ltt_kernel_session
*ksess
= NULL
;
953 assert(kchan
->channel
);
955 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
957 /* Update channel count of associated session. */
958 if (kchan
->session
) {
959 /* Keep pointer reference so we can update it after the destroy. */
960 ksess
= kchan
->session
;
963 trace_kernel_destroy_channel(kchan
);
966 * At this point the kernel channel is not visible anymore. This is safe
967 * since in order to work on a visible kernel session, the tracing session
968 * lock (ltt_session.lock) MUST be acquired.
971 ksess
->channel_count
--;
976 * Take a snapshot for a given kernel session.
978 * Return 0 on success or else return a LTTNG_ERR code.
980 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
981 struct snapshot_output
*output
, int wait
,
982 uint64_t nb_packets_per_stream
)
984 int err
, ret
, saved_metadata_fd
;
985 struct consumer_socket
*socket
;
986 struct lttng_ht_iter iter
;
987 struct ltt_kernel_metadata
*saved_metadata
;
990 assert(ksess
->consumer
);
993 DBG("Kernel snapshot record started");
995 /* Save current metadata since the following calls will change it. */
996 saved_metadata
= ksess
->metadata
;
997 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1001 ret
= kernel_open_metadata(ksess
);
1003 ret
= LTTNG_ERR_KERN_META_FAIL
;
1007 ret
= kernel_open_metadata_stream(ksess
);
1009 ret
= LTTNG_ERR_KERN_META_FAIL
;
1010 goto error_open_stream
;
1013 /* Send metadata to consumer and snapshot everything. */
1014 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1015 socket
, node
.node
) {
1016 struct consumer_output
*saved_output
;
1017 struct ltt_kernel_channel
*chan
;
1020 * Temporarly switch consumer output for our snapshot output. As long
1021 * as the session lock is taken, this is safe.
1023 saved_output
= ksess
->consumer
;
1024 ksess
->consumer
= output
->consumer
;
1026 pthread_mutex_lock(socket
->lock
);
1027 /* This stream must not be monitored by the consumer. */
1028 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1029 pthread_mutex_unlock(socket
->lock
);
1030 /* Put back the saved consumer output into the session. */
1031 ksess
->consumer
= saved_output
;
1033 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1034 goto error_consumer
;
1037 /* For each channel, ask the consumer to snapshot it. */
1038 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1039 pthread_mutex_lock(socket
->lock
);
1040 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1041 ksess
->uid
, ksess
->gid
,
1042 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1043 nb_packets_per_stream
);
1044 pthread_mutex_unlock(socket
->lock
);
1046 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1047 (void) kernel_consumer_destroy_metadata(socket
,
1049 goto error_consumer
;
1053 /* Snapshot metadata, */
1054 pthread_mutex_lock(socket
->lock
);
1055 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1056 1, ksess
->uid
, ksess
->gid
,
1057 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1058 pthread_mutex_unlock(socket
->lock
);
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
);