Loading...
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
5 */
6
7#include <inttypes.h>
8#include <sys/types.h>
9#include <sys/mman.h>
10#include <stdbool.h>
11#include <string.h>
12#include <limits.h>
13#include <errno.h>
14
15#include <linux/kernel.h>
16#include <linux/perf_event.h>
17#include <linux/types.h>
18#include <linux/bitops.h>
19#include <linux/log2.h>
20#include <linux/string.h>
21#include <linux/time64.h>
22
23#include <sys/param.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <linux/list.h>
27#include <linux/zalloc.h>
28
29#include "config.h"
30#include "evlist.h"
31#include "dso.h"
32#include "map.h"
33#include "pmu.h"
34#include "evsel.h"
35#include "evsel_config.h"
36#include "symbol.h"
37#include "util/perf_api_probe.h"
38#include "util/synthetic-events.h"
39#include "thread_map.h"
40#include "asm/bug.h"
41#include "auxtrace.h"
42
43#include <linux/hash.h>
44
45#include "event.h"
46#include "record.h"
47#include "session.h"
48#include "debug.h"
49#include <subcmd/parse-options.h>
50
51#include "cs-etm.h"
52#include "intel-pt.h"
53#include "intel-bts.h"
54#include "arm-spe.h"
55#include "hisi-ptt.h"
56#include "s390-cpumsf.h"
57#include "util/mmap.h"
58
59#include <linux/ctype.h>
60#include "symbol/kallsyms.h"
61#include <internal/lib.h>
62#include "util/sample.h"
63
64/*
65 * Make a group from 'leader' to 'last', requiring that the events were not
66 * already grouped to a different leader.
67 */
68static int evlist__regroup(struct evlist *evlist, struct evsel *leader, struct evsel *last)
69{
70 struct evsel *evsel;
71 bool grp;
72
73 if (!evsel__is_group_leader(leader))
74 return -EINVAL;
75
76 grp = false;
77 evlist__for_each_entry(evlist, evsel) {
78 if (grp) {
79 if (!(evsel__leader(evsel) == leader ||
80 (evsel__leader(evsel) == evsel &&
81 evsel->core.nr_members <= 1)))
82 return -EINVAL;
83 } else if (evsel == leader) {
84 grp = true;
85 }
86 if (evsel == last)
87 break;
88 }
89
90 grp = false;
91 evlist__for_each_entry(evlist, evsel) {
92 if (grp) {
93 if (!evsel__has_leader(evsel, leader)) {
94 evsel__set_leader(evsel, leader);
95 if (leader->core.nr_members < 1)
96 leader->core.nr_members = 1;
97 leader->core.nr_members += 1;
98 }
99 } else if (evsel == leader) {
100 grp = true;
101 }
102 if (evsel == last)
103 break;
104 }
105
106 return 0;
107}
108
109static bool auxtrace__dont_decode(struct perf_session *session)
110{
111 return !session->itrace_synth_opts ||
112 session->itrace_synth_opts->dont_decode;
113}
114
115int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
116 struct auxtrace_mmap_params *mp,
117 void *userpg, int fd)
118{
119 struct perf_event_mmap_page *pc = userpg;
120
121 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
122
123 mm->userpg = userpg;
124 mm->mask = mp->mask;
125 mm->len = mp->len;
126 mm->prev = 0;
127 mm->idx = mp->idx;
128 mm->tid = mp->tid;
129 mm->cpu = mp->cpu.cpu;
130
131 if (!mp->len || !mp->mmap_needed) {
132 mm->base = NULL;
133 return 0;
134 }
135
136 pc->aux_offset = mp->offset;
137 pc->aux_size = mp->len;
138
139 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
140 if (mm->base == MAP_FAILED) {
141 pr_debug2("failed to mmap AUX area\n");
142 mm->base = NULL;
143 return -1;
144 }
145
146 return 0;
147}
148
149void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
150{
151 if (mm->base) {
152 munmap(mm->base, mm->len);
153 mm->base = NULL;
154 }
155}
156
157void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
158 off_t auxtrace_offset,
159 unsigned int auxtrace_pages,
160 bool auxtrace_overwrite)
161{
162 if (auxtrace_pages) {
163 mp->offset = auxtrace_offset;
164 mp->len = auxtrace_pages * (size_t)page_size;
165 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
166 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
167 pr_debug2("AUX area mmap length %zu\n", mp->len);
168 } else {
169 mp->len = 0;
170 }
171}
172
173void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
174 struct evlist *evlist,
175 struct evsel *evsel, int idx)
176{
177 bool per_cpu = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
178
179 mp->mmap_needed = evsel->needs_auxtrace_mmap;
180
181 if (!mp->mmap_needed)
182 return;
183
184 mp->idx = idx;
185
186 if (per_cpu) {
187 mp->cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
188 if (evlist->core.threads)
189 mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
190 else
191 mp->tid = -1;
192 } else {
193 mp->cpu.cpu = -1;
194 mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
195 }
196}
197
198#define AUXTRACE_INIT_NR_QUEUES 32
199
200static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
201{
202 struct auxtrace_queue *queue_array;
203 unsigned int max_nr_queues, i;
204
205 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
206 if (nr_queues > max_nr_queues)
207 return NULL;
208
209 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
210 if (!queue_array)
211 return NULL;
212
213 for (i = 0; i < nr_queues; i++) {
214 INIT_LIST_HEAD(&queue_array[i].head);
215 queue_array[i].priv = NULL;
216 }
217
218 return queue_array;
219}
220
221int auxtrace_queues__init(struct auxtrace_queues *queues)
222{
223 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
224 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
225 if (!queues->queue_array)
226 return -ENOMEM;
227 return 0;
228}
229
230static int auxtrace_queues__grow(struct auxtrace_queues *queues,
231 unsigned int new_nr_queues)
232{
233 unsigned int nr_queues = queues->nr_queues;
234 struct auxtrace_queue *queue_array;
235 unsigned int i;
236
237 if (!nr_queues)
238 nr_queues = AUXTRACE_INIT_NR_QUEUES;
239
240 while (nr_queues && nr_queues < new_nr_queues)
241 nr_queues <<= 1;
242
243 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
244 return -EINVAL;
245
246 queue_array = auxtrace_alloc_queue_array(nr_queues);
247 if (!queue_array)
248 return -ENOMEM;
249
250 for (i = 0; i < queues->nr_queues; i++) {
251 list_splice_tail(&queues->queue_array[i].head,
252 &queue_array[i].head);
253 queue_array[i].tid = queues->queue_array[i].tid;
254 queue_array[i].cpu = queues->queue_array[i].cpu;
255 queue_array[i].set = queues->queue_array[i].set;
256 queue_array[i].priv = queues->queue_array[i].priv;
257 }
258
259 queues->nr_queues = nr_queues;
260 queues->queue_array = queue_array;
261
262 return 0;
263}
264
265static void *auxtrace_copy_data(u64 size, struct perf_session *session)
266{
267 int fd = perf_data__fd(session->data);
268 void *p;
269 ssize_t ret;
270
271 if (size > SSIZE_MAX)
272 return NULL;
273
274 p = malloc(size);
275 if (!p)
276 return NULL;
277
278 ret = readn(fd, p, size);
279 if (ret != (ssize_t)size) {
280 free(p);
281 return NULL;
282 }
283
284 return p;
285}
286
287static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
288 unsigned int idx,
289 struct auxtrace_buffer *buffer)
290{
291 struct auxtrace_queue *queue;
292 int err;
293
294 if (idx >= queues->nr_queues) {
295 err = auxtrace_queues__grow(queues, idx + 1);
296 if (err)
297 return err;
298 }
299
300 queue = &queues->queue_array[idx];
301
302 if (!queue->set) {
303 queue->set = true;
304 queue->tid = buffer->tid;
305 queue->cpu = buffer->cpu.cpu;
306 }
307
308 buffer->buffer_nr = queues->next_buffer_nr++;
309
310 list_add_tail(&buffer->list, &queue->head);
311
312 queues->new_data = true;
313 queues->populated = true;
314
315 return 0;
316}
317
318/* Limit buffers to 32MiB on 32-bit */
319#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
320
321static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
322 unsigned int idx,
323 struct auxtrace_buffer *buffer)
324{
325 u64 sz = buffer->size;
326 bool consecutive = false;
327 struct auxtrace_buffer *b;
328 int err;
329
330 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
331 b = memdup(buffer, sizeof(struct auxtrace_buffer));
332 if (!b)
333 return -ENOMEM;
334 b->size = BUFFER_LIMIT_FOR_32_BIT;
335 b->consecutive = consecutive;
336 err = auxtrace_queues__queue_buffer(queues, idx, b);
337 if (err) {
338 auxtrace_buffer__free(b);
339 return err;
340 }
341 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
342 sz -= BUFFER_LIMIT_FOR_32_BIT;
343 consecutive = true;
344 }
345
346 buffer->size = sz;
347 buffer->consecutive = consecutive;
348
349 return 0;
350}
351
352static bool filter_cpu(struct perf_session *session, struct perf_cpu cpu)
353{
354 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
355
356 return cpu_bitmap && cpu.cpu != -1 && !test_bit(cpu.cpu, cpu_bitmap);
357}
358
359static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
360 struct perf_session *session,
361 unsigned int idx,
362 struct auxtrace_buffer *buffer,
363 struct auxtrace_buffer **buffer_ptr)
364{
365 int err = -ENOMEM;
366
367 if (filter_cpu(session, buffer->cpu))
368 return 0;
369
370 buffer = memdup(buffer, sizeof(*buffer));
371 if (!buffer)
372 return -ENOMEM;
373
374 if (session->one_mmap) {
375 buffer->data = buffer->data_offset - session->one_mmap_offset +
376 session->one_mmap_addr;
377 } else if (perf_data__is_pipe(session->data)) {
378 buffer->data = auxtrace_copy_data(buffer->size, session);
379 if (!buffer->data)
380 goto out_free;
381 buffer->data_needs_freeing = true;
382 } else if (BITS_PER_LONG == 32 &&
383 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
384 err = auxtrace_queues__split_buffer(queues, idx, buffer);
385 if (err)
386 goto out_free;
387 }
388
389 err = auxtrace_queues__queue_buffer(queues, idx, buffer);
390 if (err)
391 goto out_free;
392
393 /* FIXME: Doesn't work for split buffer */
394 if (buffer_ptr)
395 *buffer_ptr = buffer;
396
397 return 0;
398
399out_free:
400 auxtrace_buffer__free(buffer);
401 return err;
402}
403
404int auxtrace_queues__add_event(struct auxtrace_queues *queues,
405 struct perf_session *session,
406 union perf_event *event, off_t data_offset,
407 struct auxtrace_buffer **buffer_ptr)
408{
409 struct auxtrace_buffer buffer = {
410 .pid = -1,
411 .tid = event->auxtrace.tid,
412 .cpu = { event->auxtrace.cpu },
413 .data_offset = data_offset,
414 .offset = event->auxtrace.offset,
415 .reference = event->auxtrace.reference,
416 .size = event->auxtrace.size,
417 };
418 unsigned int idx = event->auxtrace.idx;
419
420 return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
421 buffer_ptr);
422}
423
424static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
425 struct perf_session *session,
426 off_t file_offset, size_t sz)
427{
428 union perf_event *event;
429 int err;
430 char buf[PERF_SAMPLE_MAX_SIZE];
431
432 err = perf_session__peek_event(session, file_offset, buf,
433 PERF_SAMPLE_MAX_SIZE, &event, NULL);
434 if (err)
435 return err;
436
437 if (event->header.type == PERF_RECORD_AUXTRACE) {
438 if (event->header.size < sizeof(struct perf_record_auxtrace) ||
439 event->header.size != sz) {
440 err = -EINVAL;
441 goto out;
442 }
443 file_offset += event->header.size;
444 err = auxtrace_queues__add_event(queues, session, event,
445 file_offset, NULL);
446 }
447out:
448 return err;
449}
450
451void auxtrace_queues__free(struct auxtrace_queues *queues)
452{
453 unsigned int i;
454
455 for (i = 0; i < queues->nr_queues; i++) {
456 while (!list_empty(&queues->queue_array[i].head)) {
457 struct auxtrace_buffer *buffer;
458
459 buffer = list_entry(queues->queue_array[i].head.next,
460 struct auxtrace_buffer, list);
461 list_del_init(&buffer->list);
462 auxtrace_buffer__free(buffer);
463 }
464 }
465
466 zfree(&queues->queue_array);
467 queues->nr_queues = 0;
468}
469
470static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
471 unsigned int pos, unsigned int queue_nr,
472 u64 ordinal)
473{
474 unsigned int parent;
475
476 while (pos) {
477 parent = (pos - 1) >> 1;
478 if (heap_array[parent].ordinal <= ordinal)
479 break;
480 heap_array[pos] = heap_array[parent];
481 pos = parent;
482 }
483 heap_array[pos].queue_nr = queue_nr;
484 heap_array[pos].ordinal = ordinal;
485}
486
487int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
488 u64 ordinal)
489{
490 struct auxtrace_heap_item *heap_array;
491
492 if (queue_nr >= heap->heap_sz) {
493 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
494
495 while (heap_sz <= queue_nr)
496 heap_sz <<= 1;
497 heap_array = realloc(heap->heap_array,
498 heap_sz * sizeof(struct auxtrace_heap_item));
499 if (!heap_array)
500 return -ENOMEM;
501 heap->heap_array = heap_array;
502 heap->heap_sz = heap_sz;
503 }
504
505 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
506
507 return 0;
508}
509
510void auxtrace_heap__free(struct auxtrace_heap *heap)
511{
512 zfree(&heap->heap_array);
513 heap->heap_cnt = 0;
514 heap->heap_sz = 0;
515}
516
517void auxtrace_heap__pop(struct auxtrace_heap *heap)
518{
519 unsigned int pos, last, heap_cnt = heap->heap_cnt;
520 struct auxtrace_heap_item *heap_array;
521
522 if (!heap_cnt)
523 return;
524
525 heap->heap_cnt -= 1;
526
527 heap_array = heap->heap_array;
528
529 pos = 0;
530 while (1) {
531 unsigned int left, right;
532
533 left = (pos << 1) + 1;
534 if (left >= heap_cnt)
535 break;
536 right = left + 1;
537 if (right >= heap_cnt) {
538 heap_array[pos] = heap_array[left];
539 return;
540 }
541 if (heap_array[left].ordinal < heap_array[right].ordinal) {
542 heap_array[pos] = heap_array[left];
543 pos = left;
544 } else {
545 heap_array[pos] = heap_array[right];
546 pos = right;
547 }
548 }
549
550 last = heap_cnt - 1;
551 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
552 heap_array[last].ordinal);
553}
554
555size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
556 struct evlist *evlist)
557{
558 if (itr)
559 return itr->info_priv_size(itr, evlist);
560 return 0;
561}
562
563static int auxtrace_not_supported(void)
564{
565 pr_err("AUX area tracing is not supported on this architecture\n");
566 return -EINVAL;
567}
568
569int auxtrace_record__info_fill(struct auxtrace_record *itr,
570 struct perf_session *session,
571 struct perf_record_auxtrace_info *auxtrace_info,
572 size_t priv_size)
573{
574 if (itr)
575 return itr->info_fill(itr, session, auxtrace_info, priv_size);
576 return auxtrace_not_supported();
577}
578
579void auxtrace_record__free(struct auxtrace_record *itr)
580{
581 if (itr)
582 itr->free(itr);
583}
584
585int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
586{
587 if (itr && itr->snapshot_start)
588 return itr->snapshot_start(itr);
589 return 0;
590}
591
592int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
593{
594 if (!on_exit && itr && itr->snapshot_finish)
595 return itr->snapshot_finish(itr);
596 return 0;
597}
598
599int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
600 struct auxtrace_mmap *mm,
601 unsigned char *data, u64 *head, u64 *old)
602{
603 if (itr && itr->find_snapshot)
604 return itr->find_snapshot(itr, idx, mm, data, head, old);
605 return 0;
606}
607
608int auxtrace_record__options(struct auxtrace_record *itr,
609 struct evlist *evlist,
610 struct record_opts *opts)
611{
612 if (itr) {
613 itr->evlist = evlist;
614 return itr->recording_options(itr, evlist, opts);
615 }
616 return 0;
617}
618
619u64 auxtrace_record__reference(struct auxtrace_record *itr)
620{
621 if (itr)
622 return itr->reference(itr);
623 return 0;
624}
625
626int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
627 struct record_opts *opts, const char *str)
628{
629 if (!str)
630 return 0;
631
632 /* PMU-agnostic options */
633 switch (*str) {
634 case 'e':
635 opts->auxtrace_snapshot_on_exit = true;
636 str++;
637 break;
638 default:
639 break;
640 }
641
642 if (itr && itr->parse_snapshot_options)
643 return itr->parse_snapshot_options(itr, opts, str);
644
645 pr_err("No AUX area tracing to snapshot\n");
646 return -EINVAL;
647}
648
649static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
650{
651 bool per_cpu_mmaps = !perf_cpu_map__has_any_cpu_or_is_empty(evlist->core.user_requested_cpus);
652
653 if (per_cpu_mmaps) {
654 struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
655 int cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
656
657 if (cpu_map_idx == -1)
658 return -EINVAL;
659 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
660 }
661
662 return perf_evsel__enable_thread(&evsel->core, idx);
663}
664
665int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
666{
667 struct evsel *evsel;
668
669 if (!itr->evlist || !itr->pmu)
670 return -EINVAL;
671
672 evlist__for_each_entry(itr->evlist, evsel) {
673 if (evsel->core.attr.type == itr->pmu->type) {
674 if (evsel->disabled)
675 return 0;
676 return evlist__enable_event_idx(itr->evlist, evsel, idx);
677 }
678 }
679 return -EINVAL;
680}
681
682/*
683 * Event record size is 16-bit which results in a maximum size of about 64KiB.
684 * Allow about 4KiB for the rest of the sample record, to give a maximum
685 * AUX area sample size of 60KiB.
686 */
687#define MAX_AUX_SAMPLE_SIZE (60 * 1024)
688
689/* Arbitrary default size if no other default provided */
690#define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)
691
692static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
693 struct record_opts *opts)
694{
695 struct evsel *evsel;
696 bool has_aux_leader = false;
697 u32 sz;
698
699 evlist__for_each_entry(evlist, evsel) {
700 sz = evsel->core.attr.aux_sample_size;
701 if (evsel__is_group_leader(evsel)) {
702 has_aux_leader = evsel__is_aux_event(evsel);
703 if (sz) {
704 if (has_aux_leader)
705 pr_err("Cannot add AUX area sampling to an AUX area event\n");
706 else
707 pr_err("Cannot add AUX area sampling to a group leader\n");
708 return -EINVAL;
709 }
710 }
711 if (sz > MAX_AUX_SAMPLE_SIZE) {
712 pr_err("AUX area sample size %u too big, max. %d\n",
713 sz, MAX_AUX_SAMPLE_SIZE);
714 return -EINVAL;
715 }
716 if (sz) {
717 if (!has_aux_leader) {
718 pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
719 return -EINVAL;
720 }
721 evsel__set_sample_bit(evsel, AUX);
722 opts->auxtrace_sample_mode = true;
723 } else {
724 evsel__reset_sample_bit(evsel, AUX);
725 }
726 }
727
728 if (!opts->auxtrace_sample_mode) {
729 pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
730 return -EINVAL;
731 }
732
733 if (!perf_can_aux_sample()) {
734 pr_err("AUX area sampling is not supported by kernel\n");
735 return -EINVAL;
736 }
737
738 return 0;
739}
740
741int auxtrace_parse_sample_options(struct auxtrace_record *itr,
742 struct evlist *evlist,
743 struct record_opts *opts, const char *str)
744{
745 struct evsel_config_term *term;
746 struct evsel *aux_evsel;
747 bool has_aux_sample_size = false;
748 bool has_aux_leader = false;
749 struct evsel *evsel;
750 char *endptr;
751 unsigned long sz;
752
753 if (!str)
754 goto no_opt;
755
756 if (!itr) {
757 pr_err("No AUX area event to sample\n");
758 return -EINVAL;
759 }
760
761 sz = strtoul(str, &endptr, 0);
762 if (*endptr || sz > UINT_MAX) {
763 pr_err("Bad AUX area sampling option: '%s'\n", str);
764 return -EINVAL;
765 }
766
767 if (!sz)
768 sz = itr->default_aux_sample_size;
769
770 if (!sz)
771 sz = DEFAULT_AUX_SAMPLE_SIZE;
772
773 /* Set aux_sample_size based on --aux-sample option */
774 evlist__for_each_entry(evlist, evsel) {
775 if (evsel__is_group_leader(evsel)) {
776 has_aux_leader = evsel__is_aux_event(evsel);
777 } else if (has_aux_leader) {
778 evsel->core.attr.aux_sample_size = sz;
779 }
780 }
781no_opt:
782 aux_evsel = NULL;
783 /* Override with aux_sample_size from config term */
784 evlist__for_each_entry(evlist, evsel) {
785 if (evsel__is_aux_event(evsel))
786 aux_evsel = evsel;
787 term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
788 if (term) {
789 has_aux_sample_size = true;
790 evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
791 /* If possible, group with the AUX event */
792 if (aux_evsel && evsel->core.attr.aux_sample_size)
793 evlist__regroup(evlist, aux_evsel, evsel);
794 }
795 }
796
797 if (!str && !has_aux_sample_size)
798 return 0;
799
800 if (!itr) {
801 pr_err("No AUX area event to sample\n");
802 return -EINVAL;
803 }
804
805 return auxtrace_validate_aux_sample_size(evlist, opts);
806}
807
808void auxtrace_regroup_aux_output(struct evlist *evlist)
809{
810 struct evsel *evsel, *aux_evsel = NULL;
811 struct evsel_config_term *term;
812
813 evlist__for_each_entry(evlist, evsel) {
814 if (evsel__is_aux_event(evsel))
815 aux_evsel = evsel;
816 term = evsel__get_config_term(evsel, AUX_OUTPUT);
817 /* If possible, group with the AUX event */
818 if (term && aux_evsel)
819 evlist__regroup(evlist, aux_evsel, evsel);
820 }
821}
822
823struct auxtrace_record *__weak
824auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
825{
826 *err = 0;
827 return NULL;
828}
829
830static int auxtrace_index__alloc(struct list_head *head)
831{
832 struct auxtrace_index *auxtrace_index;
833
834 auxtrace_index = malloc(sizeof(struct auxtrace_index));
835 if (!auxtrace_index)
836 return -ENOMEM;
837
838 auxtrace_index->nr = 0;
839 INIT_LIST_HEAD(&auxtrace_index->list);
840
841 list_add_tail(&auxtrace_index->list, head);
842
843 return 0;
844}
845
846void auxtrace_index__free(struct list_head *head)
847{
848 struct auxtrace_index *auxtrace_index, *n;
849
850 list_for_each_entry_safe(auxtrace_index, n, head, list) {
851 list_del_init(&auxtrace_index->list);
852 free(auxtrace_index);
853 }
854}
855
856static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
857{
858 struct auxtrace_index *auxtrace_index;
859 int err;
860
861 if (list_empty(head)) {
862 err = auxtrace_index__alloc(head);
863 if (err)
864 return NULL;
865 }
866
867 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
868
869 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
870 err = auxtrace_index__alloc(head);
871 if (err)
872 return NULL;
873 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
874 list);
875 }
876
877 return auxtrace_index;
878}
879
880int auxtrace_index__auxtrace_event(struct list_head *head,
881 union perf_event *event, off_t file_offset)
882{
883 struct auxtrace_index *auxtrace_index;
884 size_t nr;
885
886 auxtrace_index = auxtrace_index__last(head);
887 if (!auxtrace_index)
888 return -ENOMEM;
889
890 nr = auxtrace_index->nr;
891 auxtrace_index->entries[nr].file_offset = file_offset;
892 auxtrace_index->entries[nr].sz = event->header.size;
893 auxtrace_index->nr += 1;
894
895 return 0;
896}
897
898static int auxtrace_index__do_write(int fd,
899 struct auxtrace_index *auxtrace_index)
900{
901 struct auxtrace_index_entry ent;
902 size_t i;
903
904 for (i = 0; i < auxtrace_index->nr; i++) {
905 ent.file_offset = auxtrace_index->entries[i].file_offset;
906 ent.sz = auxtrace_index->entries[i].sz;
907 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
908 return -errno;
909 }
910 return 0;
911}
912
913int auxtrace_index__write(int fd, struct list_head *head)
914{
915 struct auxtrace_index *auxtrace_index;
916 u64 total = 0;
917 int err;
918
919 list_for_each_entry(auxtrace_index, head, list)
920 total += auxtrace_index->nr;
921
922 if (writen(fd, &total, sizeof(total)) != sizeof(total))
923 return -errno;
924
925 list_for_each_entry(auxtrace_index, head, list) {
926 err = auxtrace_index__do_write(fd, auxtrace_index);
927 if (err)
928 return err;
929 }
930
931 return 0;
932}
933
934static int auxtrace_index__process_entry(int fd, struct list_head *head,
935 bool needs_swap)
936{
937 struct auxtrace_index *auxtrace_index;
938 struct auxtrace_index_entry ent;
939 size_t nr;
940
941 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
942 return -1;
943
944 auxtrace_index = auxtrace_index__last(head);
945 if (!auxtrace_index)
946 return -1;
947
948 nr = auxtrace_index->nr;
949 if (needs_swap) {
950 auxtrace_index->entries[nr].file_offset =
951 bswap_64(ent.file_offset);
952 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
953 } else {
954 auxtrace_index->entries[nr].file_offset = ent.file_offset;
955 auxtrace_index->entries[nr].sz = ent.sz;
956 }
957
958 auxtrace_index->nr = nr + 1;
959
960 return 0;
961}
962
963int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
964 bool needs_swap)
965{
966 struct list_head *head = &session->auxtrace_index;
967 u64 nr;
968
969 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
970 return -1;
971
972 if (needs_swap)
973 nr = bswap_64(nr);
974
975 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
976 return -1;
977
978 while (nr--) {
979 int err;
980
981 err = auxtrace_index__process_entry(fd, head, needs_swap);
982 if (err)
983 return -1;
984 }
985
986 return 0;
987}
988
989static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
990 struct perf_session *session,
991 struct auxtrace_index_entry *ent)
992{
993 return auxtrace_queues__add_indexed_event(queues, session,
994 ent->file_offset, ent->sz);
995}
996
997int auxtrace_queues__process_index(struct auxtrace_queues *queues,
998 struct perf_session *session)
999{
1000 struct auxtrace_index *auxtrace_index;
1001 struct auxtrace_index_entry *ent;
1002 size_t i;
1003 int err;
1004
1005 if (auxtrace__dont_decode(session))
1006 return 0;
1007
1008 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
1009 for (i = 0; i < auxtrace_index->nr; i++) {
1010 ent = &auxtrace_index->entries[i];
1011 err = auxtrace_queues__process_index_entry(queues,
1012 session,
1013 ent);
1014 if (err)
1015 return err;
1016 }
1017 }
1018 return 0;
1019}
1020
1021struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
1022 struct auxtrace_buffer *buffer)
1023{
1024 if (buffer) {
1025 if (list_is_last(&buffer->list, &queue->head))
1026 return NULL;
1027 return list_entry(buffer->list.next, struct auxtrace_buffer,
1028 list);
1029 } else {
1030 if (list_empty(&queue->head))
1031 return NULL;
1032 return list_entry(queue->head.next, struct auxtrace_buffer,
1033 list);
1034 }
1035}
1036
1037struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
1038 struct perf_sample *sample,
1039 struct perf_session *session)
1040{
1041 struct perf_sample_id *sid;
1042 unsigned int idx;
1043 u64 id;
1044
1045 id = sample->id;
1046 if (!id)
1047 return NULL;
1048
1049 sid = evlist__id2sid(session->evlist, id);
1050 if (!sid)
1051 return NULL;
1052
1053 idx = sid->idx;
1054
1055 if (idx >= queues->nr_queues)
1056 return NULL;
1057
1058 return &queues->queue_array[idx];
1059}
1060
1061int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
1062 struct perf_session *session,
1063 struct perf_sample *sample, u64 data_offset,
1064 u64 reference)
1065{
1066 struct auxtrace_buffer buffer = {
1067 .pid = -1,
1068 .data_offset = data_offset,
1069 .reference = reference,
1070 .size = sample->aux_sample.size,
1071 };
1072 struct perf_sample_id *sid;
1073 u64 id = sample->id;
1074 unsigned int idx;
1075
1076 if (!id)
1077 return -EINVAL;
1078
1079 sid = evlist__id2sid(session->evlist, id);
1080 if (!sid)
1081 return -ENOENT;
1082
1083 idx = sid->idx;
1084 buffer.tid = sid->tid;
1085 buffer.cpu = sid->cpu;
1086
1087 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
1088}
1089
1090struct queue_data {
1091 bool samples;
1092 bool events;
1093};
1094
1095static int auxtrace_queue_data_cb(struct perf_session *session,
1096 union perf_event *event, u64 offset,
1097 void *data)
1098{
1099 struct queue_data *qd = data;
1100 struct perf_sample sample;
1101 int err;
1102
1103 if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
1104 if (event->header.size < sizeof(struct perf_record_auxtrace))
1105 return -EINVAL;
1106 offset += event->header.size;
1107 return session->auxtrace->queue_data(session, NULL, event,
1108 offset);
1109 }
1110
1111 if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
1112 return 0;
1113
1114 err = evlist__parse_sample(session->evlist, event, &sample);
1115 if (err)
1116 return err;
1117
1118 if (!sample.aux_sample.size)
1119 return 0;
1120
1121 offset += sample.aux_sample.data - (void *)event;
1122
1123 return session->auxtrace->queue_data(session, &sample, NULL, offset);
1124}
1125
1126int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
1127{
1128 struct queue_data qd = {
1129 .samples = samples,
1130 .events = events,
1131 };
1132
1133 if (auxtrace__dont_decode(session))
1134 return 0;
1135
1136 if (perf_data__is_pipe(session->data))
1137 return 0;
1138
1139 if (!session->auxtrace || !session->auxtrace->queue_data)
1140 return -EINVAL;
1141
1142 return perf_session__peek_events(session, session->header.data_offset,
1143 session->header.data_size,
1144 auxtrace_queue_data_cb, &qd);
1145}
1146
1147void *auxtrace_buffer__get_data_rw(struct auxtrace_buffer *buffer, int fd, bool rw)
1148{
1149 int prot = rw ? PROT_READ | PROT_WRITE : PROT_READ;
1150 size_t adj = buffer->data_offset & (page_size - 1);
1151 size_t size = buffer->size + adj;
1152 off_t file_offset = buffer->data_offset - adj;
1153 void *addr;
1154
1155 if (buffer->data)
1156 return buffer->data;
1157
1158 addr = mmap(NULL, size, prot, MAP_SHARED, fd, file_offset);
1159 if (addr == MAP_FAILED)
1160 return NULL;
1161
1162 buffer->mmap_addr = addr;
1163 buffer->mmap_size = size;
1164
1165 buffer->data = addr + adj;
1166
1167 return buffer->data;
1168}
1169
1170void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
1171{
1172 if (!buffer->data || !buffer->mmap_addr)
1173 return;
1174 munmap(buffer->mmap_addr, buffer->mmap_size);
1175 buffer->mmap_addr = NULL;
1176 buffer->mmap_size = 0;
1177 buffer->data = NULL;
1178 buffer->use_data = NULL;
1179}
1180
1181void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
1182{
1183 auxtrace_buffer__put_data(buffer);
1184 if (buffer->data_needs_freeing) {
1185 buffer->data_needs_freeing = false;
1186 zfree(&buffer->data);
1187 buffer->use_data = NULL;
1188 buffer->size = 0;
1189 }
1190}
1191
1192void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
1193{
1194 auxtrace_buffer__drop_data(buffer);
1195 free(buffer);
1196}
1197
1198void auxtrace_synth_guest_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1199 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1200 const char *msg, u64 timestamp,
1201 pid_t machine_pid, int vcpu)
1202{
1203 size_t size;
1204
1205 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
1206
1207 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
1208 auxtrace_error->type = type;
1209 auxtrace_error->code = code;
1210 auxtrace_error->cpu = cpu;
1211 auxtrace_error->pid = pid;
1212 auxtrace_error->tid = tid;
1213 auxtrace_error->fmt = 1;
1214 auxtrace_error->ip = ip;
1215 auxtrace_error->time = timestamp;
1216 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
1217 if (machine_pid) {
1218 auxtrace_error->fmt = 2;
1219 auxtrace_error->machine_pid = machine_pid;
1220 auxtrace_error->vcpu = vcpu;
1221 size = sizeof(*auxtrace_error);
1222 } else {
1223 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
1224 strlen(auxtrace_error->msg) + 1;
1225 }
1226 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
1227}
1228
1229void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1230 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1231 const char *msg, u64 timestamp)
1232{
1233 auxtrace_synth_guest_error(auxtrace_error, type, code, cpu, pid, tid,
1234 ip, msg, timestamp, 0, -1);
1235}
1236
1237int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
1238 struct perf_tool *tool,
1239 struct perf_session *session,
1240 perf_event__handler_t process)
1241{
1242 union perf_event *ev;
1243 size_t priv_size;
1244 int err;
1245
1246 pr_debug2("Synthesizing auxtrace information\n");
1247 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
1248 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
1249 if (!ev)
1250 return -ENOMEM;
1251
1252 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
1253 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
1254 priv_size;
1255 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
1256 priv_size);
1257 if (err)
1258 goto out_free;
1259
1260 err = process(tool, ev, NULL, NULL);
1261out_free:
1262 free(ev);
1263 return err;
1264}
1265
1266static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
1267{
1268 struct evsel *new_leader = NULL;
1269 struct evsel *evsel;
1270
1271 /* Find new leader for the group */
1272 evlist__for_each_entry(evlist, evsel) {
1273 if (!evsel__has_leader(evsel, leader) || evsel == leader)
1274 continue;
1275 if (!new_leader)
1276 new_leader = evsel;
1277 evsel__set_leader(evsel, new_leader);
1278 }
1279
1280 /* Update group information */
1281 if (new_leader) {
1282 zfree(&new_leader->group_name);
1283 new_leader->group_name = leader->group_name;
1284 leader->group_name = NULL;
1285
1286 new_leader->core.nr_members = leader->core.nr_members - 1;
1287 leader->core.nr_members = 1;
1288 }
1289}
1290
1291static void unleader_auxtrace(struct perf_session *session)
1292{
1293 struct evsel *evsel;
1294
1295 evlist__for_each_entry(session->evlist, evsel) {
1296 if (auxtrace__evsel_is_auxtrace(session, evsel) &&
1297 evsel__is_group_leader(evsel)) {
1298 unleader_evsel(session->evlist, evsel);
1299 }
1300 }
1301}
1302
1303int perf_event__process_auxtrace_info(struct perf_session *session,
1304 union perf_event *event)
1305{
1306 enum auxtrace_type type = event->auxtrace_info.type;
1307 int err;
1308
1309 if (dump_trace)
1310 fprintf(stdout, " type: %u\n", type);
1311
1312 switch (type) {
1313 case PERF_AUXTRACE_INTEL_PT:
1314 err = intel_pt_process_auxtrace_info(event, session);
1315 break;
1316 case PERF_AUXTRACE_INTEL_BTS:
1317 err = intel_bts_process_auxtrace_info(event, session);
1318 break;
1319 case PERF_AUXTRACE_ARM_SPE:
1320 err = arm_spe_process_auxtrace_info(event, session);
1321 break;
1322 case PERF_AUXTRACE_CS_ETM:
1323 err = cs_etm__process_auxtrace_info(event, session);
1324 break;
1325 case PERF_AUXTRACE_S390_CPUMSF:
1326 err = s390_cpumsf_process_auxtrace_info(event, session);
1327 break;
1328 case PERF_AUXTRACE_HISI_PTT:
1329 err = hisi_ptt_process_auxtrace_info(event, session);
1330 break;
1331 case PERF_AUXTRACE_UNKNOWN:
1332 default:
1333 return -EINVAL;
1334 }
1335
1336 if (err)
1337 return err;
1338
1339 unleader_auxtrace(session);
1340
1341 return 0;
1342}
1343
1344s64 perf_event__process_auxtrace(struct perf_session *session,
1345 union perf_event *event)
1346{
1347 s64 err;
1348
1349 if (dump_trace)
1350 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n",
1351 event->auxtrace.size, event->auxtrace.offset,
1352 event->auxtrace.reference, event->auxtrace.idx,
1353 event->auxtrace.tid, event->auxtrace.cpu);
1354
1355 if (auxtrace__dont_decode(session))
1356 return event->auxtrace.size;
1357
1358 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
1359 return -EINVAL;
1360
1361 err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
1362 if (err < 0)
1363 return err;
1364
1365 return event->auxtrace.size;
1366}
1367
1368#define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
1369#define PERF_ITRACE_DEFAULT_PERIOD 100000
1370#define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
1371#define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
1372#define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
1373#define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
1374
1375void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
1376 bool no_sample)
1377{
1378 synth_opts->branches = true;
1379 synth_opts->transactions = true;
1380 synth_opts->ptwrites = true;
1381 synth_opts->pwr_events = true;
1382 synth_opts->other_events = true;
1383 synth_opts->intr_events = true;
1384 synth_opts->errors = true;
1385 synth_opts->flc = true;
1386 synth_opts->llc = true;
1387 synth_opts->tlb = true;
1388 synth_opts->mem = true;
1389 synth_opts->remote_access = true;
1390
1391 if (no_sample) {
1392 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
1393 synth_opts->period = 1;
1394 synth_opts->calls = true;
1395 } else {
1396 synth_opts->instructions = true;
1397 synth_opts->cycles = true;
1398 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1399 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1400 }
1401 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1402 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1403 synth_opts->initial_skip = 0;
1404}
1405
1406static int get_flag(const char **ptr, unsigned int *flags)
1407{
1408 while (1) {
1409 char c = **ptr;
1410
1411 if (c >= 'a' && c <= 'z') {
1412 *flags |= 1 << (c - 'a');
1413 ++*ptr;
1414 return 0;
1415 } else if (c == ' ') {
1416 ++*ptr;
1417 continue;
1418 } else {
1419 return -1;
1420 }
1421 }
1422}
1423
1424static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
1425{
1426 while (1) {
1427 switch (**ptr) {
1428 case '+':
1429 ++*ptr;
1430 if (get_flag(ptr, plus_flags))
1431 return -1;
1432 break;
1433 case '-':
1434 ++*ptr;
1435 if (get_flag(ptr, minus_flags))
1436 return -1;
1437 break;
1438 case ' ':
1439 ++*ptr;
1440 break;
1441 default:
1442 return 0;
1443 }
1444 }
1445}
1446
1447#define ITRACE_DFLT_LOG_ON_ERROR_SZ 16384
1448
1449static unsigned int itrace_log_on_error_size(void)
1450{
1451 unsigned int sz = 0;
1452
1453 perf_config_scan("itrace.debug-log-buffer-size", "%u", &sz);
1454 return sz ?: ITRACE_DFLT_LOG_ON_ERROR_SZ;
1455}
1456
1457/*
1458 * Please check tools/perf/Documentation/perf-script.txt for information
1459 * about the options parsed here, which is introduced after this cset,
1460 * when support in 'perf script' for these options is introduced.
1461 */
1462int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
1463 const char *str, int unset)
1464{
1465 const char *p;
1466 char *endptr;
1467 bool period_type_set = false;
1468 bool period_set = false;
1469
1470 synth_opts->set = true;
1471
1472 if (unset) {
1473 synth_opts->dont_decode = true;
1474 return 0;
1475 }
1476
1477 if (!str) {
1478 itrace_synth_opts__set_default(synth_opts,
1479 synth_opts->default_no_sample);
1480 return 0;
1481 }
1482
1483 for (p = str; *p;) {
1484 switch (*p++) {
1485 case 'i':
1486 case 'y':
1487 if (p[-1] == 'y')
1488 synth_opts->cycles = true;
1489 else
1490 synth_opts->instructions = true;
1491 while (*p == ' ' || *p == ',')
1492 p += 1;
1493 if (isdigit(*p)) {
1494 synth_opts->period = strtoull(p, &endptr, 10);
1495 period_set = true;
1496 p = endptr;
1497 while (*p == ' ' || *p == ',')
1498 p += 1;
1499 switch (*p++) {
1500 case 'i':
1501 synth_opts->period_type =
1502 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1503 period_type_set = true;
1504 break;
1505 case 't':
1506 synth_opts->period_type =
1507 PERF_ITRACE_PERIOD_TICKS;
1508 period_type_set = true;
1509 break;
1510 case 'm':
1511 synth_opts->period *= 1000;
1512 /* Fall through */
1513 case 'u':
1514 synth_opts->period *= 1000;
1515 /* Fall through */
1516 case 'n':
1517 if (*p++ != 's')
1518 goto out_err;
1519 synth_opts->period_type =
1520 PERF_ITRACE_PERIOD_NANOSECS;
1521 period_type_set = true;
1522 break;
1523 case '\0':
1524 goto out;
1525 default:
1526 goto out_err;
1527 }
1528 }
1529 break;
1530 case 'b':
1531 synth_opts->branches = true;
1532 break;
1533 case 'x':
1534 synth_opts->transactions = true;
1535 break;
1536 case 'w':
1537 synth_opts->ptwrites = true;
1538 break;
1539 case 'p':
1540 synth_opts->pwr_events = true;
1541 break;
1542 case 'o':
1543 synth_opts->other_events = true;
1544 break;
1545 case 'I':
1546 synth_opts->intr_events = true;
1547 break;
1548 case 'e':
1549 synth_opts->errors = true;
1550 if (get_flags(&p, &synth_opts->error_plus_flags,
1551 &synth_opts->error_minus_flags))
1552 goto out_err;
1553 break;
1554 case 'd':
1555 synth_opts->log = true;
1556 if (get_flags(&p, &synth_opts->log_plus_flags,
1557 &synth_opts->log_minus_flags))
1558 goto out_err;
1559 if (synth_opts->log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR)
1560 synth_opts->log_on_error_size = itrace_log_on_error_size();
1561 break;
1562 case 'c':
1563 synth_opts->branches = true;
1564 synth_opts->calls = true;
1565 break;
1566 case 'r':
1567 synth_opts->branches = true;
1568 synth_opts->returns = true;
1569 break;
1570 case 'G':
1571 case 'g':
1572 if (p[-1] == 'G')
1573 synth_opts->add_callchain = true;
1574 else
1575 synth_opts->callchain = true;
1576 synth_opts->callchain_sz =
1577 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1578 while (*p == ' ' || *p == ',')
1579 p += 1;
1580 if (isdigit(*p)) {
1581 unsigned int val;
1582
1583 val = strtoul(p, &endptr, 10);
1584 p = endptr;
1585 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1586 goto out_err;
1587 synth_opts->callchain_sz = val;
1588 }
1589 break;
1590 case 'L':
1591 case 'l':
1592 if (p[-1] == 'L')
1593 synth_opts->add_last_branch = true;
1594 else
1595 synth_opts->last_branch = true;
1596 synth_opts->last_branch_sz =
1597 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1598 while (*p == ' ' || *p == ',')
1599 p += 1;
1600 if (isdigit(*p)) {
1601 unsigned int val;
1602
1603 val = strtoul(p, &endptr, 10);
1604 p = endptr;
1605 if (!val ||
1606 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1607 goto out_err;
1608 synth_opts->last_branch_sz = val;
1609 }
1610 break;
1611 case 's':
1612 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1613 if (p == endptr)
1614 goto out_err;
1615 p = endptr;
1616 break;
1617 case 'f':
1618 synth_opts->flc = true;
1619 break;
1620 case 'm':
1621 synth_opts->llc = true;
1622 break;
1623 case 't':
1624 synth_opts->tlb = true;
1625 break;
1626 case 'a':
1627 synth_opts->remote_access = true;
1628 break;
1629 case 'M':
1630 synth_opts->mem = true;
1631 break;
1632 case 'q':
1633 synth_opts->quick += 1;
1634 break;
1635 case 'A':
1636 synth_opts->approx_ipc = true;
1637 break;
1638 case 'Z':
1639 synth_opts->timeless_decoding = true;
1640 break;
1641 case 'T':
1642 synth_opts->use_timestamp = true;
1643 break;
1644 case ' ':
1645 case ',':
1646 break;
1647 default:
1648 goto out_err;
1649 }
1650 }
1651out:
1652 if (synth_opts->instructions || synth_opts->cycles) {
1653 if (!period_type_set)
1654 synth_opts->period_type =
1655 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1656 if (!period_set)
1657 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1658 }
1659
1660 return 0;
1661
1662out_err:
1663 pr_err("Bad Instruction Tracing options '%s'\n", str);
1664 return -EINVAL;
1665}
1666
1667int itrace_parse_synth_opts(const struct option *opt, const char *str, int unset)
1668{
1669 return itrace_do_parse_synth_opts(opt->value, str, unset);
1670}
1671
1672static const char * const auxtrace_error_type_name[] = {
1673 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1674};
1675
1676static const char *auxtrace_error_name(int type)
1677{
1678 const char *error_type_name = NULL;
1679
1680 if (type < PERF_AUXTRACE_ERROR_MAX)
1681 error_type_name = auxtrace_error_type_name[type];
1682 if (!error_type_name)
1683 error_type_name = "unknown AUX";
1684 return error_type_name;
1685}
1686
1687size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1688{
1689 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1690 unsigned long long nsecs = e->time;
1691 const char *msg = e->msg;
1692 int ret;
1693
1694 ret = fprintf(fp, " %s error type %u",
1695 auxtrace_error_name(e->type), e->type);
1696
1697 if (e->fmt && nsecs) {
1698 unsigned long secs = nsecs / NSEC_PER_SEC;
1699
1700 nsecs -= secs * NSEC_PER_SEC;
1701 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1702 } else {
1703 ret += fprintf(fp, " time 0");
1704 }
1705
1706 if (!e->fmt)
1707 msg = (const char *)&e->time;
1708
1709 if (e->fmt >= 2 && e->machine_pid)
1710 ret += fprintf(fp, " machine_pid %d vcpu %d", e->machine_pid, e->vcpu);
1711
1712 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
1713 e->cpu, e->pid, e->tid, e->ip, e->code, msg);
1714 return ret;
1715}
1716
1717void perf_session__auxtrace_error_inc(struct perf_session *session,
1718 union perf_event *event)
1719{
1720 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1721
1722 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1723 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1724}
1725
1726void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1727{
1728 int i;
1729
1730 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1731 if (!stats->nr_auxtrace_errors[i])
1732 continue;
1733 ui__warning("%u %s errors\n",
1734 stats->nr_auxtrace_errors[i],
1735 auxtrace_error_name(i));
1736 }
1737}
1738
1739int perf_event__process_auxtrace_error(struct perf_session *session,
1740 union perf_event *event)
1741{
1742 if (auxtrace__dont_decode(session))
1743 return 0;
1744
1745 perf_event__fprintf_auxtrace_error(event, stdout);
1746 return 0;
1747}
1748
1749/*
1750 * In the compat mode kernel runs in 64-bit and perf tool runs in 32-bit mode,
1751 * 32-bit perf tool cannot access 64-bit value atomically, which might lead to
1752 * the issues caused by the below sequence on multiple CPUs: when perf tool
1753 * accesses either the load operation or the store operation for 64-bit value,
1754 * on some architectures the operation is divided into two instructions, one
1755 * is for accessing the low 32-bit value and another is for the high 32-bit;
1756 * thus these two user operations can give the kernel chances to access the
1757 * 64-bit value, and thus leads to the unexpected load values.
1758 *
1759 * kernel (64-bit) user (32-bit)
1760 *
1761 * if (LOAD ->aux_tail) { --, LOAD ->aux_head_lo
1762 * STORE $aux_data | ,--->
1763 * FLUSH $aux_data | | LOAD ->aux_head_hi
1764 * STORE ->aux_head --|-------` smp_rmb()
1765 * } | LOAD $data
1766 * | smp_mb()
1767 * | STORE ->aux_tail_lo
1768 * `----------->
1769 * STORE ->aux_tail_hi
1770 *
1771 * For this reason, it's impossible for the perf tool to work correctly when
1772 * the AUX head or tail is bigger than 4GB (more than 32 bits length); and we
1773 * can not simply limit the AUX ring buffer to less than 4GB, the reason is
1774 * the pointers can be increased monotonically, whatever the buffer size it is,
1775 * at the end the head and tail can be bigger than 4GB and carry out to the
1776 * high 32-bit.
1777 *
1778 * To mitigate the issues and improve the user experience, we can allow the
1779 * perf tool working in certain conditions and bail out with error if detect
1780 * any overflow cannot be handled.
1781 *
1782 * For reading the AUX head, it reads out the values for three times, and
1783 * compares the high 4 bytes of the values between the first time and the last
1784 * time, if there has no change for high 4 bytes injected by the kernel during
1785 * the user reading sequence, it's safe for use the second value.
1786 *
1787 * When compat_auxtrace_mmap__write_tail() detects any carrying in the high
1788 * 32 bits, it means there have two store operations in user space and it cannot
1789 * promise the atomicity for 64-bit write, so return '-1' in this case to tell
1790 * the caller an overflow error has happened.
1791 */
1792u64 __weak compat_auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
1793{
1794 struct perf_event_mmap_page *pc = mm->userpg;
1795 u64 first, second, last;
1796 u64 mask = (u64)(UINT32_MAX) << 32;
1797
1798 do {
1799 first = READ_ONCE(pc->aux_head);
1800 /* Ensure all reads are done after we read the head */
1801 smp_rmb();
1802 second = READ_ONCE(pc->aux_head);
1803 /* Ensure all reads are done after we read the head */
1804 smp_rmb();
1805 last = READ_ONCE(pc->aux_head);
1806 } while ((first & mask) != (last & mask));
1807
1808 return second;
1809}
1810
1811int __weak compat_auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
1812{
1813 struct perf_event_mmap_page *pc = mm->userpg;
1814 u64 mask = (u64)(UINT32_MAX) << 32;
1815
1816 if (tail & mask)
1817 return -1;
1818
1819 /* Ensure all reads are done before we write the tail out */
1820 smp_mb();
1821 WRITE_ONCE(pc->aux_tail, tail);
1822 return 0;
1823}
1824
1825static int __auxtrace_mmap__read(struct mmap *map,
1826 struct auxtrace_record *itr,
1827 struct perf_tool *tool, process_auxtrace_t fn,
1828 bool snapshot, size_t snapshot_size)
1829{
1830 struct auxtrace_mmap *mm = &map->auxtrace_mmap;
1831 u64 head, old = mm->prev, offset, ref;
1832 unsigned char *data = mm->base;
1833 size_t size, head_off, old_off, len1, len2, padding;
1834 union perf_event ev;
1835 void *data1, *data2;
1836 int kernel_is_64_bit = perf_env__kernel_is_64_bit(evsel__env(NULL));
1837
1838 head = auxtrace_mmap__read_head(mm, kernel_is_64_bit);
1839
1840 if (snapshot &&
1841 auxtrace_record__find_snapshot(itr, mm->idx, mm, data, &head, &old))
1842 return -1;
1843
1844 if (old == head)
1845 return 0;
1846
1847 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1848 mm->idx, old, head, head - old);
1849
1850 if (mm->mask) {
1851 head_off = head & mm->mask;
1852 old_off = old & mm->mask;
1853 } else {
1854 head_off = head % mm->len;
1855 old_off = old % mm->len;
1856 }
1857
1858 if (head_off > old_off)
1859 size = head_off - old_off;
1860 else
1861 size = mm->len - (old_off - head_off);
1862
1863 if (snapshot && size > snapshot_size)
1864 size = snapshot_size;
1865
1866 ref = auxtrace_record__reference(itr);
1867
1868 if (head > old || size <= head || mm->mask) {
1869 offset = head - size;
1870 } else {
1871 /*
1872 * When the buffer size is not a power of 2, 'head' wraps at the
1873 * highest multiple of the buffer size, so we have to subtract
1874 * the remainder here.
1875 */
1876 u64 rem = (0ULL - mm->len) % mm->len;
1877
1878 offset = head - size - rem;
1879 }
1880
1881 if (size > head_off) {
1882 len1 = size - head_off;
1883 data1 = &data[mm->len - len1];
1884 len2 = head_off;
1885 data2 = &data[0];
1886 } else {
1887 len1 = size;
1888 data1 = &data[head_off - len1];
1889 len2 = 0;
1890 data2 = NULL;
1891 }
1892
1893 if (itr->alignment) {
1894 unsigned int unwanted = len1 % itr->alignment;
1895
1896 len1 -= unwanted;
1897 size -= unwanted;
1898 }
1899
1900 /* padding must be written by fn() e.g. record__process_auxtrace() */
1901 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1902 if (padding)
1903 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1904
1905 memset(&ev, 0, sizeof(ev));
1906 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1907 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1908 ev.auxtrace.size = size + padding;
1909 ev.auxtrace.offset = offset;
1910 ev.auxtrace.reference = ref;
1911 ev.auxtrace.idx = mm->idx;
1912 ev.auxtrace.tid = mm->tid;
1913 ev.auxtrace.cpu = mm->cpu;
1914
1915 if (fn(tool, map, &ev, data1, len1, data2, len2))
1916 return -1;
1917
1918 mm->prev = head;
1919
1920 if (!snapshot) {
1921 int err;
1922
1923 err = auxtrace_mmap__write_tail(mm, head, kernel_is_64_bit);
1924 if (err < 0)
1925 return err;
1926
1927 if (itr->read_finish) {
1928 err = itr->read_finish(itr, mm->idx);
1929 if (err < 0)
1930 return err;
1931 }
1932 }
1933
1934 return 1;
1935}
1936
1937int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
1938 struct perf_tool *tool, process_auxtrace_t fn)
1939{
1940 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
1941}
1942
1943int auxtrace_mmap__read_snapshot(struct mmap *map,
1944 struct auxtrace_record *itr,
1945 struct perf_tool *tool, process_auxtrace_t fn,
1946 size_t snapshot_size)
1947{
1948 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
1949}
1950
1951/**
1952 * struct auxtrace_cache - hash table to implement a cache
1953 * @hashtable: the hashtable
1954 * @sz: hashtable size (number of hlists)
1955 * @entry_size: size of an entry
1956 * @limit: limit the number of entries to this maximum, when reached the cache
1957 * is dropped and caching begins again with an empty cache
1958 * @cnt: current number of entries
1959 * @bits: hashtable size (@sz = 2^@bits)
1960 */
1961struct auxtrace_cache {
1962 struct hlist_head *hashtable;
1963 size_t sz;
1964 size_t entry_size;
1965 size_t limit;
1966 size_t cnt;
1967 unsigned int bits;
1968};
1969
1970struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1971 unsigned int limit_percent)
1972{
1973 struct auxtrace_cache *c;
1974 struct hlist_head *ht;
1975 size_t sz, i;
1976
1977 c = zalloc(sizeof(struct auxtrace_cache));
1978 if (!c)
1979 return NULL;
1980
1981 sz = 1UL << bits;
1982
1983 ht = calloc(sz, sizeof(struct hlist_head));
1984 if (!ht)
1985 goto out_free;
1986
1987 for (i = 0; i < sz; i++)
1988 INIT_HLIST_HEAD(&ht[i]);
1989
1990 c->hashtable = ht;
1991 c->sz = sz;
1992 c->entry_size = entry_size;
1993 c->limit = (c->sz * limit_percent) / 100;
1994 c->bits = bits;
1995
1996 return c;
1997
1998out_free:
1999 free(c);
2000 return NULL;
2001}
2002
2003static void auxtrace_cache__drop(struct auxtrace_cache *c)
2004{
2005 struct auxtrace_cache_entry *entry;
2006 struct hlist_node *tmp;
2007 size_t i;
2008
2009 if (!c)
2010 return;
2011
2012 for (i = 0; i < c->sz; i++) {
2013 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
2014 hlist_del(&entry->hash);
2015 auxtrace_cache__free_entry(c, entry);
2016 }
2017 }
2018
2019 c->cnt = 0;
2020}
2021
2022void auxtrace_cache__free(struct auxtrace_cache *c)
2023{
2024 if (!c)
2025 return;
2026
2027 auxtrace_cache__drop(c);
2028 zfree(&c->hashtable);
2029 free(c);
2030}
2031
2032void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
2033{
2034 return malloc(c->entry_size);
2035}
2036
2037void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
2038 void *entry)
2039{
2040 free(entry);
2041}
2042
2043int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
2044 struct auxtrace_cache_entry *entry)
2045{
2046 if (c->limit && ++c->cnt > c->limit)
2047 auxtrace_cache__drop(c);
2048
2049 entry->key = key;
2050 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
2051
2052 return 0;
2053}
2054
2055static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
2056 u32 key)
2057{
2058 struct auxtrace_cache_entry *entry;
2059 struct hlist_head *hlist;
2060 struct hlist_node *n;
2061
2062 if (!c)
2063 return NULL;
2064
2065 hlist = &c->hashtable[hash_32(key, c->bits)];
2066 hlist_for_each_entry_safe(entry, n, hlist, hash) {
2067 if (entry->key == key) {
2068 hlist_del(&entry->hash);
2069 return entry;
2070 }
2071 }
2072
2073 return NULL;
2074}
2075
2076void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
2077{
2078 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);
2079
2080 auxtrace_cache__free_entry(c, entry);
2081}
2082
2083void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
2084{
2085 struct auxtrace_cache_entry *entry;
2086 struct hlist_head *hlist;
2087
2088 if (!c)
2089 return NULL;
2090
2091 hlist = &c->hashtable[hash_32(key, c->bits)];
2092 hlist_for_each_entry(entry, hlist, hash) {
2093 if (entry->key == key)
2094 return entry;
2095 }
2096
2097 return NULL;
2098}
2099
2100static void addr_filter__free_str(struct addr_filter *filt)
2101{
2102 zfree(&filt->str);
2103 filt->action = NULL;
2104 filt->sym_from = NULL;
2105 filt->sym_to = NULL;
2106 filt->filename = NULL;
2107}
2108
2109static struct addr_filter *addr_filter__new(void)
2110{
2111 struct addr_filter *filt = zalloc(sizeof(*filt));
2112
2113 if (filt)
2114 INIT_LIST_HEAD(&filt->list);
2115
2116 return filt;
2117}
2118
2119static void addr_filter__free(struct addr_filter *filt)
2120{
2121 if (filt)
2122 addr_filter__free_str(filt);
2123 free(filt);
2124}
2125
2126static void addr_filters__add(struct addr_filters *filts,
2127 struct addr_filter *filt)
2128{
2129 list_add_tail(&filt->list, &filts->head);
2130 filts->cnt += 1;
2131}
2132
2133static void addr_filters__del(struct addr_filters *filts,
2134 struct addr_filter *filt)
2135{
2136 list_del_init(&filt->list);
2137 filts->cnt -= 1;
2138}
2139
2140void addr_filters__init(struct addr_filters *filts)
2141{
2142 INIT_LIST_HEAD(&filts->head);
2143 filts->cnt = 0;
2144}
2145
2146void addr_filters__exit(struct addr_filters *filts)
2147{
2148 struct addr_filter *filt, *n;
2149
2150 list_for_each_entry_safe(filt, n, &filts->head, list) {
2151 addr_filters__del(filts, filt);
2152 addr_filter__free(filt);
2153 }
2154}
2155
2156static int parse_num_or_str(char **inp, u64 *num, const char **str,
2157 const char *str_delim)
2158{
2159 *inp += strspn(*inp, " ");
2160
2161 if (isdigit(**inp)) {
2162 char *endptr;
2163
2164 if (!num)
2165 return -EINVAL;
2166 errno = 0;
2167 *num = strtoull(*inp, &endptr, 0);
2168 if (errno)
2169 return -errno;
2170 if (endptr == *inp)
2171 return -EINVAL;
2172 *inp = endptr;
2173 } else {
2174 size_t n;
2175
2176 if (!str)
2177 return -EINVAL;
2178 *inp += strspn(*inp, " ");
2179 *str = *inp;
2180 n = strcspn(*inp, str_delim);
2181 if (!n)
2182 return -EINVAL;
2183 *inp += n;
2184 if (**inp) {
2185 **inp = '\0';
2186 *inp += 1;
2187 }
2188 }
2189 return 0;
2190}
2191
2192static int parse_action(struct addr_filter *filt)
2193{
2194 if (!strcmp(filt->action, "filter")) {
2195 filt->start = true;
2196 filt->range = true;
2197 } else if (!strcmp(filt->action, "start")) {
2198 filt->start = true;
2199 } else if (!strcmp(filt->action, "stop")) {
2200 filt->start = false;
2201 } else if (!strcmp(filt->action, "tracestop")) {
2202 filt->start = false;
2203 filt->range = true;
2204 filt->action += 5; /* Change 'tracestop' to 'stop' */
2205 } else {
2206 return -EINVAL;
2207 }
2208 return 0;
2209}
2210
2211static int parse_sym_idx(char **inp, int *idx)
2212{
2213 *idx = -1;
2214
2215 *inp += strspn(*inp, " ");
2216
2217 if (**inp != '#')
2218 return 0;
2219
2220 *inp += 1;
2221
2222 if (**inp == 'g' || **inp == 'G') {
2223 *inp += 1;
2224 *idx = 0;
2225 } else {
2226 unsigned long num;
2227 char *endptr;
2228
2229 errno = 0;
2230 num = strtoul(*inp, &endptr, 0);
2231 if (errno)
2232 return -errno;
2233 if (endptr == *inp || num > INT_MAX)
2234 return -EINVAL;
2235 *inp = endptr;
2236 *idx = num;
2237 }
2238
2239 return 0;
2240}
2241
2242static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
2243{
2244 int err = parse_num_or_str(inp, num, str, " ");
2245
2246 if (!err && *str)
2247 err = parse_sym_idx(inp, idx);
2248
2249 return err;
2250}
2251
2252static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
2253{
2254 char *fstr;
2255 int err;
2256
2257 filt->str = fstr = strdup(*filter_inp);
2258 if (!fstr)
2259 return -ENOMEM;
2260
2261 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
2262 if (err)
2263 goto out_err;
2264
2265 err = parse_action(filt);
2266 if (err)
2267 goto out_err;
2268
2269 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
2270 &filt->sym_from_idx);
2271 if (err)
2272 goto out_err;
2273
2274 fstr += strspn(fstr, " ");
2275
2276 if (*fstr == '/') {
2277 fstr += 1;
2278 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
2279 &filt->sym_to_idx);
2280 if (err)
2281 goto out_err;
2282 filt->range = true;
2283 }
2284
2285 fstr += strspn(fstr, " ");
2286
2287 if (*fstr == '@') {
2288 fstr += 1;
2289 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
2290 if (err)
2291 goto out_err;
2292 }
2293
2294 fstr += strspn(fstr, " ,");
2295
2296 *filter_inp += fstr - filt->str;
2297
2298 return 0;
2299
2300out_err:
2301 addr_filter__free_str(filt);
2302
2303 return err;
2304}
2305
2306int addr_filters__parse_bare_filter(struct addr_filters *filts,
2307 const char *filter)
2308{
2309 struct addr_filter *filt;
2310 const char *fstr = filter;
2311 int err;
2312
2313 while (*fstr) {
2314 filt = addr_filter__new();
2315 err = parse_one_filter(filt, &fstr);
2316 if (err) {
2317 addr_filter__free(filt);
2318 addr_filters__exit(filts);
2319 return err;
2320 }
2321 addr_filters__add(filts, filt);
2322 }
2323
2324 return 0;
2325}
2326
2327struct sym_args {
2328 const char *name;
2329 u64 start;
2330 u64 size;
2331 int idx;
2332 int cnt;
2333 bool started;
2334 bool global;
2335 bool selected;
2336 bool duplicate;
2337 bool near;
2338};
2339
2340static bool kern_sym_name_match(const char *kname, const char *name)
2341{
2342 size_t n = strlen(name);
2343
2344 return !strcmp(kname, name) ||
2345 (!strncmp(kname, name, n) && kname[n] == '\t');
2346}
2347
2348static bool kern_sym_match(struct sym_args *args, const char *name, char type)
2349{
2350 /* A function with the same name, and global or the n'th found or any */
2351 return kallsyms__is_function(type) &&
2352 kern_sym_name_match(name, args->name) &&
2353 ((args->global && isupper(type)) ||
2354 (args->selected && ++(args->cnt) == args->idx) ||
2355 (!args->global && !args->selected));
2356}
2357
2358static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2359{
2360 struct sym_args *args = arg;
2361
2362 if (args->started) {
2363 if (!args->size)
2364 args->size = start - args->start;
2365 if (args->selected) {
2366 if (args->size)
2367 return 1;
2368 } else if (kern_sym_match(args, name, type)) {
2369 args->duplicate = true;
2370 return 1;
2371 }
2372 } else if (kern_sym_match(args, name, type)) {
2373 args->started = true;
2374 args->start = start;
2375 }
2376
2377 return 0;
2378}
2379
2380static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2381{
2382 struct sym_args *args = arg;
2383
2384 if (kern_sym_match(args, name, type)) {
2385 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2386 ++args->cnt, start, type, name);
2387 args->near = true;
2388 } else if (args->near) {
2389 args->near = false;
2390 pr_err("\t\twhich is near\t\t%s\n", name);
2391 }
2392
2393 return 0;
2394}
2395
2396static int sym_not_found_error(const char *sym_name, int idx)
2397{
2398 if (idx > 0) {
2399 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
2400 idx, sym_name);
2401 } else if (!idx) {
2402 pr_err("Global symbol '%s' not found.\n", sym_name);
2403 } else {
2404 pr_err("Symbol '%s' not found.\n", sym_name);
2405 }
2406 pr_err("Note that symbols must be functions.\n");
2407
2408 return -EINVAL;
2409}
2410
2411static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
2412{
2413 struct sym_args args = {
2414 .name = sym_name,
2415 .idx = idx,
2416 .global = !idx,
2417 .selected = idx > 0,
2418 };
2419 int err;
2420
2421 *start = 0;
2422 *size = 0;
2423
2424 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
2425 if (err < 0) {
2426 pr_err("Failed to parse /proc/kallsyms\n");
2427 return err;
2428 }
2429
2430 if (args.duplicate) {
2431 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
2432 args.cnt = 0;
2433 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
2434 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2435 sym_name);
2436 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2437 return -EINVAL;
2438 }
2439
2440 if (!args.started) {
2441 pr_err("Kernel symbol lookup: ");
2442 return sym_not_found_error(sym_name, idx);
2443 }
2444
2445 *start = args.start;
2446 *size = args.size;
2447
2448 return 0;
2449}
2450
2451static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
2452 char type, u64 start)
2453{
2454 struct sym_args *args = arg;
2455 u64 size;
2456
2457 if (!kallsyms__is_function(type))
2458 return 0;
2459
2460 if (!args->started) {
2461 args->started = true;
2462 args->start = start;
2463 }
2464 /* Don't know exactly where the kernel ends, so we add a page */
2465 size = round_up(start, page_size) + page_size - args->start;
2466 if (size > args->size)
2467 args->size = size;
2468
2469 return 0;
2470}
2471
2472static int addr_filter__entire_kernel(struct addr_filter *filt)
2473{
2474 struct sym_args args = { .started = false };
2475 int err;
2476
2477 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
2478 if (err < 0 || !args.started) {
2479 pr_err("Failed to parse /proc/kallsyms\n");
2480 return err;
2481 }
2482
2483 filt->addr = args.start;
2484 filt->size = args.size;
2485
2486 return 0;
2487}
2488
2489static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
2490{
2491 if (start + size >= filt->addr)
2492 return 0;
2493
2494 if (filt->sym_from) {
2495 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
2496 filt->sym_to, start, filt->sym_from, filt->addr);
2497 } else {
2498 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
2499 filt->sym_to, start, filt->addr);
2500 }
2501
2502 return -EINVAL;
2503}
2504
2505static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
2506{
2507 bool no_size = false;
2508 u64 start, size;
2509 int err;
2510
2511 if (symbol_conf.kptr_restrict) {
2512 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
2513 return -EINVAL;
2514 }
2515
2516 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
2517 return addr_filter__entire_kernel(filt);
2518
2519 if (filt->sym_from) {
2520 err = find_kern_sym(filt->sym_from, &start, &size,
2521 filt->sym_from_idx);
2522 if (err)
2523 return err;
2524 filt->addr = start;
2525 if (filt->range && !filt->size && !filt->sym_to) {
2526 filt->size = size;
2527 no_size = !size;
2528 }
2529 }
2530
2531 if (filt->sym_to) {
2532 err = find_kern_sym(filt->sym_to, &start, &size,
2533 filt->sym_to_idx);
2534 if (err)
2535 return err;
2536
2537 err = check_end_after_start(filt, start, size);
2538 if (err)
2539 return err;
2540 filt->size = start + size - filt->addr;
2541 no_size = !size;
2542 }
2543
2544 /* The very last symbol in kallsyms does not imply a particular size */
2545 if (no_size) {
2546 pr_err("Cannot determine size of symbol '%s'\n",
2547 filt->sym_to ? filt->sym_to : filt->sym_from);
2548 return -EINVAL;
2549 }
2550
2551 return 0;
2552}
2553
2554static struct dso *load_dso(const char *name)
2555{
2556 struct map *map;
2557 struct dso *dso;
2558
2559 map = dso__new_map(name);
2560 if (!map)
2561 return NULL;
2562
2563 if (map__load(map) < 0)
2564 pr_err("File '%s' not found or has no symbols.\n", name);
2565
2566 dso = dso__get(map__dso(map));
2567
2568 map__put(map);
2569
2570 return dso;
2571}
2572
2573static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
2574 int idx)
2575{
2576 /* Same name, and global or the n'th found or any */
2577 return !arch__compare_symbol_names(name, sym->name) &&
2578 ((!idx && sym->binding == STB_GLOBAL) ||
2579 (idx > 0 && ++*cnt == idx) ||
2580 idx < 0);
2581}
2582
2583static void print_duplicate_syms(struct dso *dso, const char *sym_name)
2584{
2585 struct symbol *sym;
2586 bool near = false;
2587 int cnt = 0;
2588
2589 pr_err("Multiple symbols with name '%s'\n", sym_name);
2590
2591 sym = dso__first_symbol(dso);
2592 while (sym) {
2593 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
2594 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2595 ++cnt, sym->start,
2596 sym->binding == STB_GLOBAL ? 'g' :
2597 sym->binding == STB_LOCAL ? 'l' : 'w',
2598 sym->name);
2599 near = true;
2600 } else if (near) {
2601 near = false;
2602 pr_err("\t\twhich is near\t\t%s\n", sym->name);
2603 }
2604 sym = dso__next_symbol(sym);
2605 }
2606
2607 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2608 sym_name);
2609 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2610}
2611
2612static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
2613 u64 *size, int idx)
2614{
2615 struct symbol *sym;
2616 int cnt = 0;
2617
2618 *start = 0;
2619 *size = 0;
2620
2621 sym = dso__first_symbol(dso);
2622 while (sym) {
2623 if (*start) {
2624 if (!*size)
2625 *size = sym->start - *start;
2626 if (idx > 0) {
2627 if (*size)
2628 return 0;
2629 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2630 print_duplicate_syms(dso, sym_name);
2631 return -EINVAL;
2632 }
2633 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2634 *start = sym->start;
2635 *size = sym->end - sym->start;
2636 }
2637 sym = dso__next_symbol(sym);
2638 }
2639
2640 if (!*start)
2641 return sym_not_found_error(sym_name, idx);
2642
2643 return 0;
2644}
2645
2646static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2647{
2648 if (dso__data_file_size(dso, NULL)) {
2649 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2650 filt->filename);
2651 return -EINVAL;
2652 }
2653
2654 filt->addr = 0;
2655 filt->size = dso->data.file_size;
2656
2657 return 0;
2658}
2659
2660static int addr_filter__resolve_syms(struct addr_filter *filt)
2661{
2662 u64 start, size;
2663 struct dso *dso;
2664 int err = 0;
2665
2666 if (!filt->sym_from && !filt->sym_to)
2667 return 0;
2668
2669 if (!filt->filename)
2670 return addr_filter__resolve_kernel_syms(filt);
2671
2672 dso = load_dso(filt->filename);
2673 if (!dso) {
2674 pr_err("Failed to load symbols from: %s\n", filt->filename);
2675 return -EINVAL;
2676 }
2677
2678 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2679 err = addr_filter__entire_dso(filt, dso);
2680 goto put_dso;
2681 }
2682
2683 if (filt->sym_from) {
2684 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2685 filt->sym_from_idx);
2686 if (err)
2687 goto put_dso;
2688 filt->addr = start;
2689 if (filt->range && !filt->size && !filt->sym_to)
2690 filt->size = size;
2691 }
2692
2693 if (filt->sym_to) {
2694 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2695 filt->sym_to_idx);
2696 if (err)
2697 goto put_dso;
2698
2699 err = check_end_after_start(filt, start, size);
2700 if (err)
2701 return err;
2702
2703 filt->size = start + size - filt->addr;
2704 }
2705
2706put_dso:
2707 dso__put(dso);
2708
2709 return err;
2710}
2711
2712static char *addr_filter__to_str(struct addr_filter *filt)
2713{
2714 char filename_buf[PATH_MAX];
2715 const char *at = "";
2716 const char *fn = "";
2717 char *filter;
2718 int err;
2719
2720 if (filt->filename) {
2721 at = "@";
2722 fn = realpath(filt->filename, filename_buf);
2723 if (!fn)
2724 return NULL;
2725 }
2726
2727 if (filt->range) {
2728 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2729 filt->action, filt->addr, filt->size, at, fn);
2730 } else {
2731 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2732 filt->action, filt->addr, at, fn);
2733 }
2734
2735 return err < 0 ? NULL : filter;
2736}
2737
2738static int parse_addr_filter(struct evsel *evsel, const char *filter,
2739 int max_nr)
2740{
2741 struct addr_filters filts;
2742 struct addr_filter *filt;
2743 int err;
2744
2745 addr_filters__init(&filts);
2746
2747 err = addr_filters__parse_bare_filter(&filts, filter);
2748 if (err)
2749 goto out_exit;
2750
2751 if (filts.cnt > max_nr) {
2752 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2753 filts.cnt, max_nr);
2754 err = -EINVAL;
2755 goto out_exit;
2756 }
2757
2758 list_for_each_entry(filt, &filts.head, list) {
2759 char *new_filter;
2760
2761 err = addr_filter__resolve_syms(filt);
2762 if (err)
2763 goto out_exit;
2764
2765 new_filter = addr_filter__to_str(filt);
2766 if (!new_filter) {
2767 err = -ENOMEM;
2768 goto out_exit;
2769 }
2770
2771 if (evsel__append_addr_filter(evsel, new_filter)) {
2772 err = -ENOMEM;
2773 goto out_exit;
2774 }
2775 }
2776
2777out_exit:
2778 addr_filters__exit(&filts);
2779
2780 if (err) {
2781 pr_err("Failed to parse address filter: '%s'\n", filter);
2782 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2783 pr_err("Where multiple filters are separated by space or comma.\n");
2784 }
2785
2786 return err;
2787}
2788
2789static int evsel__nr_addr_filter(struct evsel *evsel)
2790{
2791 struct perf_pmu *pmu = evsel__find_pmu(evsel);
2792 int nr_addr_filters = 0;
2793
2794 if (!pmu)
2795 return 0;
2796
2797 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2798
2799 return nr_addr_filters;
2800}
2801
2802int auxtrace_parse_filters(struct evlist *evlist)
2803{
2804 struct evsel *evsel;
2805 char *filter;
2806 int err, max_nr;
2807
2808 evlist__for_each_entry(evlist, evsel) {
2809 filter = evsel->filter;
2810 max_nr = evsel__nr_addr_filter(evsel);
2811 if (!filter || !max_nr)
2812 continue;
2813 evsel->filter = NULL;
2814 err = parse_addr_filter(evsel, filter, max_nr);
2815 free(filter);
2816 if (err)
2817 return err;
2818 pr_debug("Address filter: %s\n", evsel->filter);
2819 }
2820
2821 return 0;
2822}
2823
2824int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2825 struct perf_sample *sample, struct perf_tool *tool)
2826{
2827 if (!session->auxtrace)
2828 return 0;
2829
2830 return session->auxtrace->process_event(session, event, sample, tool);
2831}
2832
2833void auxtrace__dump_auxtrace_sample(struct perf_session *session,
2834 struct perf_sample *sample)
2835{
2836 if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
2837 auxtrace__dont_decode(session))
2838 return;
2839
2840 session->auxtrace->dump_auxtrace_sample(session, sample);
2841}
2842
2843int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2844{
2845 if (!session->auxtrace)
2846 return 0;
2847
2848 return session->auxtrace->flush_events(session, tool);
2849}
2850
2851void auxtrace__free_events(struct perf_session *session)
2852{
2853 if (!session->auxtrace)
2854 return;
2855
2856 return session->auxtrace->free_events(session);
2857}
2858
2859void auxtrace__free(struct perf_session *session)
2860{
2861 if (!session->auxtrace)
2862 return;
2863
2864 return session->auxtrace->free(session);
2865}
2866
2867bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
2868 struct evsel *evsel)
2869{
2870 if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
2871 return false;
2872
2873 return session->auxtrace->evsel_is_auxtrace(session, evsel);
2874}
1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
5 */
6
7#include <inttypes.h>
8#include <sys/types.h>
9#include <sys/mman.h>
10#include <stdbool.h>
11#include <string.h>
12#include <limits.h>
13#include <errno.h>
14
15#include <linux/kernel.h>
16#include <linux/perf_event.h>
17#include <linux/types.h>
18#include <linux/bitops.h>
19#include <linux/log2.h>
20#include <linux/string.h>
21#include <linux/time64.h>
22
23#include <sys/param.h>
24#include <stdlib.h>
25#include <stdio.h>
26#include <linux/list.h>
27#include <linux/zalloc.h>
28
29#include "config.h"
30#include "evlist.h"
31#include "dso.h"
32#include "map.h"
33#include "pmu.h"
34#include "evsel.h"
35#include "evsel_config.h"
36#include "symbol.h"
37#include "util/perf_api_probe.h"
38#include "util/synthetic-events.h"
39#include "thread_map.h"
40#include "asm/bug.h"
41#include "auxtrace.h"
42
43#include <linux/hash.h>
44
45#include "event.h"
46#include "record.h"
47#include "session.h"
48#include "debug.h"
49#include <subcmd/parse-options.h>
50
51#include "cs-etm.h"
52#include "intel-pt.h"
53#include "intel-bts.h"
54#include "arm-spe.h"
55#include "hisi-ptt.h"
56#include "s390-cpumsf.h"
57#include "util/mmap.h"
58
59#include <linux/ctype.h>
60#include "symbol/kallsyms.h"
61#include <internal/lib.h>
62#include "util/sample.h"
63
64/*
65 * Make a group from 'leader' to 'last', requiring that the events were not
66 * already grouped to a different leader.
67 */
68static int evlist__regroup(struct evlist *evlist, struct evsel *leader, struct evsel *last)
69{
70 struct evsel *evsel;
71 bool grp;
72
73 if (!evsel__is_group_leader(leader))
74 return -EINVAL;
75
76 grp = false;
77 evlist__for_each_entry(evlist, evsel) {
78 if (grp) {
79 if (!(evsel__leader(evsel) == leader ||
80 (evsel__leader(evsel) == evsel &&
81 evsel->core.nr_members <= 1)))
82 return -EINVAL;
83 } else if (evsel == leader) {
84 grp = true;
85 }
86 if (evsel == last)
87 break;
88 }
89
90 grp = false;
91 evlist__for_each_entry(evlist, evsel) {
92 if (grp) {
93 if (!evsel__has_leader(evsel, leader)) {
94 evsel__set_leader(evsel, leader);
95 if (leader->core.nr_members < 1)
96 leader->core.nr_members = 1;
97 leader->core.nr_members += 1;
98 }
99 } else if (evsel == leader) {
100 grp = true;
101 }
102 if (evsel == last)
103 break;
104 }
105
106 return 0;
107}
108
109static bool auxtrace__dont_decode(struct perf_session *session)
110{
111 return !session->itrace_synth_opts ||
112 session->itrace_synth_opts->dont_decode;
113}
114
115int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
116 struct auxtrace_mmap_params *mp,
117 void *userpg, int fd)
118{
119 struct perf_event_mmap_page *pc = userpg;
120
121 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
122
123 mm->userpg = userpg;
124 mm->mask = mp->mask;
125 mm->len = mp->len;
126 mm->prev = 0;
127 mm->idx = mp->idx;
128 mm->tid = mp->tid;
129 mm->cpu = mp->cpu.cpu;
130
131 if (!mp->len || !mp->mmap_needed) {
132 mm->base = NULL;
133 return 0;
134 }
135
136 pc->aux_offset = mp->offset;
137 pc->aux_size = mp->len;
138
139 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
140 if (mm->base == MAP_FAILED) {
141 pr_debug2("failed to mmap AUX area\n");
142 mm->base = NULL;
143 return -1;
144 }
145
146 return 0;
147}
148
149void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
150{
151 if (mm->base) {
152 munmap(mm->base, mm->len);
153 mm->base = NULL;
154 }
155}
156
157void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
158 off_t auxtrace_offset,
159 unsigned int auxtrace_pages,
160 bool auxtrace_overwrite)
161{
162 if (auxtrace_pages) {
163 mp->offset = auxtrace_offset;
164 mp->len = auxtrace_pages * (size_t)page_size;
165 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
166 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
167 pr_debug2("AUX area mmap length %zu\n", mp->len);
168 } else {
169 mp->len = 0;
170 }
171}
172
173void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
174 struct evlist *evlist,
175 struct evsel *evsel, int idx)
176{
177 bool per_cpu = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
178
179 mp->mmap_needed = evsel->needs_auxtrace_mmap;
180
181 if (!mp->mmap_needed)
182 return;
183
184 mp->idx = idx;
185
186 if (per_cpu) {
187 mp->cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
188 if (evlist->core.threads)
189 mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
190 else
191 mp->tid = -1;
192 } else {
193 mp->cpu.cpu = -1;
194 mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
195 }
196}
197
198#define AUXTRACE_INIT_NR_QUEUES 32
199
200static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
201{
202 struct auxtrace_queue *queue_array;
203 unsigned int max_nr_queues, i;
204
205 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
206 if (nr_queues > max_nr_queues)
207 return NULL;
208
209 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
210 if (!queue_array)
211 return NULL;
212
213 for (i = 0; i < nr_queues; i++) {
214 INIT_LIST_HEAD(&queue_array[i].head);
215 queue_array[i].priv = NULL;
216 }
217
218 return queue_array;
219}
220
221int auxtrace_queues__init(struct auxtrace_queues *queues)
222{
223 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
224 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
225 if (!queues->queue_array)
226 return -ENOMEM;
227 return 0;
228}
229
230static int auxtrace_queues__grow(struct auxtrace_queues *queues,
231 unsigned int new_nr_queues)
232{
233 unsigned int nr_queues = queues->nr_queues;
234 struct auxtrace_queue *queue_array;
235 unsigned int i;
236
237 if (!nr_queues)
238 nr_queues = AUXTRACE_INIT_NR_QUEUES;
239
240 while (nr_queues && nr_queues < new_nr_queues)
241 nr_queues <<= 1;
242
243 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
244 return -EINVAL;
245
246 queue_array = auxtrace_alloc_queue_array(nr_queues);
247 if (!queue_array)
248 return -ENOMEM;
249
250 for (i = 0; i < queues->nr_queues; i++) {
251 list_splice_tail(&queues->queue_array[i].head,
252 &queue_array[i].head);
253 queue_array[i].tid = queues->queue_array[i].tid;
254 queue_array[i].cpu = queues->queue_array[i].cpu;
255 queue_array[i].set = queues->queue_array[i].set;
256 queue_array[i].priv = queues->queue_array[i].priv;
257 }
258
259 queues->nr_queues = nr_queues;
260 queues->queue_array = queue_array;
261
262 return 0;
263}
264
265static void *auxtrace_copy_data(u64 size, struct perf_session *session)
266{
267 int fd = perf_data__fd(session->data);
268 void *p;
269 ssize_t ret;
270
271 if (size > SSIZE_MAX)
272 return NULL;
273
274 p = malloc(size);
275 if (!p)
276 return NULL;
277
278 ret = readn(fd, p, size);
279 if (ret != (ssize_t)size) {
280 free(p);
281 return NULL;
282 }
283
284 return p;
285}
286
287static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
288 unsigned int idx,
289 struct auxtrace_buffer *buffer)
290{
291 struct auxtrace_queue *queue;
292 int err;
293
294 if (idx >= queues->nr_queues) {
295 err = auxtrace_queues__grow(queues, idx + 1);
296 if (err)
297 return err;
298 }
299
300 queue = &queues->queue_array[idx];
301
302 if (!queue->set) {
303 queue->set = true;
304 queue->tid = buffer->tid;
305 queue->cpu = buffer->cpu.cpu;
306 }
307
308 buffer->buffer_nr = queues->next_buffer_nr++;
309
310 list_add_tail(&buffer->list, &queue->head);
311
312 queues->new_data = true;
313 queues->populated = true;
314
315 return 0;
316}
317
318/* Limit buffers to 32MiB on 32-bit */
319#define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
320
321static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
322 unsigned int idx,
323 struct auxtrace_buffer *buffer)
324{
325 u64 sz = buffer->size;
326 bool consecutive = false;
327 struct auxtrace_buffer *b;
328 int err;
329
330 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
331 b = memdup(buffer, sizeof(struct auxtrace_buffer));
332 if (!b)
333 return -ENOMEM;
334 b->size = BUFFER_LIMIT_FOR_32_BIT;
335 b->consecutive = consecutive;
336 err = auxtrace_queues__queue_buffer(queues, idx, b);
337 if (err) {
338 auxtrace_buffer__free(b);
339 return err;
340 }
341 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
342 sz -= BUFFER_LIMIT_FOR_32_BIT;
343 consecutive = true;
344 }
345
346 buffer->size = sz;
347 buffer->consecutive = consecutive;
348
349 return 0;
350}
351
352static bool filter_cpu(struct perf_session *session, struct perf_cpu cpu)
353{
354 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
355
356 return cpu_bitmap && cpu.cpu != -1 && !test_bit(cpu.cpu, cpu_bitmap);
357}
358
359static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
360 struct perf_session *session,
361 unsigned int idx,
362 struct auxtrace_buffer *buffer,
363 struct auxtrace_buffer **buffer_ptr)
364{
365 int err = -ENOMEM;
366
367 if (filter_cpu(session, buffer->cpu))
368 return 0;
369
370 buffer = memdup(buffer, sizeof(*buffer));
371 if (!buffer)
372 return -ENOMEM;
373
374 if (session->one_mmap) {
375 buffer->data = buffer->data_offset - session->one_mmap_offset +
376 session->one_mmap_addr;
377 } else if (perf_data__is_pipe(session->data)) {
378 buffer->data = auxtrace_copy_data(buffer->size, session);
379 if (!buffer->data)
380 goto out_free;
381 buffer->data_needs_freeing = true;
382 } else if (BITS_PER_LONG == 32 &&
383 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
384 err = auxtrace_queues__split_buffer(queues, idx, buffer);
385 if (err)
386 goto out_free;
387 }
388
389 err = auxtrace_queues__queue_buffer(queues, idx, buffer);
390 if (err)
391 goto out_free;
392
393 /* FIXME: Doesn't work for split buffer */
394 if (buffer_ptr)
395 *buffer_ptr = buffer;
396
397 return 0;
398
399out_free:
400 auxtrace_buffer__free(buffer);
401 return err;
402}
403
404int auxtrace_queues__add_event(struct auxtrace_queues *queues,
405 struct perf_session *session,
406 union perf_event *event, off_t data_offset,
407 struct auxtrace_buffer **buffer_ptr)
408{
409 struct auxtrace_buffer buffer = {
410 .pid = -1,
411 .tid = event->auxtrace.tid,
412 .cpu = { event->auxtrace.cpu },
413 .data_offset = data_offset,
414 .offset = event->auxtrace.offset,
415 .reference = event->auxtrace.reference,
416 .size = event->auxtrace.size,
417 };
418 unsigned int idx = event->auxtrace.idx;
419
420 return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
421 buffer_ptr);
422}
423
424static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
425 struct perf_session *session,
426 off_t file_offset, size_t sz)
427{
428 union perf_event *event;
429 int err;
430 char buf[PERF_SAMPLE_MAX_SIZE];
431
432 err = perf_session__peek_event(session, file_offset, buf,
433 PERF_SAMPLE_MAX_SIZE, &event, NULL);
434 if (err)
435 return err;
436
437 if (event->header.type == PERF_RECORD_AUXTRACE) {
438 if (event->header.size < sizeof(struct perf_record_auxtrace) ||
439 event->header.size != sz) {
440 err = -EINVAL;
441 goto out;
442 }
443 file_offset += event->header.size;
444 err = auxtrace_queues__add_event(queues, session, event,
445 file_offset, NULL);
446 }
447out:
448 return err;
449}
450
451void auxtrace_queues__free(struct auxtrace_queues *queues)
452{
453 unsigned int i;
454
455 for (i = 0; i < queues->nr_queues; i++) {
456 while (!list_empty(&queues->queue_array[i].head)) {
457 struct auxtrace_buffer *buffer;
458
459 buffer = list_entry(queues->queue_array[i].head.next,
460 struct auxtrace_buffer, list);
461 list_del_init(&buffer->list);
462 auxtrace_buffer__free(buffer);
463 }
464 }
465
466 zfree(&queues->queue_array);
467 queues->nr_queues = 0;
468}
469
470static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
471 unsigned int pos, unsigned int queue_nr,
472 u64 ordinal)
473{
474 unsigned int parent;
475
476 while (pos) {
477 parent = (pos - 1) >> 1;
478 if (heap_array[parent].ordinal <= ordinal)
479 break;
480 heap_array[pos] = heap_array[parent];
481 pos = parent;
482 }
483 heap_array[pos].queue_nr = queue_nr;
484 heap_array[pos].ordinal = ordinal;
485}
486
487int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
488 u64 ordinal)
489{
490 struct auxtrace_heap_item *heap_array;
491
492 if (queue_nr >= heap->heap_sz) {
493 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
494
495 while (heap_sz <= queue_nr)
496 heap_sz <<= 1;
497 heap_array = realloc(heap->heap_array,
498 heap_sz * sizeof(struct auxtrace_heap_item));
499 if (!heap_array)
500 return -ENOMEM;
501 heap->heap_array = heap_array;
502 heap->heap_sz = heap_sz;
503 }
504
505 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
506
507 return 0;
508}
509
510void auxtrace_heap__free(struct auxtrace_heap *heap)
511{
512 zfree(&heap->heap_array);
513 heap->heap_cnt = 0;
514 heap->heap_sz = 0;
515}
516
517void auxtrace_heap__pop(struct auxtrace_heap *heap)
518{
519 unsigned int pos, last, heap_cnt = heap->heap_cnt;
520 struct auxtrace_heap_item *heap_array;
521
522 if (!heap_cnt)
523 return;
524
525 heap->heap_cnt -= 1;
526
527 heap_array = heap->heap_array;
528
529 pos = 0;
530 while (1) {
531 unsigned int left, right;
532
533 left = (pos << 1) + 1;
534 if (left >= heap_cnt)
535 break;
536 right = left + 1;
537 if (right >= heap_cnt) {
538 heap_array[pos] = heap_array[left];
539 return;
540 }
541 if (heap_array[left].ordinal < heap_array[right].ordinal) {
542 heap_array[pos] = heap_array[left];
543 pos = left;
544 } else {
545 heap_array[pos] = heap_array[right];
546 pos = right;
547 }
548 }
549
550 last = heap_cnt - 1;
551 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
552 heap_array[last].ordinal);
553}
554
555size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
556 struct evlist *evlist)
557{
558 if (itr)
559 return itr->info_priv_size(itr, evlist);
560 return 0;
561}
562
563static int auxtrace_not_supported(void)
564{
565 pr_err("AUX area tracing is not supported on this architecture\n");
566 return -EINVAL;
567}
568
569int auxtrace_record__info_fill(struct auxtrace_record *itr,
570 struct perf_session *session,
571 struct perf_record_auxtrace_info *auxtrace_info,
572 size_t priv_size)
573{
574 if (itr)
575 return itr->info_fill(itr, session, auxtrace_info, priv_size);
576 return auxtrace_not_supported();
577}
578
579void auxtrace_record__free(struct auxtrace_record *itr)
580{
581 if (itr)
582 itr->free(itr);
583}
584
585int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
586{
587 if (itr && itr->snapshot_start)
588 return itr->snapshot_start(itr);
589 return 0;
590}
591
592int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
593{
594 if (!on_exit && itr && itr->snapshot_finish)
595 return itr->snapshot_finish(itr);
596 return 0;
597}
598
599int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
600 struct auxtrace_mmap *mm,
601 unsigned char *data, u64 *head, u64 *old)
602{
603 if (itr && itr->find_snapshot)
604 return itr->find_snapshot(itr, idx, mm, data, head, old);
605 return 0;
606}
607
608int auxtrace_record__options(struct auxtrace_record *itr,
609 struct evlist *evlist,
610 struct record_opts *opts)
611{
612 if (itr) {
613 itr->evlist = evlist;
614 return itr->recording_options(itr, evlist, opts);
615 }
616 return 0;
617}
618
619u64 auxtrace_record__reference(struct auxtrace_record *itr)
620{
621 if (itr)
622 return itr->reference(itr);
623 return 0;
624}
625
626int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
627 struct record_opts *opts, const char *str)
628{
629 if (!str)
630 return 0;
631
632 /* PMU-agnostic options */
633 switch (*str) {
634 case 'e':
635 opts->auxtrace_snapshot_on_exit = true;
636 str++;
637 break;
638 default:
639 break;
640 }
641
642 if (itr && itr->parse_snapshot_options)
643 return itr->parse_snapshot_options(itr, opts, str);
644
645 pr_err("No AUX area tracing to snapshot\n");
646 return -EINVAL;
647}
648
649static int evlist__enable_event_idx(struct evlist *evlist, struct evsel *evsel, int idx)
650{
651 bool per_cpu_mmaps = !perf_cpu_map__empty(evlist->core.user_requested_cpus);
652
653 if (per_cpu_mmaps) {
654 struct perf_cpu evlist_cpu = perf_cpu_map__cpu(evlist->core.all_cpus, idx);
655 int cpu_map_idx = perf_cpu_map__idx(evsel->core.cpus, evlist_cpu);
656
657 if (cpu_map_idx == -1)
658 return -EINVAL;
659 return perf_evsel__enable_cpu(&evsel->core, cpu_map_idx);
660 }
661
662 return perf_evsel__enable_thread(&evsel->core, idx);
663}
664
665int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
666{
667 struct evsel *evsel;
668
669 if (!itr->evlist || !itr->pmu)
670 return -EINVAL;
671
672 evlist__for_each_entry(itr->evlist, evsel) {
673 if (evsel->core.attr.type == itr->pmu->type) {
674 if (evsel->disabled)
675 return 0;
676 return evlist__enable_event_idx(itr->evlist, evsel, idx);
677 }
678 }
679 return -EINVAL;
680}
681
682/*
683 * Event record size is 16-bit which results in a maximum size of about 64KiB.
684 * Allow about 4KiB for the rest of the sample record, to give a maximum
685 * AUX area sample size of 60KiB.
686 */
687#define MAX_AUX_SAMPLE_SIZE (60 * 1024)
688
689/* Arbitrary default size if no other default provided */
690#define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)
691
692static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
693 struct record_opts *opts)
694{
695 struct evsel *evsel;
696 bool has_aux_leader = false;
697 u32 sz;
698
699 evlist__for_each_entry(evlist, evsel) {
700 sz = evsel->core.attr.aux_sample_size;
701 if (evsel__is_group_leader(evsel)) {
702 has_aux_leader = evsel__is_aux_event(evsel);
703 if (sz) {
704 if (has_aux_leader)
705 pr_err("Cannot add AUX area sampling to an AUX area event\n");
706 else
707 pr_err("Cannot add AUX area sampling to a group leader\n");
708 return -EINVAL;
709 }
710 }
711 if (sz > MAX_AUX_SAMPLE_SIZE) {
712 pr_err("AUX area sample size %u too big, max. %d\n",
713 sz, MAX_AUX_SAMPLE_SIZE);
714 return -EINVAL;
715 }
716 if (sz) {
717 if (!has_aux_leader) {
718 pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
719 return -EINVAL;
720 }
721 evsel__set_sample_bit(evsel, AUX);
722 opts->auxtrace_sample_mode = true;
723 } else {
724 evsel__reset_sample_bit(evsel, AUX);
725 }
726 }
727
728 if (!opts->auxtrace_sample_mode) {
729 pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
730 return -EINVAL;
731 }
732
733 if (!perf_can_aux_sample()) {
734 pr_err("AUX area sampling is not supported by kernel\n");
735 return -EINVAL;
736 }
737
738 return 0;
739}
740
741int auxtrace_parse_sample_options(struct auxtrace_record *itr,
742 struct evlist *evlist,
743 struct record_opts *opts, const char *str)
744{
745 struct evsel_config_term *term;
746 struct evsel *aux_evsel;
747 bool has_aux_sample_size = false;
748 bool has_aux_leader = false;
749 struct evsel *evsel;
750 char *endptr;
751 unsigned long sz;
752
753 if (!str)
754 goto no_opt;
755
756 if (!itr) {
757 pr_err("No AUX area event to sample\n");
758 return -EINVAL;
759 }
760
761 sz = strtoul(str, &endptr, 0);
762 if (*endptr || sz > UINT_MAX) {
763 pr_err("Bad AUX area sampling option: '%s'\n", str);
764 return -EINVAL;
765 }
766
767 if (!sz)
768 sz = itr->default_aux_sample_size;
769
770 if (!sz)
771 sz = DEFAULT_AUX_SAMPLE_SIZE;
772
773 /* Set aux_sample_size based on --aux-sample option */
774 evlist__for_each_entry(evlist, evsel) {
775 if (evsel__is_group_leader(evsel)) {
776 has_aux_leader = evsel__is_aux_event(evsel);
777 } else if (has_aux_leader) {
778 evsel->core.attr.aux_sample_size = sz;
779 }
780 }
781no_opt:
782 aux_evsel = NULL;
783 /* Override with aux_sample_size from config term */
784 evlist__for_each_entry(evlist, evsel) {
785 if (evsel__is_aux_event(evsel))
786 aux_evsel = evsel;
787 term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
788 if (term) {
789 has_aux_sample_size = true;
790 evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
791 /* If possible, group with the AUX event */
792 if (aux_evsel && evsel->core.attr.aux_sample_size)
793 evlist__regroup(evlist, aux_evsel, evsel);
794 }
795 }
796
797 if (!str && !has_aux_sample_size)
798 return 0;
799
800 if (!itr) {
801 pr_err("No AUX area event to sample\n");
802 return -EINVAL;
803 }
804
805 return auxtrace_validate_aux_sample_size(evlist, opts);
806}
807
808void auxtrace_regroup_aux_output(struct evlist *evlist)
809{
810 struct evsel *evsel, *aux_evsel = NULL;
811 struct evsel_config_term *term;
812
813 evlist__for_each_entry(evlist, evsel) {
814 if (evsel__is_aux_event(evsel))
815 aux_evsel = evsel;
816 term = evsel__get_config_term(evsel, AUX_OUTPUT);
817 /* If possible, group with the AUX event */
818 if (term && aux_evsel)
819 evlist__regroup(evlist, aux_evsel, evsel);
820 }
821}
822
823struct auxtrace_record *__weak
824auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
825{
826 *err = 0;
827 return NULL;
828}
829
830static int auxtrace_index__alloc(struct list_head *head)
831{
832 struct auxtrace_index *auxtrace_index;
833
834 auxtrace_index = malloc(sizeof(struct auxtrace_index));
835 if (!auxtrace_index)
836 return -ENOMEM;
837
838 auxtrace_index->nr = 0;
839 INIT_LIST_HEAD(&auxtrace_index->list);
840
841 list_add_tail(&auxtrace_index->list, head);
842
843 return 0;
844}
845
846void auxtrace_index__free(struct list_head *head)
847{
848 struct auxtrace_index *auxtrace_index, *n;
849
850 list_for_each_entry_safe(auxtrace_index, n, head, list) {
851 list_del_init(&auxtrace_index->list);
852 free(auxtrace_index);
853 }
854}
855
856static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
857{
858 struct auxtrace_index *auxtrace_index;
859 int err;
860
861 if (list_empty(head)) {
862 err = auxtrace_index__alloc(head);
863 if (err)
864 return NULL;
865 }
866
867 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
868
869 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
870 err = auxtrace_index__alloc(head);
871 if (err)
872 return NULL;
873 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
874 list);
875 }
876
877 return auxtrace_index;
878}
879
880int auxtrace_index__auxtrace_event(struct list_head *head,
881 union perf_event *event, off_t file_offset)
882{
883 struct auxtrace_index *auxtrace_index;
884 size_t nr;
885
886 auxtrace_index = auxtrace_index__last(head);
887 if (!auxtrace_index)
888 return -ENOMEM;
889
890 nr = auxtrace_index->nr;
891 auxtrace_index->entries[nr].file_offset = file_offset;
892 auxtrace_index->entries[nr].sz = event->header.size;
893 auxtrace_index->nr += 1;
894
895 return 0;
896}
897
898static int auxtrace_index__do_write(int fd,
899 struct auxtrace_index *auxtrace_index)
900{
901 struct auxtrace_index_entry ent;
902 size_t i;
903
904 for (i = 0; i < auxtrace_index->nr; i++) {
905 ent.file_offset = auxtrace_index->entries[i].file_offset;
906 ent.sz = auxtrace_index->entries[i].sz;
907 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
908 return -errno;
909 }
910 return 0;
911}
912
913int auxtrace_index__write(int fd, struct list_head *head)
914{
915 struct auxtrace_index *auxtrace_index;
916 u64 total = 0;
917 int err;
918
919 list_for_each_entry(auxtrace_index, head, list)
920 total += auxtrace_index->nr;
921
922 if (writen(fd, &total, sizeof(total)) != sizeof(total))
923 return -errno;
924
925 list_for_each_entry(auxtrace_index, head, list) {
926 err = auxtrace_index__do_write(fd, auxtrace_index);
927 if (err)
928 return err;
929 }
930
931 return 0;
932}
933
934static int auxtrace_index__process_entry(int fd, struct list_head *head,
935 bool needs_swap)
936{
937 struct auxtrace_index *auxtrace_index;
938 struct auxtrace_index_entry ent;
939 size_t nr;
940
941 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
942 return -1;
943
944 auxtrace_index = auxtrace_index__last(head);
945 if (!auxtrace_index)
946 return -1;
947
948 nr = auxtrace_index->nr;
949 if (needs_swap) {
950 auxtrace_index->entries[nr].file_offset =
951 bswap_64(ent.file_offset);
952 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
953 } else {
954 auxtrace_index->entries[nr].file_offset = ent.file_offset;
955 auxtrace_index->entries[nr].sz = ent.sz;
956 }
957
958 auxtrace_index->nr = nr + 1;
959
960 return 0;
961}
962
963int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
964 bool needs_swap)
965{
966 struct list_head *head = &session->auxtrace_index;
967 u64 nr;
968
969 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
970 return -1;
971
972 if (needs_swap)
973 nr = bswap_64(nr);
974
975 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
976 return -1;
977
978 while (nr--) {
979 int err;
980
981 err = auxtrace_index__process_entry(fd, head, needs_swap);
982 if (err)
983 return -1;
984 }
985
986 return 0;
987}
988
989static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
990 struct perf_session *session,
991 struct auxtrace_index_entry *ent)
992{
993 return auxtrace_queues__add_indexed_event(queues, session,
994 ent->file_offset, ent->sz);
995}
996
997int auxtrace_queues__process_index(struct auxtrace_queues *queues,
998 struct perf_session *session)
999{
1000 struct auxtrace_index *auxtrace_index;
1001 struct auxtrace_index_entry *ent;
1002 size_t i;
1003 int err;
1004
1005 if (auxtrace__dont_decode(session))
1006 return 0;
1007
1008 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
1009 for (i = 0; i < auxtrace_index->nr; i++) {
1010 ent = &auxtrace_index->entries[i];
1011 err = auxtrace_queues__process_index_entry(queues,
1012 session,
1013 ent);
1014 if (err)
1015 return err;
1016 }
1017 }
1018 return 0;
1019}
1020
1021struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
1022 struct auxtrace_buffer *buffer)
1023{
1024 if (buffer) {
1025 if (list_is_last(&buffer->list, &queue->head))
1026 return NULL;
1027 return list_entry(buffer->list.next, struct auxtrace_buffer,
1028 list);
1029 } else {
1030 if (list_empty(&queue->head))
1031 return NULL;
1032 return list_entry(queue->head.next, struct auxtrace_buffer,
1033 list);
1034 }
1035}
1036
1037struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
1038 struct perf_sample *sample,
1039 struct perf_session *session)
1040{
1041 struct perf_sample_id *sid;
1042 unsigned int idx;
1043 u64 id;
1044
1045 id = sample->id;
1046 if (!id)
1047 return NULL;
1048
1049 sid = evlist__id2sid(session->evlist, id);
1050 if (!sid)
1051 return NULL;
1052
1053 idx = sid->idx;
1054
1055 if (idx >= queues->nr_queues)
1056 return NULL;
1057
1058 return &queues->queue_array[idx];
1059}
1060
1061int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
1062 struct perf_session *session,
1063 struct perf_sample *sample, u64 data_offset,
1064 u64 reference)
1065{
1066 struct auxtrace_buffer buffer = {
1067 .pid = -1,
1068 .data_offset = data_offset,
1069 .reference = reference,
1070 .size = sample->aux_sample.size,
1071 };
1072 struct perf_sample_id *sid;
1073 u64 id = sample->id;
1074 unsigned int idx;
1075
1076 if (!id)
1077 return -EINVAL;
1078
1079 sid = evlist__id2sid(session->evlist, id);
1080 if (!sid)
1081 return -ENOENT;
1082
1083 idx = sid->idx;
1084 buffer.tid = sid->tid;
1085 buffer.cpu = sid->cpu;
1086
1087 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
1088}
1089
1090struct queue_data {
1091 bool samples;
1092 bool events;
1093};
1094
1095static int auxtrace_queue_data_cb(struct perf_session *session,
1096 union perf_event *event, u64 offset,
1097 void *data)
1098{
1099 struct queue_data *qd = data;
1100 struct perf_sample sample;
1101 int err;
1102
1103 if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
1104 if (event->header.size < sizeof(struct perf_record_auxtrace))
1105 return -EINVAL;
1106 offset += event->header.size;
1107 return session->auxtrace->queue_data(session, NULL, event,
1108 offset);
1109 }
1110
1111 if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
1112 return 0;
1113
1114 err = evlist__parse_sample(session->evlist, event, &sample);
1115 if (err)
1116 return err;
1117
1118 if (!sample.aux_sample.size)
1119 return 0;
1120
1121 offset += sample.aux_sample.data - (void *)event;
1122
1123 return session->auxtrace->queue_data(session, &sample, NULL, offset);
1124}
1125
1126int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
1127{
1128 struct queue_data qd = {
1129 .samples = samples,
1130 .events = events,
1131 };
1132
1133 if (auxtrace__dont_decode(session))
1134 return 0;
1135
1136 if (!session->auxtrace || !session->auxtrace->queue_data)
1137 return -EINVAL;
1138
1139 return perf_session__peek_events(session, session->header.data_offset,
1140 session->header.data_size,
1141 auxtrace_queue_data_cb, &qd);
1142}
1143
1144void *auxtrace_buffer__get_data_rw(struct auxtrace_buffer *buffer, int fd, bool rw)
1145{
1146 int prot = rw ? PROT_READ | PROT_WRITE : PROT_READ;
1147 size_t adj = buffer->data_offset & (page_size - 1);
1148 size_t size = buffer->size + adj;
1149 off_t file_offset = buffer->data_offset - adj;
1150 void *addr;
1151
1152 if (buffer->data)
1153 return buffer->data;
1154
1155 addr = mmap(NULL, size, prot, MAP_SHARED, fd, file_offset);
1156 if (addr == MAP_FAILED)
1157 return NULL;
1158
1159 buffer->mmap_addr = addr;
1160 buffer->mmap_size = size;
1161
1162 buffer->data = addr + adj;
1163
1164 return buffer->data;
1165}
1166
1167void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
1168{
1169 if (!buffer->data || !buffer->mmap_addr)
1170 return;
1171 munmap(buffer->mmap_addr, buffer->mmap_size);
1172 buffer->mmap_addr = NULL;
1173 buffer->mmap_size = 0;
1174 buffer->data = NULL;
1175 buffer->use_data = NULL;
1176}
1177
1178void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
1179{
1180 auxtrace_buffer__put_data(buffer);
1181 if (buffer->data_needs_freeing) {
1182 buffer->data_needs_freeing = false;
1183 zfree(&buffer->data);
1184 buffer->use_data = NULL;
1185 buffer->size = 0;
1186 }
1187}
1188
1189void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
1190{
1191 auxtrace_buffer__drop_data(buffer);
1192 free(buffer);
1193}
1194
1195void auxtrace_synth_guest_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1196 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1197 const char *msg, u64 timestamp,
1198 pid_t machine_pid, int vcpu)
1199{
1200 size_t size;
1201
1202 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
1203
1204 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
1205 auxtrace_error->type = type;
1206 auxtrace_error->code = code;
1207 auxtrace_error->cpu = cpu;
1208 auxtrace_error->pid = pid;
1209 auxtrace_error->tid = tid;
1210 auxtrace_error->fmt = 1;
1211 auxtrace_error->ip = ip;
1212 auxtrace_error->time = timestamp;
1213 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
1214 if (machine_pid) {
1215 auxtrace_error->fmt = 2;
1216 auxtrace_error->machine_pid = machine_pid;
1217 auxtrace_error->vcpu = vcpu;
1218 size = sizeof(*auxtrace_error);
1219 } else {
1220 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
1221 strlen(auxtrace_error->msg) + 1;
1222 }
1223 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
1224}
1225
1226void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1227 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1228 const char *msg, u64 timestamp)
1229{
1230 auxtrace_synth_guest_error(auxtrace_error, type, code, cpu, pid, tid,
1231 ip, msg, timestamp, 0, -1);
1232}
1233
1234int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
1235 struct perf_tool *tool,
1236 struct perf_session *session,
1237 perf_event__handler_t process)
1238{
1239 union perf_event *ev;
1240 size_t priv_size;
1241 int err;
1242
1243 pr_debug2("Synthesizing auxtrace information\n");
1244 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
1245 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
1246 if (!ev)
1247 return -ENOMEM;
1248
1249 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
1250 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
1251 priv_size;
1252 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
1253 priv_size);
1254 if (err)
1255 goto out_free;
1256
1257 err = process(tool, ev, NULL, NULL);
1258out_free:
1259 free(ev);
1260 return err;
1261}
1262
1263static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
1264{
1265 struct evsel *new_leader = NULL;
1266 struct evsel *evsel;
1267
1268 /* Find new leader for the group */
1269 evlist__for_each_entry(evlist, evsel) {
1270 if (!evsel__has_leader(evsel, leader) || evsel == leader)
1271 continue;
1272 if (!new_leader)
1273 new_leader = evsel;
1274 evsel__set_leader(evsel, new_leader);
1275 }
1276
1277 /* Update group information */
1278 if (new_leader) {
1279 zfree(&new_leader->group_name);
1280 new_leader->group_name = leader->group_name;
1281 leader->group_name = NULL;
1282
1283 new_leader->core.nr_members = leader->core.nr_members - 1;
1284 leader->core.nr_members = 1;
1285 }
1286}
1287
1288static void unleader_auxtrace(struct perf_session *session)
1289{
1290 struct evsel *evsel;
1291
1292 evlist__for_each_entry(session->evlist, evsel) {
1293 if (auxtrace__evsel_is_auxtrace(session, evsel) &&
1294 evsel__is_group_leader(evsel)) {
1295 unleader_evsel(session->evlist, evsel);
1296 }
1297 }
1298}
1299
1300int perf_event__process_auxtrace_info(struct perf_session *session,
1301 union perf_event *event)
1302{
1303 enum auxtrace_type type = event->auxtrace_info.type;
1304 int err;
1305
1306 if (dump_trace)
1307 fprintf(stdout, " type: %u\n", type);
1308
1309 switch (type) {
1310 case PERF_AUXTRACE_INTEL_PT:
1311 err = intel_pt_process_auxtrace_info(event, session);
1312 break;
1313 case PERF_AUXTRACE_INTEL_BTS:
1314 err = intel_bts_process_auxtrace_info(event, session);
1315 break;
1316 case PERF_AUXTRACE_ARM_SPE:
1317 err = arm_spe_process_auxtrace_info(event, session);
1318 break;
1319 case PERF_AUXTRACE_CS_ETM:
1320 err = cs_etm__process_auxtrace_info(event, session);
1321 break;
1322 case PERF_AUXTRACE_S390_CPUMSF:
1323 err = s390_cpumsf_process_auxtrace_info(event, session);
1324 break;
1325 case PERF_AUXTRACE_HISI_PTT:
1326 err = hisi_ptt_process_auxtrace_info(event, session);
1327 break;
1328 case PERF_AUXTRACE_UNKNOWN:
1329 default:
1330 return -EINVAL;
1331 }
1332
1333 if (err)
1334 return err;
1335
1336 unleader_auxtrace(session);
1337
1338 return 0;
1339}
1340
1341s64 perf_event__process_auxtrace(struct perf_session *session,
1342 union perf_event *event)
1343{
1344 s64 err;
1345
1346 if (dump_trace)
1347 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n",
1348 event->auxtrace.size, event->auxtrace.offset,
1349 event->auxtrace.reference, event->auxtrace.idx,
1350 event->auxtrace.tid, event->auxtrace.cpu);
1351
1352 if (auxtrace__dont_decode(session))
1353 return event->auxtrace.size;
1354
1355 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
1356 return -EINVAL;
1357
1358 err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
1359 if (err < 0)
1360 return err;
1361
1362 return event->auxtrace.size;
1363}
1364
1365#define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
1366#define PERF_ITRACE_DEFAULT_PERIOD 100000
1367#define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
1368#define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
1369#define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
1370#define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
1371
1372void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
1373 bool no_sample)
1374{
1375 synth_opts->branches = true;
1376 synth_opts->transactions = true;
1377 synth_opts->ptwrites = true;
1378 synth_opts->pwr_events = true;
1379 synth_opts->other_events = true;
1380 synth_opts->intr_events = true;
1381 synth_opts->errors = true;
1382 synth_opts->flc = true;
1383 synth_opts->llc = true;
1384 synth_opts->tlb = true;
1385 synth_opts->mem = true;
1386 synth_opts->remote_access = true;
1387
1388 if (no_sample) {
1389 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
1390 synth_opts->period = 1;
1391 synth_opts->calls = true;
1392 } else {
1393 synth_opts->instructions = true;
1394 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1395 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1396 }
1397 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1398 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1399 synth_opts->initial_skip = 0;
1400}
1401
1402static int get_flag(const char **ptr, unsigned int *flags)
1403{
1404 while (1) {
1405 char c = **ptr;
1406
1407 if (c >= 'a' && c <= 'z') {
1408 *flags |= 1 << (c - 'a');
1409 ++*ptr;
1410 return 0;
1411 } else if (c == ' ') {
1412 ++*ptr;
1413 continue;
1414 } else {
1415 return -1;
1416 }
1417 }
1418}
1419
1420static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
1421{
1422 while (1) {
1423 switch (**ptr) {
1424 case '+':
1425 ++*ptr;
1426 if (get_flag(ptr, plus_flags))
1427 return -1;
1428 break;
1429 case '-':
1430 ++*ptr;
1431 if (get_flag(ptr, minus_flags))
1432 return -1;
1433 break;
1434 case ' ':
1435 ++*ptr;
1436 break;
1437 default:
1438 return 0;
1439 }
1440 }
1441}
1442
1443#define ITRACE_DFLT_LOG_ON_ERROR_SZ 16384
1444
1445static unsigned int itrace_log_on_error_size(void)
1446{
1447 unsigned int sz = 0;
1448
1449 perf_config_scan("itrace.debug-log-buffer-size", "%u", &sz);
1450 return sz ?: ITRACE_DFLT_LOG_ON_ERROR_SZ;
1451}
1452
1453/*
1454 * Please check tools/perf/Documentation/perf-script.txt for information
1455 * about the options parsed here, which is introduced after this cset,
1456 * when support in 'perf script' for these options is introduced.
1457 */
1458int itrace_do_parse_synth_opts(struct itrace_synth_opts *synth_opts,
1459 const char *str, int unset)
1460{
1461 const char *p;
1462 char *endptr;
1463 bool period_type_set = false;
1464 bool period_set = false;
1465
1466 synth_opts->set = true;
1467
1468 if (unset) {
1469 synth_opts->dont_decode = true;
1470 return 0;
1471 }
1472
1473 if (!str) {
1474 itrace_synth_opts__set_default(synth_opts,
1475 synth_opts->default_no_sample);
1476 return 0;
1477 }
1478
1479 for (p = str; *p;) {
1480 switch (*p++) {
1481 case 'i':
1482 synth_opts->instructions = true;
1483 while (*p == ' ' || *p == ',')
1484 p += 1;
1485 if (isdigit(*p)) {
1486 synth_opts->period = strtoull(p, &endptr, 10);
1487 period_set = true;
1488 p = endptr;
1489 while (*p == ' ' || *p == ',')
1490 p += 1;
1491 switch (*p++) {
1492 case 'i':
1493 synth_opts->period_type =
1494 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1495 period_type_set = true;
1496 break;
1497 case 't':
1498 synth_opts->period_type =
1499 PERF_ITRACE_PERIOD_TICKS;
1500 period_type_set = true;
1501 break;
1502 case 'm':
1503 synth_opts->period *= 1000;
1504 /* Fall through */
1505 case 'u':
1506 synth_opts->period *= 1000;
1507 /* Fall through */
1508 case 'n':
1509 if (*p++ != 's')
1510 goto out_err;
1511 synth_opts->period_type =
1512 PERF_ITRACE_PERIOD_NANOSECS;
1513 period_type_set = true;
1514 break;
1515 case '\0':
1516 goto out;
1517 default:
1518 goto out_err;
1519 }
1520 }
1521 break;
1522 case 'b':
1523 synth_opts->branches = true;
1524 break;
1525 case 'x':
1526 synth_opts->transactions = true;
1527 break;
1528 case 'w':
1529 synth_opts->ptwrites = true;
1530 break;
1531 case 'p':
1532 synth_opts->pwr_events = true;
1533 break;
1534 case 'o':
1535 synth_opts->other_events = true;
1536 break;
1537 case 'I':
1538 synth_opts->intr_events = true;
1539 break;
1540 case 'e':
1541 synth_opts->errors = true;
1542 if (get_flags(&p, &synth_opts->error_plus_flags,
1543 &synth_opts->error_minus_flags))
1544 goto out_err;
1545 break;
1546 case 'd':
1547 synth_opts->log = true;
1548 if (get_flags(&p, &synth_opts->log_plus_flags,
1549 &synth_opts->log_minus_flags))
1550 goto out_err;
1551 if (synth_opts->log_plus_flags & AUXTRACE_LOG_FLG_ON_ERROR)
1552 synth_opts->log_on_error_size = itrace_log_on_error_size();
1553 break;
1554 case 'c':
1555 synth_opts->branches = true;
1556 synth_opts->calls = true;
1557 break;
1558 case 'r':
1559 synth_opts->branches = true;
1560 synth_opts->returns = true;
1561 break;
1562 case 'G':
1563 case 'g':
1564 if (p[-1] == 'G')
1565 synth_opts->add_callchain = true;
1566 else
1567 synth_opts->callchain = true;
1568 synth_opts->callchain_sz =
1569 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1570 while (*p == ' ' || *p == ',')
1571 p += 1;
1572 if (isdigit(*p)) {
1573 unsigned int val;
1574
1575 val = strtoul(p, &endptr, 10);
1576 p = endptr;
1577 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1578 goto out_err;
1579 synth_opts->callchain_sz = val;
1580 }
1581 break;
1582 case 'L':
1583 case 'l':
1584 if (p[-1] == 'L')
1585 synth_opts->add_last_branch = true;
1586 else
1587 synth_opts->last_branch = true;
1588 synth_opts->last_branch_sz =
1589 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1590 while (*p == ' ' || *p == ',')
1591 p += 1;
1592 if (isdigit(*p)) {
1593 unsigned int val;
1594
1595 val = strtoul(p, &endptr, 10);
1596 p = endptr;
1597 if (!val ||
1598 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1599 goto out_err;
1600 synth_opts->last_branch_sz = val;
1601 }
1602 break;
1603 case 's':
1604 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1605 if (p == endptr)
1606 goto out_err;
1607 p = endptr;
1608 break;
1609 case 'f':
1610 synth_opts->flc = true;
1611 break;
1612 case 'm':
1613 synth_opts->llc = true;
1614 break;
1615 case 't':
1616 synth_opts->tlb = true;
1617 break;
1618 case 'a':
1619 synth_opts->remote_access = true;
1620 break;
1621 case 'M':
1622 synth_opts->mem = true;
1623 break;
1624 case 'q':
1625 synth_opts->quick += 1;
1626 break;
1627 case 'A':
1628 synth_opts->approx_ipc = true;
1629 break;
1630 case 'Z':
1631 synth_opts->timeless_decoding = true;
1632 break;
1633 case ' ':
1634 case ',':
1635 break;
1636 default:
1637 goto out_err;
1638 }
1639 }
1640out:
1641 if (synth_opts->instructions) {
1642 if (!period_type_set)
1643 synth_opts->period_type =
1644 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1645 if (!period_set)
1646 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1647 }
1648
1649 return 0;
1650
1651out_err:
1652 pr_err("Bad Instruction Tracing options '%s'\n", str);
1653 return -EINVAL;
1654}
1655
1656int itrace_parse_synth_opts(const struct option *opt, const char *str, int unset)
1657{
1658 return itrace_do_parse_synth_opts(opt->value, str, unset);
1659}
1660
1661static const char * const auxtrace_error_type_name[] = {
1662 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1663};
1664
1665static const char *auxtrace_error_name(int type)
1666{
1667 const char *error_type_name = NULL;
1668
1669 if (type < PERF_AUXTRACE_ERROR_MAX)
1670 error_type_name = auxtrace_error_type_name[type];
1671 if (!error_type_name)
1672 error_type_name = "unknown AUX";
1673 return error_type_name;
1674}
1675
1676size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1677{
1678 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1679 unsigned long long nsecs = e->time;
1680 const char *msg = e->msg;
1681 int ret;
1682
1683 ret = fprintf(fp, " %s error type %u",
1684 auxtrace_error_name(e->type), e->type);
1685
1686 if (e->fmt && nsecs) {
1687 unsigned long secs = nsecs / NSEC_PER_SEC;
1688
1689 nsecs -= secs * NSEC_PER_SEC;
1690 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1691 } else {
1692 ret += fprintf(fp, " time 0");
1693 }
1694
1695 if (!e->fmt)
1696 msg = (const char *)&e->time;
1697
1698 if (e->fmt >= 2 && e->machine_pid)
1699 ret += fprintf(fp, " machine_pid %d vcpu %d", e->machine_pid, e->vcpu);
1700
1701 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
1702 e->cpu, e->pid, e->tid, e->ip, e->code, msg);
1703 return ret;
1704}
1705
1706void perf_session__auxtrace_error_inc(struct perf_session *session,
1707 union perf_event *event)
1708{
1709 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1710
1711 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1712 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1713}
1714
1715void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1716{
1717 int i;
1718
1719 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1720 if (!stats->nr_auxtrace_errors[i])
1721 continue;
1722 ui__warning("%u %s errors\n",
1723 stats->nr_auxtrace_errors[i],
1724 auxtrace_error_name(i));
1725 }
1726}
1727
1728int perf_event__process_auxtrace_error(struct perf_session *session,
1729 union perf_event *event)
1730{
1731 if (auxtrace__dont_decode(session))
1732 return 0;
1733
1734 perf_event__fprintf_auxtrace_error(event, stdout);
1735 return 0;
1736}
1737
1738/*
1739 * In the compat mode kernel runs in 64-bit and perf tool runs in 32-bit mode,
1740 * 32-bit perf tool cannot access 64-bit value atomically, which might lead to
1741 * the issues caused by the below sequence on multiple CPUs: when perf tool
1742 * accesses either the load operation or the store operation for 64-bit value,
1743 * on some architectures the operation is divided into two instructions, one
1744 * is for accessing the low 32-bit value and another is for the high 32-bit;
1745 * thus these two user operations can give the kernel chances to access the
1746 * 64-bit value, and thus leads to the unexpected load values.
1747 *
1748 * kernel (64-bit) user (32-bit)
1749 *
1750 * if (LOAD ->aux_tail) { --, LOAD ->aux_head_lo
1751 * STORE $aux_data | ,--->
1752 * FLUSH $aux_data | | LOAD ->aux_head_hi
1753 * STORE ->aux_head --|-------` smp_rmb()
1754 * } | LOAD $data
1755 * | smp_mb()
1756 * | STORE ->aux_tail_lo
1757 * `----------->
1758 * STORE ->aux_tail_hi
1759 *
1760 * For this reason, it's impossible for the perf tool to work correctly when
1761 * the AUX head or tail is bigger than 4GB (more than 32 bits length); and we
1762 * can not simply limit the AUX ring buffer to less than 4GB, the reason is
1763 * the pointers can be increased monotonically, whatever the buffer size it is,
1764 * at the end the head and tail can be bigger than 4GB and carry out to the
1765 * high 32-bit.
1766 *
1767 * To mitigate the issues and improve the user experience, we can allow the
1768 * perf tool working in certain conditions and bail out with error if detect
1769 * any overflow cannot be handled.
1770 *
1771 * For reading the AUX head, it reads out the values for three times, and
1772 * compares the high 4 bytes of the values between the first time and the last
1773 * time, if there has no change for high 4 bytes injected by the kernel during
1774 * the user reading sequence, it's safe for use the second value.
1775 *
1776 * When compat_auxtrace_mmap__write_tail() detects any carrying in the high
1777 * 32 bits, it means there have two store operations in user space and it cannot
1778 * promise the atomicity for 64-bit write, so return '-1' in this case to tell
1779 * the caller an overflow error has happened.
1780 */
1781u64 __weak compat_auxtrace_mmap__read_head(struct auxtrace_mmap *mm)
1782{
1783 struct perf_event_mmap_page *pc = mm->userpg;
1784 u64 first, second, last;
1785 u64 mask = (u64)(UINT32_MAX) << 32;
1786
1787 do {
1788 first = READ_ONCE(pc->aux_head);
1789 /* Ensure all reads are done after we read the head */
1790 smp_rmb();
1791 second = READ_ONCE(pc->aux_head);
1792 /* Ensure all reads are done after we read the head */
1793 smp_rmb();
1794 last = READ_ONCE(pc->aux_head);
1795 } while ((first & mask) != (last & mask));
1796
1797 return second;
1798}
1799
1800int __weak compat_auxtrace_mmap__write_tail(struct auxtrace_mmap *mm, u64 tail)
1801{
1802 struct perf_event_mmap_page *pc = mm->userpg;
1803 u64 mask = (u64)(UINT32_MAX) << 32;
1804
1805 if (tail & mask)
1806 return -1;
1807
1808 /* Ensure all reads are done before we write the tail out */
1809 smp_mb();
1810 WRITE_ONCE(pc->aux_tail, tail);
1811 return 0;
1812}
1813
1814static int __auxtrace_mmap__read(struct mmap *map,
1815 struct auxtrace_record *itr,
1816 struct perf_tool *tool, process_auxtrace_t fn,
1817 bool snapshot, size_t snapshot_size)
1818{
1819 struct auxtrace_mmap *mm = &map->auxtrace_mmap;
1820 u64 head, old = mm->prev, offset, ref;
1821 unsigned char *data = mm->base;
1822 size_t size, head_off, old_off, len1, len2, padding;
1823 union perf_event ev;
1824 void *data1, *data2;
1825 int kernel_is_64_bit = perf_env__kernel_is_64_bit(evsel__env(NULL));
1826
1827 head = auxtrace_mmap__read_head(mm, kernel_is_64_bit);
1828
1829 if (snapshot &&
1830 auxtrace_record__find_snapshot(itr, mm->idx, mm, data, &head, &old))
1831 return -1;
1832
1833 if (old == head)
1834 return 0;
1835
1836 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1837 mm->idx, old, head, head - old);
1838
1839 if (mm->mask) {
1840 head_off = head & mm->mask;
1841 old_off = old & mm->mask;
1842 } else {
1843 head_off = head % mm->len;
1844 old_off = old % mm->len;
1845 }
1846
1847 if (head_off > old_off)
1848 size = head_off - old_off;
1849 else
1850 size = mm->len - (old_off - head_off);
1851
1852 if (snapshot && size > snapshot_size)
1853 size = snapshot_size;
1854
1855 ref = auxtrace_record__reference(itr);
1856
1857 if (head > old || size <= head || mm->mask) {
1858 offset = head - size;
1859 } else {
1860 /*
1861 * When the buffer size is not a power of 2, 'head' wraps at the
1862 * highest multiple of the buffer size, so we have to subtract
1863 * the remainder here.
1864 */
1865 u64 rem = (0ULL - mm->len) % mm->len;
1866
1867 offset = head - size - rem;
1868 }
1869
1870 if (size > head_off) {
1871 len1 = size - head_off;
1872 data1 = &data[mm->len - len1];
1873 len2 = head_off;
1874 data2 = &data[0];
1875 } else {
1876 len1 = size;
1877 data1 = &data[head_off - len1];
1878 len2 = 0;
1879 data2 = NULL;
1880 }
1881
1882 if (itr->alignment) {
1883 unsigned int unwanted = len1 % itr->alignment;
1884
1885 len1 -= unwanted;
1886 size -= unwanted;
1887 }
1888
1889 /* padding must be written by fn() e.g. record__process_auxtrace() */
1890 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1891 if (padding)
1892 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1893
1894 memset(&ev, 0, sizeof(ev));
1895 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1896 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1897 ev.auxtrace.size = size + padding;
1898 ev.auxtrace.offset = offset;
1899 ev.auxtrace.reference = ref;
1900 ev.auxtrace.idx = mm->idx;
1901 ev.auxtrace.tid = mm->tid;
1902 ev.auxtrace.cpu = mm->cpu;
1903
1904 if (fn(tool, map, &ev, data1, len1, data2, len2))
1905 return -1;
1906
1907 mm->prev = head;
1908
1909 if (!snapshot) {
1910 int err;
1911
1912 err = auxtrace_mmap__write_tail(mm, head, kernel_is_64_bit);
1913 if (err < 0)
1914 return err;
1915
1916 if (itr->read_finish) {
1917 err = itr->read_finish(itr, mm->idx);
1918 if (err < 0)
1919 return err;
1920 }
1921 }
1922
1923 return 1;
1924}
1925
1926int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
1927 struct perf_tool *tool, process_auxtrace_t fn)
1928{
1929 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
1930}
1931
1932int auxtrace_mmap__read_snapshot(struct mmap *map,
1933 struct auxtrace_record *itr,
1934 struct perf_tool *tool, process_auxtrace_t fn,
1935 size_t snapshot_size)
1936{
1937 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
1938}
1939
1940/**
1941 * struct auxtrace_cache - hash table to implement a cache
1942 * @hashtable: the hashtable
1943 * @sz: hashtable size (number of hlists)
1944 * @entry_size: size of an entry
1945 * @limit: limit the number of entries to this maximum, when reached the cache
1946 * is dropped and caching begins again with an empty cache
1947 * @cnt: current number of entries
1948 * @bits: hashtable size (@sz = 2^@bits)
1949 */
1950struct auxtrace_cache {
1951 struct hlist_head *hashtable;
1952 size_t sz;
1953 size_t entry_size;
1954 size_t limit;
1955 size_t cnt;
1956 unsigned int bits;
1957};
1958
1959struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1960 unsigned int limit_percent)
1961{
1962 struct auxtrace_cache *c;
1963 struct hlist_head *ht;
1964 size_t sz, i;
1965
1966 c = zalloc(sizeof(struct auxtrace_cache));
1967 if (!c)
1968 return NULL;
1969
1970 sz = 1UL << bits;
1971
1972 ht = calloc(sz, sizeof(struct hlist_head));
1973 if (!ht)
1974 goto out_free;
1975
1976 for (i = 0; i < sz; i++)
1977 INIT_HLIST_HEAD(&ht[i]);
1978
1979 c->hashtable = ht;
1980 c->sz = sz;
1981 c->entry_size = entry_size;
1982 c->limit = (c->sz * limit_percent) / 100;
1983 c->bits = bits;
1984
1985 return c;
1986
1987out_free:
1988 free(c);
1989 return NULL;
1990}
1991
1992static void auxtrace_cache__drop(struct auxtrace_cache *c)
1993{
1994 struct auxtrace_cache_entry *entry;
1995 struct hlist_node *tmp;
1996 size_t i;
1997
1998 if (!c)
1999 return;
2000
2001 for (i = 0; i < c->sz; i++) {
2002 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
2003 hlist_del(&entry->hash);
2004 auxtrace_cache__free_entry(c, entry);
2005 }
2006 }
2007
2008 c->cnt = 0;
2009}
2010
2011void auxtrace_cache__free(struct auxtrace_cache *c)
2012{
2013 if (!c)
2014 return;
2015
2016 auxtrace_cache__drop(c);
2017 zfree(&c->hashtable);
2018 free(c);
2019}
2020
2021void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
2022{
2023 return malloc(c->entry_size);
2024}
2025
2026void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
2027 void *entry)
2028{
2029 free(entry);
2030}
2031
2032int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
2033 struct auxtrace_cache_entry *entry)
2034{
2035 if (c->limit && ++c->cnt > c->limit)
2036 auxtrace_cache__drop(c);
2037
2038 entry->key = key;
2039 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
2040
2041 return 0;
2042}
2043
2044static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
2045 u32 key)
2046{
2047 struct auxtrace_cache_entry *entry;
2048 struct hlist_head *hlist;
2049 struct hlist_node *n;
2050
2051 if (!c)
2052 return NULL;
2053
2054 hlist = &c->hashtable[hash_32(key, c->bits)];
2055 hlist_for_each_entry_safe(entry, n, hlist, hash) {
2056 if (entry->key == key) {
2057 hlist_del(&entry->hash);
2058 return entry;
2059 }
2060 }
2061
2062 return NULL;
2063}
2064
2065void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
2066{
2067 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);
2068
2069 auxtrace_cache__free_entry(c, entry);
2070}
2071
2072void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
2073{
2074 struct auxtrace_cache_entry *entry;
2075 struct hlist_head *hlist;
2076
2077 if (!c)
2078 return NULL;
2079
2080 hlist = &c->hashtable[hash_32(key, c->bits)];
2081 hlist_for_each_entry(entry, hlist, hash) {
2082 if (entry->key == key)
2083 return entry;
2084 }
2085
2086 return NULL;
2087}
2088
2089static void addr_filter__free_str(struct addr_filter *filt)
2090{
2091 zfree(&filt->str);
2092 filt->action = NULL;
2093 filt->sym_from = NULL;
2094 filt->sym_to = NULL;
2095 filt->filename = NULL;
2096}
2097
2098static struct addr_filter *addr_filter__new(void)
2099{
2100 struct addr_filter *filt = zalloc(sizeof(*filt));
2101
2102 if (filt)
2103 INIT_LIST_HEAD(&filt->list);
2104
2105 return filt;
2106}
2107
2108static void addr_filter__free(struct addr_filter *filt)
2109{
2110 if (filt)
2111 addr_filter__free_str(filt);
2112 free(filt);
2113}
2114
2115static void addr_filters__add(struct addr_filters *filts,
2116 struct addr_filter *filt)
2117{
2118 list_add_tail(&filt->list, &filts->head);
2119 filts->cnt += 1;
2120}
2121
2122static void addr_filters__del(struct addr_filters *filts,
2123 struct addr_filter *filt)
2124{
2125 list_del_init(&filt->list);
2126 filts->cnt -= 1;
2127}
2128
2129void addr_filters__init(struct addr_filters *filts)
2130{
2131 INIT_LIST_HEAD(&filts->head);
2132 filts->cnt = 0;
2133}
2134
2135void addr_filters__exit(struct addr_filters *filts)
2136{
2137 struct addr_filter *filt, *n;
2138
2139 list_for_each_entry_safe(filt, n, &filts->head, list) {
2140 addr_filters__del(filts, filt);
2141 addr_filter__free(filt);
2142 }
2143}
2144
2145static int parse_num_or_str(char **inp, u64 *num, const char **str,
2146 const char *str_delim)
2147{
2148 *inp += strspn(*inp, " ");
2149
2150 if (isdigit(**inp)) {
2151 char *endptr;
2152
2153 if (!num)
2154 return -EINVAL;
2155 errno = 0;
2156 *num = strtoull(*inp, &endptr, 0);
2157 if (errno)
2158 return -errno;
2159 if (endptr == *inp)
2160 return -EINVAL;
2161 *inp = endptr;
2162 } else {
2163 size_t n;
2164
2165 if (!str)
2166 return -EINVAL;
2167 *inp += strspn(*inp, " ");
2168 *str = *inp;
2169 n = strcspn(*inp, str_delim);
2170 if (!n)
2171 return -EINVAL;
2172 *inp += n;
2173 if (**inp) {
2174 **inp = '\0';
2175 *inp += 1;
2176 }
2177 }
2178 return 0;
2179}
2180
2181static int parse_action(struct addr_filter *filt)
2182{
2183 if (!strcmp(filt->action, "filter")) {
2184 filt->start = true;
2185 filt->range = true;
2186 } else if (!strcmp(filt->action, "start")) {
2187 filt->start = true;
2188 } else if (!strcmp(filt->action, "stop")) {
2189 filt->start = false;
2190 } else if (!strcmp(filt->action, "tracestop")) {
2191 filt->start = false;
2192 filt->range = true;
2193 filt->action += 5; /* Change 'tracestop' to 'stop' */
2194 } else {
2195 return -EINVAL;
2196 }
2197 return 0;
2198}
2199
2200static int parse_sym_idx(char **inp, int *idx)
2201{
2202 *idx = -1;
2203
2204 *inp += strspn(*inp, " ");
2205
2206 if (**inp != '#')
2207 return 0;
2208
2209 *inp += 1;
2210
2211 if (**inp == 'g' || **inp == 'G') {
2212 *inp += 1;
2213 *idx = 0;
2214 } else {
2215 unsigned long num;
2216 char *endptr;
2217
2218 errno = 0;
2219 num = strtoul(*inp, &endptr, 0);
2220 if (errno)
2221 return -errno;
2222 if (endptr == *inp || num > INT_MAX)
2223 return -EINVAL;
2224 *inp = endptr;
2225 *idx = num;
2226 }
2227
2228 return 0;
2229}
2230
2231static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
2232{
2233 int err = parse_num_or_str(inp, num, str, " ");
2234
2235 if (!err && *str)
2236 err = parse_sym_idx(inp, idx);
2237
2238 return err;
2239}
2240
2241static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
2242{
2243 char *fstr;
2244 int err;
2245
2246 filt->str = fstr = strdup(*filter_inp);
2247 if (!fstr)
2248 return -ENOMEM;
2249
2250 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
2251 if (err)
2252 goto out_err;
2253
2254 err = parse_action(filt);
2255 if (err)
2256 goto out_err;
2257
2258 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
2259 &filt->sym_from_idx);
2260 if (err)
2261 goto out_err;
2262
2263 fstr += strspn(fstr, " ");
2264
2265 if (*fstr == '/') {
2266 fstr += 1;
2267 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
2268 &filt->sym_to_idx);
2269 if (err)
2270 goto out_err;
2271 filt->range = true;
2272 }
2273
2274 fstr += strspn(fstr, " ");
2275
2276 if (*fstr == '@') {
2277 fstr += 1;
2278 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
2279 if (err)
2280 goto out_err;
2281 }
2282
2283 fstr += strspn(fstr, " ,");
2284
2285 *filter_inp += fstr - filt->str;
2286
2287 return 0;
2288
2289out_err:
2290 addr_filter__free_str(filt);
2291
2292 return err;
2293}
2294
2295int addr_filters__parse_bare_filter(struct addr_filters *filts,
2296 const char *filter)
2297{
2298 struct addr_filter *filt;
2299 const char *fstr = filter;
2300 int err;
2301
2302 while (*fstr) {
2303 filt = addr_filter__new();
2304 err = parse_one_filter(filt, &fstr);
2305 if (err) {
2306 addr_filter__free(filt);
2307 addr_filters__exit(filts);
2308 return err;
2309 }
2310 addr_filters__add(filts, filt);
2311 }
2312
2313 return 0;
2314}
2315
2316struct sym_args {
2317 const char *name;
2318 u64 start;
2319 u64 size;
2320 int idx;
2321 int cnt;
2322 bool started;
2323 bool global;
2324 bool selected;
2325 bool duplicate;
2326 bool near;
2327};
2328
2329static bool kern_sym_name_match(const char *kname, const char *name)
2330{
2331 size_t n = strlen(name);
2332
2333 return !strcmp(kname, name) ||
2334 (!strncmp(kname, name, n) && kname[n] == '\t');
2335}
2336
2337static bool kern_sym_match(struct sym_args *args, const char *name, char type)
2338{
2339 /* A function with the same name, and global or the n'th found or any */
2340 return kallsyms__is_function(type) &&
2341 kern_sym_name_match(name, args->name) &&
2342 ((args->global && isupper(type)) ||
2343 (args->selected && ++(args->cnt) == args->idx) ||
2344 (!args->global && !args->selected));
2345}
2346
2347static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2348{
2349 struct sym_args *args = arg;
2350
2351 if (args->started) {
2352 if (!args->size)
2353 args->size = start - args->start;
2354 if (args->selected) {
2355 if (args->size)
2356 return 1;
2357 } else if (kern_sym_match(args, name, type)) {
2358 args->duplicate = true;
2359 return 1;
2360 }
2361 } else if (kern_sym_match(args, name, type)) {
2362 args->started = true;
2363 args->start = start;
2364 }
2365
2366 return 0;
2367}
2368
2369static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2370{
2371 struct sym_args *args = arg;
2372
2373 if (kern_sym_match(args, name, type)) {
2374 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2375 ++args->cnt, start, type, name);
2376 args->near = true;
2377 } else if (args->near) {
2378 args->near = false;
2379 pr_err("\t\twhich is near\t\t%s\n", name);
2380 }
2381
2382 return 0;
2383}
2384
2385static int sym_not_found_error(const char *sym_name, int idx)
2386{
2387 if (idx > 0) {
2388 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
2389 idx, sym_name);
2390 } else if (!idx) {
2391 pr_err("Global symbol '%s' not found.\n", sym_name);
2392 } else {
2393 pr_err("Symbol '%s' not found.\n", sym_name);
2394 }
2395 pr_err("Note that symbols must be functions.\n");
2396
2397 return -EINVAL;
2398}
2399
2400static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
2401{
2402 struct sym_args args = {
2403 .name = sym_name,
2404 .idx = idx,
2405 .global = !idx,
2406 .selected = idx > 0,
2407 };
2408 int err;
2409
2410 *start = 0;
2411 *size = 0;
2412
2413 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
2414 if (err < 0) {
2415 pr_err("Failed to parse /proc/kallsyms\n");
2416 return err;
2417 }
2418
2419 if (args.duplicate) {
2420 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
2421 args.cnt = 0;
2422 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
2423 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2424 sym_name);
2425 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2426 return -EINVAL;
2427 }
2428
2429 if (!args.started) {
2430 pr_err("Kernel symbol lookup: ");
2431 return sym_not_found_error(sym_name, idx);
2432 }
2433
2434 *start = args.start;
2435 *size = args.size;
2436
2437 return 0;
2438}
2439
2440static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
2441 char type, u64 start)
2442{
2443 struct sym_args *args = arg;
2444
2445 if (!kallsyms__is_function(type))
2446 return 0;
2447
2448 if (!args->started) {
2449 args->started = true;
2450 args->start = start;
2451 }
2452 /* Don't know exactly where the kernel ends, so we add a page */
2453 args->size = round_up(start, page_size) + page_size - args->start;
2454
2455 return 0;
2456}
2457
2458static int addr_filter__entire_kernel(struct addr_filter *filt)
2459{
2460 struct sym_args args = { .started = false };
2461 int err;
2462
2463 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
2464 if (err < 0 || !args.started) {
2465 pr_err("Failed to parse /proc/kallsyms\n");
2466 return err;
2467 }
2468
2469 filt->addr = args.start;
2470 filt->size = args.size;
2471
2472 return 0;
2473}
2474
2475static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
2476{
2477 if (start + size >= filt->addr)
2478 return 0;
2479
2480 if (filt->sym_from) {
2481 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
2482 filt->sym_to, start, filt->sym_from, filt->addr);
2483 } else {
2484 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
2485 filt->sym_to, start, filt->addr);
2486 }
2487
2488 return -EINVAL;
2489}
2490
2491static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
2492{
2493 bool no_size = false;
2494 u64 start, size;
2495 int err;
2496
2497 if (symbol_conf.kptr_restrict) {
2498 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
2499 return -EINVAL;
2500 }
2501
2502 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
2503 return addr_filter__entire_kernel(filt);
2504
2505 if (filt->sym_from) {
2506 err = find_kern_sym(filt->sym_from, &start, &size,
2507 filt->sym_from_idx);
2508 if (err)
2509 return err;
2510 filt->addr = start;
2511 if (filt->range && !filt->size && !filt->sym_to) {
2512 filt->size = size;
2513 no_size = !size;
2514 }
2515 }
2516
2517 if (filt->sym_to) {
2518 err = find_kern_sym(filt->sym_to, &start, &size,
2519 filt->sym_to_idx);
2520 if (err)
2521 return err;
2522
2523 err = check_end_after_start(filt, start, size);
2524 if (err)
2525 return err;
2526 filt->size = start + size - filt->addr;
2527 no_size = !size;
2528 }
2529
2530 /* The very last symbol in kallsyms does not imply a particular size */
2531 if (no_size) {
2532 pr_err("Cannot determine size of symbol '%s'\n",
2533 filt->sym_to ? filt->sym_to : filt->sym_from);
2534 return -EINVAL;
2535 }
2536
2537 return 0;
2538}
2539
2540static struct dso *load_dso(const char *name)
2541{
2542 struct map *map;
2543 struct dso *dso;
2544
2545 map = dso__new_map(name);
2546 if (!map)
2547 return NULL;
2548
2549 if (map__load(map) < 0)
2550 pr_err("File '%s' not found or has no symbols.\n", name);
2551
2552 dso = dso__get(map->dso);
2553
2554 map__put(map);
2555
2556 return dso;
2557}
2558
2559static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
2560 int idx)
2561{
2562 /* Same name, and global or the n'th found or any */
2563 return !arch__compare_symbol_names(name, sym->name) &&
2564 ((!idx && sym->binding == STB_GLOBAL) ||
2565 (idx > 0 && ++*cnt == idx) ||
2566 idx < 0);
2567}
2568
2569static void print_duplicate_syms(struct dso *dso, const char *sym_name)
2570{
2571 struct symbol *sym;
2572 bool near = false;
2573 int cnt = 0;
2574
2575 pr_err("Multiple symbols with name '%s'\n", sym_name);
2576
2577 sym = dso__first_symbol(dso);
2578 while (sym) {
2579 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
2580 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2581 ++cnt, sym->start,
2582 sym->binding == STB_GLOBAL ? 'g' :
2583 sym->binding == STB_LOCAL ? 'l' : 'w',
2584 sym->name);
2585 near = true;
2586 } else if (near) {
2587 near = false;
2588 pr_err("\t\twhich is near\t\t%s\n", sym->name);
2589 }
2590 sym = dso__next_symbol(sym);
2591 }
2592
2593 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2594 sym_name);
2595 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2596}
2597
2598static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
2599 u64 *size, int idx)
2600{
2601 struct symbol *sym;
2602 int cnt = 0;
2603
2604 *start = 0;
2605 *size = 0;
2606
2607 sym = dso__first_symbol(dso);
2608 while (sym) {
2609 if (*start) {
2610 if (!*size)
2611 *size = sym->start - *start;
2612 if (idx > 0) {
2613 if (*size)
2614 return 0;
2615 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2616 print_duplicate_syms(dso, sym_name);
2617 return -EINVAL;
2618 }
2619 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2620 *start = sym->start;
2621 *size = sym->end - sym->start;
2622 }
2623 sym = dso__next_symbol(sym);
2624 }
2625
2626 if (!*start)
2627 return sym_not_found_error(sym_name, idx);
2628
2629 return 0;
2630}
2631
2632static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2633{
2634 if (dso__data_file_size(dso, NULL)) {
2635 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2636 filt->filename);
2637 return -EINVAL;
2638 }
2639
2640 filt->addr = 0;
2641 filt->size = dso->data.file_size;
2642
2643 return 0;
2644}
2645
2646static int addr_filter__resolve_syms(struct addr_filter *filt)
2647{
2648 u64 start, size;
2649 struct dso *dso;
2650 int err = 0;
2651
2652 if (!filt->sym_from && !filt->sym_to)
2653 return 0;
2654
2655 if (!filt->filename)
2656 return addr_filter__resolve_kernel_syms(filt);
2657
2658 dso = load_dso(filt->filename);
2659 if (!dso) {
2660 pr_err("Failed to load symbols from: %s\n", filt->filename);
2661 return -EINVAL;
2662 }
2663
2664 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2665 err = addr_filter__entire_dso(filt, dso);
2666 goto put_dso;
2667 }
2668
2669 if (filt->sym_from) {
2670 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2671 filt->sym_from_idx);
2672 if (err)
2673 goto put_dso;
2674 filt->addr = start;
2675 if (filt->range && !filt->size && !filt->sym_to)
2676 filt->size = size;
2677 }
2678
2679 if (filt->sym_to) {
2680 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2681 filt->sym_to_idx);
2682 if (err)
2683 goto put_dso;
2684
2685 err = check_end_after_start(filt, start, size);
2686 if (err)
2687 return err;
2688
2689 filt->size = start + size - filt->addr;
2690 }
2691
2692put_dso:
2693 dso__put(dso);
2694
2695 return err;
2696}
2697
2698static char *addr_filter__to_str(struct addr_filter *filt)
2699{
2700 char filename_buf[PATH_MAX];
2701 const char *at = "";
2702 const char *fn = "";
2703 char *filter;
2704 int err;
2705
2706 if (filt->filename) {
2707 at = "@";
2708 fn = realpath(filt->filename, filename_buf);
2709 if (!fn)
2710 return NULL;
2711 }
2712
2713 if (filt->range) {
2714 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2715 filt->action, filt->addr, filt->size, at, fn);
2716 } else {
2717 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2718 filt->action, filt->addr, at, fn);
2719 }
2720
2721 return err < 0 ? NULL : filter;
2722}
2723
2724static int parse_addr_filter(struct evsel *evsel, const char *filter,
2725 int max_nr)
2726{
2727 struct addr_filters filts;
2728 struct addr_filter *filt;
2729 int err;
2730
2731 addr_filters__init(&filts);
2732
2733 err = addr_filters__parse_bare_filter(&filts, filter);
2734 if (err)
2735 goto out_exit;
2736
2737 if (filts.cnt > max_nr) {
2738 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2739 filts.cnt, max_nr);
2740 err = -EINVAL;
2741 goto out_exit;
2742 }
2743
2744 list_for_each_entry(filt, &filts.head, list) {
2745 char *new_filter;
2746
2747 err = addr_filter__resolve_syms(filt);
2748 if (err)
2749 goto out_exit;
2750
2751 new_filter = addr_filter__to_str(filt);
2752 if (!new_filter) {
2753 err = -ENOMEM;
2754 goto out_exit;
2755 }
2756
2757 if (evsel__append_addr_filter(evsel, new_filter)) {
2758 err = -ENOMEM;
2759 goto out_exit;
2760 }
2761 }
2762
2763out_exit:
2764 addr_filters__exit(&filts);
2765
2766 if (err) {
2767 pr_err("Failed to parse address filter: '%s'\n", filter);
2768 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2769 pr_err("Where multiple filters are separated by space or comma.\n");
2770 }
2771
2772 return err;
2773}
2774
2775static int evsel__nr_addr_filter(struct evsel *evsel)
2776{
2777 struct perf_pmu *pmu = evsel__find_pmu(evsel);
2778 int nr_addr_filters = 0;
2779
2780 if (!pmu)
2781 return 0;
2782
2783 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2784
2785 return nr_addr_filters;
2786}
2787
2788int auxtrace_parse_filters(struct evlist *evlist)
2789{
2790 struct evsel *evsel;
2791 char *filter;
2792 int err, max_nr;
2793
2794 evlist__for_each_entry(evlist, evsel) {
2795 filter = evsel->filter;
2796 max_nr = evsel__nr_addr_filter(evsel);
2797 if (!filter || !max_nr)
2798 continue;
2799 evsel->filter = NULL;
2800 err = parse_addr_filter(evsel, filter, max_nr);
2801 free(filter);
2802 if (err)
2803 return err;
2804 pr_debug("Address filter: %s\n", evsel->filter);
2805 }
2806
2807 return 0;
2808}
2809
2810int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2811 struct perf_sample *sample, struct perf_tool *tool)
2812{
2813 if (!session->auxtrace)
2814 return 0;
2815
2816 return session->auxtrace->process_event(session, event, sample, tool);
2817}
2818
2819void auxtrace__dump_auxtrace_sample(struct perf_session *session,
2820 struct perf_sample *sample)
2821{
2822 if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
2823 auxtrace__dont_decode(session))
2824 return;
2825
2826 session->auxtrace->dump_auxtrace_sample(session, sample);
2827}
2828
2829int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2830{
2831 if (!session->auxtrace)
2832 return 0;
2833
2834 return session->auxtrace->flush_events(session, tool);
2835}
2836
2837void auxtrace__free_events(struct perf_session *session)
2838{
2839 if (!session->auxtrace)
2840 return;
2841
2842 return session->auxtrace->free_events(session);
2843}
2844
2845void auxtrace__free(struct perf_session *session)
2846{
2847 if (!session->auxtrace)
2848 return;
2849
2850 return session->auxtrace->free(session);
2851}
2852
2853bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
2854 struct evsel *evsel)
2855{
2856 if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
2857 return false;
2858
2859 return session->auxtrace->evsel_is_auxtrace(session, evsel);
2860}