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