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.
27 #include <lttng-kernel-ctl.h>
30 #include "kernel-ctl.h"
33 * Add context on a kernel channel.
35 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
36 struct lttng_kernel_context
*ctx
)
40 DBG("Adding context to channel %s", chan
->channel
->name
);
41 ret
= kernctl_add_context(chan
->fd
, ctx
);
43 if (errno
!= EEXIST
) {
44 perror("add context ioctl");
46 /* If EEXIST, we just ignore the error */
52 chan
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
53 if (chan
->ctx
== NULL
) {
54 perror("malloc event context");
58 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
67 * Add context on a kernel event.
69 int kernel_add_event_context(struct ltt_kernel_event
*event
,
70 struct lttng_kernel_context
*ctx
)
74 DBG("Adding context to event %s", event
->event
->name
);
75 ret
= kernctl_add_context(event
->fd
, ctx
);
77 perror("add context ioctl");
81 event
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
82 if (event
->ctx
== NULL
) {
83 perror("malloc event context");
87 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
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 * Create a kernel channel, register it to the kernel tracer and add it to the
140 int kernel_create_channel(struct ltt_kernel_session
*session
,
141 struct lttng_channel
*chan
, char *path
)
144 struct ltt_kernel_channel
*lkc
;
146 /* Allocate kernel channel */
147 lkc
= trace_create_kernel_channel(chan
, path
);
152 /* Kernel tracer channel creation */
153 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
155 perror("ioctl kernel create channel");
159 /* Setup the channel fd */
161 /* Prevent fd duplication after execlp() */
162 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
164 perror("fcntl session fd");
167 /* Add channel to session */
168 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
169 session
->channel_count
++;
171 DBG("Kernel channel %s created (fd: %d and path: %s)",
172 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
181 * Create a kernel event, enable it to the kernel tracer and add it to the
182 * channel event list of the kernel session.
184 int kernel_create_event(struct lttng_event
*ev
,
185 struct ltt_kernel_channel
*channel
)
188 struct ltt_kernel_event
*event
;
190 event
= trace_create_kernel_event(ev
);
195 ret
= kernctl_create_event(channel
->fd
, event
->event
);
197 perror("create event ioctl");
202 /* Prevent fd duplication after execlp() */
203 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
205 perror("fcntl session fd");
208 /* Add event to event list */
209 cds_list_add(&event
->list
, &channel
->events_list
.head
);
210 channel
->event_count
++;
212 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
223 * Disable a kernel channel.
225 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
229 ret
= kernctl_disable(chan
->fd
);
231 perror("disable chan ioctl");
237 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
246 * Enable a kernel channel.
248 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
252 ret
= kernctl_enable(chan
->fd
);
254 perror("enable chan ioctl");
260 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
269 * Enable a kernel event.
271 int kernel_enable_event(struct ltt_kernel_event
*event
)
275 ret
= kernctl_enable(event
->fd
);
277 perror("enable event ioctl");
278 if (errno
== EEXIST
) {
285 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
294 * Disable a kernel event.
296 int kernel_disable_event(struct ltt_kernel_event
*event
)
300 ret
= kernctl_disable(event
->fd
);
302 perror("disable event ioctl");
307 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
316 * Create kernel metadata, open from the kernel tracer and add it to the
319 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
322 struct ltt_kernel_metadata
*lkm
;
324 /* Allocate kernel metadata */
325 lkm
= trace_create_kernel_metadata(path
);
330 /* Kernel tracer metadata creation */
331 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
337 /* Prevent fd duplication after execlp() */
338 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
340 perror("fcntl session fd");
343 session
->metadata
= lkm
;
345 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
354 * Start tracing session.
356 int kernel_start_session(struct ltt_kernel_session
*session
)
360 ret
= kernctl_start_session(session
->fd
);
362 perror("ioctl start session");
366 DBG("Kernel session started");
375 * Make a kernel wait to make sure in-flight probe have completed.
377 void kernel_wait_quiescent(int fd
)
381 DBG("Kernel quiescent wait on %d", fd
);
383 ret
= kernctl_wait_quiescent(fd
);
385 perror("wait quiescent ioctl");
386 ERR("Kernel quiescent wait failed");
393 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
397 ret
= kernctl_calibrate(fd
, calibrate
);
399 perror("calibrate ioctl");
408 * Force flush buffer of metadata.
410 int kernel_metadata_flush_buffer(int fd
)
414 ret
= kernctl_buffer_flush(fd
);
416 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
423 * Force flush buffer for channel.
425 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
428 struct ltt_kernel_stream
*stream
;
430 DBG("Flush buffer for channel %s", channel
->channel
->name
);
432 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
433 DBG("Flushing channel stream %d", stream
->fd
);
434 ret
= kernctl_buffer_flush(stream
->fd
);
437 ERR("Fail to flush buffer for stream %d (ret: %d)",
446 * Stop tracing session.
448 int kernel_stop_session(struct ltt_kernel_session
*session
)
452 ret
= kernctl_stop_session(session
->fd
);
457 DBG("Kernel session stopped");
466 * Open stream of channel, register it to the kernel tracer and add it
467 * to the stream list of the channel.
469 * Return the number of created stream. Else, a negative value.
471 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
474 struct ltt_kernel_stream
*lks
;
476 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
477 lks
= trace_create_kernel_stream();
484 /* Prevent fd duplication after execlp() */
485 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
487 perror("fcntl session fd");
490 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
491 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
493 perror("asprintf kernel create stream");
497 /* Add stream to channe stream list */
498 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
499 channel
->stream_count
++;
501 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
502 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
505 return channel
->stream_count
;
512 * Open the metadata stream and set it to the kernel session.
514 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
518 ret
= kernctl_create_stream(session
->metadata
->fd
);
520 perror("kernel create metadata stream");
524 DBG("Kernel metadata stream created (fd: %d)", ret
);
525 session
->metadata_stream_fd
= ret
;
526 /* Prevent fd duplication after execlp() */
527 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
529 perror("fcntl session fd");
539 * Get the event list from the kernel tracer and return the number of elements.
541 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
545 size_t nbmem
, count
= 0;
548 struct lttng_event
*elist
;
550 fd
= kernctl_tracepoint_list(tracer_fd
);
552 perror("kernel tracepoint list");
556 fp
= fdopen(fd
, "r");
558 perror("kernel tracepoint list fdopen");
563 * Init memory size counter
564 * See kernel-ctl.h for explanation of this value
566 nbmem
= KERNEL_EVENT_LIST_SIZE
;
567 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
569 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
571 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
572 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
573 /* Adding the default size again */
574 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
575 elist
= realloc(elist
, nbmem
);
577 perror("realloc list events");
582 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
583 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
588 DBG("Kernel list events done (%zu events)", count
);
590 fclose(fp
); /* closes both fp and fd */
This page took 0.054357 seconds and 5 git commands to generate.