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 perror("add context ioctl");
48 chan
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
49 if (chan
->ctx
== NULL
) {
50 perror("malloc event context");
54 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
63 * kernel_add_event_context
65 * Add context on a kernel event.
67 int kernel_add_event_context(struct ltt_kernel_event
*event
,
68 struct lttng_kernel_context
*ctx
)
72 DBG("Adding context to event %s", event
->event
->name
);
73 ret
= kernctl_add_context(event
->fd
, ctx
);
75 perror("add context ioctl");
79 event
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
80 if (event
->ctx
== NULL
) {
81 perror("malloc event context");
85 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
94 * kernel_create_session
96 * Create a new kernel session, register it to the kernel tracer and add it to
97 * the session daemon session.
99 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
102 struct ltt_kernel_session
*lks
;
104 /* Allocate data structure */
105 lks
= trace_create_kernel_session();
111 /* Kernel tracer session creation */
112 ret
= kernctl_create_session(tracer_fd
);
114 perror("ioctl kernel create session");
119 /* Prevent fd duplication after execlp() */
120 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
122 perror("fcntl session fd");
125 lks
->kconsumer_fds_sent
= 0;
126 session
->kernel_session
= lks
;
128 DBG("Kernel session created (fd: %d)", lks
->fd
);
137 * kernel_create_channel
139 * Create a kernel channel, register it to the kernel tracer and add it to the
142 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
, char *path
)
145 struct ltt_kernel_channel
*lkc
;
147 /* Allocate kernel channel */
148 lkc
= trace_create_kernel_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 * kernel_create_event
184 * Create a kernel event, enable it to the kernel tracer and add it to the
185 * channel event list of the kernel session.
187 int kernel_create_event(struct lttng_event
*ev
, struct ltt_kernel_channel
*channel
)
190 struct ltt_kernel_event
*event
;
192 event
= trace_create_kernel_event(ev
);
197 ret
= kernctl_create_event(channel
->fd
, event
->event
);
199 perror("create event ioctl");
204 /* Prevent fd duplication after execlp() */
205 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
207 perror("fcntl session fd");
210 /* Add event to event list */
211 cds_list_add(&event
->list
, &channel
->events_list
.head
);
212 channel
->event_count
++;
214 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
225 * kernel_disable_channel
227 * Disable a kernel channel.
229 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
233 ret
= kernctl_disable(chan
->fd
);
235 perror("disable chan ioctl");
241 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
250 * kernel_enable_channel
252 * Enable a kernel channel.
254 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
258 ret
= kernctl_enable(chan
->fd
);
260 perror("enable chan ioctl");
266 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
275 * kernel_enable_event
277 * Enable a kernel event.
279 int kernel_enable_event(struct ltt_kernel_event
*event
)
283 ret
= kernctl_enable(event
->fd
);
285 perror("enable event ioctl");
286 if (errno
== EEXIST
) {
293 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
302 * kernel_disable_event
304 * Disable a kernel event.
306 int kernel_disable_event(struct ltt_kernel_event
*event
)
310 ret
= kernctl_disable(event
->fd
);
312 perror("disable event ioctl");
317 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
326 * kernel_open_metadata
328 * Create kernel metadata, open from the kernel tracer and add it to the
331 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
334 struct ltt_kernel_metadata
*lkm
;
336 /* Allocate kernel metadata */
337 lkm
= trace_create_kernel_metadata(path
);
342 /* Kernel tracer metadata creation */
343 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
349 /* Prevent fd duplication after execlp() */
350 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
352 perror("fcntl session fd");
355 session
->metadata
= lkm
;
357 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
366 * kernel_start_session
368 * Start tracing session.
370 int kernel_start_session(struct ltt_kernel_session
*session
)
374 ret
= kernctl_start_session(session
->fd
);
376 perror("ioctl start session");
380 DBG("Kernel session started");
389 * kernel_wait_quiescent
391 * Make a kernel wait to make sure in-flight probe have completed.
393 void kernel_wait_quiescent(int fd
)
397 DBG("Kernel quiescent wait on %d", fd
);
399 ret
= kernctl_wait_quiescent(fd
);
401 perror("wait quiescent ioctl");
402 ERR("Kernel quiescent wait failed");
407 * kernel_metadata_flush_buffer
409 * Force flush buffer of metadata.
411 int kernel_metadata_flush_buffer(int fd
)
415 ret
= kernctl_buffer_flush(fd
);
417 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
424 * kernel_flush_buffer
426 * Force flush buffer for channel.
428 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
431 struct ltt_kernel_stream
*stream
;
433 DBG("Flush buffer for channel %s", channel
->channel
->name
);
435 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
436 DBG("Flushing channel stream %d", stream
->fd
);
437 ret
= kernctl_buffer_flush(stream
->fd
);
440 ERR("Fail to flush buffer for stream %d (ret: %d)",
449 * kernel_stop_session
451 * Stop tracing session.
453 int kernel_stop_session(struct ltt_kernel_session
*session
)
457 ret
= kernctl_stop_session(session
->fd
);
462 DBG("Kernel session stopped");
471 * kernel_open_channel_stream
473 * Open stream of channel, register it to the kernel tracer and add it
474 * to the stream list of the channel.
476 * Return the number of created stream. Else, a negative value.
478 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
481 struct ltt_kernel_stream
*lks
;
483 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
484 lks
= trace_create_kernel_stream();
491 /* Prevent fd duplication after execlp() */
492 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
494 perror("fcntl session fd");
497 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
498 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
500 perror("asprintf kernel create stream");
504 /* Add stream to channe stream list */
505 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
506 channel
->stream_count
++;
508 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
509 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
512 return channel
->stream_count
;
519 * kernel_open_metadata_stream
521 * Open the metadata stream and set it to the kernel session.
523 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
527 ret
= kernctl_create_stream(session
->metadata
->fd
);
529 perror("kernel create metadata stream");
533 DBG("Kernel metadata stream created (fd: %d)", ret
);
534 session
->metadata_stream_fd
= ret
;
535 /* Prevent fd duplication after execlp() */
536 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
538 perror("fcntl session fd");
550 * Get the event list from the kernel tracer and return that list in the CTF
553 ssize_t
kernel_list_events(int tracer_fd
, char **list
)
556 char *buf
, *line
= NULL
;
557 size_t nb
, nbmem
, total
= 0;
561 fd
= kernctl_tracepoint_list(tracer_fd
);
563 perror("kernel tracepoint list");
567 fp
= fdopen(fd
, "r");
569 perror("kernel tracepoint list fdopen");
574 * Init memory size counter
575 * See kernel-ctl.h for explanation of this value
577 nbmem
= KERNEL_EVENT_LIST_SIZE
;
580 while ((size
= getline(&line
, &nb
, fp
)) != -1) {
581 if (total
+ size
> nbmem
) {
582 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
583 total
+ size
+ KERNEL_EVENT_LIST_SIZE
);
584 /* Adding the default size again */
585 nbmem
= total
+ size
+ KERNEL_EVENT_LIST_SIZE
;
586 buf
= realloc(buf
, nbmem
);
588 perror("realloc list events");
592 memcpy(buf
+ total
, line
, size
);
598 DBG("Kernel list events done");
This page took 0.042642 seconds and 5 git commands to generate.