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