Add TODO file
[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
82a3637f
DG
6 * as published by the Free Software Foundation; only version 2
7 * of the License.
20fe2104
DG
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
1e307fab
DG
27#include <lttng-kernel-ctl.h>
28#include <lttngerr.h>
29
20fe2104 30#include "kernel-ctl.h"
20fe2104 31
d65106b1 32/*
050349bb 33 * Add context on a kernel channel.
d65106b1
DG
34 */
35int kernel_add_channel_context(struct ltt_kernel_channel *chan,
36 struct lttng_kernel_context *ctx)
37{
38 int ret;
39
40 DBG("Adding context to channel %s", chan->channel->name);
41 ret = kernctl_add_context(chan->fd, ctx);
42 if (ret < 0) {
b579acd9
DG
43 if (errno != EEXIST) {
44 perror("add context ioctl");
45 } else {
46 /* If EEXIST, we just ignore the error */
47 ret = 0;
48 }
d65106b1
DG
49 goto error;
50 }
51
52 chan->ctx = malloc(sizeof(struct lttng_kernel_context));
53 if (chan->ctx == NULL) {
54 perror("malloc event context");
55 goto error;
56 }
57
58 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
59
60 return 0;
61
62error:
63 return ret;
64}
65
66/*
050349bb 67 * Add context on a kernel event.
d65106b1
DG
68 */
69int kernel_add_event_context(struct ltt_kernel_event *event,
70 struct lttng_kernel_context *ctx)
71{
72 int ret;
73
74 DBG("Adding context to event %s", event->event->name);
75 ret = kernctl_add_context(event->fd, ctx);
76 if (ret < 0) {
77 perror("add context ioctl");
78 goto error;
79 }
80
81 event->ctx = malloc(sizeof(struct lttng_kernel_context));
82 if (event->ctx == NULL) {
83 perror("malloc event context");
84 goto error;
85 }
86
87 memcpy(event->ctx, ctx, sizeof(struct lttng_kernel_context));
88
89 return 0;
90
91error:
92 return ret;
93}
94
20fe2104 95/*
050349bb
DG
96 * Create a new kernel session, register it to the kernel tracer and add it to
97 * the session daemon session.
20fe2104 98 */
8c0faa1d 99int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
100{
101 int ret;
102 struct ltt_kernel_session *lks;
103
54012638 104 /* Allocate data structure */
f9815039 105 lks = trace_kernel_create_session(session->path);
20fe2104 106 if (lks == NULL) {
54012638 107 ret = -1;
20fe2104
DG
108 goto error;
109 }
110
54012638 111 /* Kernel tracer session creation */
20fe2104
DG
112 ret = kernctl_create_session(tracer_fd);
113 if (ret < 0) {
54012638 114 perror("ioctl kernel create session");
20fe2104
DG
115 goto error;
116 }
117
20fe2104 118 lks->fd = ret;
7b395890
DG
119 /* Prevent fd duplication after execlp() */
120 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
121 if (ret < 0) {
122 perror("fcntl session fd");
123 }
124
f3ed775e 125 lks->kconsumer_fds_sent = 0;
8c0faa1d 126 session->kernel_session = lks;
8c0faa1d
DG
127
128 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
129
130 return 0;
131
132error:
133 return ret;
134}
135
136/*
050349bb
DG
137 * Create a kernel channel, register it to the kernel tracer and add it to the
138 * kernel session.
20fe2104 139 */
050349bb
DG
140int kernel_create_channel(struct ltt_kernel_session *session,
141 struct lttng_channel *chan, char *path)
20fe2104
DG
142{
143 int ret;
144 struct ltt_kernel_channel *lkc;
20fe2104 145
54012638 146 /* Allocate kernel channel */
62499ad6 147 lkc = trace_kernel_create_channel(chan, path);
54012638 148 if (lkc == NULL) {
20fe2104
DG
149 goto error;
150 }
151
54012638 152 /* Kernel tracer channel creation */
f3ed775e 153 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 154 if (ret < 0) {
54012638 155 perror("ioctl kernel create channel");
20fe2104
DG
156 goto error;
157 }
158
54012638 159 /* Setup the channel fd */
20fe2104 160 lkc->fd = ret;
7b395890
DG
161 /* Prevent fd duplication after execlp() */
162 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
163 if (ret < 0) {
164 perror("fcntl session fd");
165 }
166
54012638 167 /* Add channel to session */
8c0faa1d
DG
168 cds_list_add(&lkc->list, &session->channel_list.head);
169 session->channel_count++;
20fe2104 170
f3ed775e
DG
171 DBG("Kernel channel %s created (fd: %d and path: %s)",
172 lkc->channel->name, lkc->fd, lkc->pathname);
20fe2104
DG
173
174 return 0;
175
176error:
54012638 177 return -1;
20fe2104 178}
f34daff7
DG
179
180/*
050349bb
DG
181 * Create a kernel event, enable it to the kernel tracer and add it to the
182 * channel event list of the kernel session.
f34daff7 183 */
050349bb
DG
184int kernel_create_event(struct lttng_event *ev,
185 struct ltt_kernel_channel *channel)
f34daff7
DG
186{
187 int ret;
188 struct ltt_kernel_event *event;
f34daff7 189
62499ad6 190 event = trace_kernel_create_event(ev);
54012638 191 if (event == NULL) {
f34daff7
DG
192 goto error;
193 }
194
f3ed775e
DG
195 ret = kernctl_create_event(channel->fd, event->event);
196 if (ret < 0) {
e953ef25
DG
197 perror("create event ioctl");
198 goto free_event;
8c0faa1d 199 }
f34daff7 200
f3ed775e 201 event->fd = ret;
7b395890
DG
202 /* Prevent fd duplication after execlp() */
203 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
204 if (ret < 0) {
205 perror("fcntl session fd");
206 }
207
f3ed775e
DG
208 /* Add event to event list */
209 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
210 channel->event_count++;
211
e953ef25
DG
212 DBG("Event %s created (fd: %d)", ev->name, event->fd);
213
214 return 0;
215
216free_event:
217 free(event);
218error:
219 return -1;
220}
221
26cc6b4e 222/*
050349bb 223 * Disable a kernel channel.
26cc6b4e
DG
224 */
225int kernel_disable_channel(struct ltt_kernel_channel *chan)
226{
227 int ret;
228
229 ret = kernctl_disable(chan->fd);
230 if (ret < 0) {
231 perror("disable chan ioctl");
232 ret = errno;
233 goto error;
234 }
235
236 chan->enabled = 0;
237 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
238
239 return 0;
240
241error:
242 return ret;
243}
244
d36b8583 245/*
050349bb 246 * Enable a kernel channel.
d36b8583
DG
247 */
248int kernel_enable_channel(struct ltt_kernel_channel *chan)
249{
250 int ret;
251
252 ret = kernctl_enable(chan->fd);
54d01ffb
DG
253 if (ret < 0 && errno != EEXIST) {
254 perror("Enable kernel chan");
d36b8583
DG
255 goto error;
256 }
257
258 chan->enabled = 1;
259 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
260
261 return 0;
262
263error:
264 return ret;
265}
266
19e70852 267/*
050349bb 268 * Enable a kernel event.
19e70852
DG
269 */
270int kernel_enable_event(struct ltt_kernel_event *event)
271{
272 int ret;
273
274 ret = kernctl_enable(event->fd);
54d01ffb
DG
275 if (ret < 0 && errno != EEXIST) {
276 perror("enable kernel event");
19e70852
DG
277 goto error;
278 }
279
280 event->enabled = 1;
281 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
282
283 return 0;
284
285error:
d36b8583 286 return ret;
19e70852
DG
287}
288
e953ef25 289/*
050349bb 290 * Disable a kernel event.
e953ef25 291 */
19e70852 292int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
293{
294 int ret;
19e70852
DG
295
296 ret = kernctl_disable(event->fd);
54d01ffb
DG
297 if (ret < 0 && errno != EEXIST) {
298 perror("disable kernel event");
19e70852 299 goto error;
e953ef25 300 }
f3ed775e 301
19e70852
DG
302 event->enabled = 0;
303 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
304
f34daff7
DG
305 return 0;
306
307error:
d36b8583 308 return ret;
f34daff7 309}
aaf26714
DG
310
311/*
050349bb
DG
312 * Create kernel metadata, open from the kernel tracer and add it to the
313 * kernel session.
aaf26714 314 */
58a97671 315int kernel_open_metadata(struct ltt_kernel_session *session, char *path)
aaf26714
DG
316{
317 int ret;
318 struct ltt_kernel_metadata *lkm;
aaf26714 319
54012638 320 /* Allocate kernel metadata */
62499ad6 321 lkm = trace_kernel_create_metadata(path);
54012638 322 if (lkm == NULL) {
aaf26714
DG
323 goto error;
324 }
325
54012638 326 /* Kernel tracer metadata creation */
f3ed775e 327 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
328 if (ret < 0) {
329 goto error;
330 }
331
8c0faa1d 332 lkm->fd = ret;
7b395890
DG
333 /* Prevent fd duplication after execlp() */
334 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
335 if (ret < 0) {
336 perror("fcntl session fd");
337 }
338
aaf26714 339 session->metadata = lkm;
8c0faa1d 340
54012638 341 DBG("Kernel metadata opened (fd: %d and path: %s)", lkm->fd, lkm->pathname);
8c0faa1d
DG
342
343 return 0;
344
345error:
54012638 346 return -1;
8c0faa1d
DG
347}
348
349/*
050349bb 350 * Start tracing session.
8c0faa1d
DG
351 */
352int kernel_start_session(struct ltt_kernel_session *session)
353{
354 int ret;
355
356 ret = kernctl_start_session(session->fd);
357 if (ret < 0) {
f3ed775e 358 perror("ioctl start session");
8c0faa1d
DG
359 goto error;
360 }
361
362 DBG("Kernel session started");
363
364 return 0;
365
366error:
367 return ret;
368}
369
f3ed775e 370/*
050349bb 371 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e
DG
372 */
373void kernel_wait_quiescent(int fd)
374{
375 int ret;
376
377 DBG("Kernel quiescent wait on %d", fd);
378
379 ret = kernctl_wait_quiescent(fd);
380 if (ret < 0) {
381 perror("wait quiescent ioctl");
382 ERR("Kernel quiescent wait failed");
383 }
384}
385
d0254c7c 386/*
050349bb 387 * Kernel calibrate
d0254c7c
MD
388 */
389int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
390{
391 int ret;
392
393 ret = kernctl_calibrate(fd, calibrate);
394 if (ret < 0) {
395 perror("calibrate ioctl");
396 return -1;
397 }
398
399 return 0;
400}
401
402
f3ed775e 403/*
f3ed775e
DG
404 * Force flush buffer of metadata.
405 */
406int kernel_metadata_flush_buffer(int fd)
407{
408 int ret;
409
410 ret = kernctl_buffer_flush(fd);
411 if (ret < 0) {
412 ERR("Fail to flush metadata buffers %d (ret: %d", fd, ret);
413 }
414
415 return 0;
416}
417
418/*
050349bb 419 * Force flush buffer for channel.
f3ed775e
DG
420 */
421int kernel_flush_buffer(struct ltt_kernel_channel *channel)
422{
423 int ret;
424 struct ltt_kernel_stream *stream;
425
426 DBG("Flush buffer for channel %s", channel->channel->name);
427
428 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
429 DBG("Flushing channel stream %d", stream->fd);
430 ret = kernctl_buffer_flush(stream->fd);
431 if (ret < 0) {
432 perror("ioctl");
433 ERR("Fail to flush buffer for stream %d (ret: %d)",
434 stream->fd, ret);
435 }
436 }
437
438 return 0;
439}
440
8c0faa1d 441/*
050349bb 442 * Stop tracing session.
8c0faa1d
DG
443 */
444int kernel_stop_session(struct ltt_kernel_session *session)
445{
446 int ret;
447
448 ret = kernctl_stop_session(session->fd);
449 if (ret < 0) {
450 goto error;
451 }
452
453 DBG("Kernel session stopped");
454
455 return 0;
456
457error:
458 return ret;
459}
460
461/*
050349bb
DG
462 * Open stream of channel, register it to the kernel tracer and add it
463 * to the stream list of the channel.
8c0faa1d 464 *
050349bb 465 * Return the number of created stream. Else, a negative value.
8c0faa1d 466 */
f3ed775e 467int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d
DG
468{
469 int ret;
470 struct ltt_kernel_stream *lks;
471
472 while ((ret = kernctl_create_stream(channel->fd)) > 0) {
62499ad6 473 lks = trace_kernel_create_stream();
8c0faa1d 474 if (lks == NULL) {
54012638 475 close(ret);
8c0faa1d
DG
476 goto error;
477 }
478
479 lks->fd = ret;
7b395890
DG
480 /* Prevent fd duplication after execlp() */
481 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
482 if (ret < 0) {
483 perror("fcntl session fd");
484 }
485
8e68d1c8
DG
486 ret = asprintf(&lks->pathname, "%s/%s_%d",
487 channel->pathname, channel->channel->name, channel->stream_count);
8c0faa1d
DG
488 if (ret < 0) {
489 perror("asprintf kernel create stream");
490 goto error;
491 }
8c0faa1d 492
54012638 493 /* Add stream to channe stream list */
8c0faa1d
DG
494 cds_list_add(&lks->list, &channel->stream_list.head);
495 channel->stream_count++;
8c0faa1d 496
54012638
DG
497 DBG("Kernel stream %d created (fd: %d, state: %d, path: %s)",
498 channel->stream_count, lks->fd, lks->state, lks->pathname);
499 }
8c0faa1d
DG
500
501 return channel->stream_count;
502
503error:
54012638 504 return -1;
8c0faa1d
DG
505}
506
507/*
050349bb 508 * Open the metadata stream and set it to the kernel session.
8c0faa1d 509 */
f3ed775e 510int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
511{
512 int ret;
513
514 ret = kernctl_create_stream(session->metadata->fd);
515 if (ret < 0) {
516 perror("kernel create metadata stream");
8c0faa1d
DG
517 goto error;
518 }
519
520 DBG("Kernel metadata stream created (fd: %d)", ret);
521 session->metadata_stream_fd = ret;
7b395890
DG
522 /* Prevent fd duplication after execlp() */
523 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
524 if (ret < 0) {
525 perror("fcntl session fd");
526 }
aaf26714
DG
527
528 return 0;
529
530error:
54012638 531 return -1;
aaf26714 532}
2ef84c95
DG
533
534/*
9f19cc17 535 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 536 */
9f19cc17 537ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 538{
9f19cc17
DG
539 int fd, pos;
540 char *event;
541 size_t nbmem, count = 0;
2ef84c95
DG
542 ssize_t size;
543 FILE *fp;
9f19cc17 544 struct lttng_event *elist;
2ef84c95
DG
545
546 fd = kernctl_tracepoint_list(tracer_fd);
547 if (fd < 0) {
548 perror("kernel tracepoint list");
549 goto error;
550 }
551
552 fp = fdopen(fd, "r");
553 if (fp == NULL) {
554 perror("kernel tracepoint list fdopen");
61b73b12 555 goto error_fp;
2ef84c95
DG
556 }
557
558 /*
559 * Init memory size counter
560 * See kernel-ctl.h for explanation of this value
561 */
562 nbmem = KERNEL_EVENT_LIST_SIZE;
9f19cc17 563 elist = malloc(sizeof(struct lttng_event) * nbmem);
2ef84c95 564
9f19cc17
DG
565 while ((size = fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos)) == 1) {
566 if (count > nbmem) {
ced2f820 567 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
9f19cc17 568 nbmem + KERNEL_EVENT_LIST_SIZE);
2ef84c95 569 /* Adding the default size again */
9f19cc17
DG
570 nbmem += KERNEL_EVENT_LIST_SIZE;
571 elist = realloc(elist, nbmem);
572 if (elist == NULL) {
2ef84c95 573 perror("realloc list events");
61b73b12
MD
574 count = -ENOMEM;
575 goto end;
2ef84c95
DG
576 }
577 }
99497cd0
MD
578 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
579 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
9f19cc17 580 count++;
2ef84c95
DG
581 }
582
9f19cc17 583 *events = elist;
ced2f820 584 DBG("Kernel list events done (%zu events)", count);
61b73b12
MD
585end:
586 fclose(fp); /* closes both fp and fd */
9f19cc17 587 return count;
2ef84c95 588
61b73b12
MD
589error_fp:
590 close(fd);
2ef84c95
DG
591error:
592 return -1;
593}
This page took 0.051073 seconds and 4 git commands to generate.