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