e42ab872e487890b8c01072aa1784bb10b9ffb19
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_create_session
35 * Create a new kernel session, register it to the kernel tracer and add it to
36 * the session daemon session.
38 int kernel_create_session(struct ltt_session
*session
, int tracer_fd
)
41 struct ltt_kernel_session
*lks
;
43 /* Allocate data structure */
44 lks
= trace_create_kernel_session();
50 /* Kernel tracer session creation */
51 ret
= kernctl_create_session(tracer_fd
);
53 perror("ioctl kernel create session");
58 /* Prevent fd duplication after execlp() */
59 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
61 perror("fcntl session fd");
64 lks
->kconsumer_fds_sent
= 0;
65 session
->kernel_session
= lks
;
66 session
->kern_session_count
++;
68 DBG("Kernel session created (fd: %d)", lks
->fd
);
77 * kernel_create_channel
79 * Create a kernel channel, register it to the kernel tracer and add it to the
82 int kernel_create_channel(struct ltt_kernel_session
*session
, struct lttng_channel
*chan
)
85 struct ltt_kernel_channel
*lkc
;
87 /* Allocate kernel channel */
88 lkc
= trace_create_kernel_channel(chan
);
93 /* Kernel tracer channel creation */
94 ret
= kernctl_create_channel(session
->fd
, &lkc
->channel
->attr
);
96 perror("ioctl kernel create channel");
100 /* Setup the channel fd */
102 /* Prevent fd duplication after execlp() */
103 ret
= fcntl(lkc
->fd
, F_SETFD
, FD_CLOEXEC
);
105 perror("fcntl session fd");
108 /* Add channel to session */
109 cds_list_add(&lkc
->list
, &session
->channel_list
.head
);
110 session
->channel_count
++;
112 DBG("Kernel channel %s created (fd: %d and path: %s)",
113 lkc
->channel
->name
, lkc
->fd
, lkc
->pathname
);
122 * kernel_create_event
124 * Create a kernel event, enable it to the kernel tracer and add it to the
125 * channel event list of the kernel session.
127 int kernel_create_event(struct lttng_event
*ev
, struct ltt_kernel_channel
*channel
)
130 struct ltt_kernel_event
*event
;
132 event
= trace_create_kernel_event(ev
);
137 ret
= kernctl_create_event(channel
->fd
, event
->event
);
139 perror("create event ioctl");
144 /* Prevent fd duplication after execlp() */
145 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
147 perror("fcntl session fd");
150 /* Add event to event list */
151 cds_list_add(&event
->list
, &channel
->events_list
.head
);
152 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
163 * kernel_disable_channel
165 * Disable a kernel channel.
167 int kernel_disable_channel(struct ltt_kernel_channel
*chan
)
171 ret
= kernctl_disable(chan
->fd
);
173 perror("disable chan ioctl");
179 DBG("Kernel channel %s disabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
188 * kernel_enable_channel
190 * Enable a kernel channel.
192 int kernel_enable_channel(struct ltt_kernel_channel
*chan
)
196 ret
= kernctl_enable(chan
->fd
);
198 perror("enable chan ioctl");
204 DBG("Kernel channel %s enabled (fd: %d)", chan
->channel
->name
, chan
->fd
);
213 * kernel_enable_event
215 * Enable a kernel event.
217 int kernel_enable_event(struct ltt_kernel_event
*event
)
221 ret
= kernctl_enable(event
->fd
);
223 perror("enable event ioctl");
228 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
237 * kernel_disable_event
239 * Disable a kernel event.
241 int kernel_disable_event(struct ltt_kernel_event
*event
)
245 ret
= kernctl_disable(event
->fd
);
247 perror("disable event ioctl");
252 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
261 * kernel_open_metadata
263 * Create kernel metadata, open from the kernel tracer and add it to the
266 int kernel_open_metadata(struct ltt_kernel_session
*session
)
269 struct ltt_kernel_metadata
*lkm
;
271 /* Allocate kernel metadata */
272 lkm
= trace_create_kernel_metadata();
277 /* Kernel tracer metadata creation */
278 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
284 /* Prevent fd duplication after execlp() */
285 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
287 perror("fcntl session fd");
290 session
->metadata
= lkm
;
292 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
301 * kernel_start_session
303 * Start tracing session.
305 int kernel_start_session(struct ltt_kernel_session
*session
)
309 ret
= kernctl_start_session(session
->fd
);
311 perror("ioctl start session");
315 DBG("Kernel session started");
324 * kernel_wait_quiescent
326 * Make a kernel wait to make sure in-flight probe have completed.
328 void kernel_wait_quiescent(int fd
)
332 DBG("Kernel quiescent wait on %d", fd
);
334 ret
= kernctl_wait_quiescent(fd
);
336 perror("wait quiescent ioctl");
337 ERR("Kernel quiescent wait failed");
342 * kernel_metadata_flush_buffer
344 * Force flush buffer of metadata.
346 int kernel_metadata_flush_buffer(int fd
)
350 ret
= kernctl_buffer_flush(fd
);
352 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
359 * kernel_flush_buffer
361 * Force flush buffer for channel.
363 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
366 struct ltt_kernel_stream
*stream
;
368 DBG("Flush buffer for channel %s", channel
->channel
->name
);
370 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
371 DBG("Flushing channel stream %d", stream
->fd
);
372 ret
= kernctl_buffer_flush(stream
->fd
);
375 ERR("Fail to flush buffer for stream %d (ret: %d)",
384 * kernel_stop_session
386 * Stop tracing session.
388 int kernel_stop_session(struct ltt_kernel_session
*session
)
392 ret
= kernctl_stop_session(session
->fd
);
397 DBG("Kernel session stopped");
406 * kernel_open_channel_stream
408 * Open stream of channel, register it to the kernel tracer and add it
409 * to the stream list of the channel.
411 * Return the number of created stream. Else, a negative value.
413 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
416 struct ltt_kernel_stream
*lks
;
418 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
419 lks
= trace_create_kernel_stream();
426 /* Prevent fd duplication after execlp() */
427 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
429 perror("fcntl session fd");
432 ret
= asprintf(&lks
->pathname
, "%s/trace_%d",
433 channel
->pathname
, channel
->stream_count
);
435 perror("asprintf kernel create stream");
439 /* Add stream to channe stream list */
440 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
441 channel
->stream_count
++;
443 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
444 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
447 return channel
->stream_count
;
454 * kernel_open_metadata_stream
456 * Open the metadata stream and set it to the kernel session.
458 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
462 ret
= kernctl_create_stream(session
->metadata
->fd
);
464 perror("kernel create metadata stream");
468 DBG("Kernel metadata stream created (fd: %d)", ret
);
469 session
->metadata_stream_fd
= ret
;
470 /* Prevent fd duplication after execlp() */
471 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
473 perror("fcntl session fd");
485 * Get the event list from the kernel tracer and return that list in the CTF
488 ssize_t
kernel_list_events(int tracer_fd
, char **list
)
491 char *buf
, *line
= NULL
;
492 size_t nb
, nbmem
, total
= 0;
496 fd
= kernctl_tracepoint_list(tracer_fd
);
498 perror("kernel tracepoint list");
502 fp
= fdopen(fd
, "r");
504 perror("kernel tracepoint list fdopen");
509 * Init memory size counter
510 * See kernel-ctl.h for explanation of this value
512 nbmem
= KERNEL_EVENT_LIST_SIZE
;
515 while ((size
= getline(&line
, &nb
, fp
)) != -1) {
516 if (total
+ size
> nbmem
) {
517 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
518 total
+ size
+ KERNEL_EVENT_LIST_SIZE
);
519 /* Adding the default size again */
520 nbmem
= total
+ size
+ KERNEL_EVENT_LIST_SIZE
;
521 buf
= realloc(buf
, nbmem
);
523 perror("realloc list events");
527 memcpy(buf
+ total
, line
, size
);
533 DBG("Kernel list events done");
This page took 0.046111 seconds and 5 git commands to generate.