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
)
46 DBG("Adding context to channel %s", chan
->channel
->name
);
47 ret
= kernctl_add_context(chan
->fd
, ctx
);
49 if (errno
!= EEXIST
) {
50 PERROR("add context ioctl");
52 /* If EEXIST, we just ignore the error */
58 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
59 if (chan
->ctx
== NULL
) {
60 PERROR("zmalloc event context");
64 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
73 * Create a new kernel session, register it to the kernel tracer and add it to
74 * the session daemon session.
76 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
79 struct ltt_kernel_session
*lks
;
83 /* Allocate data structure */
84 lks
= trace_kernel_create_session();
90 /* Kernel tracer session creation */
91 ret
= kernctl_create_session(tracer_fd
);
93 PERROR("ioctl kernel create session");
98 /* Prevent fd duplication after execlp() */
99 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
101 PERROR("fcntl session fd");
104 lks
->id
= session
->id
;
105 lks
->consumer_fds_sent
= 0;
106 session
->kernel_session
= lks
;
108 DBG("Kernel session created (fd: %d)", lks
->fd
);
117 * Create a kernel channel, register it to the kernel tracer and add it to the
120 int kernel_create_channel(struct ltt_kernel_session
*session
,
121 struct lttng_channel
*chan
)
124 struct ltt_kernel_channel
*lkc
;
129 /* Allocate kernel channel */
130 lkc
= trace_kernel_create_channel(chan
);
135 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
136 chan
->name
, lkc
->channel
->attr
.overwrite
,
137 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
138 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
139 lkc
->channel
->attr
.output
);
141 /* Kernel tracer channel creation */
142 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
144 PERROR("ioctl kernel create channel");
148 /* Setup the channel fd */
150 /* Prevent fd duplication after execlp() */
151 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
153 PERROR("fcntl session fd");
156 /* Add channel to session */
157 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
158 session
->channel_count
++;
159 lkc
->session
= session
;
161 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
170 * Create a kernel event, enable it to the kernel tracer and add it to the
171 * channel event list of the kernel session.
173 int kernel_create_event(struct lttng_event
*ev
,
174 struct ltt_kernel_channel
*channel
)
177 struct ltt_kernel_event
*event
;
182 event
= trace_kernel_create_event(ev
);
188 ret
= kernctl_create_event(channel
->fd
, event
->event
);
194 WARN("Event type not implemented");
197 PERROR("create event ioctl");
204 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
206 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
207 DBG2("Kernel event syscall creation success");
209 * We use fd == -1 to ensure that we never trigger a close of fd
217 /* Prevent fd duplication after execlp() */
218 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
220 PERROR("fcntl session fd");
224 /* Add event to event list */
225 cds_list_add(&event
->list
, &channel
->events_list
.head
);
226 channel
->event_count
++;
228 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
239 * Disable a kernel channel.
241 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
247 ret
= kernctl_disable(chan
->fd
);
249 PERROR("disable chan ioctl");
255 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
264 * Enable a kernel channel.
266 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
272 ret
= kernctl_enable(chan
->fd
);
273 if (ret
< 0 && errno
!= EEXIST
) {
274 PERROR("Enable kernel chan");
279 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
288 * Enable a kernel event.
290 int kernel_enable_event(struct ltt_kernel_event
*event
)
296 ret
= kernctl_enable(event
->fd
);
300 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
303 PERROR("enable kernel event");
310 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
319 * Disable a kernel event.
321 int kernel_disable_event(struct ltt_kernel_event
*event
)
327 ret
= kernctl_disable(event
->fd
);
331 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
334 PERROR("disable kernel event");
341 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
350 * Create kernel metadata, open from the kernel tracer and add it to the
353 int kernel_open_metadata(struct ltt_kernel_session
*session
)
356 struct ltt_kernel_metadata
*lkm
= NULL
;
360 /* Allocate kernel metadata */
361 lkm
= trace_kernel_create_metadata();
366 /* Kernel tracer metadata creation */
367 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
373 /* Prevent fd duplication after execlp() */
374 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
376 PERROR("fcntl session fd");
379 session
->metadata
= lkm
;
381 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
386 trace_kernel_destroy_metadata(lkm
);
392 * Start tracing session.
394 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
)
439 ret
= kernctl_calibrate(fd
, calibrate
);
441 PERROR("calibrate ioctl");
450 * Force flush buffer of metadata.
452 int kernel_metadata_flush_buffer(int fd
)
456 ret
= kernctl_buffer_flush(fd
);
458 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
465 * Force flush buffer for channel.
467 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
470 struct ltt_kernel_stream
*stream
;
474 DBG("Flush buffer for channel %s", channel
->channel
->name
);
476 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
477 DBG("Flushing channel stream %d", stream
->fd
);
478 ret
= kernctl_buffer_flush(stream
->fd
);
481 ERR("Fail to flush buffer for stream %d (ret: %d)",
490 * Stop tracing session.
492 int kernel_stop_session(struct ltt_kernel_session
*session
)
498 ret
= kernctl_stop_session(session
->fd
);
503 DBG("Kernel session stopped");
512 * Open stream of channel, register it to the kernel tracer and add it
513 * to the stream list of the channel.
515 * Return the number of created stream. Else, a negative value.
517 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
520 struct ltt_kernel_stream
*lks
;
524 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
525 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
535 /* Prevent fd duplication after execlp() */
536 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
538 PERROR("fcntl session fd");
541 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
542 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
544 /* Add stream to channe stream list */
545 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
546 channel
->stream_count
++;
548 /* Increment counter which represent CPU number. */
551 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
555 return channel
->stream_count
;
562 * Open the metadata stream and set it to the kernel session.
564 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
570 ret
= kernctl_create_stream(session
->metadata
->fd
);
572 PERROR("kernel create metadata stream");
576 DBG("Kernel metadata stream created (fd: %d)", ret
);
577 session
->metadata_stream_fd
= ret
;
578 /* Prevent fd duplication after execlp() */
579 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
581 PERROR("fcntl session fd");
591 * Get the event list from the kernel tracer and return the number of elements.
593 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
597 size_t nbmem
, count
= 0;
599 struct lttng_event
*elist
;
603 fd
= kernctl_tracepoint_list(tracer_fd
);
605 PERROR("kernel tracepoint list");
609 fp
= fdopen(fd
, "r");
611 PERROR("kernel tracepoint list fdopen");
616 * Init memory size counter
617 * See kernel-ctl.h for explanation of this value
619 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
620 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
622 PERROR("alloc list events");
627 while (fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
) == 1) {
628 if (count
>= nbmem
) {
629 struct lttng_event
*new_elist
;
631 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
633 /* Double the size */
635 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
636 if (new_elist
== NULL
) {
637 PERROR("realloc list events");
645 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
646 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
647 elist
[count
].enabled
= -1;
653 DBG("Kernel list events done (%zu events)", count
);
655 ret
= fclose(fp
); /* closes both fp and fd */
671 * Get kernel version and validate it.
673 int kernel_validate_version(int tracer_fd
)
676 struct lttng_kernel_tracer_version version
;
678 ret
= kernctl_tracer_version(tracer_fd
, &version
);
680 ERR("Failed at getting the lttng-modules version");
684 /* Validate version */
685 if (version
.major
!= KERN_MODULES_PRE_MAJOR
686 && version
.major
!= KERN_MODULES_MAJOR
) {
690 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
694 ERR("Kernel major version %d is not compatible (supporting <= %d)",
695 version
.major
, KERN_MODULES_MAJOR
)
703 * Kernel work-arounds called at the start of sessiond main().
705 int init_kernel_workarounds(void)
711 * boot_id needs to be read once before being used concurrently
712 * to deal with a Linux kernel race. A fix is proposed for
713 * upstream, but the work-around is needed for older kernels.
715 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
722 ret
= fread(buf
, 1, sizeof(buf
), fp
);
724 /* Ignore error, we don't really care */
736 * Complete teardown of a kernel session.
738 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
741 DBG3("No kernel session when tearing down session");
745 DBG("Tearing down kernel session");
747 /* Close any relayd session */
748 consumer_output_send_destroy_relayd(ksess
->consumer
);
750 trace_kernel_destroy_session(ksess
);
754 * Destroy a kernel channel object. It does not do anything on the tracer side.
756 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
758 struct ltt_kernel_session
*ksess
= NULL
;
761 assert(kchan
->channel
);
763 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
765 /* Update channel count of associated session. */
766 if (kchan
->session
) {
767 /* Keep pointer reference so we can update it after the destroy. */
768 ksess
= kchan
->session
;
771 trace_kernel_destroy_channel(kchan
);
774 * At this point the kernel channel is not visible anymore. This is safe
775 * since in order to work on a visible kernel session, the tracing session
776 * lock (ltt_session.lock) MUST be acquired.
779 ksess
->channel_count
--;