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>
30 #include "kern-modules.h"
33 * Add context on a kernel channel.
35 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
36 struct lttng_kernel_context
*ctx
)
40 DBG("Adding context to channel %s", chan
->channel
->name
);
41 ret
= kernctl_add_context(chan
->fd
, ctx
);
43 if (errno
!= EEXIST
) {
44 PERROR("add context ioctl");
46 /* If EEXIST, we just ignore the error */
52 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
53 if (chan
->ctx
== NULL
) {
54 PERROR("zmalloc event context");
58 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
67 * Add context on a kernel event.
69 int kernel_add_event_context(struct ltt_kernel_event
*event
,
70 struct lttng_kernel_context
*ctx
)
74 DBG("Adding context to event %s", event
->event
->name
);
75 ret
= kernctl_add_context(event
->fd
, ctx
);
77 PERROR("add context ioctl");
81 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
82 if (event
->ctx
== NULL
) {
83 PERROR("zmalloc event context");
87 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
96 * Create a new kernel session, register it to the kernel tracer and add it to
97 * the session daemon session.
99 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
102 struct ltt_kernel_session
*lks
;
104 /* Allocate data structure */
105 lks
= trace_kernel_create_session(session
->path
);
111 /* Kernel tracer session creation */
112 ret
= kernctl_create_session(tracer_fd
);
114 PERROR("ioctl kernel create session");
119 /* Prevent fd duplication after execlp() */
120 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
122 PERROR("fcntl session fd");
125 lks
->consumer_fds_sent
= 0;
126 session
->kernel_session
= lks
;
128 DBG("Kernel session created (fd: %d)", lks
->fd
);
137 * Create a kernel channel, register it to the kernel tracer and add it to the
140 int kernel_create_channel(struct ltt_kernel_session
*session
,
141 struct lttng_channel
*chan
, char *path
)
144 struct ltt_kernel_channel
*lkc
;
146 /* Allocate kernel channel */
147 lkc
= trace_kernel_create_channel(chan
, path
);
152 /* Kernel tracer channel creation */
153 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
155 PERROR("ioctl kernel create channel");
159 /* Setup the channel fd */
161 /* Prevent fd duplication after execlp() */
162 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
164 PERROR("fcntl session fd");
167 /* Add channel to session */
168 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
169 session
->channel_count
++;
171 DBG("Kernel channel %s created (fd: %d and path: %s)",
172 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
181 * Create a kernel event, enable it to the kernel tracer and add it to the
182 * channel event list of the kernel session.
184 int kernel_create_event(struct lttng_event
*ev
,
185 struct ltt_kernel_channel
*channel
)
188 struct ltt_kernel_event
*event
;
190 event
= trace_kernel_create_event(ev
);
196 ret
= kernctl_create_event(channel
->fd
, event
->event
);
198 if (errno
!= EEXIST
) {
199 PERROR("create event ioctl");
206 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
208 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
209 DBG2("Kernel event syscall creation success");
211 * We use fd == -1 to ensure that we never trigger a close of fd
219 /* Prevent fd duplication after execlp() */
220 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
222 PERROR("fcntl session fd");
226 /* Add event to event list */
227 cds_list_add(&event
->list
, &channel
->events_list
.head
);
228 channel
->event_count
++;
230 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
241 * Disable a kernel channel.
243 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
247 ret
= kernctl_disable(chan
->fd
);
249 PERROR("disable chan ioctl");
255 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
264 * Enable a kernel channel.
266 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
270 ret
= kernctl_enable(chan
->fd
);
271 if (ret
< 0 && errno
!= EEXIST
) {
272 PERROR("Enable kernel chan");
277 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
286 * Enable a kernel event.
288 int kernel_enable_event(struct ltt_kernel_event
*event
)
292 ret
= kernctl_enable(event
->fd
);
293 if (ret
< 0 && errno
!= EEXIST
) {
294 PERROR("enable kernel event");
299 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
308 * Disable a kernel event.
310 int kernel_disable_event(struct ltt_kernel_event
*event
)
314 ret
= kernctl_disable(event
->fd
);
315 if (ret
< 0 && errno
!= EEXIST
) {
316 PERROR("disable kernel event");
321 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
330 * Create kernel metadata, open from the kernel tracer and add it to the
333 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
336 struct ltt_kernel_metadata
*lkm
;
338 /* Allocate kernel metadata */
339 lkm
= trace_kernel_create_metadata(path
);
344 /* Kernel tracer metadata creation */
345 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
351 /* Prevent fd duplication after execlp() */
352 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
354 PERROR("fcntl session fd");
357 session
->metadata
= lkm
;
359 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
368 * Start tracing session.
370 int kernel_start_session(struct ltt_kernel_session
*session
)
374 ret
= kernctl_start_session(session
->fd
);
376 PERROR("ioctl start session");
380 DBG("Kernel session started");
389 * Make a kernel wait to make sure in-flight probe have completed.
391 void kernel_wait_quiescent(int fd
)
395 DBG("Kernel quiescent wait on %d", fd
);
397 ret
= kernctl_wait_quiescent(fd
);
399 PERROR("wait quiescent ioctl");
400 ERR("Kernel quiescent wait failed");
407 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
411 ret
= kernctl_calibrate(fd
, calibrate
);
413 PERROR("calibrate ioctl");
422 * Force flush buffer of metadata.
424 int kernel_metadata_flush_buffer(int fd
)
428 ret
= kernctl_buffer_flush(fd
);
430 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
437 * Force flush buffer for channel.
439 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
442 struct ltt_kernel_stream
*stream
;
444 DBG("Flush buffer for channel %s", channel
->channel
->name
);
446 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
447 DBG("Flushing channel stream %d", stream
->fd
);
448 ret
= kernctl_buffer_flush(stream
->fd
);
451 ERR("Fail to flush buffer for stream %d (ret: %d)",
460 * Stop tracing session.
462 int kernel_stop_session(struct ltt_kernel_session
*session
)
466 ret
= kernctl_stop_session(session
->fd
);
471 DBG("Kernel session stopped");
480 * Open stream of channel, register it to the kernel tracer and add it
481 * to the stream list of the channel.
483 * Return the number of created stream. Else, a negative value.
485 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
488 struct ltt_kernel_stream
*lks
;
490 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
491 lks
= trace_kernel_create_stream();
501 /* Prevent fd duplication after execlp() */
502 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
504 PERROR("fcntl session fd");
507 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
508 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
510 PERROR("asprintf kernel create stream");
514 /* Add stream to channe stream list */
515 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
516 channel
->stream_count
++;
518 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
519 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
522 return channel
->stream_count
;
529 * Open the metadata stream and set it to the kernel session.
531 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
535 ret
= kernctl_create_stream(session
->metadata
->fd
);
537 PERROR("kernel create metadata stream");
541 DBG("Kernel metadata stream created (fd: %d)", ret
);
542 session
->metadata_stream_fd
= ret
;
543 /* Prevent fd duplication after execlp() */
544 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
546 PERROR("fcntl session fd");
556 * Get the event list from the kernel tracer and return the number of elements.
558 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
562 size_t nbmem
, count
= 0;
565 struct lttng_event
*elist
;
567 fd
= kernctl_tracepoint_list(tracer_fd
);
569 PERROR("kernel tracepoint list");
573 fp
= fdopen(fd
, "r");
575 PERROR("kernel tracepoint list fdopen");
580 * Init memory size counter
581 * See kernel-ctl.h for explanation of this value
583 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
584 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
586 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
587 if (count
>= nbmem
) {
588 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
590 /* Double the size */
592 elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
594 PERROR("realloc list events");
599 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
600 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
601 elist
[count
].enabled
= -1;
606 DBG("Kernel list events done (%zu events)", count
);
608 ret
= fclose(fp
); /* closes both fp and fd */
624 * Get kernel version and validate it.
626 int kernel_validate_version(int tracer_fd
)
629 struct lttng_kernel_tracer_version version
;
631 ret
= kernctl_tracer_version(tracer_fd
, &version
);
633 ERR("Failed at getting the lttng-modules version");
637 /* Validate version */
638 if (version
.major
!= KERN_MODULES_PRE_MAJOR
639 && version
.major
!= KERN_MODULES_MAJOR
) {
643 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
647 ERR("Kernel major version %d is not compatible (supporting <= %d)",
648 version
.major
, KERN_MODULES_MAJOR
)
656 * Kernel work-arounds called at the start of sessiond main().
658 int init_kernel_workarounds(void)
664 * boot_id needs to be read once before being used concurrently
665 * to deal with a Linux kernel race. A fix is proposed for
666 * upstream, but the work-around is needed for older kernels.
668 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
675 ret
= fread(buf
, 1, sizeof(buf
), fp
);
677 /* Ignore error, we don't really care */