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 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. However
203 * this FD must not be added to the event list.
205 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
206 DBG2("Kernel event syscall creation success");
211 /* Prevent fd duplication after execlp() */
212 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
214 perror("fcntl session fd");
217 /* Add event to event list */
218 cds_list_add(&event
->list
, &channel
->events_list
.head
);
219 channel
->event_count
++;
221 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
233 * Disable a kernel channel.
235 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
239 ret
= kernctl_disable(chan
->fd
);
241 perror("disable chan ioctl");
247 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
256 * Enable a kernel channel.
258 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
262 ret
= kernctl_enable(chan
->fd
);
263 if (ret
< 0 && errno
!= EEXIST
) {
264 perror("Enable kernel chan");
269 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
278 * Enable a kernel event.
280 int kernel_enable_event(struct ltt_kernel_event
*event
)
284 ret
= kernctl_enable(event
->fd
);
285 if (ret
< 0 && errno
!= EEXIST
) {
286 perror("enable kernel event");
291 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
300 * Disable a kernel event.
302 int kernel_disable_event(struct ltt_kernel_event
*event
)
306 ret
= kernctl_disable(event
->fd
);
307 if (ret
< 0 && errno
!= EEXIST
) {
308 perror("disable kernel event");
313 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
322 * Create kernel metadata, open from the kernel tracer and add it to the
325 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
328 struct ltt_kernel_metadata
*lkm
;
330 /* Allocate kernel metadata */
331 lkm
= trace_kernel_create_metadata(path
);
336 /* Kernel tracer metadata creation */
337 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
343 /* Prevent fd duplication after execlp() */
344 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
346 perror("fcntl session fd");
349 session
->metadata
= lkm
;
351 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
360 * Start tracing session.
362 int kernel_start_session(struct ltt_kernel_session
*session
)
366 ret
= kernctl_start_session(session
->fd
);
368 perror("ioctl start session");
372 DBG("Kernel session started");
381 * Make a kernel wait to make sure in-flight probe have completed.
383 void kernel_wait_quiescent(int fd
)
387 DBG("Kernel quiescent wait on %d", fd
);
389 ret
= kernctl_wait_quiescent(fd
);
391 perror("wait quiescent ioctl");
392 ERR("Kernel quiescent wait failed");
399 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
403 ret
= kernctl_calibrate(fd
, calibrate
);
405 perror("calibrate ioctl");
414 * Force flush buffer of metadata.
416 int kernel_metadata_flush_buffer(int fd
)
420 ret
= kernctl_buffer_flush(fd
);
422 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
429 * Force flush buffer for channel.
431 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
434 struct ltt_kernel_stream
*stream
;
436 DBG("Flush buffer for channel %s", channel
->channel
->name
);
438 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
439 DBG("Flushing channel stream %d", stream
->fd
);
440 ret
= kernctl_buffer_flush(stream
->fd
);
443 ERR("Fail to flush buffer for stream %d (ret: %d)",
452 * Stop tracing session.
454 int kernel_stop_session(struct ltt_kernel_session
*session
)
458 ret
= kernctl_stop_session(session
->fd
);
463 DBG("Kernel session stopped");
472 * Open stream of channel, register it to the kernel tracer and add it
473 * to the stream list of the channel.
475 * Return the number of created stream. Else, a negative value.
477 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
480 struct ltt_kernel_stream
*lks
;
482 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
483 lks
= trace_kernel_create_stream();
490 /* Prevent fd duplication after execlp() */
491 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
493 perror("fcntl session fd");
496 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
497 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
499 perror("asprintf kernel create stream");
503 /* Add stream to channe stream list */
504 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
505 channel
->stream_count
++;
507 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
508 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
511 return channel
->stream_count
;
518 * Open the metadata stream and set it to the kernel session.
520 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
524 ret
= kernctl_create_stream(session
->metadata
->fd
);
526 perror("kernel create metadata stream");
530 DBG("Kernel metadata stream created (fd: %d)", ret
);
531 session
->metadata_stream_fd
= ret
;
532 /* Prevent fd duplication after execlp() */
533 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
535 perror("fcntl session fd");
545 * Get the event list from the kernel tracer and return the number of elements.
547 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
551 size_t nbmem
, count
= 0;
554 struct lttng_event
*elist
;
556 fd
= kernctl_tracepoint_list(tracer_fd
);
558 perror("kernel tracepoint list");
562 fp
= fdopen(fd
, "r");
564 perror("kernel tracepoint list fdopen");
569 * Init memory size counter
570 * See kernel-ctl.h for explanation of this value
572 nbmem
= KERNEL_EVENT_LIST_SIZE
;
573 elist
= malloc(sizeof(struct lttng_event
) * nbmem
);
575 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
577 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
578 nbmem
+ KERNEL_EVENT_LIST_SIZE
);
579 /* Adding the default size again */
580 nbmem
+= KERNEL_EVENT_LIST_SIZE
;
581 elist
= realloc(elist
, nbmem
);
583 perror("realloc list events");
588 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
589 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
594 DBG("Kernel list events done (%zu events)", count
);
596 fclose(fp
); /* closes both fp and fd */
This page took 0.043753 seconds and 4 git commands to generate.