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