Fix: consumerd: consumed size miscomputed during statistics sampling
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.cpp
CommitLineData
20fe2104 1/*
21cf9b6b 2 * Copyright (C) 2011 EfficiOS Inc.
20fe2104 3 *
ab5be9fa 4 * SPDX-License-Identifier: GPL-2.0-only
20fe2104 5 *
20fe2104
DG
6 */
7
6c1c0768 8#define _LGPL_SOURCE
28ab034a
JG
9#include "condition-internal.hpp"
10#include "consumer.hpp"
11#include "event-notifier-error-accounting.hpp"
12#include "kern-modules.hpp"
13#include "kernel-consumer.hpp"
14#include "kernel.hpp"
15#include "lttng-sessiond.hpp"
16#include "lttng-syscall.hpp"
17#include "modprobe.hpp"
18#include "notification-thread-commands.hpp"
28ab034a
JG
19#include "sessiond-config.hpp"
20#include "tracker.hpp"
21#include "utils.hpp"
20fe2104 22
c9e313bc
SM
23#include <common/common.hpp>
24#include <common/hashtable/utils.hpp>
c9e313bc
SM
25#include <common/kernel-ctl/kernel-ctl.hpp>
26#include <common/kernel-ctl/kernel-ioctl.hpp>
07a5d4b5 27#include <common/scope-exit.hpp>
c9e313bc 28#include <common/sessiond-comm/sessiond-comm.hpp>
28ab034a 29#include <common/trace-chunk.hpp>
c9e313bc 30#include <common/tracker.hpp>
56047f5a 31#include <common/urcu.hpp>
c9e313bc 32#include <common/utils.hpp>
1e307fab 33
c9e313bc 34#include <lttng/condition/event-rule-matches-internal.hpp>
28ab034a 35#include <lttng/condition/event-rule-matches.h>
c9e313bc 36#include <lttng/event-rule/event-rule-internal.hpp>
28ab034a 37#include <lttng/event-rule/event-rule.h>
c9e313bc 38#include <lttng/event-rule/kernel-uprobe-internal.hpp>
28ab034a
JG
39#include <lttng/event.h>
40#include <lttng/lttng-error.h>
41#include <lttng/tracker.h>
42#include <lttng/userspace-probe-internal.hpp>
43#include <lttng/userspace-probe.h>
352b58f5 44
28ab034a
JG
45#include <fcntl.h>
46#include <inttypes.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <string.h>
50#include <sys/types.h>
51#include <unistd.h>
20fe2104 52
56a9a0b7 53namespace {
07a5d4b5
JG
54/*
55 * Key used to reference a channel between the sessiond and the consumer. This
56 * is only read and updated with the session_list lock held.
57 */
58uint64_t next_kernel_channel_key;
59
60const char *module_proc_lttng = "/proc/lttng";
61
62int kernel_tracer_fd = -1;
63nonstd::optional<enum lttng_kernel_tracer_status> kernel_tracer_status = nonstd::nullopt;
64int kernel_tracer_event_notifier_group_fd = -1;
65int kernel_tracer_event_notifier_group_notification_fd = -1;
66struct cds_lfht *kernel_token_to_event_notifier_rule_ht;
67
68const char *kernel_tracer_status_to_str(lttng_kernel_tracer_status status)
69{
70 switch (status) {
71 case LTTNG_KERNEL_TRACER_STATUS_INITIALIZED:
72 return "LTTNG_KERNEL_TRACER_STATUS_INITIALIZED";
73 case LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN:
74 return "LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN";
75 case LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT:
76 return "LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT";
77 case LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER:
78 return "LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER";
79 case LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG:
80 return "LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG";
81 case LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH:
82 return "LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH";
83 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN:
84 return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN";
85 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING:
86 return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING";
87 case LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE:
88 return "LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE";
89 }
90
91 abort();
92}
93
56a9a0b7
OD
94/*
95 * On some architectures, calling convention details are embedded in the symbol
96 * addresses. Uprobe requires a "clean" symbol offset (or at least, an address
97 * where an instruction boundary would be legal) to add
98 * instrumentation. sanitize_uprobe_offset implements that sanitization logic on
99 * a per-architecture basis.
100 */
101#if defined(__arm__) || defined(__aarch64__)
102static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
103{
104 /*
105 * The least significant bit is used when branching to switch to thumb
106 * ISA. However, it's an invalid address for us; mask the least
107 * significant bit.
108 */
109 return raw_offset &= ~0b1;
110}
111#else /* defined(__arm__) || defined(__aarch64__) */
112static inline uint64_t sanitize_uprobe_offset(uint64_t raw_offset)
113{
114 return raw_offset;
115}
116#endif
28f23191 117} /* namespace */
56a9a0b7 118
d65106b1 119/*
050349bb 120 * Add context on a kernel channel.
df3c77c8
JG
121 *
122 * Assumes the ownership of ctx.
d65106b1 123 */
28ab034a 124int kernel_add_channel_context(struct ltt_kernel_channel *chan, struct ltt_kernel_context *ctx)
d65106b1
DG
125{
126 int ret;
127
a0377dfe
FD
128 LTTNG_ASSERT(chan);
129 LTTNG_ASSERT(ctx);
0525e9ae 130
d65106b1 131 DBG("Adding context to channel %s", chan->channel->name);
645328ae 132 ret = kernctl_add_context(chan->fd, &ctx->ctx);
d65106b1 133 if (ret < 0) {
32af2c95 134 switch (-ret) {
1ae5e83e
JD
135 case ENOSYS:
136 /* Exists but not available for this kernel */
137 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
138 goto error;
139 case EEXIST:
b579acd9
DG
140 /* If EEXIST, we just ignore the error */
141 ret = 0;
1ae5e83e
JD
142 goto end;
143 default:
144 PERROR("add context ioctl");
145 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
146 goto error;
b579acd9 147 }
d65106b1 148 }
21ed98c1 149 ret = 0;
d65106b1 150
1ae5e83e 151end:
645328ae 152 cds_list_add_tail(&ctx->list, &chan->ctx_list);
ba985c3a 153 ctx->in_list = true;
cd9adb8b 154 ctx = nullptr;
d65106b1 155error:
df3c77c8
JG
156 if (ctx) {
157 trace_kernel_destroy_context(ctx);
158 }
d65106b1
DG
159 return ret;
160}
161
20fe2104 162/*
050349bb
DG
163 * Create a new kernel session, register it to the kernel tracer and add it to
164 * the session daemon session.
20fe2104 165 */
7d268848 166int kernel_create_session(struct ltt_session *session)
20fe2104
DG
167{
168 int ret;
169 struct ltt_kernel_session *lks;
170
a0377dfe 171 LTTNG_ASSERT(session);
0525e9ae 172
54012638 173 /* Allocate data structure */
dec56f6c 174 lks = trace_kernel_create_session();
cd9adb8b 175 if (lks == nullptr) {
54012638 176 ret = -1;
20fe2104
DG
177 goto error;
178 }
179
54012638 180 /* Kernel tracer session creation */
7d268848 181 ret = kernctl_create_session(kernel_tracer_fd);
20fe2104 182 if (ret < 0) {
df0f840b 183 PERROR("ioctl kernel create session");
20fe2104
DG
184 goto error;
185 }
186
20fe2104 187 lks->fd = ret;
7b395890
DG
188 /* Prevent fd duplication after execlp() */
189 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
190 if (ret < 0) {
df0f840b 191 PERROR("fcntl session fd");
7b395890
DG
192 }
193
53632229 194 lks->id = session->id;
3bd1e081 195 lks->consumer_fds_sent = 0;
8c0faa1d 196 session->kernel_session = lks;
8c0faa1d
DG
197
198 DBG("Kernel session created (fd: %d)", lks->fd);
20fe2104 199
8ff4109e
JR
200 /*
201 * This is necessary since the creation time is present in the session
202 * name when it is generated.
203 */
204 if (session->has_auto_generated_name) {
205 ret = kernctl_session_set_name(lks->fd, DEFAULT_SESSION_NAME);
206 } else {
207 ret = kernctl_session_set_name(lks->fd, session->name);
208 }
209 if (ret) {
210 WARN("Could not set kernel session name for session %" PRIu64 " name: %s",
28ab034a
JG
211 session->id,
212 session->name);
8ff4109e
JR
213 }
214
e04b2181
JR
215 ret = kernctl_session_set_creation_time(lks->fd, session->creation_time);
216 if (ret) {
217 WARN("Could not set kernel session creation time for session %" PRIu64 " name: %s",
28ab034a
JG
218 session->id,
219 session->name);
e04b2181
JR
220 }
221
20fe2104
DG
222 return 0;
223
224error:
5f62c685
DG
225 if (lks) {
226 trace_kernel_destroy_session(lks);
d070c424 227 trace_kernel_free_session(lks);
5f62c685 228 }
20fe2104
DG
229 return ret;
230}
231
232/*
050349bb
DG
233 * Create a kernel channel, register it to the kernel tracer and add it to the
234 * kernel session.
20fe2104 235 */
28ab034a 236int kernel_create_channel(struct ltt_kernel_session *session, struct lttng_channel *chan)
20fe2104
DG
237{
238 int ret;
239 struct ltt_kernel_channel *lkc;
20fe2104 240
a0377dfe
FD
241 LTTNG_ASSERT(session);
242 LTTNG_ASSERT(chan);
0525e9ae 243
54012638 244 /* Allocate kernel channel */
fdd9eb17 245 lkc = trace_kernel_create_channel(chan);
cd9adb8b 246 if (lkc == nullptr) {
20fe2104
DG
247 goto error;
248 }
249
ecc48a90 250 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
28ab034a
JG
251 chan->name,
252 lkc->channel->attr.overwrite,
253 lkc->channel->attr.subbuf_size,
254 lkc->channel->attr.num_subbuf,
255 lkc->channel->attr.switch_timer_interval,
256 lkc->channel->attr.read_timer_interval,
257 lkc->channel->attr.live_timer_interval,
258 lkc->channel->attr.output);
173af62f 259
54012638 260 /* Kernel tracer channel creation */
f3ed775e 261 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
20fe2104 262 if (ret < 0) {
df0f840b 263 PERROR("ioctl kernel create channel");
20fe2104
DG
264 goto error;
265 }
266
54012638 267 /* Setup the channel fd */
20fe2104 268 lkc->fd = ret;
7b395890
DG
269 /* Prevent fd duplication after execlp() */
270 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
271 if (ret < 0) {
df0f840b 272 PERROR("fcntl session fd");
7b395890
DG
273 }
274
54012638 275 /* Add channel to session */
8c0faa1d
DG
276 cds_list_add(&lkc->list, &session->channel_list.head);
277 session->channel_count++;
fb5f35b6 278 lkc->session = session;
e1f3997a 279 lkc->key = ++next_kernel_channel_key;
20fe2104 280
e1f3997a 281 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
28ab034a
JG
282 lkc->channel->name,
283 lkc->fd,
284 lkc->key);
20fe2104
DG
285
286 return 0;
287
288error:
5f62c685
DG
289 if (lkc) {
290 free(lkc->channel);
291 free(lkc);
292 }
54012638 293 return -1;
20fe2104 294}
f34daff7 295
352b58f5
JR
296/*
297 * Create a kernel event notifier group, register it to the kernel tracer and
298 * add it to the kernel session.
299 */
300static int kernel_create_event_notifier_group(int *event_notifier_group_fd)
301{
302 int ret;
303 int local_fd = -1;
304
a0377dfe 305 LTTNG_ASSERT(event_notifier_group_fd);
352b58f5
JR
306
307 /* Kernel event notifier group creation. */
308 ret = kernctl_create_event_notifier_group(kernel_tracer_fd);
309 if (ret < 0) {
310 PERROR("Failed to create kernel event notifier group");
311 ret = -1;
312 goto error;
313 }
314
315 local_fd = ret;
316
317 /* Prevent fd duplication after execlp(). */
318 ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
319 if (ret < 0) {
320 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group file descriptor: fd = %d",
28ab034a 321 local_fd);
352b58f5
JR
322 goto error;
323 }
324
325 DBG("Created kernel event notifier group: fd = %d", local_fd);
326 *event_notifier_group_fd = local_fd;
327 local_fd = -1;
328 ret = 0;
329error:
330 if (local_fd >= 0) {
331 ret = close(local_fd);
332 if (ret) {
333 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
28ab034a 334 local_fd);
352b58f5
JR
335 }
336 }
337
338 return ret;
339}
340
410b78a0
FD
341/*
342 * Compute the offset of the instrumentation byte in the binary based on the
343 * function probe location using the ELF lookup method.
344 *
345 * Returns 0 on success and set the offset out parameter to the offset of the
346 * elf symbol
347 * Returns -1 on error
348 */
28ab034a
JG
349static int extract_userspace_probe_offset_function_elf(
350 const struct lttng_userspace_probe_location *probe_location,
351 uid_t uid,
352 gid_t gid,
353 uint64_t *offset)
410b78a0
FD
354{
355 int fd;
356 int ret = 0;
cd9adb8b
JG
357 const char *symbol = nullptr;
358 const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr;
410b78a0
FD
359 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
360
a0377dfe 361 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) ==
28ab034a 362 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
410b78a0 363
28ab034a 364 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
410b78a0
FD
365 if (!lookup) {
366 ret = -1;
367 goto end;
368 }
369
28ab034a 370 lookup_method_type = lttng_userspace_probe_location_lookup_method_get_type(lookup);
410b78a0 371
a0377dfe 372 LTTNG_ASSERT(lookup_method_type ==
28ab034a 373 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
410b78a0 374
28ab034a 375 symbol = lttng_userspace_probe_location_function_get_function_name(probe_location);
410b78a0
FD
376 if (!symbol) {
377 ret = -1;
378 goto end;
379 }
380
381 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
382 if (fd < 0) {
383 ret = -1;
384 goto end;
385 }
386
0a398bc4 387 ret = run_as_extract_elf_symbol_offset(fd, symbol, uid, gid, offset);
410b78a0
FD
388 if (ret < 0) {
389 DBG("userspace probe offset calculation failed for "
28ab034a
JG
390 "function %s",
391 symbol);
410b78a0
FD
392 goto end;
393 }
394
28ab034a 395 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t) (*offset));
410b78a0
FD
396end:
397 return ret;
398}
399
400/*
401 * Compute the offsets of the instrumentation bytes in the binary based on the
402 * tracepoint probe location using the SDT lookup method. This function
403 * allocates the offsets buffer, the caller must free it.
404 *
405 * Returns 0 on success and set the offset out parameter to the offsets of the
406 * SDT tracepoint.
407 * Returns -1 on error.
408 */
28ab034a
JG
409static int extract_userspace_probe_offset_tracepoint_sdt(
410 const struct lttng_userspace_probe_location *probe_location,
411 uid_t uid,
412 gid_t gid,
413 uint64_t **offsets,
414 uint32_t *offsets_count)
410b78a0
FD
415{
416 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
cd9adb8b
JG
417 const struct lttng_userspace_probe_location_lookup_method *lookup = nullptr;
418 const char *probe_name = nullptr, *provider_name = nullptr;
410b78a0
FD
419 int ret = 0;
420 int fd, i;
421
a0377dfe 422 LTTNG_ASSERT(lttng_userspace_probe_location_get_type(probe_location) ==
28ab034a 423 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
410b78a0
FD
424
425 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
426 if (!lookup) {
427 ret = -1;
428 goto end;
429 }
430
28ab034a 431 lookup_method_type = lttng_userspace_probe_location_lookup_method_get_type(lookup);
410b78a0 432
a0377dfe 433 LTTNG_ASSERT(lookup_method_type ==
28ab034a 434 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
410b78a0 435
28ab034a 436 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(probe_location);
410b78a0
FD
437 if (!probe_name) {
438 ret = -1;
439 goto end;
440 }
441
28ab034a 442 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(probe_location);
410b78a0
FD
443 if (!provider_name) {
444 ret = -1;
445 goto end;
446 }
447
448 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
449 if (fd < 0) {
450 ret = -1;
451 goto end;
452 }
453
28ab034a
JG
454 ret = run_as_extract_sdt_probe_offsets(
455 fd, provider_name, probe_name, uid, gid, offsets, offsets_count);
410b78a0
FD
456 if (ret < 0) {
457 DBG("userspace probe offset calculation failed for sdt "
28ab034a
JG
458 "probe %s:%s",
459 provider_name,
460 probe_name);
410b78a0
FD
461 goto end;
462 }
463
464 if (*offsets_count == 0) {
465 DBG("no userspace probe offset found");
466 goto end;
467 }
468
469 DBG("%u userspace probe SDT offsets found for %s:%s at:",
28ab034a
JG
470 *offsets_count,
471 provider_name,
472 probe_name);
410b78a0 473 for (i = 0; i < *offsets_count; i++) {
28ab034a 474 DBG("\t0x%jd", (intmax_t) ((*offsets)[i]));
410b78a0
FD
475 }
476end:
477 return ret;
478}
479
28ab034a
JG
480static int userspace_probe_add_callsite(const struct lttng_userspace_probe_location *location,
481 uid_t uid,
482 gid_t gid,
483 int fd)
410b78a0 484{
cd9adb8b 485 const struct lttng_userspace_probe_location_lookup_method *lookup_method = nullptr;
410b78a0 486 enum lttng_userspace_probe_location_lookup_method_type type;
410b78a0
FD
487 int ret;
488
0a398bc4 489 lookup_method = lttng_userspace_probe_location_get_lookup_method(location);
410b78a0
FD
490 if (!lookup_method) {
491 ret = -1;
492 goto end;
493 }
494
495 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
496 switch (type) {
497 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
498 {
b8e2fb80 499 struct lttng_kernel_abi_event_callsite callsite;
410b78a0
FD
500 uint64_t offset;
501
28ab034a 502 ret = extract_userspace_probe_offset_function_elf(location, uid, gid, &offset);
410b78a0
FD
503 if (ret) {
504 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
505 goto end;
506 }
507
56a9a0b7 508 callsite.u.uprobe.offset = sanitize_uprobe_offset(offset);
410b78a0
FD
509 ret = kernctl_add_callsite(fd, &callsite);
510 if (ret) {
0a398bc4 511 WARN("Failed to add callsite to ELF userspace probe.");
410b78a0
FD
512 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
513 goto end;
514 }
515 break;
516 }
517 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
518 {
519 int i;
cd9adb8b 520 uint64_t *offsets = nullptr;
410b78a0 521 uint32_t offsets_count;
b8e2fb80 522 struct lttng_kernel_abi_event_callsite callsite;
410b78a0
FD
523
524 /*
525 * This call allocates the offsets buffer. This buffer must be freed
526 * by the caller
527 */
28ab034a
JG
528 ret = extract_userspace_probe_offset_tracepoint_sdt(
529 location, uid, gid, &offsets, &offsets_count);
410b78a0
FD
530 if (ret) {
531 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
532 goto end;
533 }
534 for (i = 0; i < offsets_count; i++) {
56a9a0b7 535 callsite.u.uprobe.offset = sanitize_uprobe_offset(offsets[i]);
410b78a0
FD
536 ret = kernctl_add_callsite(fd, &callsite);
537 if (ret) {
0a398bc4 538 WARN("Failed to add callsite to SDT userspace probe");
410b78a0
FD
539 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
540 free(offsets);
541 goto end;
542 }
543 }
544 free(offsets);
545 break;
546 }
547 default:
548 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
549 goto end;
550 }
551end:
552 return ret;
553}
554
0a398bc4
FD
555/*
556 * Extract the offsets of the instrumentation point for the different lookup
557 * methods.
558 */
28ab034a
JG
559static int userspace_probe_event_add_callsites(struct lttng_event *ev,
560 struct ltt_kernel_session *session,
561 int fd)
0a398bc4
FD
562{
563 int ret;
cd9adb8b 564 const struct lttng_userspace_probe_location *location = nullptr;
0a398bc4 565
a0377dfe
FD
566 LTTNG_ASSERT(ev);
567 LTTNG_ASSERT(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
0a398bc4
FD
568
569 location = lttng_event_get_userspace_probe_location(ev);
570 if (!location) {
571 ret = -1;
572 goto end;
573 }
574
28ab034a 575 ret = userspace_probe_add_callsite(location, session->uid, session->gid, fd);
0a398bc4 576 if (ret) {
28ab034a 577 WARN("Failed to add callsite to userspace probe event '%s'", ev->name);
0a398bc4
FD
578 }
579
580end:
581 return ret;
582}
583
352b58f5
JR
584/*
585 * Extract the offsets of the instrumentation point for the different look-up
586 * methods.
587 */
28ab034a
JG
588static int userspace_probe_event_rule_add_callsites(const struct lttng_event_rule *rule,
589 const struct lttng_credentials *creds,
590 int fd)
352b58f5
JR
591{
592 int ret;
593 enum lttng_event_rule_status status;
594 enum lttng_event_rule_type event_rule_type;
cd9adb8b 595 const struct lttng_userspace_probe_location *location = nullptr;
352b58f5 596
a0377dfe
FD
597 LTTNG_ASSERT(rule);
598 LTTNG_ASSERT(creds);
352b58f5
JR
599
600 event_rule_type = lttng_event_rule_get_type(rule);
a0377dfe 601 LTTNG_ASSERT(event_rule_type == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE);
352b58f5 602
46fd07ac 603 status = lttng_event_rule_kernel_uprobe_get_location(rule, &location);
352b58f5
JR
604 if (status != LTTNG_EVENT_RULE_STATUS_OK || !location) {
605 ret = -1;
606 goto end;
607 }
608
28ab034a
JG
609 ret = userspace_probe_add_callsite(
610 location, lttng_credentials_get_uid(creds), lttng_credentials_get_gid(creds), fd);
352b58f5 611 if (ret) {
28ab034a 612 WARN("Failed to add callsite to user space probe object: fd = %d", fd);
352b58f5
JR
613 }
614
615end:
616 return ret;
617}
618
f34daff7 619/*
050349bb
DG
620 * Create a kernel event, enable it to the kernel tracer and add it to the
621 * channel event list of the kernel session.
49d21f93 622 * We own filter_expression and filter.
f34daff7 623 */
050349bb 624int kernel_create_event(struct lttng_event *ev,
28ab034a
JG
625 struct ltt_kernel_channel *channel,
626 char *filter_expression,
627 struct lttng_bytecode *filter)
f34daff7 628{
71a3bb01
FD
629 int err, fd;
630 enum lttng_error_code ret;
f34daff7 631 struct ltt_kernel_event *event;
f34daff7 632
a0377dfe
FD
633 LTTNG_ASSERT(ev);
634 LTTNG_ASSERT(channel);
0525e9ae 635
a969e101 636 /* We pass ownership of filter_expression and filter */
28ab034a 637 ret = trace_kernel_create_event(ev, filter_expression, filter, &event);
71a3bb01 638 if (ret != LTTNG_OK) {
f34daff7
DG
639 goto error;
640 }
641
71a3bb01
FD
642 fd = kernctl_create_event(channel->fd, event->event);
643 if (fd < 0) {
644 switch (-fd) {
bd29c13d 645 case EEXIST:
71a3bb01 646 ret = LTTNG_ERR_KERN_EVENT_EXIST;
bd29c13d
DG
647 break;
648 case ENOSYS:
649 WARN("Event type not implemented");
71a3bb01 650 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
bd29c13d 651 break;
8197a339
DG
652 case ENOENT:
653 WARN("Event %s not found!", ev->name);
71a3bb01 654 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
8197a339 655 break;
bd29c13d 656 default:
71a3bb01 657 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
d87bfb32
DG
658 PERROR("create event ioctl");
659 }
e953ef25 660 goto free_event;
8c0faa1d 661 }
f34daff7 662
d0ae4ea8 663 event->type = ev->type;
71a3bb01 664 event->fd = fd;
7b395890 665 /* Prevent fd duplication after execlp() */
71a3bb01
FD
666 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
667 if (err < 0) {
df0f840b 668 PERROR("fcntl session fd");
7b395890
DG
669 }
670
00a62084 671 if (filter) {
71a3bb01
FD
672 err = kernctl_filter(event->fd, filter);
673 if (err < 0) {
674 switch (-err) {
675 case ENOMEM:
676 ret = LTTNG_ERR_FILTER_NOMEM;
677 break;
678 default:
679 ret = LTTNG_ERR_FILTER_INVAL;
680 break;
681 }
00a62084
MD
682 goto filter_error;
683 }
684 }
685
dcabc190 686 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
28ab034a
JG
687 ret = (lttng_error_code) userspace_probe_event_add_callsites(
688 ev, channel->session, event->fd);
dcabc190
FD
689 if (ret) {
690 goto add_callsite_error;
691 }
692 }
693
71a3bb01
FD
694 err = kernctl_enable(event->fd);
695 if (err < 0) {
696 switch (-err) {
00a62084
MD
697 case EEXIST:
698 ret = LTTNG_ERR_KERN_EVENT_EXIST;
699 break;
700 default:
701 PERROR("enable kernel event");
71a3bb01 702 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
00a62084
MD
703 break;
704 }
705 goto enable_error;
706 }
707
f3ed775e
DG
708 /* Add event to event list */
709 cds_list_add(&event->list, &channel->events_list.head);
cbbbb275
DG
710 channel->event_count++;
711
e953ef25
DG
712 DBG("Event %s created (fd: %d)", ev->name, event->fd);
713
714 return 0;
715
dcabc190 716add_callsite_error:
00a62084
MD
717enable_error:
718filter_error:
28ab034a
JG
719{
720 int closeret;
00a62084 721
28ab034a
JG
722 closeret = close(event->fd);
723 if (closeret) {
724 PERROR("close event fd");
00a62084 725 }
28ab034a 726}
e953ef25
DG
727free_event:
728 free(event);
729error:
d87bfb32 730 return ret;
e953ef25
DG
731}
732
26cc6b4e 733/*
050349bb 734 * Disable a kernel channel.
26cc6b4e
DG
735 */
736int kernel_disable_channel(struct ltt_kernel_channel *chan)
737{
738 int ret;
739
a0377dfe 740 LTTNG_ASSERT(chan);
0525e9ae 741
26cc6b4e
DG
742 ret = kernctl_disable(chan->fd);
743 if (ret < 0) {
df0f840b 744 PERROR("disable chan ioctl");
26cc6b4e
DG
745 goto error;
746 }
747
66cefebd 748 chan->enabled = false;
e1f3997a 749 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
28ab034a
JG
750 chan->channel->name,
751 chan->fd,
752 chan->key);
26cc6b4e
DG
753
754 return 0;
755
756error:
757 return ret;
758}
759
d36b8583 760/*
050349bb 761 * Enable a kernel channel.
d36b8583
DG
762 */
763int kernel_enable_channel(struct ltt_kernel_channel *chan)
764{
765 int ret;
766
a0377dfe 767 LTTNG_ASSERT(chan);
0525e9ae 768
d36b8583 769 ret = kernctl_enable(chan->fd);
32af2c95 770 if (ret < 0 && ret != -EEXIST) {
df0f840b 771 PERROR("Enable kernel chan");
d36b8583
DG
772 goto error;
773 }
774
66cefebd 775 chan->enabled = true;
e1f3997a 776 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
28ab034a
JG
777 chan->channel->name,
778 chan->fd,
779 chan->key);
d36b8583
DG
780
781 return 0;
782
783error:
784 return ret;
785}
786
19e70852 787/*
050349bb 788 * Enable a kernel event.
19e70852
DG
789 */
790int kernel_enable_event(struct ltt_kernel_event *event)
791{
792 int ret;
793
a0377dfe 794 LTTNG_ASSERT(event);
0525e9ae 795
19e70852 796 ret = kernctl_enable(event->fd);
42224349 797 if (ret < 0) {
32af2c95 798 switch (-ret) {
42224349 799 case EEXIST:
f73fabfd 800 ret = LTTNG_ERR_KERN_EVENT_EXIST;
42224349
DG
801 break;
802 default:
803 PERROR("enable kernel event");
804 break;
805 }
19e70852
DG
806 goto error;
807 }
808
66cefebd 809 event->enabled = true;
19e70852
DG
810 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
811
812 return 0;
813
814error:
d36b8583 815 return ret;
19e70852
DG
816}
817
e953ef25 818/*
050349bb 819 * Disable a kernel event.
e953ef25 820 */
19e70852 821int kernel_disable_event(struct ltt_kernel_event *event)
e953ef25
DG
822{
823 int ret;
19e70852 824
a0377dfe 825 LTTNG_ASSERT(event);
0525e9ae 826
19e70852 827 ret = kernctl_disable(event->fd);
42224349 828 if (ret < 0) {
d79bd829 829 PERROR("Failed to disable kernel event: name = '%s', fd = %d",
28ab034a
JG
830 event->event->name,
831 event->fd);
19e70852 832 goto error;
e953ef25 833 }
f3ed775e 834
66cefebd 835 event->enabled = false;
19e70852
DG
836 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
837
f34daff7
DG
838 return 0;
839
840error:
d36b8583 841 return ret;
f34daff7 842}
aaf26714 843
352b58f5
JR
844/*
845 * Disable a kernel event notifier.
846 */
28ab034a 847static int kernel_disable_event_notifier_rule(struct ltt_kernel_event_notifier_rule *event)
352b58f5
JR
848{
849 int ret;
850
a0377dfe 851 LTTNG_ASSERT(event);
352b58f5 852
56047f5a 853 lttng::urcu::read_lock_guard read_lock;
352b58f5 854 cds_lfht_del(kernel_token_to_event_notifier_rule_ht, &event->ht_node);
352b58f5
JR
855
856 ret = kernctl_disable(event->fd);
857 if (ret < 0) {
d79bd829 858 PERROR("Failed to disable kernel event notifier: fd = %d, token = %" PRIu64,
28ab034a
JG
859 event->fd,
860 event->token);
352b58f5
JR
861 goto error;
862 }
863
66cefebd 864 event->enabled = false;
28ab034a 865 DBG("Disabled kernel event notifier: fd = %d, token = %" PRIu64, event->fd, event->token);
352b58f5
JR
866
867error:
868 return ret;
869}
870
28ab034a
JG
871static struct process_attr_tracker *
872_kernel_get_process_attr_tracker(struct ltt_kernel_session *session,
873 enum lttng_process_attr process_attr)
55c9e7ca 874{
159b042f
JG
875 switch (process_attr) {
876 case LTTNG_PROCESS_ATTR_PROCESS_ID:
877 return session->tracker_pid;
878 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
879 return session->tracker_vpid;
880 case LTTNG_PROCESS_ATTR_USER_ID:
881 return session->tracker_uid;
882 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
883 return session->tracker_vuid;
884 case LTTNG_PROCESS_ATTR_GROUP_ID:
885 return session->tracker_gid;
886 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
887 return session->tracker_vgid;
55c9e7ca 888 default:
cd9adb8b 889 return nullptr;
55c9e7ca
JR
890 }
891}
6e911cad 892
28ab034a
JG
893const struct process_attr_tracker *
894kernel_get_process_attr_tracker(struct ltt_kernel_session *session,
895 enum lttng_process_attr process_attr)
ccf10263 896{
28ab034a
JG
897 return (const struct process_attr_tracker *) _kernel_get_process_attr_tracker(session,
898 process_attr);
159b042f 899}
7c493d31 900
28ab034a
JG
901enum lttng_error_code
902kernel_process_attr_tracker_set_tracking_policy(struct ltt_kernel_session *session,
903 enum lttng_process_attr process_attr,
904 enum lttng_tracking_policy policy)
159b042f
JG
905{
906 int ret;
907 enum lttng_error_code ret_code = LTTNG_OK;
908 struct process_attr_tracker *tracker =
28ab034a 909 _kernel_get_process_attr_tracker(session, process_attr);
159b042f 910 enum lttng_tracking_policy previous_policy;
55c9e7ca 911
159b042f
JG
912 if (!tracker) {
913 ret_code = LTTNG_ERR_INVALID;
914 goto end;
7c493d31 915 }
ccf10263 916
159b042f
JG
917 previous_policy = process_attr_tracker_get_tracking_policy(tracker);
918 ret = process_attr_tracker_set_tracking_policy(tracker, policy);
919 if (ret) {
920 ret_code = LTTNG_ERR_UNK;
921 goto end;
55c9e7ca 922 }
7c493d31 923
159b042f 924 if (previous_policy == policy) {
55c9e7ca 925 goto end;
7c493d31 926 }
55c9e7ca 927
159b042f
JG
928 switch (policy) {
929 case LTTNG_TRACKING_POLICY_INCLUDE_ALL:
930 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
931 /*
932 * Maintain a special case for the process ID process
933 * attribute tracker as it was the only supported
934 * attribute prior to 2.12.
935 */
936 ret = kernctl_track_pid(session->fd, -1);
937 } else {
938 ret = kernctl_track_id(session->fd, process_attr, -1);
55c9e7ca
JR
939 }
940 break;
159b042f
JG
941 case LTTNG_TRACKING_POLICY_EXCLUDE_ALL:
942 case LTTNG_TRACKING_POLICY_INCLUDE_SET:
943 /* fall-through. */
944 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
945 /*
946 * Maintain a special case for the process ID process
947 * attribute tracker as it was the only supported
948 * attribute prior to 2.12.
949 */
950 ret = kernctl_untrack_pid(session->fd, -1);
951 } else {
952 ret = kernctl_untrack_id(session->fd, process_attr, -1);
55c9e7ca
JR
953 }
954 break;
955 default:
159b042f 956 abort();
55c9e7ca 957 }
159b042f 958 /* kern-ctl error handling */
32af2c95 959 switch (-ret) {
159b042f
JG
960 case 0:
961 ret_code = LTTNG_OK;
962 break;
7c493d31 963 case EINVAL:
159b042f 964 ret_code = LTTNG_ERR_INVALID;
55c9e7ca 965 break;
7c493d31 966 case ENOMEM:
159b042f 967 ret_code = LTTNG_ERR_NOMEM;
55c9e7ca
JR
968 break;
969 case EEXIST:
159b042f 970 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
55c9e7ca 971 break;
7c493d31 972 default:
159b042f 973 ret_code = LTTNG_ERR_UNK;
55c9e7ca
JR
974 break;
975 }
55c9e7ca 976end:
159b042f 977 return ret_code;
ccf10263
MD
978}
979
28ab034a
JG
980enum lttng_error_code
981kernel_process_attr_tracker_inclusion_set_add_value(struct ltt_kernel_session *session,
982 enum lttng_process_attr process_attr,
983 const struct process_attr_value *value)
a5dfbb9d 984{
159b042f
JG
985 int ret, integral_value;
986 enum lttng_error_code ret_code;
987 struct process_attr_tracker *tracker;
988 enum process_attr_tracker_status status;
a5dfbb9d 989
159b042f
JG
990 /*
991 * Convert process attribute tracker value to the integral
992 * representation required by the kern-ctl API.
993 */
994 switch (process_attr) {
995 case LTTNG_PROCESS_ATTR_PROCESS_ID:
996 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
997 integral_value = (int) value->value.pid;
55c9e7ca 998 break;
159b042f
JG
999 case LTTNG_PROCESS_ATTR_USER_ID:
1000 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1001 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1002 uid_t uid;
1003
28ab034a 1004 ret_code = utils_user_id_from_name(value->value.user_name, &uid);
159b042f
JG
1005 if (ret_code != LTTNG_OK) {
1006 goto end;
1007 }
1008 integral_value = (int) uid;
1009 } else {
1010 integral_value = (int) value->value.uid;
55c9e7ca
JR
1011 }
1012 break;
159b042f
JG
1013 case LTTNG_PROCESS_ATTR_GROUP_ID:
1014 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1015 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1016 gid_t gid;
1017
28ab034a 1018 ret_code = utils_group_id_from_name(value->value.group_name, &gid);
159b042f
JG
1019 if (ret_code != LTTNG_OK) {
1020 goto end;
1021 }
1022 integral_value = (int) gid;
1023 } else {
1024 integral_value = (int) value->value.gid;
a5dfbb9d 1025 }
55c9e7ca
JR
1026 break;
1027 default:
159b042f
JG
1028 ret_code = LTTNG_ERR_INVALID;
1029 goto end;
55c9e7ca
JR
1030 }
1031
159b042f
JG
1032 tracker = _kernel_get_process_attr_tracker(session, process_attr);
1033 if (!tracker) {
1034 ret_code = LTTNG_ERR_INVALID;
1035 goto end;
1036 }
1037
1038 status = process_attr_tracker_inclusion_set_add_value(tracker, value);
1039 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1040 switch (status) {
1041 case PROCESS_ATTR_TRACKER_STATUS_EXISTS:
1042 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
1043 break;
1044 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1045 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1046 break;
1047 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1048 default:
1049 ret_code = LTTNG_ERR_UNK;
1050 break;
1051 }
1052 goto end;
1053 }
1054
1055 DBG("Kernel track %s %d for session id %" PRIu64,
28ab034a
JG
1056 lttng_process_attr_to_string(process_attr),
1057 integral_value,
1058 session->id);
159b042f
JG
1059 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
1060 /*
1061 * Maintain a special case for the process ID process attribute
1062 * tracker as it was the only supported attribute prior to 2.12.
1063 */
1064 ret = kernctl_track_pid(session->fd, integral_value);
1065 } else {
28ab034a 1066 ret = kernctl_track_id(session->fd, process_attr, integral_value);
159b042f
JG
1067 }
1068 if (ret == 0) {
1069 ret_code = LTTNG_OK;
1070 goto end;
1071 }
1072
1073 kernel_wait_quiescent();
1074
1075 /* kern-ctl error handling */
55c9e7ca 1076 switch (-ret) {
159b042f
JG
1077 case 0:
1078 ret_code = LTTNG_OK;
1079 break;
55c9e7ca 1080 case EINVAL:
159b042f 1081 ret_code = LTTNG_ERR_INVALID;
55c9e7ca
JR
1082 break;
1083 case ENOMEM:
159b042f 1084 ret_code = LTTNG_ERR_NOMEM;
55c9e7ca
JR
1085 break;
1086 case EEXIST:
159b042f 1087 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
55c9e7ca
JR
1088 break;
1089 default:
159b042f 1090 ret_code = LTTNG_ERR_UNK;
55c9e7ca 1091 break;
a5dfbb9d
MD
1092 }
1093
159b042f 1094 /* Attempt to remove the value from the tracker. */
28ab034a 1095 status = process_attr_tracker_inclusion_set_remove_value(tracker, value);
159b042f
JG
1096 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1097 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
28ab034a
JG
1098 lttng_process_attr_to_string(process_attr),
1099 integral_value);
55c9e7ca 1100 }
a5dfbb9d 1101end:
159b042f 1102 return ret_code;
55c9e7ca 1103}
a5dfbb9d 1104
28ab034a
JG
1105enum lttng_error_code
1106kernel_process_attr_tracker_inclusion_set_remove_value(struct ltt_kernel_session *session,
1107 enum lttng_process_attr process_attr,
1108 const struct process_attr_value *value)
55c9e7ca 1109{
159b042f
JG
1110 int ret, integral_value;
1111 enum lttng_error_code ret_code;
1112 struct process_attr_tracker *tracker;
1113 enum process_attr_tracker_status status;
1114
1115 /*
1116 * Convert process attribute tracker value to the integral
1117 * representation required by the kern-ctl API.
1118 */
1119 switch (process_attr) {
1120 case LTTNG_PROCESS_ATTR_PROCESS_ID:
1121 case LTTNG_PROCESS_ATTR_VIRTUAL_PROCESS_ID:
1122 integral_value = (int) value->value.pid;
1123 break;
1124 case LTTNG_PROCESS_ATTR_USER_ID:
1125 case LTTNG_PROCESS_ATTR_VIRTUAL_USER_ID:
1126 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_USER_NAME) {
1127 uid_t uid;
1128
28ab034a 1129 ret_code = utils_user_id_from_name(value->value.user_name, &uid);
159b042f
JG
1130 if (ret_code != LTTNG_OK) {
1131 goto end;
1132 }
1133 integral_value = (int) uid;
1134 } else {
1135 integral_value = (int) value->value.uid;
1136 }
1137 break;
1138 case LTTNG_PROCESS_ATTR_GROUP_ID:
1139 case LTTNG_PROCESS_ATTR_VIRTUAL_GROUP_ID:
1140 if (value->type == LTTNG_PROCESS_ATTR_VALUE_TYPE_GROUP_NAME) {
1141 gid_t gid;
1142
28ab034a 1143 ret_code = utils_group_id_from_name(value->value.group_name, &gid);
159b042f
JG
1144 if (ret_code != LTTNG_OK) {
1145 goto end;
1146 }
1147 integral_value = (int) gid;
1148 } else {
1149 integral_value = (int) value->value.gid;
1150 }
1151 break;
1152 default:
1153 ret_code = LTTNG_ERR_INVALID;
1154 goto end;
1155 }
55c9e7ca 1156
159b042f
JG
1157 tracker = _kernel_get_process_attr_tracker(session, process_attr);
1158 if (!tracker) {
1159 ret_code = LTTNG_ERR_INVALID;
a7a533cd 1160 goto end;
a5dfbb9d 1161 }
a7a533cd 1162
28ab034a 1163 status = process_attr_tracker_inclusion_set_remove_value(tracker, value);
159b042f
JG
1164 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1165 switch (status) {
1166 case PROCESS_ATTR_TRACKER_STATUS_MISSING:
1167 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1168 break;
1169 case PROCESS_ATTR_TRACKER_STATUS_INVALID_TRACKING_POLICY:
1170 ret_code = LTTNG_ERR_PROCESS_ATTR_TRACKER_INVALID_TRACKING_POLICY;
1171 break;
1172 case PROCESS_ATTR_TRACKER_STATUS_ERROR:
1173 default:
1174 ret_code = LTTNG_ERR_UNK;
1175 break;
1176 }
a7a533cd
JR
1177 goto end;
1178 }
1179
159b042f 1180 DBG("Kernel track %s %d for session id %" PRIu64,
28ab034a
JG
1181 lttng_process_attr_to_string(process_attr),
1182 integral_value,
1183 session->id);
159b042f
JG
1184 if (process_attr == LTTNG_PROCESS_ATTR_PROCESS_ID) {
1185 /*
1186 * Maintain a special case for the process ID process attribute
1187 * tracker as it was the only supported attribute prior to 2.12.
1188 */
1189 ret = kernctl_untrack_pid(session->fd, integral_value);
1190 } else {
28ab034a 1191 ret = kernctl_untrack_id(session->fd, process_attr, integral_value);
159b042f
JG
1192 }
1193 if (ret == 0) {
1194 ret_code = LTTNG_OK;
1195 goto end;
1196 }
1197 kernel_wait_quiescent();
1198
1199 /* kern-ctl error handling */
1200 switch (-ret) {
1201 case 0:
1202 ret_code = LTTNG_OK;
1203 break;
1204 case EINVAL:
1205 ret_code = LTTNG_ERR_INVALID;
1206 break;
1207 case ENOMEM:
1208 ret_code = LTTNG_ERR_NOMEM;
1209 break;
1210 case ENOENT:
1211 ret_code = LTTNG_ERR_PROCESS_ATTR_MISSING;
1212 break;
1213 default:
1214 ret_code = LTTNG_ERR_UNK;
1215 break;
1216 }
1217
1218 /* Attempt to add the value to the tracker. */
28ab034a 1219 status = process_attr_tracker_inclusion_set_add_value(tracker, value);
159b042f
JG
1220 if (status != PROCESS_ATTR_TRACKER_STATUS_OK) {
1221 ERR("Failed to roll-back the tracking of kernel %s process attribute %d while handling a kern-ctl error",
28ab034a
JG
1222 lttng_process_attr_to_string(process_attr),
1223 integral_value);
159b042f 1224 }
a7a533cd 1225end:
159b042f 1226 return ret_code;
a5dfbb9d
MD
1227}
1228
aaf26714 1229/*
050349bb
DG
1230 * Create kernel metadata, open from the kernel tracer and add it to the
1231 * kernel session.
aaf26714 1232 */
a4b92340 1233int kernel_open_metadata(struct ltt_kernel_session *session)
aaf26714
DG
1234{
1235 int ret;
cd9adb8b 1236 struct ltt_kernel_metadata *lkm = nullptr;
aaf26714 1237
a0377dfe 1238 LTTNG_ASSERT(session);
0525e9ae 1239
54012638 1240 /* Allocate kernel metadata */
a4b92340 1241 lkm = trace_kernel_create_metadata();
cd9adb8b 1242 if (lkm == nullptr) {
aaf26714
DG
1243 goto error;
1244 }
1245
54012638 1246 /* Kernel tracer metadata creation */
f3ed775e 1247 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
aaf26714 1248 if (ret < 0) {
74024a21 1249 goto error_open;
aaf26714
DG
1250 }
1251
8c0faa1d 1252 lkm->fd = ret;
d40f0359 1253 lkm->key = ++next_kernel_channel_key;
7b395890
DG
1254 /* Prevent fd duplication after execlp() */
1255 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
1256 if (ret < 0) {
df0f840b 1257 PERROR("fcntl session fd");
7b395890
DG
1258 }
1259
aaf26714 1260 session->metadata = lkm;
8c0faa1d 1261
00e2e675 1262 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
8c0faa1d
DG
1263
1264 return 0;
1265
74024a21
DG
1266error_open:
1267 trace_kernel_destroy_metadata(lkm);
8c0faa1d 1268error:
54012638 1269 return -1;
8c0faa1d
DG
1270}
1271
1272/*
050349bb 1273 * Start tracing session.
8c0faa1d
DG
1274 */
1275int kernel_start_session(struct ltt_kernel_session *session)
1276{
1277 int ret;
1278
a0377dfe 1279 LTTNG_ASSERT(session);
0525e9ae 1280
8c0faa1d
DG
1281 ret = kernctl_start_session(session->fd);
1282 if (ret < 0) {
df0f840b 1283 PERROR("ioctl start session");
8c0faa1d
DG
1284 goto error;
1285 }
1286
1287 DBG("Kernel session started");
1288
1289 return 0;
1290
1291error:
1292 return ret;
1293}
1294
f3ed775e 1295/*
050349bb 1296 * Make a kernel wait to make sure in-flight probe have completed.
f3ed775e 1297 */
cd9adb8b 1298void kernel_wait_quiescent()
f3ed775e
DG
1299{
1300 int ret;
7d268848 1301 int fd = kernel_tracer_fd;
f3ed775e
DG
1302
1303 DBG("Kernel quiescent wait on %d", fd);
1304
1305 ret = kernctl_wait_quiescent(fd);
1306 if (ret < 0) {
df0f840b 1307 PERROR("wait quiescent ioctl");
f3ed775e
DG
1308 ERR("Kernel quiescent wait failed");
1309 }
1310}
1311
1312/*
f3ed775e
DG
1313 * Force flush buffer of metadata.
1314 */
1315int kernel_metadata_flush_buffer(int fd)
1316{
1317 int ret;
1318
169d2cb7
DG
1319 DBG("Kernel flushing metadata buffer on fd %d", fd);
1320
f3ed775e
DG
1321 ret = kernctl_buffer_flush(fd);
1322 if (ret < 0) {
00e2e675 1323 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
f3ed775e
DG
1324 }
1325
1326 return 0;
1327}
1328
1329/*
050349bb 1330 * Force flush buffer for channel.
f3ed775e
DG
1331 */
1332int kernel_flush_buffer(struct ltt_kernel_channel *channel)
1333{
1334 int ret;
1335 struct ltt_kernel_stream *stream;
1336
a0377dfe 1337 LTTNG_ASSERT(channel);
0525e9ae 1338
f3ed775e
DG
1339 DBG("Flush buffer for channel %s", channel->channel->name);
1340
28ab034a 1341 cds_list_for_each_entry (stream, &channel->stream_list.head, list) {
f3ed775e
DG
1342 DBG("Flushing channel stream %d", stream->fd);
1343 ret = kernctl_buffer_flush(stream->fd);
1344 if (ret < 0) {
df0f840b 1345 PERROR("ioctl");
28ab034a 1346 ERR("Fail to flush buffer for stream %d (ret: %d)", stream->fd, ret);
f3ed775e
DG
1347 }
1348 }
1349
1350 return 0;
1351}
1352
8c0faa1d 1353/*
050349bb 1354 * Stop tracing session.
8c0faa1d
DG
1355 */
1356int kernel_stop_session(struct ltt_kernel_session *session)
1357{
1358 int ret;
1359
a0377dfe 1360 LTTNG_ASSERT(session);
0525e9ae 1361
8c0faa1d
DG
1362 ret = kernctl_stop_session(session->fd);
1363 if (ret < 0) {
1364 goto error;
1365 }
1366
1367 DBG("Kernel session stopped");
1368
1369 return 0;
1370
1371error:
1372 return ret;
1373}
1374
1375/*
050349bb
DG
1376 * Open stream of channel, register it to the kernel tracer and add it
1377 * to the stream list of the channel.
8c0faa1d 1378 *
1cfb4b98
MD
1379 * Note: given that the streams may appear in random order wrt CPU
1380 * number (e.g. cpu hotplug), the index value of the stream number in
1381 * the stream name is not necessarily linked to the CPU number.
1382 *
050349bb 1383 * Return the number of created stream. Else, a negative value.
8c0faa1d 1384 */
f3ed775e 1385int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
8c0faa1d 1386{
1cfb4b98 1387 int ret;
8c0faa1d
DG
1388 struct ltt_kernel_stream *lks;
1389
a0377dfe 1390 LTTNG_ASSERT(channel);
0525e9ae 1391
5a47c6a2 1392 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
28ab034a 1393 lks = trace_kernel_create_stream(channel->channel->name, channel->stream_count);
cd9adb8b 1394 if (lks == nullptr) {
799e2c4f
MD
1395 ret = close(ret);
1396 if (ret) {
1397 PERROR("close");
1398 }
8c0faa1d
DG
1399 goto error;
1400 }
1401
1402 lks->fd = ret;
7b395890
DG
1403 /* Prevent fd duplication after execlp() */
1404 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
1405 if (ret < 0) {
df0f840b 1406 PERROR("fcntl session fd");
7b395890
DG
1407 }
1408
1624d5b7
JD
1409 lks->tracefile_size = channel->channel->attr.tracefile_size;
1410 lks->tracefile_count = channel->channel->attr.tracefile_count;
1411
1cfb4b98 1412 /* Add stream to channel stream list */
8c0faa1d
DG
1413 cds_list_add(&lks->list, &channel->stream_list.head);
1414 channel->stream_count++;
8c0faa1d 1415
28ab034a 1416 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd, lks->state);
54012638 1417 }
8c0faa1d
DG
1418
1419 return channel->stream_count;
1420
1421error:
54012638 1422 return -1;
8c0faa1d
DG
1423}
1424
1425/*
050349bb 1426 * Open the metadata stream and set it to the kernel session.
8c0faa1d 1427 */
f3ed775e 1428int kernel_open_metadata_stream(struct ltt_kernel_session *session)
8c0faa1d
DG
1429{
1430 int ret;
1431
a0377dfe 1432 LTTNG_ASSERT(session);
0525e9ae 1433
8c0faa1d
DG
1434 ret = kernctl_create_stream(session->metadata->fd);
1435 if (ret < 0) {
df0f840b 1436 PERROR("kernel create metadata stream");
8c0faa1d
DG
1437 goto error;
1438 }
1439
1440 DBG("Kernel metadata stream created (fd: %d)", ret);
1441 session->metadata_stream_fd = ret;
7b395890
DG
1442 /* Prevent fd duplication after execlp() */
1443 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1444 if (ret < 0) {
df0f840b 1445 PERROR("fcntl session fd");
7b395890 1446 }
aaf26714
DG
1447
1448 return 0;
1449
1450error:
54012638 1451 return -1;
aaf26714 1452}
2ef84c95
DG
1453
1454/*
9f19cc17 1455 * Get the event list from the kernel tracer and return the number of elements.
2ef84c95 1456 */
7d268848 1457ssize_t kernel_list_events(struct lttng_event **events)
2ef84c95 1458{
53efb85a 1459 int fd, ret;
9f19cc17
DG
1460 char *event;
1461 size_t nbmem, count = 0;
2ef84c95 1462 FILE *fp;
9f19cc17 1463 struct lttng_event *elist;
2ef84c95 1464
a0377dfe 1465 LTTNG_ASSERT(events);
0525e9ae 1466
7d268848 1467 fd = kernctl_tracepoint_list(kernel_tracer_fd);
2ef84c95 1468 if (fd < 0) {
df0f840b 1469 PERROR("kernel tracepoint list");
2ef84c95
DG
1470 goto error;
1471 }
1472
1473 fp = fdopen(fd, "r");
cd9adb8b 1474 if (fp == nullptr) {
df0f840b 1475 PERROR("kernel tracepoint list fdopen");
61b73b12 1476 goto error_fp;
2ef84c95
DG
1477 }
1478
1479 /*
1480 * Init memory size counter
1481 * See kernel-ctl.h for explanation of this value
1482 */
6725fe19 1483 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
64803277 1484 elist = calloc<lttng_event>(nbmem);
cd9adb8b 1485 if (elist == nullptr) {
3b870559
MD
1486 PERROR("alloc list events");
1487 count = -ENOMEM;
1488 goto end;
1489 }
2ef84c95 1490
53efb85a 1491 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
6725fe19 1492 if (count >= nbmem) {
3b870559 1493 struct lttng_event *new_elist;
53efb85a 1494 size_t new_nbmem;
3b870559 1495
53efb85a 1496 new_nbmem = nbmem << 1;
28ab034a
JG
1497 DBG("Reallocating event list from %zu to %zu bytes", nbmem, new_nbmem);
1498 new_elist = (lttng_event *) realloc(elist,
1499 new_nbmem * sizeof(struct lttng_event));
cd9adb8b 1500 if (new_elist == nullptr) {
df0f840b 1501 PERROR("realloc list events");
3b870559
MD
1502 free(event);
1503 free(elist);
61b73b12
MD
1504 count = -ENOMEM;
1505 goto end;
2ef84c95 1506 }
53efb85a 1507 /* Zero the new memory */
28ab034a
JG
1508 memset(new_elist + nbmem,
1509 0,
1510 (new_nbmem - nbmem) * sizeof(struct lttng_event));
53efb85a 1511 nbmem = new_nbmem;
3b870559 1512 elist = new_elist;
2ef84c95 1513 }
99497cd0
MD
1514 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1515 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
67b9d018 1516 elist[count].enabled = -1;
9f19cc17 1517 count++;
3b870559 1518 free(event);
2ef84c95
DG
1519 }
1520
9f19cc17 1521 *events = elist;
ced2f820 1522 DBG("Kernel list events done (%zu events)", count);
61b73b12 1523end:
28ab034a 1524 ret = fclose(fp); /* closes both fp and fd */
799e2c4f
MD
1525 if (ret) {
1526 PERROR("fclose");
1527 }
9f19cc17 1528 return count;
2ef84c95 1529
61b73b12 1530error_fp:
799e2c4f
MD
1531 ret = close(fd);
1532 if (ret) {
1533 PERROR("close");
1534 }
2ef84c95
DG
1535error:
1536 return -1;
1537}
096102bd
DG
1538
1539/*
1540 * Get kernel version and validate it.
1541 */
b8e2fb80 1542int kernel_validate_version(struct lttng_kernel_abi_tracer_version *version,
28ab034a 1543 struct lttng_kernel_abi_tracer_abi_version *abi_version)
096102bd
DG
1544{
1545 int ret;
096102bd 1546
7d268848 1547 ret = kernctl_tracer_version(kernel_tracer_fd, version);
096102bd 1548 if (ret < 0) {
521dd134 1549 ERR("Failed to retrieve the lttng-modules version");
096102bd
DG
1550 goto error;
1551 }
1552
1553 /* Validate version */
88076e89 1554 if (version->major != VERSION_MAJOR) {
c052142c 1555 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
28ab034a
JG
1556 version->major,
1557 VERSION_MAJOR);
096102bd 1558 goto error_version;
096102bd 1559 }
7d268848 1560 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
c052142c 1561 if (ret < 0) {
521dd134 1562 ERR("Failed to retrieve lttng-modules ABI version");
c052142c
MD
1563 goto error;
1564 }
b8e2fb80 1565 if (abi_version->major != LTTNG_KERNEL_ABI_MAJOR_VERSION) {
521dd134 1566 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
28ab034a
JG
1567 abi_version->major,
1568 abi_version->minor,
1569 LTTNG_KERNEL_ABI_MAJOR_VERSION);
c052142c
MD
1570 goto error;
1571 }
1572 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
28ab034a
JG
1573 version->major,
1574 version->minor,
1575 abi_version->major,
1576 abi_version->minor);
096102bd
DG
1577 return 0;
1578
1579error_version:
096102bd
DG
1580 ret = -1;
1581
1582error:
521dd134 1583 ERR("Kernel tracer version check failed; kernel tracing will not be available");
096102bd
DG
1584 return ret;
1585}
335a95b7
MD
1586
1587/*
1588 * Kernel work-arounds called at the start of sessiond main().
1589 */
cd9adb8b 1590int init_kernel_workarounds()
335a95b7 1591{
8936c33a 1592 int ret;
335a95b7
MD
1593 FILE *fp;
1594
1595 /*
1596 * boot_id needs to be read once before being used concurrently
1597 * to deal with a Linux kernel race. A fix is proposed for
1598 * upstream, but the work-around is needed for older kernels.
1599 */
1600 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1601 if (!fp) {
1602 goto end_boot_id;
1603 }
1604 while (!feof(fp)) {
1605 char buf[37] = "";
1606
8936c33a
DG
1607 ret = fread(buf, 1, sizeof(buf), fp);
1608 if (ret < 0) {
1609 /* Ignore error, we don't really care */
1610 }
335a95b7 1611 }
799e2c4f
MD
1612 ret = fclose(fp);
1613 if (ret) {
1614 PERROR("fclose");
1615 }
335a95b7 1616end_boot_id:
335a95b7
MD
1617 return 0;
1618}
2f77fc4b
DG
1619
1620/*
d070c424 1621 * Teardown of a kernel session, keeping data required by destroy notifiers.
2f77fc4b
DG
1622 */
1623void kernel_destroy_session(struct ltt_kernel_session *ksess)
1624{
82b69413
JG
1625 struct lttng_trace_chunk *trace_chunk;
1626
cd9adb8b 1627 if (ksess == nullptr) {
2f77fc4b
DG
1628 DBG3("No kernel session when tearing down session");
1629 return;
1630 }
1631
1632 DBG("Tearing down kernel session");
82b69413 1633 trace_chunk = ksess->current_trace_chunk;
2f77fc4b 1634
07b86b52 1635 /*
15dc512a
DG
1636 * Destroy channels on the consumer if at least one FD has been sent and we
1637 * are in no output mode because the streams are in *no* monitor mode so we
1638 * have to send a command to clean them up or else they leaked.
07b86b52 1639 */
15dc512a 1640 if (!ksess->output_traces && ksess->consumer_fds_sent) {
07b86b52
JD
1641 int ret;
1642 struct consumer_socket *socket;
1643 struct lttng_ht_iter iter;
1644
1645 /* For each consumer socket. */
56047f5a
JG
1646 lttng::urcu::read_lock_guard read_lock;
1647
28ab034a
JG
1648 cds_lfht_for_each_entry (
1649 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
07b86b52
JD
1650 struct ltt_kernel_channel *chan;
1651
1652 /* For each channel, ask the consumer to destroy it. */
28ab034a 1653 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
07b86b52
JD
1654 ret = kernel_consumer_destroy_channel(socket, chan);
1655 if (ret < 0) {
1656 /* Consumer is probably dead. Use next socket. */
1657 continue;
1658 }
1659 }
1660 }
1661 }
1662
2f77fc4b
DG
1663 /* Close any relayd session */
1664 consumer_output_send_destroy_relayd(ksess->consumer);
1665
1666 trace_kernel_destroy_session(ksess);
82b69413 1667 lttng_trace_chunk_put(trace_chunk);
2f77fc4b 1668}
fb5f35b6 1669
d070c424
MD
1670/* Teardown of data required by destroy notifiers. */
1671void kernel_free_session(struct ltt_kernel_session *ksess)
1672{
cd9adb8b 1673 if (ksess == nullptr) {
d070c424
MD
1674 return;
1675 }
1676 trace_kernel_free_session(ksess);
1677}
1678
fb5f35b6
DG
1679/*
1680 * Destroy a kernel channel object. It does not do anything on the tracer side.
1681 */
1682void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1683{
cd9adb8b 1684 struct ltt_kernel_session *ksess = nullptr;
fb5f35b6 1685
a0377dfe
FD
1686 LTTNG_ASSERT(kchan);
1687 LTTNG_ASSERT(kchan->channel);
fb5f35b6
DG
1688
1689 DBG3("Kernel destroy channel %s", kchan->channel->name);
1690
1691 /* Update channel count of associated session. */
1692 if (kchan->session) {
1693 /* Keep pointer reference so we can update it after the destroy. */
1694 ksess = kchan->session;
1695 }
1696
1697 trace_kernel_destroy_channel(kchan);
1698
1699 /*
1700 * At this point the kernel channel is not visible anymore. This is safe
1701 * since in order to work on a visible kernel session, the tracing session
1702 * lock (ltt_session.lock) MUST be acquired.
1703 */
1704 if (ksess) {
1705 ksess->channel_count--;
1706 }
1707}
6dc3064a
DG
1708
1709/*
1710 * Take a snapshot for a given kernel session.
1711 *
9a654598 1712 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
6dc3064a 1713 */
28ab034a
JG
1714enum lttng_error_code kernel_snapshot_record(struct ltt_kernel_session *ksess,
1715 const struct consumer_output *output,
1716 uint64_t nb_packets_per_stream)
6dc3064a 1717{
2a06df8d 1718 int err, ret, saved_metadata_fd;
9a654598 1719 enum lttng_error_code status = LTTNG_OK;
6dc3064a
DG
1720 struct consumer_socket *socket;
1721 struct lttng_ht_iter iter;
1722 struct ltt_kernel_metadata *saved_metadata;
cd9adb8b 1723 char *trace_path = nullptr;
5da88b0f 1724 size_t consumer_path_offset = 0;
6dc3064a 1725
a0377dfe
FD
1726 LTTNG_ASSERT(ksess);
1727 LTTNG_ASSERT(ksess->consumer);
1728 LTTNG_ASSERT(output);
6dc3064a
DG
1729
1730 DBG("Kernel snapshot record started");
1731
1732 /* Save current metadata since the following calls will change it. */
1733 saved_metadata = ksess->metadata;
1734 saved_metadata_fd = ksess->metadata_stream_fd;
1735
6dc3064a
DG
1736 ret = kernel_open_metadata(ksess);
1737 if (ret < 0) {
9a654598 1738 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1739 goto error;
1740 }
1741
1742 ret = kernel_open_metadata_stream(ksess);
1743 if (ret < 0) {
9a654598 1744 status = LTTNG_ERR_KERN_META_FAIL;
6dc3064a
DG
1745 goto error_open_stream;
1746 }
1747
28ab034a 1748 trace_path = setup_channel_trace_path(ksess->consumer, "", &consumer_path_offset);
3b967712
MD
1749 if (!trace_path) {
1750 status = LTTNG_ERR_INVALID;
1751 goto error;
1752 }
6dc3064a 1753
56047f5a
JG
1754 {
1755 /* Send metadata to consumer and snapshot everything. */
1756 lttng::urcu::read_lock_guard read_lock;
6dc3064a 1757
56047f5a
JG
1758 cds_lfht_for_each_entry (output->socks->ht, &iter.iter, socket, node.node) {
1759 struct ltt_kernel_channel *chan;
1760
1761 pthread_mutex_lock(socket->lock);
1762 /* This stream must not be monitored by the consumer. */
1763 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1764 pthread_mutex_unlock(socket->lock);
1765 if (ret < 0) {
1766 status = LTTNG_ERR_KERN_META_FAIL;
1767 goto error_consumer;
1768 }
1769
1770 /* For each channel, ask the consumer to snapshot it. */
1771 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
1772 status =
1773 consumer_snapshot_channel(socket,
1774 chan->key,
1775 output,
1776 0,
1777 &trace_path[consumer_path_offset],
1778 nb_packets_per_stream);
1779 if (status != LTTNG_OK) {
1780 (void) kernel_consumer_destroy_metadata(socket,
1781 ksess->metadata);
1782 goto error_consumer;
1783 }
1784 }
1785
1786 /* Snapshot metadata, */
28ab034a 1787 status = consumer_snapshot_channel(socket,
56047f5a 1788 ksess->metadata->key,
28ab034a 1789 output,
56047f5a 1790 1,
28ab034a 1791 &trace_path[consumer_path_offset],
56047f5a 1792 0);
9a654598 1793 if (status != LTTNG_OK) {
6dc3064a
DG
1794 goto error_consumer;
1795 }
6dc3064a 1796
56047f5a
JG
1797 /*
1798 * The metadata snapshot is done, ask the consumer to destroy it since
1799 * it's not monitored on the consumer side.
1800 */
1801 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
6dc3064a
DG
1802 }
1803 }
1804
1805error_consumer:
1806 /* Close newly opened metadata stream. It's now on the consumer side. */
2a06df8d
DG
1807 err = close(ksess->metadata_stream_fd);
1808 if (err < 0) {
6dc3064a
DG
1809 PERROR("close snapshot kernel");
1810 }
1811
1812error_open_stream:
1813 trace_kernel_destroy_metadata(ksess->metadata);
1814error:
1815 /* Restore metadata state.*/
1816 ksess->metadata = saved_metadata;
1817 ksess->metadata_stream_fd = saved_metadata_fd;
3b967712 1818 free(trace_path);
9a654598 1819 return status;
6dc3064a 1820}
834978fd
DG
1821
1822/*
1823 * Get the syscall mask array from the kernel tracer.
1824 *
1825 * Return 0 on success else a negative value. In both case, syscall_mask should
1826 * be freed.
1827 */
1828int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1829{
a0377dfe
FD
1830 LTTNG_ASSERT(syscall_mask);
1831 LTTNG_ASSERT(nr_bits);
834978fd
DG
1832
1833 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1834}
6e21424e 1835
28ab034a 1836static int kernel_tracer_abi_greater_or_equal(unsigned int major, unsigned int minor)
6e21424e 1837{
959e3c66 1838 int ret;
b8e2fb80 1839 struct lttng_kernel_abi_tracer_abi_version abi;
6e21424e 1840
7d268848 1841 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
6e21424e
JR
1842 if (ret < 0) {
1843 ERR("Failed to retrieve lttng-modules ABI version");
1844 goto error;
1845 }
1846
959e3c66
JR
1847 ret = abi.major > major || (abi.major == major && abi.minor >= minor);
1848error:
1849 return ret;
1850}
1851
1852/*
1853 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1854 * version number.
1855 *
1856 * Return 1 on success, 0 when feature is not supported, negative value in case
1857 * of errors.
1858 */
cd9adb8b 1859int kernel_supports_ring_buffer_snapshot_sample_positions()
959e3c66 1860{
6e21424e
JR
1861 /*
1862 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1863 */
959e3c66 1864 return kernel_tracer_abi_greater_or_equal(2, 3);
6e21424e 1865}
5c408ad8 1866
a40a503f
MD
1867/*
1868 * Check for the support of the packet sequence number via abi version number.
1869 *
1870 * Return 1 on success, 0 when feature is not supported, negative value in case
1871 * of errors.
1872 */
cd9adb8b 1873int kernel_supports_ring_buffer_packet_sequence_number()
a40a503f 1874{
a40a503f 1875 /*
3029bc92
JG
1876 * Packet sequence number was introduced in LTTng 2.8,
1877 * lttng-modules ABI 2.1.
a40a503f 1878 */
959e3c66
JR
1879 return kernel_tracer_abi_greater_or_equal(2, 1);
1880}
1881
1882/*
1883 * Check for the support of event notifiers via abi version number.
1884 *
1885 * Return 1 on success, 0 when feature is not supported, negative value in case
1886 * of errors.
1887 */
cd9adb8b 1888int kernel_supports_event_notifiers()
959e3c66
JR
1889{
1890 /*
1891 * Event notifiers were introduced in LTTng 2.13, lttng-modules ABI 2.6.
1892 */
1893 return kernel_tracer_abi_greater_or_equal(2, 6);
a40a503f
MD
1894}
1895
5c408ad8
JD
1896/*
1897 * Rotate a kernel session.
1898 *
d5a1b7aa 1899 * Return LTTNG_OK on success or else an LTTng error code.
5c408ad8 1900 */
d5a1b7aa 1901enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
5c408ad8
JD
1902{
1903 int ret;
d5a1b7aa 1904 enum lttng_error_code status = LTTNG_OK;
5c408ad8
JD
1905 struct consumer_socket *socket;
1906 struct lttng_ht_iter iter;
1907 struct ltt_kernel_session *ksess = session->kernel_session;
1908
a0377dfe
FD
1909 LTTNG_ASSERT(ksess);
1910 LTTNG_ASSERT(ksess->consumer);
5c408ad8 1911
28ab034a 1912 DBG("Rotate kernel session %s started (session %" PRIu64 ")", session->name, session->id);
5c408ad8 1913
56047f5a
JG
1914 {
1915 /*
1916 * Note that this loop will end after one iteration given that there is
1917 * only one kernel consumer.
1918 */
1919 lttng::urcu::read_lock_guard read_lock;
5c408ad8 1920
56047f5a
JG
1921 cds_lfht_for_each_entry (
1922 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
1923 struct ltt_kernel_channel *chan;
1924
1925 /* For each channel, ask the consumer to rotate it. */
1926 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
1927 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1928 chan->key,
1929 session->name);
1930 ret = consumer_rotate_channel(socket,
1931 chan->key,
1932 ksess->consumer,
1933 /* is_metadata_channel */ false);
1934 if (ret < 0) {
1935 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
1936 goto error;
1937 }
1938 }
1939
1940 /*
1941 * Rotate the metadata channel.
1942 */
28ab034a 1943 ret = consumer_rotate_channel(socket,
56047f5a 1944 ksess->metadata->key,
28ab034a 1945 ksess->consumer,
56047f5a 1946 /* is_metadata_channel */ true);
5c408ad8 1947 if (ret < 0) {
7e0de219 1948 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
5c408ad8
JD
1949 goto error;
1950 }
1951 }
5c408ad8
JD
1952 }
1953
5c408ad8 1954error:
d5a1b7aa 1955 return status;
5c408ad8 1956}
d2956687 1957
28ab034a 1958enum lttng_error_code kernel_create_channel_subdirectories(const struct ltt_kernel_session *ksess)
d2956687
JG
1959{
1960 enum lttng_error_code ret = LTTNG_OK;
1961 enum lttng_trace_chunk_status chunk_status;
1962
56047f5a 1963 lttng::urcu::read_lock_guard read_lock;
a0377dfe 1964 LTTNG_ASSERT(ksess->current_trace_chunk);
d2956687
JG
1965
1966 /*
1967 * Create the index subdirectory which will take care
1968 * of implicitly creating the channel's path.
1969 */
1970 chunk_status = lttng_trace_chunk_create_subdirectory(
28ab034a 1971 ksess->current_trace_chunk, DEFAULT_KERNEL_TRACE_DIR "/" DEFAULT_INDEX_DIR);
d2956687
JG
1972 if (chunk_status != LTTNG_TRACE_CHUNK_STATUS_OK) {
1973 ret = LTTNG_ERR_CREATE_DIR_FAIL;
1974 goto error;
1975 }
1976error:
d2956687
JG
1977 return ret;
1978}
7d268848 1979
49cddecd
KS
1980/*
1981 * Get current kernel tracer status
1982 */
1983enum lttng_kernel_tracer_status get_kernel_tracer_status()
1984{
1985 if (!kernel_tracer_status) {
1986 return LTTNG_KERNEL_TRACER_STATUS_ERR_UNKNOWN;
1987 }
1988
1989 return *kernel_tracer_status;
1990}
1991
1992/*
1993 * Sets the kernel tracer status based on the positive errno code
1994 */
1995void set_kernel_tracer_status_from_modules_ret(int code)
1996{
1997 switch (code) {
1998 case ENOENT:
1999 {
2000 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2001 LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_MISSING);
2002 break;
2003 }
2004 case ENOKEY:
2005 case EKEYEXPIRED:
2006 case EKEYREVOKED:
2007 case EKEYREJECTED:
2008 {
2009 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2010 LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_SIGNATURE);
2011 break;
2012 }
2013 default:
2014 {
2015 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2016 LTTNG_KERNEL_TRACER_STATUS_ERR_MODULES_UNKNOWN);
2017 break;
2018 }
2019 }
2020}
2021
7d268848
MD
2022/*
2023 * Setup necessary data for kernel tracer action.
2024 */
cd9adb8b 2025int init_kernel_tracer()
7d268848
MD
2026{
2027 int ret;
2028 bool is_root = !getuid();
2029
07a5d4b5
JG
2030 const auto log_status_on_exit = lttng::make_scope_exit([]() noexcept {
2031 DBG_FMT("Kernel tracer status set to `{}`",
2032 kernel_tracer_status_to_str(*kernel_tracer_status));
2033 });
2034
7d268848
MD
2035 /* Modprobe lttng kernel modules */
2036 ret = modprobe_lttng_control();
2037 if (ret < 0) {
49cddecd 2038 set_kernel_tracer_status_from_modules_ret(-ret);
7d268848
MD
2039 goto error;
2040 }
2041
2042 /* Open debugfs lttng */
2043 kernel_tracer_fd = open(module_proc_lttng, O_RDWR);
2044 if (kernel_tracer_fd < 0) {
2045 DBG("Failed to open %s", module_proc_lttng);
49cddecd
KS
2046 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2047 LTTNG_KERNEL_TRACER_STATUS_ERR_OPEN_PROC_LTTNG);
7d268848
MD
2048 goto error_open;
2049 }
2050
2051 /* Validate kernel version */
28ab034a 2052 ret = kernel_validate_version(&the_kernel_tracer_version, &the_kernel_tracer_abi_version);
7d268848 2053 if (ret < 0) {
49cddecd
KS
2054 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2055 LTTNG_KERNEL_TRACER_STATUS_ERR_VERSION_MISMATCH);
7d268848
MD
2056 goto error_version;
2057 }
2058
2059 ret = modprobe_lttng_data();
2060 if (ret < 0) {
49cddecd 2061 set_kernel_tracer_status_from_modules_ret(-ret);
7d268848
MD
2062 goto error_modules;
2063 }
2064
2065 ret = kernel_supports_ring_buffer_snapshot_sample_positions();
2066 if (ret < 0) {
2067 goto error_modules;
2068 }
7d268848
MD
2069 if (ret < 1) {
2070 WARN("Kernel tracer does not support buffer monitoring. "
28ab034a
JG
2071 "The monitoring timer of channels in the kernel domain "
2072 "will be set to 0 (disabled).");
7d268848
MD
2073 }
2074
352b58f5
JR
2075 ret = kernel_supports_event_notifiers();
2076 if (ret < 0) {
2077 ERR("Failed to check for kernel tracer event notifier support");
49cddecd
KS
2078 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2079 LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
352b58f5
JR
2080 goto error_modules;
2081 }
2082 ret = kernel_create_event_notifier_group(&kernel_tracer_event_notifier_group_fd);
2083 if (ret < 0) {
2084 /* This is not fatal. */
2085 WARN("Failed to create kernel event notifier group");
2086 kernel_tracer_event_notifier_group_fd = -1;
2087 } else {
90aa04a1
FD
2088 enum event_notifier_error_accounting_status error_accounting_status;
2089 enum lttng_error_code error_code_ret =
28ab034a
JG
2090 kernel_create_event_notifier_group_notification_fd(
2091 &kernel_tracer_event_notifier_group_notification_fd);
352b58f5
JR
2092
2093 if (error_code_ret != LTTNG_OK) {
49cddecd
KS
2094 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2095 LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
352b58f5
JR
2096 goto error_modules;
2097 }
2098
90aa04a1 2099 error_accounting_status = event_notifier_error_accounting_register_kernel(
28ab034a 2100 kernel_tracer_event_notifier_group_fd);
90aa04a1
FD
2101 if (error_accounting_status != EVENT_NOTIFIER_ERROR_ACCOUNTING_STATUS_OK) {
2102 ERR("Failed to initialize event notifier error accounting for kernel tracer");
2103 error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_ERROR_ACCOUNTING;
49cddecd
KS
2104 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2105 LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
90aa04a1
FD
2106 goto error_modules;
2107 }
2108
352b58f5 2109 kernel_token_to_event_notifier_rule_ht = cds_lfht_new(
cd9adb8b 2110 DEFAULT_HT_SIZE, 1, 0, CDS_LFHT_AUTO_RESIZE | CDS_LFHT_ACCOUNTING, nullptr);
352b58f5 2111 if (!kernel_token_to_event_notifier_rule_ht) {
49cddecd
KS
2112 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2113 LTTNG_KERNEL_TRACER_STATUS_ERR_NOTIFIER);
352b58f5
JR
2114 goto error_token_ht;
2115 }
2116 }
2117
2118 DBG("Kernel tracer initialized: kernel tracer fd = %d, event notifier group fd = %d, event notifier group notification fd = %d",
28ab034a
JG
2119 kernel_tracer_fd,
2120 kernel_tracer_event_notifier_group_fd,
2121 kernel_tracer_event_notifier_group_notification_fd);
7d268848
MD
2122
2123 ret = syscall_init_table(kernel_tracer_fd);
2124 if (ret < 0) {
2125 ERR("Unable to populate syscall table. Syscall tracing won't "
28ab034a 2126 "work for this session daemon.");
7d268848 2127 }
352b58f5 2128
11927a78
JG
2129 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2130 LTTNG_KERNEL_TRACER_STATUS_INITIALIZED);
7d268848
MD
2131 return 0;
2132
2133error_version:
2134 modprobe_remove_lttng_control();
2135 ret = close(kernel_tracer_fd);
2136 if (ret) {
28ab034a 2137 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd);
7d268848 2138 }
6dcf80d3 2139
7d268848
MD
2140 kernel_tracer_fd = -1;
2141 return LTTNG_ERR_KERN_VERSION;
2142
352b58f5
JR
2143error_token_ht:
2144 ret = close(kernel_tracer_event_notifier_group_notification_fd);
2145 if (ret) {
2146 PERROR("Failed to close kernel tracer event notifier group notification file descriptor: fd = %d",
28ab034a 2147 kernel_tracer_event_notifier_group_notification_fd);
352b58f5
JR
2148 }
2149
6dcf80d3
JG
2150 kernel_tracer_event_notifier_group_notification_fd = -1;
2151
7d268848 2152error_modules:
352b58f5
JR
2153 ret = close(kernel_tracer_event_notifier_group_fd);
2154 if (ret) {
2155 PERROR("Failed to close kernel tracer event notifier group file descriptor: fd = %d",
28ab034a 2156 kernel_tracer_event_notifier_group_fd);
352b58f5
JR
2157 }
2158
6dcf80d3
JG
2159 kernel_tracer_event_notifier_group_fd = -1;
2160
7d268848
MD
2161 ret = close(kernel_tracer_fd);
2162 if (ret) {
28ab034a 2163 PERROR("Failed to close kernel tracer file descriptor: fd = %d", kernel_tracer_fd);
7d268848
MD
2164 }
2165
6dcf80d3
JG
2166 kernel_tracer_fd = -1;
2167
7d268848
MD
2168error_open:
2169 modprobe_remove_lttng_control();
2170
2171error:
2172 WARN("No kernel tracer available");
2173 kernel_tracer_fd = -1;
2174 if (!is_root) {
49cddecd
KS
2175 kernel_tracer_status = nonstd::optional<enum lttng_kernel_tracer_status>(
2176 LTTNG_KERNEL_TRACER_STATUS_ERR_NEED_ROOT);
7d268848
MD
2177 return LTTNG_ERR_NEED_ROOT_SESSIOND;
2178 } else {
2179 return LTTNG_ERR_KERN_NA;
2180 }
2181}
2182
cd9adb8b 2183void cleanup_kernel_tracer()
7d268848 2184{
352b58f5
JR
2185 DBG2("Closing kernel event notifier group notification file descriptor");
2186 if (kernel_tracer_event_notifier_group_notification_fd >= 0) {
b69a1b40 2187 int ret = notification_thread_command_remove_tracer_event_source(
28ab034a
JG
2188 the_notification_thread_handle,
2189 kernel_tracer_event_notifier_group_notification_fd);
352b58f5
JR
2190 if (ret != LTTNG_OK) {
2191 ERR("Failed to remove kernel event notifier notification from notification thread");
2192 }
2193
2194 ret = close(kernel_tracer_event_notifier_group_notification_fd);
2195 if (ret) {
2196 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
28ab034a 2197 kernel_tracer_event_notifier_group_notification_fd);
352b58f5
JR
2198 }
2199
2200 kernel_tracer_event_notifier_group_notification_fd = -1;
2201 }
2202
2203 if (kernel_token_to_event_notifier_rule_ht) {
cd9adb8b 2204 const int ret = cds_lfht_destroy(kernel_token_to_event_notifier_rule_ht, nullptr);
a0377dfe 2205 LTTNG_ASSERT(ret == 0);
352b58f5
JR
2206 }
2207
2208 DBG2("Closing kernel event notifier group file descriptor");
2209 if (kernel_tracer_event_notifier_group_fd >= 0) {
b69a1b40
JG
2210 const int ret = close(kernel_tracer_event_notifier_group_fd);
2211
352b58f5
JR
2212 if (ret) {
2213 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
28ab034a 2214 kernel_tracer_event_notifier_group_fd);
352b58f5
JR
2215 }
2216
2217 kernel_tracer_event_notifier_group_fd = -1;
2218 }
7d268848
MD
2219
2220 DBG2("Closing kernel fd");
2221 if (kernel_tracer_fd >= 0) {
b69a1b40
JG
2222 const int ret = close(kernel_tracer_fd);
2223
7d268848 2224 if (ret) {
352b58f5 2225 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
28ab034a 2226 kernel_tracer_fd);
7d268848 2227 }
352b58f5 2228
7d268848
MD
2229 kernel_tracer_fd = -1;
2230 }
352b58f5 2231
49cddecd 2232 kernel_tracer_status = nonstd::nullopt;
7d268848
MD
2233 free(syscall_table);
2234}
2235
cd9adb8b 2236bool kernel_tracer_is_initialized()
7d268848
MD
2237{
2238 return kernel_tracer_fd >= 0;
2239}
bca212a2
MD
2240
2241/*
2242 * Clear a kernel session.
2243 *
2244 * Return LTTNG_OK on success or else an LTTng error code.
2245 */
2246enum lttng_error_code kernel_clear_session(struct ltt_session *session)
2247{
2248 int ret;
2249 enum lttng_error_code status = LTTNG_OK;
2250 struct consumer_socket *socket;
2251 struct lttng_ht_iter iter;
2252 struct ltt_kernel_session *ksess = session->kernel_session;
2253
a0377dfe
FD
2254 LTTNG_ASSERT(ksess);
2255 LTTNG_ASSERT(ksess->consumer);
bca212a2 2256
28ab034a 2257 DBG("Clear kernel session %s (session %" PRIu64 ")", session->name, session->id);
bca212a2 2258
bca212a2
MD
2259 if (ksess->active) {
2260 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
2261 status = LTTNG_ERR_FATAL;
2262 goto end;
2263 }
2264
56047f5a
JG
2265 {
2266 /*
2267 * Note that this loop will end after one iteration given that there is
2268 * only one kernel consumer.
2269 */
2270 lttng::urcu::read_lock_guard read_lock;
2271
2272 cds_lfht_for_each_entry (
2273 ksess->consumer->socks->ht, &iter.iter, socket, node.node) {
2274 struct ltt_kernel_channel *chan;
2275
2276 /* For each channel, ask the consumer to clear it. */
2277 cds_list_for_each_entry (chan, &ksess->channel_list.head, list) {
2278 DBG("Clear kernel channel %" PRIu64 ", session %s",
2279 chan->key,
2280 session->name);
2281 ret = consumer_clear_channel(socket, chan->key);
2282 if (ret < 0) {
2283 goto error;
2284 }
2285 }
2286
2287 if (!ksess->metadata) {
2288 /*
2289 * Nothing to do for the metadata.
2290 * This is a snapshot session.
2291 * The metadata is genererated on the fly.
2292 */
2293 continue;
bca212a2 2294 }
bca212a2 2295
bca212a2 2296 /*
56047f5a
JG
2297 * Clear the metadata channel.
2298 * Metadata channel is not cleared per se but we still need to
2299 * perform a rotation operation on it behind the scene.
bca212a2 2300 */
56047f5a
JG
2301 ret = consumer_clear_channel(socket, ksess->metadata->key);
2302 if (ret < 0) {
2303 goto error;
2304 }
bca212a2
MD
2305 }
2306 }
2307
2308 goto end;
2309error:
2310 switch (-ret) {
2311 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
28ab034a
JG
2312 status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
2313 break;
bca212a2 2314 default:
28ab034a
JG
2315 status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
2316 break;
bca212a2
MD
2317 }
2318end:
bca212a2
MD
2319 return status;
2320}
352b58f5 2321
28ab034a
JG
2322enum lttng_error_code
2323kernel_create_event_notifier_group_notification_fd(int *event_notifier_group_notification_fd)
352b58f5
JR
2324{
2325 int local_fd = -1, ret;
2326 enum lttng_error_code error_code_ret;
2327
a0377dfe 2328 LTTNG_ASSERT(event_notifier_group_notification_fd);
352b58f5
JR
2329
2330 ret = kernctl_create_event_notifier_group_notification_fd(
28ab034a 2331 kernel_tracer_event_notifier_group_fd);
352b58f5
JR
2332 if (ret < 0) {
2333 PERROR("Failed to create kernel event notifier group notification file descriptor");
2334 error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
2335 goto error;
2336 }
2337
2338 local_fd = ret;
2339
2340 /* Prevent fd duplication after execlp(). */
2341 ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
2342 if (ret < 0) {
2343 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
28ab034a 2344 local_fd);
352b58f5
JR
2345 error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
2346 goto error;
2347 }
2348
28ab034a 2349 DBG("Created kernel notifier group notification file descriptor: fd = %d", local_fd);
352b58f5
JR
2350 error_code_ret = LTTNG_OK;
2351 *event_notifier_group_notification_fd = local_fd;
2352 local_fd = -1;
2353
2354error:
2355 if (local_fd >= 0) {
2356 ret = close(local_fd);
2357 if (ret) {
2358 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
28ab034a 2359 local_fd);
352b58f5
JR
2360 }
2361 }
2362
2363 return error_code_ret;
2364}
2365
28ab034a
JG
2366enum lttng_error_code
2367kernel_destroy_event_notifier_group_notification_fd(int event_notifier_group_notification_fd)
352b58f5
JR
2368{
2369 enum lttng_error_code ret_code = LTTNG_OK;
2370
2371 DBG("Closing event notifier group notification file descriptor: fd = %d",
28ab034a 2372 event_notifier_group_notification_fd);
352b58f5
JR
2373 if (event_notifier_group_notification_fd >= 0) {
2374 const int ret = close(event_notifier_group_notification_fd);
2375 if (ret) {
2376 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
28ab034a 2377 event_notifier_group_notification_fd);
352b58f5
JR
2378 }
2379 }
2380
2381 return ret_code;
2382}
2383
28ab034a 2384static unsigned long hash_trigger(const struct lttng_trigger *trigger)
352b58f5 2385{
28ab034a 2386 const struct lttng_condition *condition = lttng_trigger_get_const_condition(trigger);
352b58f5
JR
2387
2388 return lttng_condition_hash(condition);
2389}
2390
28ab034a 2391static int match_trigger(struct cds_lfht_node *node, const void *key)
352b58f5
JR
2392{
2393 const struct ltt_kernel_event_notifier_rule *event_notifier_rule;
7966af57 2394 const struct lttng_trigger *trigger = (lttng_trigger *) key;
352b58f5 2395
28ab034a
JG
2396 event_notifier_rule =
2397 caa_container_of(node, const struct ltt_kernel_event_notifier_rule, ht_node);
352b58f5
JR
2398
2399 return lttng_trigger_is_equal(trigger, event_notifier_rule->trigger);
2400}
2401
2402static enum lttng_error_code kernel_create_event_notifier_rule(
28ab034a 2403 struct lttng_trigger *trigger, const struct lttng_credentials *creds, uint64_t token)
352b58f5
JR
2404{
2405 int err, fd, ret = 0;
2406 enum lttng_error_code error_code_ret;
2407 enum lttng_condition_status condition_status;
2408 enum lttng_condition_type condition_type;
2409 enum lttng_event_rule_type event_rule_type;
2410 struct ltt_kernel_event_notifier_rule *event_notifier_rule;
b8e2fb80 2411 struct lttng_kernel_abi_event_notifier kernel_event_notifier = {};
bbd6675c 2412 unsigned int capture_bytecode_count = 0, i;
cd9adb8b
JG
2413 const struct lttng_condition *condition = nullptr;
2414 const struct lttng_event_rule *event_rule = nullptr;
bbd6675c 2415 enum lttng_condition_status cond_status;
352b58f5 2416
a0377dfe 2417 LTTNG_ASSERT(trigger);
352b58f5
JR
2418
2419 condition = lttng_trigger_get_const_condition(trigger);
a0377dfe 2420 LTTNG_ASSERT(condition);
352b58f5
JR
2421
2422 condition_type = lttng_condition_get_type(condition);
a0377dfe 2423 LTTNG_ASSERT(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_MATCHES);
352b58f5
JR
2424
2425 /* Does not acquire a reference. */
28ab034a 2426 condition_status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
a0377dfe
FD
2427 LTTNG_ASSERT(condition_status == LTTNG_CONDITION_STATUS_OK);
2428 LTTNG_ASSERT(event_rule);
352b58f5
JR
2429
2430 event_rule_type = lttng_event_rule_get_type(event_rule);
a0377dfe 2431 LTTNG_ASSERT(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
352b58f5 2432
28ab034a
JG
2433 error_code_ret = trace_kernel_create_event_notifier_rule(
2434 trigger,
2435 token,
2436 lttng_condition_event_rule_matches_get_error_counter_index(condition),
2437 &event_notifier_rule);
352b58f5
JR
2438 if (error_code_ret != LTTNG_OK) {
2439 goto error;
2440 }
2441
28ab034a
JG
2442 error_code_ret = trace_kernel_init_event_notifier_from_event_rule(event_rule,
2443 &kernel_event_notifier);
352b58f5 2444 if (error_code_ret != LTTNG_OK) {
b5e3b044 2445 goto free_event;
352b58f5
JR
2446 }
2447
2448 kernel_event_notifier.event.token = event_notifier_rule->token;
90aa04a1 2449 kernel_event_notifier.error_counter_idx =
28ab034a 2450 lttng_condition_event_rule_matches_get_error_counter_index(condition);
352b58f5 2451
28ab034a
JG
2452 fd = kernctl_create_event_notifier(kernel_tracer_event_notifier_group_fd,
2453 &kernel_event_notifier);
352b58f5
JR
2454 if (fd < 0) {
2455 switch (-fd) {
2456 case EEXIST:
2457 error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
2458 break;
2459 case ENOSYS:
2460 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2461 error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
2462 break;
2463 case ENOENT:
2464 WARN("Failed to create kernel event notifier: not found: name = '%s'",
28ab034a 2465 kernel_event_notifier.event.name);
352b58f5
JR
2466 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2467 break;
2468 default:
2469 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
28ab034a
JG
2470 fd,
2471 kernel_event_notifier.event.name);
352b58f5
JR
2472 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2473 }
2474 goto free_event;
2475 }
2476
2477 event_notifier_rule->fd = fd;
2478 /* Prevent fd duplication after execlp(). */
2479 err = fcntl(event_notifier_rule->fd, F_SETFD, FD_CLOEXEC);
2480 if (err < 0) {
2481 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
28ab034a 2482 fd);
352b58f5
JR
2483 error_code_ret = LTTNG_ERR_FATAL;
2484 goto set_cloexec_error;
2485 }
2486
2487 if (event_notifier_rule->filter) {
2488 err = kernctl_filter(event_notifier_rule->fd, event_notifier_rule->filter);
2489 if (err < 0) {
2490 switch (-err) {
2491 case ENOMEM:
2492 error_code_ret = LTTNG_ERR_FILTER_NOMEM;
2493 break;
2494 default:
2495 error_code_ret = LTTNG_ERR_FILTER_INVAL;
2496 break;
2497 }
2498 goto filter_error;
2499 }
2500 }
2501
28ab034a 2502 if (lttng_event_rule_get_type(event_rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_UPROBE) {
352b58f5 2503 ret = userspace_probe_event_rule_add_callsites(
28ab034a 2504 event_rule, creds, event_notifier_rule->fd);
352b58f5
JR
2505 if (ret) {
2506 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2507 goto add_callsite_error;
2508 }
2509 }
2510
d602bd6a 2511 /* Set the capture bytecode if any. */
8dbb86b8 2512 cond_status = lttng_condition_event_rule_matches_get_capture_descriptor_count(
28ab034a 2513 condition, &capture_bytecode_count);
a0377dfe 2514 LTTNG_ASSERT(cond_status == LTTNG_CONDITION_STATUS_OK);
bbd6675c
JR
2515
2516 for (i = 0; i < capture_bytecode_count; i++) {
2517 const struct lttng_bytecode *capture_bytecode =
28ab034a
JG
2518 lttng_condition_event_rule_matches_get_capture_bytecode_at_index(condition,
2519 i);
bbd6675c 2520
cd9adb8b 2521 if (capture_bytecode == nullptr) {
bbd6675c
JR
2522 ERR("Unexpected NULL capture bytecode on condition");
2523 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
e8b1187d 2524 goto capture_error;
bbd6675c
JR
2525 }
2526
2527 ret = kernctl_capture(event_notifier_rule->fd, capture_bytecode);
2528 if (ret < 0) {
2529 ERR("Failed to set capture bytecode on event notifier rule fd: fd = %d",
28ab034a 2530 event_notifier_rule->fd);
bbd6675c 2531 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
e8b1187d 2532 goto capture_error;
bbd6675c
JR
2533 }
2534 }
2535
352b58f5
JR
2536 err = kernctl_enable(event_notifier_rule->fd);
2537 if (err < 0) {
2538 switch (-err) {
2539 case EEXIST:
2540 error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
2541 break;
2542 default:
2543 PERROR("enable kernel event notifier");
2544 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2545 break;
2546 }
2547 goto enable_error;
2548 }
2549
2550 /* Add trigger to kernel token mapping in the hash table. */
56047f5a
JG
2551 {
2552 lttng::urcu::read_lock_guard read_lock;
2553 cds_lfht_add(kernel_token_to_event_notifier_rule_ht,
2554 hash_trigger(trigger),
2555 &event_notifier_rule->ht_node);
2556 }
352b58f5
JR
2557
2558 DBG("Created kernel event notifier: name = '%s', fd = %d",
28ab034a
JG
2559 kernel_event_notifier.event.name,
2560 event_notifier_rule->fd);
352b58f5
JR
2561
2562 return LTTNG_OK;
2563
e8b1187d 2564capture_error:
352b58f5
JR
2565add_callsite_error:
2566enable_error:
2567set_cloexec_error:
2568filter_error:
28ab034a
JG
2569{
2570 const int close_ret = close(event_notifier_rule->fd);
352b58f5 2571
28ab034a
JG
2572 if (close_ret) {
2573 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2574 event_notifier_rule->fd);
352b58f5 2575 }
28ab034a 2576}
352b58f5
JR
2577free_event:
2578 free(event_notifier_rule);
2579error:
2580 return error_code_ret;
2581}
2582
28ab034a
JG
2583enum lttng_error_code kernel_register_event_notifier(struct lttng_trigger *trigger,
2584 const struct lttng_credentials *cmd_creds)
352b58f5
JR
2585{
2586 enum lttng_error_code ret;
2587 enum lttng_condition_status status;
2588 enum lttng_domain_type domain_type;
2589 const struct lttng_event_rule *event_rule;
28ab034a 2590 const struct lttng_condition *const condition = lttng_trigger_get_const_condition(trigger);
352b58f5
JR
2591 const uint64_t token = lttng_trigger_get_tracer_token(trigger);
2592
a0377dfe 2593 LTTNG_ASSERT(condition);
352b58f5
JR
2594
2595 /* Does not acquire a reference to the event rule. */
28ab034a 2596 status = lttng_condition_event_rule_matches_get_rule(condition, &event_rule);
a0377dfe 2597 LTTNG_ASSERT(status == LTTNG_CONDITION_STATUS_OK);
352b58f5
JR
2598
2599 domain_type = lttng_event_rule_get_domain_type(event_rule);
a0377dfe 2600 LTTNG_ASSERT(domain_type == LTTNG_DOMAIN_KERNEL);
352b58f5
JR
2601
2602 ret = kernel_create_event_notifier_rule(trigger, cmd_creds, token);
2603 if (ret != LTTNG_OK) {
124dda43 2604 ERR("Failed to create kernel event notifier rule");
352b58f5
JR
2605 }
2606
2607 return ret;
2608}
2609
28ab034a 2610enum lttng_error_code kernel_unregister_event_notifier(const struct lttng_trigger *trigger)
352b58f5
JR
2611{
2612 struct ltt_kernel_event_notifier_rule *token_event_rule_element;
2613 struct cds_lfht_node *node;
2614 struct cds_lfht_iter iter;
2615 enum lttng_error_code error_code_ret;
2616 int ret;
2617
56047f5a 2618 lttng::urcu::read_lock_guard read_lock;
352b58f5
JR
2619
2620 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht,
28ab034a
JG
2621 hash_trigger(trigger),
2622 match_trigger,
2623 trigger,
2624 &iter);
352b58f5
JR
2625
2626 node = cds_lfht_iter_get_node(&iter);
2627 if (!node) {
2628 error_code_ret = LTTNG_ERR_TRIGGER_NOT_FOUND;
2629 goto error;
2630 }
2631
28ab034a
JG
2632 token_event_rule_element =
2633 caa_container_of(node, struct ltt_kernel_event_notifier_rule, ht_node);
352b58f5
JR
2634
2635 ret = kernel_disable_event_notifier_rule(token_event_rule_element);
2636 if (ret) {
2637 error_code_ret = LTTNG_ERR_FATAL;
2638 goto error;
2639 }
2640
2641 trace_kernel_destroy_event_notifier_rule(token_event_rule_element);
2642 error_code_ret = LTTNG_OK;
2643
2644error:
352b58f5
JR
2645
2646 return error_code_ret;
2647}
2648
cd9adb8b 2649int kernel_get_notification_fd()
352b58f5
JR
2650{
2651 return kernel_tracer_event_notifier_group_notification_fd;
2652}
This page took 0.242407 seconds and 4 git commands to generate.