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