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 * Add context on a kernel event.
72 int kernel_add_event_context(struct ltt_kernel_event
*event
,
73 struct lttng_kernel_context
*ctx
)
77 DBG("Adding context to event %s", event
->event
->name
);
78 ret
= kernctl_add_context(event
->fd
, ctx
);
80 PERROR("add context ioctl");
84 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
85 if (event
->ctx
== NULL
) {
86 PERROR("zmalloc event context");
90 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
99 * Create a new kernel session, register it to the kernel tracer and add it to
100 * the session daemon session.
102 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
105 struct ltt_kernel_session
*lks
;
107 /* Allocate data structure */
108 lks
= trace_kernel_create_session(session
->path
);
114 /* Kernel tracer session creation */
115 ret
= kernctl_create_session(tracer_fd
);
117 PERROR("ioctl kernel create session");
122 /* Prevent fd duplication after execlp() */
123 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
125 PERROR("fcntl session fd");
128 lks
->id
= session
->id
;
129 lks
->consumer_fds_sent
= 0;
130 session
->kernel_session
= lks
;
132 DBG("Kernel session created (fd: %d)", lks
->fd
);
141 * Create a kernel channel, register it to the kernel tracer and add it to the
144 int kernel_create_channel(struct ltt_kernel_session
*session
,
145 struct lttng_channel
*chan
, char *path
)
148 struct ltt_kernel_channel
*lkc
;
150 /* Allocate kernel channel */
151 lkc
= trace_kernel_create_channel(chan
, path
);
156 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64
", %" PRIu64
", %u, %u, %d",
157 chan
->name
, path
, lkc
->channel
->attr
.overwrite
,
158 lkc
->channel
->attr
.subbuf_size
, lkc
->channel
->attr
.num_subbuf
,
159 lkc
->channel
->attr
.switch_timer_interval
, lkc
->channel
->attr
.read_timer_interval
,
160 lkc
->channel
->attr
.output
);
162 /* Kernel tracer channel creation */
163 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
165 PERROR("ioctl kernel create channel");
169 /* Setup the channel fd */
171 /* Prevent fd duplication after execlp() */
172 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
174 PERROR("fcntl session fd");
177 /* Add channel to session */
178 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
179 session
->channel_count
++;
181 DBG("Kernel channel %s created (fd: %d)", lkc
->channel
->name
, lkc
->fd
);
190 * Create a kernel event, enable it to the kernel tracer and add it to the
191 * channel event list of the kernel session.
193 int kernel_create_event(struct lttng_event
*ev
,
194 struct ltt_kernel_channel
*channel
)
197 struct ltt_kernel_event
*event
;
199 event
= trace_kernel_create_event(ev
);
205 ret
= kernctl_create_event(channel
->fd
, event
->event
);
211 WARN("Event type not implemented");
214 PERROR("create event ioctl");
221 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
223 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
224 DBG2("Kernel event syscall creation success");
226 * We use fd == -1 to ensure that we never trigger a close of fd
234 /* Prevent fd duplication after execlp() */
235 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
237 PERROR("fcntl session fd");
241 /* Add event to event list */
242 cds_list_add(&event
->list
, &channel
->events_list
.head
);
243 channel
->event_count
++;
245 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
256 * Disable a kernel channel.
258 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
262 ret
= kernctl_disable(chan
->fd
);
264 PERROR("disable chan ioctl");
270 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
279 * Enable a kernel channel.
281 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
285 ret
= kernctl_enable(chan
->fd
);
286 if (ret
< 0 && errno
!= EEXIST
) {
287 PERROR("Enable kernel chan");
292 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
301 * Enable a kernel event.
303 int kernel_enable_event(struct ltt_kernel_event
*event
)
307 ret
= kernctl_enable(event
->fd
);
311 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
314 PERROR("enable kernel event");
321 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
330 * Disable a kernel event.
332 int kernel_disable_event(struct ltt_kernel_event
*event
)
336 ret
= kernctl_disable(event
->fd
);
340 ret
= LTTNG_ERR_KERN_EVENT_EXIST
;
343 PERROR("disable kernel event");
350 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
359 * Create kernel metadata, open from the kernel tracer and add it to the
362 int kernel_open_metadata(struct ltt_kernel_session
*session
)
365 struct ltt_kernel_metadata
*lkm
;
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
);
397 * Start tracing session.
399 int kernel_start_session(struct ltt_kernel_session
*session
)
403 ret
= kernctl_start_session(session
->fd
);
405 PERROR("ioctl start session");
409 DBG("Kernel session started");
418 * Make a kernel wait to make sure in-flight probe have completed.
420 void kernel_wait_quiescent(int fd
)
424 DBG("Kernel quiescent wait on %d", fd
);
426 ret
= kernctl_wait_quiescent(fd
);
428 PERROR("wait quiescent ioctl");
429 ERR("Kernel quiescent wait failed");
436 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
440 ret
= kernctl_calibrate(fd
, calibrate
);
442 PERROR("calibrate ioctl");
451 * Force flush buffer of metadata.
453 int kernel_metadata_flush_buffer(int fd
)
457 ret
= kernctl_buffer_flush(fd
);
459 ERR("Fail to flush metadata buffers %d (ret: %d)", fd
, ret
);
466 * Force flush buffer for channel.
468 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
471 struct ltt_kernel_stream
*stream
;
473 DBG("Flush buffer for channel %s", channel
->channel
->name
);
475 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
476 DBG("Flushing channel stream %d", stream
->fd
);
477 ret
= kernctl_buffer_flush(stream
->fd
);
480 ERR("Fail to flush buffer for stream %d (ret: %d)",
489 * Stop tracing session.
491 int kernel_stop_session(struct ltt_kernel_session
*session
)
495 ret
= kernctl_stop_session(session
->fd
);
500 DBG("Kernel session stopped");
509 * Open stream of channel, register it to the kernel tracer and add it
510 * to the stream list of the channel.
512 * Return the number of created stream. Else, a negative value.
514 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
517 struct ltt_kernel_stream
*lks
;
519 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
520 lks
= trace_kernel_create_stream(channel
->channel
->name
, count
);
530 /* Prevent fd duplication after execlp() */
531 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
533 PERROR("fcntl session fd");
536 /* Add stream to channe stream list */
537 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
538 channel
->stream_count
++;
540 /* Increment counter which represent CPU number. */
543 DBG("Kernel stream %s created (fd: %d, state: %d)", lks
->name
, lks
->fd
,
547 return channel
->stream_count
;
554 * Open the metadata stream and set it to the kernel session.
556 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
560 ret
= kernctl_create_stream(session
->metadata
->fd
);
562 PERROR("kernel create metadata stream");
566 DBG("Kernel metadata stream created (fd: %d)", ret
);
567 session
->metadata_stream_fd
= ret
;
568 /* Prevent fd duplication after execlp() */
569 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
571 PERROR("fcntl session fd");
581 * Get the event list from the kernel tracer and return the number of elements.
583 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
587 size_t nbmem
, count
= 0;
590 struct lttng_event
*elist
;
592 fd
= kernctl_tracepoint_list(tracer_fd
);
594 PERROR("kernel tracepoint list");
598 fp
= fdopen(fd
, "r");
600 PERROR("kernel tracepoint list fdopen");
605 * Init memory size counter
606 * See kernel-ctl.h for explanation of this value
608 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
609 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
611 PERROR("alloc list events");
616 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
617 if (count
>= nbmem
) {
618 struct lttng_event
*new_elist
;
620 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
622 /* Double the size */
624 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
625 if (new_elist
== NULL
) {
626 PERROR("realloc list events");
634 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
635 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
636 elist
[count
].enabled
= -1;
642 DBG("Kernel list events done (%zu events)", count
);
644 ret
= fclose(fp
); /* closes both fp and fd */
660 * Get kernel version and validate it.
662 int kernel_validate_version(int tracer_fd
)
665 struct lttng_kernel_tracer_version version
;
667 ret
= kernctl_tracer_version(tracer_fd
, &version
);
669 ERR("Failed at getting the lttng-modules version");
673 /* Validate version */
674 if (version
.major
!= KERN_MODULES_PRE_MAJOR
675 && version
.major
!= KERN_MODULES_MAJOR
) {
679 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
683 ERR("Kernel major version %d is not compatible (supporting <= %d)",
684 version
.major
, KERN_MODULES_MAJOR
)
692 * Kernel work-arounds called at the start of sessiond main().
694 int init_kernel_workarounds(void)
700 * boot_id needs to be read once before being used concurrently
701 * to deal with a Linux kernel race. A fix is proposed for
702 * upstream, but the work-around is needed for older kernels.
704 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
711 ret
= fread(buf
, 1, sizeof(buf
), fp
);
713 /* Ignore error, we don't really care */
725 * Complete teardown of a kernel session.
727 void kernel_destroy_session(struct ltt_kernel_session
*ksess
)
730 DBG3("No kernel session when tearing down session");
734 DBG("Tearing down kernel session");
736 /* Close any relayd session */
737 consumer_output_send_destroy_relayd(ksess
->consumer
);
739 trace_kernel_destroy_session(ksess
);