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