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