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");
412 * kernel_metadata_flush_buffer
414 * Force flush buffer of metadata.
416 int kernel_metadata_flush_buffer(int fd
)
420 ret
= kernctl_buffer_flush(fd
);
422 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
429 * kernel_flush_buffer
431 * Force flush buffer for channel.
433 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
436 struct ltt_kernel_stream
*stream
;
438 DBG("Flush buffer for channel %s", channel
->channel
->name
);
440 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
441 DBG("Flushing channel stream %d", stream
->fd
);
442 ret
= kernctl_buffer_flush(stream
->fd
);
445 ERR("Fail to flush buffer for stream %d (ret: %d)",
454 * kernel_stop_session
456 * Stop tracing session.
458 int kernel_stop_session(struct ltt_kernel_session
*session
)
462 ret
= kernctl_stop_session(session
->fd
);
467 DBG("Kernel session stopped");
476 * kernel_open_channel_stream
478 * Open stream of channel, register it to the kernel tracer and add it
479 * to the stream list of the channel.
481 * Return the number of created stream. Else, a negative value.
483 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
486 struct ltt_kernel_stream
*lks
;
488 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
489 lks
= trace_create_kernel_stream();
496 /* Prevent fd duplication after execlp() */
497 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
499 perror("fcntl session fd");
502 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
503 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
505 perror("asprintf kernel create stream");
509 /* Add stream to channe stream list */
510 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
511 channel
->stream_count
++;
513 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
514 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
517 return channel
->stream_count
;
524 * kernel_open_metadata_stream
526 * Open the metadata stream and set it to the kernel session.
528 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
532 ret
= kernctl_create_stream(session
->metadata
->fd
);
534 perror("kernel create metadata stream");
538 DBG("Kernel metadata stream created (fd: %d)", ret
);
539 session
->metadata_stream_fd
= ret
;
540 /* Prevent fd duplication after execlp() */
541 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
543 perror("fcntl session fd");
553 * Get the event list from the kernel tracer and return the number of elements.
555 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
559 size_t nbmem
, count
= 0;
562 struct lttng_event
*elist
;
564 fd
= kernctl_tracepoint_list(tracer_fd
);
566 perror("kernel tracepoint list");
570 fp
= fdopen(fd
, "r");
572 perror("kernel tracepoint list fdopen");
577 * Init memory size counter
578 * See kernel-ctl.h for explanation of this value
580 nbmem
= KERNEL_EVENT_LIST_SIZE
;
581 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
583 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
585 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
586 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
587 /* Adding the default size again */
588 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
589 elist
= realloc(elist
, nbmem
);
591 perror("realloc list events");
595 strncpy(elist
[count
].name
, event
, strlen(event
));
601 DBG("Kernel list events done (%ld events)", count
);
This page took 0.043994 seconds and 5 git commands to generate.