2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; only version 2
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 #include "libkernelctl.h"
29 #include "kernel-ctl.h"
32 * kernel_add_channel_context
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
= malloc(sizeof(struct lttng_kernel_context
));
54 if (chan
->ctx
== NULL
) {
55 perror("malloc event context");
59 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
68 * kernel_add_event_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
= malloc(sizeof(struct lttng_kernel_context
));
85 if (event
->ctx
== NULL
) {
86 perror("malloc event context");
90 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
99 * kernel_create_session
101 * Create a new kernel session, register it to the kernel tracer and add it to
102 * the session daemon session.
104 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
107 struct ltt_kernel_session
*lks
;
109 /* Allocate data structure */
110 lks
= trace_create_kernel_session();
116 /* Kernel tracer session creation */
117 ret
= kernctl_create_session(tracer_fd
);
119 perror("ioctl kernel create session");
124 /* Prevent fd duplication after execlp() */
125 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
127 perror("fcntl session fd");
130 lks
->kconsumer_fds_sent
= 0;
131 session
->kernel_session
= lks
;
133 DBG("Kernel session created (fd: %d)", lks
->fd
);
142 * kernel_create_channel
144 * Create a kernel channel, register it to the kernel tracer and add it to the
147 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
, char *path
)
150 struct ltt_kernel_channel
*lkc
;
152 /* Allocate kernel channel */
153 lkc
= trace_create_kernel_channel(chan
, path
);
158 /* Kernel tracer channel creation */
159 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
161 perror("ioctl kernel create channel");
165 /* Setup the channel fd */
167 /* Prevent fd duplication after execlp() */
168 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
170 perror("fcntl session fd");
173 /* Add channel to session */
174 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
175 session
->channel_count
++;
177 DBG("Kernel channel %s created (fd: %d and path: %s)",
178 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
187 * kernel_create_event
189 * Create a kernel event, enable it to the kernel tracer and add it to the
190 * channel event list of the kernel session.
192 int kernel_create_event(struct lttng_event
*ev
, struct ltt_kernel_channel
*channel
)
195 struct ltt_kernel_event
*event
;
197 event
= trace_create_kernel_event(ev
);
202 ret
= kernctl_create_event(channel
->fd
, event
->event
);
204 perror("create event ioctl");
209 /* Prevent fd duplication after execlp() */
210 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
212 perror("fcntl session fd");
215 /* Add event to event list */
216 cds_list_add(&event
->list
, &channel
->events_list
.head
);
217 channel
->event_count
++;
219 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
230 * kernel_disable_channel
232 * Disable a kernel channel.
234 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
238 ret
= kernctl_disable(chan
->fd
);
240 perror("disable chan ioctl");
246 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
255 * kernel_enable_channel
257 * Enable a kernel channel.
259 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
263 ret
= kernctl_enable(chan
->fd
);
265 perror("enable chan ioctl");
271 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
280 * kernel_enable_event
282 * Enable a kernel event.
284 int kernel_enable_event(struct ltt_kernel_event
*event
)
288 ret
= kernctl_enable(event
->fd
);
290 perror("enable event ioctl");
291 if (errno
== EEXIST
) {
298 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
307 * kernel_disable_event
309 * Disable a kernel event.
311 int kernel_disable_event(struct ltt_kernel_event
*event
)
315 ret
= kernctl_disable(event
->fd
);
317 perror("disable event ioctl");
322 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
331 * kernel_open_metadata
333 * Create kernel metadata, open from the kernel tracer and add it to the
336 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
339 struct ltt_kernel_metadata
*lkm
;
341 /* Allocate kernel metadata */
342 lkm
= trace_create_kernel_metadata(path
);
347 /* Kernel tracer metadata creation */
348 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
354 /* Prevent fd duplication after execlp() */
355 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
357 perror("fcntl session fd");
360 session
->metadata
= lkm
;
362 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
371 * kernel_start_session
373 * Start tracing session.
375 int kernel_start_session(struct ltt_kernel_session
*session
)
379 ret
= kernctl_start_session(session
->fd
);
381 perror("ioctl start session");
385 DBG("Kernel session started");
394 * kernel_wait_quiescent
396 * Make a kernel wait to make sure in-flight probe have completed.
398 void kernel_wait_quiescent(int fd
)
402 DBG("Kernel quiescent wait on %d", fd
);
404 ret
= kernctl_wait_quiescent(fd
);
406 perror("wait quiescent ioctl");
407 ERR("Kernel quiescent wait failed");
414 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
418 ret
= kernctl_calibrate(fd
, calibrate
);
420 perror("calibrate ioctl");
429 * kernel_metadata_flush_buffer
431 * Force flush buffer of metadata.
433 int kernel_metadata_flush_buffer(int fd
)
437 ret
= kernctl_buffer_flush(fd
);
439 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
446 * kernel_flush_buffer
448 * Force flush buffer for channel.
450 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
453 struct ltt_kernel_stream
*stream
;
455 DBG("Flush buffer for channel %s", channel
->channel
->name
);
457 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
458 DBG("Flushing channel stream %d", stream
->fd
);
459 ret
= kernctl_buffer_flush(stream
->fd
);
462 ERR("Fail to flush buffer for stream %d (ret: %d)",
471 * kernel_stop_session
473 * Stop tracing session.
475 int kernel_stop_session(struct ltt_kernel_session
*session
)
479 ret
= kernctl_stop_session(session
->fd
);
484 DBG("Kernel session stopped");
493 * kernel_open_channel_stream
495 * Open stream of channel, register it to the kernel tracer and add it
496 * to the stream list of the channel.
498 * Return the number of created stream. Else, a negative value.
500 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
503 struct ltt_kernel_stream
*lks
;
505 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
506 lks
= trace_create_kernel_stream();
513 /* Prevent fd duplication after execlp() */
514 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
516 perror("fcntl session fd");
519 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
520 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
522 perror("asprintf kernel create stream");
526 /* Add stream to channe stream list */
527 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
528 channel
->stream_count
++;
530 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
531 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
534 return channel
->stream_count
;
541 * kernel_open_metadata_stream
543 * Open the metadata stream and set it to the kernel session.
545 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
549 ret
= kernctl_create_stream(session
->metadata
->fd
);
551 perror("kernel create metadata stream");
555 DBG("Kernel metadata stream created (fd: %d)", ret
);
556 session
->metadata_stream_fd
= ret
;
557 /* Prevent fd duplication after execlp() */
558 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
560 perror("fcntl session fd");
570 * Get the event list from the kernel tracer and return the number of elements.
572 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
576 size_t nbmem
, count
= 0;
579 struct lttng_event
*elist
;
581 fd
= kernctl_tracepoint_list(tracer_fd
);
583 perror("kernel tracepoint list");
587 fp
= fdopen(fd
, "r");
589 perror("kernel tracepoint list fdopen");
594 * Init memory size counter
595 * See kernel-ctl.h for explanation of this value
597 nbmem
= KERNEL_EVENT_LIST_SIZE
;
598 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
600 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
602 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
603 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
604 /* Adding the default size again */
605 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
606 elist
= realloc(elist
, nbmem
);
608 perror("realloc list events");
612 strncpy(elist
[count
].name
, event
, strlen(event
));
618 DBG("Kernel list events done (%zu events)", count
);
This page took 0.042586 seconds and 5 git commands to generate.