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