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