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
->consumer_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
);
196 ret
= kernctl_create_event(channel
->fd
, event
->event
);
198 if (errno
!= EEXIST
) {
199 PERROR("create event ioctl");
206 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. However
207 * this FD must not be added to the event list.
209 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
210 DBG2("Kernel event syscall creation success");
215 /* Prevent fd duplication after execlp() */
216 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
218 perror("fcntl session fd");
221 /* Add event to event list */
222 cds_list_add(&event
->list
, &channel
->events_list
.head
);
223 channel
->event_count
++;
225 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
237 * Disable a kernel channel.
239 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
243 ret
= kernctl_disable(chan
->fd
);
245 perror("disable chan ioctl");
251 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
260 * Enable a kernel channel.
262 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
266 ret
= kernctl_enable(chan
->fd
);
267 if (ret
< 0 && errno
!= EEXIST
) {
268 perror("Enable kernel chan");
273 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
282 * Enable a kernel event.
284 int kernel_enable_event(struct ltt_kernel_event
*event
)
288 ret
= kernctl_enable(event
->fd
);
289 if (ret
< 0 && errno
!= EEXIST
) {
290 perror("enable kernel event");
295 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
304 * Disable a kernel event.
306 int kernel_disable_event(struct ltt_kernel_event
*event
)
310 ret
= kernctl_disable(event
->fd
);
311 if (ret
< 0 && errno
!= EEXIST
) {
312 perror("disable kernel event");
317 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
326 * Create kernel metadata, open from the kernel tracer and add it to the
329 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
332 struct ltt_kernel_metadata
*lkm
;
334 /* Allocate kernel metadata */
335 lkm
= trace_kernel_create_metadata(path
);
340 /* Kernel tracer metadata creation */
341 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
347 /* Prevent fd duplication after execlp() */
348 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
350 perror("fcntl session fd");
353 session
->metadata
= lkm
;
355 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
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 * Make a kernel wait to make sure in-flight probe have completed.
387 void kernel_wait_quiescent(int fd
)
391 DBG("Kernel quiescent wait on %d", fd
);
393 ret
= kernctl_wait_quiescent(fd
);
395 perror("wait quiescent ioctl");
396 ERR("Kernel quiescent wait failed");
403 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
407 ret
= kernctl_calibrate(fd
, calibrate
);
409 perror("calibrate ioctl");
418 * Force flush buffer of metadata.
420 int kernel_metadata_flush_buffer(int fd
)
424 ret
= kernctl_buffer_flush(fd
);
426 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
433 * Force flush buffer for channel.
435 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
438 struct ltt_kernel_stream
*stream
;
440 DBG("Flush buffer for channel %s", channel
->channel
->name
);
442 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
443 DBG("Flushing channel stream %d", stream
->fd
);
444 ret
= kernctl_buffer_flush(stream
->fd
);
447 ERR("Fail to flush buffer for stream %d (ret: %d)",
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 * Open stream of channel, register it to the kernel tracer and add it
477 * to the stream list of the channel.
479 * Return the number of created stream. Else, a negative value.
481 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
484 struct ltt_kernel_stream
*lks
;
486 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
487 lks
= trace_kernel_create_stream();
494 /* Prevent fd duplication after execlp() */
495 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
497 perror("fcntl session fd");
500 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
501 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
503 perror("asprintf kernel create stream");
507 /* Add stream to channe stream list */
508 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
509 channel
->stream_count
++;
511 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
512 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
515 return channel
->stream_count
;
522 * Open the metadata stream and set it to the kernel session.
524 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
528 ret
= kernctl_create_stream(session
->metadata
->fd
);
530 perror("kernel create metadata stream");
534 DBG("Kernel metadata stream created (fd: %d)", ret
);
535 session
->metadata_stream_fd
= ret
;
536 /* Prevent fd duplication after execlp() */
537 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
539 perror("fcntl session fd");
549 * Get the event list from the kernel tracer and return the number of elements.
551 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
555 size_t nbmem
, count
= 0;
558 struct lttng_event
*elist
;
560 fd
= kernctl_tracepoint_list(tracer_fd
);
562 perror("kernel tracepoint list");
566 fp
= fdopen(fd
, "r");
568 perror("kernel tracepoint list fdopen");
573 * Init memory size counter
574 * See kernel-ctl.h for explanation of this value
576 nbmem
= KERNEL_EVENT_LIST_SIZE
;
577 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
579 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
581 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
582 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
583 /* Adding the default size again */
584 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
585 elist
= realloc(elist
, nbmem
);
587 perror("realloc list events");
592 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
593 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
598 DBG("Kernel list events done (%zu events)", count
);
600 fclose(fp
); /* closes both fp and fd */
This page took 0.043009 seconds and 5 git commands to generate.