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 <common/kernel-ctl/kernel-ctl.h>
28 #include <common/lttngerr.h>
29 #include <common/common.h>
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 if (errno
!= EEXIST
) {
45 perror("add context ioctl");
47 /* If EEXIST, we just ignore the error */
53 chan
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
54 if (chan
->ctx
== NULL
) {
55 perror("zmalloc event context");
59 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
68 * Add context on a kernel event.
70 int kernel_add_event_context(struct ltt_kernel_event
*event
,
71 struct lttng_kernel_context
*ctx
)
75 DBG("Adding context to event %s", event
->event
->name
);
76 ret
= kernctl_add_context(event
->fd
, ctx
);
78 perror("add context ioctl");
82 event
->ctx
= zmalloc(sizeof(struct lttng_kernel_context
));
83 if (event
->ctx
== NULL
) {
84 perror("zmalloc event context");
88 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
97 * Create a new kernel session, register it to the kernel tracer and add it to
98 * the session daemon session.
100 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
103 struct ltt_kernel_session
*lks
;
105 /* Allocate data structure */
106 lks
= trace_kernel_create_session(session
->path
);
112 /* Kernel tracer session creation */
113 ret
= kernctl_create_session(tracer_fd
);
115 perror("ioctl kernel create session");
120 /* Prevent fd duplication after execlp() */
121 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
123 perror("fcntl session fd");
126 lks
->consumer_fds_sent
= 0;
127 session
->kernel_session
= lks
;
129 DBG("Kernel session created (fd: %d)", lks
->fd
);
138 * Create a kernel channel, register it to the kernel tracer and add it to the
141 int kernel_create_channel(struct ltt_kernel_session
*session
,
142 struct lttng_channel
*chan
, char *path
)
145 struct ltt_kernel_channel
*lkc
;
147 /* Allocate kernel channel */
148 lkc
= trace_kernel_create_channel(chan
, path
);
153 /* Kernel tracer channel creation */
154 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
156 perror("ioctl kernel create channel");
160 /* Setup the channel fd */
162 /* Prevent fd duplication after execlp() */
163 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
165 perror("fcntl session fd");
168 /* Add channel to session */
169 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
170 session
->channel_count
++;
172 DBG("Kernel channel %s created (fd: %d and path: %s)",
173 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
182 * Create a kernel event, enable it to the kernel tracer and add it to the
183 * channel event list of the kernel session.
185 int kernel_create_event(struct lttng_event
*ev
,
186 struct ltt_kernel_channel
*channel
)
189 struct ltt_kernel_event
*event
;
191 event
= trace_kernel_create_event(ev
);
197 ret
= kernctl_create_event(channel
->fd
, event
->event
);
199 if (errno
!= EEXIST
) {
200 PERROR("create event ioctl");
207 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success. However
208 * this FD must not be added to the event list.
210 if (ret
== 0 && event
->event
->instrumentation
== LTTNG_KERNEL_SYSCALL
) {
211 DBG2("Kernel event syscall creation success");
216 /* Prevent fd duplication after execlp() */
217 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
219 perror("fcntl session fd");
222 /* Add event to event list */
223 cds_list_add(&event
->list
, &channel
->events_list
.head
);
224 channel
->event_count
++;
226 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
238 * Disable a kernel channel.
240 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
244 ret
= kernctl_disable(chan
->fd
);
246 perror("disable chan ioctl");
252 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
261 * Enable a kernel channel.
263 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
267 ret
= kernctl_enable(chan
->fd
);
268 if (ret
< 0 && errno
!= EEXIST
) {
269 perror("Enable kernel chan");
274 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
283 * Enable a kernel event.
285 int kernel_enable_event(struct ltt_kernel_event
*event
)
289 ret
= kernctl_enable(event
->fd
);
290 if (ret
< 0 && errno
!= EEXIST
) {
291 perror("enable kernel event");
296 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
305 * Disable a kernel event.
307 int kernel_disable_event(struct ltt_kernel_event
*event
)
311 ret
= kernctl_disable(event
->fd
);
312 if (ret
< 0 && errno
!= EEXIST
) {
313 perror("disable kernel event");
318 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
327 * Create kernel metadata, open from the kernel tracer and add it to the
330 int kernel_open_metadata(struct ltt_kernel_session
*session
, char *path
)
333 struct ltt_kernel_metadata
*lkm
;
335 /* Allocate kernel metadata */
336 lkm
= trace_kernel_create_metadata(path
);
341 /* Kernel tracer metadata creation */
342 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
348 /* Prevent fd duplication after execlp() */
349 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
351 perror("fcntl session fd");
354 session
->metadata
= lkm
;
356 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
365 * Start tracing session.
367 int kernel_start_session(struct ltt_kernel_session
*session
)
371 ret
= kernctl_start_session(session
->fd
);
373 perror("ioctl start session");
377 DBG("Kernel session started");
386 * Make a kernel wait to make sure in-flight probe have completed.
388 void kernel_wait_quiescent(int fd
)
392 DBG("Kernel quiescent wait on %d", fd
);
394 ret
= kernctl_wait_quiescent(fd
);
396 perror("wait quiescent ioctl");
397 ERR("Kernel quiescent wait failed");
404 int kernel_calibrate(int fd
, struct lttng_kernel_calibrate
*calibrate
)
408 ret
= kernctl_calibrate(fd
, calibrate
);
410 perror("calibrate ioctl");
419 * Force flush buffer of metadata.
421 int kernel_metadata_flush_buffer(int fd
)
425 ret
= kernctl_buffer_flush(fd
);
427 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
434 * Force flush buffer for channel.
436 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
439 struct ltt_kernel_stream
*stream
;
441 DBG("Flush buffer for channel %s", channel
->channel
->name
);
443 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
444 DBG("Flushing channel stream %d", stream
->fd
);
445 ret
= kernctl_buffer_flush(stream
->fd
);
448 ERR("Fail to flush buffer for stream %d (ret: %d)",
457 * Stop tracing session.
459 int kernel_stop_session(struct ltt_kernel_session
*session
)
463 ret
= kernctl_stop_session(session
->fd
);
468 DBG("Kernel session stopped");
477 * Open stream of channel, register it to the kernel tracer and add it
478 * to the stream list of the channel.
480 * Return the number of created stream. Else, a negative value.
482 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
485 struct ltt_kernel_stream
*lks
;
487 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
488 lks
= trace_kernel_create_stream();
495 /* Prevent fd duplication after execlp() */
496 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
498 perror("fcntl session fd");
501 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
502 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
504 perror("asprintf kernel create stream");
508 /* Add stream to channe stream list */
509 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
510 channel
->stream_count
++;
512 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
513 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
516 return channel
->stream_count
;
523 * Open the metadata stream and set it to the kernel session.
525 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
529 ret
= kernctl_create_stream(session
->metadata
->fd
);
531 perror("kernel create metadata stream");
535 DBG("Kernel metadata stream created (fd: %d)", ret
);
536 session
->metadata_stream_fd
= ret
;
537 /* Prevent fd duplication after execlp() */
538 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
540 perror("fcntl session fd");
550 * Get the event list from the kernel tracer and return the number of elements.
552 ssize_t
kernel_list_events(int tracer_fd
, struct lttng_event
**events
)
556 size_t nbmem
, count
= 0;
559 struct lttng_event
*elist
;
561 fd
= kernctl_tracepoint_list(tracer_fd
);
563 perror("kernel tracepoint list");
567 fp
= fdopen(fd
, "r");
569 perror("kernel tracepoint list fdopen");
574 * Init memory size counter
575 * See kernel-ctl.h for explanation of this value
577 nbmem
= KERNEL_EVENT_INIT_LIST_SIZE
;
578 elist
= zmalloc(sizeof(struct lttng_event
) * nbmem
);
580 while ((size
= fscanf(fp
, "event { name = %m[^;]; };%n\n", &event
, &pos
)) == 1) {
581 if (count
>= nbmem
) {
582 DBG("Reallocating event list from %zu to %zu bytes", nbmem
,
584 /* Double the size */
586 elist
= realloc(elist
, nbmem
* sizeof(struct lttng_event
));
588 perror("realloc list events");
593 strncpy(elist
[count
].name
, event
, LTTNG_SYMBOL_NAME_LEN
);
594 elist
[count
].name
[LTTNG_SYMBOL_NAME_LEN
- 1] = '\0';
595 elist
[count
].enabled
= -1;
600 DBG("Kernel list events done (%zu events)", count
);
602 fclose(fp
); /* closes both fp and fd */