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