Fix: rename of perf counter context
[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"
6dc3064a 33#include "kernel-consumer.h"
096102bd 34#include "kern-modules.h"
20fe2104 35
d65106b1 36/*
050349bb 37 * Add context on a kernel channel.
d65106b1
DG
38 */
39int kernel_add_channel_context(struct ltt_kernel_channel *chan,
40 struct lttng_kernel_context *ctx)
41{
42 int ret;
43
0525e9ae
DG
44 assert(chan);
45 assert(ctx);
46
d65106b1
DG
47 DBG("Adding context to channel %s", chan->channel->name);
48 ret = kernctl_add_context(chan->fd, ctx);
49 if (ret < 0) {
b579acd9 50 if (errno != EEXIST) {
df0f840b 51 PERROR("add context ioctl");
b579acd9
DG
52 } else {
53 /* If EEXIST, we just ignore the error */
54 ret = 0;
55 }
d65106b1
DG
56 goto error;
57 }
58
ba7f0ae5 59 chan->ctx = zmalloc(sizeof(struct lttng_kernel_context));
d65106b1 60 if (chan->ctx == NULL) {
df0f840b 61 PERROR("zmalloc event context");
d65106b1
DG
62 goto error;
63 }
64
65 memcpy(chan->ctx, ctx, sizeof(struct lttng_kernel_context));
66
67 return 0;
68
69error:
70 return ret;
71}
72
20fe2104 73/*
050349bb
DG
74 * Create a new kernel session, register it to the kernel tracer and add it to
75 * the session daemon session.
20fe2104 76 */
8c0faa1d 77int kernel_create_session(struct ltt_session *session, int tracer_fd)
20fe2104
DG
78{
79 int ret;
80 struct ltt_kernel_session *lks;
81
0525e9ae
DG
82 assert(session);
83
54012638 84 /* Allocate data structure */
dec56f6c 85 lks = trace_kernel_create_session();
20fe2104 86 if (lks == NULL) {
54012638 87 ret = -1;
20fe2104
DG
88 goto error;
89 }
90
54012638 91 /* Kernel tracer session creation */
20fe2104
DG
92 ret = kernctl_create_session(tracer_fd);
93 if (ret < 0) {
df0f840b 94 PERROR("ioctl kernel create session");
20fe2104
DG
95 goto error;
96 }
97
20fe2104 98 lks->fd = ret;
7b395890
DG
99 /* Prevent fd duplication after execlp() */
100 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
101 if (ret < 0) {
df0f840b 102 PERROR("fcntl session fd");
7b395890
DG
103 }
104
53632229 105 lks->id = session->id;
3bd1e081 106 lks->consumer_fds_sent = 0;
8c0faa1d 107 session->kernel_session = lks;
8c0faa1d
DG
108
109 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104
DG
110
111 return 0;
112
113error:
5f62c685
DG
114 if (lks) {
115 trace_kernel_destroy_session(lks);
116 }
20fe2104
DG
117 return ret;
118}
119
120/*
050349bb
DG
121 * Create a kernel channel, register it to the kernel tracer and add it to the
122 * kernel session.
20fe2104 123 */
050349bb 124int kernel_create_channel(struct ltt_kernel_session *session,
fdd9eb17 125 struct lttng_channel *chan)
20fe2104
DG
126{
127 int ret;
128 struct ltt_kernel_channel *lkc;
20fe2104 129
0525e9ae
DG
130 assert(session);
131 assert(chan);
0525e9ae 132
54012638 133 /* Allocate kernel channel */
fdd9eb17 134 lkc = trace_kernel_create_channel(chan);
54012638 135 if (lkc == NULL) {
20fe2104
DG
136 goto error;
137 }
138
ecc48a90 139 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
fdd9eb17 140 chan->name, lkc->channel->attr.overwrite,
173af62f
DG
141 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
142 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
ecc48a90 143 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
173af62f 144
54012638 145 /* Kernel tracer channel creation */
f3ed775e 146 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 147 if (ret < 0) {
df0f840b 148 PERROR("ioctl kernel create channel");
20fe2104
DG
149 goto error;
150 }
151
54012638 152 /* Setup the channel fd */
20fe2104 153 lkc->fd = ret;
7b395890
DG
154 /* Prevent fd duplication after execlp() */
155 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
156 if (ret < 0) {
df0f840b 157 PERROR("fcntl session fd");
7b395890
DG
158 }
159
54012638 160 /* Add channel to session */
8c0faa1d
DG
161 cds_list_add(&lkc->list, &session->channel_list.head);
162 session->channel_count++;
fb5f35b6 163 lkc->session = session;
20fe2104 164
00e2e675 165 DBG("Kernel channel %s created (fd: %d)", lkc->channel->name, lkc->fd);
20fe2104
DG
166
167 return 0;
168
169error:
5f62c685
DG
170 if (lkc) {
171 free(lkc->channel);
172 free(lkc);
173 }
54012638 174 return -1;
20fe2104 175}
f34daff7
DG
176
177/*
050349bb
DG
178 * Create a kernel event, enable it to the kernel tracer and add it to the
179 * channel event list of the kernel session.
f34daff7 180 */
050349bb
DG
181int kernel_create_event(struct lttng_event *ev,
182 struct ltt_kernel_channel *channel)
f34daff7
DG
183{
184 int ret;
185 struct ltt_kernel_event *event;
f34daff7 186
0525e9ae
DG
187 assert(ev);
188 assert(channel);
189
62499ad6 190 event = trace_kernel_create_event(ev);
54012638 191 if (event == NULL) {
d87bfb32 192 ret = -1;
f34daff7
DG
193 goto error;
194 }
195
f3ed775e
DG
196 ret = kernctl_create_event(channel->fd, event->event);
197 if (ret < 0) {
bd29c13d
DG
198 switch (errno) {
199 case EEXIST:
200 break;
201 case ENOSYS:
202 WARN("Event type not implemented");
203 break;
204 default:
d87bfb32
DG
205 PERROR("create event ioctl");
206 }
207 ret = -errno;
e953ef25 208 goto free_event;
8c0faa1d 209 }
f34daff7 210
5f822d0a 211 /*
2c425ff7 212 * LTTNG_KERNEL_SYSCALL event creation will return 0 on success.
5f822d0a
DG
213 */
214 if (ret == 0 && event->event->instrumentation == LTTNG_KERNEL_SYSCALL) {
215 DBG2("Kernel event syscall creation success");
87eb4ab8
MD
216 /*
217 * We use fd == -1 to ensure that we never trigger a close of fd
218 * 0.
219 */
220 event->fd = -1;
2c425ff7 221 goto add_list;
5f822d0a
DG
222 }
223
f3ed775e 224 event->fd = ret;
7b395890
DG
225 /* Prevent fd duplication after execlp() */
226 ret = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
227 if (ret < 0) {
df0f840b 228 PERROR("fcntl session fd");
7b395890
DG
229 }
230
2c425ff7 231add_list:
f3ed775e
DG
232 /* Add event to event list */
233 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
234 channel->event_count++;
235
e953ef25
DG
236 DBG("Event %s created (fd: %d)", ev->name, event->fd);
237
238 return 0;
239
240free_event:
241 free(event);
242error:
d87bfb32 243 return ret;
e953ef25
DG
244}
245
26cc6b4e 246/*
050349bb 247 * Disable a kernel channel.
26cc6b4e
DG
248 */
249int kernel_disable_channel(struct ltt_kernel_channel *chan)
250{
251 int ret;
252
0525e9ae
DG
253 assert(chan);
254
26cc6b4e
DG
255 ret = kernctl_disable(chan->fd);
256 if (ret < 0) {
df0f840b 257 PERROR("disable chan ioctl");
26cc6b4e
DG
258 ret = errno;
259 goto error;
260 }
261
262 chan->enabled = 0;
263 DBG("Kernel channel %s disabled (fd: %d)", chan->channel->name, chan->fd);
264
265 return 0;
266
267error:
268 return ret;
269}
270
d36b8583 271/*
050349bb 272 * Enable a kernel channel.
d36b8583
DG
273 */
274int kernel_enable_channel(struct ltt_kernel_channel *chan)
275{
276 int ret;
277
0525e9ae
DG
278 assert(chan);
279
d36b8583 280 ret = kernctl_enable(chan->fd);
54d01ffb 281 if (ret < 0 && errno != EEXIST) {
df0f840b 282 PERROR("Enable kernel chan");
d36b8583
DG
283 goto error;
284 }
285
286 chan->enabled = 1;
287 DBG("Kernel channel %s enabled (fd: %d)", chan->channel->name, chan->fd);
288
289 return 0;
290
291error:
292 return ret;
293}
294
19e70852 295/*
050349bb 296 * Enable a kernel event.
19e70852
DG
297 */
298int kernel_enable_event(struct ltt_kernel_event *event)
299{
300 int ret;
301
0525e9ae
DG
302 assert(event);
303
19e70852 304 ret = kernctl_enable(event->fd);
42224349
DG
305 if (ret < 0) {
306 switch (errno) {
307 case EEXIST:
f73fabfd 308 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
309 break;
310 default:
311 PERROR("enable kernel event");
312 break;
313 }
19e70852
DG
314 goto error;
315 }
316
317 event->enabled = 1;
318 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
319
320 return 0;
321
322error:
d36b8583 323 return ret;
19e70852
DG
324}
325
e953ef25 326/*
050349bb 327 * Disable a kernel event.
e953ef25 328 */
19e70852 329int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
330{
331 int ret;
19e70852 332
0525e9ae
DG
333 assert(event);
334
19e70852 335 ret = kernctl_disable(event->fd);
42224349
DG
336 if (ret < 0) {
337 switch (errno) {
338 case EEXIST:
f73fabfd 339 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
340 break;
341 default:
342 PERROR("disable kernel event");
343 break;
344 }
19e70852 345 goto error;
e953ef25 346 }
f3ed775e 347
19e70852
DG
348 event->enabled = 0;
349 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
350
f34daff7
DG
351 return 0;
352
353error:
d36b8583 354 return ret;
f34daff7 355}
aaf26714
DG
356
357/*
050349bb
DG
358 * Create kernel metadata, open from the kernel tracer and add it to the
359 * kernel session.
aaf26714 360 */
a4b92340 361int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
362{
363 int ret;
74024a21 364 struct ltt_kernel_metadata *lkm = NULL;
aaf26714 365
0525e9ae
DG
366 assert(session);
367
54012638 368 /* Allocate kernel metadata */
a4b92340 369 lkm = trace_kernel_create_metadata();
54012638 370 if (lkm == NULL) {
aaf26714
DG
371 goto error;
372 }
373
54012638 374 /* Kernel tracer metadata creation */
f3ed775e 375 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 376 if (ret < 0) {
74024a21 377 goto error_open;
aaf26714
DG
378 }
379
8c0faa1d 380 lkm->fd = ret;
7b395890
DG
381 /* Prevent fd duplication after execlp() */
382 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
383 if (ret < 0) {
df0f840b 384 PERROR("fcntl session fd");
7b395890
DG
385 }
386
aaf26714 387 session->metadata = lkm;
8c0faa1d 388
00e2e675 389 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
390
391 return 0;
392
74024a21
DG
393error_open:
394 trace_kernel_destroy_metadata(lkm);
8c0faa1d 395error:
54012638 396 return -1;
8c0faa1d
DG
397}
398
399/*
050349bb 400 * Start tracing session.
8c0faa1d
DG
401 */
402int kernel_start_session(struct ltt_kernel_session *session)
403{
404 int ret;
405
0525e9ae
DG
406 assert(session);
407
8c0faa1d
DG
408 ret = kernctl_start_session(session->fd);
409 if (ret < 0) {
df0f840b 410 PERROR("ioctl start session");
8c0faa1d
DG
411 goto error;
412 }
413
414 DBG("Kernel session started");
415
416 return 0;
417
418error:
419 return ret;
420}
421
f3ed775e 422/*
050349bb 423 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e
DG
424 */
425void kernel_wait_quiescent(int fd)
426{
427 int ret;
428
429 DBG("Kernel quiescent wait on %d", fd);
430
431 ret = kernctl_wait_quiescent(fd);
432 if (ret < 0) {
df0f840b 433 PERROR("wait quiescent ioctl");
f3ed775e
DG
434 ERR("Kernel quiescent wait failed");
435 }
436}
437
d0254c7c 438/*
050349bb 439 * Kernel calibrate
d0254c7c
MD
440 */
441int kernel_calibrate(int fd, struct lttng_kernel_calibrate *calibrate)
442{
443 int ret;
444
0525e9ae
DG
445 assert(calibrate);
446
d0254c7c
MD
447 ret = kernctl_calibrate(fd, calibrate);
448 if (ret < 0) {
df0f840b 449 PERROR("calibrate ioctl");
d0254c7c
MD
450 return -1;
451 }
452
453 return 0;
454}
455
456
f3ed775e 457/*
f3ed775e
DG
458 * Force flush buffer of metadata.
459 */
460int kernel_metadata_flush_buffer(int fd)
461{
462 int ret;
463
169d2cb7
DG
464 DBG("Kernel flushing metadata buffer on fd %d", fd);
465
f3ed775e
DG
466 ret = kernctl_buffer_flush(fd);
467 if (ret < 0) {
00e2e675 468 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
469 }
470
471 return 0;
472}
473
474/*
050349bb 475 * Force flush buffer for channel.
f3ed775e
DG
476 */
477int kernel_flush_buffer(struct ltt_kernel_channel *channel)
478{
479 int ret;
480 struct ltt_kernel_stream *stream;
481
0525e9ae
DG
482 assert(channel);
483
f3ed775e
DG
484 DBG("Flush buffer for channel %s", channel->channel->name);
485
486 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
487 DBG("Flushing channel stream %d", stream->fd);
488 ret = kernctl_buffer_flush(stream->fd);
489 if (ret < 0) {
df0f840b 490 PERROR("ioctl");
f3ed775e
DG
491 ERR("Fail to flush buffer for stream %d (ret: %d)",
492 stream->fd, ret);
493 }
494 }
495
496 return 0;
497}
498
8c0faa1d 499/*
050349bb 500 * Stop tracing session.
8c0faa1d
DG
501 */
502int kernel_stop_session(struct ltt_kernel_session *session)
503{
504 int ret;
505
0525e9ae
DG
506 assert(session);
507
8c0faa1d
DG
508 ret = kernctl_stop_session(session->fd);
509 if (ret < 0) {
510 goto error;
511 }
512
513 DBG("Kernel session stopped");
514
515 return 0;
516
517error:
518 return ret;
519}
520
521/*
050349bb
DG
522 * Open stream of channel, register it to the kernel tracer and add it
523 * to the stream list of the channel.
8c0faa1d 524 *
050349bb 525 * Return the number of created stream. Else, a negative value.
8c0faa1d 526 */
f3ed775e 527int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 528{
00e2e675 529 int ret, count = 0;
8c0faa1d
DG
530 struct ltt_kernel_stream *lks;
531
0525e9ae
DG
532 assert(channel);
533
5a47c6a2 534 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
00e2e675 535 lks = trace_kernel_create_stream(channel->channel->name, count);
8c0faa1d 536 if (lks == NULL) {
799e2c4f
MD
537 ret = close(ret);
538 if (ret) {
539 PERROR("close");
540 }
8c0faa1d
DG
541 goto error;
542 }
543
544 lks->fd = ret;
7b395890
DG
545 /* Prevent fd duplication after execlp() */
546 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
547 if (ret < 0) {
df0f840b 548 PERROR("fcntl session fd");
7b395890
DG
549 }
550
1624d5b7
JD
551 lks->tracefile_size = channel->channel->attr.tracefile_size;
552 lks->tracefile_count = channel->channel->attr.tracefile_count;
553
54012638 554 /* Add stream to channe stream list */
8c0faa1d
DG
555 cds_list_add(&lks->list, &channel->stream_list.head);
556 channel->stream_count++;
8c0faa1d 557
00e2e675
DG
558 /* Increment counter which represent CPU number. */
559 count++;
560
561 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
562 lks->state);
54012638 563 }
8c0faa1d
DG
564
565 return channel->stream_count;
566
567error:
54012638 568 return -1;
8c0faa1d
DG
569}
570
571/*
050349bb 572 * Open the metadata stream and set it to the kernel session.
8c0faa1d 573 */
f3ed775e 574int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
575{
576 int ret;
577
0525e9ae
DG
578 assert(session);
579
8c0faa1d
DG
580 ret = kernctl_create_stream(session->metadata->fd);
581 if (ret < 0) {
df0f840b 582 PERROR("kernel create metadata stream");
8c0faa1d
DG
583 goto error;
584 }
585
586 DBG("Kernel metadata stream created (fd: %d)", ret);
587 session->metadata_stream_fd = ret;
7b395890
DG
588 /* Prevent fd duplication after execlp() */
589 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
590 if (ret < 0) {
df0f840b 591 PERROR("fcntl session fd");
7b395890 592 }
aaf26714
DG
593
594 return 0;
595
596error:
54012638 597 return -1;
aaf26714 598}
2ef84c95
DG
599
600/*
9f19cc17 601 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 602 */
9f19cc17 603ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
2ef84c95 604{
53efb85a 605 int fd, ret;
9f19cc17
DG
606 char *event;
607 size_t nbmem, count = 0;
2ef84c95 608 FILE *fp;
9f19cc17 609 struct lttng_event *elist;
2ef84c95 610
0525e9ae
DG
611 assert(events);
612
2ef84c95
DG
613 fd = kernctl_tracepoint_list(tracer_fd);
614 if (fd < 0) {
df0f840b 615 PERROR("kernel tracepoint list");
2ef84c95
DG
616 goto error;
617 }
618
619 fp = fdopen(fd, "r");
620 if (fp == NULL) {
df0f840b 621 PERROR("kernel tracepoint list fdopen");
61b73b12 622 goto error_fp;
2ef84c95
DG
623 }
624
625 /*
626 * Init memory size counter
627 * See kernel-ctl.h for explanation of this value
628 */
6725fe19 629 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 630 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
631 if (elist == NULL) {
632 PERROR("alloc list events");
633 count = -ENOMEM;
634 goto end;
635 }
2ef84c95 636
53efb85a 637 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 638 if (count >= nbmem) {
3b870559 639 struct lttng_event *new_elist;
53efb85a 640 size_t new_nbmem;
3b870559 641
53efb85a
MD
642 new_nbmem = nbmem << 1;
643 DBG("Reallocating event list from %zu to %zu bytes",
644 nbmem, new_nbmem);
645 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
3b870559 646 if (new_elist == NULL) {
df0f840b 647 PERROR("realloc list events");
3b870559
MD
648 free(event);
649 free(elist);
61b73b12
MD
650 count = -ENOMEM;
651 goto end;
2ef84c95 652 }
53efb85a
MD
653 /* Zero the new memory */
654 memset(new_elist + nbmem, 0,
655 (new_nbmem - nbmem) * sizeof(struct lttng_event));
656 nbmem = new_nbmem;
3b870559 657 elist = new_elist;
2ef84c95 658 }
99497cd0
MD
659 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
660 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 661 elist[count].enabled = -1;
9f19cc17 662 count++;
3b870559 663 free(event);
2ef84c95
DG
664 }
665
9f19cc17 666 *events = elist;
ced2f820 667 DBG("Kernel list events done (%zu events)", count);
61b73b12 668end:
799e2c4f
MD
669 ret = fclose(fp); /* closes both fp and fd */
670 if (ret) {
671 PERROR("fclose");
672 }
9f19cc17 673 return count;
2ef84c95 674
61b73b12 675error_fp:
799e2c4f
MD
676 ret = close(fd);
677 if (ret) {
678 PERROR("close");
679 }
2ef84c95
DG
680error:
681 return -1;
682}
096102bd
DG
683
684/*
685 * Get kernel version and validate it.
686 */
687int kernel_validate_version(int tracer_fd)
688{
689 int ret;
690 struct lttng_kernel_tracer_version version;
691
692 ret = kernctl_tracer_version(tracer_fd, &version);
693 if (ret < 0) {
694 ERR("Failed at getting the lttng-modules version");
695 goto error;
696 }
697
698 /* Validate version */
a62a6556
MD
699 if (version.major != KERN_MODULES_PRE_MAJOR
700 && version.major != KERN_MODULES_MAJOR) {
096102bd 701 goto error_version;
096102bd
DG
702 }
703
a62a6556 704 DBG2("Kernel tracer version validated (major version %d)", version.major);
096102bd
DG
705 return 0;
706
707error_version:
5df0f285 708 ERR("Kernel major version %d is not compatible (supporting <= %d)",
a62a6556 709 version.major, KERN_MODULES_MAJOR)
096102bd
DG
710 ret = -1;
711
712error:
713 return ret;
714}
335a95b7
MD
715
716/*
717 * Kernel work-arounds called at the start of sessiond main().
718 */
719int init_kernel_workarounds(void)
720{
8936c33a 721 int ret;
335a95b7
MD
722 FILE *fp;
723
724 /*
725 * boot_id needs to be read once before being used concurrently
726 * to deal with a Linux kernel race. A fix is proposed for
727 * upstream, but the work-around is needed for older kernels.
728 */
729 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
730 if (!fp) {
731 goto end_boot_id;
732 }
733 while (!feof(fp)) {
734 char buf[37] = "";
735
8936c33a
DG
736 ret = fread(buf, 1, sizeof(buf), fp);
737 if (ret < 0) {
738 /* Ignore error, we don't really care */
739 }
335a95b7 740 }
799e2c4f
MD
741 ret = fclose(fp);
742 if (ret) {
743 PERROR("fclose");
744 }
335a95b7 745end_boot_id:
335a95b7
MD
746 return 0;
747}
2f77fc4b
DG
748
749/*
750 * Complete teardown of a kernel session.
751 */
752void kernel_destroy_session(struct ltt_kernel_session *ksess)
753{
754 if (ksess == NULL) {
755 DBG3("No kernel session when tearing down session");
756 return;
757 }
758
759 DBG("Tearing down kernel session");
760
07b86b52 761 /*
15dc512a
DG
762 * Destroy channels on the consumer if at least one FD has been sent and we
763 * are in no output mode because the streams are in *no* monitor mode so we
764 * have to send a command to clean them up or else they leaked.
07b86b52 765 */
15dc512a 766 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
767 int ret;
768 struct consumer_socket *socket;
769 struct lttng_ht_iter iter;
770
771 /* For each consumer socket. */
772 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
773 socket, node.node) {
774 struct ltt_kernel_channel *chan;
775
776 /* For each channel, ask the consumer to destroy it. */
777 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
778 ret = kernel_consumer_destroy_channel(socket, chan);
779 if (ret < 0) {
780 /* Consumer is probably dead. Use next socket. */
781 continue;
782 }
783 }
784 }
785 }
786
2f77fc4b
DG
787 /* Close any relayd session */
788 consumer_output_send_destroy_relayd(ksess->consumer);
789
790 trace_kernel_destroy_session(ksess);
791}
fb5f35b6
DG
792
793/*
794 * Destroy a kernel channel object. It does not do anything on the tracer side.
795 */
796void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
797{
798 struct ltt_kernel_session *ksess = NULL;
799
800 assert(kchan);
801 assert(kchan->channel);
802
803 DBG3("Kernel destroy channel %s", kchan->channel->name);
804
805 /* Update channel count of associated session. */
806 if (kchan->session) {
807 /* Keep pointer reference so we can update it after the destroy. */
808 ksess = kchan->session;
809 }
810
811 trace_kernel_destroy_channel(kchan);
812
813 /*
814 * At this point the kernel channel is not visible anymore. This is safe
815 * since in order to work on a visible kernel session, the tracing session
816 * lock (ltt_session.lock) MUST be acquired.
817 */
818 if (ksess) {
819 ksess->channel_count--;
820 }
821}
6dc3064a
DG
822
823/*
824 * Take a snapshot for a given kernel session.
825 *
2a06df8d 826 * Return 0 on success or else return a LTTNG_ERR code.
6dc3064a
DG
827 */
828int kernel_snapshot_record(struct ltt_kernel_session *ksess,
5c786ded 829 struct snapshot_output *output, int wait, unsigned int nb_streams)
6dc3064a 830{
2a06df8d 831 int err, ret, saved_metadata_fd;
6dc3064a
DG
832 struct consumer_socket *socket;
833 struct lttng_ht_iter iter;
834 struct ltt_kernel_metadata *saved_metadata;
5c786ded 835 uint64_t max_size_per_stream = 0;
6dc3064a
DG
836
837 assert(ksess);
838 assert(ksess->consumer);
839 assert(output);
840
841 DBG("Kernel snapshot record started");
842
843 /* Save current metadata since the following calls will change it. */
844 saved_metadata = ksess->metadata;
845 saved_metadata_fd = ksess->metadata_stream_fd;
846
847 rcu_read_lock();
848
849 ret = kernel_open_metadata(ksess);
850 if (ret < 0) {
851 ret = LTTNG_ERR_KERN_META_FAIL;
852 goto error;
853 }
854
855 ret = kernel_open_metadata_stream(ksess);
856 if (ret < 0) {
857 ret = LTTNG_ERR_KERN_META_FAIL;
858 goto error_open_stream;
859 }
860
5c786ded
JD
861 if (output->max_size > 0 && nb_streams > 0) {
862 max_size_per_stream = output->max_size / nb_streams;
863 }
864
6dc3064a
DG
865 /* Send metadata to consumer and snapshot everything. */
866 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
867 socket, node.node) {
868 struct consumer_output *saved_output;
869 struct ltt_kernel_channel *chan;
6dc3064a
DG
870
871 /*
872 * Temporarly switch consumer output for our snapshot output. As long
873 * as the session lock is taken, this is safe.
874 */
875 saved_output = ksess->consumer;
876 ksess->consumer = output->consumer;
877
878 pthread_mutex_lock(socket->lock);
879 /* This stream must not be monitored by the consumer. */
07b86b52 880 ret = kernel_consumer_add_metadata(socket, ksess, 0);
6dc3064a 881 pthread_mutex_unlock(socket->lock);
07b86b52 882 /* Put back the saved consumer output into the session. */
6dc3064a
DG
883 ksess->consumer = saved_output;
884 if (ret < 0) {
885 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
886 goto error_consumer;
887 }
888
889 /* For each channel, ask the consumer to snapshot it. */
890 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
5c786ded
JD
891 if (max_size_per_stream &&
892 chan->channel->attr.subbuf_size > max_size_per_stream) {
893 ret = LTTNG_ERR_INVALID;
894 DBG3("Kernel snapshot record maximum stream size %" PRIu64
895 " is smaller than subbuffer size of %" PRIu64,
896 max_size_per_stream, chan->channel->attr.subbuf_size);
2a06df8d
DG
897 (void) kernel_consumer_destroy_metadata(socket,
898 ksess->metadata);
5c786ded
JD
899 goto error_consumer;
900 }
901
07b86b52 902 pthread_mutex_lock(socket->lock);
6dc3064a 903 ret = consumer_snapshot_channel(socket, chan->fd, output, 0,
5c786ded
JD
904 ksess->uid, ksess->gid,
905 DEFAULT_KERNEL_TRACE_DIR, wait,
906 max_size_per_stream);
07b86b52 907 pthread_mutex_unlock(socket->lock);
6dc3064a
DG
908 if (ret < 0) {
909 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
2a06df8d
DG
910 (void) kernel_consumer_destroy_metadata(socket,
911 ksess->metadata);
6dc3064a
DG
912 goto error_consumer;
913 }
914 }
915
916 /* Snapshot metadata, */
07b86b52 917 pthread_mutex_lock(socket->lock);
6dc3064a 918 ret = consumer_snapshot_channel(socket, ksess->metadata->fd, output,
5c786ded
JD
919 1, ksess->uid, ksess->gid,
920 DEFAULT_KERNEL_TRACE_DIR, wait, max_size_per_stream);
07b86b52 921 pthread_mutex_unlock(socket->lock);
6dc3064a
DG
922 if (ret < 0) {
923 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
924 goto error_consumer;
925 }
07b86b52
JD
926
927 /*
928 * The metadata snapshot is done, ask the consumer to destroy it since
929 * it's not monitored on the consumer side.
930 */
931 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
932 }
933
fac41e72
DG
934 ret = LTTNG_OK;
935
6dc3064a
DG
936error_consumer:
937 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
938 err = close(ksess->metadata_stream_fd);
939 if (err < 0) {
6dc3064a
DG
940 PERROR("close snapshot kernel");
941 }
942
943error_open_stream:
944 trace_kernel_destroy_metadata(ksess->metadata);
945error:
946 /* Restore metadata state.*/
947 ksess->metadata = saved_metadata;
948 ksess->metadata_stream_fd = saved_metadata_fd;
949
950 rcu_read_unlock();
951 return ret;
952}
This page took 0.088474 seconds and 4 git commands to generate.