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