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
);
54 /* Exists but not available for this kernel */
55 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
58 /* If EEXIST, we just ignore the error */
62 PERROR("add context ioctl");
63 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
69 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
77 * Create a new kernel session, register it to the kernel tracer and add it to
78 * the session daemon session.
80 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
83 struct ltt_kernel_session
*lks
;
87 /* Allocate data structure */
88 lks
= trace_kernel_create_session();
94 /* Kernel tracer session creation */
95 ret
= kernctl_create_session(tracer_fd
);
97 PERROR("ioctl kernel create session");
102 /* Prevent fd duplication after execlp() */
103 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
105 PERROR("fcntl session fd");
108 lks
->id
= session
->id
;
109 lks
->consumer_fds_sent
= 0;
110 session
->kernel_session
= lks
;
112 DBG("Kernel session created (fd: %d)", lks
->fd
);
118 trace_kernel_destroy_session(lks
);
124 * Create a kernel channel, register it to the kernel tracer and add it to the
127 int kernel_create_channel(struct ltt_kernel_session
*session
,
128 struct lttng_channel
*chan
)
131 struct ltt_kernel_channel
*lkc
;
136 /* Allocate kernel channel */
137 lkc
= trace_kernel_create_channel(chan
);
142 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
143 chan
->name
, lkc
->channel
->attr
.overwrite
,
144 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
145 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
146 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
148 /* Kernel tracer channel creation */
149 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
151 PERROR("ioctl kernel create channel");
155 /* Setup the channel fd */
157 /* Prevent fd duplication after execlp() */
158 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
160 PERROR("fcntl session fd");
163 /* Add channel to session */
164 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
165 session
->channel_count
++;
166 lkc
->session
= session
;
168 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
181 * Create a kernel event, enable it to the kernel tracer and add it to the
182 * channel event list of the kernel session.
183 * We own filter_expression and filter.
185 int kernel_create_event(struct lttng_event
*ev
,
186 struct ltt_kernel_channel
*channel
,
187 char *filter_expression
,
188 struct lttng_filter_bytecode
*filter
)
191 struct ltt_kernel_event
*event
;
196 /* We pass ownership of filter_expression and filter */
197 event
= trace_kernel_create_event(ev
, filter_expression
,
204 ret
= kernctl_create_event(channel
->fd
, event
->event
);
210 WARN("Event type not implemented");
213 WARN("Event %s not found!", ev
->name
);
216 PERROR("create event ioctl");
222 event
->type
= ev
->type
;
224 /* Prevent fd duplication after execlp() */
225 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
227 PERROR("fcntl session fd");
231 ret
= kernctl_filter(event
->fd
, filter
);
237 ret
= kernctl_enable(event
->fd
);
241 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
244 PERROR("enable kernel event");
250 /* Add event to event list */
251 cds_list_add(&event
->list
, &channel
->events_list
.head
);
252 channel
->event_count
++;
254 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
263 closeret
= close(event
->fd
);
265 PERROR("close event fd");
275 * Disable a kernel channel.
277 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
283 ret
= kernctl_disable(chan
->fd
);
285 PERROR("disable chan ioctl");
291 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
300 * Enable a kernel channel.
302 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
308 ret
= kernctl_enable(chan
->fd
);
309 if (ret
< 0 && errno
!= EEXIST
) {
310 PERROR("Enable kernel chan");
315 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
324 * Enable a kernel event.
326 int kernel_enable_event(struct ltt_kernel_event
*event
)
332 ret
= kernctl_enable(event
->fd
);
336 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
339 PERROR("enable kernel event");
346 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
355 * Disable a kernel event.
357 int kernel_disable_event(struct ltt_kernel_event
*event
)
363 ret
= kernctl_disable(event
->fd
);
367 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
370 PERROR("disable kernel event");
377 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
386 int kernel_track_pid(struct ltt_kernel_session
*session
, int pid
)
390 DBG("Kernel track PID %d for session id %" PRIu64
".",
392 ret
= kernctl_track_pid(session
->fd
, pid
);
398 return LTTNG_ERR_INVALID
;
400 return LTTNG_ERR_NOMEM
;
402 return LTTNG_ERR_PID_TRACKED
;
404 return LTTNG_ERR_UNK
;
408 int kernel_untrack_pid(struct ltt_kernel_session
*session
, int pid
)
412 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
414 ret
= kernctl_untrack_pid(session
->fd
, pid
);
420 return LTTNG_ERR_INVALID
;
422 return LTTNG_ERR_NOMEM
;
424 return LTTNG_ERR_PID_NOT_TRACKED
;
426 return LTTNG_ERR_UNK
;
430 ssize_t
kernel_list_tracker_pids(struct ltt_kernel_session
*session
,
435 ssize_t nbmem
, count
= 0;
439 fd
= kernctl_list_tracker_pids(session
->fd
);
441 PERROR("kernel tracker pids list");
445 fp
= fdopen(fd
, "r");
447 PERROR("kernel tracker pids list fdopen");
451 nbmem
= KERNEL_TRACKER_PIDS_INIT_LIST_SIZE
;
452 pids
= zmalloc(sizeof(*pids
) * nbmem
);
454 PERROR("alloc list pids");
459 while (fscanf(fp
, "process { pid = %u; };\n", &pid
) == 1) {
460 if (count
>= nbmem
) {
464 new_nbmem
= nbmem
<< 1;
465 DBG("Reallocating pids list from %zu to %zu entries",
467 new_pids
= realloc(pids
, new_nbmem
* sizeof(*new_pids
));
468 if (new_pids
== NULL
) {
469 PERROR("realloc list events");
474 /* Zero the new memory */
475 memset(new_pids
+ nbmem
, 0,
476 (new_nbmem
- nbmem
) * sizeof(*new_pids
));
484 DBG("Kernel list tracker pids done (%zd pids)", count
);
486 ret
= fclose(fp
); /* closes both fp and fd */
502 * Create kernel metadata, open from the kernel tracer and add it to the
505 int kernel_open_metadata(struct ltt_kernel_session
*session
)
508 struct ltt_kernel_metadata
*lkm
= NULL
;
512 /* Allocate kernel metadata */
513 lkm
= trace_kernel_create_metadata();
518 /* Kernel tracer metadata creation */
519 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
525 /* Prevent fd duplication after execlp() */
526 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
528 PERROR("fcntl session fd");
531 session
->metadata
= lkm
;
533 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
538 trace_kernel_destroy_metadata(lkm
);
544 * Start tracing session.
546 int kernel_start_session(struct ltt_kernel_session
*session
)
552 ret
= kernctl_start_session(session
->fd
);
554 PERROR("ioctl start session");
558 DBG("Kernel session started");
567 * Make a kernel wait to make sure in-flight probe have completed.
569 void kernel_wait_quiescent(int fd
)
573 DBG("Kernel quiescent wait on %d", fd
);
575 ret
= kernctl_wait_quiescent(fd
);
577 PERROR("wait quiescent ioctl");
578 ERR("Kernel quiescent wait failed");
585 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
591 ret
= kernctl_calibrate(fd
, calibrate
);
593 PERROR("calibrate ioctl");
602 * Force flush buffer of metadata.
604 int kernel_metadata_flush_buffer(int fd
)
608 DBG("Kernel flushing metadata buffer on fd %d", fd
);
610 ret
= kernctl_buffer_flush(fd
);
612 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
619 * Force flush buffer for channel.
621 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
624 struct ltt_kernel_stream
*stream
;
628 DBG("Flush buffer for channel %s", channel
->channel
->name
);
630 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
631 DBG("Flushing channel stream %d", stream
->fd
);
632 ret
= kernctl_buffer_flush(stream
->fd
);
635 ERR("Fail to flush buffer for stream %d (ret: %d)",
644 * Stop tracing session.
646 int kernel_stop_session(struct ltt_kernel_session
*session
)
652 ret
= kernctl_stop_session(session
->fd
);
657 DBG("Kernel session stopped");
666 * Open stream of channel, register it to the kernel tracer and add it
667 * to the stream list of the channel.
669 * Return the number of created stream. Else, a negative value.
671 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
674 struct ltt_kernel_stream
*lks
;
678 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
679 lks
= trace_kernel_create_stream(channel
->channel
->name
, 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 channe stream list */
699 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
700 channel
->stream_count
++;
702 /* Increment counter which represent CPU number. */
705 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
709 return channel
->stream_count
;
716 * Open the metadata stream and set it to the kernel session.
718 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
724 ret
= kernctl_create_stream(session
->metadata
->fd
);
726 PERROR("kernel create metadata stream");
730 DBG("Kernel metadata stream created (fd: %d)", ret
);
731 session
->metadata_stream_fd
= ret
;
732 /* Prevent fd duplication after execlp() */
733 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
735 PERROR("fcntl session fd");
745 * Get the event list from the kernel tracer and return the number of elements.
747 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
751 size_t nbmem
, count
= 0;
753 struct lttng_event
*elist
;
757 fd
= kernctl_tracepoint_list(tracer_fd
);
759 PERROR("kernel tracepoint list");
763 fp
= fdopen(fd
, "r");
765 PERROR("kernel tracepoint list fdopen");
770 * Init memory size counter
771 * See kernel-ctl.h for explanation of this value
773 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
774 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
776 PERROR("alloc list events");
781 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
782 if (count
>= nbmem
) {
783 struct lttng_event
*new_elist
;
786 new_nbmem
= nbmem
<< 1;
787 DBG("Reallocating event list from %zu to %zu bytes",
789 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
790 if (new_elist
== NULL
) {
791 PERROR("realloc list events");
797 /* Zero the new memory */
798 memset(new_elist
+ nbmem
, 0,
799 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
803 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
804 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
805 elist
[count
].enabled
= -1;
811 DBG("Kernel list events done (%zu events)", count
);
813 ret
= fclose(fp
); /* closes both fp and fd */
829 * Get kernel version and validate it.
831 int kernel_validate_version(int tracer_fd
)
834 struct lttng_kernel_tracer_version version
;
835 struct lttng_kernel_tracer_abi_version abi_version
;
837 ret
= kernctl_tracer_version(tracer_fd
, &version
);
839 ERR("Failed at getting the lttng-modules version");
843 /* Validate version */
844 if (version
.major
!= VERSION_MAJOR
) {
845 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
846 version
.major
, VERSION_MAJOR
);
849 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi_version
);
851 ERR("Failed at getting lttng-modules ABI version");
854 if (abi_version
.major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
855 ERR("Kernel tracer ABI version (%d.%d) is not compatible with expected ABI major version (%d.*)",
856 abi_version
.major
, abi_version
.minor
,
857 LTTNG_MODULES_ABI_MAJOR_VERSION
);
860 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
861 version
.major
, version
.minor
,
862 abi_version
.major
, abi_version
.minor
);
873 * Kernel work-arounds called at the start of sessiond main().
875 int init_kernel_workarounds(void)
881 * boot_id needs to be read once before being used concurrently
882 * to deal with a Linux kernel race. A fix is proposed for
883 * upstream, but the work-around is needed for older kernels.
885 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
892 ret
= fread(buf
, 1, sizeof(buf
), fp
);
894 /* Ignore error, we don't really care */
906 * Complete teardown of a kernel session.
908 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
911 DBG3("No kernel session when tearing down session");
915 DBG("Tearing down kernel session");
918 * Destroy channels on the consumer if at least one FD has been sent and we
919 * are in no output mode because the streams are in *no* monitor mode so we
920 * have to send a command to clean them up or else they leaked.
922 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
924 struct consumer_socket
*socket
;
925 struct lttng_ht_iter iter
;
927 /* For each consumer socket. */
929 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
931 struct ltt_kernel_channel
*chan
;
933 /* For each channel, ask the consumer to destroy it. */
934 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
935 ret
= kernel_consumer_destroy_channel(socket
, chan
);
937 /* Consumer is probably dead. Use next socket. */
945 /* Close any relayd session */
946 consumer_output_send_destroy_relayd(ksess
->consumer
);
948 trace_kernel_destroy_session(ksess
);
952 * Destroy a kernel channel object. It does not do anything on the tracer side.
954 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
956 struct ltt_kernel_session
*ksess
= NULL
;
959 assert(kchan
->channel
);
961 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
963 /* Update channel count of associated session. */
964 if (kchan
->session
) {
965 /* Keep pointer reference so we can update it after the destroy. */
966 ksess
= kchan
->session
;
969 trace_kernel_destroy_channel(kchan
);
972 * At this point the kernel channel is not visible anymore. This is safe
973 * since in order to work on a visible kernel session, the tracing session
974 * lock (ltt_session.lock) MUST be acquired.
977 ksess
->channel_count
--;
982 * Take a snapshot for a given kernel session.
984 * Return 0 on success or else return a LTTNG_ERR code.
986 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
987 struct snapshot_output
*output
, int wait
,
988 uint64_t nb_packets_per_stream
)
990 int err
, ret
, saved_metadata_fd
;
991 struct consumer_socket
*socket
;
992 struct lttng_ht_iter iter
;
993 struct ltt_kernel_metadata
*saved_metadata
;
996 assert(ksess
->consumer
);
999 DBG("Kernel snapshot record started");
1001 /* Save current metadata since the following calls will change it. */
1002 saved_metadata
= ksess
->metadata
;
1003 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1007 ret
= kernel_open_metadata(ksess
);
1009 ret
= LTTNG_ERR_KERN_META_FAIL
;
1013 ret
= kernel_open_metadata_stream(ksess
);
1015 ret
= LTTNG_ERR_KERN_META_FAIL
;
1016 goto error_open_stream
;
1019 /* Send metadata to consumer and snapshot everything. */
1020 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1021 socket
, node
.node
) {
1022 struct consumer_output
*saved_output
;
1023 struct ltt_kernel_channel
*chan
;
1026 * Temporarly switch consumer output for our snapshot output. As long
1027 * as the session lock is taken, this is safe.
1029 saved_output
= ksess
->consumer
;
1030 ksess
->consumer
= output
->consumer
;
1032 pthread_mutex_lock(socket
->lock
);
1033 /* This stream must not be monitored by the consumer. */
1034 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1035 pthread_mutex_unlock(socket
->lock
);
1036 /* Put back the saved consumer output into the session. */
1037 ksess
->consumer
= saved_output
;
1039 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1040 goto error_consumer
;
1043 /* For each channel, ask the consumer to snapshot it. */
1044 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1045 pthread_mutex_lock(socket
->lock
);
1046 ret
= consumer_snapshot_channel(socket
, chan
->fd
, output
, 0,
1047 ksess
->uid
, ksess
->gid
,
1048 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1049 nb_packets_per_stream
);
1050 pthread_mutex_unlock(socket
->lock
);
1052 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1053 (void) kernel_consumer_destroy_metadata(socket
,
1055 goto error_consumer
;
1059 /* Snapshot metadata, */
1060 pthread_mutex_lock(socket
->lock
);
1061 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->fd
, output
,
1062 1, ksess
->uid
, ksess
->gid
,
1063 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0);
1064 pthread_mutex_unlock(socket
->lock
);
1066 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1067 goto error_consumer
;
1071 * The metadata snapshot is done, ask the consumer to destroy it since
1072 * it's not monitored on the consumer side.
1074 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1080 /* Close newly opened metadata stream. It's now on the consumer side. */
1081 err
= close(ksess
->metadata_stream_fd
);
1083 PERROR("close snapshot kernel");
1087 trace_kernel_destroy_metadata(ksess
->metadata
);
1089 /* Restore metadata state.*/
1090 ksess
->metadata
= saved_metadata
;
1091 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1098 * Get the syscall mask array from the kernel tracer.
1100 * Return 0 on success else a negative value. In both case, syscall_mask should
1103 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1105 assert(syscall_mask
);
1108 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);