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; either version 2
7 * of the License, or (at your option) any later version.
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.
28 #include "ltt-sessiond.h"
29 #include "libkernelctl.h"
30 #include "kernel-ctl.h"
33 * kernel_add_channel_context
35 * Add context on a kernel channel.
37 int kernel_add_channel_context(struct ltt_kernel_channel
*chan
,
38 struct lttng_kernel_context
*ctx
)
42 DBG("Adding context to channel %s", chan
->channel
->name
);
43 ret
= kernctl_add_context(chan
->fd
, ctx
);
45 perror("add context ioctl");
49 chan
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
50 if (chan
->ctx
== NULL
) {
51 perror("malloc event context");
55 memcpy(chan
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
64 * kernel_add_event_context
66 * Add context on a kernel event.
68 int kernel_add_event_context(struct ltt_kernel_event
*event
,
69 struct lttng_kernel_context
*ctx
)
73 DBG("Adding context to event %s", event
->event
->name
);
74 ret
= kernctl_add_context(event
->fd
, ctx
);
76 perror("add context ioctl");
80 event
->ctx
= malloc(sizeof(struct lttng_kernel_context
));
81 if (event
->ctx
== NULL
) {
82 perror("malloc event context");
86 memcpy(event
->ctx
, ctx
, sizeof(struct lttng_kernel_context
));
95 * kernel_create_session
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_create_kernel_session();
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
->kconsumer_fds_sent
= 0;
127 session
->kernel_session
= lks
;
128 session
->kern_session_count
++;
130 DBG("Kernel session created (fd: %d)", lks
->fd
);
139 * kernel_create_channel
141 * Create a kernel channel, register it to the kernel tracer and add it to the
144 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
)
147 struct ltt_kernel_channel
*lkc
;
149 /* Allocate kernel channel */
150 lkc
= trace_create_kernel_channel(chan
);
155 /* Kernel tracer channel creation */
156 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
158 perror("ioctl kernel create channel");
162 /* Setup the channel fd */
164 /* Prevent fd duplication after execlp() */
165 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
167 perror("fcntl session fd");
170 /* Add channel to session */
171 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
172 session
->channel_count
++;
174 DBG("Kernel channel %s created (fd: %d and path: %s)",
175 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
184 * kernel_create_event
186 * Create a kernel event, enable it to the kernel tracer and add it to the
187 * channel event list of the kernel session.
189 int kernel_create_event(struct lttng_event
*ev
, struct ltt_kernel_channel
*channel
)
192 struct ltt_kernel_event
*event
;
194 event
= trace_create_kernel_event(ev
);
199 ret
= kernctl_create_event(channel
->fd
, event
->event
);
201 perror("create event ioctl");
206 /* Prevent fd duplication after execlp() */
207 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
209 perror("fcntl session fd");
212 /* Add event to event list */
213 cds_list_add(&event
->list
, &channel
->events_list
.head
);
214 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
225 * kernel_disable_channel
227 * Disable a kernel channel.
229 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
233 ret
= kernctl_disable(chan
->fd
);
235 perror("disable chan ioctl");
241 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
250 * kernel_enable_channel
252 * Enable a kernel channel.
254 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
258 ret
= kernctl_enable(chan
->fd
);
260 perror("enable chan ioctl");
266 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
275 * kernel_enable_event
277 * Enable a kernel event.
279 int kernel_enable_event(struct ltt_kernel_event
*event
)
283 ret
= kernctl_enable(event
->fd
);
285 perror("enable event ioctl");
290 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
299 * kernel_disable_event
301 * Disable a kernel event.
303 int kernel_disable_event(struct ltt_kernel_event
*event
)
307 ret
= kernctl_disable(event
->fd
);
309 perror("disable event ioctl");
314 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
323 * kernel_open_metadata
325 * Create kernel metadata, open from the kernel tracer and add it to the
328 int kernel_open_metadata(struct ltt_kernel_session
*session
)
331 struct ltt_kernel_metadata
*lkm
;
333 /* Allocate kernel metadata */
334 lkm
= trace_create_kernel_metadata();
339 /* Kernel tracer metadata creation */
340 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
346 /* Prevent fd duplication after execlp() */
347 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
349 perror("fcntl session fd");
352 session
->metadata
= lkm
;
354 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
363 * kernel_start_session
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 * kernel_wait_quiescent
388 * Make a kernel wait to make sure in-flight probe have completed.
390 void kernel_wait_quiescent(int fd
)
394 DBG("Kernel quiescent wait on %d", fd
);
396 ret
= kernctl_wait_quiescent(fd
);
398 perror("wait quiescent ioctl");
399 ERR("Kernel quiescent wait failed");
404 * kernel_metadata_flush_buffer
406 * Force flush buffer of metadata.
408 int kernel_metadata_flush_buffer(int fd
)
412 ret
= kernctl_buffer_flush(fd
);
414 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
421 * kernel_flush_buffer
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 * kernel_stop_session
448 * Stop tracing session.
450 int kernel_stop_session(struct ltt_kernel_session
*session
)
454 ret
= kernctl_stop_session(session
->fd
);
459 DBG("Kernel session stopped");
468 * kernel_open_channel_stream
470 * Open stream of channel, register it to the kernel tracer and add it
471 * to the stream list of the channel.
473 * Return the number of created stream. Else, a negative value.
475 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
478 struct ltt_kernel_stream
*lks
;
480 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
481 lks
= trace_create_kernel_stream();
488 /* Prevent fd duplication after execlp() */
489 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
491 perror("fcntl session fd");
494 ret
= asprintf(&lks
->pathname
, "%s/%s_%d",
495 channel
->pathname
, channel
->channel
->name
, channel
->stream_count
);
497 perror("asprintf kernel create stream");
501 /* Add stream to channe stream list */
502 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
503 channel
->stream_count
++;
505 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
506 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
509 return channel
->stream_count
;
516 * kernel_open_metadata_stream
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");
547 * Get the event list from the kernel tracer and return that list in the CTF
550 ssize_t
kernel_list_events(int tracer_fd
, char **list
)
553 char *buf
, *line
= NULL
;
554 size_t nb
, nbmem
, total
= 0;
558 fd
= kernctl_tracepoint_list(tracer_fd
);
560 perror("kernel tracepoint list");
564 fp
= fdopen(fd
, "r");
566 perror("kernel tracepoint list fdopen");
571 * Init memory size counter
572 * See kernel-ctl.h for explanation of this value
574 nbmem
= KERNEL_EVENT_LIST_SIZE
;
577 while ((size
= getline(&line
, &nb
, fp
)) != -1) {
578 if (total
+ size
> nbmem
) {
579 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
580 total
+ size
+ KERNEL_EVENT_LIST_SIZE
);
581 /* Adding the default size again */
582 nbmem
= total
+ size
+ KERNEL_EVENT_LIST_SIZE
;
583 buf
= realloc(buf
, nbmem
);
585 perror("realloc list events");
589 memcpy(buf
+ total
, line
, size
);
595 DBG("Kernel list events done");
This page took 0.052129 seconds and 4 git commands to generate.