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