Merge tag 'block-5.9-2020-08-14' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / perf / util / auxtrace.c
blob42a85c86421d73d7da55f9f1e87e40de4fca291e
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
5 */
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>
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>
23 #include <sys/param.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <linux/list.h>
27 #include <linux/zalloc.h>
29 #include "evlist.h"
30 #include "dso.h"
31 #include "map.h"
32 #include "pmu.h"
33 #include "evsel.h"
34 #include "evsel_config.h"
35 #include "symbol.h"
36 #include "util/perf_api_probe.h"
37 #include "util/synthetic-events.h"
38 #include "thread_map.h"
39 #include "asm/bug.h"
40 #include "auxtrace.h"
42 #include <linux/hash.h>
44 #include "event.h"
45 #include "record.h"
46 #include "session.h"
47 #include "debug.h"
48 #include <subcmd/parse-options.h>
50 #include "cs-etm.h"
51 #include "intel-pt.h"
52 #include "intel-bts.h"
53 #include "arm-spe.h"
54 #include "s390-cpumsf.h"
55 #include "util/mmap.h"
57 #include <linux/ctype.h>
58 #include "symbol/kallsyms.h"
59 #include <internal/lib.h>
62 * Make a group from 'leader' to 'last', requiring that the events were not
63 * already grouped to a different leader.
65 static int perf_evlist__regroup(struct evlist *evlist,
66 struct evsel *leader,
67 struct evsel *last)
69 struct evsel *evsel;
70 bool grp;
72 if (!evsel__is_group_leader(leader))
73 return -EINVAL;
75 grp = false;
76 evlist__for_each_entry(evlist, evsel) {
77 if (grp) {
78 if (!(evsel->leader == leader ||
79 (evsel->leader == evsel &&
80 evsel->core.nr_members <= 1)))
81 return -EINVAL;
82 } else if (evsel == leader) {
83 grp = true;
85 if (evsel == last)
86 break;
89 grp = false;
90 evlist__for_each_entry(evlist, evsel) {
91 if (grp) {
92 if (evsel->leader != leader) {
93 evsel->leader = leader;
94 if (leader->core.nr_members < 1)
95 leader->core.nr_members = 1;
96 leader->core.nr_members += 1;
98 } else if (evsel == leader) {
99 grp = true;
101 if (evsel == last)
102 break;
105 return 0;
108 static bool auxtrace__dont_decode(struct perf_session *session)
110 return !session->itrace_synth_opts ||
111 session->itrace_synth_opts->dont_decode;
114 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
115 struct auxtrace_mmap_params *mp,
116 void *userpg, int fd)
118 struct perf_event_mmap_page *pc = userpg;
120 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
122 mm->userpg = userpg;
123 mm->mask = mp->mask;
124 mm->len = mp->len;
125 mm->prev = 0;
126 mm->idx = mp->idx;
127 mm->tid = mp->tid;
128 mm->cpu = mp->cpu;
130 if (!mp->len) {
131 mm->base = NULL;
132 return 0;
135 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
136 pr_err("Cannot use AUX area tracing mmaps\n");
137 return -1;
138 #endif
140 pc->aux_offset = mp->offset;
141 pc->aux_size = mp->len;
143 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
144 if (mm->base == MAP_FAILED) {
145 pr_debug2("failed to mmap AUX area\n");
146 mm->base = NULL;
147 return -1;
150 return 0;
153 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
155 if (mm->base) {
156 munmap(mm->base, mm->len);
157 mm->base = NULL;
161 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
162 off_t auxtrace_offset,
163 unsigned int auxtrace_pages,
164 bool auxtrace_overwrite)
166 if (auxtrace_pages) {
167 mp->offset = auxtrace_offset;
168 mp->len = auxtrace_pages * (size_t)page_size;
169 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
170 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
171 pr_debug2("AUX area mmap length %zu\n", mp->len);
172 } else {
173 mp->len = 0;
177 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
178 struct evlist *evlist, int idx,
179 bool per_cpu)
181 mp->idx = idx;
183 if (per_cpu) {
184 mp->cpu = evlist->core.cpus->map[idx];
185 if (evlist->core.threads)
186 mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
187 else
188 mp->tid = -1;
189 } else {
190 mp->cpu = -1;
191 mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
195 #define AUXTRACE_INIT_NR_QUEUES 32
197 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
199 struct auxtrace_queue *queue_array;
200 unsigned int max_nr_queues, i;
202 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
203 if (nr_queues > max_nr_queues)
204 return NULL;
206 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
207 if (!queue_array)
208 return NULL;
210 for (i = 0; i < nr_queues; i++) {
211 INIT_LIST_HEAD(&queue_array[i].head);
212 queue_array[i].priv = NULL;
215 return queue_array;
218 int auxtrace_queues__init(struct auxtrace_queues *queues)
220 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
221 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
222 if (!queues->queue_array)
223 return -ENOMEM;
224 return 0;
227 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
228 unsigned int new_nr_queues)
230 unsigned int nr_queues = queues->nr_queues;
231 struct auxtrace_queue *queue_array;
232 unsigned int i;
234 if (!nr_queues)
235 nr_queues = AUXTRACE_INIT_NR_QUEUES;
237 while (nr_queues && nr_queues < new_nr_queues)
238 nr_queues <<= 1;
240 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
241 return -EINVAL;
243 queue_array = auxtrace_alloc_queue_array(nr_queues);
244 if (!queue_array)
245 return -ENOMEM;
247 for (i = 0; i < queues->nr_queues; i++) {
248 list_splice_tail(&queues->queue_array[i].head,
249 &queue_array[i].head);
250 queue_array[i].tid = queues->queue_array[i].tid;
251 queue_array[i].cpu = queues->queue_array[i].cpu;
252 queue_array[i].set = queues->queue_array[i].set;
253 queue_array[i].priv = queues->queue_array[i].priv;
256 queues->nr_queues = nr_queues;
257 queues->queue_array = queue_array;
259 return 0;
262 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
264 int fd = perf_data__fd(session->data);
265 void *p;
266 ssize_t ret;
268 if (size > SSIZE_MAX)
269 return NULL;
271 p = malloc(size);
272 if (!p)
273 return NULL;
275 ret = readn(fd, p, size);
276 if (ret != (ssize_t)size) {
277 free(p);
278 return NULL;
281 return p;
284 static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
285 unsigned int idx,
286 struct auxtrace_buffer *buffer)
288 struct auxtrace_queue *queue;
289 int err;
291 if (idx >= queues->nr_queues) {
292 err = auxtrace_queues__grow(queues, idx + 1);
293 if (err)
294 return err;
297 queue = &queues->queue_array[idx];
299 if (!queue->set) {
300 queue->set = true;
301 queue->tid = buffer->tid;
302 queue->cpu = buffer->cpu;
303 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
304 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
305 queue->cpu, queue->tid, buffer->cpu, buffer->tid);
306 return -EINVAL;
309 buffer->buffer_nr = queues->next_buffer_nr++;
311 list_add_tail(&buffer->list, &queue->head);
313 queues->new_data = true;
314 queues->populated = true;
316 return 0;
319 /* Limit buffers to 32MiB on 32-bit */
320 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
322 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
323 unsigned int idx,
324 struct auxtrace_buffer *buffer)
326 u64 sz = buffer->size;
327 bool consecutive = false;
328 struct auxtrace_buffer *b;
329 int err;
331 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
332 b = memdup(buffer, sizeof(struct auxtrace_buffer));
333 if (!b)
334 return -ENOMEM;
335 b->size = BUFFER_LIMIT_FOR_32_BIT;
336 b->consecutive = consecutive;
337 err = auxtrace_queues__queue_buffer(queues, idx, b);
338 if (err) {
339 auxtrace_buffer__free(b);
340 return err;
342 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
343 sz -= BUFFER_LIMIT_FOR_32_BIT;
344 consecutive = true;
347 buffer->size = sz;
348 buffer->consecutive = consecutive;
350 return 0;
353 static bool filter_cpu(struct perf_session *session, int cpu)
355 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
357 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap);
360 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
361 struct perf_session *session,
362 unsigned int idx,
363 struct auxtrace_buffer *buffer,
364 struct auxtrace_buffer **buffer_ptr)
366 int err = -ENOMEM;
368 if (filter_cpu(session, buffer->cpu))
369 return 0;
371 buffer = memdup(buffer, sizeof(*buffer));
372 if (!buffer)
373 return -ENOMEM;
375 if (session->one_mmap) {
376 buffer->data = buffer->data_offset - session->one_mmap_offset +
377 session->one_mmap_addr;
378 } else if (perf_data__is_pipe(session->data)) {
379 buffer->data = auxtrace_copy_data(buffer->size, session);
380 if (!buffer->data)
381 goto out_free;
382 buffer->data_needs_freeing = true;
383 } else if (BITS_PER_LONG == 32 &&
384 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
385 err = auxtrace_queues__split_buffer(queues, idx, buffer);
386 if (err)
387 goto out_free;
390 err = auxtrace_queues__queue_buffer(queues, idx, buffer);
391 if (err)
392 goto out_free;
394 /* FIXME: Doesn't work for split buffer */
395 if (buffer_ptr)
396 *buffer_ptr = buffer;
398 return 0;
400 out_free:
401 auxtrace_buffer__free(buffer);
402 return err;
405 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
406 struct perf_session *session,
407 union perf_event *event, off_t data_offset,
408 struct auxtrace_buffer **buffer_ptr)
410 struct auxtrace_buffer buffer = {
411 .pid = -1,
412 .tid = event->auxtrace.tid,
413 .cpu = event->auxtrace.cpu,
414 .data_offset = data_offset,
415 .offset = event->auxtrace.offset,
416 .reference = event->auxtrace.reference,
417 .size = event->auxtrace.size,
419 unsigned int idx = event->auxtrace.idx;
421 return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
422 buffer_ptr);
425 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
426 struct perf_session *session,
427 off_t file_offset, size_t sz)
429 union perf_event *event;
430 int err;
431 char buf[PERF_SAMPLE_MAX_SIZE];
433 err = perf_session__peek_event(session, file_offset, buf,
434 PERF_SAMPLE_MAX_SIZE, &event, NULL);
435 if (err)
436 return err;
438 if (event->header.type == PERF_RECORD_AUXTRACE) {
439 if (event->header.size < sizeof(struct perf_record_auxtrace) ||
440 event->header.size != sz) {
441 err = -EINVAL;
442 goto out;
444 file_offset += event->header.size;
445 err = auxtrace_queues__add_event(queues, session, event,
446 file_offset, NULL);
448 out:
449 return err;
452 void auxtrace_queues__free(struct auxtrace_queues *queues)
454 unsigned int i;
456 for (i = 0; i < queues->nr_queues; i++) {
457 while (!list_empty(&queues->queue_array[i].head)) {
458 struct auxtrace_buffer *buffer;
460 buffer = list_entry(queues->queue_array[i].head.next,
461 struct auxtrace_buffer, list);
462 list_del_init(&buffer->list);
463 auxtrace_buffer__free(buffer);
467 zfree(&queues->queue_array);
468 queues->nr_queues = 0;
471 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
472 unsigned int pos, unsigned int queue_nr,
473 u64 ordinal)
475 unsigned int parent;
477 while (pos) {
478 parent = (pos - 1) >> 1;
479 if (heap_array[parent].ordinal <= ordinal)
480 break;
481 heap_array[pos] = heap_array[parent];
482 pos = parent;
484 heap_array[pos].queue_nr = queue_nr;
485 heap_array[pos].ordinal = ordinal;
488 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
489 u64 ordinal)
491 struct auxtrace_heap_item *heap_array;
493 if (queue_nr >= heap->heap_sz) {
494 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
496 while (heap_sz <= queue_nr)
497 heap_sz <<= 1;
498 heap_array = realloc(heap->heap_array,
499 heap_sz * sizeof(struct auxtrace_heap_item));
500 if (!heap_array)
501 return -ENOMEM;
502 heap->heap_array = heap_array;
503 heap->heap_sz = heap_sz;
506 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
508 return 0;
511 void auxtrace_heap__free(struct auxtrace_heap *heap)
513 zfree(&heap->heap_array);
514 heap->heap_cnt = 0;
515 heap->heap_sz = 0;
518 void auxtrace_heap__pop(struct auxtrace_heap *heap)
520 unsigned int pos, last, heap_cnt = heap->heap_cnt;
521 struct auxtrace_heap_item *heap_array;
523 if (!heap_cnt)
524 return;
526 heap->heap_cnt -= 1;
528 heap_array = heap->heap_array;
530 pos = 0;
531 while (1) {
532 unsigned int left, right;
534 left = (pos << 1) + 1;
535 if (left >= heap_cnt)
536 break;
537 right = left + 1;
538 if (right >= heap_cnt) {
539 heap_array[pos] = heap_array[left];
540 return;
542 if (heap_array[left].ordinal < heap_array[right].ordinal) {
543 heap_array[pos] = heap_array[left];
544 pos = left;
545 } else {
546 heap_array[pos] = heap_array[right];
547 pos = right;
551 last = heap_cnt - 1;
552 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
553 heap_array[last].ordinal);
556 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
557 struct evlist *evlist)
559 if (itr)
560 return itr->info_priv_size(itr, evlist);
561 return 0;
564 static int auxtrace_not_supported(void)
566 pr_err("AUX area tracing is not supported on this architecture\n");
567 return -EINVAL;
570 int auxtrace_record__info_fill(struct auxtrace_record *itr,
571 struct perf_session *session,
572 struct perf_record_auxtrace_info *auxtrace_info,
573 size_t priv_size)
575 if (itr)
576 return itr->info_fill(itr, session, auxtrace_info, priv_size);
577 return auxtrace_not_supported();
580 void auxtrace_record__free(struct auxtrace_record *itr)
582 if (itr)
583 itr->free(itr);
586 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
588 if (itr && itr->snapshot_start)
589 return itr->snapshot_start(itr);
590 return 0;
593 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
595 if (!on_exit && itr && itr->snapshot_finish)
596 return itr->snapshot_finish(itr);
597 return 0;
600 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
601 struct auxtrace_mmap *mm,
602 unsigned char *data, u64 *head, u64 *old)
604 if (itr && itr->find_snapshot)
605 return itr->find_snapshot(itr, idx, mm, data, head, old);
606 return 0;
609 int auxtrace_record__options(struct auxtrace_record *itr,
610 struct evlist *evlist,
611 struct record_opts *opts)
613 if (itr) {
614 itr->evlist = evlist;
615 return itr->recording_options(itr, evlist, opts);
617 return 0;
620 u64 auxtrace_record__reference(struct auxtrace_record *itr)
622 if (itr)
623 return itr->reference(itr);
624 return 0;
627 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
628 struct record_opts *opts, const char *str)
630 if (!str)
631 return 0;
633 /* PMU-agnostic options */
634 switch (*str) {
635 case 'e':
636 opts->auxtrace_snapshot_on_exit = true;
637 str++;
638 break;
639 default:
640 break;
643 if (itr)
644 return itr->parse_snapshot_options(itr, opts, str);
646 pr_err("No AUX area tracing to snapshot\n");
647 return -EINVAL;
650 int auxtrace_record__read_finish(struct auxtrace_record *itr, int idx)
652 struct evsel *evsel;
654 if (!itr->evlist || !itr->pmu)
655 return -EINVAL;
657 evlist__for_each_entry(itr->evlist, evsel) {
658 if (evsel->core.attr.type == itr->pmu->type) {
659 if (evsel->disabled)
660 return 0;
661 return perf_evlist__enable_event_idx(itr->evlist, evsel,
662 idx);
665 return -EINVAL;
669 * Event record size is 16-bit which results in a maximum size of about 64KiB.
670 * Allow about 4KiB for the rest of the sample record, to give a maximum
671 * AUX area sample size of 60KiB.
673 #define MAX_AUX_SAMPLE_SIZE (60 * 1024)
675 /* Arbitrary default size if no other default provided */
676 #define DEFAULT_AUX_SAMPLE_SIZE (4 * 1024)
678 static int auxtrace_validate_aux_sample_size(struct evlist *evlist,
679 struct record_opts *opts)
681 struct evsel *evsel;
682 bool has_aux_leader = false;
683 u32 sz;
685 evlist__for_each_entry(evlist, evsel) {
686 sz = evsel->core.attr.aux_sample_size;
687 if (evsel__is_group_leader(evsel)) {
688 has_aux_leader = evsel__is_aux_event(evsel);
689 if (sz) {
690 if (has_aux_leader)
691 pr_err("Cannot add AUX area sampling to an AUX area event\n");
692 else
693 pr_err("Cannot add AUX area sampling to a group leader\n");
694 return -EINVAL;
697 if (sz > MAX_AUX_SAMPLE_SIZE) {
698 pr_err("AUX area sample size %u too big, max. %d\n",
699 sz, MAX_AUX_SAMPLE_SIZE);
700 return -EINVAL;
702 if (sz) {
703 if (!has_aux_leader) {
704 pr_err("Cannot add AUX area sampling because group leader is not an AUX area event\n");
705 return -EINVAL;
707 evsel__set_sample_bit(evsel, AUX);
708 opts->auxtrace_sample_mode = true;
709 } else {
710 evsel__reset_sample_bit(evsel, AUX);
714 if (!opts->auxtrace_sample_mode) {
715 pr_err("AUX area sampling requires an AUX area event group leader plus other events to which to add samples\n");
716 return -EINVAL;
719 if (!perf_can_aux_sample()) {
720 pr_err("AUX area sampling is not supported by kernel\n");
721 return -EINVAL;
724 return 0;
727 int auxtrace_parse_sample_options(struct auxtrace_record *itr,
728 struct evlist *evlist,
729 struct record_opts *opts, const char *str)
731 struct evsel_config_term *term;
732 struct evsel *aux_evsel;
733 bool has_aux_sample_size = false;
734 bool has_aux_leader = false;
735 struct evsel *evsel;
736 char *endptr;
737 unsigned long sz;
739 if (!str)
740 goto no_opt;
742 if (!itr) {
743 pr_err("No AUX area event to sample\n");
744 return -EINVAL;
747 sz = strtoul(str, &endptr, 0);
748 if (*endptr || sz > UINT_MAX) {
749 pr_err("Bad AUX area sampling option: '%s'\n", str);
750 return -EINVAL;
753 if (!sz)
754 sz = itr->default_aux_sample_size;
756 if (!sz)
757 sz = DEFAULT_AUX_SAMPLE_SIZE;
759 /* Set aux_sample_size based on --aux-sample option */
760 evlist__for_each_entry(evlist, evsel) {
761 if (evsel__is_group_leader(evsel)) {
762 has_aux_leader = evsel__is_aux_event(evsel);
763 } else if (has_aux_leader) {
764 evsel->core.attr.aux_sample_size = sz;
767 no_opt:
768 aux_evsel = NULL;
769 /* Override with aux_sample_size from config term */
770 evlist__for_each_entry(evlist, evsel) {
771 if (evsel__is_aux_event(evsel))
772 aux_evsel = evsel;
773 term = evsel__get_config_term(evsel, AUX_SAMPLE_SIZE);
774 if (term) {
775 has_aux_sample_size = true;
776 evsel->core.attr.aux_sample_size = term->val.aux_sample_size;
777 /* If possible, group with the AUX event */
778 if (aux_evsel && evsel->core.attr.aux_sample_size)
779 perf_evlist__regroup(evlist, aux_evsel, evsel);
783 if (!str && !has_aux_sample_size)
784 return 0;
786 if (!itr) {
787 pr_err("No AUX area event to sample\n");
788 return -EINVAL;
791 return auxtrace_validate_aux_sample_size(evlist, opts);
794 struct auxtrace_record *__weak
795 auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
797 *err = 0;
798 return NULL;
801 static int auxtrace_index__alloc(struct list_head *head)
803 struct auxtrace_index *auxtrace_index;
805 auxtrace_index = malloc(sizeof(struct auxtrace_index));
806 if (!auxtrace_index)
807 return -ENOMEM;
809 auxtrace_index->nr = 0;
810 INIT_LIST_HEAD(&auxtrace_index->list);
812 list_add_tail(&auxtrace_index->list, head);
814 return 0;
817 void auxtrace_index__free(struct list_head *head)
819 struct auxtrace_index *auxtrace_index, *n;
821 list_for_each_entry_safe(auxtrace_index, n, head, list) {
822 list_del_init(&auxtrace_index->list);
823 free(auxtrace_index);
827 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
829 struct auxtrace_index *auxtrace_index;
830 int err;
832 if (list_empty(head)) {
833 err = auxtrace_index__alloc(head);
834 if (err)
835 return NULL;
838 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
840 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
841 err = auxtrace_index__alloc(head);
842 if (err)
843 return NULL;
844 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
845 list);
848 return auxtrace_index;
851 int auxtrace_index__auxtrace_event(struct list_head *head,
852 union perf_event *event, off_t file_offset)
854 struct auxtrace_index *auxtrace_index;
855 size_t nr;
857 auxtrace_index = auxtrace_index__last(head);
858 if (!auxtrace_index)
859 return -ENOMEM;
861 nr = auxtrace_index->nr;
862 auxtrace_index->entries[nr].file_offset = file_offset;
863 auxtrace_index->entries[nr].sz = event->header.size;
864 auxtrace_index->nr += 1;
866 return 0;
869 static int auxtrace_index__do_write(int fd,
870 struct auxtrace_index *auxtrace_index)
872 struct auxtrace_index_entry ent;
873 size_t i;
875 for (i = 0; i < auxtrace_index->nr; i++) {
876 ent.file_offset = auxtrace_index->entries[i].file_offset;
877 ent.sz = auxtrace_index->entries[i].sz;
878 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
879 return -errno;
881 return 0;
884 int auxtrace_index__write(int fd, struct list_head *head)
886 struct auxtrace_index *auxtrace_index;
887 u64 total = 0;
888 int err;
890 list_for_each_entry(auxtrace_index, head, list)
891 total += auxtrace_index->nr;
893 if (writen(fd, &total, sizeof(total)) != sizeof(total))
894 return -errno;
896 list_for_each_entry(auxtrace_index, head, list) {
897 err = auxtrace_index__do_write(fd, auxtrace_index);
898 if (err)
899 return err;
902 return 0;
905 static int auxtrace_index__process_entry(int fd, struct list_head *head,
906 bool needs_swap)
908 struct auxtrace_index *auxtrace_index;
909 struct auxtrace_index_entry ent;
910 size_t nr;
912 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
913 return -1;
915 auxtrace_index = auxtrace_index__last(head);
916 if (!auxtrace_index)
917 return -1;
919 nr = auxtrace_index->nr;
920 if (needs_swap) {
921 auxtrace_index->entries[nr].file_offset =
922 bswap_64(ent.file_offset);
923 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
924 } else {
925 auxtrace_index->entries[nr].file_offset = ent.file_offset;
926 auxtrace_index->entries[nr].sz = ent.sz;
929 auxtrace_index->nr = nr + 1;
931 return 0;
934 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
935 bool needs_swap)
937 struct list_head *head = &session->auxtrace_index;
938 u64 nr;
940 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
941 return -1;
943 if (needs_swap)
944 nr = bswap_64(nr);
946 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
947 return -1;
949 while (nr--) {
950 int err;
952 err = auxtrace_index__process_entry(fd, head, needs_swap);
953 if (err)
954 return -1;
957 return 0;
960 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
961 struct perf_session *session,
962 struct auxtrace_index_entry *ent)
964 return auxtrace_queues__add_indexed_event(queues, session,
965 ent->file_offset, ent->sz);
968 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
969 struct perf_session *session)
971 struct auxtrace_index *auxtrace_index;
972 struct auxtrace_index_entry *ent;
973 size_t i;
974 int err;
976 if (auxtrace__dont_decode(session))
977 return 0;
979 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
980 for (i = 0; i < auxtrace_index->nr; i++) {
981 ent = &auxtrace_index->entries[i];
982 err = auxtrace_queues__process_index_entry(queues,
983 session,
984 ent);
985 if (err)
986 return err;
989 return 0;
992 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
993 struct auxtrace_buffer *buffer)
995 if (buffer) {
996 if (list_is_last(&buffer->list, &queue->head))
997 return NULL;
998 return list_entry(buffer->list.next, struct auxtrace_buffer,
999 list);
1000 } else {
1001 if (list_empty(&queue->head))
1002 return NULL;
1003 return list_entry(queue->head.next, struct auxtrace_buffer,
1004 list);
1008 struct auxtrace_queue *auxtrace_queues__sample_queue(struct auxtrace_queues *queues,
1009 struct perf_sample *sample,
1010 struct perf_session *session)
1012 struct perf_sample_id *sid;
1013 unsigned int idx;
1014 u64 id;
1016 id = sample->id;
1017 if (!id)
1018 return NULL;
1020 sid = perf_evlist__id2sid(session->evlist, id);
1021 if (!sid)
1022 return NULL;
1024 idx = sid->idx;
1026 if (idx >= queues->nr_queues)
1027 return NULL;
1029 return &queues->queue_array[idx];
1032 int auxtrace_queues__add_sample(struct auxtrace_queues *queues,
1033 struct perf_session *session,
1034 struct perf_sample *sample, u64 data_offset,
1035 u64 reference)
1037 struct auxtrace_buffer buffer = {
1038 .pid = -1,
1039 .data_offset = data_offset,
1040 .reference = reference,
1041 .size = sample->aux_sample.size,
1043 struct perf_sample_id *sid;
1044 u64 id = sample->id;
1045 unsigned int idx;
1047 if (!id)
1048 return -EINVAL;
1050 sid = perf_evlist__id2sid(session->evlist, id);
1051 if (!sid)
1052 return -ENOENT;
1054 idx = sid->idx;
1055 buffer.tid = sid->tid;
1056 buffer.cpu = sid->cpu;
1058 return auxtrace_queues__add_buffer(queues, session, idx, &buffer, NULL);
1061 struct queue_data {
1062 bool samples;
1063 bool events;
1066 static int auxtrace_queue_data_cb(struct perf_session *session,
1067 union perf_event *event, u64 offset,
1068 void *data)
1070 struct queue_data *qd = data;
1071 struct perf_sample sample;
1072 int err;
1074 if (qd->events && event->header.type == PERF_RECORD_AUXTRACE) {
1075 if (event->header.size < sizeof(struct perf_record_auxtrace))
1076 return -EINVAL;
1077 offset += event->header.size;
1078 return session->auxtrace->queue_data(session, NULL, event,
1079 offset);
1082 if (!qd->samples || event->header.type != PERF_RECORD_SAMPLE)
1083 return 0;
1085 err = perf_evlist__parse_sample(session->evlist, event, &sample);
1086 if (err)
1087 return err;
1089 if (!sample.aux_sample.size)
1090 return 0;
1092 offset += sample.aux_sample.data - (void *)event;
1094 return session->auxtrace->queue_data(session, &sample, NULL, offset);
1097 int auxtrace_queue_data(struct perf_session *session, bool samples, bool events)
1099 struct queue_data qd = {
1100 .samples = samples,
1101 .events = events,
1104 if (auxtrace__dont_decode(session))
1105 return 0;
1107 if (!session->auxtrace || !session->auxtrace->queue_data)
1108 return -EINVAL;
1110 return perf_session__peek_events(session, session->header.data_offset,
1111 session->header.data_size,
1112 auxtrace_queue_data_cb, &qd);
1115 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
1117 size_t adj = buffer->data_offset & (page_size - 1);
1118 size_t size = buffer->size + adj;
1119 off_t file_offset = buffer->data_offset - adj;
1120 void *addr;
1122 if (buffer->data)
1123 return buffer->data;
1125 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
1126 if (addr == MAP_FAILED)
1127 return NULL;
1129 buffer->mmap_addr = addr;
1130 buffer->mmap_size = size;
1132 buffer->data = addr + adj;
1134 return buffer->data;
1137 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
1139 if (!buffer->data || !buffer->mmap_addr)
1140 return;
1141 munmap(buffer->mmap_addr, buffer->mmap_size);
1142 buffer->mmap_addr = NULL;
1143 buffer->mmap_size = 0;
1144 buffer->data = NULL;
1145 buffer->use_data = NULL;
1148 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
1150 auxtrace_buffer__put_data(buffer);
1151 if (buffer->data_needs_freeing) {
1152 buffer->data_needs_freeing = false;
1153 zfree(&buffer->data);
1154 buffer->use_data = NULL;
1155 buffer->size = 0;
1159 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
1161 auxtrace_buffer__drop_data(buffer);
1162 free(buffer);
1165 void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
1166 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
1167 const char *msg, u64 timestamp)
1169 size_t size;
1171 memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
1173 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
1174 auxtrace_error->type = type;
1175 auxtrace_error->code = code;
1176 auxtrace_error->cpu = cpu;
1177 auxtrace_error->pid = pid;
1178 auxtrace_error->tid = tid;
1179 auxtrace_error->fmt = 1;
1180 auxtrace_error->ip = ip;
1181 auxtrace_error->time = timestamp;
1182 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
1184 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
1185 strlen(auxtrace_error->msg) + 1;
1186 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
1189 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
1190 struct perf_tool *tool,
1191 struct perf_session *session,
1192 perf_event__handler_t process)
1194 union perf_event *ev;
1195 size_t priv_size;
1196 int err;
1198 pr_debug2("Synthesizing auxtrace information\n");
1199 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
1200 ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
1201 if (!ev)
1202 return -ENOMEM;
1204 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
1205 ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
1206 priv_size;
1207 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
1208 priv_size);
1209 if (err)
1210 goto out_free;
1212 err = process(tool, ev, NULL, NULL);
1213 out_free:
1214 free(ev);
1215 return err;
1218 static void unleader_evsel(struct evlist *evlist, struct evsel *leader)
1220 struct evsel *new_leader = NULL;
1221 struct evsel *evsel;
1223 /* Find new leader for the group */
1224 evlist__for_each_entry(evlist, evsel) {
1225 if (evsel->leader != leader || evsel == leader)
1226 continue;
1227 if (!new_leader)
1228 new_leader = evsel;
1229 evsel->leader = new_leader;
1232 /* Update group information */
1233 if (new_leader) {
1234 zfree(&new_leader->group_name);
1235 new_leader->group_name = leader->group_name;
1236 leader->group_name = NULL;
1238 new_leader->core.nr_members = leader->core.nr_members - 1;
1239 leader->core.nr_members = 1;
1243 static void unleader_auxtrace(struct perf_session *session)
1245 struct evsel *evsel;
1247 evlist__for_each_entry(session->evlist, evsel) {
1248 if (auxtrace__evsel_is_auxtrace(session, evsel) &&
1249 evsel__is_group_leader(evsel)) {
1250 unleader_evsel(session->evlist, evsel);
1255 int perf_event__process_auxtrace_info(struct perf_session *session,
1256 union perf_event *event)
1258 enum auxtrace_type type = event->auxtrace_info.type;
1259 int err;
1261 if (dump_trace)
1262 fprintf(stdout, " type: %u\n", type);
1264 switch (type) {
1265 case PERF_AUXTRACE_INTEL_PT:
1266 err = intel_pt_process_auxtrace_info(event, session);
1267 break;
1268 case PERF_AUXTRACE_INTEL_BTS:
1269 err = intel_bts_process_auxtrace_info(event, session);
1270 break;
1271 case PERF_AUXTRACE_ARM_SPE:
1272 err = arm_spe_process_auxtrace_info(event, session);
1273 break;
1274 case PERF_AUXTRACE_CS_ETM:
1275 err = cs_etm__process_auxtrace_info(event, session);
1276 break;
1277 case PERF_AUXTRACE_S390_CPUMSF:
1278 err = s390_cpumsf_process_auxtrace_info(event, session);
1279 break;
1280 case PERF_AUXTRACE_UNKNOWN:
1281 default:
1282 return -EINVAL;
1285 if (err)
1286 return err;
1288 unleader_auxtrace(session);
1290 return 0;
1293 s64 perf_event__process_auxtrace(struct perf_session *session,
1294 union perf_event *event)
1296 s64 err;
1298 if (dump_trace)
1299 fprintf(stdout, " size: %#"PRI_lx64" offset: %#"PRI_lx64" ref: %#"PRI_lx64" idx: %u tid: %d cpu: %d\n",
1300 event->auxtrace.size, event->auxtrace.offset,
1301 event->auxtrace.reference, event->auxtrace.idx,
1302 event->auxtrace.tid, event->auxtrace.cpu);
1304 if (auxtrace__dont_decode(session))
1305 return event->auxtrace.size;
1307 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
1308 return -EINVAL;
1310 err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
1311 if (err < 0)
1312 return err;
1314 return event->auxtrace.size;
1317 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
1318 #define PERF_ITRACE_DEFAULT_PERIOD 100000
1319 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
1320 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
1321 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
1322 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
1324 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
1325 bool no_sample)
1327 synth_opts->branches = true;
1328 synth_opts->transactions = true;
1329 synth_opts->ptwrites = true;
1330 synth_opts->pwr_events = true;
1331 synth_opts->other_events = true;
1332 synth_opts->errors = true;
1333 synth_opts->flc = true;
1334 synth_opts->llc = true;
1335 synth_opts->tlb = true;
1336 synth_opts->remote_access = true;
1338 if (no_sample) {
1339 synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
1340 synth_opts->period = 1;
1341 synth_opts->calls = true;
1342 } else {
1343 synth_opts->instructions = true;
1344 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1345 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1347 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1348 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1349 synth_opts->initial_skip = 0;
1352 static int get_flag(const char **ptr, unsigned int *flags)
1354 while (1) {
1355 char c = **ptr;
1357 if (c >= 'a' && c <= 'z') {
1358 *flags |= 1 << (c - 'a');
1359 ++*ptr;
1360 return 0;
1361 } else if (c == ' ') {
1362 ++*ptr;
1363 continue;
1364 } else {
1365 return -1;
1370 static int get_flags(const char **ptr, unsigned int *plus_flags, unsigned int *minus_flags)
1372 while (1) {
1373 switch (**ptr) {
1374 case '+':
1375 ++*ptr;
1376 if (get_flag(ptr, plus_flags))
1377 return -1;
1378 break;
1379 case '-':
1380 ++*ptr;
1381 if (get_flag(ptr, minus_flags))
1382 return -1;
1383 break;
1384 case ' ':
1385 ++*ptr;
1386 break;
1387 default:
1388 return 0;
1394 * Please check tools/perf/Documentation/perf-script.txt for information
1395 * about the options parsed here, which is introduced after this cset,
1396 * when support in 'perf script' for these options is introduced.
1398 int itrace_parse_synth_opts(const struct option *opt, const char *str,
1399 int unset)
1401 struct itrace_synth_opts *synth_opts = opt->value;
1402 const char *p;
1403 char *endptr;
1404 bool period_type_set = false;
1405 bool period_set = false;
1407 synth_opts->set = true;
1409 if (unset) {
1410 synth_opts->dont_decode = true;
1411 return 0;
1414 if (!str) {
1415 itrace_synth_opts__set_default(synth_opts,
1416 synth_opts->default_no_sample);
1417 return 0;
1420 for (p = str; *p;) {
1421 switch (*p++) {
1422 case 'i':
1423 synth_opts->instructions = true;
1424 while (*p == ' ' || *p == ',')
1425 p += 1;
1426 if (isdigit(*p)) {
1427 synth_opts->period = strtoull(p, &endptr, 10);
1428 period_set = true;
1429 p = endptr;
1430 while (*p == ' ' || *p == ',')
1431 p += 1;
1432 switch (*p++) {
1433 case 'i':
1434 synth_opts->period_type =
1435 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1436 period_type_set = true;
1437 break;
1438 case 't':
1439 synth_opts->period_type =
1440 PERF_ITRACE_PERIOD_TICKS;
1441 period_type_set = true;
1442 break;
1443 case 'm':
1444 synth_opts->period *= 1000;
1445 /* Fall through */
1446 case 'u':
1447 synth_opts->period *= 1000;
1448 /* Fall through */
1449 case 'n':
1450 if (*p++ != 's')
1451 goto out_err;
1452 synth_opts->period_type =
1453 PERF_ITRACE_PERIOD_NANOSECS;
1454 period_type_set = true;
1455 break;
1456 case '\0':
1457 goto out;
1458 default:
1459 goto out_err;
1462 break;
1463 case 'b':
1464 synth_opts->branches = true;
1465 break;
1466 case 'x':
1467 synth_opts->transactions = true;
1468 break;
1469 case 'w':
1470 synth_opts->ptwrites = true;
1471 break;
1472 case 'p':
1473 synth_opts->pwr_events = true;
1474 break;
1475 case 'o':
1476 synth_opts->other_events = true;
1477 break;
1478 case 'e':
1479 synth_opts->errors = true;
1480 if (get_flags(&p, &synth_opts->error_plus_flags,
1481 &synth_opts->error_minus_flags))
1482 goto out_err;
1483 break;
1484 case 'd':
1485 synth_opts->log = true;
1486 if (get_flags(&p, &synth_opts->log_plus_flags,
1487 &synth_opts->log_minus_flags))
1488 goto out_err;
1489 break;
1490 case 'c':
1491 synth_opts->branches = true;
1492 synth_opts->calls = true;
1493 break;
1494 case 'r':
1495 synth_opts->branches = true;
1496 synth_opts->returns = true;
1497 break;
1498 case 'G':
1499 case 'g':
1500 if (p[-1] == 'G')
1501 synth_opts->add_callchain = true;
1502 else
1503 synth_opts->callchain = true;
1504 synth_opts->callchain_sz =
1505 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1506 while (*p == ' ' || *p == ',')
1507 p += 1;
1508 if (isdigit(*p)) {
1509 unsigned int val;
1511 val = strtoul(p, &endptr, 10);
1512 p = endptr;
1513 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1514 goto out_err;
1515 synth_opts->callchain_sz = val;
1517 break;
1518 case 'L':
1519 case 'l':
1520 if (p[-1] == 'L')
1521 synth_opts->add_last_branch = true;
1522 else
1523 synth_opts->last_branch = true;
1524 synth_opts->last_branch_sz =
1525 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1526 while (*p == ' ' || *p == ',')
1527 p += 1;
1528 if (isdigit(*p)) {
1529 unsigned int val;
1531 val = strtoul(p, &endptr, 10);
1532 p = endptr;
1533 if (!val ||
1534 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1535 goto out_err;
1536 synth_opts->last_branch_sz = val;
1538 break;
1539 case 's':
1540 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1541 if (p == endptr)
1542 goto out_err;
1543 p = endptr;
1544 break;
1545 case 'f':
1546 synth_opts->flc = true;
1547 break;
1548 case 'm':
1549 synth_opts->llc = true;
1550 break;
1551 case 't':
1552 synth_opts->tlb = true;
1553 break;
1554 case 'a':
1555 synth_opts->remote_access = true;
1556 break;
1557 case 'q':
1558 synth_opts->quick += 1;
1559 break;
1560 case ' ':
1561 case ',':
1562 break;
1563 default:
1564 goto out_err;
1567 out:
1568 if (synth_opts->instructions) {
1569 if (!period_type_set)
1570 synth_opts->period_type =
1571 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1572 if (!period_set)
1573 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1576 return 0;
1578 out_err:
1579 pr_err("Bad Instruction Tracing options '%s'\n", str);
1580 return -EINVAL;
1583 static const char * const auxtrace_error_type_name[] = {
1584 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1587 static const char *auxtrace_error_name(int type)
1589 const char *error_type_name = NULL;
1591 if (type < PERF_AUXTRACE_ERROR_MAX)
1592 error_type_name = auxtrace_error_type_name[type];
1593 if (!error_type_name)
1594 error_type_name = "unknown AUX";
1595 return error_type_name;
1598 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1600 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1601 unsigned long long nsecs = e->time;
1602 const char *msg = e->msg;
1603 int ret;
1605 ret = fprintf(fp, " %s error type %u",
1606 auxtrace_error_name(e->type), e->type);
1608 if (e->fmt && nsecs) {
1609 unsigned long secs = nsecs / NSEC_PER_SEC;
1611 nsecs -= secs * NSEC_PER_SEC;
1612 ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1613 } else {
1614 ret += fprintf(fp, " time 0");
1617 if (!e->fmt)
1618 msg = (const char *)&e->time;
1620 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
1621 e->cpu, e->pid, e->tid, e->ip, e->code, msg);
1622 return ret;
1625 void perf_session__auxtrace_error_inc(struct perf_session *session,
1626 union perf_event *event)
1628 struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1630 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1631 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1634 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1636 int i;
1638 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1639 if (!stats->nr_auxtrace_errors[i])
1640 continue;
1641 ui__warning("%u %s errors\n",
1642 stats->nr_auxtrace_errors[i],
1643 auxtrace_error_name(i));
1647 int perf_event__process_auxtrace_error(struct perf_session *session,
1648 union perf_event *event)
1650 if (auxtrace__dont_decode(session))
1651 return 0;
1653 perf_event__fprintf_auxtrace_error(event, stdout);
1654 return 0;
1657 static int __auxtrace_mmap__read(struct mmap *map,
1658 struct auxtrace_record *itr,
1659 struct perf_tool *tool, process_auxtrace_t fn,
1660 bool snapshot, size_t snapshot_size)
1662 struct auxtrace_mmap *mm = &map->auxtrace_mmap;
1663 u64 head, old = mm->prev, offset, ref;
1664 unsigned char *data = mm->base;
1665 size_t size, head_off, old_off, len1, len2, padding;
1666 union perf_event ev;
1667 void *data1, *data2;
1669 if (snapshot) {
1670 head = auxtrace_mmap__read_snapshot_head(mm);
1671 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1672 &head, &old))
1673 return -1;
1674 } else {
1675 head = auxtrace_mmap__read_head(mm);
1678 if (old == head)
1679 return 0;
1681 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1682 mm->idx, old, head, head - old);
1684 if (mm->mask) {
1685 head_off = head & mm->mask;
1686 old_off = old & mm->mask;
1687 } else {
1688 head_off = head % mm->len;
1689 old_off = old % mm->len;
1692 if (head_off > old_off)
1693 size = head_off - old_off;
1694 else
1695 size = mm->len - (old_off - head_off);
1697 if (snapshot && size > snapshot_size)
1698 size = snapshot_size;
1700 ref = auxtrace_record__reference(itr);
1702 if (head > old || size <= head || mm->mask) {
1703 offset = head - size;
1704 } else {
1706 * When the buffer size is not a power of 2, 'head' wraps at the
1707 * highest multiple of the buffer size, so we have to subtract
1708 * the remainder here.
1710 u64 rem = (0ULL - mm->len) % mm->len;
1712 offset = head - size - rem;
1715 if (size > head_off) {
1716 len1 = size - head_off;
1717 data1 = &data[mm->len - len1];
1718 len2 = head_off;
1719 data2 = &data[0];
1720 } else {
1721 len1 = size;
1722 data1 = &data[head_off - len1];
1723 len2 = 0;
1724 data2 = NULL;
1727 if (itr->alignment) {
1728 unsigned int unwanted = len1 % itr->alignment;
1730 len1 -= unwanted;
1731 size -= unwanted;
1734 /* padding must be written by fn() e.g. record__process_auxtrace() */
1735 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1736 if (padding)
1737 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1739 memset(&ev, 0, sizeof(ev));
1740 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1741 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1742 ev.auxtrace.size = size + padding;
1743 ev.auxtrace.offset = offset;
1744 ev.auxtrace.reference = ref;
1745 ev.auxtrace.idx = mm->idx;
1746 ev.auxtrace.tid = mm->tid;
1747 ev.auxtrace.cpu = mm->cpu;
1749 if (fn(tool, map, &ev, data1, len1, data2, len2))
1750 return -1;
1752 mm->prev = head;
1754 if (!snapshot) {
1755 auxtrace_mmap__write_tail(mm, head);
1756 if (itr->read_finish) {
1757 int err;
1759 err = itr->read_finish(itr, mm->idx);
1760 if (err < 0)
1761 return err;
1765 return 1;
1768 int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
1769 struct perf_tool *tool, process_auxtrace_t fn)
1771 return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
1774 int auxtrace_mmap__read_snapshot(struct mmap *map,
1775 struct auxtrace_record *itr,
1776 struct perf_tool *tool, process_auxtrace_t fn,
1777 size_t snapshot_size)
1779 return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
1783 * struct auxtrace_cache - hash table to implement a cache
1784 * @hashtable: the hashtable
1785 * @sz: hashtable size (number of hlists)
1786 * @entry_size: size of an entry
1787 * @limit: limit the number of entries to this maximum, when reached the cache
1788 * is dropped and caching begins again with an empty cache
1789 * @cnt: current number of entries
1790 * @bits: hashtable size (@sz = 2^@bits)
1792 struct auxtrace_cache {
1793 struct hlist_head *hashtable;
1794 size_t sz;
1795 size_t entry_size;
1796 size_t limit;
1797 size_t cnt;
1798 unsigned int bits;
1801 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1802 unsigned int limit_percent)
1804 struct auxtrace_cache *c;
1805 struct hlist_head *ht;
1806 size_t sz, i;
1808 c = zalloc(sizeof(struct auxtrace_cache));
1809 if (!c)
1810 return NULL;
1812 sz = 1UL << bits;
1814 ht = calloc(sz, sizeof(struct hlist_head));
1815 if (!ht)
1816 goto out_free;
1818 for (i = 0; i < sz; i++)
1819 INIT_HLIST_HEAD(&ht[i]);
1821 c->hashtable = ht;
1822 c->sz = sz;
1823 c->entry_size = entry_size;
1824 c->limit = (c->sz * limit_percent) / 100;
1825 c->bits = bits;
1827 return c;
1829 out_free:
1830 free(c);
1831 return NULL;
1834 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1836 struct auxtrace_cache_entry *entry;
1837 struct hlist_node *tmp;
1838 size_t i;
1840 if (!c)
1841 return;
1843 for (i = 0; i < c->sz; i++) {
1844 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1845 hlist_del(&entry->hash);
1846 auxtrace_cache__free_entry(c, entry);
1850 c->cnt = 0;
1853 void auxtrace_cache__free(struct auxtrace_cache *c)
1855 if (!c)
1856 return;
1858 auxtrace_cache__drop(c);
1859 zfree(&c->hashtable);
1860 free(c);
1863 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1865 return malloc(c->entry_size);
1868 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1869 void *entry)
1871 free(entry);
1874 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1875 struct auxtrace_cache_entry *entry)
1877 if (c->limit && ++c->cnt > c->limit)
1878 auxtrace_cache__drop(c);
1880 entry->key = key;
1881 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1883 return 0;
1886 static struct auxtrace_cache_entry *auxtrace_cache__rm(struct auxtrace_cache *c,
1887 u32 key)
1889 struct auxtrace_cache_entry *entry;
1890 struct hlist_head *hlist;
1891 struct hlist_node *n;
1893 if (!c)
1894 return NULL;
1896 hlist = &c->hashtable[hash_32(key, c->bits)];
1897 hlist_for_each_entry_safe(entry, n, hlist, hash) {
1898 if (entry->key == key) {
1899 hlist_del(&entry->hash);
1900 return entry;
1904 return NULL;
1907 void auxtrace_cache__remove(struct auxtrace_cache *c, u32 key)
1909 struct auxtrace_cache_entry *entry = auxtrace_cache__rm(c, key);
1911 auxtrace_cache__free_entry(c, entry);
1914 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1916 struct auxtrace_cache_entry *entry;
1917 struct hlist_head *hlist;
1919 if (!c)
1920 return NULL;
1922 hlist = &c->hashtable[hash_32(key, c->bits)];
1923 hlist_for_each_entry(entry, hlist, hash) {
1924 if (entry->key == key)
1925 return entry;
1928 return NULL;
1931 static void addr_filter__free_str(struct addr_filter *filt)
1933 zfree(&filt->str);
1934 filt->action = NULL;
1935 filt->sym_from = NULL;
1936 filt->sym_to = NULL;
1937 filt->filename = NULL;
1940 static struct addr_filter *addr_filter__new(void)
1942 struct addr_filter *filt = zalloc(sizeof(*filt));
1944 if (filt)
1945 INIT_LIST_HEAD(&filt->list);
1947 return filt;
1950 static void addr_filter__free(struct addr_filter *filt)
1952 if (filt)
1953 addr_filter__free_str(filt);
1954 free(filt);
1957 static void addr_filters__add(struct addr_filters *filts,
1958 struct addr_filter *filt)
1960 list_add_tail(&filt->list, &filts->head);
1961 filts->cnt += 1;
1964 static void addr_filters__del(struct addr_filters *filts,
1965 struct addr_filter *filt)
1967 list_del_init(&filt->list);
1968 filts->cnt -= 1;
1971 void addr_filters__init(struct addr_filters *filts)
1973 INIT_LIST_HEAD(&filts->head);
1974 filts->cnt = 0;
1977 void addr_filters__exit(struct addr_filters *filts)
1979 struct addr_filter *filt, *n;
1981 list_for_each_entry_safe(filt, n, &filts->head, list) {
1982 addr_filters__del(filts, filt);
1983 addr_filter__free(filt);
1987 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1988 const char *str_delim)
1990 *inp += strspn(*inp, " ");
1992 if (isdigit(**inp)) {
1993 char *endptr;
1995 if (!num)
1996 return -EINVAL;
1997 errno = 0;
1998 *num = strtoull(*inp, &endptr, 0);
1999 if (errno)
2000 return -errno;
2001 if (endptr == *inp)
2002 return -EINVAL;
2003 *inp = endptr;
2004 } else {
2005 size_t n;
2007 if (!str)
2008 return -EINVAL;
2009 *inp += strspn(*inp, " ");
2010 *str = *inp;
2011 n = strcspn(*inp, str_delim);
2012 if (!n)
2013 return -EINVAL;
2014 *inp += n;
2015 if (**inp) {
2016 **inp = '\0';
2017 *inp += 1;
2020 return 0;
2023 static int parse_action(struct addr_filter *filt)
2025 if (!strcmp(filt->action, "filter")) {
2026 filt->start = true;
2027 filt->range = true;
2028 } else if (!strcmp(filt->action, "start")) {
2029 filt->start = true;
2030 } else if (!strcmp(filt->action, "stop")) {
2031 filt->start = false;
2032 } else if (!strcmp(filt->action, "tracestop")) {
2033 filt->start = false;
2034 filt->range = true;
2035 filt->action += 5; /* Change 'tracestop' to 'stop' */
2036 } else {
2037 return -EINVAL;
2039 return 0;
2042 static int parse_sym_idx(char **inp, int *idx)
2044 *idx = -1;
2046 *inp += strspn(*inp, " ");
2048 if (**inp != '#')
2049 return 0;
2051 *inp += 1;
2053 if (**inp == 'g' || **inp == 'G') {
2054 *inp += 1;
2055 *idx = 0;
2056 } else {
2057 unsigned long num;
2058 char *endptr;
2060 errno = 0;
2061 num = strtoul(*inp, &endptr, 0);
2062 if (errno)
2063 return -errno;
2064 if (endptr == *inp || num > INT_MAX)
2065 return -EINVAL;
2066 *inp = endptr;
2067 *idx = num;
2070 return 0;
2073 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
2075 int err = parse_num_or_str(inp, num, str, " ");
2077 if (!err && *str)
2078 err = parse_sym_idx(inp, idx);
2080 return err;
2083 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
2085 char *fstr;
2086 int err;
2088 filt->str = fstr = strdup(*filter_inp);
2089 if (!fstr)
2090 return -ENOMEM;
2092 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
2093 if (err)
2094 goto out_err;
2096 err = parse_action(filt);
2097 if (err)
2098 goto out_err;
2100 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
2101 &filt->sym_from_idx);
2102 if (err)
2103 goto out_err;
2105 fstr += strspn(fstr, " ");
2107 if (*fstr == '/') {
2108 fstr += 1;
2109 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
2110 &filt->sym_to_idx);
2111 if (err)
2112 goto out_err;
2113 filt->range = true;
2116 fstr += strspn(fstr, " ");
2118 if (*fstr == '@') {
2119 fstr += 1;
2120 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
2121 if (err)
2122 goto out_err;
2125 fstr += strspn(fstr, " ,");
2127 *filter_inp += fstr - filt->str;
2129 return 0;
2131 out_err:
2132 addr_filter__free_str(filt);
2134 return err;
2137 int addr_filters__parse_bare_filter(struct addr_filters *filts,
2138 const char *filter)
2140 struct addr_filter *filt;
2141 const char *fstr = filter;
2142 int err;
2144 while (*fstr) {
2145 filt = addr_filter__new();
2146 err = parse_one_filter(filt, &fstr);
2147 if (err) {
2148 addr_filter__free(filt);
2149 addr_filters__exit(filts);
2150 return err;
2152 addr_filters__add(filts, filt);
2155 return 0;
2158 struct sym_args {
2159 const char *name;
2160 u64 start;
2161 u64 size;
2162 int idx;
2163 int cnt;
2164 bool started;
2165 bool global;
2166 bool selected;
2167 bool duplicate;
2168 bool near;
2171 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
2173 /* A function with the same name, and global or the n'th found or any */
2174 return kallsyms__is_function(type) &&
2175 !strcmp(name, args->name) &&
2176 ((args->global && isupper(type)) ||
2177 (args->selected && ++(args->cnt) == args->idx) ||
2178 (!args->global && !args->selected));
2181 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2183 struct sym_args *args = arg;
2185 if (args->started) {
2186 if (!args->size)
2187 args->size = start - args->start;
2188 if (args->selected) {
2189 if (args->size)
2190 return 1;
2191 } else if (kern_sym_match(args, name, type)) {
2192 args->duplicate = true;
2193 return 1;
2195 } else if (kern_sym_match(args, name, type)) {
2196 args->started = true;
2197 args->start = start;
2200 return 0;
2203 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
2205 struct sym_args *args = arg;
2207 if (kern_sym_match(args, name, type)) {
2208 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2209 ++args->cnt, start, type, name);
2210 args->near = true;
2211 } else if (args->near) {
2212 args->near = false;
2213 pr_err("\t\twhich is near\t\t%s\n", name);
2216 return 0;
2219 static int sym_not_found_error(const char *sym_name, int idx)
2221 if (idx > 0) {
2222 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
2223 idx, sym_name);
2224 } else if (!idx) {
2225 pr_err("Global symbol '%s' not found.\n", sym_name);
2226 } else {
2227 pr_err("Symbol '%s' not found.\n", sym_name);
2229 pr_err("Note that symbols must be functions.\n");
2231 return -EINVAL;
2234 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
2236 struct sym_args args = {
2237 .name = sym_name,
2238 .idx = idx,
2239 .global = !idx,
2240 .selected = idx > 0,
2242 int err;
2244 *start = 0;
2245 *size = 0;
2247 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
2248 if (err < 0) {
2249 pr_err("Failed to parse /proc/kallsyms\n");
2250 return err;
2253 if (args.duplicate) {
2254 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
2255 args.cnt = 0;
2256 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
2257 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2258 sym_name);
2259 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2260 return -EINVAL;
2263 if (!args.started) {
2264 pr_err("Kernel symbol lookup: ");
2265 return sym_not_found_error(sym_name, idx);
2268 *start = args.start;
2269 *size = args.size;
2271 return 0;
2274 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
2275 char type, u64 start)
2277 struct sym_args *args = arg;
2279 if (!kallsyms__is_function(type))
2280 return 0;
2282 if (!args->started) {
2283 args->started = true;
2284 args->start = start;
2286 /* Don't know exactly where the kernel ends, so we add a page */
2287 args->size = round_up(start, page_size) + page_size - args->start;
2289 return 0;
2292 static int addr_filter__entire_kernel(struct addr_filter *filt)
2294 struct sym_args args = { .started = false };
2295 int err;
2297 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
2298 if (err < 0 || !args.started) {
2299 pr_err("Failed to parse /proc/kallsyms\n");
2300 return err;
2303 filt->addr = args.start;
2304 filt->size = args.size;
2306 return 0;
2309 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
2311 if (start + size >= filt->addr)
2312 return 0;
2314 if (filt->sym_from) {
2315 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
2316 filt->sym_to, start, filt->sym_from, filt->addr);
2317 } else {
2318 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
2319 filt->sym_to, start, filt->addr);
2322 return -EINVAL;
2325 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
2327 bool no_size = false;
2328 u64 start, size;
2329 int err;
2331 if (symbol_conf.kptr_restrict) {
2332 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
2333 return -EINVAL;
2336 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
2337 return addr_filter__entire_kernel(filt);
2339 if (filt->sym_from) {
2340 err = find_kern_sym(filt->sym_from, &start, &size,
2341 filt->sym_from_idx);
2342 if (err)
2343 return err;
2344 filt->addr = start;
2345 if (filt->range && !filt->size && !filt->sym_to) {
2346 filt->size = size;
2347 no_size = !size;
2351 if (filt->sym_to) {
2352 err = find_kern_sym(filt->sym_to, &start, &size,
2353 filt->sym_to_idx);
2354 if (err)
2355 return err;
2357 err = check_end_after_start(filt, start, size);
2358 if (err)
2359 return err;
2360 filt->size = start + size - filt->addr;
2361 no_size = !size;
2364 /* The very last symbol in kallsyms does not imply a particular size */
2365 if (no_size) {
2366 pr_err("Cannot determine size of symbol '%s'\n",
2367 filt->sym_to ? filt->sym_to : filt->sym_from);
2368 return -EINVAL;
2371 return 0;
2374 static struct dso *load_dso(const char *name)
2376 struct map *map;
2377 struct dso *dso;
2379 map = dso__new_map(name);
2380 if (!map)
2381 return NULL;
2383 if (map__load(map) < 0)
2384 pr_err("File '%s' not found or has no symbols.\n", name);
2386 dso = dso__get(map->dso);
2388 map__put(map);
2390 return dso;
2393 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
2394 int idx)
2396 /* Same name, and global or the n'th found or any */
2397 return !arch__compare_symbol_names(name, sym->name) &&
2398 ((!idx && sym->binding == STB_GLOBAL) ||
2399 (idx > 0 && ++*cnt == idx) ||
2400 idx < 0);
2403 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
2405 struct symbol *sym;
2406 bool near = false;
2407 int cnt = 0;
2409 pr_err("Multiple symbols with name '%s'\n", sym_name);
2411 sym = dso__first_symbol(dso);
2412 while (sym) {
2413 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
2414 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
2415 ++cnt, sym->start,
2416 sym->binding == STB_GLOBAL ? 'g' :
2417 sym->binding == STB_LOCAL ? 'l' : 'w',
2418 sym->name);
2419 near = true;
2420 } else if (near) {
2421 near = false;
2422 pr_err("\t\twhich is near\t\t%s\n", sym->name);
2424 sym = dso__next_symbol(sym);
2427 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
2428 sym_name);
2429 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
2432 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
2433 u64 *size, int idx)
2435 struct symbol *sym;
2436 int cnt = 0;
2438 *start = 0;
2439 *size = 0;
2441 sym = dso__first_symbol(dso);
2442 while (sym) {
2443 if (*start) {
2444 if (!*size)
2445 *size = sym->start - *start;
2446 if (idx > 0) {
2447 if (*size)
2448 return 1;
2449 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2450 print_duplicate_syms(dso, sym_name);
2451 return -EINVAL;
2453 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2454 *start = sym->start;
2455 *size = sym->end - sym->start;
2457 sym = dso__next_symbol(sym);
2460 if (!*start)
2461 return sym_not_found_error(sym_name, idx);
2463 return 0;
2466 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2468 if (dso__data_file_size(dso, NULL)) {
2469 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2470 filt->filename);
2471 return -EINVAL;
2474 filt->addr = 0;
2475 filt->size = dso->data.file_size;
2477 return 0;
2480 static int addr_filter__resolve_syms(struct addr_filter *filt)
2482 u64 start, size;
2483 struct dso *dso;
2484 int err = 0;
2486 if (!filt->sym_from && !filt->sym_to)
2487 return 0;
2489 if (!filt->filename)
2490 return addr_filter__resolve_kernel_syms(filt);
2492 dso = load_dso(filt->filename);
2493 if (!dso) {
2494 pr_err("Failed to load symbols from: %s\n", filt->filename);
2495 return -EINVAL;
2498 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2499 err = addr_filter__entire_dso(filt, dso);
2500 goto put_dso;
2503 if (filt->sym_from) {
2504 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2505 filt->sym_from_idx);
2506 if (err)
2507 goto put_dso;
2508 filt->addr = start;
2509 if (filt->range && !filt->size && !filt->sym_to)
2510 filt->size = size;
2513 if (filt->sym_to) {
2514 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2515 filt->sym_to_idx);
2516 if (err)
2517 goto put_dso;
2519 err = check_end_after_start(filt, start, size);
2520 if (err)
2521 return err;
2523 filt->size = start + size - filt->addr;
2526 put_dso:
2527 dso__put(dso);
2529 return err;
2532 static char *addr_filter__to_str(struct addr_filter *filt)
2534 char filename_buf[PATH_MAX];
2535 const char *at = "";
2536 const char *fn = "";
2537 char *filter;
2538 int err;
2540 if (filt->filename) {
2541 at = "@";
2542 fn = realpath(filt->filename, filename_buf);
2543 if (!fn)
2544 return NULL;
2547 if (filt->range) {
2548 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2549 filt->action, filt->addr, filt->size, at, fn);
2550 } else {
2551 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2552 filt->action, filt->addr, at, fn);
2555 return err < 0 ? NULL : filter;
2558 static int parse_addr_filter(struct evsel *evsel, const char *filter,
2559 int max_nr)
2561 struct addr_filters filts;
2562 struct addr_filter *filt;
2563 int err;
2565 addr_filters__init(&filts);
2567 err = addr_filters__parse_bare_filter(&filts, filter);
2568 if (err)
2569 goto out_exit;
2571 if (filts.cnt > max_nr) {
2572 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2573 filts.cnt, max_nr);
2574 err = -EINVAL;
2575 goto out_exit;
2578 list_for_each_entry(filt, &filts.head, list) {
2579 char *new_filter;
2581 err = addr_filter__resolve_syms(filt);
2582 if (err)
2583 goto out_exit;
2585 new_filter = addr_filter__to_str(filt);
2586 if (!new_filter) {
2587 err = -ENOMEM;
2588 goto out_exit;
2591 if (evsel__append_addr_filter(evsel, new_filter)) {
2592 err = -ENOMEM;
2593 goto out_exit;
2597 out_exit:
2598 addr_filters__exit(&filts);
2600 if (err) {
2601 pr_err("Failed to parse address filter: '%s'\n", filter);
2602 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2603 pr_err("Where multiple filters are separated by space or comma.\n");
2606 return err;
2609 static int evsel__nr_addr_filter(struct evsel *evsel)
2611 struct perf_pmu *pmu = evsel__find_pmu(evsel);
2612 int nr_addr_filters = 0;
2614 if (!pmu)
2615 return 0;
2617 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2619 return nr_addr_filters;
2622 int auxtrace_parse_filters(struct evlist *evlist)
2624 struct evsel *evsel;
2625 char *filter;
2626 int err, max_nr;
2628 evlist__for_each_entry(evlist, evsel) {
2629 filter = evsel->filter;
2630 max_nr = evsel__nr_addr_filter(evsel);
2631 if (!filter || !max_nr)
2632 continue;
2633 evsel->filter = NULL;
2634 err = parse_addr_filter(evsel, filter, max_nr);
2635 free(filter);
2636 if (err)
2637 return err;
2638 pr_debug("Address filter: %s\n", evsel->filter);
2641 return 0;
2644 int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2645 struct perf_sample *sample, struct perf_tool *tool)
2647 if (!session->auxtrace)
2648 return 0;
2650 return session->auxtrace->process_event(session, event, sample, tool);
2653 void auxtrace__dump_auxtrace_sample(struct perf_session *session,
2654 struct perf_sample *sample)
2656 if (!session->auxtrace || !session->auxtrace->dump_auxtrace_sample ||
2657 auxtrace__dont_decode(session))
2658 return;
2660 session->auxtrace->dump_auxtrace_sample(session, sample);
2663 int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2665 if (!session->auxtrace)
2666 return 0;
2668 return session->auxtrace->flush_events(session, tool);
2671 void auxtrace__free_events(struct perf_session *session)
2673 if (!session->auxtrace)
2674 return;
2676 return session->auxtrace->free_events(session);
2679 void auxtrace__free(struct perf_session *session)
2681 if (!session->auxtrace)
2682 return;
2684 return session->auxtrace->free(session);
2687 bool auxtrace__evsel_is_auxtrace(struct perf_session *session,
2688 struct evsel *evsel)
2690 if (!session->auxtrace || !session->auxtrace->evsel_is_auxtrace)
2691 return false;
2693 return session->auxtrace->evsel_is_auxtrace(session, evsel);