Fix: add missing rcu read side lock/unlock
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
CommitLineData
20fe2104
DG
1/*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
d14d33bf
AM
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
20fe2104
DG
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
d14d33bf
AM
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20fe2104
DG
16 */
17
8c0faa1d 18#define _GNU_SOURCE
20fe2104 19#include <errno.h>
7b395890 20#include <fcntl.h>
20fe2104
DG
21#include <stdlib.h>
22#include <stdio.h>
f34daff7 23#include <string.h>
8c0faa1d 24#include <unistd.h>
77c7c900 25#include <inttypes.h>
20fe2104 26
990570ed 27#include <common/common.h>
db758600 28#include <common/kernel-ctl/kernel-ctl.h>
42224349 29#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 30
2f77fc4b 31#include "consumer.h"
4771f025 32#include "kernel.h"
096102bd 33#include "kern-modules.h"
20fe2104 34
d65106b1 35/*
050349bb 36 * Add context on a kernel channel.
d65106b1
DG
37 */
38int kernel_add_channel_context(struct ltt_kernel_channel *chan,
39 struct lttng_kernel_context *ctx)
40{
41 int ret;
42
43 DBG("Adding context to channel %s", chan->channel->name);
44 ret = kernctl_add_context(chan->fd, ctx);
45 if (ret < 0) {
b579acd9 46 if (errno != EEXIST) {
df0f840b 47 PERROR("add context ioctl");
b579acd9
DG
48 } else {
49 /* If EEXIST, we just ignore the error */
50 ret = 0;
51 }
d65106b1
DG
52 goto error;
53 }
54
ba7f0ae5 55 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
d65106b1 56 if (chan->ctx == NULL) {
df0f840b 57 PERROR("zmalloc event context");
d65106b1
DG
58 goto error;
59 }
60
61 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
62
63 return 0;
64
65error:
66 return ret;
67}
68
20fe2104 69/*
050349bb
DG
70 * Create a new kernel session, register it to the kernel tracer and add it to
71 * the session daemon session.
20fe2104 72 */
8c0faa1d 73int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
74{
75 int ret;
76 struct ltt_kernel_session *lks;
77
54012638 78 /* Allocate data structure */
f9815039 79 lks = trace_kernel_create_session(session->path);
20fe2104 80 if (lks == NULL) {
54012638 81 ret = -1;
20fe2104
DG
82 goto error;
83 }
84
54012638 85 /* Kernel tracer session creation */
20fe2104
DG
86 ret = kernctl_create_session(tracer_fd);
87 if (ret < 0) {
df0f840b 88 PERROR("ioctl kernel create session");
20fe2104
DG
89 goto error;
90 }
91
20fe2104 92 lks->fd = ret;
7b395890
DG
93 /* Prevent fd duplication after execlp() */
94 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
95 if (ret < 0) {
df0f840b 96 PERROR("fcntl session fd");
7b395890
DG
97 }
98
53632229 99 lks->id = session->id;
3bd1e081 100 lks->consumer_fds_sent = 0;
8c0faa1d 101 session->kernel_session = lks;
8c0faa1d
DG
102
103 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
104
105 return 0;
106
107error:
108 return ret;
109}
110
111/*
050349bb
DG
112 * Create a kernel channel, register it to the kernel tracer and add it to the
113 * kernel session.
20fe2104 114 */
050349bb
DG
115int kernel_create_channel(struct ltt_kernel_session *session,
116 struct lttng_channel *chan, char *path)
20fe2104
DG
117{
118 int ret;
119 struct ltt_kernel_channel *lkc;
20fe2104 120
54012638 121 /* Allocate kernel channel */
62499ad6 122 lkc = trace_kernel_create_channel(chan, path);
54012638 123 if (lkc == NULL) {
20fe2104
DG
124 goto error;
125 }
126
77c7c900 127 DBG3("Kernel create channel %s in %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d",
173af62f
DG
128 chan->name, path, lkc->channel->attr.overwrite,
129 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
130 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
131 lkc->channel->attr.output);
132
54012638 133 /* Kernel tracer channel creation */
f3ed775e 134 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 135 if (ret < 0) {
df0f840b 136 PERROR("ioctl kernel create channel");
20fe2104
DG
137 goto error;
138 }
139
54012638 140 /* Setup the channel fd */
20fe2104 141 lkc->fd = ret;
7b395890
DG
142 /* Prevent fd duplication after execlp() */
143 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
144 if (ret < 0) {
df0f840b 145 PERROR("fcntl session fd");
7b395890
DG
146 }
147
54012638 148 /* Add channel to session */
8c0faa1d
DG
149 cds_list_add(&lkc->list, &session->channel_list.head);
150 session->channel_count++;
20fe2104 151
00e2e675 152 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
20fe2104
DG
153
154 return 0;
155
156error:
54012638 157 return -1;
20fe2104 158}
f34daff7
DG
159
160/*
050349bb
DG
161 * Create a kernel event, enable it to the kernel tracer and add it to the
162 * channel event list of the kernel session.
f34daff7 163 */
050349bb
DG
164int kernel_create_event(struct lttng_event *ev,
165 struct ltt_kernel_channel *channel)
f34daff7
DG
166{
167 int ret;
168 struct ltt_kernel_event *event;
f34daff7 169
62499ad6 170 event = trace_kernel_create_event(ev);
54012638 171 if (event == NULL) {
d87bfb32 172 ret = -1;
f34daff7
DG
173 goto error;
174 }
175
f3ed775e
DG
176 ret = kernctl_create_event(channel->fd, event->event);
177 if (ret < 0) {
bd29c13d
DG
178 switch (errno) {
179 case EEXIST:
180 break;
181 case ENOSYS:
182 WARN("Event type not implemented");
183 break;
184 default:
d87bfb32
DG
185 PERROR("create event ioctl");
186 }
187 ret = -errno;
e953ef25 188 goto free_event;
8c0faa1d 189 }
f34daff7 190
5f822d0a 191 /*
2c425ff7 192 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
5f822d0a
DG
193 */
194 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
195 DBG2("Kernel event syscall creation success");
87eb4ab8
MD
196 /*
197 * We use fd == -1 to ensure that we never trigger a close of fd
198 * 0.
199 */
200 event->fd = -1;
2c425ff7 201 goto add_list;
5f822d0a
DG
202 }
203
f3ed775e 204 event->fd = ret;
7b395890
DG
205 /* Prevent fd duplication after execlp() */
206 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
207 if (ret < 0) {
df0f840b 208 PERROR("fcntl session fd");
7b395890
DG
209 }
210
2c425ff7 211add_list:
f3ed775e
DG
212 /* Add event to event list */
213 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
214 channel->event_count++;
215
e953ef25
DG
216 DBG("Event %s created (fd: %d)", ev->name, event->fd);
217
218 return 0;
219
220free_event:
221 free(event);
222error:
d87bfb32 223 return ret;
e953ef25
DG
224}
225
26cc6b4e 226/*
050349bb 227 * Disable a kernel channel.
26cc6b4e
DG
228 */
229int kernel_disable_channel(struct ltt_kernel_channel *chan)
230{
231 int ret;
232
233 ret = kernctl_disable(chan->fd);
234 if (ret < 0) {
df0f840b 235 PERROR("disable chan ioctl");
26cc6b4e
DG
236 ret = errno;
237 goto error;
238 }
239
240 chan->enabled = 0;
241 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
242
243 return 0;
244
245error:
246 return ret;
247}
248
d36b8583 249/*
050349bb 250 * Enable a kernel channel.
d36b8583
DG
251 */
252int kernel_enable_channel(struct ltt_kernel_channel *chan)
253{
254 int ret;
255
256 ret = kernctl_enable(chan->fd);
54d01ffb 257 if (ret < 0 && errno != EEXIST) {
df0f840b 258 PERROR("Enable kernel chan");
d36b8583
DG
259 goto error;
260 }
261
262 chan->enabled = 1;
263 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
264
265 return 0;
266
267error:
268 return ret;
269}
270
19e70852 271/*
050349bb 272 * Enable a kernel event.
19e70852
DG
273 */
274int kernel_enable_event(struct ltt_kernel_event *event)
275{
276 int ret;
277
278 ret = kernctl_enable(event->fd);
42224349
DG
279 if (ret < 0) {
280 switch (errno) {
281 case EEXIST:
f73fabfd 282 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
283 break;
284 default:
285 PERROR("enable kernel event");
286 break;
287 }
19e70852
DG
288 goto error;
289 }
290
291 event->enabled = 1;
292 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
293
294 return 0;
295
296error:
d36b8583 297 return ret;
19e70852
DG
298}
299
e953ef25 300/*
050349bb 301 * Disable a kernel event.
e953ef25 302 */
19e70852 303int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
304{
305 int ret;
19e70852
DG
306
307 ret = kernctl_disable(event->fd);
42224349
DG
308 if (ret < 0) {
309 switch (errno) {
310 case EEXIST:
f73fabfd 311 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
312 break;
313 default:
314 PERROR("disable kernel event");
315 break;
316 }
19e70852 317 goto error;
e953ef25 318 }
f3ed775e 319
19e70852
DG
320 event->enabled = 0;
321 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
322
f34daff7
DG
323 return 0;
324
325error:
d36b8583 326 return ret;
f34daff7 327}
aaf26714
DG
328
329/*
050349bb
DG
330 * Create kernel metadata, open from the kernel tracer and add it to the
331 * kernel session.
aaf26714 332 */
a4b92340 333int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
334{
335 int ret;
336 struct ltt_kernel_metadata *lkm;
aaf26714 337
54012638 338 /* Allocate kernel metadata */
a4b92340 339 lkm = trace_kernel_create_metadata();
54012638 340 if (lkm == NULL) {
aaf26714
DG
341 goto error;
342 }
343
54012638 344 /* Kernel tracer metadata creation */
f3ed775e 345 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714
DG
346 if (ret < 0) {
347 goto error;
348 }
349
8c0faa1d 350 lkm->fd = ret;
7b395890
DG
351 /* Prevent fd duplication after execlp() */
352 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
353 if (ret < 0) {
df0f840b 354 PERROR("fcntl session fd");
7b395890
DG
355 }
356
aaf26714 357 session->metadata = lkm;
8c0faa1d 358
00e2e675 359 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
360
361 return 0;
362
363error:
54012638 364 return -1;
8c0faa1d
DG
365}
366
367/*
050349bb 368 * Start tracing session.
8c0faa1d
DG
369 */
370int kernel_start_session(struct ltt_kernel_session *session)
371{
372 int ret;
373
374 ret = kernctl_start_session(session->fd);
375 if (ret < 0) {
df0f840b 376 PERROR("ioctl start session");
8c0faa1d
DG
377 goto error;
378 }
379
380 DBG("Kernel session started");
381
382 return 0;
383
384error:
385 return ret;
386}
387
f3ed775e 388/*
050349bb 389 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e
DG
390 */
391void kernel_wait_quiescent(int fd)
392{
393 int ret;
394
395 DBG("Kernel quiescent wait on %d", fd);
396
397 ret = kernctl_wait_quiescent(fd);
398 if (ret < 0) {
df0f840b 399 PERROR("wait quiescent ioctl");
f3ed775e
DG
400 ERR("Kernel quiescent wait failed");
401 }
402}
403
d0254c7c 404/*
050349bb 405 * Kernel calibrate
d0254c7c
MD
406 */
407int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
408{
409 int ret;
410
411 ret = kernctl_calibrate(fd, calibrate);
412 if (ret < 0) {
df0f840b 413 PERROR("calibrate ioctl");
d0254c7c
MD
414 return -1;
415 }
416
417 return 0;
418}
419
420
f3ed775e 421/*
f3ed775e
DG
422 * Force flush buffer of metadata.
423 */
424int kernel_metadata_flush_buffer(int fd)
425{
426 int ret;
427
428 ret = kernctl_buffer_flush(fd);
429 if (ret < 0) {
00e2e675 430 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
431 }
432
433 return 0;
434}
435
436/*
050349bb 437 * Force flush buffer for channel.
f3ed775e
DG
438 */
439int kernel_flush_buffer(struct ltt_kernel_channel *channel)
440{
441 int ret;
442 struct ltt_kernel_stream *stream;
443
444 DBG("Flush buffer for channel %s", channel->channel->name);
445
446 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
447 DBG("Flushing channel stream %d", stream->fd);
448 ret = kernctl_buffer_flush(stream->fd);
449 if (ret < 0) {
df0f840b 450 PERROR("ioctl");
f3ed775e
DG
451 ERR("Fail to flush buffer for stream %d (ret: %d)",
452 stream->fd, ret);
453 }
454 }
455
456 return 0;
457}
458
8c0faa1d 459/*
050349bb 460 * Stop tracing session.
8c0faa1d
DG
461 */
462int kernel_stop_session(struct ltt_kernel_session *session)
463{
464 int ret;
465
466 ret = kernctl_stop_session(session->fd);
467 if (ret < 0) {
468 goto error;
469 }
470
471 DBG("Kernel session stopped");
472
473 return 0;
474
475error:
476 return ret;
477}
478
479/*
050349bb
DG
480 * Open stream of channel, register it to the kernel tracer and add it
481 * to the stream list of the channel.
8c0faa1d 482 *
050349bb 483 * Return the number of created stream. Else, a negative value.
8c0faa1d 484 */
f3ed775e 485int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 486{
00e2e675 487 int ret, count = 0;
8c0faa1d
DG
488 struct ltt_kernel_stream *lks;
489
5a47c6a2 490 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
00e2e675 491 lks = trace_kernel_create_stream(channel->channel->name, count);
8c0faa1d 492 if (lks == NULL) {
799e2c4f
MD
493 ret = close(ret);
494 if (ret) {
495 PERROR("close");
496 }
8c0faa1d
DG
497 goto error;
498 }
499
500 lks->fd = ret;
7b395890
DG
501 /* Prevent fd duplication after execlp() */
502 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
503 if (ret < 0) {
df0f840b 504 PERROR("fcntl session fd");
7b395890
DG
505 }
506
54012638 507 /* Add stream to channe stream list */
8c0faa1d
DG
508 cds_list_add(&lks->list, &channel->stream_list.head);
509 channel->stream_count++;
8c0faa1d 510
00e2e675
DG
511 /* Increment counter which represent CPU number. */
512 count++;
513
514 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
515 lks->state);
54012638 516 }
8c0faa1d
DG
517
518 return channel->stream_count;
519
520error:
54012638 521 return -1;
8c0faa1d
DG
522}
523
524/*
050349bb 525 * Open the metadata stream and set it to the kernel session.
8c0faa1d 526 */
f3ed775e 527int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
528{
529 int ret;
530
531 ret = kernctl_create_stream(session->metadata->fd);
532 if (ret < 0) {
df0f840b 533 PERROR("kernel create metadata stream");
8c0faa1d
DG
534 goto error;
535 }
536
537 DBG("Kernel metadata stream created (fd: %d)", ret);
538 session->metadata_stream_fd = ret;
7b395890
DG
539 /* Prevent fd duplication after execlp() */
540 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
541 if (ret < 0) {
df0f840b 542 PERROR("fcntl session fd");
7b395890 543 }
aaf26714
DG
544
545 return 0;
546
547error:
54012638 548 return -1;
aaf26714 549}
2ef84c95
DG
550
551/*
9f19cc17 552 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 553 */
9f19cc17 554ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 555{
799e2c4f 556 int fd, pos, ret;
9f19cc17
DG
557 char *event;
558 size_t nbmem, count = 0;
2ef84c95 559 FILE *fp;
9f19cc17 560 struct lttng_event *elist;
2ef84c95
DG
561
562 fd = kernctl_tracepoint_list(tracer_fd);
563 if (fd < 0) {
df0f840b 564 PERROR("kernel tracepoint list");
2ef84c95
DG
565 goto error;
566 }
567
568 fp = fdopen(fd, "r");
569 if (fp == NULL) {
df0f840b 570 PERROR("kernel tracepoint list fdopen");
61b73b12 571 goto error_fp;
2ef84c95
DG
572 }
573
574 /*
575 * Init memory size counter
576 * See kernel-ctl.h for explanation of this value
577 */
6725fe19 578 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 579 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
580 if (elist == NULL) {
581 PERROR("alloc list events");
582 count = -ENOMEM;
583 goto end;
584 }
2ef84c95 585
c617c0c6 586 while (fscanf(fp, "event { name = %m[^;]; };%n\n", &event, &pos) == 1) {
6725fe19 587 if (count >= nbmem) {
3b870559
MD
588 struct lttng_event *new_elist;
589
ced2f820 590 DBG("Reallocating event list from %zu to %zu bytes", nbmem,
6725fe19
DG
591 nbmem * 2);
592 /* Double the size */
593 nbmem <<= 1;
3b870559
MD
594 new_elist = realloc(elist, nbmem * sizeof(struct lttng_event));
595 if (new_elist == NULL) {
df0f840b 596 PERROR("realloc list events");
3b870559
MD
597 free(event);
598 free(elist);
61b73b12
MD
599 count = -ENOMEM;
600 goto end;
2ef84c95 601 }
3b870559 602 elist = new_elist;
2ef84c95 603 }
99497cd0
MD
604 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
605 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 606 elist[count].enabled = -1;
9f19cc17 607 count++;
3b870559 608 free(event);
2ef84c95
DG
609 }
610
9f19cc17 611 *events = elist;
ced2f820 612 DBG("Kernel list events done (%zu events)", count);
61b73b12 613end:
799e2c4f
MD
614 ret = fclose(fp); /* closes both fp and fd */
615 if (ret) {
616 PERROR("fclose");
617 }
9f19cc17 618 return count;
2ef84c95 619
61b73b12 620error_fp:
799e2c4f
MD
621 ret = close(fd);
622 if (ret) {
623 PERROR("close");
624 }
2ef84c95
DG
625error:
626 return -1;
627}
096102bd
DG
628
629/*
630 * Get kernel version and validate it.
631 */
632int kernel_validate_version(int tracer_fd)
633{
634 int ret;
635 struct lttng_kernel_tracer_version version;
636
637 ret = kernctl_tracer_version(tracer_fd, &version);
638 if (ret < 0) {
639 ERR("Failed at getting the lttng-modules version");
640 goto error;
641 }
642
643 /* Validate version */
a62a6556
MD
644 if (version.major != KERN_MODULES_PRE_MAJOR
645 && version.major != KERN_MODULES_MAJOR) {
096102bd 646 goto error_version;
096102bd
DG
647 }
648
a62a6556 649 DBG2("Kernel tracer version validated (major version %d)", version.major);
096102bd
DG
650 return 0;
651
652error_version:
5df0f285 653 ERR("Kernel major version %d is not compatible (supporting <= %d)",
a62a6556 654 version.major, KERN_MODULES_MAJOR)
096102bd
DG
655 ret = -1;
656
657error:
658 return ret;
659}
335a95b7
MD
660
661/*
662 * Kernel work-arounds called at the start of sessiond main().
663 */
664int init_kernel_workarounds(void)
665{
8936c33a 666 int ret;
335a95b7
MD
667 FILE *fp;
668
669 /*
670 * boot_id needs to be read once before being used concurrently
671 * to deal with a Linux kernel race. A fix is proposed for
672 * upstream, but the work-around is needed for older kernels.
673 */
674 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
675 if (!fp) {
676 goto end_boot_id;
677 }
678 while (!feof(fp)) {
679 char buf[37] = "";
680
8936c33a
DG
681 ret = fread(buf, 1, sizeof(buf), fp);
682 if (ret < 0) {
683 /* Ignore error, we don't really care */
684 }
335a95b7 685 }
799e2c4f
MD
686 ret = fclose(fp);
687 if (ret) {
688 PERROR("fclose");
689 }
335a95b7 690end_boot_id:
335a95b7
MD
691 return 0;
692}
2f77fc4b
DG
693
694/*
695 * Complete teardown of a kernel session.
696 */
697void kernel_destroy_session(struct ltt_kernel_session *ksess)
698{
699 if (ksess == NULL) {
700 DBG3("No kernel session when tearing down session");
701 return;
702 }
703
704 DBG("Tearing down kernel session");
705
706 /* Close any relayd session */
707 consumer_output_send_destroy_relayd(ksess->consumer);
708
709 trace_kernel_destroy_session(ksess);
710}
This page took 0.074381 seconds and 5 git commands to generate.