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