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>
33 #include "kern-modules.h"
36 * Add context on a kernel channel.
38 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
39 struct lttng_kernel_context
*ctx
)
43 DBG("Adding context to channel %s", chan
->channel
->name
);
44 ret
= kernctl_add_context(chan
->fd
, ctx
);
46 if (errno
!= EEXIST
) {
47 PERROR("add context ioctl");
49 /* If EEXIST, we just ignore the error */
55 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
56 if (chan
->ctx
== NULL
) {
57 PERROR("zmalloc event context");
61 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
73 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
76 struct ltt_kernel_session
*lks
;
78 /* Allocate data structure */
79 lks
= trace_kernel_create_session(session
->path
);
85 /* Kernel tracer session creation */
86 ret
= kernctl_create_session(tracer_fd
);
88 PERROR("ioctl kernel create session");
93 /* Prevent fd duplication after execlp() */
94 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
96 PERROR("fcntl session fd");
99 lks
->id
= session
->id
;
100 lks
->consumer_fds_sent
= 0;
101 session
->kernel_session
= lks
;
103 DBG("Kernel session created (fd: %d)", lks
->fd
);
112 * Create a kernel channel, register it to the kernel tracer and add it to the
115 int kernel_create_channel(struct ltt_kernel_session
*session
,
116 struct lttng_channel
*chan
, char *path
)
119 struct ltt_kernel_channel
*lkc
;
121 /* Allocate kernel channel */
122 lkc
= trace_kernel_create_channel(chan
, path
);
127 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
128 chan
->name
, path
, lkc
->channel
->attr
.overwrite
,
129 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
130 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
131 lkc
->channel
->attr
.output
);
133 /* Kernel tracer channel creation */
134 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
136 PERROR("ioctl kernel create channel");
140 /* Setup the channel fd */
142 /* Prevent fd duplication after execlp() */
143 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
145 PERROR("fcntl session fd");
148 /* Add channel to session */
149 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
150 session
->channel_count
++;
152 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
161 * Create a kernel event, enable it to the kernel tracer and add it to the
162 * channel event list of the kernel session.
164 int kernel_create_event(struct lttng_event
*ev
,
165 struct ltt_kernel_channel
*channel
)
168 struct ltt_kernel_event
*event
;
170 event
= trace_kernel_create_event(ev
);
176 ret
= kernctl_create_event(channel
->fd
, event
->event
);
182 WARN("Event type not implemented");
185 PERROR("create event ioctl");
192 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
194 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
195 DBG2("Kernel event syscall creation success");
197 * We use fd == -1 to ensure that we never trigger a close of fd
205 /* Prevent fd duplication after execlp() */
206 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
208 PERROR("fcntl session fd");
212 /* Add event to event list */
213 cds_list_add(&event
->list
, &channel
->events_list
.head
);
214 channel
->event_count
++;
216 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
227 * Disable a kernel channel.
229 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
233 ret
= kernctl_disable(chan
->fd
);
235 PERROR("disable chan ioctl");
241 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
250 * Enable a kernel channel.
252 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
256 ret
= kernctl_enable(chan
->fd
);
257 if (ret
< 0 && errno
!= EEXIST
) {
258 PERROR("Enable kernel chan");
263 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
272 * Enable a kernel event.
274 int kernel_enable_event(struct ltt_kernel_event
*event
)
278 ret
= kernctl_enable(event
->fd
);
282 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
285 PERROR("enable kernel event");
292 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
301 * Disable a kernel event.
303 int kernel_disable_event(struct ltt_kernel_event
*event
)
307 ret
= kernctl_disable(event
->fd
);
311 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
314 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
)
336 struct ltt_kernel_metadata
*lkm
;
338 /* Allocate kernel metadata */
339 lkm
= trace_kernel_create_metadata();
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)", lkm
->fd
);
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(channel
->channel
->name
, count
);
501 /* Prevent fd duplication after execlp() */
502 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
504 PERROR("fcntl session fd");
507 /* Add stream to channe stream list */
508 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
509 channel
->stream_count
++;
511 /* Increment counter which represent CPU number. */
514 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
518 return channel
->stream_count
;
525 * Open the metadata stream and set it to the kernel session.
527 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
531 ret
= kernctl_create_stream(session
->metadata
->fd
);
533 PERROR("kernel create metadata stream");
537 DBG("Kernel metadata stream created (fd: %d)", ret
);
538 session
->metadata_stream_fd
= ret
;
539 /* Prevent fd duplication after execlp() */
540 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
542 PERROR("fcntl session fd");
552 * Get the event list from the kernel tracer and return the number of elements.
554 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
558 size_t nbmem
, count
= 0;
561 struct lttng_event
*elist
;
563 fd
= kernctl_tracepoint_list(tracer_fd
);
565 PERROR("kernel tracepoint list");
569 fp
= fdopen(fd
, "r");
571 PERROR("kernel tracepoint list fdopen");
576 * Init memory size counter
577 * See kernel-ctl.h for explanation of this value
579 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
580 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
582 PERROR("alloc list events");
587 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
588 if (count
>= nbmem
) {
589 struct lttng_event
*new_elist
;
591 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
593 /* Double the size */
595 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
596 if (new_elist
== NULL
) {
597 PERROR("realloc list events");
605 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
606 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
607 elist
[count
].enabled
= -1;
613 DBG("Kernel list events done (%zu events)", count
);
615 ret
= fclose(fp
); /* closes both fp and fd */
631 * Get kernel version and validate it.
633 int kernel_validate_version(int tracer_fd
)
636 struct lttng_kernel_tracer_version version
;
638 ret
= kernctl_tracer_version(tracer_fd
, &version
);
640 ERR("Failed at getting the lttng-modules version");
644 /* Validate version */
645 if (version
.major
!= KERN_MODULES_PRE_MAJOR
646 && version
.major
!= KERN_MODULES_MAJOR
) {
650 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
654 ERR("Kernel major version %d is not compatible (supporting <= %d)",
655 version
.major
, KERN_MODULES_MAJOR
)
663 * Kernel work-arounds called at the start of sessiond main().
665 int init_kernel_workarounds(void)
671 * boot_id needs to be read once before being used concurrently
672 * to deal with a Linux kernel race. A fix is proposed for
673 * upstream, but the work-around is needed for older kernels.
675 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
682 ret
= fread(buf
, 1, sizeof(buf
), fp
);
684 /* Ignore error, we don't really care */
696 * Complete teardown of a kernel session.
698 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
701 DBG3("No kernel session when tearing down session");
705 DBG("Tearing down kernel session");
707 /* Close any relayd session */
708 consumer_output_send_destroy_relayd(ksess
->consumer
);
710 trace_kernel_destroy_session(ksess
);