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/sessiond-comm/sessiond-comm.h>
33 #include "kern-modules.h"
36 * Add context on a kernel channel.
38 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
39 struct lttng_kernel_context
*ctx
)
43 DBG("Adding context to channel %s", chan
->channel
->name
);
44 ret
= kernctl_add_context(chan
->fd
, ctx
);
46 if (errno
!= EEXIST
) {
47 PERROR("add context ioctl");
49 /* If EEXIST, we just ignore the error */
55 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
56 if (chan
->ctx
== NULL
) {
57 PERROR("zmalloc event context");
61 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
70 * Add context on a kernel event.
72 int kernel_add_event_context(struct ltt_kernel_event
*event
,
73 struct lttng_kernel_context
*ctx
)
77 DBG("Adding context to event %s", event
->event
->name
);
78 ret
= kernctl_add_context(event
->fd
, ctx
);
80 PERROR("add context ioctl");
84 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
85 if (event
->ctx
== NULL
) {
86 PERROR("zmalloc event context");
90 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
99 * Create a new kernel session, register it to the kernel tracer and add it to
100 * the session daemon session.
102 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
105 struct ltt_kernel_session
*lks
;
107 /* Allocate data structure */
108 lks
= trace_kernel_create_session(session
->path
);
114 /* Kernel tracer session creation */
115 ret
= kernctl_create_session(tracer_fd
);
117 PERROR("ioctl kernel create session");
122 /* Prevent fd duplication after execlp() */
123 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
125 PERROR("fcntl session fd");
128 lks
->consumer_fds_sent
= 0;
129 session
->kernel_session
= lks
;
131 DBG("Kernel session created (fd: %d)", lks
->fd
);
140 * Create a kernel channel, register it to the kernel tracer and add it to the
143 int kernel_create_channel(struct ltt_kernel_session
*session
,
144 struct lttng_channel
*chan
, char *path
)
147 struct ltt_kernel_channel
*lkc
;
149 /* Allocate kernel channel */
150 lkc
= trace_kernel_create_channel(chan
, path
);
155 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
156 chan
->name
, path
, lkc
->channel
->attr
.overwrite
,
157 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
158 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
159 lkc
->channel
->attr
.output
);
161 /* Kernel tracer channel creation */
162 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
164 PERROR("ioctl kernel create channel");
168 /* Setup the channel fd */
170 /* Prevent fd duplication after execlp() */
171 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
173 PERROR("fcntl session fd");
176 /* Add channel to session */
177 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
178 session
->channel_count
++;
180 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
189 * Create a kernel event, enable it to the kernel tracer and add it to the
190 * channel event list of the kernel session.
192 int kernel_create_event(struct lttng_event
*ev
,
193 struct ltt_kernel_channel
*channel
)
196 struct ltt_kernel_event
*event
;
198 event
= trace_kernel_create_event(ev
);
204 ret
= kernctl_create_event(channel
->fd
, event
->event
);
210 WARN("Event type not implemented");
213 PERROR("create event ioctl");
220 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
222 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
223 DBG2("Kernel event syscall creation success");
225 * We use fd == -1 to ensure that we never trigger a close of fd
233 /* Prevent fd duplication after execlp() */
234 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
236 PERROR("fcntl session fd");
240 /* Add event to event list */
241 cds_list_add(&event
->list
, &channel
->events_list
.head
);
242 channel
->event_count
++;
244 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
255 * Disable a kernel channel.
257 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
261 ret
= kernctl_disable(chan
->fd
);
263 PERROR("disable chan ioctl");
269 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
278 * Enable a kernel channel.
280 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
284 ret
= kernctl_enable(chan
->fd
);
285 if (ret
< 0 && errno
!= EEXIST
) {
286 PERROR("Enable kernel chan");
291 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
300 * Enable a kernel event.
302 int kernel_enable_event(struct ltt_kernel_event
*event
)
306 ret
= kernctl_enable(event
->fd
);
310 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
313 PERROR("enable kernel event");
320 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
329 * Disable a kernel event.
331 int kernel_disable_event(struct ltt_kernel_event
*event
)
335 ret
= kernctl_disable(event
->fd
);
339 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
342 PERROR("disable kernel event");
349 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
358 * Create kernel metadata, open from the kernel tracer and add it to the
361 int kernel_open_metadata(struct ltt_kernel_session
*session
)
364 struct ltt_kernel_metadata
*lkm
;
366 /* Allocate kernel metadata */
367 lkm
= trace_kernel_create_metadata();
372 /* Kernel tracer metadata creation */
373 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
379 /* Prevent fd duplication after execlp() */
380 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
382 PERROR("fcntl session fd");
385 session
->metadata
= lkm
;
387 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
396 * Start tracing session.
398 int kernel_start_session(struct ltt_kernel_session
*session
)
402 ret
= kernctl_start_session(session
->fd
);
404 PERROR("ioctl start session");
408 DBG("Kernel session started");
417 * Make a kernel wait to make sure in-flight probe have completed.
419 void kernel_wait_quiescent(int fd
)
423 DBG("Kernel quiescent wait on %d", fd
);
425 ret
= kernctl_wait_quiescent(fd
);
427 PERROR("wait quiescent ioctl");
428 ERR("Kernel quiescent wait failed");
435 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
439 ret
= kernctl_calibrate(fd
, calibrate
);
441 PERROR("calibrate ioctl");
450 * Force flush buffer of metadata.
452 int kernel_metadata_flush_buffer(int fd
)
456 ret
= kernctl_buffer_flush(fd
);
458 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
465 * Force flush buffer for channel.
467 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
470 struct ltt_kernel_stream
*stream
;
472 DBG("Flush buffer for channel %s", channel
->channel
->name
);
474 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
475 DBG("Flushing channel stream %d", stream
->fd
);
476 ret
= kernctl_buffer_flush(stream
->fd
);
479 ERR("Fail to flush buffer for stream %d (ret: %d)",
488 * Stop tracing session.
490 int kernel_stop_session(struct ltt_kernel_session
*session
)
494 ret
= kernctl_stop_session(session
->fd
);
499 DBG("Kernel session stopped");
508 * Open stream of channel, register it to the kernel tracer and add it
509 * to the stream list of the channel.
511 * Return the number of created stream. Else, a negative value.
513 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
516 struct ltt_kernel_stream
*lks
;
518 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
519 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
529 /* Prevent fd duplication after execlp() */
530 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
532 PERROR("fcntl session fd");
535 /* Add stream to channe stream list */
536 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
537 channel
->stream_count
++;
539 /* Increment counter which represent CPU number. */
542 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
546 return channel
->stream_count
;
553 * Open the metadata stream and set it to the kernel session.
555 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
559 ret
= kernctl_create_stream(session
->metadata
->fd
);
561 PERROR("kernel create metadata stream");
565 DBG("Kernel metadata stream created (fd: %d)", ret
);
566 session
->metadata_stream_fd
= ret
;
567 /* Prevent fd duplication after execlp() */
568 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
570 PERROR("fcntl session fd");
580 * Get the event list from the kernel tracer and return the number of elements.
582 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
586 size_t nbmem
, count
= 0;
589 struct lttng_event
*elist
;
591 fd
= kernctl_tracepoint_list(tracer_fd
);
593 PERROR("kernel tracepoint list");
597 fp
= fdopen(fd
, "r");
599 PERROR("kernel tracepoint list fdopen");
604 * Init memory size counter
605 * See kernel-ctl.h for explanation of this value
607 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
608 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
610 PERROR("alloc list events");
615 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
616 if (count
>= nbmem
) {
617 struct lttng_event
*new_elist
;
619 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
621 /* Double the size */
623 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
624 if (new_elist
== NULL
) {
625 PERROR("realloc list events");
633 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
634 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
635 elist
[count
].enabled
= -1;
641 DBG("Kernel list events done (%zu events)", count
);
643 ret
= fclose(fp
); /* closes both fp and fd */
659 * Get kernel version and validate it.
661 int kernel_validate_version(int tracer_fd
)
664 struct lttng_kernel_tracer_version version
;
666 ret
= kernctl_tracer_version(tracer_fd
, &version
);
668 ERR("Failed at getting the lttng-modules version");
672 /* Validate version */
673 if (version
.major
!= KERN_MODULES_PRE_MAJOR
674 && version
.major
!= KERN_MODULES_MAJOR
) {
678 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
682 ERR("Kernel major version %d is not compatible (supporting <= %d)",
683 version
.major
, KERN_MODULES_MAJOR
)
691 * Kernel work-arounds called at the start of sessiond main().
693 int init_kernel_workarounds(void)
699 * boot_id needs to be read once before being used concurrently
700 * to deal with a Linux kernel race. A fix is proposed for
701 * upstream, but the work-around is needed for older kernels.
703 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
710 ret
= fread(buf
, 1, sizeof(buf
), fp
);
712 /* Ignore error, we don't really care */
724 * Complete teardown of a kernel session.
726 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
729 DBG3("No kernel session when tearing down session");
733 DBG("Tearing down kernel session");
735 /* Close any relayd session */
736 consumer_output_send_destroy_relayd(ksess
->consumer
);
738 trace_kernel_destroy_session(ksess
);