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>
32 #include "kern-modules.h"
35 * Add context on a kernel channel.
37 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
38 struct lttng_kernel_context
*ctx
)
42 DBG("Adding context to channel %s", chan
->channel
->name
);
43 ret
= kernctl_add_context(chan
->fd
, ctx
);
45 if (errno
!= EEXIST
) {
46 PERROR("add context ioctl");
48 /* If EEXIST, we just ignore the error */
54 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
55 if (chan
->ctx
== NULL
) {
56 PERROR("zmalloc event context");
60 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
69 * Add context on a kernel event.
71 int kernel_add_event_context(struct ltt_kernel_event
*event
,
72 struct lttng_kernel_context
*ctx
)
76 DBG("Adding context to event %s", event
->event
->name
);
77 ret
= kernctl_add_context(event
->fd
, ctx
);
79 PERROR("add context ioctl");
83 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
84 if (event
->ctx
== NULL
) {
85 PERROR("zmalloc event context");
89 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
98 * Create a new kernel session, register it to the kernel tracer and add it to
99 * the session daemon session.
101 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
104 struct ltt_kernel_session
*lks
;
106 /* Allocate data structure */
107 lks
= trace_kernel_create_session(session
->path
);
113 /* Kernel tracer session creation */
114 ret
= kernctl_create_session(tracer_fd
);
116 PERROR("ioctl kernel create session");
121 /* Prevent fd duplication after execlp() */
122 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
124 PERROR("fcntl session fd");
127 lks
->consumer_fds_sent
= 0;
128 session
->kernel_session
= lks
;
130 DBG("Kernel session created (fd: %d)", lks
->fd
);
139 * Create a kernel channel, register it to the kernel tracer and add it to the
142 int kernel_create_channel(struct ltt_kernel_session
*session
,
143 struct lttng_channel
*chan
, char *path
)
146 struct ltt_kernel_channel
*lkc
;
148 /* Allocate kernel channel */
149 lkc
= trace_kernel_create_channel(chan
, path
);
154 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
155 chan
->name
, path
, lkc
->channel
->attr
.overwrite
,
156 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
157 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
158 lkc
->channel
->attr
.output
);
160 /* Kernel tracer channel creation */
161 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
163 PERROR("ioctl kernel create channel");
167 /* Setup the channel fd */
169 /* Prevent fd duplication after execlp() */
170 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
172 PERROR("fcntl session fd");
175 /* Add channel to session */
176 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
177 session
->channel_count
++;
179 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
188 * Create a kernel event, enable it to the kernel tracer and add it to the
189 * channel event list of the kernel session.
191 int kernel_create_event(struct lttng_event
*ev
,
192 struct ltt_kernel_channel
*channel
)
195 struct ltt_kernel_event
*event
;
197 event
= trace_kernel_create_event(ev
);
203 ret
= kernctl_create_event(channel
->fd
, event
->event
);
209 WARN("Event type not implemented");
212 PERROR("create event ioctl");
219 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
221 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
222 DBG2("Kernel event syscall creation success");
224 * We use fd == -1 to ensure that we never trigger a close of fd
232 /* Prevent fd duplication after execlp() */
233 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
235 PERROR("fcntl session fd");
239 /* Add event to event list */
240 cds_list_add(&event
->list
, &channel
->events_list
.head
);
241 channel
->event_count
++;
243 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
254 * Disable a kernel channel.
256 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
260 ret
= kernctl_disable(chan
->fd
);
262 PERROR("disable chan ioctl");
268 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
277 * Enable a kernel channel.
279 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
283 ret
= kernctl_enable(chan
->fd
);
284 if (ret
< 0 && errno
!= EEXIST
) {
285 PERROR("Enable kernel chan");
290 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
299 * Enable a kernel event.
301 int kernel_enable_event(struct ltt_kernel_event
*event
)
305 ret
= kernctl_enable(event
->fd
);
309 ret
= LTTCOMM_KERN_EVENT_EXIST
;
312 PERROR("enable kernel event");
319 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
328 * Disable a kernel event.
330 int kernel_disable_event(struct ltt_kernel_event
*event
)
334 ret
= kernctl_disable(event
->fd
);
338 ret
= LTTCOMM_KERN_EVENT_EXIST
;
341 PERROR("disable kernel event");
348 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
357 * Create kernel metadata, open from the kernel tracer and add it to the
360 int kernel_open_metadata(struct ltt_kernel_session
*session
)
363 struct ltt_kernel_metadata
*lkm
;
365 /* Allocate kernel metadata */
366 lkm
= trace_kernel_create_metadata();
371 /* Kernel tracer metadata creation */
372 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
378 /* Prevent fd duplication after execlp() */
379 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
381 PERROR("fcntl session fd");
384 session
->metadata
= lkm
;
386 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
395 * Start tracing session.
397 int kernel_start_session(struct ltt_kernel_session
*session
)
401 ret
= kernctl_start_session(session
->fd
);
403 PERROR("ioctl start session");
407 DBG("Kernel session started");
416 * Make a kernel wait to make sure in-flight probe have completed.
418 void kernel_wait_quiescent(int fd
)
422 DBG("Kernel quiescent wait on %d", fd
);
424 ret
= kernctl_wait_quiescent(fd
);
426 PERROR("wait quiescent ioctl");
427 ERR("Kernel quiescent wait failed");
434 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
438 ret
= kernctl_calibrate(fd
, calibrate
);
440 PERROR("calibrate ioctl");
449 * Force flush buffer of metadata.
451 int kernel_metadata_flush_buffer(int fd
)
455 ret
= kernctl_buffer_flush(fd
);
457 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
464 * Force flush buffer for channel.
466 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
469 struct ltt_kernel_stream
*stream
;
471 DBG("Flush buffer for channel %s", channel
->channel
->name
);
473 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
474 DBG("Flushing channel stream %d", stream
->fd
);
475 ret
= kernctl_buffer_flush(stream
->fd
);
478 ERR("Fail to flush buffer for stream %d (ret: %d)",
487 * Stop tracing session.
489 int kernel_stop_session(struct ltt_kernel_session
*session
)
493 ret
= kernctl_stop_session(session
->fd
);
498 DBG("Kernel session stopped");
507 * Open stream of channel, register it to the kernel tracer and add it
508 * to the stream list of the channel.
510 * Return the number of created stream. Else, a negative value.
512 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
515 struct ltt_kernel_stream
*lks
;
517 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
518 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
528 /* Prevent fd duplication after execlp() */
529 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
531 PERROR("fcntl session fd");
534 /* Add stream to channe stream list */
535 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
536 channel
->stream_count
++;
538 /* Increment counter which represent CPU number. */
541 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
545 return channel
->stream_count
;
552 * Open the metadata stream and set it to the kernel session.
554 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
558 ret
= kernctl_create_stream(session
->metadata
->fd
);
560 PERROR("kernel create metadata stream");
564 DBG("Kernel metadata stream created (fd: %d)", ret
);
565 session
->metadata_stream_fd
= ret
;
566 /* Prevent fd duplication after execlp() */
567 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
569 PERROR("fcntl session fd");
579 * Get the event list from the kernel tracer and return the number of elements.
581 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
585 size_t nbmem
, count
= 0;
588 struct lttng_event
*elist
;
590 fd
= kernctl_tracepoint_list(tracer_fd
);
592 PERROR("kernel tracepoint list");
596 fp
= fdopen(fd
, "r");
598 PERROR("kernel tracepoint list fdopen");
603 * Init memory size counter
604 * See kernel-ctl.h for explanation of this value
606 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
607 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
609 PERROR("alloc list events");
614 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
615 if (count
>= nbmem
) {
616 struct lttng_event
*new_elist
;
618 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
620 /* Double the size */
622 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
623 if (new_elist
== NULL
) {
624 PERROR("realloc list events");
632 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
633 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
634 elist
[count
].enabled
= -1;
640 DBG("Kernel list events done (%zu events)", count
);
642 ret
= fclose(fp
); /* closes both fp and fd */
658 * Get kernel version and validate it.
660 int kernel_validate_version(int tracer_fd
)
663 struct lttng_kernel_tracer_version version
;
665 ret
= kernctl_tracer_version(tracer_fd
, &version
);
667 ERR("Failed at getting the lttng-modules version");
671 /* Validate version */
672 if (version
.major
!= KERN_MODULES_PRE_MAJOR
673 && version
.major
!= KERN_MODULES_MAJOR
) {
677 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
681 ERR("Kernel major version %d is not compatible (supporting <= %d)",
682 version
.major
, KERN_MODULES_MAJOR
)
690 * Kernel work-arounds called at the start of sessiond main().
692 int init_kernel_workarounds(void)
698 * boot_id needs to be read once before being used concurrently
699 * to deal with a Linux kernel race. A fix is proposed for
700 * upstream, but the work-around is needed for older kernels.
702 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
709 ret
= fread(buf
, 1, sizeof(buf
), fp
);
711 /* Ignore error, we don't really care */