sessiond: kernel: misleading log message on event rule creation failure
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
1 /*
2 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #define _LGPL_SOURCE
9 #include <fcntl.h>
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <inttypes.h>
15 #include <sys/types.h>
16
17 #include <common/common.h>
18 #include <common/hashtable/utils.h>
19 #include <common/trace-chunk.h>
20 #include <common/kernel-ctl/kernel-ctl.h>
21 #include <common/kernel-ctl/kernel-ioctl.h>
22 #include <common/sessiond-comm/sessiond-comm.h>
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>
28
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
37 #include "lttng-sessiond.h"
38 #include "lttng-syscall.h"
39 #include "condition-internal.h"
40 #include "consumer.h"
41 #include "kernel.h"
42 #include "kernel-consumer.h"
43 #include "kern-modules.h"
44 #include "utils.h"
45 #include "rotate.h"
46 #include "modprobe.h"
47 #include "tracker.h"
48 #include "notification-thread-commands.h"
49
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 */
54 static uint64_t next_kernel_channel_key;
55
56 static const char *module_proc_lttng = "/proc/lttng";
57
58 static int kernel_tracer_fd = -1;
59 static int kernel_tracer_event_notifier_group_fd = -1;
60 static int kernel_tracer_event_notifier_group_notification_fd = -1;
61 static struct cds_lfht *kernel_token_to_event_notifier_rule_ht;
62
63 /*
64 * Add context on a kernel channel.
65 *
66 * Assumes the ownership of ctx.
67 */
68 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
69 struct ltt_kernel_context *ctx)
70 {
71 int ret;
72
73 assert(chan);
74 assert(ctx);
75
76 DBG("Adding context to channel %s", chan->channel->name);
77 ret = kernctl_add_context(chan->fd, &ctx->ctx);
78 if (ret < 0) {
79 switch (-ret) {
80 case ENOSYS:
81 /* Exists but not available for this kernel */
82 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
83 goto error;
84 case EEXIST:
85 /* If EEXIST, we just ignore the error */
86 ret = 0;
87 goto end;
88 default:
89 PERROR("add context ioctl");
90 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
91 goto error;
92 }
93 }
94 ret = 0;
95
96 end:
97 cds_list_add_tail(&ctx->list, &chan->ctx_list);
98 ctx->in_list = true;
99 ctx = NULL;
100 error:
101 if (ctx) {
102 trace_kernel_destroy_context(ctx);
103 }
104 return ret;
105 }
106
107 /*
108 * Create a new kernel session, register it to the kernel tracer and add it to
109 * the session daemon session.
110 */
111 int kernel_create_session(struct ltt_session *session)
112 {
113 int ret;
114 struct ltt_kernel_session *lks;
115
116 assert(session);
117
118 /* Allocate data structure */
119 lks = trace_kernel_create_session();
120 if (lks == NULL) {
121 ret = -1;
122 goto error;
123 }
124
125 /* Kernel tracer session creation */
126 ret = kernctl_create_session(kernel_tracer_fd);
127 if (ret < 0) {
128 PERROR("ioctl kernel create session");
129 goto error;
130 }
131
132 lks->fd = ret;
133 /* Prevent fd duplication after execlp() */
134 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
135 if (ret < 0) {
136 PERROR("fcntl session fd");
137 }
138
139 lks->id = session->id;
140 lks->consumer_fds_sent = 0;
141 session->kernel_session = lks;
142
143 DBG("Kernel session created (fd: %d)", lks->fd);
144
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
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
165 return 0;
166
167 error:
168 if (lks) {
169 trace_kernel_destroy_session(lks);
170 trace_kernel_free_session(lks);
171 }
172 return ret;
173 }
174
175 /*
176 * Create a kernel channel, register it to the kernel tracer and add it to the
177 * kernel session.
178 */
179 int kernel_create_channel(struct ltt_kernel_session *session,
180 struct lttng_channel *chan)
181 {
182 int ret;
183 struct ltt_kernel_channel *lkc;
184
185 assert(session);
186 assert(chan);
187
188 /* Allocate kernel channel */
189 lkc = trace_kernel_create_channel(chan);
190 if (lkc == NULL) {
191 goto error;
192 }
193
194 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
195 chan->name, lkc->channel->attr.overwrite,
196 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
197 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
198 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
199
200 /* Kernel tracer channel creation */
201 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
202 if (ret < 0) {
203 PERROR("ioctl kernel create channel");
204 goto error;
205 }
206
207 /* Setup the channel fd */
208 lkc->fd = ret;
209 /* Prevent fd duplication after execlp() */
210 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
211 if (ret < 0) {
212 PERROR("fcntl session fd");
213 }
214
215 /* Add channel to session */
216 cds_list_add(&lkc->list, &session->channel_list.head);
217 session->channel_count++;
218 lkc->session = session;
219 lkc->key = ++next_kernel_channel_key;
220
221 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
222 lkc->channel->name, lkc->fd, lkc->key);
223
224 return 0;
225
226 error:
227 if (lkc) {
228 free(lkc->channel);
229 free(lkc);
230 }
231 return -1;
232 }
233
234 /*
235 * Create a kernel event notifier group, register it to the kernel tracer and
236 * add it to the kernel session.
237 */
238 static 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;
267 error:
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
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 */
287 static
288 int extract_userspace_probe_offset_function_elf(
289 const struct lttng_userspace_probe_location *probe_location,
290 uid_t uid, gid_t gid, uint64_t *offset)
291 {
292 int fd;
293 int ret = 0;
294 const char *symbol = NULL;
295 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
296 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
297
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
327 ret = run_as_extract_elf_symbol_offset(fd, symbol, uid, gid, offset);
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));
335 end:
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 */
348 static
349 int extract_userspace_probe_offset_tracepoint_sdt(
350 const struct lttng_userspace_probe_location *probe_location,
351 uid_t uid, gid_t gid, uint64_t **offsets,
352 uint32_t *offsets_count)
353 {
354 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
355 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
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,
397 uid, gid, offsets, offsets_count);
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 }
414 end:
415 return ret;
416 }
417
418 static
419 int userspace_probe_add_callsite(
420 const struct lttng_userspace_probe_location *location,
421 uid_t uid, gid_t gid, int fd)
422 {
423 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
424 enum lttng_userspace_probe_location_lookup_method_type type;
425 int ret;
426
427 lookup_method = lttng_userspace_probe_location_get_lookup_method(location);
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
440 ret = extract_userspace_probe_offset_function_elf(location,
441 uid, gid, &offset);
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) {
450 WARN("Failed to add callsite to ELF userspace probe.");
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 */
467 ret = extract_userspace_probe_offset_tracepoint_sdt(location,
468 uid, gid, &offsets, &offsets_count);
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) {
477 WARN("Failed to add callsite to SDT userspace probe");
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 }
490 end:
491 return ret;
492 }
493
494 /*
495 * Extract the offsets of the instrumentation point for the different lookup
496 * methods.
497 */
498 static
499 int 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
521 end:
522 return ret;
523 }
524
525 /*
526 * Extract the offsets of the instrumentation point for the different look-up
527 * methods.
528 */
529 static 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
559 end:
560 return ret;
561 }
562
563 /*
564 * Create a kernel event, enable it to the kernel tracer and add it to the
565 * channel event list of the kernel session.
566 * We own filter_expression and filter.
567 */
568 int kernel_create_event(struct lttng_event *ev,
569 struct ltt_kernel_channel *channel,
570 char *filter_expression,
571 struct lttng_filter_bytecode *filter)
572 {
573 int err, fd;
574 enum lttng_error_code ret;
575 struct ltt_kernel_event *event;
576
577 assert(ev);
578 assert(channel);
579
580 /* We pass ownership of filter_expression and filter */
581 ret = trace_kernel_create_event(ev, filter_expression,
582 filter, &event);
583 if (ret != LTTNG_OK) {
584 goto error;
585 }
586
587 fd = kernctl_create_event(channel->fd, event->event);
588 if (fd < 0) {
589 switch (-fd) {
590 case EEXIST:
591 ret = LTTNG_ERR_KERN_EVENT_EXIST;
592 break;
593 case ENOSYS:
594 WARN("Event type not implemented");
595 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
596 break;
597 case ENOENT:
598 WARN("Event %s not found!", ev->name);
599 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
600 break;
601 default:
602 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
603 PERROR("create event ioctl");
604 }
605 goto free_event;
606 }
607
608 event->type = ev->type;
609 event->fd = fd;
610 /* Prevent fd duplication after execlp() */
611 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
612 if (err < 0) {
613 PERROR("fcntl session fd");
614 }
615
616 if (filter) {
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 }
627 goto filter_error;
628 }
629 }
630
631 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
632 ret = userspace_probe_event_add_callsites(ev, channel->session,
633 event->fd);
634 if (ret) {
635 goto add_callsite_error;
636 }
637 }
638
639 err = kernctl_enable(event->fd);
640 if (err < 0) {
641 switch (-err) {
642 case EEXIST:
643 ret = LTTNG_ERR_KERN_EVENT_EXIST;
644 break;
645 default:
646 PERROR("enable kernel event");
647 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
648 break;
649 }
650 goto enable_error;
651 }
652
653 /* Add event to event list */
654 cds_list_add(&event->list, &channel->events_list.head);
655 channel->event_count++;
656
657 DBG("Event %s created (fd: %d)", ev->name, event->fd);
658
659 return 0;
660
661 add_callsite_error:
662 enable_error:
663 filter_error:
664 {
665 int closeret;
666
667 closeret = close(event->fd);
668 if (closeret) {
669 PERROR("close event fd");
670 }
671 }
672 free_event:
673 free(event);
674 error:
675 return ret;
676 }
677
678 /*
679 * Disable a kernel channel.
680 */
681 int kernel_disable_channel(struct ltt_kernel_channel *chan)
682 {
683 int ret;
684
685 assert(chan);
686
687 ret = kernctl_disable(chan->fd);
688 if (ret < 0) {
689 PERROR("disable chan ioctl");
690 goto error;
691 }
692
693 chan->enabled = 0;
694 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
695 chan->channel->name, chan->fd, chan->key);
696
697 return 0;
698
699 error:
700 return ret;
701 }
702
703 /*
704 * Enable a kernel channel.
705 */
706 int kernel_enable_channel(struct ltt_kernel_channel *chan)
707 {
708 int ret;
709
710 assert(chan);
711
712 ret = kernctl_enable(chan->fd);
713 if (ret < 0 && ret != -EEXIST) {
714 PERROR("Enable kernel chan");
715 goto error;
716 }
717
718 chan->enabled = 1;
719 DBG("Kernel channel %s enabled (fd: %d, key: %" PRIu64 ")",
720 chan->channel->name, chan->fd, chan->key);
721
722 return 0;
723
724 error:
725 return ret;
726 }
727
728 /*
729 * Enable a kernel event.
730 */
731 int kernel_enable_event(struct ltt_kernel_event *event)
732 {
733 int ret;
734
735 assert(event);
736
737 ret = kernctl_enable(event->fd);
738 if (ret < 0) {
739 switch (-ret) {
740 case EEXIST:
741 ret = LTTNG_ERR_KERN_EVENT_EXIST;
742 break;
743 default:
744 PERROR("enable kernel event");
745 break;
746 }
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
755 error:
756 return ret;
757 }
758
759 /*
760 * Disable a kernel event.
761 */
762 int kernel_disable_event(struct ltt_kernel_event *event)
763 {
764 int ret;
765
766 assert(event);
767
768 ret = kernctl_disable(event->fd);
769 if (ret < 0) {
770 switch (-ret) {
771 case EEXIST:
772 ret = LTTNG_ERR_KERN_EVENT_EXIST;
773 break;
774 default:
775 PERROR("disable kernel event");
776 break;
777 }
778 goto error;
779 }
780
781 event->enabled = 0;
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
794 int 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
822 error:
823 return ret;
824 }
825
826 static
827 struct process_attr_tracker *_kernel_get_process_attr_tracker(
828 struct ltt_kernel_session *session,
829 enum lttng_process_attr process_attr)
830 {
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;
844 default:
845 return NULL;
846 }
847 }
848
849 const struct process_attr_tracker *kernel_get_process_attr_tracker(
850 struct ltt_kernel_session *session,
851 enum lttng_process_attr process_attr)
852 {
853 return (const struct process_attr_tracker *)
854 _kernel_get_process_attr_tracker(session, process_attr);
855 }
856
857 enum 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;
867
868 if (!tracker) {
869 ret_code = LTTNG_ERR_INVALID;
870 goto end;
871 }
872
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;
878 }
879
880 if (previous_policy == policy) {
881 goto end;
882 }
883
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);
895 }
896 break;
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);
909 }
910 break;
911 default:
912 abort();
913 }
914 /* kern-ctl error handling */
915 switch (-ret) {
916 case 0:
917 ret_code = LTTNG_OK;
918 break;
919 case EINVAL:
920 ret_code = LTTNG_ERR_INVALID;
921 break;
922 case ENOMEM:
923 ret_code = LTTNG_ERR_NOMEM;
924 break;
925 case EEXIST:
926 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
927 break;
928 default:
929 ret_code = LTTNG_ERR_UNK;
930 break;
931 }
932 end:
933 return ret_code;
934 }
935
936 enum lttng_error_code kernel_process_attr_tracker_inclusion_set_add_value(
937 struct ltt_kernel_session *session,
938 enum lttng_process_attr process_attr,
939 const struct process_attr_value *value)
940 {
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;
945
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;
954 break;
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;
968 }
969 break;
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;
983 }
984 break;
985 default:
986 ret_code = LTTNG_ERR_INVALID;
987 goto end;
988 }
989
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 */
1034 switch (-ret) {
1035 case 0:
1036 ret_code = LTTNG_OK;
1037 break;
1038 case EINVAL:
1039 ret_code = LTTNG_ERR_INVALID;
1040 break;
1041 case ENOMEM:
1042 ret_code = LTTNG_ERR_NOMEM;
1043 break;
1044 case EEXIST:
1045 ret_code = LTTNG_ERR_PROCESS_ATTR_EXISTS;
1046 break;
1047 default:
1048 ret_code = LTTNG_ERR_UNK;
1049 break;
1050 }
1051
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);
1059 }
1060 end:
1061 return ret_code;
1062 }
1063
1064 enum lttng_error_code kernel_process_attr_tracker_inclusion_set_remove_value(
1065 struct ltt_kernel_session *session,
1066 enum lttng_process_attr process_attr,
1067 const struct process_attr_value *value)
1068 {
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 }
1117
1118 tracker = _kernel_get_process_attr_tracker(session, process_attr);
1119 if (!tracker) {
1120 ret_code = LTTNG_ERR_INVALID;
1121 goto end;
1122 }
1123
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 }
1139 goto end;
1140 }
1141
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 }
1188 end:
1189 return ret_code;
1190 }
1191
1192 /*
1193 * Create kernel metadata, open from the kernel tracer and add it to the
1194 * kernel session.
1195 */
1196 int kernel_open_metadata(struct ltt_kernel_session *session)
1197 {
1198 int ret;
1199 struct ltt_kernel_metadata *lkm = NULL;
1200
1201 assert(session);
1202
1203 /* Allocate kernel metadata */
1204 lkm = trace_kernel_create_metadata();
1205 if (lkm == NULL) {
1206 goto error;
1207 }
1208
1209 /* Kernel tracer metadata creation */
1210 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
1211 if (ret < 0) {
1212 goto error_open;
1213 }
1214
1215 lkm->fd = ret;
1216 lkm->key = ++next_kernel_channel_key;
1217 /* Prevent fd duplication after execlp() */
1218 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
1219 if (ret < 0) {
1220 PERROR("fcntl session fd");
1221 }
1222
1223 session->metadata = lkm;
1224
1225 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
1226
1227 return 0;
1228
1229 error_open:
1230 trace_kernel_destroy_metadata(lkm);
1231 error:
1232 return -1;
1233 }
1234
1235 /*
1236 * Start tracing session.
1237 */
1238 int kernel_start_session(struct ltt_kernel_session *session)
1239 {
1240 int ret;
1241
1242 assert(session);
1243
1244 ret = kernctl_start_session(session->fd);
1245 if (ret < 0) {
1246 PERROR("ioctl start session");
1247 goto error;
1248 }
1249
1250 DBG("Kernel session started");
1251
1252 return 0;
1253
1254 error:
1255 return ret;
1256 }
1257
1258 /*
1259 * Make a kernel wait to make sure in-flight probe have completed.
1260 */
1261 void kernel_wait_quiescent(void)
1262 {
1263 int ret;
1264 int fd = kernel_tracer_fd;
1265
1266 DBG("Kernel quiescent wait on %d", fd);
1267
1268 ret = kernctl_wait_quiescent(fd);
1269 if (ret < 0) {
1270 PERROR("wait quiescent ioctl");
1271 ERR("Kernel quiescent wait failed");
1272 }
1273 }
1274
1275 /*
1276 * Force flush buffer of metadata.
1277 */
1278 int kernel_metadata_flush_buffer(int fd)
1279 {
1280 int ret;
1281
1282 DBG("Kernel flushing metadata buffer on fd %d", fd);
1283
1284 ret = kernctl_buffer_flush(fd);
1285 if (ret < 0) {
1286 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
1287 }
1288
1289 return 0;
1290 }
1291
1292 /*
1293 * Force flush buffer for channel.
1294 */
1295 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
1296 {
1297 int ret;
1298 struct ltt_kernel_stream *stream;
1299
1300 assert(channel);
1301
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) {
1308 PERROR("ioctl");
1309 ERR("Fail to flush buffer for stream %d (ret: %d)",
1310 stream->fd, ret);
1311 }
1312 }
1313
1314 return 0;
1315 }
1316
1317 /*
1318 * Stop tracing session.
1319 */
1320 int kernel_stop_session(struct ltt_kernel_session *session)
1321 {
1322 int ret;
1323
1324 assert(session);
1325
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
1335 error:
1336 return ret;
1337 }
1338
1339 /*
1340 * Open stream of channel, register it to the kernel tracer and add it
1341 * to the stream list of the channel.
1342 *
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 *
1347 * Return the number of created stream. Else, a negative value.
1348 */
1349 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
1350 {
1351 int ret;
1352 struct ltt_kernel_stream *lks;
1353
1354 assert(channel);
1355
1356 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
1357 lks = trace_kernel_create_stream(channel->channel->name,
1358 channel->stream_count);
1359 if (lks == NULL) {
1360 ret = close(ret);
1361 if (ret) {
1362 PERROR("close");
1363 }
1364 goto error;
1365 }
1366
1367 lks->fd = ret;
1368 /* Prevent fd duplication after execlp() */
1369 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
1370 if (ret < 0) {
1371 PERROR("fcntl session fd");
1372 }
1373
1374 lks->tracefile_size = channel->channel->attr.tracefile_size;
1375 lks->tracefile_count = channel->channel->attr.tracefile_count;
1376
1377 /* Add stream to channel stream list */
1378 cds_list_add(&lks->list, &channel->stream_list.head);
1379 channel->stream_count++;
1380
1381 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
1382 lks->state);
1383 }
1384
1385 return channel->stream_count;
1386
1387 error:
1388 return -1;
1389 }
1390
1391 /*
1392 * Open the metadata stream and set it to the kernel session.
1393 */
1394 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
1395 {
1396 int ret;
1397
1398 assert(session);
1399
1400 ret = kernctl_create_stream(session->metadata->fd);
1401 if (ret < 0) {
1402 PERROR("kernel create metadata stream");
1403 goto error;
1404 }
1405
1406 DBG("Kernel metadata stream created (fd: %d)", ret);
1407 session->metadata_stream_fd = ret;
1408 /* Prevent fd duplication after execlp() */
1409 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
1410 if (ret < 0) {
1411 PERROR("fcntl session fd");
1412 }
1413
1414 return 0;
1415
1416 error:
1417 return -1;
1418 }
1419
1420 /*
1421 * Get the event list from the kernel tracer and return the number of elements.
1422 */
1423 ssize_t kernel_list_events(struct lttng_event **events)
1424 {
1425 int fd, ret;
1426 char *event;
1427 size_t nbmem, count = 0;
1428 FILE *fp;
1429 struct lttng_event *elist;
1430
1431 assert(events);
1432
1433 fd = kernctl_tracepoint_list(kernel_tracer_fd);
1434 if (fd < 0) {
1435 PERROR("kernel tracepoint list");
1436 goto error;
1437 }
1438
1439 fp = fdopen(fd, "r");
1440 if (fp == NULL) {
1441 PERROR("kernel tracepoint list fdopen");
1442 goto error_fp;
1443 }
1444
1445 /*
1446 * Init memory size counter
1447 * See kernel-ctl.h for explanation of this value
1448 */
1449 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
1450 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
1451 if (elist == NULL) {
1452 PERROR("alloc list events");
1453 count = -ENOMEM;
1454 goto end;
1455 }
1456
1457 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
1458 if (count >= nbmem) {
1459 struct lttng_event *new_elist;
1460 size_t new_nbmem;
1461
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));
1466 if (new_elist == NULL) {
1467 PERROR("realloc list events");
1468 free(event);
1469 free(elist);
1470 count = -ENOMEM;
1471 goto end;
1472 }
1473 /* Zero the new memory */
1474 memset(new_elist + nbmem, 0,
1475 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1476 nbmem = new_nbmem;
1477 elist = new_elist;
1478 }
1479 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1480 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1481 elist[count].enabled = -1;
1482 count++;
1483 free(event);
1484 }
1485
1486 *events = elist;
1487 DBG("Kernel list events done (%zu events)", count);
1488 end:
1489 ret = fclose(fp); /* closes both fp and fd */
1490 if (ret) {
1491 PERROR("fclose");
1492 }
1493 return count;
1494
1495 error_fp:
1496 ret = close(fd);
1497 if (ret) {
1498 PERROR("close");
1499 }
1500 error:
1501 return -1;
1502 }
1503
1504 /*
1505 * Get kernel version and validate it.
1506 */
1507 int kernel_validate_version(struct lttng_kernel_tracer_version *version,
1508 struct lttng_kernel_tracer_abi_version *abi_version)
1509 {
1510 int ret;
1511
1512 ret = kernctl_tracer_version(kernel_tracer_fd, version);
1513 if (ret < 0) {
1514 ERR("Failed to retrieve the lttng-modules version");
1515 goto error;
1516 }
1517
1518 /* Validate version */
1519 if (version->major != VERSION_MAJOR) {
1520 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1521 version->major, VERSION_MAJOR);
1522 goto error_version;
1523 }
1524 ret = kernctl_tracer_abi_version(kernel_tracer_fd, abi_version);
1525 if (ret < 0) {
1526 ERR("Failed to retrieve lttng-modules ABI version");
1527 goto error;
1528 }
1529 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
1530 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1531 abi_version->major, abi_version->minor,
1532 LTTNG_MODULES_ABI_MAJOR_VERSION);
1533 goto error;
1534 }
1535 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1536 version->major, version->minor,
1537 abi_version->major, abi_version->minor);
1538 return 0;
1539
1540 error_version:
1541 ret = -1;
1542
1543 error:
1544 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1545 return ret;
1546 }
1547
1548 /*
1549 * Kernel work-arounds called at the start of sessiond main().
1550 */
1551 int init_kernel_workarounds(void)
1552 {
1553 int ret;
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
1568 ret = fread(buf, 1, sizeof(buf), fp);
1569 if (ret < 0) {
1570 /* Ignore error, we don't really care */
1571 }
1572 }
1573 ret = fclose(fp);
1574 if (ret) {
1575 PERROR("fclose");
1576 }
1577 end_boot_id:
1578 return 0;
1579 }
1580
1581 /*
1582 * Teardown of a kernel session, keeping data required by destroy notifiers.
1583 */
1584 void kernel_destroy_session(struct ltt_kernel_session *ksess)
1585 {
1586 struct lttng_trace_chunk *trace_chunk;
1587
1588 if (ksess == NULL) {
1589 DBG3("No kernel session when tearing down session");
1590 return;
1591 }
1592
1593 DBG("Tearing down kernel session");
1594 trace_chunk = ksess->current_trace_chunk;
1595
1596 /*
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.
1600 */
1601 if (!ksess->output_traces && ksess->consumer_fds_sent) {
1602 int ret;
1603 struct consumer_socket *socket;
1604 struct lttng_ht_iter iter;
1605
1606 /* For each consumer socket. */
1607 rcu_read_lock();
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 }
1621 rcu_read_unlock();
1622 }
1623
1624 /* Close any relayd session */
1625 consumer_output_send_destroy_relayd(ksess->consumer);
1626
1627 trace_kernel_destroy_session(ksess);
1628 lttng_trace_chunk_put(trace_chunk);
1629 }
1630
1631 /* Teardown of data required by destroy notifiers. */
1632 void kernel_free_session(struct ltt_kernel_session *ksess)
1633 {
1634 if (ksess == NULL) {
1635 return;
1636 }
1637 trace_kernel_free_session(ksess);
1638 }
1639
1640 /*
1641 * Destroy a kernel channel object. It does not do anything on the tracer side.
1642 */
1643 void 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 }
1669
1670 /*
1671 * Take a snapshot for a given kernel session.
1672 *
1673 * Return LTTNG_OK on success or else return a LTTNG_ERR code.
1674 */
1675 enum lttng_error_code kernel_snapshot_record(
1676 struct ltt_kernel_session *ksess,
1677 const struct consumer_output *output, int wait,
1678 uint64_t nb_packets_per_stream)
1679 {
1680 int err, ret, saved_metadata_fd;
1681 enum lttng_error_code status = LTTNG_OK;
1682 struct consumer_socket *socket;
1683 struct lttng_ht_iter iter;
1684 struct ltt_kernel_metadata *saved_metadata;
1685 char *trace_path = NULL;
1686 size_t consumer_path_offset = 0;
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) {
1702 status = LTTNG_ERR_KERN_META_FAIL;
1703 goto error;
1704 }
1705
1706 ret = kernel_open_metadata_stream(ksess);
1707 if (ret < 0) {
1708 status = LTTNG_ERR_KERN_META_FAIL;
1709 goto error_open_stream;
1710 }
1711
1712 trace_path = setup_channel_trace_path(ksess->consumer,
1713 DEFAULT_KERNEL_TRACE_DIR, &consumer_path_offset);
1714 if (!trace_path) {
1715 status = LTTNG_ERR_INVALID;
1716 goto error;
1717 }
1718 /* Send metadata to consumer and snapshot everything. */
1719 cds_lfht_for_each_entry(output->socks->ht, &iter.iter,
1720 socket, node.node) {
1721 struct ltt_kernel_channel *chan;
1722
1723 pthread_mutex_lock(socket->lock);
1724 /* This stream must not be monitored by the consumer. */
1725 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1726 pthread_mutex_unlock(socket->lock);
1727 if (ret < 0) {
1728 status = LTTNG_ERR_KERN_META_FAIL;
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) {
1734 status = consumer_snapshot_channel(socket, chan->key, output, 0,
1735 ksess->uid, ksess->gid,
1736 &trace_path[consumer_path_offset], wait,
1737 nb_packets_per_stream);
1738 if (status != LTTNG_OK) {
1739 (void) kernel_consumer_destroy_metadata(socket,
1740 ksess->metadata);
1741 goto error_consumer;
1742 }
1743 }
1744
1745 /* Snapshot metadata, */
1746 status = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1747 1, ksess->uid, ksess->gid, &trace_path[consumer_path_offset],
1748 wait, 0);
1749 if (status != LTTNG_OK) {
1750 goto error_consumer;
1751 }
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);
1758 }
1759
1760 error_consumer:
1761 /* Close newly opened metadata stream. It's now on the consumer side. */
1762 err = close(ksess->metadata_stream_fd);
1763 if (err < 0) {
1764 PERROR("close snapshot kernel");
1765 }
1766
1767 error_open_stream:
1768 trace_kernel_destroy_metadata(ksess->metadata);
1769 error:
1770 /* Restore metadata state.*/
1771 ksess->metadata = saved_metadata;
1772 ksess->metadata_stream_fd = saved_metadata_fd;
1773 rcu_read_unlock();
1774 free(trace_path);
1775 return status;
1776 }
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 */
1784 int 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 }
1791
1792 static
1793 int kernel_tracer_abi_greater_or_equal(unsigned int major, unsigned int minor)
1794 {
1795 int ret;
1796 struct lttng_kernel_tracer_abi_version abi;
1797
1798 ret = kernctl_tracer_abi_version(kernel_tracer_fd, &abi);
1799 if (ret < 0) {
1800 ERR("Failed to retrieve lttng-modules ABI version");
1801 goto error;
1802 }
1803
1804 ret = abi.major > major || (abi.major == major && abi.minor >= minor);
1805 error:
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 */
1816 int kernel_supports_ring_buffer_snapshot_sample_positions(void)
1817 {
1818 /*
1819 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1820 */
1821 return kernel_tracer_abi_greater_or_equal(2, 3);
1822 }
1823
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 */
1830 int kernel_supports_ring_buffer_packet_sequence_number(void)
1831 {
1832 /*
1833 * Packet sequence number was introduced in LTTng 2.8,
1834 * lttng-modules ABI 2.1.
1835 */
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 */
1845 int 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);
1851 }
1852
1853 /*
1854 * Rotate a kernel session.
1855 *
1856 * Return LTTNG_OK on success or else an LTTng error code.
1857 */
1858 enum lttng_error_code kernel_rotate_session(struct ltt_session *session)
1859 {
1860 int ret;
1861 enum lttng_error_code status = LTTNG_OK;
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
1882 /* For each channel, ask the consumer to rotate it. */
1883 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1884 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1885 chan->key, session->name);
1886 ret = consumer_rotate_channel(socket, chan->key,
1887 ksess->uid, ksess->gid, ksess->consumer,
1888 /* is_metadata_channel */ false);
1889 if (ret < 0) {
1890 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
1891 goto error;
1892 }
1893 }
1894
1895 /*
1896 * Rotate the metadata channel.
1897 */
1898 ret = consumer_rotate_channel(socket, ksess->metadata->key,
1899 ksess->uid, ksess->gid, ksess->consumer,
1900 /* is_metadata_channel */ true);
1901 if (ret < 0) {
1902 status = LTTNG_ERR_ROTATION_FAIL_CONSUMER;
1903 goto error;
1904 }
1905 }
1906
1907 error:
1908 rcu_read_unlock();
1909 return status;
1910 }
1911
1912 enum 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 }
1932 error:
1933 rcu_read_unlock();
1934 return ret;
1935 }
1936
1937 /*
1938 * Setup necessary data for kernel tracer action.
1939 */
1940 LTTNG_HIDDEN
1941 int 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 }
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
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);
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 }
2018
2019 return 0;
2020
2021 error_version:
2022 modprobe_remove_lttng_control();
2023 ret = close(kernel_tracer_fd);
2024 if (ret) {
2025 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2026 kernel_tracer_fd);
2027 }
2028 kernel_tracer_fd = -1;
2029 return LTTNG_ERR_KERN_VERSION;
2030
2031
2032 error_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
2039 error_modules:
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
2046 ret = close(kernel_tracer_fd);
2047 if (ret) {
2048 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2049 kernel_tracer_fd);
2050 }
2051
2052 error_open:
2053 modprobe_remove_lttng_control();
2054
2055 error:
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
2065 LTTNG_HIDDEN
2066 void cleanup_kernel_tracer(void)
2067 {
2068 DBG2("Closing kernel event notifier group notification file descriptor");
2069 if (kernel_tracer_event_notifier_group_notification_fd >= 0) {
2070 int ret = notification_thread_command_remove_tracer_event_source(
2071 notification_thread_handle,
2072 kernel_tracer_event_notifier_group_notification_fd);
2073 if (ret != LTTNG_OK) {
2074 ERR("Failed to remove kernel event notifier notification from notification thread");
2075 }
2076
2077 ret = close(kernel_tracer_event_notifier_group_notification_fd);
2078 if (ret) {
2079 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2080 kernel_tracer_event_notifier_group_notification_fd);
2081 }
2082
2083 kernel_tracer_event_notifier_group_notification_fd = -1;
2084 }
2085
2086 if (kernel_token_to_event_notifier_rule_ht) {
2087 const int ret = cds_lfht_destroy(
2088 kernel_token_to_event_notifier_rule_ht, NULL);
2089 assert(ret == 0);
2090 }
2091
2092 DBG2("Closing kernel event notifier group file descriptor");
2093 if (kernel_tracer_event_notifier_group_fd >= 0) {
2094 const int ret = close(kernel_tracer_event_notifier_group_fd);
2095
2096 if (ret) {
2097 PERROR("Failed to close kernel event notifier group file descriptor: fd = %d",
2098 kernel_tracer_event_notifier_group_fd);
2099 }
2100
2101 kernel_tracer_event_notifier_group_fd = -1;
2102 }
2103
2104 DBG2("Closing kernel fd");
2105 if (kernel_tracer_fd >= 0) {
2106 const int ret = close(kernel_tracer_fd);
2107
2108 if (ret) {
2109 PERROR("Failed to close kernel tracer file descriptor: fd = %d",
2110 kernel_tracer_fd);
2111 }
2112
2113 kernel_tracer_fd = -1;
2114 }
2115
2116 DBG("Unloading kernel modules");
2117 modprobe_remove_lttng_all();
2118 free(syscall_table);
2119 }
2120
2121 LTTNG_HIDDEN
2122 bool kernel_tracer_is_initialized(void)
2123 {
2124 return kernel_tracer_fd >= 0;
2125 }
2126
2127 /*
2128 * Clear a kernel session.
2129 *
2130 * Return LTTNG_OK on success or else an LTTng error code.
2131 */
2132 enum lttng_error_code kernel_clear_session(struct ltt_session *session)
2133 {
2134 int ret;
2135 enum lttng_error_code status = LTTNG_OK;
2136 struct consumer_socket *socket;
2137 struct lttng_ht_iter iter;
2138 struct ltt_kernel_session *ksess = session->kernel_session;
2139
2140 assert(ksess);
2141 assert(ksess->consumer);
2142
2143 DBG("Clear kernel session %s (session %" PRIu64 ")",
2144 session->name, session->id);
2145
2146 rcu_read_lock();
2147
2148 if (ksess->active) {
2149 ERR("Expecting inactive session %s (%" PRIu64 ")", session->name, session->id);
2150 status = LTTNG_ERR_FATAL;
2151 goto end;
2152 }
2153
2154 /*
2155 * Note that this loop will end after one iteration given that there is
2156 * only one kernel consumer.
2157 */
2158 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
2159 socket, node.node) {
2160 struct ltt_kernel_channel *chan;
2161
2162 /* For each channel, ask the consumer to clear it. */
2163 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
2164 DBG("Clear kernel channel %" PRIu64 ", session %s",
2165 chan->key, session->name);
2166 ret = consumer_clear_channel(socket, chan->key);
2167 if (ret < 0) {
2168 goto error;
2169 }
2170 }
2171
2172 if (!ksess->metadata) {
2173 /*
2174 * Nothing to do for the metadata.
2175 * This is a snapshot session.
2176 * The metadata is genererated on the fly.
2177 */
2178 continue;
2179 }
2180
2181 /*
2182 * Clear the metadata channel.
2183 * Metadata channel is not cleared per se but we still need to
2184 * perform a rotation operation on it behind the scene.
2185 */
2186 ret = consumer_clear_channel(socket, ksess->metadata->key);
2187 if (ret < 0) {
2188 goto error;
2189 }
2190 }
2191
2192 goto end;
2193 error:
2194 switch (-ret) {
2195 case LTTCOMM_CONSUMERD_RELAYD_CLEAR_DISALLOWED:
2196 status = LTTNG_ERR_CLEAR_RELAY_DISALLOWED;
2197 break;
2198 default:
2199 status = LTTNG_ERR_CLEAR_FAIL_CONSUMER;
2200 break;
2201 }
2202 end:
2203 rcu_read_unlock();
2204 return status;
2205 }
2206
2207 enum lttng_error_code kernel_create_event_notifier_group_notification_fd(
2208 int *event_notifier_group_notification_fd)
2209 {
2210 int local_fd = -1, ret;
2211 enum lttng_error_code error_code_ret;
2212
2213 assert(event_notifier_group_notification_fd);
2214
2215 ret = kernctl_create_event_notifier_group_notification_fd(
2216 kernel_tracer_event_notifier_group_fd);
2217 if (ret < 0) {
2218 PERROR("Failed to create kernel event notifier group notification file descriptor");
2219 error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
2220 goto error;
2221 }
2222
2223 local_fd = ret;
2224
2225 /* Prevent fd duplication after execlp(). */
2226 ret = fcntl(local_fd, F_SETFD, FD_CLOEXEC);
2227 if (ret < 0) {
2228 PERROR("Failed to set FD_CLOEXEC on kernel event notifier group notification file descriptor: fd = %d",
2229 local_fd);
2230 error_code_ret = LTTNG_ERR_EVENT_NOTIFIER_GROUP_NOTIFICATION_FD;
2231 goto error;
2232 }
2233
2234 DBG("Created kernel notifier group notification file descriptor: fd = %d",
2235 local_fd);
2236 error_code_ret = LTTNG_OK;
2237 *event_notifier_group_notification_fd = local_fd;
2238 local_fd = -1;
2239
2240 error:
2241 if (local_fd >= 0) {
2242 ret = close(local_fd);
2243 if (ret) {
2244 PERROR("Failed to close kernel event notifier group notification file descriptor: fd = %d",
2245 local_fd);
2246 }
2247 }
2248
2249 return error_code_ret;
2250 }
2251
2252 enum lttng_error_code kernel_destroy_event_notifier_group_notification_fd(
2253 int event_notifier_group_notification_fd)
2254 {
2255 enum lttng_error_code ret_code = LTTNG_OK;
2256
2257 DBG("Closing event notifier group notification file descriptor: fd = %d",
2258 event_notifier_group_notification_fd);
2259 if (event_notifier_group_notification_fd >= 0) {
2260 const int ret = close(event_notifier_group_notification_fd);
2261 if (ret) {
2262 PERROR("Failed to close event notifier group notification file descriptor: fd = %d",
2263 event_notifier_group_notification_fd);
2264 }
2265 }
2266
2267 return ret_code;
2268 }
2269
2270 static
2271 unsigned long hash_trigger(const struct lttng_trigger *trigger)
2272 {
2273 const struct lttng_condition *condition =
2274 lttng_trigger_get_const_condition(trigger);
2275
2276 return lttng_condition_hash(condition);
2277 }
2278
2279 static
2280 int match_trigger(struct cds_lfht_node *node, const void *key)
2281 {
2282 const struct ltt_kernel_event_notifier_rule *event_notifier_rule;
2283 const struct lttng_trigger *trigger = key;
2284
2285 event_notifier_rule = caa_container_of(node,
2286 const struct ltt_kernel_event_notifier_rule, ht_node);
2287
2288 return lttng_trigger_is_equal(trigger, event_notifier_rule->trigger);
2289 }
2290
2291 static enum lttng_error_code kernel_create_event_notifier_rule(
2292 struct lttng_trigger *trigger,
2293 const struct lttng_credentials *creds, uint64_t token)
2294 {
2295 int err, fd, ret = 0;
2296 enum lttng_error_code error_code_ret;
2297 enum lttng_condition_status condition_status;
2298 enum lttng_condition_type condition_type;
2299 enum lttng_event_rule_type event_rule_type;
2300 struct ltt_kernel_event_notifier_rule *event_notifier_rule;
2301 struct lttng_kernel_event_notifier kernel_event_notifier = {};
2302 const struct lttng_condition *condition = NULL;
2303 const struct lttng_event_rule *event_rule = NULL;
2304
2305 assert(trigger);
2306
2307 condition = lttng_trigger_get_const_condition(trigger);
2308 assert(condition);
2309
2310 condition_type = lttng_condition_get_type(condition);
2311 assert(condition_type == LTTNG_CONDITION_TYPE_EVENT_RULE_HIT);
2312
2313 /* Does not acquire a reference. */
2314 condition_status = lttng_condition_event_rule_get_rule(
2315 condition, &event_rule);
2316 assert(condition_status == LTTNG_CONDITION_STATUS_OK);
2317 assert(event_rule);
2318
2319 event_rule_type = lttng_event_rule_get_type(event_rule);
2320 assert(event_rule_type != LTTNG_EVENT_RULE_TYPE_UNKNOWN);
2321
2322 error_code_ret = trace_kernel_create_event_notifier_rule(trigger, token,
2323 &event_notifier_rule);
2324 if (error_code_ret != LTTNG_OK) {
2325 goto error;
2326 }
2327
2328 error_code_ret = trace_kernel_init_event_notifier_from_event_rule(
2329 event_rule, &kernel_event_notifier);
2330 if (error_code_ret != LTTNG_OK) {
2331 goto error;
2332 }
2333
2334 kernel_event_notifier.event.token = event_notifier_rule->token;
2335
2336 fd = kernctl_create_event_notifier(
2337 kernel_tracer_event_notifier_group_fd,
2338 &kernel_event_notifier);
2339 if (fd < 0) {
2340 switch (-fd) {
2341 case EEXIST:
2342 error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
2343 break;
2344 case ENOSYS:
2345 WARN("Failed to create kernel event notifier: not notifier type not implemented");
2346 error_code_ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
2347 break;
2348 case ENOENT:
2349 WARN("Failed to create kernel event notifier: not found: name = '%s'",
2350 kernel_event_notifier.event.name);
2351 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2352 break;
2353 default:
2354 PERROR("Failed to create kernel event notifier: error code = %d, name = '%s'",
2355 fd, kernel_event_notifier.event.name);
2356 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2357 }
2358 goto free_event;
2359 }
2360
2361 event_notifier_rule->fd = fd;
2362 /* Prevent fd duplication after execlp(). */
2363 err = fcntl(event_notifier_rule->fd, F_SETFD, FD_CLOEXEC);
2364 if (err < 0) {
2365 PERROR("Failed to set FD_CLOEXEC on kernel event notifier file descriptor: fd = %d",
2366 fd);
2367 error_code_ret = LTTNG_ERR_FATAL;
2368 goto set_cloexec_error;
2369 }
2370
2371 if (event_notifier_rule->filter) {
2372 err = kernctl_filter(event_notifier_rule->fd, event_notifier_rule->filter);
2373 if (err < 0) {
2374 switch (-err) {
2375 case ENOMEM:
2376 error_code_ret = LTTNG_ERR_FILTER_NOMEM;
2377 break;
2378 default:
2379 error_code_ret = LTTNG_ERR_FILTER_INVAL;
2380 break;
2381 }
2382 goto filter_error;
2383 }
2384 }
2385
2386 if (lttng_event_rule_get_type(event_rule) ==
2387 LTTNG_EVENT_RULE_TYPE_UPROBE) {
2388 ret = userspace_probe_event_rule_add_callsites(
2389 event_rule, creds, event_notifier_rule->fd);
2390 if (ret) {
2391 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2392 goto add_callsite_error;
2393 }
2394 }
2395
2396 err = kernctl_enable(event_notifier_rule->fd);
2397 if (err < 0) {
2398 switch (-err) {
2399 case EEXIST:
2400 error_code_ret = LTTNG_ERR_KERN_EVENT_EXIST;
2401 break;
2402 default:
2403 PERROR("enable kernel event notifier");
2404 error_code_ret = LTTNG_ERR_KERN_ENABLE_FAIL;
2405 break;
2406 }
2407 goto enable_error;
2408 }
2409
2410 /* Add trigger to kernel token mapping in the hash table. */
2411 rcu_read_lock();
2412 cds_lfht_add(kernel_token_to_event_notifier_rule_ht, hash_trigger(trigger),
2413 &event_notifier_rule->ht_node);
2414 rcu_read_unlock();
2415
2416 DBG("Created kernel event notifier: name = '%s', fd = %d",
2417 kernel_event_notifier.event.name,
2418 event_notifier_rule->fd);
2419
2420 return LTTNG_OK;
2421
2422 add_callsite_error:
2423 enable_error:
2424 set_cloexec_error:
2425 filter_error:
2426 {
2427 const int close_ret = close(event_notifier_rule->fd);
2428
2429 if (close_ret) {
2430 PERROR("Failed to close kernel event notifier file descriptor: fd = %d",
2431 event_notifier_rule->fd);
2432 }
2433 }
2434 free_event:
2435 free(event_notifier_rule);
2436 error:
2437 return error_code_ret;
2438 }
2439
2440 enum lttng_error_code kernel_register_event_notifier(
2441 struct lttng_trigger *trigger,
2442 const struct lttng_credentials *cmd_creds)
2443 {
2444 enum lttng_error_code ret;
2445 enum lttng_condition_status status;
2446 enum lttng_domain_type domain_type;
2447 const struct lttng_event_rule *event_rule;
2448 const struct lttng_condition *const condition =
2449 lttng_trigger_get_const_condition(trigger);
2450 const uint64_t token = lttng_trigger_get_tracer_token(trigger);
2451
2452 assert(condition);
2453
2454 /* Does not acquire a reference to the event rule. */
2455 status = lttng_condition_event_rule_get_rule(
2456 condition, &event_rule);
2457 assert(status == LTTNG_CONDITION_STATUS_OK);
2458
2459 domain_type = lttng_event_rule_get_domain_type(event_rule);
2460 assert(domain_type == LTTNG_DOMAIN_KERNEL);
2461
2462 ret = kernel_create_event_notifier_rule(trigger, cmd_creds, token);
2463 if (ret != LTTNG_OK) {
2464 ERR("Failed to create kernel event notifier rule");
2465 }
2466
2467 return ret;
2468 }
2469
2470 enum lttng_error_code kernel_unregister_event_notifier(
2471 const struct lttng_trigger *trigger)
2472 {
2473 struct ltt_kernel_event_notifier_rule *token_event_rule_element;
2474 struct cds_lfht_node *node;
2475 struct cds_lfht_iter iter;
2476 enum lttng_error_code error_code_ret;
2477 int ret;
2478
2479 rcu_read_lock();
2480
2481 cds_lfht_lookup(kernel_token_to_event_notifier_rule_ht,
2482 hash_trigger(trigger), match_trigger, trigger, &iter);
2483
2484 node = cds_lfht_iter_get_node(&iter);
2485 if (!node) {
2486 error_code_ret = LTTNG_ERR_TRIGGER_NOT_FOUND;
2487 goto error;
2488 }
2489
2490 token_event_rule_element = caa_container_of(node,
2491 struct ltt_kernel_event_notifier_rule, ht_node);
2492
2493 ret = kernel_disable_event_notifier_rule(token_event_rule_element);
2494 if (ret) {
2495 error_code_ret = LTTNG_ERR_FATAL;
2496 goto error;
2497 }
2498
2499 trace_kernel_destroy_event_notifier_rule(token_event_rule_element);
2500 error_code_ret = LTTNG_OK;
2501
2502 error:
2503 rcu_read_unlock();
2504
2505 return error_code_ret;
2506 }
2507
2508 int kernel_get_notification_fd(void)
2509 {
2510 return kernel_tracer_event_notifier_group_notification_fd;
2511 }
This page took 0.145696 seconds and 4 git commands to generate.