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