Add disable kernel channel support
[lttng-tools.git] / ltt-sessiond / kernel-ctl.c
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
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.
8 *
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.
13 *
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.
17 */
18
19#define _GNU_SOURCE
20#include <errno.h>
21#include <fcntl.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26
27#include "lttngerr.h"
28#include "ltt-sessiond.h"
29#include "libkernelctl.h"
30#include "kernel-ctl.h"
31
32/*
33 * kernel_create_session
34 *
35 * Create a new kernel session, register it to the kernel tracer and add it to
36 * the session daemon session.
37 */
38int kernel_create_session(struct ltt_session *session, int tracer_fd)
39{
40 int ret;
41 struct ltt_kernel_session *lks;
42
43 /* Allocate data structure */
44 lks = trace_create_kernel_session();
45 if (lks == NULL) {
46 ret = -1;
47 goto error;
48 }
49
50 /* Kernel tracer session creation */
51 ret = kernctl_create_session(tracer_fd);
52 if (ret < 0) {
53 perror("ioctl kernel create session");
54 goto error;
55 }
56
57 lks->fd = ret;
58 /* Prevent fd duplication after execlp() */
59 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
60 if (ret < 0) {
61 perror("fcntl session fd");
62 }
63
64 lks->kconsumer_fds_sent = 0;
65 session->kernel_session = lks;
66 session->kern_session_count++;
67
68 DBG("Kernel session created (fd: %d)", lks->fd);
69
70 return 0;
71
72error:
73 return ret;
74}
75
76/*
77 * kernel_create_channel
78 *
79 * Create a kernel channel, register it to the kernel tracer and add it to the
80 * kernel session.
81 */
82int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan)
83{
84 int ret;
85 struct ltt_kernel_channel *lkc;
86
87 /* Allocate kernel channel */
88 lkc = trace_create_kernel_channel(chan);
89 if (lkc == NULL) {
90 goto error;
91 }
92
93 /* Kernel tracer channel creation */
94 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
95 if (ret < 0) {
96 perror("ioctl kernel create channel");
97 goto error;
98 }
99
100 /* Setup the channel fd */
101 lkc->fd = ret;
102 /* Prevent fd duplication after execlp() */
103 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
104 if (ret < 0) {
105 perror("fcntl session fd");
106 }
107
108 /* Add channel to session */
109 cds_list_add(&lkc->list, &session->channel_list.head);
110 session->channel_count++;
111
112 DBG("Kernel channel %s created (fd: %d and path: %s)",
113 lkc->channel->name, lkc->fd, lkc->pathname);
114
115 return 0;
116
117error:
118 return -1;
119}
120
121/*
122 * kernel_create_event
123 *
124 * Create a kernel event, enable it to the kernel tracer and add it to the
125 * channel event list of the kernel session.
126 */
127int kernel_create_event(struct lttng_event *ev, struct ltt_kernel_channel *channel)
128{
129 int ret;
130 struct ltt_kernel_event *event;
131
132 event = trace_create_kernel_event(ev);
133 if (event == NULL) {
134 goto error;
135 }
136
137 ret = kernctl_create_event(channel->fd, event->event);
138 if (ret < 0) {
139 perror("create event ioctl");
140 goto free_event;
141 }
142
143 event->fd = ret;
144 /* Prevent fd duplication after execlp() */
145 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
146 if (ret < 0) {
147 perror("fcntl session fd");
148 }
149
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);
153
154 return 0;
155
156free_event:
157 free(event);
158error:
159 return -1;
160}
161
162/*
163 * kernel_disable_channel
164 *
165 * Disable a kernel channel.
166 */
167int kernel_disable_channel(struct ltt_kernel_channel *chan)
168{
169 int ret;
170
171 ret = kernctl_disable(chan->fd);
172 if (ret < 0) {
173 perror("disable chan ioctl");
174 ret = errno;
175 goto error;
176 }
177
178 chan->enabled = 0;
179 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
180
181 return 0;
182
183error:
184 return ret;
185}
186
187/*
188 * kernel_enable_channel
189 *
190 * Enable a kernel channel.
191 */
192int kernel_enable_channel(struct ltt_kernel_channel *chan)
193{
194 int ret;
195
196 ret = kernctl_enable(chan->fd);
197 if (ret < 0) {
198 perror("enable chan ioctl");
199 ret = errno;
200 goto error;
201 }
202
203 chan->enabled = 1;
204 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
205
206 return 0;
207
208error:
209 return ret;
210}
211
212/*
213 * kernel_enable_event
214 *
215 * Enable a kernel event.
216 */
217int kernel_enable_event(struct ltt_kernel_event *event)
218{
219 int ret;
220
221 ret = kernctl_enable(event->fd);
222 if (ret < 0) {
223 perror("enable event ioctl");
224 goto error;
225 }
226
227 event->enabled = 1;
228 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
229
230 return 0;
231
232error:
233 return ret;
234}
235
236/*
237 * kernel_disable_event
238 *
239 * Disable a kernel event.
240 */
241int kernel_disable_event(struct ltt_kernel_event *event)
242{
243 int ret;
244
245 ret = kernctl_disable(event->fd);
246 if (ret < 0) {
247 perror("disable event ioctl");
248 goto error;
249 }
250
251 event->enabled = 0;
252 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
253
254 return 0;
255
256error:
257 return ret;
258}
259
260/*
261 * kernel_open_metadata
262 *
263 * Create kernel metadata, open from the kernel tracer and add it to the
264 * kernel session.
265 */
266int kernel_open_metadata(struct ltt_kernel_session *session)
267{
268 int ret;
269 struct ltt_kernel_metadata *lkm;
270
271 /* Allocate kernel metadata */
272 lkm = trace_create_kernel_metadata();
273 if (lkm == NULL) {
274 goto error;
275 }
276
277 /* Kernel tracer metadata creation */
278 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
279 if (ret < 0) {
280 goto error;
281 }
282
283 lkm->fd = ret;
284 /* Prevent fd duplication after execlp() */
285 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
286 if (ret < 0) {
287 perror("fcntl session fd");
288 }
289
290 session->metadata = lkm;
291
292 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
293
294 return 0;
295
296error:
297 return -1;
298}
299
300/*
301 * kernel_start_session
302 *
303 * Start tracing session.
304 */
305int kernel_start_session(struct ltt_kernel_session *session)
306{
307 int ret;
308
309 ret = kernctl_start_session(session->fd);
310 if (ret < 0) {
311 perror("ioctl start session");
312 goto error;
313 }
314
315 DBG("Kernel session started");
316
317 return 0;
318
319error:
320 return ret;
321}
322
323/*
324 * kernel_wait_quiescent
325 *
326 * Make a kernel wait to make sure in-flight probe have completed.
327 */
328void kernel_wait_quiescent(int fd)
329{
330 int ret;
331
332 DBG("Kernel quiescent wait on %d", fd);
333
334 ret = kernctl_wait_quiescent(fd);
335 if (ret < 0) {
336 perror("wait quiescent ioctl");
337 ERR("Kernel quiescent wait failed");
338 }
339}
340
341/*
342 * kernel_metadata_flush_buffer
343 *
344 * Force flush buffer of metadata.
345 */
346int kernel_metadata_flush_buffer(int fd)
347{
348 int ret;
349
350 ret = kernctl_buffer_flush(fd);
351 if (ret < 0) {
352 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
353 }
354
355 return 0;
356}
357
358/*
359 * kernel_flush_buffer
360 *
361 * Force flush buffer for channel.
362 */
363int kernel_flush_buffer(struct ltt_kernel_channel *channel)
364{
365 int ret;
366 struct ltt_kernel_stream *stream;
367
368 DBG("Flush buffer for channel %s", channel->channel->name);
369
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);
373 if (ret < 0) {
374 perror("ioctl");
375 ERR("Fail to flush buffer for stream %d (ret: %d)",
376 stream->fd, ret);
377 }
378 }
379
380 return 0;
381}
382
383/*
384 * kernel_stop_session
385 *
386 * Stop tracing session.
387 */
388int kernel_stop_session(struct ltt_kernel_session *session)
389{
390 int ret;
391
392 ret = kernctl_stop_session(session->fd);
393 if (ret < 0) {
394 goto error;
395 }
396
397 DBG("Kernel session stopped");
398
399 return 0;
400
401error:
402 return ret;
403}
404
405/*
406 * kernel_open_channel_stream
407 *
408 * Open stream of channel, register it to the kernel tracer and add it
409 * to the stream list of the channel.
410 *
411 * Return the number of created stream. Else, a negative value.
412 */
413int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
414{
415 int ret;
416 struct ltt_kernel_stream *lks;
417
418 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
419 lks = trace_create_kernel_stream();
420 if (lks == NULL) {
421 close(ret);
422 goto error;
423 }
424
425 lks->fd = ret;
426 /* Prevent fd duplication after execlp() */
427 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
428 if (ret < 0) {
429 perror("fcntl session fd");
430 }
431
432 ret = asprintf(&lks->pathname, "%s/trace_%d",
433 channel->pathname, channel->stream_count);
434 if (ret < 0) {
435 perror("asprintf kernel create stream");
436 goto error;
437 }
438
439 /* Add stream to channe stream list */
440 cds_list_add(&lks->list, &channel->stream_list.head);
441 channel->stream_count++;
442
443 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
444 channel->stream_count, lks->fd, lks->state, lks->pathname);
445 }
446
447 return channel->stream_count;
448
449error:
450 return -1;
451}
452
453/*
454 * kernel_open_metadata_stream
455 *
456 * Open the metadata stream and set it to the kernel session.
457 */
458int kernel_open_metadata_stream(struct ltt_kernel_session *session)
459{
460 int ret;
461
462 ret = kernctl_create_stream(session->metadata->fd);
463 if (ret < 0) {
464 perror("kernel create metadata stream");
465 goto error;
466 }
467
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);
472 if (ret < 0) {
473 perror("fcntl session fd");
474 }
475
476 return 0;
477
478error:
479 return -1;
480}
481
482/*
483 * kernel_list_events
484 *
485 * Get the event list from the kernel tracer and return that list in the CTF
486 * format.
487 */
488ssize_t kernel_list_events(int tracer_fd, char **list)
489{
490 int fd;
491 char *buf, *line = NULL;
492 size_t nb, nbmem, total = 0;
493 ssize_t size;
494 FILE *fp;
495
496 fd = kernctl_tracepoint_list(tracer_fd);
497 if (fd < 0) {
498 perror("kernel tracepoint list");
499 goto error;
500 }
501
502 fp = fdopen(fd, "r");
503 if (fp == NULL) {
504 perror("kernel tracepoint list fdopen");
505 goto error;
506 }
507
508 /*
509 * Init memory size counter
510 * See kernel-ctl.h for explanation of this value
511 */
512 nbmem = KERNEL_EVENT_LIST_SIZE;
513 buf = malloc(nbmem);
514
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);
522 if (buf == NULL) {
523 perror("realloc list events");
524 goto error;
525 }
526 }
527 memcpy(buf + total, line, size);
528 total += size;
529 }
530
531 *list = buf;
532
533 DBG("Kernel list events done");
534
535 return total;
536
537error:
538 return -1;
539}
This page took 0.024756 seconds and 4 git commands to generate.