16b85bdb5bcb5b6793450b0f2794023541fa2466
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");
145 /* Prevent fd duplication after execlp() */
146 ret
= fcntl(event
->fd
, F_SETFD
, FD_CLOEXEC
);
148 perror("fcntl session fd");
151 /* Add event to event list */
152 cds_list_add(&event
->list
, &channel
->events_list
.head
);
153 DBG("Event %s created (fd: %d)", ev
->name
, event
->fd
);
164 * kernel_enable_event
166 * Enable a kernel event.
168 int kernel_enable_event(struct ltt_kernel_event
*event
)
172 ret
= kernctl_enable(event
->fd
);
174 perror("enable event ioctl");
179 DBG("Kernel event %s enabled (fd: %d)", event
->event
->name
, event
->fd
);
188 * kernel_disable_event
190 * Disable a kernel event.
192 int kernel_disable_event(struct ltt_kernel_event
*event
)
196 ret
= kernctl_disable(event
->fd
);
198 perror("disable event ioctl");
203 DBG("Kernel event %s disabled (fd: %d)", event
->event
->name
, event
->fd
);
212 * kernel_open_metadata
214 * Create kernel metadata, open from the kernel tracer and add it to the
217 int kernel_open_metadata(struct ltt_kernel_session
*session
)
220 struct ltt_kernel_metadata
*lkm
;
222 /* Allocate kernel metadata */
223 lkm
= trace_create_kernel_metadata();
228 /* Kernel tracer metadata creation */
229 ret
= kernctl_open_metadata(session
->fd
, &lkm
->conf
->attr
);
235 /* Prevent fd duplication after execlp() */
236 ret
= fcntl(lkm
->fd
, F_SETFD
, FD_CLOEXEC
);
238 perror("fcntl session fd");
241 session
->metadata
= lkm
;
243 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm
->fd
, lkm
->pathname
);
252 * kernel_start_session
254 * Start tracing session.
256 int kernel_start_session(struct ltt_kernel_session
*session
)
260 ret
= kernctl_start_session(session
->fd
);
262 perror("ioctl start session");
266 DBG("Kernel session started");
275 * kernel_wait_quiescent
277 * Make a kernel wait to make sure in-flight probe have completed.
279 void kernel_wait_quiescent(int fd
)
283 DBG("Kernel quiescent wait on %d", fd
);
285 ret
= kernctl_wait_quiescent(fd
);
287 perror("wait quiescent ioctl");
288 ERR("Kernel quiescent wait failed");
293 * kernel_metadata_flush_buffer
295 * Force flush buffer of metadata.
297 int kernel_metadata_flush_buffer(int fd
)
301 ret
= kernctl_buffer_flush(fd
);
303 ERR("Fail to flush metadata buffers %d (ret: %d", fd
, ret
);
310 * kernel_flush_buffer
312 * Force flush buffer for channel.
314 int kernel_flush_buffer(struct ltt_kernel_channel
*channel
)
317 struct ltt_kernel_stream
*stream
;
319 DBG("Flush buffer for channel %s", channel
->channel
->name
);
321 cds_list_for_each_entry(stream
, &channel
->stream_list
.head
, list
) {
322 DBG("Flushing channel stream %d", stream
->fd
);
323 ret
= kernctl_buffer_flush(stream
->fd
);
326 ERR("Fail to flush buffer for stream %d (ret: %d)",
335 * kernel_stop_session
337 * Stop tracing session.
339 int kernel_stop_session(struct ltt_kernel_session
*session
)
343 ret
= kernctl_stop_session(session
->fd
);
348 DBG("Kernel session stopped");
357 * kernel_open_channel_stream
359 * Open stream of channel, register it to the kernel tracer and add it
360 * to the stream list of the channel.
362 * Return the number of created stream. Else, a negative value.
364 int kernel_open_channel_stream(struct ltt_kernel_channel
*channel
)
367 struct ltt_kernel_stream
*lks
;
369 while ((ret
= kernctl_create_stream(channel
->fd
)) > 0) {
370 lks
= trace_create_kernel_stream();
377 /* Prevent fd duplication after execlp() */
378 ret
= fcntl(lks
->fd
, F_SETFD
, FD_CLOEXEC
);
380 perror("fcntl session fd");
383 ret
= asprintf(&lks
->pathname
, "%s/trace_%d",
384 channel
->pathname
, channel
->stream_count
);
386 perror("asprintf kernel create stream");
390 /* Add stream to channe stream list */
391 cds_list_add(&lks
->list
, &channel
->stream_list
.head
);
392 channel
->stream_count
++;
394 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
395 channel
->stream_count
, lks
->fd
, lks
->state
, lks
->pathname
);
398 return channel
->stream_count
;
405 * kernel_open_metadata_stream
407 * Open the metadata stream and set it to the kernel session.
409 int kernel_open_metadata_stream(struct ltt_kernel_session
*session
)
413 ret
= kernctl_create_stream(session
->metadata
->fd
);
415 perror("kernel create metadata stream");
419 DBG("Kernel metadata stream created (fd: %d)", ret
);
420 session
->metadata_stream_fd
= ret
;
421 /* Prevent fd duplication after execlp() */
422 ret
= fcntl(session
->metadata_stream_fd
, F_SETFD
, FD_CLOEXEC
);
424 perror("fcntl session fd");
436 * Get the event list from the kernel tracer and return that list in the CTF
439 ssize_t
kernel_list_events(int tracer_fd
, char **list
)
442 char *buf
, *line
= NULL
;
443 size_t nb
, nbmem
, total
= 0;
447 fd
= kernctl_tracepoint_list(tracer_fd
);
449 perror("kernel tracepoint list");
453 fp
= fdopen(fd
, "r");
455 perror("kernel tracepoint list fdopen");
460 * Init memory size counter
461 * See kernel-ctl.h for explanation of this value
463 nbmem
= KERNEL_EVENT_LIST_SIZE
;
466 while ((size
= getline(&line
, &nb
, fp
)) != -1) {
467 if (total
+ size
> nbmem
) {
468 DBG("Reallocating event list from %zd to %zd bytes", nbmem
,
469 total
+ size
+ KERNEL_EVENT_LIST_SIZE
);
470 /* Adding the default size again */
471 nbmem
= total
+ size
+ KERNEL_EVENT_LIST_SIZE
;
472 buf
= realloc(buf
, nbmem
);
474 perror("realloc list events");
478 memcpy(buf
+ total
, line
, size
);
484 DBG("Kernel list events done");
This page took 0.05282 seconds and 4 git commands to generate.