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