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.
25 #include <sys/types.h>
27 #include <common/common.h>
28 #include <common/trace-chunk.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>
33 #include "lttng-sessiond.h"
34 #include "lttng-syscall.h"
37 #include "kernel-consumer.h"
38 #include "kern-modules.h"
44 * Key used to reference a channel between the sessiond and the consumer. This
45 * is only read and updated with the session_list lock held.
47 static uint64_t next_kernel_channel_key
;
49 static const char *module_proc_lttng
= "/proc/lttng";
51 static int kernel_tracer_fd
= -1;
53 #include <lttng/userspace-probe.h>
54 #include <lttng/userspace-probe-internal.h>
56 * Add context on a kernel channel.
58 * Assumes the ownership of ctx.
60 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
61 struct ltt_kernel_context
*ctx
)
68 DBG("Adding context to channel %s", chan
->channel
->name
);
69 ret
= kernctl_add_context(chan
->fd
, &ctx
->ctx
);
73 /* Exists but not available for this kernel */
74 ret
= LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE
;
77 /* If EEXIST, we just ignore the error */
81 PERROR("add context ioctl");
82 ret
= LTTNG_ERR_KERN_CONTEXT_FAIL
;
89 cds_list_add_tail(&ctx
->list
, &chan
->ctx_list
);
94 trace_kernel_destroy_context(ctx
);
100 * Create a new kernel session, register it to the kernel tracer and add it to
101 * the session daemon session.
103 int kernel_create_session(struct ltt_session
*session
)
106 struct ltt_kernel_session
*lks
;
110 /* Allocate data structure */
111 lks
= trace_kernel_create_session();
117 /* Kernel tracer session creation */
118 ret
= kernctl_create_session(kernel_tracer_fd
);
120 PERROR("ioctl kernel create session");
125 /* Prevent fd duplication after execlp() */
126 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
128 PERROR("fcntl session fd");
131 lks
->id
= session
->id
;
132 lks
->consumer_fds_sent
= 0;
133 session
->kernel_session
= lks
;
135 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * This is necessary since the creation time is present in the session
139 * name when it is generated.
141 if (session
->has_auto_generated_name
) {
142 ret
= kernctl_session_set_name(lks
->fd
, DEFAULT_SESSION_NAME
);
144 ret
= kernctl_session_set_name(lks
->fd
, session
->name
);
147 WARN("Could not set kernel session name for session %" PRIu64
" name: %s",
148 session
->id
, session
->name
);
151 ret
= kernctl_session_set_creation_time(lks
->fd
, session
->creation_time
);
153 WARN("Could not set kernel session creation time for session %" PRIu64
" name: %s",
154 session
->id
, session
->name
);
161 trace_kernel_destroy_session(lks
);
162 trace_kernel_free_session(lks
);
168 * Create a kernel channel, register it to the kernel tracer and add it to the
171 int kernel_create_channel(struct ltt_kernel_session
*session
,
172 struct lttng_channel
*chan
)
175 struct ltt_kernel_channel
*lkc
;
180 /* Allocate kernel channel */
181 lkc
= trace_kernel_create_channel(chan
);
186 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d, %d",
187 chan
->name
, lkc
->channel
->attr
.overwrite
,
188 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
189 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
190 lkc
->channel
->attr
.live_timer_interval
, lkc
->channel
->attr
.output
);
192 /* Kernel tracer channel creation */
193 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
195 PERROR("ioctl kernel create channel");
199 /* Setup the channel fd */
201 /* Prevent fd duplication after execlp() */
202 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
204 PERROR("fcntl session fd");
207 /* Add channel to session */
208 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
209 session
->channel_count
++;
210 lkc
->session
= session
;
211 lkc
->key
= ++next_kernel_channel_key
;
213 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64
")",
214 lkc
->channel
->name
, lkc
->fd
, lkc
->key
);
227 * Compute the offset of the instrumentation byte in the binary based on the
228 * function probe location using the ELF lookup method.
230 * Returns 0 on success and set the offset out parameter to the offset of the
232 * Returns -1 on error
235 int extract_userspace_probe_offset_function_elf(
236 const struct lttng_userspace_probe_location
*probe_location
,
237 struct ltt_kernel_session
*session
, uint64_t *offset
)
241 const char *symbol
= NULL
;
242 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
243 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
245 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
246 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION
);
248 lookup
= lttng_userspace_probe_location_get_lookup_method(
256 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
258 assert(lookup_method_type
==
259 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
);
261 symbol
= lttng_userspace_probe_location_function_get_function_name(
268 fd
= lttng_userspace_probe_location_function_get_binary_fd(probe_location
);
274 ret
= run_as_extract_elf_symbol_offset(fd
, symbol
, session
->uid
,
275 session
->gid
, offset
);
277 DBG("userspace probe offset calculation failed for "
278 "function %s", symbol
);
282 DBG("userspace probe elf offset for %s is 0x%jd", symbol
, (intmax_t)(*offset
));
288 * Compute the offsets of the instrumentation bytes in the binary based on the
289 * tracepoint probe location using the SDT lookup method. This function
290 * allocates the offsets buffer, the caller must free it.
292 * Returns 0 on success and set the offset out parameter to the offsets of the
294 * Returns -1 on error.
297 int extract_userspace_probe_offset_tracepoint_sdt(
298 const struct lttng_userspace_probe_location
*probe_location
,
299 struct ltt_kernel_session
*session
, uint64_t **offsets
,
300 uint32_t *offsets_count
)
302 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type
;
303 const struct lttng_userspace_probe_location_lookup_method
*lookup
= NULL
;
304 const char *probe_name
= NULL
, *provider_name
= NULL
;
308 assert(lttng_userspace_probe_location_get_type(probe_location
) ==
309 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT
);
311 lookup
= lttng_userspace_probe_location_get_lookup_method(probe_location
);
318 lttng_userspace_probe_location_lookup_method_get_type(lookup
);
320 assert(lookup_method_type
==
321 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
);
324 probe_name
= lttng_userspace_probe_location_tracepoint_get_probe_name(
331 provider_name
= lttng_userspace_probe_location_tracepoint_get_provider_name(
333 if (!provider_name
) {
338 fd
= lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location
);
344 ret
= run_as_extract_sdt_probe_offsets(fd
, provider_name
, probe_name
,
345 session
->uid
, session
->gid
, offsets
, offsets_count
);
347 DBG("userspace probe offset calculation failed for sdt "
348 "probe %s:%s", provider_name
, probe_name
);
352 if (*offsets_count
== 0) {
353 DBG("no userspace probe offset found");
357 DBG("%u userspace probe SDT offsets found for %s:%s at:",
358 *offsets_count
, provider_name
, probe_name
);
359 for (i
= 0; i
< *offsets_count
; i
++) {
360 DBG("\t0x%jd", (intmax_t)((*offsets
)[i
]));
367 * Extract the offsets of the instrumentation point for the different lookup
371 int userspace_probe_add_callsites(struct lttng_event
*ev
,
372 struct ltt_kernel_session
*session
, int fd
)
374 const struct lttng_userspace_probe_location_lookup_method
*lookup_method
= NULL
;
375 enum lttng_userspace_probe_location_lookup_method_type type
;
376 const struct lttng_userspace_probe_location
*location
= NULL
;
380 assert(ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
);
382 location
= lttng_event_get_userspace_probe_location(ev
);
388 lttng_userspace_probe_location_get_lookup_method(location
);
389 if (!lookup_method
) {
394 type
= lttng_userspace_probe_location_lookup_method_get_type(lookup_method
);
396 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF
:
398 struct lttng_kernel_event_callsite callsite
;
401 ret
= extract_userspace_probe_offset_function_elf(location
, session
, &offset
);
403 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
407 callsite
.u
.uprobe
.offset
= offset
;
408 ret
= kernctl_add_callsite(fd
, &callsite
);
410 WARN("Adding callsite to userspace probe "
411 "event %s failed.", ev
->name
);
412 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
417 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT
:
420 uint64_t *offsets
= NULL
;
421 uint32_t offsets_count
;
422 struct lttng_kernel_event_callsite callsite
;
425 * This call allocates the offsets buffer. This buffer must be freed
428 ret
= extract_userspace_probe_offset_tracepoint_sdt(location
, session
,
429 &offsets
, &offsets_count
);
431 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
434 for (i
= 0; i
< offsets_count
; i
++) {
435 callsite
.u
.uprobe
.offset
= offsets
[i
];
436 ret
= kernctl_add_callsite(fd
, &callsite
);
438 WARN("Adding callsite to userspace probe "
439 "event %s failed.", ev
->name
);
440 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
449 ret
= LTTNG_ERR_PROBE_LOCATION_INVAL
;
457 * Create a kernel event, enable it to the kernel tracer and add it to the
458 * channel event list of the kernel session.
459 * We own filter_expression and filter.
461 int kernel_create_event(struct lttng_event
*ev
,
462 struct ltt_kernel_channel
*channel
,
463 char *filter_expression
,
464 struct lttng_filter_bytecode
*filter
)
467 enum lttng_error_code ret
;
468 struct ltt_kernel_event
*event
;
473 /* We pass ownership of filter_expression and filter */
474 ret
= trace_kernel_create_event(ev
, filter_expression
,
476 if (ret
!= LTTNG_OK
) {
480 fd
= kernctl_create_event(channel
->fd
, event
->event
);
484 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
487 WARN("Event type not implemented");
488 ret
= LTTNG_ERR_KERN_EVENT_ENOSYS
;
491 WARN("Event %s not found!", ev
->name
);
492 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
495 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
496 PERROR("create event ioctl");
501 event
->type
= ev
->type
;
503 /* Prevent fd duplication after execlp() */
504 err
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
506 PERROR("fcntl session fd");
510 err
= kernctl_filter(event
->fd
, filter
);
514 ret
= LTTNG_ERR_FILTER_NOMEM
;
517 ret
= LTTNG_ERR_FILTER_INVAL
;
524 if (ev
->type
== LTTNG_EVENT_USERSPACE_PROBE
) {
525 ret
= userspace_probe_add_callsites(ev
, channel
->session
, event
->fd
);
527 goto add_callsite_error
;
531 err
= kernctl_enable(event
->fd
);
535 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
538 PERROR("enable kernel event");
539 ret
= LTTNG_ERR_KERN_ENABLE_FAIL
;
545 /* Add event to event list */
546 cds_list_add(&event
->list
, &channel
->events_list
.head
);
547 channel
->event_count
++;
549 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
559 closeret
= close(event
->fd
);
561 PERROR("close event fd");
571 * Disable a kernel channel.
573 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
579 ret
= kernctl_disable(chan
->fd
);
581 PERROR("disable chan ioctl");
586 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64
")",
587 chan
->channel
->name
, chan
->fd
, chan
->key
);
596 * Enable a kernel channel.
598 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
604 ret
= kernctl_enable(chan
->fd
);
605 if (ret
< 0 && ret
!= -EEXIST
) {
606 PERROR("Enable kernel chan");
611 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64
")",
612 chan
->channel
->name
, chan
->fd
, chan
->key
);
621 * Enable a kernel event.
623 int kernel_enable_event(struct ltt_kernel_event
*event
)
629 ret
= kernctl_enable(event
->fd
);
633 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
636 PERROR("enable kernel event");
643 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
652 * Disable a kernel event.
654 int kernel_disable_event(struct ltt_kernel_event
*event
)
660 ret
= kernctl_disable(event
->fd
);
664 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
667 PERROR("disable kernel event");
674 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
682 static struct lttng_tracker_list
*get_id_tracker_list(
683 struct ltt_kernel_session
*session
,
684 enum lttng_tracker_type tracker_type
)
686 switch (tracker_type
) {
687 case LTTNG_TRACKER_PID
:
688 return session
->tracker_list_pid
;
689 case LTTNG_TRACKER_VPID
:
690 return session
->tracker_list_vpid
;
691 case LTTNG_TRACKER_UID
:
692 return session
->tracker_list_uid
;
693 case LTTNG_TRACKER_VUID
:
694 return session
->tracker_list_vuid
;
695 case LTTNG_TRACKER_GID
:
696 return session
->tracker_list_gid
;
697 case LTTNG_TRACKER_VGID
:
698 return session
->tracker_list_vgid
;
704 int kernel_track_id(enum lttng_tracker_type tracker_type
,
705 struct ltt_kernel_session
*session
,
706 const struct lttng_tracker_id
*id
)
709 struct lttng_tracker_list
*tracker_list
;
710 struct lttng_tracker_ids
*saved_ids
;
712 ret
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
713 if (ret
!= LTTNG_OK
) {
717 tracker_list
= get_id_tracker_list(session
, tracker_type
);
719 return LTTNG_ERR_INVALID
;
722 /* Save list for restore on error. */
723 ret
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
724 if (ret
!= LTTNG_OK
) {
725 return LTTNG_ERR_INVALID
;
729 ret
= lttng_tracker_list_add(tracker_list
, id
);
730 if (ret
!= LTTNG_OK
) {
734 switch (tracker_type
) {
735 case LTTNG_TRACKER_PID
:
736 DBG("Kernel track PID %d for session id %" PRIu64
".", value
,
738 ret
= kernctl_track_pid(session
->fd
, value
);
744 case LTTNG_TRACKER_VPID
:
745 DBG("Kernel track VPID %d for session id %" PRIu64
".", value
,
747 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VPID
, value
);
753 case LTTNG_TRACKER_UID
:
754 DBG("Kernel track UID %d for session id %" PRIu64
".", value
,
756 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_UID
, value
);
762 case LTTNG_TRACKER_GID
:
763 DBG("Kernel track GID %d for session id %" PRIu64
".", value
,
765 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_GID
, value
);
771 case LTTNG_TRACKER_VUID
:
772 DBG("Kernel track VUID %d for session id %" PRIu64
".", value
,
774 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VUID
, value
);
780 case LTTNG_TRACKER_VGID
:
781 DBG("Kernel track VGID %d for session id %" PRIu64
".", value
,
783 ret
= kernctl_track_id(session
->fd
, LTTNG_TRACKER_VGID
, value
);
794 /* Error handling. */
797 ret
= LTTNG_ERR_INVALID
;
800 ret
= LTTNG_ERR_NOMEM
;
803 ret
= LTTNG_ERR_ID_TRACKED
;
810 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
811 ERR("Error on tracker add error handling.\n");
814 lttng_tracker_ids_destroy(saved_ids
);
818 int kernel_untrack_id(enum lttng_tracker_type tracker_type
,
819 struct ltt_kernel_session
*session
,
820 const struct lttng_tracker_id
*id
)
823 struct lttng_tracker_list
*tracker_list
;
824 struct lttng_tracker_ids
*saved_ids
;
826 ret
= lttng_tracker_id_lookup_string(tracker_type
, id
, &value
);
827 if (ret
!= LTTNG_OK
) {
831 tracker_list
= get_id_tracker_list(session
, tracker_type
);
833 return LTTNG_ERR_INVALID
;
835 /* Save list for restore on error. */
836 ret
= lttng_tracker_id_get_list(tracker_list
, &saved_ids
);
837 if (ret
!= LTTNG_OK
) {
838 return LTTNG_ERR_INVALID
;
840 /* Remove from list. */
841 ret
= lttng_tracker_list_remove(tracker_list
, id
);
842 if (ret
!= LTTNG_OK
) {
846 switch (tracker_type
) {
847 case LTTNG_TRACKER_PID
:
848 DBG("Kernel untrack PID %d for session id %" PRIu64
".", value
,
850 ret
= kernctl_untrack_pid(session
->fd
, value
);
856 case LTTNG_TRACKER_VPID
:
857 DBG("Kernel untrack VPID %d for session id %" PRIu64
".", value
,
859 ret
= kernctl_untrack_id(
860 session
->fd
, LTTNG_TRACKER_VPID
, value
);
866 case LTTNG_TRACKER_UID
:
867 DBG("Kernel untrack UID %d for session id %" PRIu64
".", value
,
869 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_UID
, value
);
875 case LTTNG_TRACKER_GID
:
876 DBG("Kernel untrack GID %d for session id %" PRIu64
".", value
,
878 ret
= kernctl_untrack_id(session
->fd
, LTTNG_TRACKER_GID
, value
);
884 case LTTNG_TRACKER_VUID
:
885 DBG("Kernel untrack VUID %d for session id %" PRIu64
".", value
,
887 ret
= kernctl_untrack_id(
888 session
->fd
, LTTNG_TRACKER_VUID
, value
);
894 case LTTNG_TRACKER_VGID
:
895 DBG("Kernel untrack VGID %d for session id %" PRIu64
".", value
,
897 ret
= kernctl_untrack_id(
898 session
->fd
, LTTNG_TRACKER_VGID
, value
);
909 /* Error handling. */
912 ret
= LTTNG_ERR_INVALID
;
915 ret
= LTTNG_ERR_NOMEM
;
918 ret
= LTTNG_ERR_ID_TRACKED
;
925 if (lttng_tracker_id_set_list(tracker_list
, saved_ids
) != LTTNG_OK
) {
926 ERR("Error on tracker remove error handling.\n");
929 lttng_tracker_ids_destroy(saved_ids
);
934 * Called with session lock held.
936 int kernel_list_tracker_ids(enum lttng_tracker_type tracker_type
,
937 struct ltt_kernel_session
*session
,
938 struct lttng_tracker_ids
**_ids
)
941 struct lttng_tracker_list
*tracker_list
;
943 tracker_list
= get_id_tracker_list(session
, tracker_type
);
945 ret
= -LTTNG_ERR_INVALID
;
949 ret
= lttng_tracker_id_get_list(tracker_list
, _ids
);
950 if (ret
!= LTTNG_OK
) {
951 ret
= -LTTNG_ERR_INVALID
;
960 * Create kernel metadata, open from the kernel tracer and add it to the
963 int kernel_open_metadata(struct ltt_kernel_session
*session
)
966 struct ltt_kernel_metadata
*lkm
= NULL
;
970 /* Allocate kernel metadata */
971 lkm
= trace_kernel_create_metadata();
976 /* Kernel tracer metadata creation */
977 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
983 lkm
->key
= ++next_kernel_channel_key
;
984 /* Prevent fd duplication after execlp() */
985 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
987 PERROR("fcntl session fd");
990 session
->metadata
= lkm
;
992 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
997 trace_kernel_destroy_metadata(lkm
);
1003 * Start tracing session.
1005 int kernel_start_session(struct ltt_kernel_session
*session
)
1011 ret
= kernctl_start_session(session
->fd
);
1013 PERROR("ioctl start session");
1017 DBG("Kernel session started");
1026 * Make a kernel wait to make sure in-flight probe have completed.
1028 void kernel_wait_quiescent(void)
1031 int fd
= kernel_tracer_fd
;
1033 DBG("Kernel quiescent wait on %d", fd
);
1035 ret
= kernctl_wait_quiescent(fd
);
1037 PERROR("wait quiescent ioctl");
1038 ERR("Kernel quiescent wait failed");
1043 * Force flush buffer of metadata.
1045 int kernel_metadata_flush_buffer(int fd
)
1049 DBG("Kernel flushing metadata buffer on fd %d", fd
);
1051 ret
= kernctl_buffer_flush(fd
);
1053 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
1060 * Force flush buffer for channel.
1062 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
1065 struct ltt_kernel_stream
*stream
;
1069 DBG("Flush buffer for channel %s", channel
->channel
->name
);
1071 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
1072 DBG("Flushing channel stream %d", stream
->fd
);
1073 ret
= kernctl_buffer_flush(stream
->fd
);
1076 ERR("Fail to flush buffer for stream %d (ret: %d)",
1085 * Stop tracing session.
1087 int kernel_stop_session(struct ltt_kernel_session
*session
)
1093 ret
= kernctl_stop_session(session
->fd
);
1098 DBG("Kernel session stopped");
1107 * Open stream of channel, register it to the kernel tracer and add it
1108 * to the stream list of the channel.
1110 * Note: given that the streams may appear in random order wrt CPU
1111 * number (e.g. cpu hotplug), the index value of the stream number in
1112 * the stream name is not necessarily linked to the CPU number.
1114 * Return the number of created stream. Else, a negative value.
1116 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
1119 struct ltt_kernel_stream
*lks
;
1123 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
1124 lks
= trace_kernel_create_stream(channel
->channel
->name
,
1125 channel
->stream_count
);
1135 /* Prevent fd duplication after execlp() */
1136 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
1138 PERROR("fcntl session fd");
1141 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
1142 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
1144 /* Add stream to channel stream list */
1145 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
1146 channel
->stream_count
++;
1148 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
1152 return channel
->stream_count
;
1159 * Open the metadata stream and set it to the kernel session.
1161 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
1167 ret
= kernctl_create_stream(session
->metadata
->fd
);
1169 PERROR("kernel create metadata stream");
1173 DBG("Kernel metadata stream created (fd: %d)", ret
);
1174 session
->metadata_stream_fd
= ret
;
1175 /* Prevent fd duplication after execlp() */
1176 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
1178 PERROR("fcntl session fd");
1188 * Get the event list from the kernel tracer and return the number of elements.
1190 ssize_t
kernel_list_events(struct lttng_event
**events
)
1194 size_t nbmem
, count
= 0;
1196 struct lttng_event
*elist
;
1200 fd
= kernctl_tracepoint_list(kernel_tracer_fd
);
1202 PERROR("kernel tracepoint list");
1206 fp
= fdopen(fd
, "r");
1208 PERROR("kernel tracepoint list fdopen");
1213 * Init memory size counter
1214 * See kernel-ctl.h for explanation of this value
1216 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
1217 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
1218 if (elist
== NULL
) {
1219 PERROR("alloc list events");
1224 while (fscanf(fp
, "event { name = %m[^;]; };\n", &event
) == 1) {
1225 if (count
>= nbmem
) {
1226 struct lttng_event
*new_elist
;
1229 new_nbmem
= nbmem
<< 1;
1230 DBG("Reallocating event list from %zu to %zu bytes",
1232 new_elist
= realloc(elist
, new_nbmem
* sizeof(struct lttng_event
));
1233 if (new_elist
== NULL
) {
1234 PERROR("realloc list events");
1240 /* Zero the new memory */
1241 memset(new_elist
+ nbmem
, 0,
1242 (new_nbmem
- nbmem
) * sizeof(struct lttng_event
));
1246 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
1247 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
1248 elist
[count
].enabled
= -1;
1254 DBG("Kernel list events done (%zu events)", count
);
1256 ret
= fclose(fp
); /* closes both fp and fd */
1272 * Get kernel version and validate it.
1274 int kernel_validate_version(struct lttng_kernel_tracer_version
*version
,
1275 struct lttng_kernel_tracer_abi_version
*abi_version
)
1279 ret
= kernctl_tracer_version(kernel_tracer_fd
, version
);
1281 ERR("Failed to retrieve the lttng-modules version");
1285 /* Validate version */
1286 if (version
->major
!= VERSION_MAJOR
) {
1287 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1288 version
->major
, VERSION_MAJOR
);
1291 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, abi_version
);
1293 ERR("Failed to retrieve lttng-modules ABI version");
1296 if (abi_version
->major
!= LTTNG_MODULES_ABI_MAJOR_VERSION
) {
1297 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1298 abi_version
->major
, abi_version
->minor
,
1299 LTTNG_MODULES_ABI_MAJOR_VERSION
);
1302 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1303 version
->major
, version
->minor
,
1304 abi_version
->major
, abi_version
->minor
);
1311 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1316 * Kernel work-arounds called at the start of sessiond main().
1318 int init_kernel_workarounds(void)
1324 * boot_id needs to be read once before being used concurrently
1325 * to deal with a Linux kernel race. A fix is proposed for
1326 * upstream, but the work-around is needed for older kernels.
1328 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
1335 ret
= fread(buf
, 1, sizeof(buf
), fp
);
1337 /* Ignore error, we don't really care */
1349 * Teardown of a kernel session, keeping data required by destroy notifiers.
1351 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
1353 struct lttng_trace_chunk
*trace_chunk
;
1355 if (ksess
== NULL
) {
1356 DBG3("No kernel session when tearing down session");
1360 DBG("Tearing down kernel session");
1361 trace_chunk
= ksess
->current_trace_chunk
;
1364 * Destroy channels on the consumer if at least one FD has been sent and we
1365 * are in no output mode because the streams are in *no* monitor mode so we
1366 * have to send a command to clean them up or else they leaked.
1368 if (!ksess
->output_traces
&& ksess
->consumer_fds_sent
) {
1370 struct consumer_socket
*socket
;
1371 struct lttng_ht_iter iter
;
1373 /* For each consumer socket. */
1375 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1376 socket
, node
.node
) {
1377 struct ltt_kernel_channel
*chan
;
1379 /* For each channel, ask the consumer to destroy it. */
1380 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1381 ret
= kernel_consumer_destroy_channel(socket
, chan
);
1383 /* Consumer is probably dead. Use next socket. */
1391 /* Close any relayd session */
1392 consumer_output_send_destroy_relayd(ksess
->consumer
);
1394 trace_kernel_destroy_session(ksess
);
1395 lttng_trace_chunk_put(trace_chunk
);
1398 /* Teardown of data required by destroy notifiers. */
1399 void kernel_free_session(struct ltt_kernel_session
*ksess
)
1401 if (ksess
== NULL
) {
1404 trace_kernel_free_session(ksess
);
1408 * Destroy a kernel channel object. It does not do anything on the tracer side.
1410 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
1412 struct ltt_kernel_session
*ksess
= NULL
;
1415 assert(kchan
->channel
);
1417 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
1419 /* Update channel count of associated session. */
1420 if (kchan
->session
) {
1421 /* Keep pointer reference so we can update it after the destroy. */
1422 ksess
= kchan
->session
;
1425 trace_kernel_destroy_channel(kchan
);
1428 * At this point the kernel channel is not visible anymore. This is safe
1429 * since in order to work on a visible kernel session, the tracing session
1430 * lock (ltt_session.lock) MUST be acquired.
1433 ksess
->channel_count
--;
1438 * Take a snapshot for a given kernel session.
1440 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1442 enum lttng_error_code
kernel_snapshot_record(
1443 struct ltt_kernel_session
*ksess
,
1444 const struct consumer_output
*output
, int wait
,
1445 uint64_t nb_packets_per_stream
)
1447 int err
, ret
, saved_metadata_fd
;
1448 enum lttng_error_code status
= LTTNG_OK
;
1449 struct consumer_socket
*socket
;
1450 struct lttng_ht_iter iter
;
1451 struct ltt_kernel_metadata
*saved_metadata
;
1452 char *trace_path
= NULL
;
1453 size_t consumer_path_offset
= 0;
1456 assert(ksess
->consumer
);
1459 DBG("Kernel snapshot record started");
1461 /* Save current metadata since the following calls will change it. */
1462 saved_metadata
= ksess
->metadata
;
1463 saved_metadata_fd
= ksess
->metadata_stream_fd
;
1467 ret
= kernel_open_metadata(ksess
);
1469 status
= LTTNG_ERR_KERN_META_FAIL
;
1473 ret
= kernel_open_metadata_stream(ksess
);
1475 status
= LTTNG_ERR_KERN_META_FAIL
;
1476 goto error_open_stream
;
1479 trace_path
= setup_channel_trace_path(ksess
->consumer
,
1480 DEFAULT_KERNEL_TRACE_DIR
, &consumer_path_offset
);
1482 status
= LTTNG_ERR_INVALID
;
1485 /* Send metadata to consumer and snapshot everything. */
1486 cds_lfht_for_each_entry(output
->socks
->ht
, &iter
.iter
,
1487 socket
, node
.node
) {
1488 struct ltt_kernel_channel
*chan
;
1490 pthread_mutex_lock(socket
->lock
);
1491 /* This stream must not be monitored by the consumer. */
1492 ret
= kernel_consumer_add_metadata(socket
, ksess
, 0);
1493 pthread_mutex_unlock(socket
->lock
);
1495 status
= LTTNG_ERR_KERN_META_FAIL
;
1496 goto error_consumer
;
1499 /* For each channel, ask the consumer to snapshot it. */
1500 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1501 status
= consumer_snapshot_channel(socket
, chan
->key
, output
, 0,
1502 ksess
->uid
, ksess
->gid
,
1503 &trace_path
[consumer_path_offset
], wait
,
1504 nb_packets_per_stream
);
1505 if (status
!= LTTNG_OK
) {
1506 (void) kernel_consumer_destroy_metadata(socket
,
1508 goto error_consumer
;
1512 /* Snapshot metadata, */
1513 status
= consumer_snapshot_channel(socket
, ksess
->metadata
->key
, output
,
1514 1, ksess
->uid
, ksess
->gid
, &trace_path
[consumer_path_offset
],
1516 if (status
!= LTTNG_OK
) {
1517 goto error_consumer
;
1521 * The metadata snapshot is done, ask the consumer to destroy it since
1522 * it's not monitored on the consumer side.
1524 (void) kernel_consumer_destroy_metadata(socket
, ksess
->metadata
);
1528 /* Close newly opened metadata stream. It's now on the consumer side. */
1529 err
= close(ksess
->metadata_stream_fd
);
1531 PERROR("close snapshot kernel");
1535 trace_kernel_destroy_metadata(ksess
->metadata
);
1537 /* Restore metadata state.*/
1538 ksess
->metadata
= saved_metadata
;
1539 ksess
->metadata_stream_fd
= saved_metadata_fd
;
1546 * Get the syscall mask array from the kernel tracer.
1548 * Return 0 on success else a negative value. In both case, syscall_mask should
1551 int kernel_syscall_mask(int chan_fd
, char **syscall_mask
, uint32_t *nr_bits
)
1553 assert(syscall_mask
);
1556 return kernctl_syscall_mask(chan_fd
, syscall_mask
, nr_bits
);
1560 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1563 * Return 1 on success, 0 when feature is not supported, negative value in case
1566 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1568 int ret
= 0; // Not supported by default
1569 struct lttng_kernel_tracer_abi_version abi
;
1571 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1573 ERR("Failed to retrieve lttng-modules ABI version");
1578 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1580 if (abi
.major
>= 2 && abi
.minor
>= 3) {
1592 * Check for the support of the packet sequence number via abi version number.
1594 * Return 1 on success, 0 when feature is not supported, negative value in case
1597 int kernel_supports_ring_buffer_packet_sequence_number(void)
1599 int ret
= 0; // Not supported by default
1600 struct lttng_kernel_tracer_abi_version abi
;
1602 ret
= kernctl_tracer_abi_version(kernel_tracer_fd
, &abi
);
1604 ERR("Failed to retrieve lttng-modules ABI version");
1609 * Packet sequence number was introduced in LTTng 2.8,
1610 * lttng-modules ABI 2.1.
1612 if (abi
.major
>= 2 && abi
.minor
>= 1) {
1624 * Rotate a kernel session.
1626 * Return LTTNG_OK on success or else an LTTng error code.
1628 enum lttng_error_code
kernel_rotate_session(struct ltt_session
*session
)
1631 enum lttng_error_code status
= LTTNG_OK
;
1632 struct consumer_socket
*socket
;
1633 struct lttng_ht_iter iter
;
1634 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1637 assert(ksess
->consumer
);
1639 DBG("Rotate kernel session %s started (session %" PRIu64
")",
1640 session
->name
, session
->id
);
1645 * Note that this loop will end after one iteration given that there is
1646 * only one kernel consumer.
1648 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1649 socket
, node
.node
) {
1650 struct ltt_kernel_channel
*chan
;
1652 /* For each channel, ask the consumer to rotate it. */
1653 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1654 DBG("Rotate kernel channel %" PRIu64
", session %s",
1655 chan
->key
, session
->name
);
1656 ret
= consumer_rotate_channel(socket
, chan
->key
,
1657 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1658 /* is_metadata_channel */ false);
1660 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1666 * Rotate the metadata channel.
1668 ret
= consumer_rotate_channel(socket
, ksess
->metadata
->key
,
1669 ksess
->uid
, ksess
->gid
, ksess
->consumer
,
1670 /* is_metadata_channel */ true);
1672 status
= LTTNG_ERR_KERN_CONSUMER_FAIL
;
1682 enum lttng_error_code
kernel_create_channel_subdirectories(
1683 const struct ltt_kernel_session
*ksess
)
1685 enum lttng_error_code ret
= LTTNG_OK
;
1686 enum lttng_trace_chunk_status chunk_status
;
1689 assert(ksess
->current_trace_chunk
);
1692 * Create the index subdirectory which will take care
1693 * of implicitly creating the channel's path.
1695 chunk_status
= lttng_trace_chunk_create_subdirectory(
1696 ksess
->current_trace_chunk
,
1697 DEFAULT_KERNEL_TRACE_DIR
"/" DEFAULT_INDEX_DIR
);
1698 if (chunk_status
!= LTTNG_TRACE_CHUNK_STATUS_OK
) {
1699 ret
= LTTNG_ERR_CREATE_DIR_FAIL
;
1708 * Setup necessary data for kernel tracer action.
1711 int init_kernel_tracer(void)
1714 bool is_root
= !getuid();
1716 /* Modprobe lttng kernel modules */
1717 ret
= modprobe_lttng_control();
1722 /* Open debugfs lttng */
1723 kernel_tracer_fd
= open(module_proc_lttng
, O_RDWR
);
1724 if (kernel_tracer_fd
< 0) {
1725 DBG("Failed to open %s", module_proc_lttng
);
1729 /* Validate kernel version */
1730 ret
= kernel_validate_version(&kernel_tracer_version
,
1731 &kernel_tracer_abi_version
);
1736 ret
= modprobe_lttng_data();
1741 ret
= kernel_supports_ring_buffer_snapshot_sample_positions();
1747 WARN("Kernel tracer does not support buffer monitoring. "
1748 "The monitoring timer of channels in the kernel domain "
1749 "will be set to 0 (disabled).");
1752 DBG("Kernel tracer fd %d", kernel_tracer_fd
);
1754 ret
= syscall_init_table(kernel_tracer_fd
);
1756 ERR("Unable to populate syscall table. Syscall tracing won't "
1757 "work for this session daemon.");
1762 modprobe_remove_lttng_control();
1763 ret
= close(kernel_tracer_fd
);
1767 kernel_tracer_fd
= -1;
1768 return LTTNG_ERR_KERN_VERSION
;
1771 ret
= close(kernel_tracer_fd
);
1777 modprobe_remove_lttng_control();
1780 WARN("No kernel tracer available");
1781 kernel_tracer_fd
= -1;
1783 return LTTNG_ERR_NEED_ROOT_SESSIOND
;
1785 return LTTNG_ERR_KERN_NA
;
1790 void cleanup_kernel_tracer(void)
1794 DBG2("Closing kernel fd");
1795 if (kernel_tracer_fd
>= 0) {
1796 ret
= close(kernel_tracer_fd
);
1800 kernel_tracer_fd
= -1;
1802 DBG("Unloading kernel modules");
1803 modprobe_remove_lttng_all();
1804 free(syscall_table
);
1808 bool kernel_tracer_is_initialized(void)
1810 return kernel_tracer_fd
>= 0;
1814 * Clear a kernel session.
1816 * Return LTTNG_OK on success or else an LTTng error code.
1818 enum lttng_error_code
kernel_clear_session(struct ltt_session
*session
)
1821 enum lttng_error_code status
= LTTNG_OK
;
1822 struct consumer_socket
*socket
;
1823 struct lttng_ht_iter iter
;
1824 struct ltt_kernel_session
*ksess
= session
->kernel_session
;
1827 assert(ksess
->consumer
);
1829 DBG("Clear kernel session %s (session %" PRIu64
")",
1830 session
->name
, session
->id
);
1834 if (ksess
->active
) {
1835 ERR("Expecting inactive session %s (%" PRIu64
")", session
->name
, session
->id
);
1836 status
= LTTNG_ERR_FATAL
;
1841 * Note that this loop will end after one iteration given that there is
1842 * only one kernel consumer.
1844 cds_lfht_for_each_entry(ksess
->consumer
->socks
->ht
, &iter
.iter
,
1845 socket
, node
.node
) {
1846 struct ltt_kernel_channel
*chan
;
1848 /* For each channel, ask the consumer to clear it. */
1849 cds_list_for_each_entry(chan
, &ksess
->channel_list
.head
, list
) {
1850 DBG("Clear kernel channel %" PRIu64
", session %s",
1851 chan
->key
, session
->name
);
1852 ret
= consumer_clear_channel(socket
, chan
->key
);
1858 if (!ksess
->metadata
) {
1860 * Nothing to do for the metadata.
1861 * This is a snapshot session.
1862 * The metadata is genererated on the fly.
1868 * Clear the metadata channel.
1869 * Metadata channel is not cleared per se but we still need to
1870 * perform a rotation operation on it behind the scene.
1872 ret
= consumer_clear_channel(socket
, ksess
->metadata
->key
);
1881 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED
:
1882 status
= LTTNG_ERR_CLEAR_RELAY_DISALLOWED
;
1885 status
= LTTNG_ERR_CLEAR_FAIL_CONSUMER
;