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/sessiond-comm/sessiond-comm.h>
31 #include "kern-modules.h"
34 * Add context on a kernel channel.
36 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
37 struct lttng_kernel_context
*ctx
)
41 DBG("Adding context to channel %s", chan
->channel
->name
);
42 ret
= kernctl_add_context(chan
->fd
, ctx
);
44 if (errno
!= EEXIST
) {
45 PERROR("add context ioctl");
47 /* If EEXIST, we just ignore the error */
53 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
54 if (chan
->ctx
== NULL
) {
55 PERROR("zmalloc event context");
59 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
68 * Add context on a kernel event.
70 int kernel_add_event_context(struct ltt_kernel_event
*event
,
71 struct lttng_kernel_context
*ctx
)
75 DBG("Adding context to event %s", event
->event
->name
);
76 ret
= kernctl_add_context(event
->fd
, ctx
);
78 PERROR("add context ioctl");
82 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
83 if (event
->ctx
== NULL
) {
84 PERROR("zmalloc event context");
88 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
97 * Create a new kernel session, register it to the kernel tracer and add it to
98 * the session daemon session.
100 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
103 struct ltt_kernel_session
*lks
;
105 /* Allocate data structure */
106 lks
= trace_kernel_create_session(session
->path
);
112 /* Kernel tracer session creation */
113 ret
= kernctl_create_session(tracer_fd
);
115 PERROR("ioctl kernel create session");
120 /* Prevent fd duplication after execlp() */
121 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
123 PERROR("fcntl session fd");
126 lks
->consumer_fds_sent
= 0;
127 session
->kernel_session
= lks
;
129 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * Create a kernel channel, register it to the kernel tracer and add it to the
141 int kernel_create_channel(struct ltt_kernel_session
*session
,
142 struct lttng_channel
*chan
, char *path
)
145 struct ltt_kernel_channel
*lkc
;
147 /* Allocate kernel channel */
148 lkc
= trace_kernel_create_channel(chan
, path
);
153 /* Kernel tracer channel creation */
154 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
156 PERROR("ioctl kernel create channel");
160 /* Setup the channel fd */
162 /* Prevent fd duplication after execlp() */
163 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
165 PERROR("fcntl session fd");
168 /* Add channel to session */
169 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
170 session
->channel_count
++;
172 DBG("Kernel channel %s created (fd: %d and path: %s)",
173 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
182 * Create a kernel event, enable it to the kernel tracer and add it to the
183 * channel event list of the kernel session.
185 int kernel_create_event(struct lttng_event
*ev
,
186 struct ltt_kernel_channel
*channel
)
189 struct ltt_kernel_event
*event
;
191 event
= trace_kernel_create_event(ev
);
197 ret
= kernctl_create_event(channel
->fd
, event
->event
);
199 if (errno
!= EEXIST
) {
200 PERROR("create event ioctl");
207 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
209 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
210 DBG2("Kernel event syscall creation success");
212 * We use fd == -1 to ensure that we never trigger a close of fd
220 /* Prevent fd duplication after execlp() */
221 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
223 PERROR("fcntl session fd");
227 /* Add event to event list */
228 cds_list_add(&event
->list
, &channel
->events_list
.head
);
229 channel
->event_count
++;
231 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
242 * Disable a kernel channel.
244 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
248 ret
= kernctl_disable(chan
->fd
);
250 PERROR("disable chan ioctl");
256 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
265 * Enable a kernel channel.
267 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
271 ret
= kernctl_enable(chan
->fd
);
272 if (ret
< 0 && errno
!= EEXIST
) {
273 PERROR("Enable kernel chan");
278 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
287 * Enable a kernel event.
289 int kernel_enable_event(struct ltt_kernel_event
*event
)
293 ret
= kernctl_enable(event
->fd
);
297 ret
= LTTCOMM_KERN_EVENT_EXIST
;
300 PERROR("enable kernel event");
307 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
316 * Disable a kernel event.
318 int kernel_disable_event(struct ltt_kernel_event
*event
)
322 ret
= kernctl_disable(event
->fd
);
326 ret
= LTTCOMM_KERN_EVENT_EXIST
;
329 PERROR("disable kernel event");
336 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
345 * Create kernel metadata, open from the kernel tracer and add it to the
348 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
351 struct ltt_kernel_metadata
*lkm
;
353 /* Allocate kernel metadata */
354 lkm
= trace_kernel_create_metadata(path
);
359 /* Kernel tracer metadata creation */
360 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
366 /* Prevent fd duplication after execlp() */
367 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
369 PERROR("fcntl session fd");
372 session
->metadata
= lkm
;
374 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
383 * Start tracing session.
385 int kernel_start_session(struct ltt_kernel_session
*session
)
389 ret
= kernctl_start_session(session
->fd
);
391 PERROR("ioctl start session");
395 DBG("Kernel session started");
404 * Make a kernel wait to make sure in-flight probe have completed.
406 void kernel_wait_quiescent(int fd
)
410 DBG("Kernel quiescent wait on %d", fd
);
412 ret
= kernctl_wait_quiescent(fd
);
414 PERROR("wait quiescent ioctl");
415 ERR("Kernel quiescent wait failed");
422 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
426 ret
= kernctl_calibrate(fd
, calibrate
);
428 PERROR("calibrate ioctl");
437 * Force flush buffer of metadata.
439 int kernel_metadata_flush_buffer(int fd
)
443 ret
= kernctl_buffer_flush(fd
);
445 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
452 * Force flush buffer for channel.
454 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
457 struct ltt_kernel_stream
*stream
;
459 DBG("Flush buffer for channel %s", channel
->channel
->name
);
461 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
462 DBG("Flushing channel stream %d", stream
->fd
);
463 ret
= kernctl_buffer_flush(stream
->fd
);
466 ERR("Fail to flush buffer for stream %d (ret: %d)",
475 * Stop tracing session.
477 int kernel_stop_session(struct ltt_kernel_session
*session
)
481 ret
= kernctl_stop_session(session
->fd
);
486 DBG("Kernel session stopped");
495 * Open stream of channel, register it to the kernel tracer and add it
496 * to the stream list of the channel.
498 * Return the number of created stream. Else, a negative value.
500 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
503 struct ltt_kernel_stream
*lks
;
505 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
506 lks
= trace_kernel_create_stream();
516 /* Prevent fd duplication after execlp() */
517 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
519 PERROR("fcntl session fd");
522 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
523 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
525 PERROR("asprintf kernel create stream");
529 /* Add stream to channe stream list */
530 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
531 channel
->stream_count
++;
533 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
534 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
537 return channel
->stream_count
;
544 * Open the metadata stream and set it to the kernel session.
546 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
550 ret
= kernctl_create_stream(session
->metadata
->fd
);
552 PERROR("kernel create metadata stream");
556 DBG("Kernel metadata stream created (fd: %d)", ret
);
557 session
->metadata_stream_fd
= ret
;
558 /* Prevent fd duplication after execlp() */
559 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
561 PERROR("fcntl session fd");
571 * Get the event list from the kernel tracer and return the number of elements.
573 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
577 size_t nbmem
, count
= 0;
580 struct lttng_event
*elist
;
582 fd
= kernctl_tracepoint_list(tracer_fd
);
584 PERROR("kernel tracepoint list");
588 fp
= fdopen(fd
, "r");
590 PERROR("kernel tracepoint list fdopen");
595 * Init memory size counter
596 * See kernel-ctl.h for explanation of this value
598 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
599 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
601 PERROR("alloc list events");
606 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
607 if (count
>= nbmem
) {
608 struct lttng_event
*new_elist
;
610 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
612 /* Double the size */
614 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
615 if (new_elist
== NULL
) {
616 PERROR("realloc list events");
624 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
625 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
626 elist
[count
].enabled
= -1;
632 DBG("Kernel list events done (%zu events)", count
);
634 ret
= fclose(fp
); /* closes both fp and fd */
650 * Get kernel version and validate it.
652 int kernel_validate_version(int tracer_fd
)
655 struct lttng_kernel_tracer_version version
;
657 ret
= kernctl_tracer_version(tracer_fd
, &version
);
659 ERR("Failed at getting the lttng-modules version");
663 /* Validate version */
664 if (version
.major
!= KERN_MODULES_PRE_MAJOR
665 && version
.major
!= KERN_MODULES_MAJOR
) {
669 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
673 ERR("Kernel major version %d is not compatible (supporting <= %d)",
674 version
.major
, KERN_MODULES_MAJOR
)
682 * Kernel work-arounds called at the start of sessiond main().
684 int init_kernel_workarounds(void)
690 * boot_id needs to be read once before being used concurrently
691 * to deal with a Linux kernel race. A fix is proposed for
692 * upstream, but the work-around is needed for older kernels.
694 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
701 ret
= fread(buf
, 1, sizeof(buf
), fp
);
703 /* Ignore error, we don't really care */