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