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; either version 2
7 * of the License, or (at your option) any later version.
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
;
127 session
->kern_session_count
++;
129 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * kernel_create_channel
140 * Create a kernel channel, register it to the kernel tracer and add it to the
143 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
, char *path
)
146 struct ltt_kernel_channel
*lkc
;
148 /* Allocate kernel channel */
149 lkc
= trace_create_kernel_channel(chan
, path
);
154 /* Kernel tracer channel creation */
155 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
157 perror("ioctl kernel create channel");
161 /* Setup the channel fd */
163 /* Prevent fd duplication after execlp() */
164 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
166 perror("fcntl session fd");
169 /* Add channel to session */
170 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
171 session
->channel_count
++;
173 DBG("Kernel channel %s created (fd: %d and path: %s)",
174 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
183 * kernel_create_event
185 * Create a kernel event, enable it to the kernel tracer and add it to the
186 * channel event list of the kernel session.
188 int kernel_create_event(struct lttng_event
*ev
, struct ltt_kernel_channel
*channel
)
191 struct ltt_kernel_event
*event
;
193 event
= trace_create_kernel_event(ev
);
198 ret
= kernctl_create_event(channel
->fd
, event
->event
);
200 perror("create event ioctl");
205 /* Prevent fd duplication after execlp() */
206 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
208 perror("fcntl session fd");
211 /* Add event to event list */
212 cds_list_add(&event
->list
, &channel
->events_list
.head
);
213 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
224 * kernel_disable_channel
226 * Disable a kernel channel.
228 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
232 ret
= kernctl_disable(chan
->fd
);
234 perror("disable chan ioctl");
240 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
249 * kernel_enable_channel
251 * Enable a kernel channel.
253 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
257 ret
= kernctl_enable(chan
->fd
);
259 perror("enable chan ioctl");
265 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
274 * kernel_enable_event
276 * Enable a kernel event.
278 int kernel_enable_event(struct ltt_kernel_event
*event
)
282 ret
= kernctl_enable(event
->fd
);
284 perror("enable event ioctl");
289 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
298 * kernel_disable_event
300 * Disable a kernel event.
302 int kernel_disable_event(struct ltt_kernel_event
*event
)
306 ret
= kernctl_disable(event
->fd
);
308 perror("disable event ioctl");
313 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
322 * kernel_open_metadata
324 * Create kernel metadata, open from the kernel tracer and add it to the
327 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
330 struct ltt_kernel_metadata
*lkm
;
332 /* Allocate kernel metadata */
333 lkm
= trace_create_kernel_metadata(path
);
338 /* Kernel tracer metadata creation */
339 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
345 /* Prevent fd duplication after execlp() */
346 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
348 perror("fcntl session fd");
351 session
->metadata
= lkm
;
353 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
362 * kernel_start_session
364 * Start tracing session.
366 int kernel_start_session(struct ltt_kernel_session
*session
)
370 ret
= kernctl_start_session(session
->fd
);
372 perror("ioctl start session");
376 DBG("Kernel session started");
385 * kernel_wait_quiescent
387 * Make a kernel wait to make sure in-flight probe have completed.
389 void kernel_wait_quiescent(int fd
)
393 DBG("Kernel quiescent wait on %d", fd
);
395 ret
= kernctl_wait_quiescent(fd
);
397 perror("wait quiescent ioctl");
398 ERR("Kernel quiescent wait failed");
403 * kernel_metadata_flush_buffer
405 * Force flush buffer of metadata.
407 int kernel_metadata_flush_buffer(int fd
)
411 ret
= kernctl_buffer_flush(fd
);
413 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
420 * kernel_flush_buffer
422 * Force flush buffer for channel.
424 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
427 struct ltt_kernel_stream
*stream
;
429 DBG("Flush buffer for channel %s", channel
->channel
->name
);
431 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
432 DBG("Flushing channel stream %d", stream
->fd
);
433 ret
= kernctl_buffer_flush(stream
->fd
);
436 ERR("Fail to flush buffer for stream %d (ret: %d)",
445 * kernel_stop_session
447 * Stop tracing session.
449 int kernel_stop_session(struct ltt_kernel_session
*session
)
453 ret
= kernctl_stop_session(session
->fd
);
458 DBG("Kernel session stopped");
467 * kernel_open_channel_stream
469 * Open stream of channel, register it to the kernel tracer and add it
470 * to the stream list of the channel.
472 * Return the number of created stream. Else, a negative value.
474 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
477 struct ltt_kernel_stream
*lks
;
479 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
480 lks
= trace_create_kernel_stream();
487 /* Prevent fd duplication after execlp() */
488 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
490 perror("fcntl session fd");
493 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
494 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
496 perror("asprintf kernel create stream");
500 /* Add stream to channe stream list */
501 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
502 channel
->stream_count
++;
504 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
505 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
508 return channel
->stream_count
;
515 * kernel_open_metadata_stream
517 * Open the metadata stream and set it to the kernel session.
519 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
523 ret
= kernctl_create_stream(session
->metadata
->fd
);
525 perror("kernel create metadata stream");
529 DBG("Kernel metadata stream created (fd: %d)", ret
);
530 session
->metadata_stream_fd
= ret
;
531 /* Prevent fd duplication after execlp() */
532 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
534 perror("fcntl session fd");
546 * Get the event list from the kernel tracer and return that list in the CTF
549 ssize_t
kernel_list_events(int tracer_fd
, char **list
)
552 char *buf
, *line
= NULL
;
553 size_t nb
, nbmem
, total
= 0;
557 fd
= kernctl_tracepoint_list(tracer_fd
);
559 perror("kernel tracepoint list");
563 fp
= fdopen(fd
, "r");
565 perror("kernel tracepoint list fdopen");
570 * Init memory size counter
571 * See kernel-ctl.h for explanation of this value
573 nbmem
= KERNEL_EVENT_LIST_SIZE
;
576 while ((size
= getline(&line
, &nb
, fp
)) != -1) {
577 if (total
+ size
> nbmem
) {
578 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
579 total
+ size
+ KERNEL_EVENT_LIST_SIZE
);
580 /* Adding the default size again */
581 nbmem
= total
+ size
+ KERNEL_EVENT_LIST_SIZE
;
582 buf
= realloc(buf
, nbmem
);
584 perror("realloc list events");
588 memcpy(buf
+ total
, line
, size
);
594 DBG("Kernel list events done");
This page took 0.054512 seconds and 4 git commands to generate.