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