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