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"
39 * Key used to reference a channel between the sessiond and the consumer. This
40 * is only read and updated with the session_list lock held.
42 static uint64_t next_kernel_channel_key
;
45 * Add context on a kernel channel.
47 * Assumes the ownership of ctx.
49 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
50 struct ltt_kernel_context
*ctx
)
57 DBG("Adding context to channel %s", chan
->channel
->name
);
58 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
62 /* Exists but not available for this kernel */
63 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
66 /* If EEXIST, we just ignore the error */
70 PERROR("add context ioctl");
71 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
78 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
83 trace_kernel_destroy_context(ctx
);
89 * Create a new kernel session, register it to the kernel tracer and add it to
90 * the session daemon session.
92 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
95 struct ltt_kernel_session
*lks
;
99 /* Allocate data structure */
100 lks
= trace_kernel_create_session();
106 /* Kernel tracer session creation */
107 ret
= kernctl_create_session(tracer_fd
);
109 PERROR("ioctl kernel create session");
114 /* Prevent fd duplication after execlp() */
115 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
117 PERROR("fcntl session fd");
120 lks
->id
= session
->id
;
121 lks
->consumer_fds_sent
= 0;
122 session
->kernel_session
= lks
;
124 DBG("Kernel session created (fd: %d)", lks
->fd
);
130 trace_kernel_destroy_session(lks
);
136 * Create a kernel channel, register it to the kernel tracer and add it to the
139 int kernel_create_channel(struct ltt_kernel_session
*session
,
140 struct lttng_channel
*chan
)
143 struct ltt_kernel_channel
*lkc
;
148 /* Allocate kernel channel */
149 lkc
= trace_kernel_create_channel(chan
);
154 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
155 chan
->name
, lkc
->channel
->attr
.overwrite
,
156 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
157 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
158 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
160 /* Kernel tracer channel creation */
161 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
163 PERROR("ioctl kernel create channel");
167 /* Setup the channel fd */
169 /* Prevent fd duplication after execlp() */
170 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
172 PERROR("fcntl session fd");
175 /* Add channel to session */
176 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
177 session
->channel_count
++;
178 lkc
->session
= session
;
179 lkc
->key
= ++next_kernel_channel_key
;
181 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
182 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
195 * Create a kernel event, enable it to the kernel tracer and add it to the
196 * channel event list of the kernel session.
197 * We own filter_expression and filter.
199 int kernel_create_event(struct lttng_event
*ev
,
200 struct ltt_kernel_channel
*channel
,
201 char *filter_expression
,
202 struct lttng_filter_bytecode
*filter
)
205 struct ltt_kernel_event
*event
;
210 /* We pass ownership of filter_expression and filter */
211 event
= trace_kernel_create_event(ev
, filter_expression
,
218 ret
= kernctl_create_event(channel
->fd
, event
->event
);
224 WARN("Event type not implemented");
227 WARN("Event %s not found!", ev
->name
);
230 PERROR("create event ioctl");
235 event
->type
= ev
->type
;
237 /* Prevent fd duplication after execlp() */
238 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
240 PERROR("fcntl session fd");
244 ret
= kernctl_filter(event
->fd
, filter
);
250 ret
= kernctl_enable(event
->fd
);
254 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
257 PERROR("enable kernel event");
263 /* Add event to event list */
264 cds_list_add(&event
->list
, &channel
->events_list
.head
);
265 channel
->event_count
++;
267 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
276 closeret
= close(event
->fd
);
278 PERROR("close event fd");
288 * Disable a kernel channel.
290 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
296 ret
= kernctl_disable(chan
->fd
);
298 PERROR("disable chan ioctl");
303 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
304 chan
->channel
->name
, chan
->fd
, chan
->key
);
313 * Enable a kernel channel.
315 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
321 ret
= kernctl_enable(chan
->fd
);
322 if (ret
< 0 && ret
!= -EEXIST
) {
323 PERROR("Enable kernel chan");
328 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
329 chan
->channel
->name
, chan
->fd
, chan
->key
);
338 * Enable a kernel event.
340 int kernel_enable_event(struct ltt_kernel_event
*event
)
346 ret
= kernctl_enable(event
->fd
);
350 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
353 PERROR("enable kernel event");
360 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
369 * Disable a kernel event.
371 int kernel_disable_event(struct ltt_kernel_event
*event
)
377 ret
= kernctl_disable(event
->fd
);
381 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
384 PERROR("disable kernel event");
391 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
400 struct lttng_tracker_list
*get_id_tracker_list(struct ltt_kernel_session
*session
,
401 enum lttng_tracker_type tracker_type
)
403 switch (tracker_type
) {
404 case LTTNG_TRACKER_PID
:
405 return session
->tracker_list_pid
;
406 case LTTNG_TRACKER_VPID
:
407 return session
->tracker_list_vpid
;
408 case LTTNG_TRACKER_UID
:
409 return session
->tracker_list_uid
;
410 case LTTNG_TRACKER_VUID
:
411 return session
->tracker_list_vuid
;
412 case LTTNG_TRACKER_GID
:
413 return session
->tracker_list_gid
;
414 case LTTNG_TRACKER_VGID
:
415 return session
->tracker_list_vgid
;
421 int kernel_track_id(enum lttng_tracker_type tracker_type
,
422 struct ltt_kernel_session
*session
,
423 struct lttng_tracker_id
*id
)
426 struct lttng_tracker_list
*tracker_list
;
428 ret
= lttng_tracker_id_lookup_string(tracker_type
,
430 if (ret
!= LTTNG_OK
) {
435 tracker_list
= get_id_tracker_list(session
, tracker_type
);
437 return LTTNG_ERR_INVALID
;
439 ret
= lttng_tracker_list_add(tracker_list
, id
);
440 if (ret
!= LTTNG_OK
) {
444 switch (tracker_type
) {
445 case LTTNG_TRACKER_PID
:
446 DBG("Kernel track PID %d for session id %" PRIu64
".",
448 ret
= kernctl_track_pid(session
->fd
, value
);
453 case LTTNG_TRACKER_VPID
:
454 DBG("Kernel track VPID %d for session id %" PRIu64
".",
456 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VPID
, value
);
461 case LTTNG_TRACKER_UID
:
462 DBG("Kernel track UID %d for session id %" PRIu64
".",
464 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_UID
, value
);
469 case LTTNG_TRACKER_GID
:
470 DBG("Kernel track GID %d for session id %" PRIu64
".",
472 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_GID
, value
);
477 case LTTNG_TRACKER_VUID
:
478 DBG("Kernel track VUID %d for session id %" PRIu64
".",
480 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VUID
, value
);
485 case LTTNG_TRACKER_VGID
:
486 DBG("Kernel track VGID %d for session id %" PRIu64
".",
488 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VGID
, value
);
494 return LTTNG_ERR_INVALID
;
498 return LTTNG_ERR_INVALID
;
500 return LTTNG_ERR_NOMEM
;
502 return LTTNG_ERR_ID_TRACKED
;
504 return LTTNG_ERR_UNK
;
508 int kernel_untrack_id(enum lttng_tracker_type tracker_type
,
509 struct ltt_kernel_session
*session
,
510 struct lttng_tracker_id
*id
)
513 struct lttng_tracker_list
*tracker_list
;
515 ret
= lttng_tracker_id_lookup_string(tracker_type
,
517 if (ret
!= LTTNG_OK
) {
521 /* Remove from list. */
522 tracker_list
= get_id_tracker_list(session
, tracker_type
);
524 return LTTNG_ERR_INVALID
;
526 ret
= lttng_tracker_list_remove(tracker_list
, id
);
527 if (ret
!= LTTNG_OK
) {
531 switch (tracker_type
) {
532 case LTTNG_TRACKER_PID
:
533 DBG("Kernel untrack PID %d for session id %" PRIu64
".",
535 ret
= kernctl_untrack_pid(session
->fd
, value
);
540 case LTTNG_TRACKER_VPID
:
541 DBG("Kernel untrack VPID %d for session id %" PRIu64
".",
543 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_VPID
, value
);
548 case LTTNG_TRACKER_UID
:
549 DBG("Kernel untrack UID %d for session id %" PRIu64
".",
551 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_UID
, value
);
556 case LTTNG_TRACKER_GID
:
557 DBG("Kernel untrack GID %d for session id %" PRIu64
".",
559 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_GID
, value
);
564 case LTTNG_TRACKER_VUID
:
565 DBG("Kernel untrack VUID %d for session id %" PRIu64
".",
567 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_VUID
, value
);
572 case LTTNG_TRACKER_VGID
:
573 DBG("Kernel untrack VGID %d for session id %" PRIu64
".",
575 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_VGID
, value
);
581 return LTTNG_ERR_INVALID
;
586 return LTTNG_ERR_INVALID
;
588 return LTTNG_ERR_NOMEM
;
590 return LTTNG_ERR_ID_NOT_TRACKED
;
592 return LTTNG_ERR_UNK
;
597 * Called with session lock held.
599 ssize_t
kernel_list_tracker_ids(enum lttng_tracker_type tracker_type
,
600 struct ltt_kernel_session
*session
,
601 struct lttng_tracker_id
**_ids
)
603 struct lttng_tracker_list
*tracker_list
;
605 tracker_list
= get_id_tracker_list(session
, tracker_type
);
607 return -LTTNG_ERR_INVALID
;
609 return lttng_tracker_id_get_list(tracker_list
, _ids
);
613 * Create kernel metadata, open from the kernel tracer and add it to the
616 int kernel_open_metadata(struct ltt_kernel_session
*session
)
619 struct ltt_kernel_metadata
*lkm
= NULL
;
623 /* Allocate kernel metadata */
624 lkm
= trace_kernel_create_metadata();
629 /* Kernel tracer metadata creation */
630 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
636 lkm
->key
= ++next_kernel_channel_key
;
637 /* Prevent fd duplication after execlp() */
638 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
640 PERROR("fcntl session fd");
643 session
->metadata
= lkm
;
645 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
650 trace_kernel_destroy_metadata(lkm
);
656 * Start tracing session.
658 int kernel_start_session(struct ltt_kernel_session
*session
)
664 ret
= kernctl_start_session(session
->fd
);
666 PERROR("ioctl start session");
670 DBG("Kernel session started");
679 * Make a kernel wait to make sure in-flight probe have completed.
681 void kernel_wait_quiescent(int fd
)
685 DBG("Kernel quiescent wait on %d", fd
);
687 ret
= kernctl_wait_quiescent(fd
);
689 PERROR("wait quiescent ioctl");
690 ERR("Kernel quiescent wait failed");
695 * Force flush buffer of metadata.
697 int kernel_metadata_flush_buffer(int fd
)
701 DBG("Kernel flushing metadata buffer on fd %d", fd
);
703 ret
= kernctl_buffer_flush(fd
);
705 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
712 * Force flush buffer for channel.
714 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
717 struct ltt_kernel_stream
*stream
;
721 DBG("Flush buffer for channel %s", channel
->channel
->name
);
723 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
724 DBG("Flushing channel stream %d", stream
->fd
);
725 ret
= kernctl_buffer_flush(stream
->fd
);
728 ERR("Fail to flush buffer for stream %d (ret: %d)",
737 * Stop tracing session.
739 int kernel_stop_session(struct ltt_kernel_session
*session
)
745 ret
= kernctl_stop_session(session
->fd
);
750 DBG("Kernel session stopped");
759 * Open stream of channel, register it to the kernel tracer and add it
760 * to the stream list of the channel.
762 * Note: given that the streams may appear in random order wrt CPU
763 * number (e.g. cpu hotplug), the index value of the stream number in
764 * the stream name is not necessarily linked to the CPU number.
766 * Return the number of created stream. Else, a negative value.
768 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
771 struct ltt_kernel_stream
*lks
;
775 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
776 lks
= trace_kernel_create_stream(channel
->channel
->name
,
777 channel
->stream_count
);
787 /* Prevent fd duplication after execlp() */
788 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
790 PERROR("fcntl session fd");
793 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
794 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
796 /* Add stream to channel stream list */
797 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
798 channel
->stream_count
++;
800 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
804 return channel
->stream_count
;
811 * Open the metadata stream and set it to the kernel session.
813 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
819 ret
= kernctl_create_stream(session
->metadata
->fd
);
821 PERROR("kernel create metadata stream");
825 DBG("Kernel metadata stream created (fd: %d)", ret
);
826 session
->metadata_stream_fd
= ret
;
827 /* Prevent fd duplication after execlp() */
828 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
830 PERROR("fcntl session fd");
840 * Get the event list from the kernel tracer and return the number of elements.
842 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
846 size_t nbmem
, count
= 0;
848 struct lttng_event
*elist
;
852 fd
= kernctl_tracepoint_list(tracer_fd
);
854 PERROR("kernel tracepoint list");
858 fp
= fdopen(fd
, "r");
860 PERROR("kernel tracepoint list fdopen");
865 * Init memory size counter
866 * See kernel-ctl.h for explanation of this value
868 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
869 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
871 PERROR("alloc list events");
876 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
877 if (count
>= nbmem
) {
878 struct lttng_event
*new_elist
;
881 new_nbmem
= nbmem
<< 1;
882 DBG("Reallocating event list from %zu to %zu bytes",
884 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
885 if (new_elist
== NULL
) {
886 PERROR("realloc list events");
892 /* Zero the new memory */
893 memset(new_elist
+ nbmem
, 0,
894 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
898 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
899 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
900 elist
[count
].enabled
= -1;
906 DBG("Kernel list events done (%zu events)", count
);
908 ret
= fclose(fp
); /* closes both fp and fd */
924 * Get kernel version and validate it.
926 int kernel_validate_version(int tracer_fd
,
927 struct lttng_kernel_tracer_version
*version
,
928 struct lttng_kernel_tracer_abi_version
*abi_version
)
932 ret
= kernctl_tracer_version(tracer_fd
, version
);
934 ERR("Failed to retrieve the lttng-modules version");
938 /* Validate version */
939 if (version
->major
!= VERSION_MAJOR
) {
940 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
941 version
->major
, VERSION_MAJOR
);
944 ret
= kernctl_tracer_abi_version(tracer_fd
, abi_version
);
946 ERR("Failed to retrieve lttng-modules ABI version");
949 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
950 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
951 abi_version
->major
, abi_version
->minor
,
952 LTTNG_MODULES_ABI_MAJOR_VERSION
);
955 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
956 version
->major
, version
->minor
,
957 abi_version
->major
, abi_version
->minor
);
964 ERR("Kernel tracer version check failed; kernel tracing will not be available");
969 * Kernel work-arounds called at the start of sessiond main().
971 int init_kernel_workarounds(void)
977 * boot_id needs to be read once before being used concurrently
978 * to deal with a Linux kernel race. A fix is proposed for
979 * upstream, but the work-around is needed for older kernels.
981 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
988 ret
= fread(buf
, 1, sizeof(buf
), fp
);
990 /* Ignore error, we don't really care */
1002 * Complete teardown of a kernel session.
1004 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1006 if (ksess
== NULL
) {
1007 DBG3("No kernel session when tearing down session");
1011 DBG("Tearing down kernel session");
1014 * Destroy channels on the consumer if at least one FD has been sent and we
1015 * are in no output mode because the streams are in *no* monitor mode so we
1016 * have to send a command to clean them up or else they leaked.
1018 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1020 struct consumer_socket
*socket
;
1021 struct lttng_ht_iter iter
;
1023 /* For each consumer socket. */
1025 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1026 socket
, node
.node
) {
1027 struct ltt_kernel_channel
*chan
;
1029 /* For each channel, ask the consumer to destroy it. */
1030 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1031 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1033 /* Consumer is probably dead. Use next socket. */
1041 /* Close any relayd session */
1042 consumer_output_send_destroy_relayd(ksess
->consumer
);
1044 trace_kernel_destroy_session(ksess
);
1048 * Destroy a kernel channel object. It does not do anything on the tracer side.
1050 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1052 struct ltt_kernel_session
*ksess
= NULL
;
1055 assert(kchan
->channel
);
1057 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1059 /* Update channel count of associated session. */
1060 if (kchan
->session
) {
1061 /* Keep pointer reference so we can update it after the destroy. */
1062 ksess
= kchan
->session
;
1065 trace_kernel_destroy_channel(kchan
);
1068 * At this point the kernel channel is not visible anymore. This is safe
1069 * since in order to work on a visible kernel session, the tracing session
1070 * lock (ltt_session.lock) MUST be acquired.
1073 ksess
->channel_count
--;
1078 * Take a snapshot for a given kernel session.
1080 * Return 0 on success or else return a LTTNG_ERR code.
1082 int kernel_snapshot_record(struct ltt_kernel_session
*ksess
,
1083 struct snapshot_output
*output
, int wait
,
1084 uint64_t nb_packets_per_stream
)
1086 int err
, ret
, saved_metadata_fd
;
1087 struct consumer_socket
*socket
;
1088 struct lttng_ht_iter iter
;
1089 struct ltt_kernel_metadata
*saved_metadata
;
1090 struct ltt_session
*session
;
1091 uint64_t trace_archive_id
;
1094 assert(ksess
->consumer
);
1097 DBG("Kernel snapshot record started");
1099 session
= session_find_by_id(ksess
->id
);
1101 assert(pthread_mutex_trylock(&session
->lock
));
1102 assert(session_trylock_list());
1103 trace_archive_id
= session
->current_archive_id
;
1105 /* Save current metadata since the following calls will change it. */
1106 saved_metadata
= ksess
->metadata
;
1107 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1111 ret
= kernel_open_metadata(ksess
);
1113 ret
= LTTNG_ERR_KERN_META_FAIL
;
1117 ret
= kernel_open_metadata_stream(ksess
);
1119 ret
= LTTNG_ERR_KERN_META_FAIL
;
1120 goto error_open_stream
;
1123 /* Send metadata to consumer and snapshot everything. */
1124 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1125 socket
, node
.node
) {
1126 struct consumer_output
*saved_output
;
1127 struct ltt_kernel_channel
*chan
;
1130 * Temporarly switch consumer output for our snapshot output. As long
1131 * as the session lock is taken, this is safe.
1133 saved_output
= ksess
->consumer
;
1134 ksess
->consumer
= output
->consumer
;
1136 pthread_mutex_lock(socket
->lock
);
1137 /* This stream must not be monitored by the consumer. */
1138 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1139 pthread_mutex_unlock(socket
->lock
);
1140 /* Put back the saved consumer output into the session. */
1141 ksess
->consumer
= saved_output
;
1143 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1144 goto error_consumer
;
1147 /* For each channel, ask the consumer to snapshot it. */
1148 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1149 ret
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1150 ksess
->uid
, ksess
->gid
,
1151 DEFAULT_KERNEL_TRACE_DIR
, wait
,
1152 nb_packets_per_stream
,
1155 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1156 (void) kernel_consumer_destroy_metadata(socket
,
1158 goto error_consumer
;
1162 /* Snapshot metadata, */
1163 ret
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1164 1, ksess
->uid
, ksess
->gid
,
1165 DEFAULT_KERNEL_TRACE_DIR
, wait
, 0,
1168 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1169 goto error_consumer
;
1173 * The metadata snapshot is done, ask the consumer to destroy it since
1174 * it's not monitored on the consumer side.
1176 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1182 /* Close newly opened metadata stream. It's now on the consumer side. */
1183 err
= close(ksess
->metadata_stream_fd
);
1185 PERROR("close snapshot kernel");
1189 trace_kernel_destroy_metadata(ksess
->metadata
);
1191 /* Restore metadata state.*/
1192 ksess
->metadata
= saved_metadata
;
1193 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1200 * Get the syscall mask array from the kernel tracer.
1202 * Return 0 on success else a negative value. In both case, syscall_mask should
1205 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1207 assert(syscall_mask
);
1210 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1214 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1217 * Return 1 on success, 0 when feature is not supported, negative value in case
1220 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd
)
1222 int ret
= 0; // Not supported by default
1223 struct lttng_kernel_tracer_abi_version abi
;
1225 ret
= kernctl_tracer_abi_version(tracer_fd
, &abi
);
1227 ERR("Failed to retrieve lttng-modules ABI version");
1232 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1234 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1246 * Rotate a kernel session.
1248 * Return 0 on success or else return a LTTNG_ERR code.
1250 int kernel_rotate_session(struct ltt_session
*session
)
1253 struct consumer_socket
*socket
;
1254 struct lttng_ht_iter iter
;
1255 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1258 assert(ksess
->consumer
);
1260 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1261 session
->name
, session
->id
);
1266 * Note that this loop will end after one iteration given that there is
1267 * only one kernel consumer.
1269 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1270 socket
, node
.node
) {
1271 struct ltt_kernel_channel
*chan
;
1274 * Account the metadata channel first to make sure the
1275 * number of channels waiting for a rotation cannot
1276 * reach 0 before we complete the iteration over all
1279 ret
= rotate_add_channel_pending(ksess
->metadata
->key
,
1280 LTTNG_DOMAIN_KERNEL
, session
);
1282 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1286 /* For each channel, ask the consumer to rotate it. */
1287 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1288 ret
= rotate_add_channel_pending(chan
->key
,
1289 LTTNG_DOMAIN_KERNEL
, session
);
1291 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1295 DBG("Rotate channel %" PRIu64
", session %s", chan
->key
, session
->name
);
1296 ret
= consumer_rotate_channel(socket
, chan
->key
,
1297 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1298 ksess
->consumer
->subdir
,
1299 /* is_metadata_channel */ false,
1300 session
->current_archive_id
,
1301 &session
->rotate_pending_relay
);
1303 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1309 * Rotate the metadata channel.
1311 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1312 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1313 ksess
->consumer
->subdir
,
1314 /* is_metadata_channel */ true,
1315 session
->current_archive_id
,
1316 &session
->rotate_pending_relay
);
1318 ret
= LTTNG_ERR_KERN_CONSUMER_FAIL
;