2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include <common/common.h>
28 #include <common/kernel-ctl/kernel-ctl.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
);
294 if (ret
< 0 && errno
!= EEXIST
) {
295 PERROR("enable kernel event");
300 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
309 * Disable a kernel event.
311 int kernel_disable_event(struct ltt_kernel_event
*event
)
315 ret
= kernctl_disable(event
->fd
);
316 if (ret
< 0 && errno
!= EEXIST
) {
317 PERROR("disable kernel event");
322 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
331 * Create kernel metadata, open from the kernel tracer and add it to the
334 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
337 struct ltt_kernel_metadata
*lkm
;
339 /* Allocate kernel metadata */
340 lkm
= trace_kernel_create_metadata(path
);
345 /* Kernel tracer metadata creation */
346 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
352 /* Prevent fd duplication after execlp() */
353 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
355 PERROR("fcntl session fd");
358 session
->metadata
= lkm
;
360 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
369 * Start tracing session.
371 int kernel_start_session(struct ltt_kernel_session
*session
)
375 ret
= kernctl_start_session(session
->fd
);
377 PERROR("ioctl start session");
381 DBG("Kernel session started");
390 * Make a kernel wait to make sure in-flight probe have completed.
392 void kernel_wait_quiescent(int fd
)
396 DBG("Kernel quiescent wait on %d", fd
);
398 ret
= kernctl_wait_quiescent(fd
);
400 PERROR("wait quiescent ioctl");
401 ERR("Kernel quiescent wait failed");
408 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
412 ret
= kernctl_calibrate(fd
, calibrate
);
414 PERROR("calibrate ioctl");
423 * Force flush buffer of metadata.
425 int kernel_metadata_flush_buffer(int fd
)
429 ret
= kernctl_buffer_flush(fd
);
431 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
438 * Force flush buffer for channel.
440 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
443 struct ltt_kernel_stream
*stream
;
445 DBG("Flush buffer for channel %s", channel
->channel
->name
);
447 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
448 DBG("Flushing channel stream %d", stream
->fd
);
449 ret
= kernctl_buffer_flush(stream
->fd
);
452 ERR("Fail to flush buffer for stream %d (ret: %d)",
461 * Stop tracing session.
463 int kernel_stop_session(struct ltt_kernel_session
*session
)
467 ret
= kernctl_stop_session(session
->fd
);
472 DBG("Kernel session stopped");
481 * Open stream of channel, register it to the kernel tracer and add it
482 * to the stream list of the channel.
484 * Return the number of created stream. Else, a negative value.
486 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
489 struct ltt_kernel_stream
*lks
;
491 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
492 lks
= trace_kernel_create_stream();
502 /* Prevent fd duplication after execlp() */
503 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
505 PERROR("fcntl session fd");
508 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
509 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
511 PERROR("asprintf kernel create stream");
515 /* Add stream to channe stream list */
516 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
517 channel
->stream_count
++;
519 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
520 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
523 return channel
->stream_count
;
530 * Open the metadata stream and set it to the kernel session.
532 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
536 ret
= kernctl_create_stream(session
->metadata
->fd
);
538 PERROR("kernel create metadata stream");
542 DBG("Kernel metadata stream created (fd: %d)", ret
);
543 session
->metadata_stream_fd
= ret
;
544 /* Prevent fd duplication after execlp() */
545 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
547 PERROR("fcntl session fd");
557 * Get the event list from the kernel tracer and return the number of elements.
559 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
563 size_t nbmem
, count
= 0;
566 struct lttng_event
*elist
;
568 fd
= kernctl_tracepoint_list(tracer_fd
);
570 PERROR("kernel tracepoint list");
574 fp
= fdopen(fd
, "r");
576 PERROR("kernel tracepoint list fdopen");
581 * Init memory size counter
582 * See kernel-ctl.h for explanation of this value
584 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
585 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
587 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
588 if (count
>= nbmem
) {
589 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
591 /* Double the size */
593 elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
595 PERROR("realloc list events");
600 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
601 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
602 elist
[count
].enabled
= -1;
607 DBG("Kernel list events done (%zu events)", count
);
609 ret
= fclose(fp
); /* closes both fp and fd */
625 * Get kernel version and validate it.
627 int kernel_validate_version(int tracer_fd
)
630 struct lttng_kernel_tracer_version version
;
632 ret
= kernctl_tracer_version(tracer_fd
, &version
);
634 ERR("Failed at getting the lttng-modules version");
638 /* Validate version */
639 if (version
.major
!= KERN_MODULES_PRE_MAJOR
640 && version
.major
!= KERN_MODULES_MAJOR
) {
644 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
648 ERR("Kernel major version %d is not compatible (supporting <= %d)",
649 version
.major
, KERN_MODULES_MAJOR
)
657 * Kernel work-arounds called at the start of sessiond main().
659 int init_kernel_workarounds(void)
665 * boot_id needs to be read once before being used concurrently
666 * to deal with a Linux kernel race. A fix is proposed for
667 * upstream, but the work-around is needed for older kernels.
669 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
676 ret
= fread(buf
, 1, sizeof(buf
), fp
);
678 /* Ignore error, we don't really care */