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_kernel_create_session(session
->path
);
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_kernel_create_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_kernel_create_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
);
253 if (ret
< 0 && errno
!= EEXIST
) {
254 perror("Enable kernel chan");
259 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
268 * Enable a kernel event.
270 int kernel_enable_event(struct ltt_kernel_event
*event
)
274 ret
= kernctl_enable(event
->fd
);
275 if (ret
< 0 && errno
!= EEXIST
) {
276 perror("enable kernel event");
281 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
290 * Disable a kernel event.
292 int kernel_disable_event(struct ltt_kernel_event
*event
)
296 ret
= kernctl_disable(event
->fd
);
297 if (ret
< 0 && errno
!= EEXIST
) {
298 perror("disable kernel event");
303 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
312 * Create kernel metadata, open from the kernel tracer and add it to the
315 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
318 struct ltt_kernel_metadata
*lkm
;
320 /* Allocate kernel metadata */
321 lkm
= trace_kernel_create_metadata(path
);
326 /* Kernel tracer metadata creation */
327 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
333 /* Prevent fd duplication after execlp() */
334 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
336 perror("fcntl session fd");
339 session
->metadata
= lkm
;
341 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
350 * Start tracing session.
352 int kernel_start_session(struct ltt_kernel_session
*session
)
356 ret
= kernctl_start_session(session
->fd
);
358 perror("ioctl start session");
362 DBG("Kernel session started");
371 * Make a kernel wait to make sure in-flight probe have completed.
373 void kernel_wait_quiescent(int fd
)
377 DBG("Kernel quiescent wait on %d", fd
);
379 ret
= kernctl_wait_quiescent(fd
);
381 perror("wait quiescent ioctl");
382 ERR("Kernel quiescent wait failed");
389 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
393 ret
= kernctl_calibrate(fd
, calibrate
);
395 perror("calibrate ioctl");
404 * Force flush buffer of metadata.
406 int kernel_metadata_flush_buffer(int fd
)
410 ret
= kernctl_buffer_flush(fd
);
412 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
419 * Force flush buffer for channel.
421 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
424 struct ltt_kernel_stream
*stream
;
426 DBG("Flush buffer for channel %s", channel
->channel
->name
);
428 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
429 DBG("Flushing channel stream %d", stream
->fd
);
430 ret
= kernctl_buffer_flush(stream
->fd
);
433 ERR("Fail to flush buffer for stream %d (ret: %d)",
442 * Stop tracing session.
444 int kernel_stop_session(struct ltt_kernel_session
*session
)
448 ret
= kernctl_stop_session(session
->fd
);
453 DBG("Kernel session stopped");
462 * Open stream of channel, register it to the kernel tracer and add it
463 * to the stream list of the channel.
465 * Return the number of created stream. Else, a negative value.
467 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
470 struct ltt_kernel_stream
*lks
;
472 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
473 lks
= trace_kernel_create_stream();
480 /* Prevent fd duplication after execlp() */
481 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
483 perror("fcntl session fd");
486 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
487 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
489 perror("asprintf kernel create stream");
493 /* Add stream to channe stream list */
494 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
495 channel
->stream_count
++;
497 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
498 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
501 return channel
->stream_count
;
508 * Open the metadata stream and set it to the kernel session.
510 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
514 ret
= kernctl_create_stream(session
->metadata
->fd
);
516 perror("kernel create metadata stream");
520 DBG("Kernel metadata stream created (fd: %d)", ret
);
521 session
->metadata_stream_fd
= ret
;
522 /* Prevent fd duplication after execlp() */
523 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
525 perror("fcntl session fd");
535 * Get the event list from the kernel tracer and return the number of elements.
537 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
541 size_t nbmem
, count
= 0;
544 struct lttng_event
*elist
;
546 fd
= kernctl_tracepoint_list(tracer_fd
);
548 perror("kernel tracepoint list");
552 fp
= fdopen(fd
, "r");
554 perror("kernel tracepoint list fdopen");
559 * Init memory size counter
560 * See kernel-ctl.h for explanation of this value
562 nbmem
= KERNEL_EVENT_LIST_SIZE
;
563 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
565 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
567 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
568 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
569 /* Adding the default size again */
570 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
571 elist
= realloc(elist
, nbmem
);
573 perror("realloc list events");
578 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
579 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
584 DBG("Kernel list events done (%zu events)", count
);
586 fclose(fp
); /* closes both fp and fd */
This page took 0.04311 seconds and 5 git commands to generate.