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