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