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