xref: /openbmc/linux/tools/perf/util/auxtrace.c (revision b9df3997)
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 "evlist.h"
30 #include "dso.h"
31 #include "map.h"
32 #include "pmu.h"
33 #include "evsel.h"
34 #include "symbol.h"
35 #include "util/synthetic-events.h"
36 #include "thread_map.h"
37 #include "asm/bug.h"
38 #include "auxtrace.h"
39 
40 #include <linux/hash.h>
41 
42 #include "event.h"
43 #include "record.h"
44 #include "session.h"
45 #include "debug.h"
46 #include <subcmd/parse-options.h>
47 
48 #include "cs-etm.h"
49 #include "intel-pt.h"
50 #include "intel-bts.h"
51 #include "arm-spe.h"
52 #include "s390-cpumsf.h"
53 #include "util/mmap.h"
54 
55 #include <linux/ctype.h>
56 #include <linux/kernel.h>
57 #include "symbol/kallsyms.h"
58 #include <internal/lib.h>
59 
60 static bool auxtrace__dont_decode(struct perf_session *session)
61 {
62 	return !session->itrace_synth_opts ||
63 	       session->itrace_synth_opts->dont_decode;
64 }
65 
66 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
67 			struct auxtrace_mmap_params *mp,
68 			void *userpg, int fd)
69 {
70 	struct perf_event_mmap_page *pc = userpg;
71 
72 	WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
73 
74 	mm->userpg = userpg;
75 	mm->mask = mp->mask;
76 	mm->len = mp->len;
77 	mm->prev = 0;
78 	mm->idx = mp->idx;
79 	mm->tid = mp->tid;
80 	mm->cpu = mp->cpu;
81 
82 	if (!mp->len) {
83 		mm->base = NULL;
84 		return 0;
85 	}
86 
87 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
88 	pr_err("Cannot use AUX area tracing mmaps\n");
89 	return -1;
90 #endif
91 
92 	pc->aux_offset = mp->offset;
93 	pc->aux_size = mp->len;
94 
95 	mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
96 	if (mm->base == MAP_FAILED) {
97 		pr_debug2("failed to mmap AUX area\n");
98 		mm->base = NULL;
99 		return -1;
100 	}
101 
102 	return 0;
103 }
104 
105 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
106 {
107 	if (mm->base) {
108 		munmap(mm->base, mm->len);
109 		mm->base = NULL;
110 	}
111 }
112 
113 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
114 				off_t auxtrace_offset,
115 				unsigned int auxtrace_pages,
116 				bool auxtrace_overwrite)
117 {
118 	if (auxtrace_pages) {
119 		mp->offset = auxtrace_offset;
120 		mp->len = auxtrace_pages * (size_t)page_size;
121 		mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
122 		mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
123 		pr_debug2("AUX area mmap length %zu\n", mp->len);
124 	} else {
125 		mp->len = 0;
126 	}
127 }
128 
129 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
130 				   struct evlist *evlist, int idx,
131 				   bool per_cpu)
132 {
133 	mp->idx = idx;
134 
135 	if (per_cpu) {
136 		mp->cpu = evlist->core.cpus->map[idx];
137 		if (evlist->core.threads)
138 			mp->tid = perf_thread_map__pid(evlist->core.threads, 0);
139 		else
140 			mp->tid = -1;
141 	} else {
142 		mp->cpu = -1;
143 		mp->tid = perf_thread_map__pid(evlist->core.threads, idx);
144 	}
145 }
146 
147 #define AUXTRACE_INIT_NR_QUEUES	32
148 
149 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
150 {
151 	struct auxtrace_queue *queue_array;
152 	unsigned int max_nr_queues, i;
153 
154 	max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
155 	if (nr_queues > max_nr_queues)
156 		return NULL;
157 
158 	queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
159 	if (!queue_array)
160 		return NULL;
161 
162 	for (i = 0; i < nr_queues; i++) {
163 		INIT_LIST_HEAD(&queue_array[i].head);
164 		queue_array[i].priv = NULL;
165 	}
166 
167 	return queue_array;
168 }
169 
170 int auxtrace_queues__init(struct auxtrace_queues *queues)
171 {
172 	queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
173 	queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
174 	if (!queues->queue_array)
175 		return -ENOMEM;
176 	return 0;
177 }
178 
179 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
180 				 unsigned int new_nr_queues)
181 {
182 	unsigned int nr_queues = queues->nr_queues;
183 	struct auxtrace_queue *queue_array;
184 	unsigned int i;
185 
186 	if (!nr_queues)
187 		nr_queues = AUXTRACE_INIT_NR_QUEUES;
188 
189 	while (nr_queues && nr_queues < new_nr_queues)
190 		nr_queues <<= 1;
191 
192 	if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
193 		return -EINVAL;
194 
195 	queue_array = auxtrace_alloc_queue_array(nr_queues);
196 	if (!queue_array)
197 		return -ENOMEM;
198 
199 	for (i = 0; i < queues->nr_queues; i++) {
200 		list_splice_tail(&queues->queue_array[i].head,
201 				 &queue_array[i].head);
202 		queue_array[i].tid = queues->queue_array[i].tid;
203 		queue_array[i].cpu = queues->queue_array[i].cpu;
204 		queue_array[i].set = queues->queue_array[i].set;
205 		queue_array[i].priv = queues->queue_array[i].priv;
206 	}
207 
208 	queues->nr_queues = nr_queues;
209 	queues->queue_array = queue_array;
210 
211 	return 0;
212 }
213 
214 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
215 {
216 	int fd = perf_data__fd(session->data);
217 	void *p;
218 	ssize_t ret;
219 
220 	if (size > SSIZE_MAX)
221 		return NULL;
222 
223 	p = malloc(size);
224 	if (!p)
225 		return NULL;
226 
227 	ret = readn(fd, p, size);
228 	if (ret != (ssize_t)size) {
229 		free(p);
230 		return NULL;
231 	}
232 
233 	return p;
234 }
235 
236 static int auxtrace_queues__queue_buffer(struct auxtrace_queues *queues,
237 					 unsigned int idx,
238 					 struct auxtrace_buffer *buffer)
239 {
240 	struct auxtrace_queue *queue;
241 	int err;
242 
243 	if (idx >= queues->nr_queues) {
244 		err = auxtrace_queues__grow(queues, idx + 1);
245 		if (err)
246 			return err;
247 	}
248 
249 	queue = &queues->queue_array[idx];
250 
251 	if (!queue->set) {
252 		queue->set = true;
253 		queue->tid = buffer->tid;
254 		queue->cpu = buffer->cpu;
255 	} else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
256 		pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
257 		       queue->cpu, queue->tid, buffer->cpu, buffer->tid);
258 		return -EINVAL;
259 	}
260 
261 	buffer->buffer_nr = queues->next_buffer_nr++;
262 
263 	list_add_tail(&buffer->list, &queue->head);
264 
265 	queues->new_data = true;
266 	queues->populated = true;
267 
268 	return 0;
269 }
270 
271 /* Limit buffers to 32MiB on 32-bit */
272 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
273 
274 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
275 					 unsigned int idx,
276 					 struct auxtrace_buffer *buffer)
277 {
278 	u64 sz = buffer->size;
279 	bool consecutive = false;
280 	struct auxtrace_buffer *b;
281 	int err;
282 
283 	while (sz > BUFFER_LIMIT_FOR_32_BIT) {
284 		b = memdup(buffer, sizeof(struct auxtrace_buffer));
285 		if (!b)
286 			return -ENOMEM;
287 		b->size = BUFFER_LIMIT_FOR_32_BIT;
288 		b->consecutive = consecutive;
289 		err = auxtrace_queues__queue_buffer(queues, idx, b);
290 		if (err) {
291 			auxtrace_buffer__free(b);
292 			return err;
293 		}
294 		buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
295 		sz -= BUFFER_LIMIT_FOR_32_BIT;
296 		consecutive = true;
297 	}
298 
299 	buffer->size = sz;
300 	buffer->consecutive = consecutive;
301 
302 	return 0;
303 }
304 
305 static bool filter_cpu(struct perf_session *session, int cpu)
306 {
307 	unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
308 
309 	return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap);
310 }
311 
312 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
313 				       struct perf_session *session,
314 				       unsigned int idx,
315 				       struct auxtrace_buffer *buffer,
316 				       struct auxtrace_buffer **buffer_ptr)
317 {
318 	int err = -ENOMEM;
319 
320 	if (filter_cpu(session, buffer->cpu))
321 		return 0;
322 
323 	buffer = memdup(buffer, sizeof(*buffer));
324 	if (!buffer)
325 		return -ENOMEM;
326 
327 	if (session->one_mmap) {
328 		buffer->data = buffer->data_offset - session->one_mmap_offset +
329 			       session->one_mmap_addr;
330 	} else if (perf_data__is_pipe(session->data)) {
331 		buffer->data = auxtrace_copy_data(buffer->size, session);
332 		if (!buffer->data)
333 			goto out_free;
334 		buffer->data_needs_freeing = true;
335 	} else if (BITS_PER_LONG == 32 &&
336 		   buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
337 		err = auxtrace_queues__split_buffer(queues, idx, buffer);
338 		if (err)
339 			goto out_free;
340 	}
341 
342 	err = auxtrace_queues__queue_buffer(queues, idx, buffer);
343 	if (err)
344 		goto out_free;
345 
346 	/* FIXME: Doesn't work for split buffer */
347 	if (buffer_ptr)
348 		*buffer_ptr = buffer;
349 
350 	return 0;
351 
352 out_free:
353 	auxtrace_buffer__free(buffer);
354 	return err;
355 }
356 
357 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
358 			       struct perf_session *session,
359 			       union perf_event *event, off_t data_offset,
360 			       struct auxtrace_buffer **buffer_ptr)
361 {
362 	struct auxtrace_buffer buffer = {
363 		.pid = -1,
364 		.tid = event->auxtrace.tid,
365 		.cpu = event->auxtrace.cpu,
366 		.data_offset = data_offset,
367 		.offset = event->auxtrace.offset,
368 		.reference = event->auxtrace.reference,
369 		.size = event->auxtrace.size,
370 	};
371 	unsigned int idx = event->auxtrace.idx;
372 
373 	return auxtrace_queues__add_buffer(queues, session, idx, &buffer,
374 					   buffer_ptr);
375 }
376 
377 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
378 					      struct perf_session *session,
379 					      off_t file_offset, size_t sz)
380 {
381 	union perf_event *event;
382 	int err;
383 	char buf[PERF_SAMPLE_MAX_SIZE];
384 
385 	err = perf_session__peek_event(session, file_offset, buf,
386 				       PERF_SAMPLE_MAX_SIZE, &event, NULL);
387 	if (err)
388 		return err;
389 
390 	if (event->header.type == PERF_RECORD_AUXTRACE) {
391 		if (event->header.size < sizeof(struct perf_record_auxtrace) ||
392 		    event->header.size != sz) {
393 			err = -EINVAL;
394 			goto out;
395 		}
396 		file_offset += event->header.size;
397 		err = auxtrace_queues__add_event(queues, session, event,
398 						 file_offset, NULL);
399 	}
400 out:
401 	return err;
402 }
403 
404 void auxtrace_queues__free(struct auxtrace_queues *queues)
405 {
406 	unsigned int i;
407 
408 	for (i = 0; i < queues->nr_queues; i++) {
409 		while (!list_empty(&queues->queue_array[i].head)) {
410 			struct auxtrace_buffer *buffer;
411 
412 			buffer = list_entry(queues->queue_array[i].head.next,
413 					    struct auxtrace_buffer, list);
414 			list_del_init(&buffer->list);
415 			auxtrace_buffer__free(buffer);
416 		}
417 	}
418 
419 	zfree(&queues->queue_array);
420 	queues->nr_queues = 0;
421 }
422 
423 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
424 			     unsigned int pos, unsigned int queue_nr,
425 			     u64 ordinal)
426 {
427 	unsigned int parent;
428 
429 	while (pos) {
430 		parent = (pos - 1) >> 1;
431 		if (heap_array[parent].ordinal <= ordinal)
432 			break;
433 		heap_array[pos] = heap_array[parent];
434 		pos = parent;
435 	}
436 	heap_array[pos].queue_nr = queue_nr;
437 	heap_array[pos].ordinal = ordinal;
438 }
439 
440 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
441 		       u64 ordinal)
442 {
443 	struct auxtrace_heap_item *heap_array;
444 
445 	if (queue_nr >= heap->heap_sz) {
446 		unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
447 
448 		while (heap_sz <= queue_nr)
449 			heap_sz <<= 1;
450 		heap_array = realloc(heap->heap_array,
451 				     heap_sz * sizeof(struct auxtrace_heap_item));
452 		if (!heap_array)
453 			return -ENOMEM;
454 		heap->heap_array = heap_array;
455 		heap->heap_sz = heap_sz;
456 	}
457 
458 	auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
459 
460 	return 0;
461 }
462 
463 void auxtrace_heap__free(struct auxtrace_heap *heap)
464 {
465 	zfree(&heap->heap_array);
466 	heap->heap_cnt = 0;
467 	heap->heap_sz = 0;
468 }
469 
470 void auxtrace_heap__pop(struct auxtrace_heap *heap)
471 {
472 	unsigned int pos, last, heap_cnt = heap->heap_cnt;
473 	struct auxtrace_heap_item *heap_array;
474 
475 	if (!heap_cnt)
476 		return;
477 
478 	heap->heap_cnt -= 1;
479 
480 	heap_array = heap->heap_array;
481 
482 	pos = 0;
483 	while (1) {
484 		unsigned int left, right;
485 
486 		left = (pos << 1) + 1;
487 		if (left >= heap_cnt)
488 			break;
489 		right = left + 1;
490 		if (right >= heap_cnt) {
491 			heap_array[pos] = heap_array[left];
492 			return;
493 		}
494 		if (heap_array[left].ordinal < heap_array[right].ordinal) {
495 			heap_array[pos] = heap_array[left];
496 			pos = left;
497 		} else {
498 			heap_array[pos] = heap_array[right];
499 			pos = right;
500 		}
501 	}
502 
503 	last = heap_cnt - 1;
504 	auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
505 			 heap_array[last].ordinal);
506 }
507 
508 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
509 				       struct evlist *evlist)
510 {
511 	if (itr)
512 		return itr->info_priv_size(itr, evlist);
513 	return 0;
514 }
515 
516 static int auxtrace_not_supported(void)
517 {
518 	pr_err("AUX area tracing is not supported on this architecture\n");
519 	return -EINVAL;
520 }
521 
522 int auxtrace_record__info_fill(struct auxtrace_record *itr,
523 			       struct perf_session *session,
524 			       struct perf_record_auxtrace_info *auxtrace_info,
525 			       size_t priv_size)
526 {
527 	if (itr)
528 		return itr->info_fill(itr, session, auxtrace_info, priv_size);
529 	return auxtrace_not_supported();
530 }
531 
532 void auxtrace_record__free(struct auxtrace_record *itr)
533 {
534 	if (itr)
535 		itr->free(itr);
536 }
537 
538 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
539 {
540 	if (itr && itr->snapshot_start)
541 		return itr->snapshot_start(itr);
542 	return 0;
543 }
544 
545 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr, bool on_exit)
546 {
547 	if (!on_exit && itr && itr->snapshot_finish)
548 		return itr->snapshot_finish(itr);
549 	return 0;
550 }
551 
552 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
553 				   struct auxtrace_mmap *mm,
554 				   unsigned char *data, u64 *head, u64 *old)
555 {
556 	if (itr && itr->find_snapshot)
557 		return itr->find_snapshot(itr, idx, mm, data, head, old);
558 	return 0;
559 }
560 
561 int auxtrace_record__options(struct auxtrace_record *itr,
562 			     struct evlist *evlist,
563 			     struct record_opts *opts)
564 {
565 	if (itr)
566 		return itr->recording_options(itr, evlist, opts);
567 	return 0;
568 }
569 
570 u64 auxtrace_record__reference(struct auxtrace_record *itr)
571 {
572 	if (itr)
573 		return itr->reference(itr);
574 	return 0;
575 }
576 
577 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
578 				    struct record_opts *opts, const char *str)
579 {
580 	if (!str)
581 		return 0;
582 
583 	/* PMU-agnostic options */
584 	switch (*str) {
585 	case 'e':
586 		opts->auxtrace_snapshot_on_exit = true;
587 		str++;
588 		break;
589 	default:
590 		break;
591 	}
592 
593 	if (itr)
594 		return itr->parse_snapshot_options(itr, opts, str);
595 
596 	pr_err("No AUX area tracing to snapshot\n");
597 	return -EINVAL;
598 }
599 
600 struct auxtrace_record *__weak
601 auxtrace_record__init(struct evlist *evlist __maybe_unused, int *err)
602 {
603 	*err = 0;
604 	return NULL;
605 }
606 
607 static int auxtrace_index__alloc(struct list_head *head)
608 {
609 	struct auxtrace_index *auxtrace_index;
610 
611 	auxtrace_index = malloc(sizeof(struct auxtrace_index));
612 	if (!auxtrace_index)
613 		return -ENOMEM;
614 
615 	auxtrace_index->nr = 0;
616 	INIT_LIST_HEAD(&auxtrace_index->list);
617 
618 	list_add_tail(&auxtrace_index->list, head);
619 
620 	return 0;
621 }
622 
623 void auxtrace_index__free(struct list_head *head)
624 {
625 	struct auxtrace_index *auxtrace_index, *n;
626 
627 	list_for_each_entry_safe(auxtrace_index, n, head, list) {
628 		list_del_init(&auxtrace_index->list);
629 		free(auxtrace_index);
630 	}
631 }
632 
633 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
634 {
635 	struct auxtrace_index *auxtrace_index;
636 	int err;
637 
638 	if (list_empty(head)) {
639 		err = auxtrace_index__alloc(head);
640 		if (err)
641 			return NULL;
642 	}
643 
644 	auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
645 
646 	if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
647 		err = auxtrace_index__alloc(head);
648 		if (err)
649 			return NULL;
650 		auxtrace_index = list_entry(head->prev, struct auxtrace_index,
651 					    list);
652 	}
653 
654 	return auxtrace_index;
655 }
656 
657 int auxtrace_index__auxtrace_event(struct list_head *head,
658 				   union perf_event *event, off_t file_offset)
659 {
660 	struct auxtrace_index *auxtrace_index;
661 	size_t nr;
662 
663 	auxtrace_index = auxtrace_index__last(head);
664 	if (!auxtrace_index)
665 		return -ENOMEM;
666 
667 	nr = auxtrace_index->nr;
668 	auxtrace_index->entries[nr].file_offset = file_offset;
669 	auxtrace_index->entries[nr].sz = event->header.size;
670 	auxtrace_index->nr += 1;
671 
672 	return 0;
673 }
674 
675 static int auxtrace_index__do_write(int fd,
676 				    struct auxtrace_index *auxtrace_index)
677 {
678 	struct auxtrace_index_entry ent;
679 	size_t i;
680 
681 	for (i = 0; i < auxtrace_index->nr; i++) {
682 		ent.file_offset = auxtrace_index->entries[i].file_offset;
683 		ent.sz = auxtrace_index->entries[i].sz;
684 		if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
685 			return -errno;
686 	}
687 	return 0;
688 }
689 
690 int auxtrace_index__write(int fd, struct list_head *head)
691 {
692 	struct auxtrace_index *auxtrace_index;
693 	u64 total = 0;
694 	int err;
695 
696 	list_for_each_entry(auxtrace_index, head, list)
697 		total += auxtrace_index->nr;
698 
699 	if (writen(fd, &total, sizeof(total)) != sizeof(total))
700 		return -errno;
701 
702 	list_for_each_entry(auxtrace_index, head, list) {
703 		err = auxtrace_index__do_write(fd, auxtrace_index);
704 		if (err)
705 			return err;
706 	}
707 
708 	return 0;
709 }
710 
711 static int auxtrace_index__process_entry(int fd, struct list_head *head,
712 					 bool needs_swap)
713 {
714 	struct auxtrace_index *auxtrace_index;
715 	struct auxtrace_index_entry ent;
716 	size_t nr;
717 
718 	if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
719 		return -1;
720 
721 	auxtrace_index = auxtrace_index__last(head);
722 	if (!auxtrace_index)
723 		return -1;
724 
725 	nr = auxtrace_index->nr;
726 	if (needs_swap) {
727 		auxtrace_index->entries[nr].file_offset =
728 						bswap_64(ent.file_offset);
729 		auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
730 	} else {
731 		auxtrace_index->entries[nr].file_offset = ent.file_offset;
732 		auxtrace_index->entries[nr].sz = ent.sz;
733 	}
734 
735 	auxtrace_index->nr = nr + 1;
736 
737 	return 0;
738 }
739 
740 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
741 			    bool needs_swap)
742 {
743 	struct list_head *head = &session->auxtrace_index;
744 	u64 nr;
745 
746 	if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
747 		return -1;
748 
749 	if (needs_swap)
750 		nr = bswap_64(nr);
751 
752 	if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
753 		return -1;
754 
755 	while (nr--) {
756 		int err;
757 
758 		err = auxtrace_index__process_entry(fd, head, needs_swap);
759 		if (err)
760 			return -1;
761 	}
762 
763 	return 0;
764 }
765 
766 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
767 						struct perf_session *session,
768 						struct auxtrace_index_entry *ent)
769 {
770 	return auxtrace_queues__add_indexed_event(queues, session,
771 						  ent->file_offset, ent->sz);
772 }
773 
774 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
775 				   struct perf_session *session)
776 {
777 	struct auxtrace_index *auxtrace_index;
778 	struct auxtrace_index_entry *ent;
779 	size_t i;
780 	int err;
781 
782 	if (auxtrace__dont_decode(session))
783 		return 0;
784 
785 	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
786 		for (i = 0; i < auxtrace_index->nr; i++) {
787 			ent = &auxtrace_index->entries[i];
788 			err = auxtrace_queues__process_index_entry(queues,
789 								   session,
790 								   ent);
791 			if (err)
792 				return err;
793 		}
794 	}
795 	return 0;
796 }
797 
798 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
799 					      struct auxtrace_buffer *buffer)
800 {
801 	if (buffer) {
802 		if (list_is_last(&buffer->list, &queue->head))
803 			return NULL;
804 		return list_entry(buffer->list.next, struct auxtrace_buffer,
805 				  list);
806 	} else {
807 		if (list_empty(&queue->head))
808 			return NULL;
809 		return list_entry(queue->head.next, struct auxtrace_buffer,
810 				  list);
811 	}
812 }
813 
814 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
815 {
816 	size_t adj = buffer->data_offset & (page_size - 1);
817 	size_t size = buffer->size + adj;
818 	off_t file_offset = buffer->data_offset - adj;
819 	void *addr;
820 
821 	if (buffer->data)
822 		return buffer->data;
823 
824 	addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
825 	if (addr == MAP_FAILED)
826 		return NULL;
827 
828 	buffer->mmap_addr = addr;
829 	buffer->mmap_size = size;
830 
831 	buffer->data = addr + adj;
832 
833 	return buffer->data;
834 }
835 
836 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
837 {
838 	if (!buffer->data || !buffer->mmap_addr)
839 		return;
840 	munmap(buffer->mmap_addr, buffer->mmap_size);
841 	buffer->mmap_addr = NULL;
842 	buffer->mmap_size = 0;
843 	buffer->data = NULL;
844 	buffer->use_data = NULL;
845 }
846 
847 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
848 {
849 	auxtrace_buffer__put_data(buffer);
850 	if (buffer->data_needs_freeing) {
851 		buffer->data_needs_freeing = false;
852 		zfree(&buffer->data);
853 		buffer->use_data = NULL;
854 		buffer->size = 0;
855 	}
856 }
857 
858 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
859 {
860 	auxtrace_buffer__drop_data(buffer);
861 	free(buffer);
862 }
863 
864 void auxtrace_synth_error(struct perf_record_auxtrace_error *auxtrace_error, int type,
865 			  int code, int cpu, pid_t pid, pid_t tid, u64 ip,
866 			  const char *msg, u64 timestamp)
867 {
868 	size_t size;
869 
870 	memset(auxtrace_error, 0, sizeof(struct perf_record_auxtrace_error));
871 
872 	auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
873 	auxtrace_error->type = type;
874 	auxtrace_error->code = code;
875 	auxtrace_error->cpu = cpu;
876 	auxtrace_error->pid = pid;
877 	auxtrace_error->tid = tid;
878 	auxtrace_error->fmt = 1;
879 	auxtrace_error->ip = ip;
880 	auxtrace_error->time = timestamp;
881 	strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
882 
883 	size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
884 	       strlen(auxtrace_error->msg) + 1;
885 	auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
886 }
887 
888 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
889 					 struct perf_tool *tool,
890 					 struct perf_session *session,
891 					 perf_event__handler_t process)
892 {
893 	union perf_event *ev;
894 	size_t priv_size;
895 	int err;
896 
897 	pr_debug2("Synthesizing auxtrace information\n");
898 	priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
899 	ev = zalloc(sizeof(struct perf_record_auxtrace_info) + priv_size);
900 	if (!ev)
901 		return -ENOMEM;
902 
903 	ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
904 	ev->auxtrace_info.header.size = sizeof(struct perf_record_auxtrace_info) +
905 					priv_size;
906 	err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
907 					 priv_size);
908 	if (err)
909 		goto out_free;
910 
911 	err = process(tool, ev, NULL, NULL);
912 out_free:
913 	free(ev);
914 	return err;
915 }
916 
917 int perf_event__process_auxtrace_info(struct perf_session *session,
918 				      union perf_event *event)
919 {
920 	enum auxtrace_type type = event->auxtrace_info.type;
921 
922 	if (dump_trace)
923 		fprintf(stdout, " type: %u\n", type);
924 
925 	switch (type) {
926 	case PERF_AUXTRACE_INTEL_PT:
927 		return intel_pt_process_auxtrace_info(event, session);
928 	case PERF_AUXTRACE_INTEL_BTS:
929 		return intel_bts_process_auxtrace_info(event, session);
930 	case PERF_AUXTRACE_ARM_SPE:
931 		return arm_spe_process_auxtrace_info(event, session);
932 	case PERF_AUXTRACE_CS_ETM:
933 		return cs_etm__process_auxtrace_info(event, session);
934 	case PERF_AUXTRACE_S390_CPUMSF:
935 		return s390_cpumsf_process_auxtrace_info(event, session);
936 	case PERF_AUXTRACE_UNKNOWN:
937 	default:
938 		return -EINVAL;
939 	}
940 }
941 
942 s64 perf_event__process_auxtrace(struct perf_session *session,
943 				 union perf_event *event)
944 {
945 	s64 err;
946 
947 	if (dump_trace)
948 		fprintf(stdout, " size: %#"PRI_lx64"  offset: %#"PRI_lx64"  ref: %#"PRI_lx64"  idx: %u  tid: %d  cpu: %d\n",
949 			event->auxtrace.size, event->auxtrace.offset,
950 			event->auxtrace.reference, event->auxtrace.idx,
951 			event->auxtrace.tid, event->auxtrace.cpu);
952 
953 	if (auxtrace__dont_decode(session))
954 		return event->auxtrace.size;
955 
956 	if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
957 		return -EINVAL;
958 
959 	err = session->auxtrace->process_auxtrace_event(session, event, session->tool);
960 	if (err < 0)
961 		return err;
962 
963 	return event->auxtrace.size;
964 }
965 
966 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE		PERF_ITRACE_PERIOD_NANOSECS
967 #define PERF_ITRACE_DEFAULT_PERIOD		100000
968 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ	16
969 #define PERF_ITRACE_MAX_CALLCHAIN_SZ		1024
970 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ	64
971 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ		1024
972 
973 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts,
974 				    bool no_sample)
975 {
976 	synth_opts->branches = true;
977 	synth_opts->transactions = true;
978 	synth_opts->ptwrites = true;
979 	synth_opts->pwr_events = true;
980 	synth_opts->other_events = true;
981 	synth_opts->errors = true;
982 	if (no_sample) {
983 		synth_opts->period_type = PERF_ITRACE_PERIOD_INSTRUCTIONS;
984 		synth_opts->period = 1;
985 		synth_opts->calls = true;
986 	} else {
987 		synth_opts->instructions = true;
988 		synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
989 		synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
990 	}
991 	synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
992 	synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
993 	synth_opts->initial_skip = 0;
994 }
995 
996 /*
997  * Please check tools/perf/Documentation/perf-script.txt for information
998  * about the options parsed here, which is introduced after this cset,
999  * when support in 'perf script' for these options is introduced.
1000  */
1001 int itrace_parse_synth_opts(const struct option *opt, const char *str,
1002 			    int unset)
1003 {
1004 	struct itrace_synth_opts *synth_opts = opt->value;
1005 	const char *p;
1006 	char *endptr;
1007 	bool period_type_set = false;
1008 	bool period_set = false;
1009 
1010 	synth_opts->set = true;
1011 
1012 	if (unset) {
1013 		synth_opts->dont_decode = true;
1014 		return 0;
1015 	}
1016 
1017 	if (!str) {
1018 		itrace_synth_opts__set_default(synth_opts,
1019 					       synth_opts->default_no_sample);
1020 		return 0;
1021 	}
1022 
1023 	for (p = str; *p;) {
1024 		switch (*p++) {
1025 		case 'i':
1026 			synth_opts->instructions = true;
1027 			while (*p == ' ' || *p == ',')
1028 				p += 1;
1029 			if (isdigit(*p)) {
1030 				synth_opts->period = strtoull(p, &endptr, 10);
1031 				period_set = true;
1032 				p = endptr;
1033 				while (*p == ' ' || *p == ',')
1034 					p += 1;
1035 				switch (*p++) {
1036 				case 'i':
1037 					synth_opts->period_type =
1038 						PERF_ITRACE_PERIOD_INSTRUCTIONS;
1039 					period_type_set = true;
1040 					break;
1041 				case 't':
1042 					synth_opts->period_type =
1043 						PERF_ITRACE_PERIOD_TICKS;
1044 					period_type_set = true;
1045 					break;
1046 				case 'm':
1047 					synth_opts->period *= 1000;
1048 					/* Fall through */
1049 				case 'u':
1050 					synth_opts->period *= 1000;
1051 					/* Fall through */
1052 				case 'n':
1053 					if (*p++ != 's')
1054 						goto out_err;
1055 					synth_opts->period_type =
1056 						PERF_ITRACE_PERIOD_NANOSECS;
1057 					period_type_set = true;
1058 					break;
1059 				case '\0':
1060 					goto out;
1061 				default:
1062 					goto out_err;
1063 				}
1064 			}
1065 			break;
1066 		case 'b':
1067 			synth_opts->branches = true;
1068 			break;
1069 		case 'x':
1070 			synth_opts->transactions = true;
1071 			break;
1072 		case 'w':
1073 			synth_opts->ptwrites = true;
1074 			break;
1075 		case 'p':
1076 			synth_opts->pwr_events = true;
1077 			break;
1078 		case 'o':
1079 			synth_opts->other_events = true;
1080 			break;
1081 		case 'e':
1082 			synth_opts->errors = true;
1083 			break;
1084 		case 'd':
1085 			synth_opts->log = true;
1086 			break;
1087 		case 'c':
1088 			synth_opts->branches = true;
1089 			synth_opts->calls = true;
1090 			break;
1091 		case 'r':
1092 			synth_opts->branches = true;
1093 			synth_opts->returns = true;
1094 			break;
1095 		case 'g':
1096 			synth_opts->callchain = true;
1097 			synth_opts->callchain_sz =
1098 					PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1099 			while (*p == ' ' || *p == ',')
1100 				p += 1;
1101 			if (isdigit(*p)) {
1102 				unsigned int val;
1103 
1104 				val = strtoul(p, &endptr, 10);
1105 				p = endptr;
1106 				if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1107 					goto out_err;
1108 				synth_opts->callchain_sz = val;
1109 			}
1110 			break;
1111 		case 'l':
1112 			synth_opts->last_branch = true;
1113 			synth_opts->last_branch_sz =
1114 					PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1115 			while (*p == ' ' || *p == ',')
1116 				p += 1;
1117 			if (isdigit(*p)) {
1118 				unsigned int val;
1119 
1120 				val = strtoul(p, &endptr, 10);
1121 				p = endptr;
1122 				if (!val ||
1123 				    val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1124 					goto out_err;
1125 				synth_opts->last_branch_sz = val;
1126 			}
1127 			break;
1128 		case 's':
1129 			synth_opts->initial_skip = strtoul(p, &endptr, 10);
1130 			if (p == endptr)
1131 				goto out_err;
1132 			p = endptr;
1133 			break;
1134 		case ' ':
1135 		case ',':
1136 			break;
1137 		default:
1138 			goto out_err;
1139 		}
1140 	}
1141 out:
1142 	if (synth_opts->instructions) {
1143 		if (!period_type_set)
1144 			synth_opts->period_type =
1145 					PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1146 		if (!period_set)
1147 			synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1148 	}
1149 
1150 	return 0;
1151 
1152 out_err:
1153 	pr_err("Bad Instruction Tracing options '%s'\n", str);
1154 	return -EINVAL;
1155 }
1156 
1157 static const char * const auxtrace_error_type_name[] = {
1158 	[PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1159 };
1160 
1161 static const char *auxtrace_error_name(int type)
1162 {
1163 	const char *error_type_name = NULL;
1164 
1165 	if (type < PERF_AUXTRACE_ERROR_MAX)
1166 		error_type_name = auxtrace_error_type_name[type];
1167 	if (!error_type_name)
1168 		error_type_name = "unknown AUX";
1169 	return error_type_name;
1170 }
1171 
1172 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1173 {
1174 	struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1175 	unsigned long long nsecs = e->time;
1176 	const char *msg = e->msg;
1177 	int ret;
1178 
1179 	ret = fprintf(fp, " %s error type %u",
1180 		      auxtrace_error_name(e->type), e->type);
1181 
1182 	if (e->fmt && nsecs) {
1183 		unsigned long secs = nsecs / NSEC_PER_SEC;
1184 
1185 		nsecs -= secs * NSEC_PER_SEC;
1186 		ret += fprintf(fp, " time %lu.%09llu", secs, nsecs);
1187 	} else {
1188 		ret += fprintf(fp, " time 0");
1189 	}
1190 
1191 	if (!e->fmt)
1192 		msg = (const char *)&e->time;
1193 
1194 	ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRI_lx64" code %u: %s\n",
1195 		       e->cpu, e->pid, e->tid, e->ip, e->code, msg);
1196 	return ret;
1197 }
1198 
1199 void perf_session__auxtrace_error_inc(struct perf_session *session,
1200 				      union perf_event *event)
1201 {
1202 	struct perf_record_auxtrace_error *e = &event->auxtrace_error;
1203 
1204 	if (e->type < PERF_AUXTRACE_ERROR_MAX)
1205 		session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1206 }
1207 
1208 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1209 {
1210 	int i;
1211 
1212 	for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1213 		if (!stats->nr_auxtrace_errors[i])
1214 			continue;
1215 		ui__warning("%u %s errors\n",
1216 			    stats->nr_auxtrace_errors[i],
1217 			    auxtrace_error_name(i));
1218 	}
1219 }
1220 
1221 int perf_event__process_auxtrace_error(struct perf_session *session,
1222 				       union perf_event *event)
1223 {
1224 	if (auxtrace__dont_decode(session))
1225 		return 0;
1226 
1227 	perf_event__fprintf_auxtrace_error(event, stdout);
1228 	return 0;
1229 }
1230 
1231 static int __auxtrace_mmap__read(struct mmap *map,
1232 				 struct auxtrace_record *itr,
1233 				 struct perf_tool *tool, process_auxtrace_t fn,
1234 				 bool snapshot, size_t snapshot_size)
1235 {
1236 	struct auxtrace_mmap *mm = &map->auxtrace_mmap;
1237 	u64 head, old = mm->prev, offset, ref;
1238 	unsigned char *data = mm->base;
1239 	size_t size, head_off, old_off, len1, len2, padding;
1240 	union perf_event ev;
1241 	void *data1, *data2;
1242 
1243 	if (snapshot) {
1244 		head = auxtrace_mmap__read_snapshot_head(mm);
1245 		if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1246 						   &head, &old))
1247 			return -1;
1248 	} else {
1249 		head = auxtrace_mmap__read_head(mm);
1250 	}
1251 
1252 	if (old == head)
1253 		return 0;
1254 
1255 	pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1256 		  mm->idx, old, head, head - old);
1257 
1258 	if (mm->mask) {
1259 		head_off = head & mm->mask;
1260 		old_off = old & mm->mask;
1261 	} else {
1262 		head_off = head % mm->len;
1263 		old_off = old % mm->len;
1264 	}
1265 
1266 	if (head_off > old_off)
1267 		size = head_off - old_off;
1268 	else
1269 		size = mm->len - (old_off - head_off);
1270 
1271 	if (snapshot && size > snapshot_size)
1272 		size = snapshot_size;
1273 
1274 	ref = auxtrace_record__reference(itr);
1275 
1276 	if (head > old || size <= head || mm->mask) {
1277 		offset = head - size;
1278 	} else {
1279 		/*
1280 		 * When the buffer size is not a power of 2, 'head' wraps at the
1281 		 * highest multiple of the buffer size, so we have to subtract
1282 		 * the remainder here.
1283 		 */
1284 		u64 rem = (0ULL - mm->len) % mm->len;
1285 
1286 		offset = head - size - rem;
1287 	}
1288 
1289 	if (size > head_off) {
1290 		len1 = size - head_off;
1291 		data1 = &data[mm->len - len1];
1292 		len2 = head_off;
1293 		data2 = &data[0];
1294 	} else {
1295 		len1 = size;
1296 		data1 = &data[head_off - len1];
1297 		len2 = 0;
1298 		data2 = NULL;
1299 	}
1300 
1301 	if (itr->alignment) {
1302 		unsigned int unwanted = len1 % itr->alignment;
1303 
1304 		len1 -= unwanted;
1305 		size -= unwanted;
1306 	}
1307 
1308 	/* padding must be written by fn() e.g. record__process_auxtrace() */
1309 	padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1310 	if (padding)
1311 		padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1312 
1313 	memset(&ev, 0, sizeof(ev));
1314 	ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1315 	ev.auxtrace.header.size = sizeof(ev.auxtrace);
1316 	ev.auxtrace.size = size + padding;
1317 	ev.auxtrace.offset = offset;
1318 	ev.auxtrace.reference = ref;
1319 	ev.auxtrace.idx = mm->idx;
1320 	ev.auxtrace.tid = mm->tid;
1321 	ev.auxtrace.cpu = mm->cpu;
1322 
1323 	if (fn(tool, map, &ev, data1, len1, data2, len2))
1324 		return -1;
1325 
1326 	mm->prev = head;
1327 
1328 	if (!snapshot) {
1329 		auxtrace_mmap__write_tail(mm, head);
1330 		if (itr->read_finish) {
1331 			int err;
1332 
1333 			err = itr->read_finish(itr, mm->idx);
1334 			if (err < 0)
1335 				return err;
1336 		}
1337 	}
1338 
1339 	return 1;
1340 }
1341 
1342 int auxtrace_mmap__read(struct mmap *map, struct auxtrace_record *itr,
1343 			struct perf_tool *tool, process_auxtrace_t fn)
1344 {
1345 	return __auxtrace_mmap__read(map, itr, tool, fn, false, 0);
1346 }
1347 
1348 int auxtrace_mmap__read_snapshot(struct mmap *map,
1349 				 struct auxtrace_record *itr,
1350 				 struct perf_tool *tool, process_auxtrace_t fn,
1351 				 size_t snapshot_size)
1352 {
1353 	return __auxtrace_mmap__read(map, itr, tool, fn, true, snapshot_size);
1354 }
1355 
1356 /**
1357  * struct auxtrace_cache - hash table to implement a cache
1358  * @hashtable: the hashtable
1359  * @sz: hashtable size (number of hlists)
1360  * @entry_size: size of an entry
1361  * @limit: limit the number of entries to this maximum, when reached the cache
1362  *         is dropped and caching begins again with an empty cache
1363  * @cnt: current number of entries
1364  * @bits: hashtable size (@sz = 2^@bits)
1365  */
1366 struct auxtrace_cache {
1367 	struct hlist_head *hashtable;
1368 	size_t sz;
1369 	size_t entry_size;
1370 	size_t limit;
1371 	size_t cnt;
1372 	unsigned int bits;
1373 };
1374 
1375 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1376 					   unsigned int limit_percent)
1377 {
1378 	struct auxtrace_cache *c;
1379 	struct hlist_head *ht;
1380 	size_t sz, i;
1381 
1382 	c = zalloc(sizeof(struct auxtrace_cache));
1383 	if (!c)
1384 		return NULL;
1385 
1386 	sz = 1UL << bits;
1387 
1388 	ht = calloc(sz, sizeof(struct hlist_head));
1389 	if (!ht)
1390 		goto out_free;
1391 
1392 	for (i = 0; i < sz; i++)
1393 		INIT_HLIST_HEAD(&ht[i]);
1394 
1395 	c->hashtable = ht;
1396 	c->sz = sz;
1397 	c->entry_size = entry_size;
1398 	c->limit = (c->sz * limit_percent) / 100;
1399 	c->bits = bits;
1400 
1401 	return c;
1402 
1403 out_free:
1404 	free(c);
1405 	return NULL;
1406 }
1407 
1408 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1409 {
1410 	struct auxtrace_cache_entry *entry;
1411 	struct hlist_node *tmp;
1412 	size_t i;
1413 
1414 	if (!c)
1415 		return;
1416 
1417 	for (i = 0; i < c->sz; i++) {
1418 		hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1419 			hlist_del(&entry->hash);
1420 			auxtrace_cache__free_entry(c, entry);
1421 		}
1422 	}
1423 
1424 	c->cnt = 0;
1425 }
1426 
1427 void auxtrace_cache__free(struct auxtrace_cache *c)
1428 {
1429 	if (!c)
1430 		return;
1431 
1432 	auxtrace_cache__drop(c);
1433 	zfree(&c->hashtable);
1434 	free(c);
1435 }
1436 
1437 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1438 {
1439 	return malloc(c->entry_size);
1440 }
1441 
1442 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1443 				void *entry)
1444 {
1445 	free(entry);
1446 }
1447 
1448 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1449 			struct auxtrace_cache_entry *entry)
1450 {
1451 	if (c->limit && ++c->cnt > c->limit)
1452 		auxtrace_cache__drop(c);
1453 
1454 	entry->key = key;
1455 	hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1456 
1457 	return 0;
1458 }
1459 
1460 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1461 {
1462 	struct auxtrace_cache_entry *entry;
1463 	struct hlist_head *hlist;
1464 
1465 	if (!c)
1466 		return NULL;
1467 
1468 	hlist = &c->hashtable[hash_32(key, c->bits)];
1469 	hlist_for_each_entry(entry, hlist, hash) {
1470 		if (entry->key == key)
1471 			return entry;
1472 	}
1473 
1474 	return NULL;
1475 }
1476 
1477 static void addr_filter__free_str(struct addr_filter *filt)
1478 {
1479 	zfree(&filt->str);
1480 	filt->action   = NULL;
1481 	filt->sym_from = NULL;
1482 	filt->sym_to   = NULL;
1483 	filt->filename = NULL;
1484 }
1485 
1486 static struct addr_filter *addr_filter__new(void)
1487 {
1488 	struct addr_filter *filt = zalloc(sizeof(*filt));
1489 
1490 	if (filt)
1491 		INIT_LIST_HEAD(&filt->list);
1492 
1493 	return filt;
1494 }
1495 
1496 static void addr_filter__free(struct addr_filter *filt)
1497 {
1498 	if (filt)
1499 		addr_filter__free_str(filt);
1500 	free(filt);
1501 }
1502 
1503 static void addr_filters__add(struct addr_filters *filts,
1504 			      struct addr_filter *filt)
1505 {
1506 	list_add_tail(&filt->list, &filts->head);
1507 	filts->cnt += 1;
1508 }
1509 
1510 static void addr_filters__del(struct addr_filters *filts,
1511 			      struct addr_filter *filt)
1512 {
1513 	list_del_init(&filt->list);
1514 	filts->cnt -= 1;
1515 }
1516 
1517 void addr_filters__init(struct addr_filters *filts)
1518 {
1519 	INIT_LIST_HEAD(&filts->head);
1520 	filts->cnt = 0;
1521 }
1522 
1523 void addr_filters__exit(struct addr_filters *filts)
1524 {
1525 	struct addr_filter *filt, *n;
1526 
1527 	list_for_each_entry_safe(filt, n, &filts->head, list) {
1528 		addr_filters__del(filts, filt);
1529 		addr_filter__free(filt);
1530 	}
1531 }
1532 
1533 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1534 			    const char *str_delim)
1535 {
1536 	*inp += strspn(*inp, " ");
1537 
1538 	if (isdigit(**inp)) {
1539 		char *endptr;
1540 
1541 		if (!num)
1542 			return -EINVAL;
1543 		errno = 0;
1544 		*num = strtoull(*inp, &endptr, 0);
1545 		if (errno)
1546 			return -errno;
1547 		if (endptr == *inp)
1548 			return -EINVAL;
1549 		*inp = endptr;
1550 	} else {
1551 		size_t n;
1552 
1553 		if (!str)
1554 			return -EINVAL;
1555 		*inp += strspn(*inp, " ");
1556 		*str = *inp;
1557 		n = strcspn(*inp, str_delim);
1558 		if (!n)
1559 			return -EINVAL;
1560 		*inp += n;
1561 		if (**inp) {
1562 			**inp = '\0';
1563 			*inp += 1;
1564 		}
1565 	}
1566 	return 0;
1567 }
1568 
1569 static int parse_action(struct addr_filter *filt)
1570 {
1571 	if (!strcmp(filt->action, "filter")) {
1572 		filt->start = true;
1573 		filt->range = true;
1574 	} else if (!strcmp(filt->action, "start")) {
1575 		filt->start = true;
1576 	} else if (!strcmp(filt->action, "stop")) {
1577 		filt->start = false;
1578 	} else if (!strcmp(filt->action, "tracestop")) {
1579 		filt->start = false;
1580 		filt->range = true;
1581 		filt->action += 5; /* Change 'tracestop' to 'stop' */
1582 	} else {
1583 		return -EINVAL;
1584 	}
1585 	return 0;
1586 }
1587 
1588 static int parse_sym_idx(char **inp, int *idx)
1589 {
1590 	*idx = -1;
1591 
1592 	*inp += strspn(*inp, " ");
1593 
1594 	if (**inp != '#')
1595 		return 0;
1596 
1597 	*inp += 1;
1598 
1599 	if (**inp == 'g' || **inp == 'G') {
1600 		*inp += 1;
1601 		*idx = 0;
1602 	} else {
1603 		unsigned long num;
1604 		char *endptr;
1605 
1606 		errno = 0;
1607 		num = strtoul(*inp, &endptr, 0);
1608 		if (errno)
1609 			return -errno;
1610 		if (endptr == *inp || num > INT_MAX)
1611 			return -EINVAL;
1612 		*inp = endptr;
1613 		*idx = num;
1614 	}
1615 
1616 	return 0;
1617 }
1618 
1619 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
1620 {
1621 	int err = parse_num_or_str(inp, num, str, " ");
1622 
1623 	if (!err && *str)
1624 		err = parse_sym_idx(inp, idx);
1625 
1626 	return err;
1627 }
1628 
1629 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
1630 {
1631 	char *fstr;
1632 	int err;
1633 
1634 	filt->str = fstr = strdup(*filter_inp);
1635 	if (!fstr)
1636 		return -ENOMEM;
1637 
1638 	err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
1639 	if (err)
1640 		goto out_err;
1641 
1642 	err = parse_action(filt);
1643 	if (err)
1644 		goto out_err;
1645 
1646 	err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
1647 			      &filt->sym_from_idx);
1648 	if (err)
1649 		goto out_err;
1650 
1651 	fstr += strspn(fstr, " ");
1652 
1653 	if (*fstr == '/') {
1654 		fstr += 1;
1655 		err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
1656 				      &filt->sym_to_idx);
1657 		if (err)
1658 			goto out_err;
1659 		filt->range = true;
1660 	}
1661 
1662 	fstr += strspn(fstr, " ");
1663 
1664 	if (*fstr == '@') {
1665 		fstr += 1;
1666 		err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
1667 		if (err)
1668 			goto out_err;
1669 	}
1670 
1671 	fstr += strspn(fstr, " ,");
1672 
1673 	*filter_inp += fstr - filt->str;
1674 
1675 	return 0;
1676 
1677 out_err:
1678 	addr_filter__free_str(filt);
1679 
1680 	return err;
1681 }
1682 
1683 int addr_filters__parse_bare_filter(struct addr_filters *filts,
1684 				    const char *filter)
1685 {
1686 	struct addr_filter *filt;
1687 	const char *fstr = filter;
1688 	int err;
1689 
1690 	while (*fstr) {
1691 		filt = addr_filter__new();
1692 		err = parse_one_filter(filt, &fstr);
1693 		if (err) {
1694 			addr_filter__free(filt);
1695 			addr_filters__exit(filts);
1696 			return err;
1697 		}
1698 		addr_filters__add(filts, filt);
1699 	}
1700 
1701 	return 0;
1702 }
1703 
1704 struct sym_args {
1705 	const char	*name;
1706 	u64		start;
1707 	u64		size;
1708 	int		idx;
1709 	int		cnt;
1710 	bool		started;
1711 	bool		global;
1712 	bool		selected;
1713 	bool		duplicate;
1714 	bool		near;
1715 };
1716 
1717 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
1718 {
1719 	/* A function with the same name, and global or the n'th found or any */
1720 	return kallsyms__is_function(type) &&
1721 	       !strcmp(name, args->name) &&
1722 	       ((args->global && isupper(type)) ||
1723 		(args->selected && ++(args->cnt) == args->idx) ||
1724 		(!args->global && !args->selected));
1725 }
1726 
1727 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1728 {
1729 	struct sym_args *args = arg;
1730 
1731 	if (args->started) {
1732 		if (!args->size)
1733 			args->size = start - args->start;
1734 		if (args->selected) {
1735 			if (args->size)
1736 				return 1;
1737 		} else if (kern_sym_match(args, name, type)) {
1738 			args->duplicate = true;
1739 			return 1;
1740 		}
1741 	} else if (kern_sym_match(args, name, type)) {
1742 		args->started = true;
1743 		args->start = start;
1744 	}
1745 
1746 	return 0;
1747 }
1748 
1749 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1750 {
1751 	struct sym_args *args = arg;
1752 
1753 	if (kern_sym_match(args, name, type)) {
1754 		pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1755 		       ++args->cnt, start, type, name);
1756 		args->near = true;
1757 	} else if (args->near) {
1758 		args->near = false;
1759 		pr_err("\t\twhich is near\t\t%s\n", name);
1760 	}
1761 
1762 	return 0;
1763 }
1764 
1765 static int sym_not_found_error(const char *sym_name, int idx)
1766 {
1767 	if (idx > 0) {
1768 		pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1769 		       idx, sym_name);
1770 	} else if (!idx) {
1771 		pr_err("Global symbol '%s' not found.\n", sym_name);
1772 	} else {
1773 		pr_err("Symbol '%s' not found.\n", sym_name);
1774 	}
1775 	pr_err("Note that symbols must be functions.\n");
1776 
1777 	return -EINVAL;
1778 }
1779 
1780 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
1781 {
1782 	struct sym_args args = {
1783 		.name = sym_name,
1784 		.idx = idx,
1785 		.global = !idx,
1786 		.selected = idx > 0,
1787 	};
1788 	int err;
1789 
1790 	*start = 0;
1791 	*size = 0;
1792 
1793 	err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
1794 	if (err < 0) {
1795 		pr_err("Failed to parse /proc/kallsyms\n");
1796 		return err;
1797 	}
1798 
1799 	if (args.duplicate) {
1800 		pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
1801 		args.cnt = 0;
1802 		kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
1803 		pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1804 		       sym_name);
1805 		pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1806 		return -EINVAL;
1807 	}
1808 
1809 	if (!args.started) {
1810 		pr_err("Kernel symbol lookup: ");
1811 		return sym_not_found_error(sym_name, idx);
1812 	}
1813 
1814 	*start = args.start;
1815 	*size = args.size;
1816 
1817 	return 0;
1818 }
1819 
1820 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
1821 			       char type, u64 start)
1822 {
1823 	struct sym_args *args = arg;
1824 
1825 	if (!kallsyms__is_function(type))
1826 		return 0;
1827 
1828 	if (!args->started) {
1829 		args->started = true;
1830 		args->start = start;
1831 	}
1832 	/* Don't know exactly where the kernel ends, so we add a page */
1833 	args->size = round_up(start, page_size) + page_size - args->start;
1834 
1835 	return 0;
1836 }
1837 
1838 static int addr_filter__entire_kernel(struct addr_filter *filt)
1839 {
1840 	struct sym_args args = { .started = false };
1841 	int err;
1842 
1843 	err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
1844 	if (err < 0 || !args.started) {
1845 		pr_err("Failed to parse /proc/kallsyms\n");
1846 		return err;
1847 	}
1848 
1849 	filt->addr = args.start;
1850 	filt->size = args.size;
1851 
1852 	return 0;
1853 }
1854 
1855 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
1856 {
1857 	if (start + size >= filt->addr)
1858 		return 0;
1859 
1860 	if (filt->sym_from) {
1861 		pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
1862 		       filt->sym_to, start, filt->sym_from, filt->addr);
1863 	} else {
1864 		pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
1865 		       filt->sym_to, start, filt->addr);
1866 	}
1867 
1868 	return -EINVAL;
1869 }
1870 
1871 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
1872 {
1873 	bool no_size = false;
1874 	u64 start, size;
1875 	int err;
1876 
1877 	if (symbol_conf.kptr_restrict) {
1878 		pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1879 		return -EINVAL;
1880 	}
1881 
1882 	if (filt->sym_from && !strcmp(filt->sym_from, "*"))
1883 		return addr_filter__entire_kernel(filt);
1884 
1885 	if (filt->sym_from) {
1886 		err = find_kern_sym(filt->sym_from, &start, &size,
1887 				    filt->sym_from_idx);
1888 		if (err)
1889 			return err;
1890 		filt->addr = start;
1891 		if (filt->range && !filt->size && !filt->sym_to) {
1892 			filt->size = size;
1893 			no_size = !size;
1894 		}
1895 	}
1896 
1897 	if (filt->sym_to) {
1898 		err = find_kern_sym(filt->sym_to, &start, &size,
1899 				    filt->sym_to_idx);
1900 		if (err)
1901 			return err;
1902 
1903 		err = check_end_after_start(filt, start, size);
1904 		if (err)
1905 			return err;
1906 		filt->size = start + size - filt->addr;
1907 		no_size = !size;
1908 	}
1909 
1910 	/* The very last symbol in kallsyms does not imply a particular size */
1911 	if (no_size) {
1912 		pr_err("Cannot determine size of symbol '%s'\n",
1913 		       filt->sym_to ? filt->sym_to : filt->sym_from);
1914 		return -EINVAL;
1915 	}
1916 
1917 	return 0;
1918 }
1919 
1920 static struct dso *load_dso(const char *name)
1921 {
1922 	struct map *map;
1923 	struct dso *dso;
1924 
1925 	map = dso__new_map(name);
1926 	if (!map)
1927 		return NULL;
1928 
1929 	if (map__load(map) < 0)
1930 		pr_err("File '%s' not found or has no symbols.\n", name);
1931 
1932 	dso = dso__get(map->dso);
1933 
1934 	map__put(map);
1935 
1936 	return dso;
1937 }
1938 
1939 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
1940 			  int idx)
1941 {
1942 	/* Same name, and global or the n'th found or any */
1943 	return !arch__compare_symbol_names(name, sym->name) &&
1944 	       ((!idx && sym->binding == STB_GLOBAL) ||
1945 		(idx > 0 && ++*cnt == idx) ||
1946 		idx < 0);
1947 }
1948 
1949 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
1950 {
1951 	struct symbol *sym;
1952 	bool near = false;
1953 	int cnt = 0;
1954 
1955 	pr_err("Multiple symbols with name '%s'\n", sym_name);
1956 
1957 	sym = dso__first_symbol(dso);
1958 	while (sym) {
1959 		if (dso_sym_match(sym, sym_name, &cnt, -1)) {
1960 			pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1961 			       ++cnt, sym->start,
1962 			       sym->binding == STB_GLOBAL ? 'g' :
1963 			       sym->binding == STB_LOCAL  ? 'l' : 'w',
1964 			       sym->name);
1965 			near = true;
1966 		} else if (near) {
1967 			near = false;
1968 			pr_err("\t\twhich is near\t\t%s\n", sym->name);
1969 		}
1970 		sym = dso__next_symbol(sym);
1971 	}
1972 
1973 	pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1974 	       sym_name);
1975 	pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1976 }
1977 
1978 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
1979 			u64 *size, int idx)
1980 {
1981 	struct symbol *sym;
1982 	int cnt = 0;
1983 
1984 	*start = 0;
1985 	*size = 0;
1986 
1987 	sym = dso__first_symbol(dso);
1988 	while (sym) {
1989 		if (*start) {
1990 			if (!*size)
1991 				*size = sym->start - *start;
1992 			if (idx > 0) {
1993 				if (*size)
1994 					return 1;
1995 			} else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1996 				print_duplicate_syms(dso, sym_name);
1997 				return -EINVAL;
1998 			}
1999 		} else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
2000 			*start = sym->start;
2001 			*size = sym->end - sym->start;
2002 		}
2003 		sym = dso__next_symbol(sym);
2004 	}
2005 
2006 	if (!*start)
2007 		return sym_not_found_error(sym_name, idx);
2008 
2009 	return 0;
2010 }
2011 
2012 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
2013 {
2014 	if (dso__data_file_size(dso, NULL)) {
2015 		pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2016 		       filt->filename);
2017 		return -EINVAL;
2018 	}
2019 
2020 	filt->addr = 0;
2021 	filt->size = dso->data.file_size;
2022 
2023 	return 0;
2024 }
2025 
2026 static int addr_filter__resolve_syms(struct addr_filter *filt)
2027 {
2028 	u64 start, size;
2029 	struct dso *dso;
2030 	int err = 0;
2031 
2032 	if (!filt->sym_from && !filt->sym_to)
2033 		return 0;
2034 
2035 	if (!filt->filename)
2036 		return addr_filter__resolve_kernel_syms(filt);
2037 
2038 	dso = load_dso(filt->filename);
2039 	if (!dso) {
2040 		pr_err("Failed to load symbols from: %s\n", filt->filename);
2041 		return -EINVAL;
2042 	}
2043 
2044 	if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2045 		err = addr_filter__entire_dso(filt, dso);
2046 		goto put_dso;
2047 	}
2048 
2049 	if (filt->sym_from) {
2050 		err = find_dso_sym(dso, filt->sym_from, &start, &size,
2051 				   filt->sym_from_idx);
2052 		if (err)
2053 			goto put_dso;
2054 		filt->addr = start;
2055 		if (filt->range && !filt->size && !filt->sym_to)
2056 			filt->size = size;
2057 	}
2058 
2059 	if (filt->sym_to) {
2060 		err = find_dso_sym(dso, filt->sym_to, &start, &size,
2061 				   filt->sym_to_idx);
2062 		if (err)
2063 			goto put_dso;
2064 
2065 		err = check_end_after_start(filt, start, size);
2066 		if (err)
2067 			return err;
2068 
2069 		filt->size = start + size - filt->addr;
2070 	}
2071 
2072 put_dso:
2073 	dso__put(dso);
2074 
2075 	return err;
2076 }
2077 
2078 static char *addr_filter__to_str(struct addr_filter *filt)
2079 {
2080 	char filename_buf[PATH_MAX];
2081 	const char *at = "";
2082 	const char *fn = "";
2083 	char *filter;
2084 	int err;
2085 
2086 	if (filt->filename) {
2087 		at = "@";
2088 		fn = realpath(filt->filename, filename_buf);
2089 		if (!fn)
2090 			return NULL;
2091 	}
2092 
2093 	if (filt->range) {
2094 		err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2095 			       filt->action, filt->addr, filt->size, at, fn);
2096 	} else {
2097 		err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2098 			       filt->action, filt->addr, at, fn);
2099 	}
2100 
2101 	return err < 0 ? NULL : filter;
2102 }
2103 
2104 static int parse_addr_filter(struct evsel *evsel, const char *filter,
2105 			     int max_nr)
2106 {
2107 	struct addr_filters filts;
2108 	struct addr_filter *filt;
2109 	int err;
2110 
2111 	addr_filters__init(&filts);
2112 
2113 	err = addr_filters__parse_bare_filter(&filts, filter);
2114 	if (err)
2115 		goto out_exit;
2116 
2117 	if (filts.cnt > max_nr) {
2118 		pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2119 		       filts.cnt, max_nr);
2120 		err = -EINVAL;
2121 		goto out_exit;
2122 	}
2123 
2124 	list_for_each_entry(filt, &filts.head, list) {
2125 		char *new_filter;
2126 
2127 		err = addr_filter__resolve_syms(filt);
2128 		if (err)
2129 			goto out_exit;
2130 
2131 		new_filter = addr_filter__to_str(filt);
2132 		if (!new_filter) {
2133 			err = -ENOMEM;
2134 			goto out_exit;
2135 		}
2136 
2137 		if (perf_evsel__append_addr_filter(evsel, new_filter)) {
2138 			err = -ENOMEM;
2139 			goto out_exit;
2140 		}
2141 	}
2142 
2143 out_exit:
2144 	addr_filters__exit(&filts);
2145 
2146 	if (err) {
2147 		pr_err("Failed to parse address filter: '%s'\n", filter);
2148 		pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2149 		pr_err("Where multiple filters are separated by space or comma.\n");
2150 	}
2151 
2152 	return err;
2153 }
2154 
2155 static struct perf_pmu *perf_evsel__find_pmu(struct evsel *evsel)
2156 {
2157 	struct perf_pmu *pmu = NULL;
2158 
2159 	while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2160 		if (pmu->type == evsel->core.attr.type)
2161 			break;
2162 	}
2163 
2164 	return pmu;
2165 }
2166 
2167 static int perf_evsel__nr_addr_filter(struct evsel *evsel)
2168 {
2169 	struct perf_pmu *pmu = perf_evsel__find_pmu(evsel);
2170 	int nr_addr_filters = 0;
2171 
2172 	if (!pmu)
2173 		return 0;
2174 
2175 	perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2176 
2177 	return nr_addr_filters;
2178 }
2179 
2180 int auxtrace_parse_filters(struct evlist *evlist)
2181 {
2182 	struct evsel *evsel;
2183 	char *filter;
2184 	int err, max_nr;
2185 
2186 	evlist__for_each_entry(evlist, evsel) {
2187 		filter = evsel->filter;
2188 		max_nr = perf_evsel__nr_addr_filter(evsel);
2189 		if (!filter || !max_nr)
2190 			continue;
2191 		evsel->filter = NULL;
2192 		err = parse_addr_filter(evsel, filter, max_nr);
2193 		free(filter);
2194 		if (err)
2195 			return err;
2196 		pr_debug("Address filter: %s\n", evsel->filter);
2197 	}
2198 
2199 	return 0;
2200 }
2201 
2202 int auxtrace__process_event(struct perf_session *session, union perf_event *event,
2203 			    struct perf_sample *sample, struct perf_tool *tool)
2204 {
2205 	if (!session->auxtrace)
2206 		return 0;
2207 
2208 	return session->auxtrace->process_event(session, event, sample, tool);
2209 }
2210 
2211 int auxtrace__flush_events(struct perf_session *session, struct perf_tool *tool)
2212 {
2213 	if (!session->auxtrace)
2214 		return 0;
2215 
2216 	return session->auxtrace->flush_events(session, tool);
2217 }
2218 
2219 void auxtrace__free_events(struct perf_session *session)
2220 {
2221 	if (!session->auxtrace)
2222 		return;
2223 
2224 	return session->auxtrace->free_events(session);
2225 }
2226 
2227 void auxtrace__free(struct perf_session *session)
2228 {
2229 	if (!session->auxtrace)
2230 		return;
2231 
2232 	return session->auxtrace->free(session);
2233 }
2234