xref: /openbmc/linux/tools/perf/util/auxtrace.c (revision 15b7cc78)
1 /*
2  * auxtrace.c: AUX area trace support
3  * Copyright (c) 2013-2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 
16 #include <sys/types.h>
17 #include <sys/mman.h>
18 #include <stdbool.h>
19 
20 #include <linux/kernel.h>
21 #include <linux/perf_event.h>
22 #include <linux/types.h>
23 #include <linux/bitops.h>
24 #include <linux/log2.h>
25 #include <linux/string.h>
26 
27 #include <sys/param.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <errno.h>
33 #include <linux/list.h>
34 
35 #include "../perf.h"
36 #include "util.h"
37 #include "evlist.h"
38 #include "cpumap.h"
39 #include "thread_map.h"
40 #include "asm/bug.h"
41 #include "auxtrace.h"
42 
43 #include <linux/hash.h>
44 
45 #include "event.h"
46 #include "session.h"
47 #include "debug.h"
48 #include <subcmd/parse-options.h>
49 
50 #include "intel-pt.h"
51 #include "intel-bts.h"
52 
53 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
54 			struct auxtrace_mmap_params *mp,
55 			void *userpg, int fd)
56 {
57 	struct perf_event_mmap_page *pc = userpg;
58 
59 	WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
60 
61 	mm->userpg = userpg;
62 	mm->mask = mp->mask;
63 	mm->len = mp->len;
64 	mm->prev = 0;
65 	mm->idx = mp->idx;
66 	mm->tid = mp->tid;
67 	mm->cpu = mp->cpu;
68 
69 	if (!mp->len) {
70 		mm->base = NULL;
71 		return 0;
72 	}
73 
74 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
75 	pr_err("Cannot use AUX area tracing mmaps\n");
76 	return -1;
77 #endif
78 
79 	pc->aux_offset = mp->offset;
80 	pc->aux_size = mp->len;
81 
82 	mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
83 	if (mm->base == MAP_FAILED) {
84 		pr_debug2("failed to mmap AUX area\n");
85 		mm->base = NULL;
86 		return -1;
87 	}
88 
89 	return 0;
90 }
91 
92 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
93 {
94 	if (mm->base) {
95 		munmap(mm->base, mm->len);
96 		mm->base = NULL;
97 	}
98 }
99 
100 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
101 				off_t auxtrace_offset,
102 				unsigned int auxtrace_pages,
103 				bool auxtrace_overwrite)
104 {
105 	if (auxtrace_pages) {
106 		mp->offset = auxtrace_offset;
107 		mp->len = auxtrace_pages * (size_t)page_size;
108 		mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
109 		mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
110 		pr_debug2("AUX area mmap length %zu\n", mp->len);
111 	} else {
112 		mp->len = 0;
113 	}
114 }
115 
116 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
117 				   struct perf_evlist *evlist, int idx,
118 				   bool per_cpu)
119 {
120 	mp->idx = idx;
121 
122 	if (per_cpu) {
123 		mp->cpu = evlist->cpus->map[idx];
124 		if (evlist->threads)
125 			mp->tid = thread_map__pid(evlist->threads, 0);
126 		else
127 			mp->tid = -1;
128 	} else {
129 		mp->cpu = -1;
130 		mp->tid = thread_map__pid(evlist->threads, idx);
131 	}
132 }
133 
134 #define AUXTRACE_INIT_NR_QUEUES	32
135 
136 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
137 {
138 	struct auxtrace_queue *queue_array;
139 	unsigned int max_nr_queues, i;
140 
141 	max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
142 	if (nr_queues > max_nr_queues)
143 		return NULL;
144 
145 	queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
146 	if (!queue_array)
147 		return NULL;
148 
149 	for (i = 0; i < nr_queues; i++) {
150 		INIT_LIST_HEAD(&queue_array[i].head);
151 		queue_array[i].priv = NULL;
152 	}
153 
154 	return queue_array;
155 }
156 
157 int auxtrace_queues__init(struct auxtrace_queues *queues)
158 {
159 	queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
160 	queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
161 	if (!queues->queue_array)
162 		return -ENOMEM;
163 	return 0;
164 }
165 
166 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
167 				 unsigned int new_nr_queues)
168 {
169 	unsigned int nr_queues = queues->nr_queues;
170 	struct auxtrace_queue *queue_array;
171 	unsigned int i;
172 
173 	if (!nr_queues)
174 		nr_queues = AUXTRACE_INIT_NR_QUEUES;
175 
176 	while (nr_queues && nr_queues < new_nr_queues)
177 		nr_queues <<= 1;
178 
179 	if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
180 		return -EINVAL;
181 
182 	queue_array = auxtrace_alloc_queue_array(nr_queues);
183 	if (!queue_array)
184 		return -ENOMEM;
185 
186 	for (i = 0; i < queues->nr_queues; i++) {
187 		list_splice_tail(&queues->queue_array[i].head,
188 				 &queue_array[i].head);
189 		queue_array[i].priv = queues->queue_array[i].priv;
190 	}
191 
192 	queues->nr_queues = nr_queues;
193 	queues->queue_array = queue_array;
194 
195 	return 0;
196 }
197 
198 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
199 {
200 	int fd = perf_data_file__fd(session->file);
201 	void *p;
202 	ssize_t ret;
203 
204 	if (size > SSIZE_MAX)
205 		return NULL;
206 
207 	p = malloc(size);
208 	if (!p)
209 		return NULL;
210 
211 	ret = readn(fd, p, size);
212 	if (ret != (ssize_t)size) {
213 		free(p);
214 		return NULL;
215 	}
216 
217 	return p;
218 }
219 
220 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
221 				       unsigned int idx,
222 				       struct auxtrace_buffer *buffer)
223 {
224 	struct auxtrace_queue *queue;
225 	int err;
226 
227 	if (idx >= queues->nr_queues) {
228 		err = auxtrace_queues__grow(queues, idx + 1);
229 		if (err)
230 			return err;
231 	}
232 
233 	queue = &queues->queue_array[idx];
234 
235 	if (!queue->set) {
236 		queue->set = true;
237 		queue->tid = buffer->tid;
238 		queue->cpu = buffer->cpu;
239 	} else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
240 		pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
241 		       queue->cpu, queue->tid, buffer->cpu, buffer->tid);
242 		return -EINVAL;
243 	}
244 
245 	buffer->buffer_nr = queues->next_buffer_nr++;
246 
247 	list_add_tail(&buffer->list, &queue->head);
248 
249 	queues->new_data = true;
250 	queues->populated = true;
251 
252 	return 0;
253 }
254 
255 /* Limit buffers to 32MiB on 32-bit */
256 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
257 
258 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
259 					 unsigned int idx,
260 					 struct auxtrace_buffer *buffer)
261 {
262 	u64 sz = buffer->size;
263 	bool consecutive = false;
264 	struct auxtrace_buffer *b;
265 	int err;
266 
267 	while (sz > BUFFER_LIMIT_FOR_32_BIT) {
268 		b = memdup(buffer, sizeof(struct auxtrace_buffer));
269 		if (!b)
270 			return -ENOMEM;
271 		b->size = BUFFER_LIMIT_FOR_32_BIT;
272 		b->consecutive = consecutive;
273 		err = auxtrace_queues__add_buffer(queues, idx, b);
274 		if (err) {
275 			auxtrace_buffer__free(b);
276 			return err;
277 		}
278 		buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
279 		sz -= BUFFER_LIMIT_FOR_32_BIT;
280 		consecutive = true;
281 	}
282 
283 	buffer->size = sz;
284 	buffer->consecutive = consecutive;
285 
286 	return 0;
287 }
288 
289 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
290 					     struct perf_session *session,
291 					     unsigned int idx,
292 					     struct auxtrace_buffer *buffer)
293 {
294 	if (session->one_mmap) {
295 		buffer->data = buffer->data_offset - session->one_mmap_offset +
296 			       session->one_mmap_addr;
297 	} else if (perf_data_file__is_pipe(session->file)) {
298 		buffer->data = auxtrace_copy_data(buffer->size, session);
299 		if (!buffer->data)
300 			return -ENOMEM;
301 		buffer->data_needs_freeing = true;
302 	} else if (BITS_PER_LONG == 32 &&
303 		   buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
304 		int err;
305 
306 		err = auxtrace_queues__split_buffer(queues, idx, buffer);
307 		if (err)
308 			return err;
309 	}
310 
311 	return auxtrace_queues__add_buffer(queues, idx, buffer);
312 }
313 
314 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
315 			       struct perf_session *session,
316 			       union perf_event *event, off_t data_offset,
317 			       struct auxtrace_buffer **buffer_ptr)
318 {
319 	struct auxtrace_buffer *buffer;
320 	unsigned int idx;
321 	int err;
322 
323 	buffer = zalloc(sizeof(struct auxtrace_buffer));
324 	if (!buffer)
325 		return -ENOMEM;
326 
327 	buffer->pid = -1;
328 	buffer->tid = event->auxtrace.tid;
329 	buffer->cpu = event->auxtrace.cpu;
330 	buffer->data_offset = data_offset;
331 	buffer->offset = event->auxtrace.offset;
332 	buffer->reference = event->auxtrace.reference;
333 	buffer->size = event->auxtrace.size;
334 	idx = event->auxtrace.idx;
335 
336 	err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
337 	if (err)
338 		goto out_err;
339 
340 	if (buffer_ptr)
341 		*buffer_ptr = buffer;
342 
343 	return 0;
344 
345 out_err:
346 	auxtrace_buffer__free(buffer);
347 	return err;
348 }
349 
350 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
351 					      struct perf_session *session,
352 					      off_t file_offset, size_t sz)
353 {
354 	union perf_event *event;
355 	int err;
356 	char buf[PERF_SAMPLE_MAX_SIZE];
357 
358 	err = perf_session__peek_event(session, file_offset, buf,
359 				       PERF_SAMPLE_MAX_SIZE, &event, NULL);
360 	if (err)
361 		return err;
362 
363 	if (event->header.type == PERF_RECORD_AUXTRACE) {
364 		if (event->header.size < sizeof(struct auxtrace_event) ||
365 		    event->header.size != sz) {
366 			err = -EINVAL;
367 			goto out;
368 		}
369 		file_offset += event->header.size;
370 		err = auxtrace_queues__add_event(queues, session, event,
371 						 file_offset, NULL);
372 	}
373 out:
374 	return err;
375 }
376 
377 void auxtrace_queues__free(struct auxtrace_queues *queues)
378 {
379 	unsigned int i;
380 
381 	for (i = 0; i < queues->nr_queues; i++) {
382 		while (!list_empty(&queues->queue_array[i].head)) {
383 			struct auxtrace_buffer *buffer;
384 
385 			buffer = list_entry(queues->queue_array[i].head.next,
386 					    struct auxtrace_buffer, list);
387 			list_del(&buffer->list);
388 			auxtrace_buffer__free(buffer);
389 		}
390 	}
391 
392 	zfree(&queues->queue_array);
393 	queues->nr_queues = 0;
394 }
395 
396 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
397 			     unsigned int pos, unsigned int queue_nr,
398 			     u64 ordinal)
399 {
400 	unsigned int parent;
401 
402 	while (pos) {
403 		parent = (pos - 1) >> 1;
404 		if (heap_array[parent].ordinal <= ordinal)
405 			break;
406 		heap_array[pos] = heap_array[parent];
407 		pos = parent;
408 	}
409 	heap_array[pos].queue_nr = queue_nr;
410 	heap_array[pos].ordinal = ordinal;
411 }
412 
413 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
414 		       u64 ordinal)
415 {
416 	struct auxtrace_heap_item *heap_array;
417 
418 	if (queue_nr >= heap->heap_sz) {
419 		unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
420 
421 		while (heap_sz <= queue_nr)
422 			heap_sz <<= 1;
423 		heap_array = realloc(heap->heap_array,
424 				     heap_sz * sizeof(struct auxtrace_heap_item));
425 		if (!heap_array)
426 			return -ENOMEM;
427 		heap->heap_array = heap_array;
428 		heap->heap_sz = heap_sz;
429 	}
430 
431 	auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
432 
433 	return 0;
434 }
435 
436 void auxtrace_heap__free(struct auxtrace_heap *heap)
437 {
438 	zfree(&heap->heap_array);
439 	heap->heap_cnt = 0;
440 	heap->heap_sz = 0;
441 }
442 
443 void auxtrace_heap__pop(struct auxtrace_heap *heap)
444 {
445 	unsigned int pos, last, heap_cnt = heap->heap_cnt;
446 	struct auxtrace_heap_item *heap_array;
447 
448 	if (!heap_cnt)
449 		return;
450 
451 	heap->heap_cnt -= 1;
452 
453 	heap_array = heap->heap_array;
454 
455 	pos = 0;
456 	while (1) {
457 		unsigned int left, right;
458 
459 		left = (pos << 1) + 1;
460 		if (left >= heap_cnt)
461 			break;
462 		right = left + 1;
463 		if (right >= heap_cnt) {
464 			heap_array[pos] = heap_array[left];
465 			return;
466 		}
467 		if (heap_array[left].ordinal < heap_array[right].ordinal) {
468 			heap_array[pos] = heap_array[left];
469 			pos = left;
470 		} else {
471 			heap_array[pos] = heap_array[right];
472 			pos = right;
473 		}
474 	}
475 
476 	last = heap_cnt - 1;
477 	auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
478 			 heap_array[last].ordinal);
479 }
480 
481 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
482 				       struct perf_evlist *evlist)
483 {
484 	if (itr)
485 		return itr->info_priv_size(itr, evlist);
486 	return 0;
487 }
488 
489 static int auxtrace_not_supported(void)
490 {
491 	pr_err("AUX area tracing is not supported on this architecture\n");
492 	return -EINVAL;
493 }
494 
495 int auxtrace_record__info_fill(struct auxtrace_record *itr,
496 			       struct perf_session *session,
497 			       struct auxtrace_info_event *auxtrace_info,
498 			       size_t priv_size)
499 {
500 	if (itr)
501 		return itr->info_fill(itr, session, auxtrace_info, priv_size);
502 	return auxtrace_not_supported();
503 }
504 
505 void auxtrace_record__free(struct auxtrace_record *itr)
506 {
507 	if (itr)
508 		itr->free(itr);
509 }
510 
511 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
512 {
513 	if (itr && itr->snapshot_start)
514 		return itr->snapshot_start(itr);
515 	return 0;
516 }
517 
518 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
519 {
520 	if (itr && itr->snapshot_finish)
521 		return itr->snapshot_finish(itr);
522 	return 0;
523 }
524 
525 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
526 				   struct auxtrace_mmap *mm,
527 				   unsigned char *data, u64 *head, u64 *old)
528 {
529 	if (itr && itr->find_snapshot)
530 		return itr->find_snapshot(itr, idx, mm, data, head, old);
531 	return 0;
532 }
533 
534 int auxtrace_record__options(struct auxtrace_record *itr,
535 			     struct perf_evlist *evlist,
536 			     struct record_opts *opts)
537 {
538 	if (itr)
539 		return itr->recording_options(itr, evlist, opts);
540 	return 0;
541 }
542 
543 u64 auxtrace_record__reference(struct auxtrace_record *itr)
544 {
545 	if (itr)
546 		return itr->reference(itr);
547 	return 0;
548 }
549 
550 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
551 				    struct record_opts *opts, const char *str)
552 {
553 	if (!str)
554 		return 0;
555 
556 	if (itr)
557 		return itr->parse_snapshot_options(itr, opts, str);
558 
559 	pr_err("No AUX area tracing to snapshot\n");
560 	return -EINVAL;
561 }
562 
563 struct auxtrace_record *__weak
564 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
565 {
566 	*err = 0;
567 	return NULL;
568 }
569 
570 static int auxtrace_index__alloc(struct list_head *head)
571 {
572 	struct auxtrace_index *auxtrace_index;
573 
574 	auxtrace_index = malloc(sizeof(struct auxtrace_index));
575 	if (!auxtrace_index)
576 		return -ENOMEM;
577 
578 	auxtrace_index->nr = 0;
579 	INIT_LIST_HEAD(&auxtrace_index->list);
580 
581 	list_add_tail(&auxtrace_index->list, head);
582 
583 	return 0;
584 }
585 
586 void auxtrace_index__free(struct list_head *head)
587 {
588 	struct auxtrace_index *auxtrace_index, *n;
589 
590 	list_for_each_entry_safe(auxtrace_index, n, head, list) {
591 		list_del(&auxtrace_index->list);
592 		free(auxtrace_index);
593 	}
594 }
595 
596 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
597 {
598 	struct auxtrace_index *auxtrace_index;
599 	int err;
600 
601 	if (list_empty(head)) {
602 		err = auxtrace_index__alloc(head);
603 		if (err)
604 			return NULL;
605 	}
606 
607 	auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
608 
609 	if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
610 		err = auxtrace_index__alloc(head);
611 		if (err)
612 			return NULL;
613 		auxtrace_index = list_entry(head->prev, struct auxtrace_index,
614 					    list);
615 	}
616 
617 	return auxtrace_index;
618 }
619 
620 int auxtrace_index__auxtrace_event(struct list_head *head,
621 				   union perf_event *event, off_t file_offset)
622 {
623 	struct auxtrace_index *auxtrace_index;
624 	size_t nr;
625 
626 	auxtrace_index = auxtrace_index__last(head);
627 	if (!auxtrace_index)
628 		return -ENOMEM;
629 
630 	nr = auxtrace_index->nr;
631 	auxtrace_index->entries[nr].file_offset = file_offset;
632 	auxtrace_index->entries[nr].sz = event->header.size;
633 	auxtrace_index->nr += 1;
634 
635 	return 0;
636 }
637 
638 static int auxtrace_index__do_write(int fd,
639 				    struct auxtrace_index *auxtrace_index)
640 {
641 	struct auxtrace_index_entry ent;
642 	size_t i;
643 
644 	for (i = 0; i < auxtrace_index->nr; i++) {
645 		ent.file_offset = auxtrace_index->entries[i].file_offset;
646 		ent.sz = auxtrace_index->entries[i].sz;
647 		if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
648 			return -errno;
649 	}
650 	return 0;
651 }
652 
653 int auxtrace_index__write(int fd, struct list_head *head)
654 {
655 	struct auxtrace_index *auxtrace_index;
656 	u64 total = 0;
657 	int err;
658 
659 	list_for_each_entry(auxtrace_index, head, list)
660 		total += auxtrace_index->nr;
661 
662 	if (writen(fd, &total, sizeof(total)) != sizeof(total))
663 		return -errno;
664 
665 	list_for_each_entry(auxtrace_index, head, list) {
666 		err = auxtrace_index__do_write(fd, auxtrace_index);
667 		if (err)
668 			return err;
669 	}
670 
671 	return 0;
672 }
673 
674 static int auxtrace_index__process_entry(int fd, struct list_head *head,
675 					 bool needs_swap)
676 {
677 	struct auxtrace_index *auxtrace_index;
678 	struct auxtrace_index_entry ent;
679 	size_t nr;
680 
681 	if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
682 		return -1;
683 
684 	auxtrace_index = auxtrace_index__last(head);
685 	if (!auxtrace_index)
686 		return -1;
687 
688 	nr = auxtrace_index->nr;
689 	if (needs_swap) {
690 		auxtrace_index->entries[nr].file_offset =
691 						bswap_64(ent.file_offset);
692 		auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
693 	} else {
694 		auxtrace_index->entries[nr].file_offset = ent.file_offset;
695 		auxtrace_index->entries[nr].sz = ent.sz;
696 	}
697 
698 	auxtrace_index->nr = nr + 1;
699 
700 	return 0;
701 }
702 
703 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
704 			    bool needs_swap)
705 {
706 	struct list_head *head = &session->auxtrace_index;
707 	u64 nr;
708 
709 	if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
710 		return -1;
711 
712 	if (needs_swap)
713 		nr = bswap_64(nr);
714 
715 	if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
716 		return -1;
717 
718 	while (nr--) {
719 		int err;
720 
721 		err = auxtrace_index__process_entry(fd, head, needs_swap);
722 		if (err)
723 			return -1;
724 	}
725 
726 	return 0;
727 }
728 
729 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
730 						struct perf_session *session,
731 						struct auxtrace_index_entry *ent)
732 {
733 	return auxtrace_queues__add_indexed_event(queues, session,
734 						  ent->file_offset, ent->sz);
735 }
736 
737 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
738 				   struct perf_session *session)
739 {
740 	struct auxtrace_index *auxtrace_index;
741 	struct auxtrace_index_entry *ent;
742 	size_t i;
743 	int err;
744 
745 	list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
746 		for (i = 0; i < auxtrace_index->nr; i++) {
747 			ent = &auxtrace_index->entries[i];
748 			err = auxtrace_queues__process_index_entry(queues,
749 								   session,
750 								   ent);
751 			if (err)
752 				return err;
753 		}
754 	}
755 	return 0;
756 }
757 
758 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
759 					      struct auxtrace_buffer *buffer)
760 {
761 	if (buffer) {
762 		if (list_is_last(&buffer->list, &queue->head))
763 			return NULL;
764 		return list_entry(buffer->list.next, struct auxtrace_buffer,
765 				  list);
766 	} else {
767 		if (list_empty(&queue->head))
768 			return NULL;
769 		return list_entry(queue->head.next, struct auxtrace_buffer,
770 				  list);
771 	}
772 }
773 
774 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
775 {
776 	size_t adj = buffer->data_offset & (page_size - 1);
777 	size_t size = buffer->size + adj;
778 	off_t file_offset = buffer->data_offset - adj;
779 	void *addr;
780 
781 	if (buffer->data)
782 		return buffer->data;
783 
784 	addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
785 	if (addr == MAP_FAILED)
786 		return NULL;
787 
788 	buffer->mmap_addr = addr;
789 	buffer->mmap_size = size;
790 
791 	buffer->data = addr + adj;
792 
793 	return buffer->data;
794 }
795 
796 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
797 {
798 	if (!buffer->data || !buffer->mmap_addr)
799 		return;
800 	munmap(buffer->mmap_addr, buffer->mmap_size);
801 	buffer->mmap_addr = NULL;
802 	buffer->mmap_size = 0;
803 	buffer->data = NULL;
804 	buffer->use_data = NULL;
805 }
806 
807 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
808 {
809 	auxtrace_buffer__put_data(buffer);
810 	if (buffer->data_needs_freeing) {
811 		buffer->data_needs_freeing = false;
812 		zfree(&buffer->data);
813 		buffer->use_data = NULL;
814 		buffer->size = 0;
815 	}
816 }
817 
818 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
819 {
820 	auxtrace_buffer__drop_data(buffer);
821 	free(buffer);
822 }
823 
824 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
825 			  int code, int cpu, pid_t pid, pid_t tid, u64 ip,
826 			  const char *msg)
827 {
828 	size_t size;
829 
830 	memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
831 
832 	auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
833 	auxtrace_error->type = type;
834 	auxtrace_error->code = code;
835 	auxtrace_error->cpu = cpu;
836 	auxtrace_error->pid = pid;
837 	auxtrace_error->tid = tid;
838 	auxtrace_error->ip = ip;
839 	strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
840 
841 	size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
842 	       strlen(auxtrace_error->msg) + 1;
843 	auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
844 }
845 
846 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
847 					 struct perf_tool *tool,
848 					 struct perf_session *session,
849 					 perf_event__handler_t process)
850 {
851 	union perf_event *ev;
852 	size_t priv_size;
853 	int err;
854 
855 	pr_debug2("Synthesizing auxtrace information\n");
856 	priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
857 	ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
858 	if (!ev)
859 		return -ENOMEM;
860 
861 	ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
862 	ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
863 					priv_size;
864 	err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
865 					 priv_size);
866 	if (err)
867 		goto out_free;
868 
869 	err = process(tool, ev, NULL, NULL);
870 out_free:
871 	free(ev);
872 	return err;
873 }
874 
875 static bool auxtrace__dont_decode(struct perf_session *session)
876 {
877 	return !session->itrace_synth_opts ||
878 	       session->itrace_synth_opts->dont_decode;
879 }
880 
881 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
882 				      union perf_event *event,
883 				      struct perf_session *session)
884 {
885 	enum auxtrace_type type = event->auxtrace_info.type;
886 
887 	if (dump_trace)
888 		fprintf(stdout, " type: %u\n", type);
889 
890 	switch (type) {
891 	case PERF_AUXTRACE_INTEL_PT:
892 		return intel_pt_process_auxtrace_info(event, session);
893 	case PERF_AUXTRACE_INTEL_BTS:
894 		return intel_bts_process_auxtrace_info(event, session);
895 	case PERF_AUXTRACE_UNKNOWN:
896 	default:
897 		return -EINVAL;
898 	}
899 }
900 
901 s64 perf_event__process_auxtrace(struct perf_tool *tool,
902 				 union perf_event *event,
903 				 struct perf_session *session)
904 {
905 	s64 err;
906 
907 	if (dump_trace)
908 		fprintf(stdout, " size: %#"PRIx64"  offset: %#"PRIx64"  ref: %#"PRIx64"  idx: %u  tid: %d  cpu: %d\n",
909 			event->auxtrace.size, event->auxtrace.offset,
910 			event->auxtrace.reference, event->auxtrace.idx,
911 			event->auxtrace.tid, event->auxtrace.cpu);
912 
913 	if (auxtrace__dont_decode(session))
914 		return event->auxtrace.size;
915 
916 	if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
917 		return -EINVAL;
918 
919 	err = session->auxtrace->process_auxtrace_event(session, event, tool);
920 	if (err < 0)
921 		return err;
922 
923 	return event->auxtrace.size;
924 }
925 
926 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE		PERF_ITRACE_PERIOD_NANOSECS
927 #define PERF_ITRACE_DEFAULT_PERIOD		100000
928 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ	16
929 #define PERF_ITRACE_MAX_CALLCHAIN_SZ		1024
930 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ	64
931 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ		1024
932 
933 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
934 {
935 	synth_opts->instructions = true;
936 	synth_opts->branches = true;
937 	synth_opts->transactions = true;
938 	synth_opts->errors = true;
939 	synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
940 	synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
941 	synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
942 	synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
943 	synth_opts->initial_skip = 0;
944 }
945 
946 /*
947  * Please check tools/perf/Documentation/perf-script.txt for information
948  * about the options parsed here, which is introduced after this cset,
949  * when support in 'perf script' for these options is introduced.
950  */
951 int itrace_parse_synth_opts(const struct option *opt, const char *str,
952 			    int unset)
953 {
954 	struct itrace_synth_opts *synth_opts = opt->value;
955 	const char *p;
956 	char *endptr;
957 	bool period_type_set = false;
958 	bool period_set = false;
959 
960 	synth_opts->set = true;
961 
962 	if (unset) {
963 		synth_opts->dont_decode = true;
964 		return 0;
965 	}
966 
967 	if (!str) {
968 		itrace_synth_opts__set_default(synth_opts);
969 		return 0;
970 	}
971 
972 	for (p = str; *p;) {
973 		switch (*p++) {
974 		case 'i':
975 			synth_opts->instructions = true;
976 			while (*p == ' ' || *p == ',')
977 				p += 1;
978 			if (isdigit(*p)) {
979 				synth_opts->period = strtoull(p, &endptr, 10);
980 				period_set = true;
981 				p = endptr;
982 				while (*p == ' ' || *p == ',')
983 					p += 1;
984 				switch (*p++) {
985 				case 'i':
986 					synth_opts->period_type =
987 						PERF_ITRACE_PERIOD_INSTRUCTIONS;
988 					period_type_set = true;
989 					break;
990 				case 't':
991 					synth_opts->period_type =
992 						PERF_ITRACE_PERIOD_TICKS;
993 					period_type_set = true;
994 					break;
995 				case 'm':
996 					synth_opts->period *= 1000;
997 					/* Fall through */
998 				case 'u':
999 					synth_opts->period *= 1000;
1000 					/* Fall through */
1001 				case 'n':
1002 					if (*p++ != 's')
1003 						goto out_err;
1004 					synth_opts->period_type =
1005 						PERF_ITRACE_PERIOD_NANOSECS;
1006 					period_type_set = true;
1007 					break;
1008 				case '\0':
1009 					goto out;
1010 				default:
1011 					goto out_err;
1012 				}
1013 			}
1014 			break;
1015 		case 'b':
1016 			synth_opts->branches = true;
1017 			break;
1018 		case 'x':
1019 			synth_opts->transactions = true;
1020 			break;
1021 		case 'e':
1022 			synth_opts->errors = true;
1023 			break;
1024 		case 'd':
1025 			synth_opts->log = true;
1026 			break;
1027 		case 'c':
1028 			synth_opts->branches = true;
1029 			synth_opts->calls = true;
1030 			break;
1031 		case 'r':
1032 			synth_opts->branches = true;
1033 			synth_opts->returns = true;
1034 			break;
1035 		case 'g':
1036 			synth_opts->callchain = true;
1037 			synth_opts->callchain_sz =
1038 					PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1039 			while (*p == ' ' || *p == ',')
1040 				p += 1;
1041 			if (isdigit(*p)) {
1042 				unsigned int val;
1043 
1044 				val = strtoul(p, &endptr, 10);
1045 				p = endptr;
1046 				if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1047 					goto out_err;
1048 				synth_opts->callchain_sz = val;
1049 			}
1050 			break;
1051 		case 'l':
1052 			synth_opts->last_branch = true;
1053 			synth_opts->last_branch_sz =
1054 					PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1055 			while (*p == ' ' || *p == ',')
1056 				p += 1;
1057 			if (isdigit(*p)) {
1058 				unsigned int val;
1059 
1060 				val = strtoul(p, &endptr, 10);
1061 				p = endptr;
1062 				if (!val ||
1063 				    val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1064 					goto out_err;
1065 				synth_opts->last_branch_sz = val;
1066 			}
1067 			break;
1068 		case 's':
1069 			synth_opts->initial_skip = strtoul(p, &endptr, 10);
1070 			if (p == endptr)
1071 				goto out_err;
1072 			p = endptr;
1073 			break;
1074 		case ' ':
1075 		case ',':
1076 			break;
1077 		default:
1078 			goto out_err;
1079 		}
1080 	}
1081 out:
1082 	if (synth_opts->instructions) {
1083 		if (!period_type_set)
1084 			synth_opts->period_type =
1085 					PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1086 		if (!period_set)
1087 			synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1088 	}
1089 
1090 	return 0;
1091 
1092 out_err:
1093 	pr_err("Bad Instruction Tracing options '%s'\n", str);
1094 	return -EINVAL;
1095 }
1096 
1097 static const char * const auxtrace_error_type_name[] = {
1098 	[PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1099 };
1100 
1101 static const char *auxtrace_error_name(int type)
1102 {
1103 	const char *error_type_name = NULL;
1104 
1105 	if (type < PERF_AUXTRACE_ERROR_MAX)
1106 		error_type_name = auxtrace_error_type_name[type];
1107 	if (!error_type_name)
1108 		error_type_name = "unknown AUX";
1109 	return error_type_name;
1110 }
1111 
1112 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1113 {
1114 	struct auxtrace_error_event *e = &event->auxtrace_error;
1115 	int ret;
1116 
1117 	ret = fprintf(fp, " %s error type %u",
1118 		      auxtrace_error_name(e->type), e->type);
1119 	ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1120 		       e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1121 	return ret;
1122 }
1123 
1124 void perf_session__auxtrace_error_inc(struct perf_session *session,
1125 				      union perf_event *event)
1126 {
1127 	struct auxtrace_error_event *e = &event->auxtrace_error;
1128 
1129 	if (e->type < PERF_AUXTRACE_ERROR_MAX)
1130 		session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1131 }
1132 
1133 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1134 {
1135 	int i;
1136 
1137 	for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1138 		if (!stats->nr_auxtrace_errors[i])
1139 			continue;
1140 		ui__warning("%u %s errors\n",
1141 			    stats->nr_auxtrace_errors[i],
1142 			    auxtrace_error_name(i));
1143 	}
1144 }
1145 
1146 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1147 				       union perf_event *event,
1148 				       struct perf_session *session)
1149 {
1150 	if (auxtrace__dont_decode(session))
1151 		return 0;
1152 
1153 	perf_event__fprintf_auxtrace_error(event, stdout);
1154 	return 0;
1155 }
1156 
1157 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1158 				 struct auxtrace_record *itr,
1159 				 struct perf_tool *tool, process_auxtrace_t fn,
1160 				 bool snapshot, size_t snapshot_size)
1161 {
1162 	u64 head, old = mm->prev, offset, ref;
1163 	unsigned char *data = mm->base;
1164 	size_t size, head_off, old_off, len1, len2, padding;
1165 	union perf_event ev;
1166 	void *data1, *data2;
1167 
1168 	if (snapshot) {
1169 		head = auxtrace_mmap__read_snapshot_head(mm);
1170 		if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1171 						   &head, &old))
1172 			return -1;
1173 	} else {
1174 		head = auxtrace_mmap__read_head(mm);
1175 	}
1176 
1177 	if (old == head)
1178 		return 0;
1179 
1180 	pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1181 		  mm->idx, old, head, head - old);
1182 
1183 	if (mm->mask) {
1184 		head_off = head & mm->mask;
1185 		old_off = old & mm->mask;
1186 	} else {
1187 		head_off = head % mm->len;
1188 		old_off = old % mm->len;
1189 	}
1190 
1191 	if (head_off > old_off)
1192 		size = head_off - old_off;
1193 	else
1194 		size = mm->len - (old_off - head_off);
1195 
1196 	if (snapshot && size > snapshot_size)
1197 		size = snapshot_size;
1198 
1199 	ref = auxtrace_record__reference(itr);
1200 
1201 	if (head > old || size <= head || mm->mask) {
1202 		offset = head - size;
1203 	} else {
1204 		/*
1205 		 * When the buffer size is not a power of 2, 'head' wraps at the
1206 		 * highest multiple of the buffer size, so we have to subtract
1207 		 * the remainder here.
1208 		 */
1209 		u64 rem = (0ULL - mm->len) % mm->len;
1210 
1211 		offset = head - size - rem;
1212 	}
1213 
1214 	if (size > head_off) {
1215 		len1 = size - head_off;
1216 		data1 = &data[mm->len - len1];
1217 		len2 = head_off;
1218 		data2 = &data[0];
1219 	} else {
1220 		len1 = size;
1221 		data1 = &data[head_off - len1];
1222 		len2 = 0;
1223 		data2 = NULL;
1224 	}
1225 
1226 	if (itr->alignment) {
1227 		unsigned int unwanted = len1 % itr->alignment;
1228 
1229 		len1 -= unwanted;
1230 		size -= unwanted;
1231 	}
1232 
1233 	/* padding must be written by fn() e.g. record__process_auxtrace() */
1234 	padding = size & 7;
1235 	if (padding)
1236 		padding = 8 - padding;
1237 
1238 	memset(&ev, 0, sizeof(ev));
1239 	ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1240 	ev.auxtrace.header.size = sizeof(ev.auxtrace);
1241 	ev.auxtrace.size = size + padding;
1242 	ev.auxtrace.offset = offset;
1243 	ev.auxtrace.reference = ref;
1244 	ev.auxtrace.idx = mm->idx;
1245 	ev.auxtrace.tid = mm->tid;
1246 	ev.auxtrace.cpu = mm->cpu;
1247 
1248 	if (fn(tool, &ev, data1, len1, data2, len2))
1249 		return -1;
1250 
1251 	mm->prev = head;
1252 
1253 	if (!snapshot) {
1254 		auxtrace_mmap__write_tail(mm, head);
1255 		if (itr->read_finish) {
1256 			int err;
1257 
1258 			err = itr->read_finish(itr, mm->idx);
1259 			if (err < 0)
1260 				return err;
1261 		}
1262 	}
1263 
1264 	return 1;
1265 }
1266 
1267 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1268 			struct perf_tool *tool, process_auxtrace_t fn)
1269 {
1270 	return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1271 }
1272 
1273 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1274 				 struct auxtrace_record *itr,
1275 				 struct perf_tool *tool, process_auxtrace_t fn,
1276 				 size_t snapshot_size)
1277 {
1278 	return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1279 }
1280 
1281 /**
1282  * struct auxtrace_cache - hash table to implement a cache
1283  * @hashtable: the hashtable
1284  * @sz: hashtable size (number of hlists)
1285  * @entry_size: size of an entry
1286  * @limit: limit the number of entries to this maximum, when reached the cache
1287  *         is dropped and caching begins again with an empty cache
1288  * @cnt: current number of entries
1289  * @bits: hashtable size (@sz = 2^@bits)
1290  */
1291 struct auxtrace_cache {
1292 	struct hlist_head *hashtable;
1293 	size_t sz;
1294 	size_t entry_size;
1295 	size_t limit;
1296 	size_t cnt;
1297 	unsigned int bits;
1298 };
1299 
1300 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1301 					   unsigned int limit_percent)
1302 {
1303 	struct auxtrace_cache *c;
1304 	struct hlist_head *ht;
1305 	size_t sz, i;
1306 
1307 	c = zalloc(sizeof(struct auxtrace_cache));
1308 	if (!c)
1309 		return NULL;
1310 
1311 	sz = 1UL << bits;
1312 
1313 	ht = calloc(sz, sizeof(struct hlist_head));
1314 	if (!ht)
1315 		goto out_free;
1316 
1317 	for (i = 0; i < sz; i++)
1318 		INIT_HLIST_HEAD(&ht[i]);
1319 
1320 	c->hashtable = ht;
1321 	c->sz = sz;
1322 	c->entry_size = entry_size;
1323 	c->limit = (c->sz * limit_percent) / 100;
1324 	c->bits = bits;
1325 
1326 	return c;
1327 
1328 out_free:
1329 	free(c);
1330 	return NULL;
1331 }
1332 
1333 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1334 {
1335 	struct auxtrace_cache_entry *entry;
1336 	struct hlist_node *tmp;
1337 	size_t i;
1338 
1339 	if (!c)
1340 		return;
1341 
1342 	for (i = 0; i < c->sz; i++) {
1343 		hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1344 			hlist_del(&entry->hash);
1345 			auxtrace_cache__free_entry(c, entry);
1346 		}
1347 	}
1348 
1349 	c->cnt = 0;
1350 }
1351 
1352 void auxtrace_cache__free(struct auxtrace_cache *c)
1353 {
1354 	if (!c)
1355 		return;
1356 
1357 	auxtrace_cache__drop(c);
1358 	free(c->hashtable);
1359 	free(c);
1360 }
1361 
1362 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1363 {
1364 	return malloc(c->entry_size);
1365 }
1366 
1367 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1368 				void *entry)
1369 {
1370 	free(entry);
1371 }
1372 
1373 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1374 			struct auxtrace_cache_entry *entry)
1375 {
1376 	if (c->limit && ++c->cnt > c->limit)
1377 		auxtrace_cache__drop(c);
1378 
1379 	entry->key = key;
1380 	hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1381 
1382 	return 0;
1383 }
1384 
1385 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1386 {
1387 	struct auxtrace_cache_entry *entry;
1388 	struct hlist_head *hlist;
1389 
1390 	if (!c)
1391 		return NULL;
1392 
1393 	hlist = &c->hashtable[hash_32(key, c->bits)];
1394 	hlist_for_each_entry(entry, hlist, hash) {
1395 		if (entry->key == key)
1396 			return entry;
1397 	}
1398 
1399 	return NULL;
1400 }
1401