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
);
114 trace_kernel_destroy_session(lks
);
120 * Create a kernel channel, register it to the kernel tracer and add it to the
123 int kernel_create_channel(struct ltt_kernel_session
*session
,
124 struct lttng_channel
*chan
)
127 struct ltt_kernel_channel
*lkc
;
132 /* Allocate kernel channel */
133 lkc
= trace_kernel_create_channel(chan
);
138 DBG3("Kernel create channel %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
139 chan
->name
, lkc
->channel
->attr
.overwrite
,
140 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
141 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
142 lkc
->channel
->attr
.output
);
144 /* Kernel tracer channel creation */
145 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
147 PERROR("ioctl kernel create channel");
151 /* Setup the channel fd */
153 /* Prevent fd duplication after execlp() */
154 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
156 PERROR("fcntl session fd");
159 /* Add channel to session */
160 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
161 session
->channel_count
++;
162 lkc
->session
= session
;
164 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
177 * Create a kernel event, enable it to the kernel tracer and add it to the
178 * channel event list of the kernel session.
180 int kernel_create_event(struct lttng_event
*ev
,
181 struct ltt_kernel_channel
*channel
)
184 struct ltt_kernel_event
*event
;
189 event
= trace_kernel_create_event(ev
);
195 ret
= kernctl_create_event(channel
->fd
, event
->event
);
201 WARN("Event type not implemented");
204 PERROR("create event ioctl");
211 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
213 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
214 DBG2("Kernel event syscall creation success");
216 * We use fd == -1 to ensure that we never trigger a close of fd
224 /* Prevent fd duplication after execlp() */
225 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
227 PERROR("fcntl session fd");
231 /* Add event to event list */
232 cds_list_add(&event
->list
, &channel
->events_list
.head
);
233 channel
->event_count
++;
235 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
246 * Disable a kernel channel.
248 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
254 ret
= kernctl_disable(chan
->fd
);
256 PERROR("disable chan ioctl");
262 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
271 * Enable a kernel channel.
273 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
279 ret
= kernctl_enable(chan
->fd
);
280 if (ret
< 0 && errno
!= EEXIST
) {
281 PERROR("Enable kernel chan");
286 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
295 * Enable a kernel event.
297 int kernel_enable_event(struct ltt_kernel_event
*event
)
303 ret
= kernctl_enable(event
->fd
);
307 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
310 PERROR("enable kernel event");
317 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
326 * Disable a kernel event.
328 int kernel_disable_event(struct ltt_kernel_event
*event
)
334 ret
= kernctl_disable(event
->fd
);
338 ret
= LTTNG_ERR_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
= NULL
;
367 /* Allocate kernel metadata */
368 lkm
= trace_kernel_create_metadata();
373 /* Kernel tracer metadata creation */
374 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
380 /* Prevent fd duplication after execlp() */
381 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
383 PERROR("fcntl session fd");
386 session
->metadata
= lkm
;
388 DBG("Kernel metadata opened (fd: %d)", lkm
->fd
);
393 trace_kernel_destroy_metadata(lkm
);
399 * Start tracing session.
401 int kernel_start_session(struct ltt_kernel_session
*session
)
407 ret
= kernctl_start_session(session
->fd
);
409 PERROR("ioctl start session");
413 DBG("Kernel session started");
422 * Make a kernel wait to make sure in-flight probe have completed.
424 void kernel_wait_quiescent(int fd
)
428 DBG("Kernel quiescent wait on %d", fd
);
430 ret
= kernctl_wait_quiescent(fd
);
432 PERROR("wait quiescent ioctl");
433 ERR("Kernel quiescent wait failed");
440 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
446 ret
= kernctl_calibrate(fd
, calibrate
);
448 PERROR("calibrate ioctl");
457 * Force flush buffer of metadata.
459 int kernel_metadata_flush_buffer(int fd
)
463 DBG("Kernel flushing metadata buffer on fd %d", fd
);
465 ret
= kernctl_buffer_flush(fd
);
467 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
474 * Force flush buffer for channel.
476 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
479 struct ltt_kernel_stream
*stream
;
483 DBG("Flush buffer for channel %s", channel
->channel
->name
);
485 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
486 DBG("Flushing channel stream %d", stream
->fd
);
487 ret
= kernctl_buffer_flush(stream
->fd
);
490 ERR("Fail to flush buffer for stream %d (ret: %d)",
499 * Stop tracing session.
501 int kernel_stop_session(struct ltt_kernel_session
*session
)
507 ret
= kernctl_stop_session(session
->fd
);
512 DBG("Kernel session stopped");
521 * Open stream of channel, register it to the kernel tracer and add it
522 * to the stream list of the channel.
524 * Return the number of created stream. Else, a negative value.
526 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
529 struct ltt_kernel_stream
*lks
;
533 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
534 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
544 /* Prevent fd duplication after execlp() */
545 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
547 PERROR("fcntl session fd");
550 lks
->tracefile_size
= channel
->channel
->attr
.tracefile_size
;
551 lks
->tracefile_count
= channel
->channel
->attr
.tracefile_count
;
553 /* Add stream to channe stream list */
554 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
555 channel
->stream_count
++;
557 /* Increment counter which represent CPU number. */
560 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
564 return channel
->stream_count
;
571 * Open the metadata stream and set it to the kernel session.
573 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
579 ret
= kernctl_create_stream(session
->metadata
->fd
);
581 PERROR("kernel create metadata stream");
585 DBG("Kernel metadata stream created (fd: %d)", ret
);
586 session
->metadata_stream_fd
= ret
;
587 /* Prevent fd duplication after execlp() */
588 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
590 PERROR("fcntl session fd");
600 * Get the event list from the kernel tracer and return the number of elements.
602 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
606 size_t nbmem
, count
= 0;
608 struct lttng_event
*elist
;
612 fd
= kernctl_tracepoint_list(tracer_fd
);
614 PERROR("kernel tracepoint list");
618 fp
= fdopen(fd
, "r");
620 PERROR("kernel tracepoint list fdopen");
625 * Init memory size counter
626 * See kernel-ctl.h for explanation of this value
628 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
629 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
631 PERROR("alloc list events");
636 while (fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
) == 1) {
637 if (count
>= nbmem
) {
638 struct lttng_event
*new_elist
;
640 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
642 /* Double the size */
644 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
645 if (new_elist
== NULL
) {
646 PERROR("realloc list events");
654 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
655 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
656 elist
[count
].enabled
= -1;
662 DBG("Kernel list events done (%zu events)", count
);
664 ret
= fclose(fp
); /* closes both fp and fd */
680 * Get kernel version and validate it.
682 int kernel_validate_version(int tracer_fd
)
685 struct lttng_kernel_tracer_version version
;
687 ret
= kernctl_tracer_version(tracer_fd
, &version
);
689 ERR("Failed at getting the lttng-modules version");
693 /* Validate version */
694 if (version
.major
!= KERN_MODULES_PRE_MAJOR
695 && version
.major
!= KERN_MODULES_MAJOR
) {
699 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
703 ERR("Kernel major version %d is not compatible (supporting <= %d)",
704 version
.major
, KERN_MODULES_MAJOR
)
712 * Kernel work-arounds called at the start of sessiond main().
714 int init_kernel_workarounds(void)
720 * boot_id needs to be read once before being used concurrently
721 * to deal with a Linux kernel race. A fix is proposed for
722 * upstream, but the work-around is needed for older kernels.
724 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
731 ret
= fread(buf
, 1, sizeof(buf
), fp
);
733 /* Ignore error, we don't really care */
745 * Complete teardown of a kernel session.
747 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
750 DBG3("No kernel session when tearing down session");
754 DBG("Tearing down kernel session");
756 /* Close any relayd session */
757 consumer_output_send_destroy_relayd(ksess
->consumer
);
759 trace_kernel_destroy_session(ksess
);
763 * Destroy a kernel channel object. It does not do anything on the tracer side.
765 void kernel_destroy_channel(struct ltt_kernel_channel
*kchan
)
767 struct ltt_kernel_session
*ksess
= NULL
;
770 assert(kchan
->channel
);
772 DBG3("Kernel destroy channel %s", kchan
->channel
->name
);
774 /* Update channel count of associated session. */
775 if (kchan
->session
) {
776 /* Keep pointer reference so we can update it after the destroy. */
777 ksess
= kchan
->session
;
780 trace_kernel_destroy_channel(kchan
);
783 * At this point the kernel channel is not visible anymore. This is safe
784 * since in order to work on a visible kernel session, the tracing session
785 * lock (ltt_session.lock) MUST be acquired.
788 ksess
->channel_count
--;