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