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 DBG3("Kernel create channel %s in %s with attr: %d, %zu, %zu, %u, %u, %d",
154 chan
->name
, path
, lkc
->channel
->attr
.overwrite
,
155 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
156 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
157 lkc
->channel
->attr
.output
);
159 /* Kernel tracer channel creation */
160 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
162 PERROR("ioctl kernel create channel");
166 /* Setup the channel fd */
168 /* Prevent fd duplication after execlp() */
169 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
171 PERROR("fcntl session fd");
174 /* Add channel to session */
175 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
176 session
->channel_count
++;
178 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
187 * Create a kernel event, enable it to the kernel tracer and add it to the
188 * channel event list of the kernel session.
190 int kernel_create_event(struct lttng_event
*ev
,
191 struct ltt_kernel_channel
*channel
)
194 struct ltt_kernel_event
*event
;
196 event
= trace_kernel_create_event(ev
);
202 ret
= kernctl_create_event(channel
->fd
, event
->event
);
208 WARN("Event type not implemented");
211 PERROR("create event ioctl");
218 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
220 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
221 DBG2("Kernel event syscall creation success");
223 * We use fd == -1 to ensure that we never trigger a close of fd
231 /* Prevent fd duplication after execlp() */
232 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
234 PERROR("fcntl session fd");
238 /* Add event to event list */
239 cds_list_add(&event
->list
, &channel
->events_list
.head
);
240 channel
->event_count
++;
242 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
253 * Disable a kernel channel.
255 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
259 ret
= kernctl_disable(chan
->fd
);
261 PERROR("disable chan ioctl");
267 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
276 * Enable a kernel channel.
278 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
282 ret
= kernctl_enable(chan
->fd
);
283 if (ret
< 0 && errno
!= EEXIST
) {
284 PERROR("Enable kernel chan");
289 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
298 * Enable a kernel event.
300 int kernel_enable_event(struct ltt_kernel_event
*event
)
304 ret
= kernctl_enable(event
->fd
);
308 ret
= LTTCOMM_KERN_EVENT_EXIST
;
311 PERROR("enable kernel event");
318 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
327 * Disable a kernel event.
329 int kernel_disable_event(struct ltt_kernel_event
*event
)
333 ret
= kernctl_disable(event
->fd
);
337 ret
= LTTCOMM_KERN_EVENT_EXIST
;
340 PERROR("disable kernel event");
347 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
356 * Create kernel metadata, open from the kernel tracer and add it to the
359 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
362 struct ltt_kernel_metadata
*lkm
;
364 /* Allocate kernel metadata */
365 lkm
= trace_kernel_create_metadata(path
);
370 /* Kernel tracer metadata creation */
371 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
377 /* Prevent fd duplication after execlp() */
378 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
380 PERROR("fcntl session fd");
383 session
->metadata
= lkm
;
385 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
394 * Start tracing session.
396 int kernel_start_session(struct ltt_kernel_session
*session
)
400 ret
= kernctl_start_session(session
->fd
);
402 PERROR("ioctl start session");
406 DBG("Kernel session started");
415 * Make a kernel wait to make sure in-flight probe have completed.
417 void kernel_wait_quiescent(int fd
)
421 DBG("Kernel quiescent wait on %d", fd
);
423 ret
= kernctl_wait_quiescent(fd
);
425 PERROR("wait quiescent ioctl");
426 ERR("Kernel quiescent wait failed");
433 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
437 ret
= kernctl_calibrate(fd
, calibrate
);
439 PERROR("calibrate ioctl");
448 * Force flush buffer of metadata.
450 int kernel_metadata_flush_buffer(int fd
)
454 ret
= kernctl_buffer_flush(fd
);
456 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
463 * Force flush buffer for channel.
465 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
468 struct ltt_kernel_stream
*stream
;
470 DBG("Flush buffer for channel %s", channel
->channel
->name
);
472 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
473 DBG("Flushing channel stream %d", stream
->fd
);
474 ret
= kernctl_buffer_flush(stream
->fd
);
477 ERR("Fail to flush buffer for stream %d (ret: %d)",
486 * Stop tracing session.
488 int kernel_stop_session(struct ltt_kernel_session
*session
)
492 ret
= kernctl_stop_session(session
->fd
);
497 DBG("Kernel session stopped");
506 * Open stream of channel, register it to the kernel tracer and add it
507 * to the stream list of the channel.
509 * Return the number of created stream. Else, a negative value.
511 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
514 struct ltt_kernel_stream
*lks
;
516 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
517 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
527 /* Prevent fd duplication after execlp() */
528 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
530 PERROR("fcntl session fd");
533 /* Add stream to channe stream list */
534 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
535 channel
->stream_count
++;
537 /* Increment counter which represent CPU number. */
540 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
544 return channel
->stream_count
;
551 * Open the metadata stream and set it to the kernel session.
553 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
557 ret
= kernctl_create_stream(session
->metadata
->fd
);
559 PERROR("kernel create metadata stream");
563 DBG("Kernel metadata stream created (fd: %d)", ret
);
564 session
->metadata_stream_fd
= ret
;
565 /* Prevent fd duplication after execlp() */
566 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
568 PERROR("fcntl session fd");
578 * Get the event list from the kernel tracer and return the number of elements.
580 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
584 size_t nbmem
, count
= 0;
587 struct lttng_event
*elist
;
589 fd
= kernctl_tracepoint_list(tracer_fd
);
591 PERROR("kernel tracepoint list");
595 fp
= fdopen(fd
, "r");
597 PERROR("kernel tracepoint list fdopen");
602 * Init memory size counter
603 * See kernel-ctl.h for explanation of this value
605 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
606 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
608 PERROR("alloc list events");
613 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
614 if (count
>= nbmem
) {
615 struct lttng_event
*new_elist
;
617 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
619 /* Double the size */
621 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
622 if (new_elist
== NULL
) {
623 PERROR("realloc list events");
631 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
632 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
633 elist
[count
].enabled
= -1;
639 DBG("Kernel list events done (%zu events)", count
);
641 ret
= fclose(fp
); /* closes both fp and fd */
657 * Get kernel version and validate it.
659 int kernel_validate_version(int tracer_fd
)
662 struct lttng_kernel_tracer_version version
;
664 ret
= kernctl_tracer_version(tracer_fd
, &version
);
666 ERR("Failed at getting the lttng-modules version");
670 /* Validate version */
671 if (version
.major
!= KERN_MODULES_PRE_MAJOR
672 && version
.major
!= KERN_MODULES_MAJOR
) {
676 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
680 ERR("Kernel major version %d is not compatible (supporting <= %d)",
681 version
.major
, KERN_MODULES_MAJOR
)
689 * Kernel work-arounds called at the start of sessiond main().
691 int init_kernel_workarounds(void)
697 * boot_id needs to be read once before being used concurrently
698 * to deal with a Linux kernel race. A fix is proposed for
699 * upstream, but the work-around is needed for older kernels.
701 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
708 ret
= fread(buf
, 1, sizeof(buf
), fp
);
710 /* Ignore error, we don't really care */