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