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.
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/sessiond-comm/sessiond-comm.h>
31 #include "kern-modules.h"
34 * Add context on a kernel channel.
36 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
37 struct lttng_kernel_context
*ctx
)
41 DBG("Adding context to channel %s", chan
->channel
->name
);
42 ret
= kernctl_add_context(chan
->fd
, ctx
);
44 if (errno
!= EEXIST
) {
45 PERROR("add context ioctl");
47 /* If EEXIST, we just ignore the error */
53 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
54 if (chan
->ctx
== NULL
) {
55 PERROR("zmalloc event context");
59 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
68 * Add context on a kernel event.
70 int kernel_add_event_context(struct ltt_kernel_event
*event
,
71 struct lttng_kernel_context
*ctx
)
75 DBG("Adding context to event %s", event
->event
->name
);
76 ret
= kernctl_add_context(event
->fd
, ctx
);
78 PERROR("add context ioctl");
82 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
83 if (event
->ctx
== NULL
) {
84 PERROR("zmalloc event context");
88 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
97 * Create a new kernel session, register it to the kernel tracer and add it to
98 * the session daemon session.
100 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
103 struct ltt_kernel_session
*lks
;
105 /* Allocate data structure */
106 lks
= trace_kernel_create_session(session
->path
);
112 /* Kernel tracer session creation */
113 ret
= kernctl_create_session(tracer_fd
);
115 PERROR("ioctl kernel create session");
120 /* Prevent fd duplication after execlp() */
121 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
123 PERROR("fcntl session fd");
126 lks
->consumer_fds_sent
= 0;
127 session
->kernel_session
= lks
;
129 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * Create a kernel channel, register it to the kernel tracer and add it to the
141 int kernel_create_channel(struct ltt_kernel_session
*session
,
142 struct lttng_channel
*chan
, char *path
)
145 struct ltt_kernel_channel
*lkc
;
147 /* Allocate kernel channel */
148 lkc
= trace_kernel_create_channel(chan
, path
);
153 /* Kernel tracer channel creation */
154 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
156 PERROR("ioctl kernel create channel");
160 /* Setup the channel fd */
162 /* Prevent fd duplication after execlp() */
163 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
165 PERROR("fcntl session fd");
168 /* Add channel to session */
169 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
170 session
->channel_count
++;
172 DBG("Kernel channel %s created (fd: %d and path: %s)",
173 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
182 * Create a kernel event, enable it to the kernel tracer and add it to the
183 * channel event list of the kernel session.
185 int kernel_create_event(struct lttng_event
*ev
,
186 struct ltt_kernel_channel
*channel
)
189 struct ltt_kernel_event
*event
;
191 event
= trace_kernel_create_event(ev
);
197 ret
= kernctl_create_event(channel
->fd
, event
->event
);
203 WARN("Event type not implemented");
206 PERROR("create event ioctl");
213 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
215 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
216 DBG2("Kernel event syscall creation success");
218 * We use fd == -1 to ensure that we never trigger a close of fd
226 /* Prevent fd duplication after execlp() */
227 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
229 PERROR("fcntl session fd");
233 /* Add event to event list */
234 cds_list_add(&event
->list
, &channel
->events_list
.head
);
235 channel
->event_count
++;
237 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
248 * Disable a kernel channel.
250 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
)
277 ret
= kernctl_enable(chan
->fd
);
278 if (ret
< 0 && errno
!= EEXIST
) {
279 PERROR("Enable kernel chan");
284 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
293 * Enable a kernel event.
295 int kernel_enable_event(struct ltt_kernel_event
*event
)
299 ret
= kernctl_enable(event
->fd
);
303 ret
= LTTCOMM_KERN_EVENT_EXIST
;
306 PERROR("enable kernel event");
313 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
322 * Disable a kernel event.
324 int kernel_disable_event(struct ltt_kernel_event
*event
)
328 ret
= kernctl_disable(event
->fd
);
332 ret
= LTTCOMM_KERN_EVENT_EXIST
;
335 PERROR("disable kernel event");
342 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
351 * Create kernel metadata, open from the kernel tracer and add it to the
354 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
357 struct ltt_kernel_metadata
*lkm
;
359 /* Allocate kernel metadata */
360 lkm
= trace_kernel_create_metadata(path
);
365 /* Kernel tracer metadata creation */
366 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
372 /* Prevent fd duplication after execlp() */
373 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
375 PERROR("fcntl session fd");
378 session
->metadata
= lkm
;
380 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
389 * Start tracing session.
391 int kernel_start_session(struct ltt_kernel_session
*session
)
395 ret
= kernctl_start_session(session
->fd
);
397 PERROR("ioctl start session");
401 DBG("Kernel session started");
410 * Make a kernel wait to make sure in-flight probe have completed.
412 void kernel_wait_quiescent(int fd
)
416 DBG("Kernel quiescent wait on %d", fd
);
418 ret
= kernctl_wait_quiescent(fd
);
420 PERROR("wait quiescent ioctl");
421 ERR("Kernel quiescent wait failed");
428 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
432 ret
= kernctl_calibrate(fd
, calibrate
);
434 PERROR("calibrate ioctl");
443 * Force flush buffer of metadata.
445 int kernel_metadata_flush_buffer(int fd
)
449 ret
= kernctl_buffer_flush(fd
);
451 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
458 * Force flush buffer for channel.
460 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
463 struct ltt_kernel_stream
*stream
;
465 DBG("Flush buffer for channel %s", channel
->channel
->name
);
467 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
468 DBG("Flushing channel stream %d", stream
->fd
);
469 ret
= kernctl_buffer_flush(stream
->fd
);
472 ERR("Fail to flush buffer for stream %d (ret: %d)",
481 * Stop tracing session.
483 int kernel_stop_session(struct ltt_kernel_session
*session
)
487 ret
= kernctl_stop_session(session
->fd
);
492 DBG("Kernel session stopped");
501 * Open stream of channel, register it to the kernel tracer and add it
502 * to the stream list of the channel.
504 * Return the number of created stream. Else, a negative value.
506 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
509 struct ltt_kernel_stream
*lks
;
511 while ((ret
= kernctl_create_stream(channel
->fd
)) >= 0) {
512 lks
= trace_kernel_create_stream();
522 /* Prevent fd duplication after execlp() */
523 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
525 PERROR("fcntl session fd");
528 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
529 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
531 PERROR("asprintf kernel create stream");
535 /* Add stream to channe stream list */
536 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
537 channel
->stream_count
++;
539 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
540 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
543 return channel
->stream_count
;
550 * Open the metadata stream and set it to the kernel session.
552 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
556 ret
= kernctl_create_stream(session
->metadata
->fd
);
558 PERROR("kernel create metadata stream");
562 DBG("Kernel metadata stream created (fd: %d)", ret
);
563 session
->metadata_stream_fd
= ret
;
564 /* Prevent fd duplication after execlp() */
565 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
567 PERROR("fcntl session fd");
577 * Get the event list from the kernel tracer and return the number of elements.
579 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
583 size_t nbmem
, count
= 0;
586 struct lttng_event
*elist
;
588 fd
= kernctl_tracepoint_list(tracer_fd
);
590 PERROR("kernel tracepoint list");
594 fp
= fdopen(fd
, "r");
596 PERROR("kernel tracepoint list fdopen");
601 * Init memory size counter
602 * See kernel-ctl.h for explanation of this value
604 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
605 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
607 PERROR("alloc list events");
612 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
613 if (count
>= nbmem
) {
614 struct lttng_event
*new_elist
;
616 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
618 /* Double the size */
620 new_elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
621 if (new_elist
== NULL
) {
622 PERROR("realloc list events");
630 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
631 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
632 elist
[count
].enabled
= -1;
638 DBG("Kernel list events done (%zu events)", count
);
640 ret
= fclose(fp
); /* closes both fp and fd */
656 * Get kernel version and validate it.
658 int kernel_validate_version(int tracer_fd
)
661 struct lttng_kernel_tracer_version version
;
663 ret
= kernctl_tracer_version(tracer_fd
, &version
);
665 ERR("Failed at getting the lttng-modules version");
669 /* Validate version */
670 if (version
.major
!= KERN_MODULES_PRE_MAJOR
671 && version
.major
!= KERN_MODULES_MAJOR
) {
675 DBG2("Kernel tracer version validated (major version %d)", version
.major
);
679 ERR("Kernel major version %d is not compatible (supporting <= %d)",
680 version
.major
, KERN_MODULES_MAJOR
)
688 * Kernel work-arounds called at the start of sessiond main().
690 int init_kernel_workarounds(void)
696 * boot_id needs to be read once before being used concurrently
697 * to deal with a Linux kernel race. A fix is proposed for
698 * upstream, but the work-around is needed for older kernels.
700 fp
= fopen("/proc/sys/kernel/random/boot_id", "r");
707 ret
= fread(buf
, 1, sizeof(buf
), fp
);
709 /* Ignore error, we don't really care */