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