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