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
++;
151 lkc
->session
= session
;
153 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
162 * Create a kernel event, enable it to the kernel tracer and add it to the
163 * channel event list of the kernel session.
165 int kernel_create_event(struct lttng_event
*ev
,
166 struct ltt_kernel_channel
*channel
)
169 struct ltt_kernel_event
*event
;
171 event
= trace_kernel_create_event(ev
);
177 ret
= kernctl_create_event(channel
->fd
, event
->event
);
183 WARN("Event type not implemented");
186 PERROR("create event ioctl");
193 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
195 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
196 DBG2("Kernel event syscall creation success");
198 * We use fd == -1 to ensure that we never trigger a close of fd
206 /* Prevent fd duplication after execlp() */
207 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
209 PERROR("fcntl session fd");
213 /* Add event to event list */
214 cds_list_add(&event
->list
, &channel
->events_list
.head
);
215 channel
->event_count
++;
217 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
228 * Disable a kernel channel.
230 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
234 ret
= kernctl_disable(chan
->fd
);
236 PERROR("disable chan ioctl");
242 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
251 * Enable a kernel channel.
253 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
257 ret
= kernctl_enable(chan
->fd
);
258 if (ret
< 0 && errno
!= EEXIST
) {
259 PERROR("Enable kernel chan");
264 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
273 * Enable a kernel event.
275 int kernel_enable_event(struct ltt_kernel_event
*event
)
279 ret
= kernctl_enable(event
->fd
);
283 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
286 PERROR("enable kernel event");
293 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
302 * Disable a kernel event.
304 int kernel_disable_event(struct ltt_kernel_event
*event
)
308 ret
= kernctl_disable(event
->fd
);
312 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
315 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
)
337 struct ltt_kernel_metadata
*lkm
;
339 /* Allocate kernel metadata */
340 lkm
= trace_kernel_create_metadata();
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)", lkm
->fd
);
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(channel
->channel
->name
, count
);
502 /* Prevent fd duplication after execlp() */
503 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
505 PERROR("fcntl session fd");
508 /* Add stream to channe stream list */
509 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
510 channel
->stream_count
++;
512 /* Increment counter which represent CPU number. */
515 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
519 return channel
->stream_count
;
526 * Open the metadata stream and set it to the kernel session.
528 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
532 ret
= kernctl_create_stream(session
->metadata
->fd
);
534 PERROR("kernel create metadata stream");
538 DBG("Kernel metadata stream created (fd: %d)", ret
);
539 session
->metadata_stream_fd
= ret
;
540 /* Prevent fd duplication after execlp() */
541 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
543 PERROR("fcntl session fd");
553 * Get the event list from the kernel tracer and return the number of elements.
555 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
559 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 (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
);
714 * Destroy a kernel channel object. It does not do anything on the tracer side.
716 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
718 struct ltt_kernel_session
*ksess
= NULL
;
721 assert(kchan
->channel
);
723 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
725 /* Update channel count of associated session. */
726 if (kchan
->session
) {
727 /* Keep pointer reference so we can update it after the destroy. */
728 ksess
= kchan
->session
;
731 trace_kernel_destroy_channel(kchan
);
734 * At this point the kernel channel is not visible anymore. This is safe
735 * since in order to work on a visible kernel session, the tracing session
736 * lock (ltt_session.lock) MUST be acquired.
739 ksess
->channel_count
--;