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