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