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