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