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