be263943f0211a9cffdb563f0a22ddfda9e9653c
[lttng-tools.git] / src / bin / lttng-sessiond / kernel.c
1 /*
2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License, version 2 only,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <fcntl.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <inttypes.h>
25
26 #include <common/common.h>
27 #include <common/kernel-ctl/kernel-ctl.h>
28 #include <common/kernel-ctl/kernel-ioctl.h>
29 #include <common/sessiond-comm/sessiond-comm.h>
30
31 #include "consumer.h"
32 #include "kernel.h"
33 #include "kernel-consumer.h"
34 #include "kern-modules.h"
35 #include "utils.h"
36 #include "rotate.h"
37
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 #include <lttng/userspace-probe.h>
45 #include <lttng/userspace-probe-internal.h>
46 /*
47 * Add context on a kernel channel.
48 *
49 * Assumes the ownership of ctx.
50 */
51 int kernel_add_channel_context(struct ltt_kernel_channel *chan,
52 struct ltt_kernel_context *ctx)
53 {
54 int ret;
55
56 assert(chan);
57 assert(ctx);
58
59 DBG("Adding context to channel %s", chan->channel->name);
60 ret = kernctl_add_context(chan->fd, &ctx->ctx);
61 if (ret < 0) {
62 switch (-ret) {
63 case ENOSYS:
64 /* Exists but not available for this kernel */
65 ret = LTTNG_ERR_KERN_CONTEXT_UNAVAILABLE;
66 goto error;
67 case EEXIST:
68 /* If EEXIST, we just ignore the error */
69 ret = 0;
70 goto end;
71 default:
72 PERROR("add context ioctl");
73 ret = LTTNG_ERR_KERN_CONTEXT_FAIL;
74 goto error;
75 }
76 }
77 ret = 0;
78
79 end:
80 cds_list_add_tail(&ctx->list, &chan->ctx_list);
81 ctx->in_list = true;
82 ctx = NULL;
83 error:
84 if (ctx) {
85 trace_kernel_destroy_context(ctx);
86 }
87 return ret;
88 }
89
90 /*
91 * Create a new kernel session, register it to the kernel tracer and add it to
92 * the session daemon session.
93 */
94 int kernel_create_session(struct ltt_session *session, int tracer_fd)
95 {
96 int ret;
97 struct ltt_kernel_session *lks;
98
99 assert(session);
100
101 /* Allocate data structure */
102 lks = trace_kernel_create_session();
103 if (lks == NULL) {
104 ret = -1;
105 goto error;
106 }
107
108 /* Kernel tracer session creation */
109 ret = kernctl_create_session(tracer_fd);
110 if (ret < 0) {
111 PERROR("ioctl kernel create session");
112 goto error;
113 }
114
115 lks->fd = ret;
116 /* Prevent fd duplication after execlp() */
117 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
118 if (ret < 0) {
119 PERROR("fcntl session fd");
120 }
121
122 lks->id = session->id;
123 lks->consumer_fds_sent = 0;
124 session->kernel_session = lks;
125
126 DBG("Kernel session created (fd: %d)", lks->fd);
127
128 return 0;
129
130 error:
131 if (lks) {
132 trace_kernel_destroy_session(lks);
133 }
134 return ret;
135 }
136
137 /*
138 * Create a kernel channel, register it to the kernel tracer and add it to the
139 * kernel session.
140 */
141 int kernel_create_channel(struct ltt_kernel_session *session,
142 struct lttng_channel *chan)
143 {
144 int ret;
145 struct ltt_kernel_channel *lkc;
146
147 assert(session);
148 assert(chan);
149
150 /* Allocate kernel channel */
151 lkc = trace_kernel_create_channel(chan);
152 if (lkc == NULL) {
153 goto error;
154 }
155
156 DBG3("Kernel create channel %s with attr: %d, %" PRIu64 ", %" PRIu64 ", %u, %u, %d, %d",
157 chan->name, lkc->channel->attr.overwrite,
158 lkc->channel->attr.subbuf_size, lkc->channel->attr.num_subbuf,
159 lkc->channel->attr.switch_timer_interval, lkc->channel->attr.read_timer_interval,
160 lkc->channel->attr.live_timer_interval, lkc->channel->attr.output);
161
162 /* Kernel tracer channel creation */
163 ret = kernctl_create_channel(session->fd, &lkc->channel->attr);
164 if (ret < 0) {
165 PERROR("ioctl kernel create channel");
166 goto error;
167 }
168
169 /* Setup the channel fd */
170 lkc->fd = ret;
171 /* Prevent fd duplication after execlp() */
172 ret = fcntl(lkc->fd, F_SETFD, FD_CLOEXEC);
173 if (ret < 0) {
174 PERROR("fcntl session fd");
175 }
176
177 /* Add channel to session */
178 cds_list_add(&lkc->list, &session->channel_list.head);
179 session->channel_count++;
180 lkc->session = session;
181 lkc->key = ++next_kernel_channel_key;
182
183 DBG("Kernel channel %s created (fd: %d, key: %" PRIu64 ")",
184 lkc->channel->name, lkc->fd, lkc->key);
185
186 return 0;
187
188 error:
189 if (lkc) {
190 free(lkc->channel);
191 free(lkc);
192 }
193 return -1;
194 }
195
196 /*
197 * Compute the offset of the instrumentation byte in the binary based on the
198 * function probe location using the ELF lookup method.
199 *
200 * Returns 0 on success and set the offset out parameter to the offset of the
201 * elf symbol
202 * Returns -1 on error
203 */
204 static
205 int extract_userspace_probe_offset_function_elf(
206 const struct lttng_userspace_probe_location *probe_location,
207 struct ltt_kernel_session *session, uint64_t *offset)
208 {
209 int fd;
210 int ret = 0;
211 const char *symbol = NULL;
212 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
213 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
214
215 assert(lttng_userspace_probe_location_get_type(probe_location) ==
216 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_FUNCTION);
217
218 lookup = lttng_userspace_probe_location_get_lookup_method(
219 probe_location);
220 if (!lookup) {
221 ret = -1;
222 goto end;
223 }
224
225 lookup_method_type =
226 lttng_userspace_probe_location_lookup_method_get_type(lookup);
227
228 assert(lookup_method_type ==
229 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF);
230
231 symbol = lttng_userspace_probe_location_function_get_function_name(
232 probe_location);
233 if (!symbol) {
234 ret = -1;
235 goto end;
236 }
237
238 fd = lttng_userspace_probe_location_function_get_binary_fd(probe_location);
239 if (fd < 0) {
240 ret = -1;
241 goto end;
242 }
243
244 ret = run_as_extract_elf_symbol_offset(fd, symbol, session->uid,
245 session->gid, offset);
246 if (ret < 0) {
247 DBG("userspace probe offset calculation failed for "
248 "function %s", symbol);
249 goto end;
250 }
251
252 DBG("userspace probe elf offset for %s is 0x%jd", symbol, (intmax_t)(*offset));
253 end:
254 return ret;
255 }
256
257 /*
258 * Compute the offsets of the instrumentation bytes in the binary based on the
259 * tracepoint probe location using the SDT lookup method. This function
260 * allocates the offsets buffer, the caller must free it.
261 *
262 * Returns 0 on success and set the offset out parameter to the offsets of the
263 * SDT tracepoint.
264 * Returns -1 on error.
265 */
266 static
267 int extract_userspace_probe_offset_tracepoint_sdt(
268 const struct lttng_userspace_probe_location *probe_location,
269 struct ltt_kernel_session *session, uint64_t **offsets,
270 uint32_t *offsets_count)
271 {
272 enum lttng_userspace_probe_location_lookup_method_type lookup_method_type;
273 const struct lttng_userspace_probe_location_lookup_method *lookup = NULL;
274 const char *probe_name = NULL, *provider_name = NULL;
275 int ret = 0;
276 int fd, i;
277
278 assert(lttng_userspace_probe_location_get_type(probe_location) ==
279 LTTNG_USERSPACE_PROBE_LOCATION_TYPE_TRACEPOINT);
280
281 lookup = lttng_userspace_probe_location_get_lookup_method(probe_location);
282 if (!lookup) {
283 ret = -1;
284 goto end;
285 }
286
287 lookup_method_type =
288 lttng_userspace_probe_location_lookup_method_get_type(lookup);
289
290 assert(lookup_method_type ==
291 LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT);
292
293
294 probe_name = lttng_userspace_probe_location_tracepoint_get_probe_name(
295 probe_location);
296 if (!probe_name) {
297 ret = -1;
298 goto end;
299 }
300
301 provider_name = lttng_userspace_probe_location_tracepoint_get_provider_name(
302 probe_location);
303 if (!provider_name) {
304 ret = -1;
305 goto end;
306 }
307
308 fd = lttng_userspace_probe_location_tracepoint_get_binary_fd(probe_location);
309 if (fd < 0) {
310 ret = -1;
311 goto end;
312 }
313
314 ret = run_as_extract_sdt_probe_offsets(fd, provider_name, probe_name,
315 session->uid, session->gid, offsets, offsets_count);
316 if (ret < 0) {
317 DBG("userspace probe offset calculation failed for sdt "
318 "probe %s:%s", provider_name, probe_name);
319 goto end;
320 }
321
322 if (*offsets_count == 0) {
323 DBG("no userspace probe offset found");
324 goto end;
325 }
326
327 DBG("%u userspace probe SDT offsets found for %s:%s at:",
328 *offsets_count, provider_name, probe_name);
329 for (i = 0; i < *offsets_count; i++) {
330 DBG("\t0x%jd", (intmax_t)((*offsets)[i]));
331 }
332 end:
333 return ret;
334 }
335
336 /*
337 * Extract the offsets of the instrumentation point for the different lookup
338 * methods.
339 */
340 static
341 int userspace_probe_add_callsites(struct lttng_event *ev,
342 struct ltt_kernel_session *session, int fd)
343 {
344 const struct lttng_userspace_probe_location_lookup_method *lookup_method = NULL;
345 enum lttng_userspace_probe_location_lookup_method_type type;
346 const struct lttng_userspace_probe_location *location = NULL;
347 int ret;
348
349 assert(ev);
350 assert(ev->type == LTTNG_EVENT_USERSPACE_PROBE);
351
352 location = lttng_event_get_userspace_probe_location(ev);
353 if (!location) {
354 ret = -1;
355 goto end;
356 }
357 lookup_method =
358 lttng_userspace_probe_location_get_lookup_method(location);
359 if (!lookup_method) {
360 ret = -1;
361 goto end;
362 }
363
364 type = lttng_userspace_probe_location_lookup_method_get_type(lookup_method);
365 switch (type) {
366 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_FUNCTION_ELF:
367 {
368 struct lttng_kernel_event_callsite callsite;
369 uint64_t offset;
370
371 ret = extract_userspace_probe_offset_function_elf(location, session, &offset);
372 if (ret) {
373 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
374 goto end;
375 }
376
377 callsite.u.uprobe.offset = offset;
378 ret = kernctl_add_callsite(fd, &callsite);
379 if (ret) {
380 WARN("Adding callsite to userspace probe "
381 "event %s failed.", ev->name);
382 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
383 goto end;
384 }
385 break;
386 }
387 case LTTNG_USERSPACE_PROBE_LOCATION_LOOKUP_METHOD_TYPE_TRACEPOINT_SDT:
388 {
389 int i;
390 uint64_t *offsets = NULL;
391 uint32_t offsets_count;
392 struct lttng_kernel_event_callsite callsite;
393
394 /*
395 * This call allocates the offsets buffer. This buffer must be freed
396 * by the caller
397 */
398 ret = extract_userspace_probe_offset_tracepoint_sdt(location, session,
399 &offsets, &offsets_count);
400 if (ret) {
401 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
402 goto end;
403 }
404 for (i = 0; i < offsets_count; i++) {
405 callsite.u.uprobe.offset = offsets[i];
406 ret = kernctl_add_callsite(fd, &callsite);
407 if (ret) {
408 WARN("Adding callsite to userspace probe "
409 "event %s failed.", ev->name);
410 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
411 free(offsets);
412 goto end;
413 }
414 }
415 free(offsets);
416 break;
417 }
418 default:
419 ret = LTTNG_ERR_PROBE_LOCATION_INVAL;
420 goto end;
421 }
422 end:
423 return ret;
424 }
425
426 /*
427 * Create a kernel event, enable it to the kernel tracer and add it to the
428 * channel event list of the kernel session.
429 * We own filter_expression and filter.
430 */
431 int kernel_create_event(struct lttng_event *ev,
432 struct ltt_kernel_channel *channel,
433 char *filter_expression,
434 struct lttng_filter_bytecode *filter)
435 {
436 int err, fd;
437 enum lttng_error_code ret;
438 struct ltt_kernel_event *event;
439
440 assert(ev);
441 assert(channel);
442
443 /* We pass ownership of filter_expression and filter */
444 ret = trace_kernel_create_event(ev, filter_expression,
445 filter, &event);
446 if (ret != LTTNG_OK) {
447 goto error;
448 }
449
450 fd = kernctl_create_event(channel->fd, event->event);
451 if (fd < 0) {
452 switch (-fd) {
453 case EEXIST:
454 ret = LTTNG_ERR_KERN_EVENT_EXIST;
455 break;
456 case ENOSYS:
457 WARN("Event type not implemented");
458 ret = LTTNG_ERR_KERN_EVENT_ENOSYS;
459 break;
460 case ENOENT:
461 WARN("Event %s not found!", ev->name);
462 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
463 break;
464 default:
465 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
466 PERROR("create event ioctl");
467 }
468 goto free_event;
469 }
470
471 event->type = ev->type;
472 event->fd = fd;
473 /* Prevent fd duplication after execlp() */
474 err = fcntl(event->fd, F_SETFD, FD_CLOEXEC);
475 if (err < 0) {
476 PERROR("fcntl session fd");
477 }
478
479 if (filter) {
480 err = kernctl_filter(event->fd, filter);
481 if (err < 0) {
482 switch (-err) {
483 case ENOMEM:
484 ret = LTTNG_ERR_FILTER_NOMEM;
485 break;
486 default:
487 ret = LTTNG_ERR_FILTER_INVAL;
488 break;
489 }
490 goto filter_error;
491 }
492 }
493
494 if (ev->type == LTTNG_EVENT_USERSPACE_PROBE) {
495 ret = userspace_probe_add_callsites(ev, channel->session, event->fd);
496 if (ret) {
497 goto add_callsite_error;
498 }
499 }
500
501 err = kernctl_enable(event->fd);
502 if (err < 0) {
503 switch (-err) {
504 case EEXIST:
505 ret = LTTNG_ERR_KERN_EVENT_EXIST;
506 break;
507 default:
508 PERROR("enable kernel event");
509 ret = LTTNG_ERR_KERN_ENABLE_FAIL;
510 break;
511 }
512 goto enable_error;
513 }
514
515 /* Add event to event list */
516 cds_list_add(&event->list, &channel->events_list.head);
517 channel->event_count++;
518
519 DBG("Event %s created (fd: %d)", ev->name, event->fd);
520
521 return 0;
522
523 add_callsite_error:
524 enable_error:
525 filter_error:
526 {
527 int closeret;
528
529 closeret = close(event->fd);
530 if (closeret) {
531 PERROR("close event fd");
532 }
533 }
534 free_event:
535 free(event);
536 error:
537 return ret;
538 }
539
540 /*
541 * Disable a kernel channel.
542 */
543 int kernel_disable_channel(struct ltt_kernel_channel *chan)
544 {
545 int ret;
546
547 assert(chan);
548
549 ret = kernctl_disable(chan->fd);
550 if (ret < 0) {
551 PERROR("disable chan ioctl");
552 goto error;
553 }
554
555 chan->enabled = 0;
556 DBG("Kernel channel %s disabled (fd: %d, key: %" PRIu64 ")",
557 chan->channel->name, chan->fd, chan->key);
558
559 return 0;
560
561 error:
562 return ret;
563 }
564
565 /*
566 * Enable a kernel channel.
567 */
568 int kernel_enable_channel(struct ltt_kernel_channel *chan)
569 {
570 int ret;
571
572 assert(chan);
573
574 ret = kernctl_enable(chan->fd);
575 if (ret < 0 && ret != -EEXIST) {
576 PERROR("Enable kernel chan");
577 goto error;
578 }
579
580 chan->enabled = 1;
581 DBG("Kernel channel %s enabled (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 event.
592 */
593 int kernel_enable_event(struct ltt_kernel_event *event)
594 {
595 int ret;
596
597 assert(event);
598
599 ret = kernctl_enable(event->fd);
600 if (ret < 0) {
601 switch (-ret) {
602 case EEXIST:
603 ret = LTTNG_ERR_KERN_EVENT_EXIST;
604 break;
605 default:
606 PERROR("enable kernel event");
607 break;
608 }
609 goto error;
610 }
611
612 event->enabled = 1;
613 DBG("Kernel event %s enabled (fd: %d)", event->event->name, event->fd);
614
615 return 0;
616
617 error:
618 return ret;
619 }
620
621 /*
622 * Disable a kernel event.
623 */
624 int kernel_disable_event(struct ltt_kernel_event *event)
625 {
626 int ret;
627
628 assert(event);
629
630 ret = kernctl_disable(event->fd);
631 if (ret < 0) {
632 switch (-ret) {
633 case EEXIST:
634 ret = LTTNG_ERR_KERN_EVENT_EXIST;
635 break;
636 default:
637 PERROR("disable kernel event");
638 break;
639 }
640 goto error;
641 }
642
643 event->enabled = 0;
644 DBG("Kernel event %s disabled (fd: %d)", event->event->name, event->fd);
645
646 return 0;
647
648 error:
649 return ret;
650 }
651
652
653 int kernel_track_pid(struct ltt_kernel_session *session, int pid)
654 {
655 int ret;
656
657 DBG("Kernel track PID %d for session id %" PRIu64 ".",
658 pid, session->id);
659 ret = kernctl_track_pid(session->fd, pid);
660 if (!ret) {
661 return LTTNG_OK;
662 }
663 switch (-ret) {
664 case EINVAL:
665 return LTTNG_ERR_INVALID;
666 case ENOMEM:
667 return LTTNG_ERR_NOMEM;
668 case EEXIST:
669 return LTTNG_ERR_PID_TRACKED;
670 default:
671 return LTTNG_ERR_UNK;
672 }
673 }
674
675 int kernel_untrack_pid(struct ltt_kernel_session *session, int pid)
676 {
677 int ret;
678
679 DBG("Kernel untrack PID %d for session id %" PRIu64 ".",
680 pid, session->id);
681 ret = kernctl_untrack_pid(session->fd, pid);
682 if (!ret) {
683 return LTTNG_OK;
684 }
685 switch (-ret) {
686 case EINVAL:
687 return LTTNG_ERR_INVALID;
688 case ENOMEM:
689 return LTTNG_ERR_NOMEM;
690 case ENOENT:
691 return LTTNG_ERR_PID_NOT_TRACKED;
692 default:
693 return LTTNG_ERR_UNK;
694 }
695 }
696
697 ssize_t kernel_list_tracker_pids(struct ltt_kernel_session *session,
698 int **_pids)
699 {
700 int fd, ret;
701 int pid;
702 ssize_t nbmem, count = 0;
703 FILE *fp;
704 int *pids;
705
706 fd = kernctl_list_tracker_pids(session->fd);
707 if (fd < 0) {
708 PERROR("kernel tracker pids list");
709 goto error;
710 }
711
712 fp = fdopen(fd, "r");
713 if (fp == NULL) {
714 PERROR("kernel tracker pids list fdopen");
715 goto error_fp;
716 }
717
718 nbmem = KERNEL_TRACKER_PIDS_INIT_LIST_SIZE;
719 pids = zmalloc(sizeof(*pids) * nbmem);
720 if (pids == NULL) {
721 PERROR("alloc list pids");
722 count = -ENOMEM;
723 goto end;
724 }
725
726 while (fscanf(fp, "process { pid = %u; };\n", &pid) == 1) {
727 if (count >= nbmem) {
728 int *new_pids;
729 size_t new_nbmem;
730
731 new_nbmem = nbmem << 1;
732 DBG("Reallocating pids list from %zu to %zu entries",
733 nbmem, new_nbmem);
734 new_pids = realloc(pids, new_nbmem * sizeof(*new_pids));
735 if (new_pids == NULL) {
736 PERROR("realloc list events");
737 free(pids);
738 count = -ENOMEM;
739 goto end;
740 }
741 /* Zero the new memory */
742 memset(new_pids + nbmem, 0,
743 (new_nbmem - nbmem) * sizeof(*new_pids));
744 nbmem = new_nbmem;
745 pids = new_pids;
746 }
747 pids[count++] = pid;
748 }
749
750 *_pids = pids;
751 DBG("Kernel list tracker pids done (%zd pids)", count);
752 end:
753 ret = fclose(fp); /* closes both fp and fd */
754 if (ret) {
755 PERROR("fclose");
756 }
757 return count;
758
759 error_fp:
760 ret = close(fd);
761 if (ret) {
762 PERROR("close");
763 }
764 error:
765 return -1;
766 }
767
768 /*
769 * Create kernel metadata, open from the kernel tracer and add it to the
770 * kernel session.
771 */
772 int kernel_open_metadata(struct ltt_kernel_session *session)
773 {
774 int ret;
775 struct ltt_kernel_metadata *lkm = NULL;
776
777 assert(session);
778
779 /* Allocate kernel metadata */
780 lkm = trace_kernel_create_metadata();
781 if (lkm == NULL) {
782 goto error;
783 }
784
785 /* Kernel tracer metadata creation */
786 ret = kernctl_open_metadata(session->fd, &lkm->conf->attr);
787 if (ret < 0) {
788 goto error_open;
789 }
790
791 lkm->fd = ret;
792 lkm->key = ++next_kernel_channel_key;
793 /* Prevent fd duplication after execlp() */
794 ret = fcntl(lkm->fd, F_SETFD, FD_CLOEXEC);
795 if (ret < 0) {
796 PERROR("fcntl session fd");
797 }
798
799 session->metadata = lkm;
800
801 DBG("Kernel metadata opened (fd: %d)", lkm->fd);
802
803 return 0;
804
805 error_open:
806 trace_kernel_destroy_metadata(lkm);
807 error:
808 return -1;
809 }
810
811 /*
812 * Start tracing session.
813 */
814 int kernel_start_session(struct ltt_kernel_session *session)
815 {
816 int ret;
817
818 assert(session);
819
820 ret = kernctl_start_session(session->fd);
821 if (ret < 0) {
822 PERROR("ioctl start session");
823 goto error;
824 }
825
826 DBG("Kernel session started");
827
828 return 0;
829
830 error:
831 return ret;
832 }
833
834 /*
835 * Make a kernel wait to make sure in-flight probe have completed.
836 */
837 void kernel_wait_quiescent(int fd)
838 {
839 int ret;
840
841 DBG("Kernel quiescent wait on %d", fd);
842
843 ret = kernctl_wait_quiescent(fd);
844 if (ret < 0) {
845 PERROR("wait quiescent ioctl");
846 ERR("Kernel quiescent wait failed");
847 }
848 }
849
850 /*
851 * Force flush buffer of metadata.
852 */
853 int kernel_metadata_flush_buffer(int fd)
854 {
855 int ret;
856
857 DBG("Kernel flushing metadata buffer on fd %d", fd);
858
859 ret = kernctl_buffer_flush(fd);
860 if (ret < 0) {
861 ERR("Fail to flush metadata buffers %d (ret: %d)", fd, ret);
862 }
863
864 return 0;
865 }
866
867 /*
868 * Force flush buffer for channel.
869 */
870 int kernel_flush_buffer(struct ltt_kernel_channel *channel)
871 {
872 int ret;
873 struct ltt_kernel_stream *stream;
874
875 assert(channel);
876
877 DBG("Flush buffer for channel %s", channel->channel->name);
878
879 cds_list_for_each_entry(stream, &channel->stream_list.head, list) {
880 DBG("Flushing channel stream %d", stream->fd);
881 ret = kernctl_buffer_flush(stream->fd);
882 if (ret < 0) {
883 PERROR("ioctl");
884 ERR("Fail to flush buffer for stream %d (ret: %d)",
885 stream->fd, ret);
886 }
887 }
888
889 return 0;
890 }
891
892 /*
893 * Stop tracing session.
894 */
895 int kernel_stop_session(struct ltt_kernel_session *session)
896 {
897 int ret;
898
899 assert(session);
900
901 ret = kernctl_stop_session(session->fd);
902 if (ret < 0) {
903 goto error;
904 }
905
906 DBG("Kernel session stopped");
907
908 return 0;
909
910 error:
911 return ret;
912 }
913
914 /*
915 * Open stream of channel, register it to the kernel tracer and add it
916 * to the stream list of the channel.
917 *
918 * Note: given that the streams may appear in random order wrt CPU
919 * number (e.g. cpu hotplug), the index value of the stream number in
920 * the stream name is not necessarily linked to the CPU number.
921 *
922 * Return the number of created stream. Else, a negative value.
923 */
924 int kernel_open_channel_stream(struct ltt_kernel_channel *channel)
925 {
926 int ret;
927 struct ltt_kernel_stream *lks;
928
929 assert(channel);
930
931 while ((ret = kernctl_create_stream(channel->fd)) >= 0) {
932 lks = trace_kernel_create_stream(channel->channel->name,
933 channel->stream_count);
934 if (lks == NULL) {
935 ret = close(ret);
936 if (ret) {
937 PERROR("close");
938 }
939 goto error;
940 }
941
942 lks->fd = ret;
943 /* Prevent fd duplication after execlp() */
944 ret = fcntl(lks->fd, F_SETFD, FD_CLOEXEC);
945 if (ret < 0) {
946 PERROR("fcntl session fd");
947 }
948
949 lks->tracefile_size = channel->channel->attr.tracefile_size;
950 lks->tracefile_count = channel->channel->attr.tracefile_count;
951
952 /* Add stream to channel stream list */
953 cds_list_add(&lks->list, &channel->stream_list.head);
954 channel->stream_count++;
955
956 DBG("Kernel stream %s created (fd: %d, state: %d)", lks->name, lks->fd,
957 lks->state);
958 }
959
960 return channel->stream_count;
961
962 error:
963 return -1;
964 }
965
966 /*
967 * Open the metadata stream and set it to the kernel session.
968 */
969 int kernel_open_metadata_stream(struct ltt_kernel_session *session)
970 {
971 int ret;
972
973 assert(session);
974
975 ret = kernctl_create_stream(session->metadata->fd);
976 if (ret < 0) {
977 PERROR("kernel create metadata stream");
978 goto error;
979 }
980
981 DBG("Kernel metadata stream created (fd: %d)", ret);
982 session->metadata_stream_fd = ret;
983 /* Prevent fd duplication after execlp() */
984 ret = fcntl(session->metadata_stream_fd, F_SETFD, FD_CLOEXEC);
985 if (ret < 0) {
986 PERROR("fcntl session fd");
987 }
988
989 return 0;
990
991 error:
992 return -1;
993 }
994
995 /*
996 * Get the event list from the kernel tracer and return the number of elements.
997 */
998 ssize_t kernel_list_events(int tracer_fd, struct lttng_event **events)
999 {
1000 int fd, ret;
1001 char *event;
1002 size_t nbmem, count = 0;
1003 FILE *fp;
1004 struct lttng_event *elist;
1005
1006 assert(events);
1007
1008 fd = kernctl_tracepoint_list(tracer_fd);
1009 if (fd < 0) {
1010 PERROR("kernel tracepoint list");
1011 goto error;
1012 }
1013
1014 fp = fdopen(fd, "r");
1015 if (fp == NULL) {
1016 PERROR("kernel tracepoint list fdopen");
1017 goto error_fp;
1018 }
1019
1020 /*
1021 * Init memory size counter
1022 * See kernel-ctl.h for explanation of this value
1023 */
1024 nbmem = KERNEL_EVENT_INIT_LIST_SIZE;
1025 elist = zmalloc(sizeof(struct lttng_event) * nbmem);
1026 if (elist == NULL) {
1027 PERROR("alloc list events");
1028 count = -ENOMEM;
1029 goto end;
1030 }
1031
1032 while (fscanf(fp, "event { name = %m[^;]; };\n", &event) == 1) {
1033 if (count >= nbmem) {
1034 struct lttng_event *new_elist;
1035 size_t new_nbmem;
1036
1037 new_nbmem = nbmem << 1;
1038 DBG("Reallocating event list from %zu to %zu bytes",
1039 nbmem, new_nbmem);
1040 new_elist = realloc(elist, new_nbmem * sizeof(struct lttng_event));
1041 if (new_elist == NULL) {
1042 PERROR("realloc list events");
1043 free(event);
1044 free(elist);
1045 count = -ENOMEM;
1046 goto end;
1047 }
1048 /* Zero the new memory */
1049 memset(new_elist + nbmem, 0,
1050 (new_nbmem - nbmem) * sizeof(struct lttng_event));
1051 nbmem = new_nbmem;
1052 elist = new_elist;
1053 }
1054 strncpy(elist[count].name, event, LTTNG_SYMBOL_NAME_LEN);
1055 elist[count].name[LTTNG_SYMBOL_NAME_LEN - 1] = '\0';
1056 elist[count].enabled = -1;
1057 count++;
1058 free(event);
1059 }
1060
1061 *events = elist;
1062 DBG("Kernel list events done (%zu events)", count);
1063 end:
1064 ret = fclose(fp); /* closes both fp and fd */
1065 if (ret) {
1066 PERROR("fclose");
1067 }
1068 return count;
1069
1070 error_fp:
1071 ret = close(fd);
1072 if (ret) {
1073 PERROR("close");
1074 }
1075 error:
1076 return -1;
1077 }
1078
1079 /*
1080 * Get kernel version and validate it.
1081 */
1082 int kernel_validate_version(int tracer_fd,
1083 struct lttng_kernel_tracer_version *version,
1084 struct lttng_kernel_tracer_abi_version *abi_version)
1085 {
1086 int ret;
1087
1088 ret = kernctl_tracer_version(tracer_fd, version);
1089 if (ret < 0) {
1090 ERR("Failed to retrieve the lttng-modules version");
1091 goto error;
1092 }
1093
1094 /* Validate version */
1095 if (version->major != VERSION_MAJOR) {
1096 ERR("Kernel tracer major version (%d) is not compatible with lttng-tools major version (%d)",
1097 version->major, VERSION_MAJOR);
1098 goto error_version;
1099 }
1100 ret = kernctl_tracer_abi_version(tracer_fd, abi_version);
1101 if (ret < 0) {
1102 ERR("Failed to retrieve lttng-modules ABI version");
1103 goto error;
1104 }
1105 if (abi_version->major != LTTNG_MODULES_ABI_MAJOR_VERSION) {
1106 ERR("Kernel tracer ABI version (%d.%d) does not match the expected ABI major version (%d.*)",
1107 abi_version->major, abi_version->minor,
1108 LTTNG_MODULES_ABI_MAJOR_VERSION);
1109 goto error;
1110 }
1111 DBG2("Kernel tracer version validated (%d.%d, ABI %d.%d)",
1112 version->major, version->minor,
1113 abi_version->major, abi_version->minor);
1114 return 0;
1115
1116 error_version:
1117 ret = -1;
1118
1119 error:
1120 ERR("Kernel tracer version check failed; kernel tracing will not be available");
1121 return ret;
1122 }
1123
1124 /*
1125 * Kernel work-arounds called at the start of sessiond main().
1126 */
1127 int init_kernel_workarounds(void)
1128 {
1129 int ret;
1130 FILE *fp;
1131
1132 /*
1133 * boot_id needs to be read once before being used concurrently
1134 * to deal with a Linux kernel race. A fix is proposed for
1135 * upstream, but the work-around is needed for older kernels.
1136 */
1137 fp = fopen("/proc/sys/kernel/random/boot_id", "r");
1138 if (!fp) {
1139 goto end_boot_id;
1140 }
1141 while (!feof(fp)) {
1142 char buf[37] = "";
1143
1144 ret = fread(buf, 1, sizeof(buf), fp);
1145 if (ret < 0) {
1146 /* Ignore error, we don't really care */
1147 }
1148 }
1149 ret = fclose(fp);
1150 if (ret) {
1151 PERROR("fclose");
1152 }
1153 end_boot_id:
1154 return 0;
1155 }
1156
1157 /*
1158 * Complete teardown of a kernel session.
1159 */
1160 void kernel_destroy_session(struct ltt_kernel_session *ksess)
1161 {
1162 if (ksess == NULL) {
1163 DBG3("No kernel session when tearing down session");
1164 return;
1165 }
1166
1167 DBG("Tearing down kernel session");
1168
1169 /*
1170 * Destroy channels on the consumer if at least one FD has been sent and we
1171 * are in no output mode because the streams are in *no* monitor mode so we
1172 * have to send a command to clean them up or else they leaked.
1173 */
1174 if (!ksess->output_traces && ksess->consumer_fds_sent) {
1175 int ret;
1176 struct consumer_socket *socket;
1177 struct lttng_ht_iter iter;
1178
1179 /* For each consumer socket. */
1180 rcu_read_lock();
1181 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1182 socket, node.node) {
1183 struct ltt_kernel_channel *chan;
1184
1185 /* For each channel, ask the consumer to destroy it. */
1186 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1187 ret = kernel_consumer_destroy_channel(socket, chan);
1188 if (ret < 0) {
1189 /* Consumer is probably dead. Use next socket. */
1190 continue;
1191 }
1192 }
1193 }
1194 rcu_read_unlock();
1195 }
1196
1197 /* Close any relayd session */
1198 consumer_output_send_destroy_relayd(ksess->consumer);
1199
1200 trace_kernel_destroy_session(ksess);
1201 }
1202
1203 /*
1204 * Destroy a kernel channel object. It does not do anything on the tracer side.
1205 */
1206 void kernel_destroy_channel(struct ltt_kernel_channel *kchan)
1207 {
1208 struct ltt_kernel_session *ksess = NULL;
1209
1210 assert(kchan);
1211 assert(kchan->channel);
1212
1213 DBG3("Kernel destroy channel %s", kchan->channel->name);
1214
1215 /* Update channel count of associated session. */
1216 if (kchan->session) {
1217 /* Keep pointer reference so we can update it after the destroy. */
1218 ksess = kchan->session;
1219 }
1220
1221 trace_kernel_destroy_channel(kchan);
1222
1223 /*
1224 * At this point the kernel channel is not visible anymore. This is safe
1225 * since in order to work on a visible kernel session, the tracing session
1226 * lock (ltt_session.lock) MUST be acquired.
1227 */
1228 if (ksess) {
1229 ksess->channel_count--;
1230 }
1231 }
1232
1233 /*
1234 * Take a snapshot for a given kernel session.
1235 *
1236 * Return 0 on success or else return a LTTNG_ERR code.
1237 */
1238 int kernel_snapshot_record(struct ltt_kernel_session *ksess,
1239 struct snapshot_output *output, int wait,
1240 uint64_t nb_packets_per_stream)
1241 {
1242 int err, ret, saved_metadata_fd;
1243 struct consumer_socket *socket;
1244 struct lttng_ht_iter iter;
1245 struct ltt_kernel_metadata *saved_metadata;
1246 struct ltt_session *session;
1247 uint64_t trace_archive_id;
1248
1249 assert(ksess);
1250 assert(ksess->consumer);
1251 assert(output);
1252
1253 DBG("Kernel snapshot record started");
1254
1255 session = session_find_by_id(ksess->id);
1256 assert(session);
1257 assert(pthread_mutex_trylock(&session->lock));
1258 assert(session_trylock_list());
1259 trace_archive_id = session->current_archive_id;
1260
1261 /* Save current metadata since the following calls will change it. */
1262 saved_metadata = ksess->metadata;
1263 saved_metadata_fd = ksess->metadata_stream_fd;
1264
1265 rcu_read_lock();
1266
1267 ret = kernel_open_metadata(ksess);
1268 if (ret < 0) {
1269 ret = LTTNG_ERR_KERN_META_FAIL;
1270 goto error;
1271 }
1272
1273 ret = kernel_open_metadata_stream(ksess);
1274 if (ret < 0) {
1275 ret = LTTNG_ERR_KERN_META_FAIL;
1276 goto error_open_stream;
1277 }
1278
1279 /* Send metadata to consumer and snapshot everything. */
1280 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1281 socket, node.node) {
1282 struct consumer_output *saved_output;
1283 struct ltt_kernel_channel *chan;
1284
1285 /*
1286 * Temporarly switch consumer output for our snapshot output. As long
1287 * as the session lock is taken, this is safe.
1288 */
1289 saved_output = ksess->consumer;
1290 ksess->consumer = output->consumer;
1291
1292 pthread_mutex_lock(socket->lock);
1293 /* This stream must not be monitored by the consumer. */
1294 ret = kernel_consumer_add_metadata(socket, ksess, 0);
1295 pthread_mutex_unlock(socket->lock);
1296 /* Put back the saved consumer output into the session. */
1297 ksess->consumer = saved_output;
1298 if (ret < 0) {
1299 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1300 goto error_consumer;
1301 }
1302
1303 /* For each channel, ask the consumer to snapshot it. */
1304 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1305 ret = consumer_snapshot_channel(socket, chan->key, output, 0,
1306 ksess->uid, ksess->gid,
1307 DEFAULT_KERNEL_TRACE_DIR, wait,
1308 nb_packets_per_stream,
1309 trace_archive_id);
1310 if (ret < 0) {
1311 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1312 (void) kernel_consumer_destroy_metadata(socket,
1313 ksess->metadata);
1314 goto error_consumer;
1315 }
1316 }
1317
1318 /* Snapshot metadata, */
1319 ret = consumer_snapshot_channel(socket, ksess->metadata->key, output,
1320 1, ksess->uid, ksess->gid,
1321 DEFAULT_KERNEL_TRACE_DIR, wait, 0,
1322 trace_archive_id);
1323 if (ret < 0) {
1324 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1325 goto error_consumer;
1326 }
1327
1328 /*
1329 * The metadata snapshot is done, ask the consumer to destroy it since
1330 * it's not monitored on the consumer side.
1331 */
1332 (void) kernel_consumer_destroy_metadata(socket, ksess->metadata);
1333 }
1334
1335 ret = LTTNG_OK;
1336
1337 error_consumer:
1338 /* Close newly opened metadata stream. It's now on the consumer side. */
1339 err = close(ksess->metadata_stream_fd);
1340 if (err < 0) {
1341 PERROR("close snapshot kernel");
1342 }
1343
1344 error_open_stream:
1345 trace_kernel_destroy_metadata(ksess->metadata);
1346 error:
1347 /* Restore metadata state.*/
1348 ksess->metadata = saved_metadata;
1349 ksess->metadata_stream_fd = saved_metadata_fd;
1350
1351 rcu_read_unlock();
1352 return ret;
1353 }
1354
1355 /*
1356 * Get the syscall mask array from the kernel tracer.
1357 *
1358 * Return 0 on success else a negative value. In both case, syscall_mask should
1359 * be freed.
1360 */
1361 int kernel_syscall_mask(int chan_fd, char **syscall_mask, uint32_t *nr_bits)
1362 {
1363 assert(syscall_mask);
1364 assert(nr_bits);
1365
1366 return kernctl_syscall_mask(chan_fd, syscall_mask, nr_bits);
1367 }
1368
1369 /*
1370 * Check for the support of the RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS via abi
1371 * version number.
1372 *
1373 * Return 1 on success, 0 when feature is not supported, negative value in case
1374 * of errors.
1375 */
1376 int kernel_supports_ring_buffer_snapshot_sample_positions(int tracer_fd)
1377 {
1378 int ret = 0; // Not supported by default
1379 struct lttng_kernel_tracer_abi_version abi;
1380
1381 ret = kernctl_tracer_abi_version(tracer_fd, &abi);
1382 if (ret < 0) {
1383 ERR("Failed to retrieve lttng-modules ABI version");
1384 goto error;
1385 }
1386
1387 /*
1388 * RING_BUFFER_SNAPSHOT_SAMPLE_POSITIONS was introduced in 2.3
1389 */
1390 if (abi.major >= 2 && abi.minor >= 3) {
1391 /* Supported */
1392 ret = 1;
1393 } else {
1394 /* Not supported */
1395 ret = 0;
1396 }
1397 error:
1398 return ret;
1399 }
1400
1401 /*
1402 * Rotate a kernel session.
1403 *
1404 * Return 0 on success or else return a LTTNG_ERR code.
1405 */
1406 int kernel_rotate_session(struct ltt_session *session)
1407 {
1408 int ret;
1409 struct consumer_socket *socket;
1410 struct lttng_ht_iter iter;
1411 struct ltt_kernel_session *ksess = session->kernel_session;
1412
1413 assert(ksess);
1414 assert(ksess->consumer);
1415
1416 DBG("Rotate kernel session %s started (session %" PRIu64 ")",
1417 session->name, session->id);
1418
1419 rcu_read_lock();
1420
1421 /*
1422 * Note that this loop will end after one iteration given that there is
1423 * only one kernel consumer.
1424 */
1425 cds_lfht_for_each_entry(ksess->consumer->socks->ht, &iter.iter,
1426 socket, node.node) {
1427 struct ltt_kernel_channel *chan;
1428
1429 /* For each channel, ask the consumer to rotate it. */
1430 cds_list_for_each_entry(chan, &ksess->channel_list.head, list) {
1431 DBG("Rotate kernel channel %" PRIu64 ", session %s",
1432 chan->key, session->name);
1433 ret = consumer_rotate_channel(socket, chan->key,
1434 ksess->uid, ksess->gid, ksess->consumer,
1435 ksess->consumer->subdir,
1436 /* is_metadata_channel */ false,
1437 session->current_archive_id);
1438 if (ret < 0) {
1439 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1440 goto error;
1441 }
1442 }
1443
1444 /*
1445 * Rotate the metadata channel.
1446 */
1447 ret = consumer_rotate_channel(socket, ksess->metadata->key,
1448 ksess->uid, ksess->gid, ksess->consumer,
1449 ksess->consumer->subdir,
1450 /* is_metadata_channel */ true,
1451 session->current_archive_id);
1452 if (ret < 0) {
1453 ret = LTTNG_ERR_KERN_CONSUMER_FAIL;
1454 goto error;
1455 }
1456 }
1457
1458 ret = LTTNG_OK;
1459
1460 error:
1461 rcu_read_unlock();
1462 return ret;
1463 }
This page took 0.097766 seconds and 3 git commands to generate.