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