lttng: list: replace domain headers with the official names
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
CommitLineData
20fe2104 1/*
ab5be9fa 2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
20fe2104 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
20fe2104 5 *
20fe2104
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
7b395890 9#include <fcntl.h>
20fe2104
DG
10#include <stdlib.h>
11#include <stdio.h>
f34daff7 12#include <string.h>
8c0faa1d 13#include <unistd.h>
77c7c900 14#include <inttypes.h>
7d268848 15#include <sys/types.h>
20fe2104 16
990570ed 17#include <common/common.h>
82b69413 18#include <common/trace-chunk.h>
db758600 19#include <common/kernel-ctl/kernel-ctl.h>
c052142c 20#include <common/kernel-ctl/kernel-ioctl.h>
42224349 21#include <common/sessiond-comm/sessiond-comm.h>
1e307fab 22
7d268848
MD
23#include "lttng-sessiond.h"
24#include "lttng-syscall.h"
2f77fc4b 25#include "consumer.h"
4771f025 26#include "kernel.h"
6dc3064a 27#include "kernel-consumer.h"
096102bd 28#include "kern-modules.h"
834978fd 29#include "utils.h"
5c408ad8 30#include "rotate.h"
7d268848 31#include "modprobe.h"
20fe2104 32
e1f3997a
JD
33/*
34 * Key used to reference a channel between the sessiond and the consumer. This
35 * is only read and updated with the session_list lock held.
36 */
37static uint64_t next_kernel_channel_key;
38
7d268848
MD
39static const char *module_proc_lttng = "/proc/lttng";
40
41static int kernel_tracer_fd = -1;
42
410b78a0
FD
43#include <lttng/userspace-probe.h>
44#include <lttng/userspace-probe-internal.h>
d65106b1 45/*
050349bb 46 * Add context on a kernel channel.
df3c77c8
JG
47 *
48 * Assumes the ownership of ctx.
d65106b1
DG
49 */
50int kernel_add_channel_context(struct ltt_kernel_channel *chan,
645328ae 51 struct ltt_kernel_context *ctx)
d65106b1
DG
52{
53 int ret;
54
0525e9ae
DG
55 assert(chan);
56 assert(ctx);
57
d65106b1 58 DBG("Adding context to channel %s", chan->channel->name);
645328ae 59 ret = kernctl_add_context(chan->fd, &ctx->ctx);
d65106b1 60 if (ret < 0) {
32af2c95 61 switch (-ret) {
1ae5e83e
JD
62 case ENOSYS:
63 /* Exists but not available for this kernel */
64 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
65 goto error;
66 case EEXIST:
b579acd9
DG
67 /* If EEXIST, we just ignore the error */
68 ret = 0;
1ae5e83e
JD
69 goto end;
70 default:
71 PERROR("add context ioctl");
72 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
73 goto error;
b579acd9 74 }
d65106b1 75 }
21ed98c1 76 ret = 0;
d65106b1 77
1ae5e83e 78end:
645328ae 79 cds_list_add_tail(&ctx->list, &chan->ctx_list);
ba985c3a 80 ctx->in_list = true;
df3c77c8 81 ctx = NULL;
d65106b1 82error:
df3c77c8
JG
83 if (ctx) {
84 trace_kernel_destroy_context(ctx);
85 }
d65106b1
DG
86 return ret;
87}
88
20fe2104 89/*
050349bb
DG
90 * Create a new kernel session, register it to the kernel tracer and add it to
91 * the session daemon session.
20fe2104 92 */
7d268848 93int kernel_create_session(struct ltt_session *session)
20fe2104
DG
94{
95 int ret;
96 struct ltt_kernel_session *lks;
97
0525e9ae
DG
98 assert(session);
99
54012638 100 /* Allocate data structure */
dec56f6c 101 lks = trace_kernel_create_session();
20fe2104 102 if (lks == NULL) {
54012638 103 ret = -1;
20fe2104
DG
104 goto error;
105 }
106
54012638 107 /* Kernel tracer session creation */
7d268848 108 ret = kernctl_create_session(kernel_tracer_fd);
20fe2104 109 if (ret < 0) {
df0f840b 110 PERROR("ioctl kernel create session");
20fe2104
DG
111 goto error;
112 }
113
20fe2104 114 lks->fd = ret;
7b395890
DG
115 /* Prevent fd duplication after execlp() */
116 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
117 if (ret < 0) {
df0f840b 118 PERROR("fcntl session fd");
7b395890
DG
119 }
120
53632229 121 lks->id = session->id;
3bd1e081 122 lks->consumer_fds_sent = 0;
8c0faa1d 123 session->kernel_session = lks;
8c0faa1d
DG
124
125 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104 126
8ff4109e
JR
127 /*
128 * This is necessary since the creation time is present in the session
129 * name when it is generated.
130 */
131 if (session->has_auto_generated_name) {
132 ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME);
133 } else {
134 ret = kernctl_session_set_name(lks->fd, session->name);
135 }
136 if (ret) {
137 WARN("Could not set kernel session name for session %" PRIu64 " name: %s",
138 session->id, session->name);
139 }
140
e04b2181
JR
141 ret = kernctl_session_set_creation_time(lks->fd, session->creation_time);
142 if (ret) {
143 WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s",
144 session->id, session->name);
145 }
146
20fe2104
DG
147 return 0;
148
149error:
5f62c685
DG
150 if (lks) {
151 trace_kernel_destroy_session(lks);
d070c424 152 trace_kernel_free_session(lks);
5f62c685 153 }
20fe2104
DG
154 return ret;
155}
156
157/*
050349bb
DG
158 * Create a kernel channel, register it to the kernel tracer and add it to the
159 * kernel session.
20fe2104 160 */
050349bb 161int kernel_create_channel(struct ltt_kernel_session *session,
fdd9eb17 162 struct lttng_channel *chan)
20fe2104
DG
163{
164 int ret;
165 struct ltt_kernel_channel *lkc;
20fe2104 166
0525e9ae
DG
167 assert(session);
168 assert(chan);
0525e9ae 169
54012638 170 /* Allocate kernel channel */
fdd9eb17 171 lkc = trace_kernel_create_channel(chan);
54012638 172 if (lkc == NULL) {
20fe2104
DG
173 goto error;
174 }
175
ecc48a90 176 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
fdd9eb17 177 chan->name, lkc->channel->attr.overwrite,
173af62f
DG
178 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
179 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
ecc48a90 180 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
173af62f 181
54012638 182 /* Kernel tracer channel creation */
f3ed775e 183 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 184 if (ret < 0) {
df0f840b 185 PERROR("ioctl kernel create channel");
20fe2104
DG
186 goto error;
187 }
188
54012638 189 /* Setup the channel fd */
20fe2104 190 lkc->fd = ret;
7b395890
DG
191 /* Prevent fd duplication after execlp() */
192 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
193 if (ret < 0) {
df0f840b 194 PERROR("fcntl session fd");
7b395890
DG
195 }
196
54012638 197 /* Add channel to session */
8c0faa1d
DG
198 cds_list_add(&lkc->list, &session->channel_list.head);
199 session->channel_count++;
fb5f35b6 200 lkc->session = session;
e1f3997a 201 lkc->key = ++next_kernel_channel_key;
20fe2104 202
e1f3997a
JD
203 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
204 lkc->channel->name, lkc->fd, lkc->key);
20fe2104
DG
205
206 return 0;
207
208error:
5f62c685
DG
209 if (lkc) {
210 free(lkc->channel);
211 free(lkc);
212 }
54012638 213 return -1;
20fe2104 214}
f34daff7 215
410b78a0
FD
216/*
217 * Compute the offset of the instrumentation byte in the binary based on the
218 * function probe location using the ELF lookup method.
219 *
220 * Returns 0 on success and set the offset out parameter to the offset of the
221 * elf symbol
222 * Returns -1 on error
223 */
224static
225int extract_userspace_probe_offset_function_elf(
87597c2c 226 const struct lttng_userspace_probe_location *probe_location,
410b78a0
FD
227 struct ltt_kernel_session *session, uint64_t *offset)
228{
229 int fd;
230 int ret = 0;
231 const char *symbol = NULL;
87597c2c 232 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
233 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
234
410b78a0
FD
235 assert(lttng_userspace_probe_location_get_type(probe_location) ==
236 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
237
238 lookup = lttng_userspace_probe_location_get_lookup_method(
239 probe_location);
240 if (!lookup) {
241 ret = -1;
242 goto end;
243 }
244
245 lookup_method_type =
246 lttng_userspace_probe_location_lookup_method_get_type(lookup);
247
248 assert(lookup_method_type ==
249 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
250
251 symbol = lttng_userspace_probe_location_function_get_function_name(
252 probe_location);
253 if (!symbol) {
254 ret = -1;
255 goto end;
256 }
257
258 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
259 if (fd < 0) {
260 ret = -1;
261 goto end;
262 }
263
264 ret = run_as_extract_elf_symbol_offset(fd, symbol, session->uid,
265 session->gid, offset);
266 if (ret < 0) {
267 DBG("userspace probe offset calculation failed for "
268 "function %s", symbol);
269 goto end;
270 }
271
272 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset));
273end:
274 return ret;
275}
276
277/*
278 * Compute the offsets of the instrumentation bytes in the binary based on the
279 * tracepoint probe location using the SDT lookup method. This function
280 * allocates the offsets buffer, the caller must free it.
281 *
282 * Returns 0 on success and set the offset out parameter to the offsets of the
283 * SDT tracepoint.
284 * Returns -1 on error.
285 */
286static
287int extract_userspace_probe_offset_tracepoint_sdt(
87597c2c 288 const struct lttng_userspace_probe_location *probe_location,
410b78a0
FD
289 struct ltt_kernel_session *session, uint64_t **offsets,
290 uint32_t *offsets_count)
291{
292 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
87597c2c 293 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
410b78a0
FD
294 const char *probe_name = NULL, *provider_name = NULL;
295 int ret = 0;
296 int fd, i;
297
298 assert(lttng_userspace_probe_location_get_type(probe_location) ==
299 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
300
301 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
302 if (!lookup) {
303 ret = -1;
304 goto end;
305 }
306
307 lookup_method_type =
308 lttng_userspace_probe_location_lookup_method_get_type(lookup);
309
310 assert(lookup_method_type ==
311 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
312
313
314 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
315 probe_location);
316 if (!probe_name) {
317 ret = -1;
318 goto end;
319 }
320
321 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
322 probe_location);
323 if (!provider_name) {
324 ret = -1;
325 goto end;
326 }
327
328 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
329 if (fd < 0) {
330 ret = -1;
331 goto end;
332 }
333
334 ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name,
335 session->uid, session->gid, offsets, offsets_count);
336 if (ret < 0) {
337 DBG("userspace probe offset calculation failed for sdt "
338 "probe %s:%s", provider_name, probe_name);
339 goto end;
340 }
341
342 if (*offsets_count == 0) {
343 DBG("no userspace probe offset found");
344 goto end;
345 }
346
347 DBG("%u userspace probe SDT offsets found for %s:%s at:",
348 *offsets_count, provider_name, probe_name);
349 for (i = 0; i < *offsets_count; i++) {
350 DBG("\t0x%jd", (intmax_t)((*offsets)[i]));
351 }
352end:
353 return ret;
354}
355
356/*
357 * Extract the offsets of the instrumentation point for the different lookup
358 * methods.
359 */
360static
361int userspace_probe_add_callsites(struct lttng_event *ev,
362 struct ltt_kernel_session *session, int fd)
363{
87597c2c 364 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
410b78a0 365 enum lttng_userspace_probe_location_lookup_method_type type;
87597c2c 366 const struct lttng_userspace_probe_location *location = NULL;
410b78a0
FD
367 int ret;
368
369 assert(ev);
370 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
371
372 location = lttng_event_get_userspace_probe_location(ev);
373 if (!location) {
374 ret = -1;
375 goto end;
376 }
377 lookup_method =
378 lttng_userspace_probe_location_get_lookup_method(location);
379 if (!lookup_method) {
380 ret = -1;
381 goto end;
382 }
383
384 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
385 switch (type) {
386 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
387 {
388 struct lttng_kernel_event_callsite callsite;
389 uint64_t offset;
390
391 ret = extract_userspace_probe_offset_function_elf(location, session, &offset);
392 if (ret) {
393 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
394 goto end;
395 }
396
397 callsite.u.uprobe.offset = offset;
398 ret = kernctl_add_callsite(fd, &callsite);
399 if (ret) {
400 WARN("Adding callsite to userspace probe "
401 "event %s failed.", ev->name);
402 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
403 goto end;
404 }
405 break;
406 }
407 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
408 {
409 int i;
410 uint64_t *offsets = NULL;
411 uint32_t offsets_count;
412 struct lttng_kernel_event_callsite callsite;
413
414 /*
415 * This call allocates the offsets buffer. This buffer must be freed
416 * by the caller
417 */
418 ret = extract_userspace_probe_offset_tracepoint_sdt(location, session,
419 &offsets, &offsets_count);
420 if (ret) {
421 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
422 goto end;
423 }
424 for (i = 0; i < offsets_count; i++) {
425 callsite.u.uprobe.offset = offsets[i];
426 ret = kernctl_add_callsite(fd, &callsite);
427 if (ret) {
428 WARN("Adding callsite to userspace probe "
429 "event %s failed.", ev->name);
430 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
431 free(offsets);
432 goto end;
433 }
434 }
435 free(offsets);
436 break;
437 }
438 default:
439 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
440 goto end;
441 }
442end:
443 return ret;
444}
445
f34daff7 446/*
050349bb
DG
447 * Create a kernel event, enable it to the kernel tracer and add it to the
448 * channel event list of the kernel session.
49d21f93 449 * We own filter_expression and filter.
f34daff7 450 */
050349bb 451int kernel_create_event(struct lttng_event *ev,
00a62084
MD
452 struct ltt_kernel_channel *channel,
453 char *filter_expression,
454 struct lttng_filter_bytecode *filter)
f34daff7 455{
71a3bb01
FD
456 int err, fd;
457 enum lttng_error_code ret;
f34daff7 458 struct ltt_kernel_event *event;
f34daff7 459
0525e9ae
DG
460 assert(ev);
461 assert(channel);
462
a969e101 463 /* We pass ownership of filter_expression and filter */
71a3bb01
FD
464 ret = trace_kernel_create_event(ev, filter_expression,
465 filter, &event);
466 if (ret != LTTNG_OK) {
f34daff7
DG
467 goto error;
468 }
469
71a3bb01
FD
470 fd = kernctl_create_event(channel->fd, event->event);
471 if (fd < 0) {
472 switch (-fd) {
bd29c13d 473 case EEXIST:
71a3bb01 474 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
475 break;
476 case ENOSYS:
477 WARN("Event type not implemented");
71a3bb01 478 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d 479 break;
8197a339
DG
480 case ENOENT:
481 WARN("Event %s not found!", ev->name);
71a3bb01 482 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
8197a339 483 break;
bd29c13d 484 default:
71a3bb01 485 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
d87bfb32
DG
486 PERROR("create event ioctl");
487 }
e953ef25 488 goto free_event;
8c0faa1d 489 }
f34daff7 490
d0ae4ea8 491 event->type = ev->type;
71a3bb01 492 event->fd = fd;
7b395890 493 /* Prevent fd duplication after execlp() */
71a3bb01
FD
494 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
495 if (err < 0) {
df0f840b 496 PERROR("fcntl session fd");
7b395890
DG
497 }
498
00a62084 499 if (filter) {
71a3bb01
FD
500 err = kernctl_filter(event->fd, filter);
501 if (err < 0) {
502 switch (-err) {
503 case ENOMEM:
504 ret = LTTNG_ERR_FILTER_NOMEM;
505 break;
506 default:
507 ret = LTTNG_ERR_FILTER_INVAL;
508 break;
509 }
00a62084
MD
510 goto filter_error;
511 }
512 }
513
dcabc190
FD
514 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
515 ret = userspace_probe_add_callsites(ev, channel->session, event->fd);
516 if (ret) {
517 goto add_callsite_error;
518 }
519 }
520
71a3bb01
FD
521 err = kernctl_enable(event->fd);
522 if (err < 0) {
523 switch (-err) {
00a62084
MD
524 case EEXIST:
525 ret = LTTNG_ERR_KERN_EVENT_EXIST;
526 break;
527 default:
528 PERROR("enable kernel event");
71a3bb01 529 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
00a62084
MD
530 break;
531 }
532 goto enable_error;
533 }
534
f3ed775e
DG
535 /* Add event to event list */
536 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
537 channel->event_count++;
538
e953ef25
DG
539 DBG("Event %s created (fd: %d)", ev->name, event->fd);
540
541 return 0;
542
dcabc190 543add_callsite_error:
00a62084
MD
544enable_error:
545filter_error:
546 {
547 int closeret;
548
549 closeret = close(event->fd);
550 if (closeret) {
551 PERROR("close event fd");
552 }
553 }
e953ef25
DG
554free_event:
555 free(event);
556error:
d87bfb32 557 return ret;
e953ef25
DG
558}
559
26cc6b4e 560/*
050349bb 561 * Disable a kernel channel.
26cc6b4e
DG
562 */
563int kernel_disable_channel(struct ltt_kernel_channel *chan)
564{
565 int ret;
566
0525e9ae
DG
567 assert(chan);
568
26cc6b4e
DG
569 ret = kernctl_disable(chan->fd);
570 if (ret < 0) {
df0f840b 571 PERROR("disable chan ioctl");
26cc6b4e
DG
572 goto error;
573 }
574
575 chan->enabled = 0;
e1f3997a
JD
576 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
577 chan->channel->name, chan->fd, chan->key);
26cc6b4e
DG
578
579 return 0;
580
581error:
582 return ret;
583}
584
d36b8583 585/*
050349bb 586 * Enable a kernel channel.
d36b8583
DG
587 */
588int kernel_enable_channel(struct ltt_kernel_channel *chan)
589{
590 int ret;
591
0525e9ae
DG
592 assert(chan);
593
d36b8583 594 ret = kernctl_enable(chan->fd);
32af2c95 595 if (ret < 0 && ret != -EEXIST) {
df0f840b 596 PERROR("Enable kernel chan");
d36b8583
DG
597 goto error;
598 }
599
600 chan->enabled = 1;
e1f3997a
JD
601 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
602 chan->channel->name, chan->fd, chan->key);
d36b8583
DG
603
604 return 0;
605
606error:
607 return ret;
608}
609
19e70852 610/*
050349bb 611 * Enable a kernel event.
19e70852
DG
612 */
613int kernel_enable_event(struct ltt_kernel_event *event)
614{
615 int ret;
616
0525e9ae
DG
617 assert(event);
618
19e70852 619 ret = kernctl_enable(event->fd);
42224349 620 if (ret < 0) {
32af2c95 621 switch (-ret) {
42224349 622 case EEXIST:
f73fabfd 623 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
624 break;
625 default:
626 PERROR("enable kernel event");
627 break;
628 }
19e70852
DG
629 goto error;
630 }
631
632 event->enabled = 1;
633 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
634
635 return 0;
636
637error:
d36b8583 638 return ret;
19e70852
DG
639}
640
e953ef25 641/*
050349bb 642 * Disable a kernel event.
e953ef25 643 */
19e70852 644int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
645{
646 int ret;
19e70852 647
0525e9ae
DG
648 assert(event);
649
19e70852 650 ret = kernctl_disable(event->fd);
42224349 651 if (ret < 0) {
32af2c95 652 switch (-ret) {
42224349 653 case EEXIST:
f73fabfd 654 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
655 break;
656 default:
657 PERROR("disable kernel event");
658 break;
659 }
19e70852 660 goto error;
e953ef25 661 }
f3ed775e 662
19e70852
DG
663 event->enabled = 0;
664 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
665
f34daff7
DG
666 return 0;
667
668error:
d36b8583 669 return ret;
f34daff7 670}
aaf26714 671
55c9e7ca
JR
672static struct lttng_tracker_list *get_id_tracker_list(
673 struct ltt_kernel_session *session,
674 enum lttng_tracker_type tracker_type)
675{
676 switch (tracker_type) {
677 case LTTNG_TRACKER_PID:
678 return session->tracker_list_pid;
679 case LTTNG_TRACKER_VPID:
680 return session->tracker_list_vpid;
681 case LTTNG_TRACKER_UID:
682 return session->tracker_list_uid;
683 case LTTNG_TRACKER_VUID:
684 return session->tracker_list_vuid;
685 case LTTNG_TRACKER_GID:
686 return session->tracker_list_gid;
687 case LTTNG_TRACKER_VGID:
688 return session->tracker_list_vgid;
689 default:
690 return NULL;
691 }
692}
6e911cad 693
55c9e7ca
JR
694int kernel_track_id(enum lttng_tracker_type tracker_type,
695 struct ltt_kernel_session *session,
696 const struct lttng_tracker_id *id)
ccf10263 697{
55c9e7ca
JR
698 int ret, value;
699 struct lttng_tracker_list *tracker_list;
a7a533cd 700 struct lttng_tracker_ids *saved_ids;
7c493d31 701
55c9e7ca
JR
702 ret = lttng_tracker_id_lookup_string(tracker_type, id, &value);
703 if (ret != LTTNG_OK) {
704 return ret;
7c493d31 705 }
55c9e7ca
JR
706
707 tracker_list = get_id_tracker_list(session, tracker_type);
708 if (!tracker_list) {
7c493d31 709 return LTTNG_ERR_INVALID;
7c493d31 710 }
ccf10263 711
55c9e7ca 712 /* Save list for restore on error. */
a7a533cd
JR
713 ret = lttng_tracker_id_get_list(tracker_list, &saved_ids);
714 if (ret != LTTNG_OK) {
55c9e7ca
JR
715 return LTTNG_ERR_INVALID;
716 }
7c493d31 717
55c9e7ca
JR
718 /* Add to list. */
719 ret = lttng_tracker_list_add(tracker_list, id);
720 if (ret != LTTNG_OK) {
721 goto end;
7c493d31 722 }
55c9e7ca
JR
723
724 switch (tracker_type) {
725 case LTTNG_TRACKER_PID:
726 DBG("Kernel track PID %d for session id %" PRIu64 ".", value,
727 session->id);
728 ret = kernctl_track_pid(session->fd, value);
729 if (!ret) {
730 ret = LTTNG_OK;
731 goto end;
732 }
733 break;
734 case LTTNG_TRACKER_VPID:
735 DBG("Kernel track VPID %d for session id %" PRIu64 ".", value,
736 session->id);
737 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VPID, value);
738 if (!ret) {
739 ret = LTTNG_OK;
740 goto end;
741 }
742 break;
743 case LTTNG_TRACKER_UID:
744 DBG("Kernel track UID %d for session id %" PRIu64 ".", value,
745 session->id);
746 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_UID, value);
747 if (!ret) {
748 ret = LTTNG_OK;
749 goto end;
750 }
751 break;
752 case LTTNG_TRACKER_GID:
753 DBG("Kernel track GID %d for session id %" PRIu64 ".", value,
754 session->id);
755 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_GID, value);
756 if (!ret) {
757 ret = LTTNG_OK;
758 goto end;
759 }
760 break;
761 case LTTNG_TRACKER_VUID:
762 DBG("Kernel track VUID %d for session id %" PRIu64 ".", value,
763 session->id);
764 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VUID, value);
765 if (!ret) {
766 ret = LTTNG_OK;
767 goto end;
768 }
769 break;
770 case LTTNG_TRACKER_VGID:
771 DBG("Kernel track VGID %d for session id %" PRIu64 ".", value,
772 session->id);
773 ret = kernctl_track_id(session->fd, LTTNG_TRACKER_VGID, value);
774 if (!ret) {
775 ret = LTTNG_OK;
776 goto end;
777 }
778 break;
779 default:
780 ret = -EINVAL;
781 break;
782 }
783
784 /* Error handling. */
32af2c95 785 switch (-ret) {
7c493d31 786 case EINVAL:
55c9e7ca
JR
787 ret = LTTNG_ERR_INVALID;
788 break;
7c493d31 789 case ENOMEM:
55c9e7ca
JR
790 ret = LTTNG_ERR_NOMEM;
791 break;
792 case EEXIST:
793 ret = LTTNG_ERR_ID_TRACKED;
794 break;
7c493d31 795 default:
55c9e7ca
JR
796 ret = LTTNG_ERR_UNK;
797 break;
798 }
799
a7a533cd 800 if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
55c9e7ca 801 ERR("Error on tracker add error handling.\n");
7c493d31 802 }
55c9e7ca 803end:
a7a533cd 804 lttng_tracker_ids_destroy(saved_ids);
55c9e7ca 805 return ret;
ccf10263
MD
806}
807
55c9e7ca
JR
808int kernel_untrack_id(enum lttng_tracker_type tracker_type,
809 struct ltt_kernel_session *session,
810 const struct lttng_tracker_id *id)
a5dfbb9d 811{
55c9e7ca
JR
812 int ret, value;
813 struct lttng_tracker_list *tracker_list;
a7a533cd 814 struct lttng_tracker_ids *saved_ids;
a5dfbb9d 815
55c9e7ca
JR
816 ret = lttng_tracker_id_lookup_string(tracker_type, id, &value);
817 if (ret != LTTNG_OK) {
818 return ret;
a5dfbb9d
MD
819 }
820
55c9e7ca
JR
821 tracker_list = get_id_tracker_list(session, tracker_type);
822 if (!tracker_list) {
823 return LTTNG_ERR_INVALID;
a5dfbb9d 824 }
55c9e7ca 825 /* Save list for restore on error. */
a7a533cd
JR
826 ret = lttng_tracker_id_get_list(tracker_list, &saved_ids);
827 if (ret != LTTNG_OK) {
55c9e7ca
JR
828 return LTTNG_ERR_INVALID;
829 }
830 /* Remove from list. */
831 ret = lttng_tracker_list_remove(tracker_list, id);
832 if (ret != LTTNG_OK) {
a5dfbb9d
MD
833 goto end;
834 }
835
55c9e7ca
JR
836 switch (tracker_type) {
837 case LTTNG_TRACKER_PID:
838 DBG("Kernel untrack PID %d for session id %" PRIu64 ".", value,
839 session->id);
840 ret = kernctl_untrack_pid(session->fd, value);
841 if (!ret) {
842 ret = LTTNG_OK;
843 goto end;
844 }
845 break;
846 case LTTNG_TRACKER_VPID:
847 DBG("Kernel untrack VPID %d for session id %" PRIu64 ".", value,
848 session->id);
849 ret = kernctl_untrack_id(
850 session->fd, LTTNG_TRACKER_VPID, value);
851 if (!ret) {
852 ret = LTTNG_OK;
853 goto end;
854 }
855 break;
856 case LTTNG_TRACKER_UID:
857 DBG("Kernel untrack UID %d for session id %" PRIu64 ".", value,
858 session->id);
859 ret = kernctl_untrack_id(session->fd, LTTNG_TRACKER_UID, value);
860 if (!ret) {
861 ret = LTTNG_OK;
862 goto end;
863 }
864 break;
865 case LTTNG_TRACKER_GID:
866 DBG("Kernel untrack GID %d for session id %" PRIu64 ".", value,
867 session->id);
868 ret = kernctl_untrack_id(session->fd, LTTNG_TRACKER_GID, value);
869 if (!ret) {
870 ret = LTTNG_OK;
871 goto end;
872 }
873 break;
874 case LTTNG_TRACKER_VUID:
875 DBG("Kernel untrack VUID %d for session id %" PRIu64 ".", value,
876 session->id);
877 ret = kernctl_untrack_id(
878 session->fd, LTTNG_TRACKER_VUID, value);
879 if (!ret) {
880 ret = LTTNG_OK;
881 goto end;
882 }
883 break;
884 case LTTNG_TRACKER_VGID:
885 DBG("Kernel untrack VGID %d for session id %" PRIu64 ".", value,
886 session->id);
887 ret = kernctl_untrack_id(
888 session->fd, LTTNG_TRACKER_VGID, value);
889 if (!ret) {
890 ret = LTTNG_OK;
891 goto end;
a5dfbb9d 892 }
55c9e7ca
JR
893 break;
894 default:
895 ret = -EINVAL;
896 break;
897 }
898
899 /* Error handling. */
900 switch (-ret) {
901 case EINVAL:
902 ret = LTTNG_ERR_INVALID;
903 break;
904 case ENOMEM:
905 ret = LTTNG_ERR_NOMEM;
906 break;
907 case EEXIST:
908 ret = LTTNG_ERR_ID_TRACKED;
909 break;
910 default:
911 ret = LTTNG_ERR_UNK;
912 break;
a5dfbb9d
MD
913 }
914
a7a533cd 915 if (lttng_tracker_id_set_list(tracker_list, saved_ids) != LTTNG_OK) {
55c9e7ca
JR
916 ERR("Error on tracker remove error handling.\n");
917 }
a5dfbb9d 918end:
a7a533cd 919 lttng_tracker_ids_destroy(saved_ids);
55c9e7ca
JR
920 return ret;
921}
a5dfbb9d 922
55c9e7ca
JR
923/*
924 * Called with session lock held.
925 */
a7a533cd 926int kernel_list_tracker_ids(enum lttng_tracker_type tracker_type,
55c9e7ca 927 struct ltt_kernel_session *session,
a7a533cd 928 struct lttng_tracker_ids **_ids)
55c9e7ca 929{
a7a533cd 930 int ret = 0;
55c9e7ca
JR
931 struct lttng_tracker_list *tracker_list;
932
933 tracker_list = get_id_tracker_list(session, tracker_type);
934 if (!tracker_list) {
a7a533cd
JR
935 ret = -LTTNG_ERR_INVALID;
936 goto end;
a5dfbb9d 937 }
a7a533cd
JR
938
939 ret = lttng_tracker_id_get_list(tracker_list, _ids);
940 if (ret != LTTNG_OK) {
941 ret = -LTTNG_ERR_INVALID;
942 goto end;
943 }
944
945end:
946 return ret;
a5dfbb9d
MD
947}
948
aaf26714 949/*
050349bb
DG
950 * Create kernel metadata, open from the kernel tracer and add it to the
951 * kernel session.
aaf26714 952 */
a4b92340 953int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
954{
955 int ret;
74024a21 956 struct ltt_kernel_metadata *lkm = NULL;
aaf26714 957
0525e9ae
DG
958 assert(session);
959
54012638 960 /* Allocate kernel metadata */
a4b92340 961 lkm = trace_kernel_create_metadata();
54012638 962 if (lkm == NULL) {
aaf26714
DG
963 goto error;
964 }
965
54012638 966 /* Kernel tracer metadata creation */
f3ed775e 967 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 968 if (ret < 0) {
74024a21 969 goto error_open;
aaf26714
DG
970 }
971
8c0faa1d 972 lkm->fd = ret;
d40f0359 973 lkm->key = ++next_kernel_channel_key;
7b395890
DG
974 /* Prevent fd duplication after execlp() */
975 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
976 if (ret < 0) {
df0f840b 977 PERROR("fcntl session fd");
7b395890
DG
978 }
979
aaf26714 980 session->metadata = lkm;
8c0faa1d 981
00e2e675 982 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
983
984 return 0;
985
74024a21
DG
986error_open:
987 trace_kernel_destroy_metadata(lkm);
8c0faa1d 988error:
54012638 989 return -1;
8c0faa1d
DG
990}
991
992/*
050349bb 993 * Start tracing session.
8c0faa1d
DG
994 */
995int kernel_start_session(struct ltt_kernel_session *session)
996{
997 int ret;
998
0525e9ae
DG
999 assert(session);
1000
8c0faa1d
DG
1001 ret = kernctl_start_session(session->fd);
1002 if (ret < 0) {
df0f840b 1003 PERROR("ioctl start session");
8c0faa1d
DG
1004 goto error;
1005 }
1006
1007 DBG("Kernel session started");
1008
1009 return 0;
1010
1011error:
1012 return ret;
1013}
1014
f3ed775e 1015/*
050349bb 1016 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e 1017 */
7d268848 1018void kernel_wait_quiescent(void)
f3ed775e
DG
1019{
1020 int ret;
7d268848 1021 int fd = kernel_tracer_fd;
f3ed775e
DG
1022
1023 DBG("Kernel quiescent wait on %d", fd);
1024
1025 ret = kernctl_wait_quiescent(fd);
1026 if (ret < 0) {
df0f840b 1027 PERROR("wait quiescent ioctl");
f3ed775e
DG
1028 ERR("Kernel quiescent wait failed");
1029 }
1030}
1031
1032/*
f3ed775e
DG
1033 * Force flush buffer of metadata.
1034 */
1035int kernel_metadata_flush_buffer(int fd)
1036{
1037 int ret;
1038
169d2cb7
DG
1039 DBG("Kernel flushing metadata buffer on fd %d", fd);
1040
f3ed775e
DG
1041 ret = kernctl_buffer_flush(fd);
1042 if (ret < 0) {
00e2e675 1043 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
1044 }
1045
1046 return 0;
1047}
1048
1049/*
050349bb 1050 * Force flush buffer for channel.
f3ed775e
DG
1051 */
1052int kernel_flush_buffer(struct ltt_kernel_channel *channel)
1053{
1054 int ret;
1055 struct ltt_kernel_stream *stream;
1056
0525e9ae
DG
1057 assert(channel);
1058
f3ed775e
DG
1059 DBG("Flush buffer for channel %s", channel->channel->name);
1060
1061 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
1062 DBG("Flushing channel stream %d", stream->fd);
1063 ret = kernctl_buffer_flush(stream->fd);
1064 if (ret < 0) {
df0f840b 1065 PERROR("ioctl");
f3ed775e
DG
1066 ERR("Fail to flush buffer for stream %d (ret: %d)",
1067 stream->fd, ret);
1068 }
1069 }
1070
1071 return 0;
1072}
1073
8c0faa1d 1074/*
050349bb 1075 * Stop tracing session.
8c0faa1d
DG
1076 */
1077int kernel_stop_session(struct ltt_kernel_session *session)
1078{
1079 int ret;
1080
0525e9ae
DG
1081 assert(session);
1082
8c0faa1d
DG
1083 ret = kernctl_stop_session(session->fd);
1084 if (ret < 0) {
1085 goto error;
1086 }
1087
1088 DBG("Kernel session stopped");
1089
1090 return 0;
1091
1092error:
1093 return ret;
1094}
1095
1096/*
050349bb
DG
1097 * Open stream of channel, register it to the kernel tracer and add it
1098 * to the stream list of the channel.
8c0faa1d 1099 *
1cfb4b98
MD
1100 * Note: given that the streams may appear in random order wrt CPU
1101 * number (e.g. cpu hotplug), the index value of the stream number in
1102 * the stream name is not necessarily linked to the CPU number.
1103 *
050349bb 1104 * Return the number of created stream. Else, a negative value.
8c0faa1d 1105 */
f3ed775e 1106int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 1107{
1cfb4b98 1108 int ret;
8c0faa1d
DG
1109 struct ltt_kernel_stream *lks;
1110
0525e9ae
DG
1111 assert(channel);
1112
5a47c6a2 1113 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1cfb4b98
MD
1114 lks = trace_kernel_create_stream(channel->channel->name,
1115 channel->stream_count);
8c0faa1d 1116 if (lks == NULL) {
799e2c4f
MD
1117 ret = close(ret);
1118 if (ret) {
1119 PERROR("close");
1120 }
8c0faa1d
DG
1121 goto error;
1122 }
1123
1124 lks->fd = ret;
7b395890
DG
1125 /* Prevent fd duplication after execlp() */
1126 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
1127 if (ret < 0) {
df0f840b 1128 PERROR("fcntl session fd");
7b395890
DG
1129 }
1130
1624d5b7
JD
1131 lks->tracefile_size = channel->channel->attr.tracefile_size;
1132 lks->tracefile_count = channel->channel->attr.tracefile_count;
1133
1cfb4b98 1134 /* Add stream to channel stream list */
8c0faa1d
DG
1135 cds_list_add(&lks->list, &channel->stream_list.head);
1136 channel->stream_count++;
8c0faa1d 1137
00e2e675
DG
1138 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
1139 lks->state);
54012638 1140 }
8c0faa1d
DG
1141
1142 return channel->stream_count;
1143
1144error:
54012638 1145 return -1;
8c0faa1d
DG
1146}
1147
1148/*
050349bb 1149 * Open the metadata stream and set it to the kernel session.
8c0faa1d 1150 */
f3ed775e 1151int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
1152{
1153 int ret;
1154
0525e9ae
DG
1155 assert(session);
1156
8c0faa1d
DG
1157 ret = kernctl_create_stream(session->metadata->fd);
1158 if (ret < 0) {
df0f840b 1159 PERROR("kernel create metadata stream");
8c0faa1d
DG
1160 goto error;
1161 }
1162
1163 DBG("Kernel metadata stream created (fd: %d)", ret);
1164 session->metadata_stream_fd = ret;
7b395890
DG
1165 /* Prevent fd duplication after execlp() */
1166 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1167 if (ret < 0) {
df0f840b 1168 PERROR("fcntl session fd");
7b395890 1169 }
aaf26714
DG
1170
1171 return 0;
1172
1173error:
54012638 1174 return -1;
aaf26714 1175}
2ef84c95
DG
1176
1177/*
9f19cc17 1178 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 1179 */
7d268848 1180ssize_t kernel_list_events(struct lttng_event **events)
2ef84c95 1181{
53efb85a 1182 int fd, ret;
9f19cc17
DG
1183 char *event;
1184 size_t nbmem, count = 0;
2ef84c95 1185 FILE *fp;
9f19cc17 1186 struct lttng_event *elist;
2ef84c95 1187
0525e9ae
DG
1188 assert(events);
1189
7d268848 1190 fd = kernctl_tracepoint_list(kernel_tracer_fd);
2ef84c95 1191 if (fd < 0) {
df0f840b 1192 PERROR("kernel tracepoint list");
2ef84c95
DG
1193 goto error;
1194 }
1195
1196 fp = fdopen(fd, "r");
1197 if (fp == NULL) {
df0f840b 1198 PERROR("kernel tracepoint list fdopen");
61b73b12 1199 goto error_fp;
2ef84c95
DG
1200 }
1201
1202 /*
1203 * Init memory size counter
1204 * See kernel-ctl.h for explanation of this value
1205 */
6725fe19 1206 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
ba7f0ae5 1207 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
3b870559
MD
1208 if (elist == NULL) {
1209 PERROR("alloc list events");
1210 count = -ENOMEM;
1211 goto end;
1212 }
2ef84c95 1213
53efb85a 1214 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 1215 if (count >= nbmem) {
3b870559 1216 struct lttng_event *new_elist;
53efb85a 1217 size_t new_nbmem;
3b870559 1218
53efb85a
MD
1219 new_nbmem = nbmem << 1;
1220 DBG("Reallocating event list from %zu to %zu bytes",
1221 nbmem, new_nbmem);
1222 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
3b870559 1223 if (new_elist == NULL) {
df0f840b 1224 PERROR("realloc list events");
3b870559
MD
1225 free(event);
1226 free(elist);
61b73b12
MD
1227 count = -ENOMEM;
1228 goto end;
2ef84c95 1229 }
53efb85a
MD
1230 /* Zero the new memory */
1231 memset(new_elist + nbmem, 0,
1232 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1233 nbmem = new_nbmem;
3b870559 1234 elist = new_elist;
2ef84c95 1235 }
99497cd0
MD
1236 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1237 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 1238 elist[count].enabled = -1;
9f19cc17 1239 count++;
3b870559 1240 free(event);
2ef84c95
DG
1241 }
1242
9f19cc17 1243 *events = elist;
ced2f820 1244 DBG("Kernel list events done (%zu events)", count);
61b73b12 1245end:
799e2c4f
MD
1246 ret = fclose(fp); /* closes both fp and fd */
1247 if (ret) {
1248 PERROR("fclose");
1249 }
9f19cc17 1250 return count;
2ef84c95 1251
61b73b12 1252error_fp:
799e2c4f
MD
1253 ret = close(fd);
1254 if (ret) {
1255 PERROR("close");
1256 }
2ef84c95
DG
1257error:
1258 return -1;
1259}
096102bd
DG
1260
1261/*
1262 * Get kernel version and validate it.
1263 */
7d268848 1264int kernel_validate_version(struct lttng_kernel_tracer_version *version,
88076e89 1265 struct lttng_kernel_tracer_abi_version *abi_version)
096102bd
DG
1266{
1267 int ret;
096102bd 1268
7d268848 1269 ret = kernctl_tracer_version(kernel_tracer_fd, version);
096102bd 1270 if (ret < 0) {
521dd134 1271 ERR("Failed to retrieve the lttng-modules version");
096102bd
DG
1272 goto error;
1273 }
1274
1275 /* Validate version */
88076e89 1276 if (version->major != VERSION_MAJOR) {
c052142c 1277 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
88076e89 1278 version->major, VERSION_MAJOR);
096102bd 1279 goto error_version;
096102bd 1280 }
7d268848 1281 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
c052142c 1282 if (ret < 0) {
521dd134 1283 ERR("Failed to retrieve lttng-modules ABI version");
c052142c
MD
1284 goto error;
1285 }
88076e89 1286 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
521dd134 1287 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
88076e89 1288 abi_version->major, abi_version->minor,
c052142c
MD
1289 LTTNG_MODULES_ABI_MAJOR_VERSION);
1290 goto error;
1291 }
1292 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
88076e89
JD
1293 version->major, version->minor,
1294 abi_version->major, abi_version->minor);
096102bd
DG
1295 return 0;
1296
1297error_version:
096102bd
DG
1298 ret = -1;
1299
1300error:
521dd134 1301 ERR("Kernel tracer version check failed; kernel tracing will not be available");
096102bd
DG
1302 return ret;
1303}
335a95b7
MD
1304
1305/*
1306 * Kernel work-arounds called at the start of sessiond main().
1307 */
1308int init_kernel_workarounds(void)
1309{
8936c33a 1310 int ret;
335a95b7
MD
1311 FILE *fp;
1312
1313 /*
1314 * boot_id needs to be read once before being used concurrently
1315 * to deal with a Linux kernel race. A fix is proposed for
1316 * upstream, but the work-around is needed for older kernels.
1317 */
1318 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1319 if (!fp) {
1320 goto end_boot_id;
1321 }
1322 while (!feof(fp)) {
1323 char buf[37] = "";
1324
8936c33a
DG
1325 ret = fread(buf, 1, sizeof(buf), fp);
1326 if (ret < 0) {
1327 /* Ignore error, we don't really care */
1328 }
335a95b7 1329 }
799e2c4f
MD
1330 ret = fclose(fp);
1331 if (ret) {
1332 PERROR("fclose");
1333 }
335a95b7 1334end_boot_id:
335a95b7
MD
1335 return 0;
1336}
2f77fc4b
DG
1337
1338/*
d070c424 1339 * Teardown of a kernel session, keeping data required by destroy notifiers.
2f77fc4b
DG
1340 */
1341void kernel_destroy_session(struct ltt_kernel_session *ksess)
1342{
82b69413
JG
1343 struct lttng_trace_chunk *trace_chunk;
1344
2f77fc4b
DG
1345 if (ksess == NULL) {
1346 DBG3("No kernel session when tearing down session");
1347 return;
1348 }
1349
1350 DBG("Tearing down kernel session");
82b69413 1351 trace_chunk = ksess->current_trace_chunk;
2f77fc4b 1352
07b86b52 1353 /*
15dc512a
DG
1354 * Destroy channels on the consumer if at least one FD has been sent and we
1355 * are in no output mode because the streams are in *no* monitor mode so we
1356 * have to send a command to clean them up or else they leaked.
07b86b52 1357 */
15dc512a 1358 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
1359 int ret;
1360 struct consumer_socket *socket;
1361 struct lttng_ht_iter iter;
1362
1363 /* For each consumer socket. */
d069d577 1364 rcu_read_lock();
07b86b52
JD
1365 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1366 socket, node.node) {
1367 struct ltt_kernel_channel *chan;
1368
1369 /* For each channel, ask the consumer to destroy it. */
1370 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1371 ret = kernel_consumer_destroy_channel(socket, chan);
1372 if (ret < 0) {
1373 /* Consumer is probably dead. Use next socket. */
1374 continue;
1375 }
1376 }
1377 }
d069d577 1378 rcu_read_unlock();
07b86b52
JD
1379 }
1380
2f77fc4b
DG
1381 /* Close any relayd session */
1382 consumer_output_send_destroy_relayd(ksess->consumer);
1383
1384 trace_kernel_destroy_session(ksess);
82b69413 1385 lttng_trace_chunk_put(trace_chunk);
2f77fc4b 1386}
fb5f35b6 1387
d070c424
MD
1388/* Teardown of data required by destroy notifiers. */
1389void kernel_free_session(struct ltt_kernel_session *ksess)
1390{
1391 if (ksess == NULL) {
1392 return;
1393 }
1394 trace_kernel_free_session(ksess);
1395}
1396
fb5f35b6
DG
1397/*
1398 * Destroy a kernel channel object. It does not do anything on the tracer side.
1399 */
1400void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1401{
1402 struct ltt_kernel_session *ksess = NULL;
1403
1404 assert(kchan);
1405 assert(kchan->channel);
1406
1407 DBG3("Kernel destroy channel %s", kchan->channel->name);
1408
1409 /* Update channel count of associated session. */
1410 if (kchan->session) {
1411 /* Keep pointer reference so we can update it after the destroy. */
1412 ksess = kchan->session;
1413 }
1414
1415 trace_kernel_destroy_channel(kchan);
1416
1417 /*
1418 * At this point the kernel channel is not visible anymore. This is safe
1419 * since in order to work on a visible kernel session, the tracing session
1420 * lock (ltt_session.lock) MUST be acquired.
1421 */
1422 if (ksess) {
1423 ksess->channel_count--;
1424 }
1425}
6dc3064a
DG
1426
1427/*
1428 * Take a snapshot for a given kernel session.
1429 *
9a654598 1430 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
6dc3064a 1431 */
fb9a95c4
JG
1432enum lttng_error_code kernel_snapshot_record(
1433 struct ltt_kernel_session *ksess,
348a81dc 1434 const struct consumer_output *output, int wait,
d07ceecd 1435 uint64_t nb_packets_per_stream)
6dc3064a 1436{
2a06df8d 1437 int err, ret, saved_metadata_fd;
9a654598 1438 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1439 struct consumer_socket *socket;
1440 struct lttng_ht_iter iter;
1441 struct ltt_kernel_metadata *saved_metadata;
3b967712 1442 char *trace_path = NULL;
5da88b0f 1443 size_t consumer_path_offset = 0;
6dc3064a
DG
1444
1445 assert(ksess);
1446 assert(ksess->consumer);
1447 assert(output);
1448
1449 DBG("Kernel snapshot record started");
1450
1451 /* Save current metadata since the following calls will change it. */
1452 saved_metadata = ksess->metadata;
1453 saved_metadata_fd = ksess->metadata_stream_fd;
1454
1455 rcu_read_lock();
1456
1457 ret = kernel_open_metadata(ksess);
1458 if (ret < 0) {
9a654598 1459 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1460 goto error;
1461 }
1462
1463 ret = kernel_open_metadata_stream(ksess);
1464 if (ret < 0) {
9a654598 1465 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1466 goto error_open_stream;
1467 }
1468
3b967712 1469 trace_path = setup_channel_trace_path(ksess->consumer,
5da88b0f 1470 DEFAULT_KERNEL_TRACE_DIR, &consumer_path_offset);
3b967712
MD
1471 if (!trace_path) {
1472 status = LTTNG_ERR_INVALID;
1473 goto error;
1474 }
6dc3064a 1475 /* Send metadata to consumer and snapshot everything. */
348a81dc 1476 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
6dc3064a 1477 socket, node.node) {
6dc3064a 1478 struct ltt_kernel_channel *chan;
6dc3064a 1479
6dc3064a
DG
1480 pthread_mutex_lock(socket->lock);
1481 /* This stream must not be monitored by the consumer. */
07b86b52 1482 ret = kernel_consumer_add_metadata(socket, ksess, 0);
6dc3064a 1483 pthread_mutex_unlock(socket->lock);
6dc3064a 1484 if (ret < 0) {
0ed78e50 1485 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1486 goto error_consumer;
1487 }
1488
1489 /* For each channel, ask the consumer to snapshot it. */
1490 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
9a654598 1491 status = consumer_snapshot_channel(socket, chan->key, output, 0,
5c786ded 1492 ksess->uid, ksess->gid,
5da88b0f 1493 &trace_path[consumer_path_offset], wait,
d2956687 1494 nb_packets_per_stream);
9a654598 1495 if (status != LTTNG_OK) {
2a06df8d
DG
1496 (void) kernel_consumer_destroy_metadata(socket,
1497 ksess->metadata);
6dc3064a
DG
1498 goto error_consumer;
1499 }
1500 }
1501
1502 /* Snapshot metadata, */
9a654598 1503 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
5da88b0f
MD
1504 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset],
1505 wait, 0);
9a654598 1506 if (status != LTTNG_OK) {
6dc3064a
DG
1507 goto error_consumer;
1508 }
07b86b52
JD
1509
1510 /*
1511 * The metadata snapshot is done, ask the consumer to destroy it since
1512 * it's not monitored on the consumer side.
1513 */
1514 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
1515 }
1516
1517error_consumer:
1518 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
1519 err = close(ksess->metadata_stream_fd);
1520 if (err < 0) {
6dc3064a
DG
1521 PERROR("close snapshot kernel");
1522 }
1523
1524error_open_stream:
1525 trace_kernel_destroy_metadata(ksess->metadata);
1526error:
1527 /* Restore metadata state.*/
1528 ksess->metadata = saved_metadata;
1529 ksess->metadata_stream_fd = saved_metadata_fd;
6dc3064a 1530 rcu_read_unlock();
3b967712 1531 free(trace_path);
9a654598 1532 return status;
6dc3064a 1533}
834978fd
DG
1534
1535/*
1536 * Get the syscall mask array from the kernel tracer.
1537 *
1538 * Return 0 on success else a negative value. In both case, syscall_mask should
1539 * be freed.
1540 */
1541int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1542{
1543 assert(syscall_mask);
1544 assert(nr_bits);
1545
1546 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1547}
6e21424e
JR
1548
1549/*
1550 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1551 * version number.
1552 *
1553 * Return 1 on success, 0 when feature is not supported, negative value in case
1554 * of errors.
1555 */
7d268848 1556int kernel_supports_ring_buffer_snapshot_sample_positions(void)
6e21424e
JR
1557{
1558 int ret = 0; // Not supported by default
1559 struct lttng_kernel_tracer_abi_version abi;
1560
7d268848 1561 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
6e21424e
JR
1562 if (ret < 0) {
1563 ERR("Failed to retrieve lttng-modules ABI version");
1564 goto error;
1565 }
1566
1567 /*
1568 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1569 */
1570 if (abi.major >= 2 && abi.minor >= 3) {
1571 /* Supported */
1572 ret = 1;
1573 } else {
1574 /* Not supported */
1575 ret = 0;
1576 }
1577error:
1578 return ret;
1579}
5c408ad8 1580
a40a503f
MD
1581/*
1582 * Check for the support of the packet sequence number via abi version number.
1583 *
1584 * Return 1 on success, 0 when feature is not supported, negative value in case
1585 * of errors.
1586 */
1587int kernel_supports_ring_buffer_packet_sequence_number(void)
1588{
1589 int ret = 0; // Not supported by default
1590 struct lttng_kernel_tracer_abi_version abi;
1591
1592 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1593 if (ret < 0) {
1594 ERR("Failed to retrieve lttng-modules ABI version");
1595 goto error;
1596 }
1597
1598 /*
3029bc92
JG
1599 * Packet sequence number was introduced in LTTng 2.8,
1600 * lttng-modules ABI 2.1.
a40a503f 1601 */
3029bc92 1602 if (abi.major >= 2 && abi.minor >= 1) {
a40a503f
MD
1603 /* Supported */
1604 ret = 1;
1605 } else {
1606 /* Not supported */
1607 ret = 0;
1608 }
1609error:
1610 return ret;
1611}
1612
5c408ad8
JD
1613/*
1614 * Rotate a kernel session.
1615 *
d5a1b7aa 1616 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 1617 */
d5a1b7aa 1618enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
5c408ad8
JD
1619{
1620 int ret;
d5a1b7aa 1621 enum lttng_error_code status = LTTNG_OK;
5c408ad8
JD
1622 struct consumer_socket *socket;
1623 struct lttng_ht_iter iter;
1624 struct ltt_kernel_session *ksess = session->kernel_session;
1625
1626 assert(ksess);
1627 assert(ksess->consumer);
1628
1629 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1630 session->name, session->id);
1631
1632 rcu_read_lock();
1633
1634 /*
1635 * Note that this loop will end after one iteration given that there is
1636 * only one kernel consumer.
1637 */
1638 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1639 socket, node.node) {
1640 struct ltt_kernel_channel *chan;
1641
d2956687 1642 /* For each channel, ask the consumer to rotate it. */
5c408ad8 1643 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
92816cc3
JG
1644 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1645 chan->key, session->name);
5c408ad8
JD
1646 ret = consumer_rotate_channel(socket, chan->key,
1647 ksess->uid, ksess->gid, ksess->consumer,
d2956687 1648 /* is_metadata_channel */ false);
5c408ad8 1649 if (ret < 0) {
d5a1b7aa 1650 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1651 goto error;
1652 }
1653 }
1654
1655 /*
1656 * Rotate the metadata channel.
1657 */
22a1b931 1658 ret = consumer_rotate_channel(socket, ksess->metadata->key,
5c408ad8 1659 ksess->uid, ksess->gid, ksess->consumer,
d2956687 1660 /* is_metadata_channel */ true);
5c408ad8 1661 if (ret < 0) {
d5a1b7aa 1662 status = LTTNG_ERR_KERN_CONSUMER_FAIL;
5c408ad8
JD
1663 goto error;
1664 }
1665 }
1666
5c408ad8
JD
1667error:
1668 rcu_read_unlock();
d5a1b7aa 1669 return status;
5c408ad8 1670}
d2956687
JG
1671
1672enum lttng_error_code kernel_create_channel_subdirectories(
1673 const struct ltt_kernel_session *ksess)
1674{
1675 enum lttng_error_code ret = LTTNG_OK;
1676 enum lttng_trace_chunk_status chunk_status;
1677
1678 rcu_read_lock();
1679 assert(ksess->current_trace_chunk);
1680
1681 /*
1682 * Create the index subdirectory which will take care
1683 * of implicitly creating the channel's path.
1684 */
1685 chunk_status = lttng_trace_chunk_create_subdirectory(
1686 ksess->current_trace_chunk,
1687 DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
1688 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1689 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1690 goto error;
1691 }
1692error:
1693 rcu_read_unlock();
1694 return ret;
1695}
7d268848
MD
1696
1697/*
1698 * Setup necessary data for kernel tracer action.
1699 */
1700LTTNG_HIDDEN
1701int init_kernel_tracer(void)
1702{
1703 int ret;
1704 bool is_root = !getuid();
1705
1706 /* Modprobe lttng kernel modules */
1707 ret = modprobe_lttng_control();
1708 if (ret < 0) {
1709 goto error;
1710 }
1711
1712 /* Open debugfs lttng */
1713 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
1714 if (kernel_tracer_fd < 0) {
1715 DBG("Failed to open %s", module_proc_lttng);
1716 goto error_open;
1717 }
1718
1719 /* Validate kernel version */
1720 ret = kernel_validate_version(&kernel_tracer_version,
1721 &kernel_tracer_abi_version);
1722 if (ret < 0) {
1723 goto error_version;
1724 }
1725
1726 ret = modprobe_lttng_data();
1727 if (ret < 0) {
1728 goto error_modules;
1729 }
1730
1731 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
1732 if (ret < 0) {
1733 goto error_modules;
1734 }
1735
1736 if (ret < 1) {
1737 WARN("Kernel tracer does not support buffer monitoring. "
1738 "The monitoring timer of channels in the kernel domain "
1739 "will be set to 0 (disabled).");
1740 }
1741
1742 DBG("Kernel tracer fd %d", kernel_tracer_fd);
1743
1744 ret = syscall_init_table(kernel_tracer_fd);
1745 if (ret < 0) {
1746 ERR("Unable to populate syscall table. Syscall tracing won't "
1747 "work for this session daemon.");
1748 }
1749 return 0;
1750
1751error_version:
1752 modprobe_remove_lttng_control();
1753 ret = close(kernel_tracer_fd);
1754 if (ret) {
1755 PERROR("close");
1756 }
1757 kernel_tracer_fd = -1;
1758 return LTTNG_ERR_KERN_VERSION;
1759
1760error_modules:
1761 ret = close(kernel_tracer_fd);
1762 if (ret) {
1763 PERROR("close");
1764 }
1765
1766error_open:
1767 modprobe_remove_lttng_control();
1768
1769error:
1770 WARN("No kernel tracer available");
1771 kernel_tracer_fd = -1;
1772 if (!is_root) {
1773 return LTTNG_ERR_NEED_ROOT_SESSIOND;
1774 } else {
1775 return LTTNG_ERR_KERN_NA;
1776 }
1777}
1778
1779LTTNG_HIDDEN
1780void cleanup_kernel_tracer(void)
1781{
1782 int ret;
1783
1784 DBG2("Closing kernel fd");
1785 if (kernel_tracer_fd >= 0) {
1786 ret = close(kernel_tracer_fd);
1787 if (ret) {
1788 PERROR("close");
1789 }
1790 kernel_tracer_fd = -1;
1791 }
1792 DBG("Unloading kernel modules");
1793 modprobe_remove_lttng_all();
1794 free(syscall_table);
1795}
1796
1797LTTNG_HIDDEN
1798bool kernel_tracer_is_initialized(void)
1799{
1800 return kernel_tracer_fd >= 0;
1801}
bca212a2
MD
1802
1803/*
1804 * Clear a kernel session.
1805 *
1806 * Return LTTNG_OK on success or else an LTTng error code.
1807 */
1808enum lttng_error_code kernel_clear_session(struct ltt_session *session)
1809{
1810 int ret;
1811 enum lttng_error_code status = LTTNG_OK;
1812 struct consumer_socket *socket;
1813 struct lttng_ht_iter iter;
1814 struct ltt_kernel_session *ksess = session->kernel_session;
1815
1816 assert(ksess);
1817 assert(ksess->consumer);
1818
1819 DBG("Clear kernel session %s (session %" PRIu64 ")",
1820 session->name, session->id);
1821
1822 rcu_read_lock();
1823
1824 if (ksess->active) {
1825 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
1826 status = LTTNG_ERR_FATAL;
1827 goto end;
1828 }
1829
1830 /*
1831 * Note that this loop will end after one iteration given that there is
1832 * only one kernel consumer.
1833 */
1834 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1835 socket, node.node) {
1836 struct ltt_kernel_channel *chan;
1837
1838 /* For each channel, ask the consumer to clear it. */
1839 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1840 DBG("Clear kernel channel %" PRIu64 ", session %s",
1841 chan->key, session->name);
1842 ret = consumer_clear_channel(socket, chan->key);
1843 if (ret < 0) {
1844 goto error;
1845 }
1846 }
1847
1848 if (!ksess->metadata) {
1849 /*
1850 * Nothing to do for the metadata.
1851 * This is a snapshot session.
1852 * The metadata is genererated on the fly.
1853 */
1854 continue;
1855 }
1856
1857 /*
1858 * Clear the metadata channel.
1859 * Metadata channel is not cleared per se but we still need to
1860 * perform a rotation operation on it behind the scene.
1861 */
1862 ret = consumer_clear_channel(socket, ksess->metadata->key);
1863 if (ret < 0) {
1864 goto error;
1865 }
1866 }
1867
1868 goto end;
1869error:
1870 switch (-ret) {
1871 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
1872 status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
1873 break;
1874 default:
1875 status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
1876 break;
1877 }
1878end:
1879 rcu_read_unlock();
1880 return status;
1881}
This page took 0.261686 seconds and 4 git commands to generate.