1 // SPDX-License-Identifier: GPL-2.0-only
3 * auxtrace.c: AUX area trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
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>
23 #include <sys/param.h>
26 #include <linux/list.h>
27 #include <linux/zalloc.h>
37 #include "thread_map.h"
41 #include <linux/hash.h>
46 #include <subcmd/parse-options.h>
50 #include "intel-bts.h"
52 #include "s390-cpumsf.h"
54 #include <linux/ctype.h>
55 #include "symbol/kallsyms.h"
57 static bool auxtrace__dont_decode(struct perf_session
*session
)
59 return !session
->itrace_synth_opts
||
60 session
->itrace_synth_opts
->dont_decode
;
63 int auxtrace_mmap__mmap(struct auxtrace_mmap
*mm
,
64 struct auxtrace_mmap_params
*mp
,
67 struct perf_event_mmap_page
*pc
= userpg
;
69 WARN_ONCE(mm
->base
, "Uninitialized auxtrace_mmap\n");
84 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
85 pr_err("Cannot use AUX area tracing mmaps\n");
89 pc
->aux_offset
= mp
->offset
;
90 pc
->aux_size
= mp
->len
;
92 mm
->base
= mmap(NULL
, mp
->len
, mp
->prot
, MAP_SHARED
, fd
, mp
->offset
);
93 if (mm
->base
== MAP_FAILED
) {
94 pr_debug2("failed to mmap AUX area\n");
102 void auxtrace_mmap__munmap(struct auxtrace_mmap
*mm
)
105 munmap(mm
->base
, mm
->len
);
110 void auxtrace_mmap_params__init(struct auxtrace_mmap_params
*mp
,
111 off_t auxtrace_offset
,
112 unsigned int auxtrace_pages
,
113 bool auxtrace_overwrite
)
115 if (auxtrace_pages
) {
116 mp
->offset
= auxtrace_offset
;
117 mp
->len
= auxtrace_pages
* (size_t)page_size
;
118 mp
->mask
= is_power_of_2(mp
->len
) ? mp
->len
- 1 : 0;
119 mp
->prot
= PROT_READ
| (auxtrace_overwrite
? 0 : PROT_WRITE
);
120 pr_debug2("AUX area mmap length %zu\n", mp
->len
);
126 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params
*mp
,
127 struct evlist
*evlist
, int idx
,
133 mp
->cpu
= evlist
->cpus
->map
[idx
];
135 mp
->tid
= thread_map__pid(evlist
->threads
, 0);
140 mp
->tid
= thread_map__pid(evlist
->threads
, idx
);
144 #define AUXTRACE_INIT_NR_QUEUES 32
146 static struct auxtrace_queue
*auxtrace_alloc_queue_array(unsigned int nr_queues
)
148 struct auxtrace_queue
*queue_array
;
149 unsigned int max_nr_queues
, i
;
151 max_nr_queues
= UINT_MAX
/ sizeof(struct auxtrace_queue
);
152 if (nr_queues
> max_nr_queues
)
155 queue_array
= calloc(nr_queues
, sizeof(struct auxtrace_queue
));
159 for (i
= 0; i
< nr_queues
; i
++) {
160 INIT_LIST_HEAD(&queue_array
[i
].head
);
161 queue_array
[i
].priv
= NULL
;
167 int auxtrace_queues__init(struct auxtrace_queues
*queues
)
169 queues
->nr_queues
= AUXTRACE_INIT_NR_QUEUES
;
170 queues
->queue_array
= auxtrace_alloc_queue_array(queues
->nr_queues
);
171 if (!queues
->queue_array
)
176 static int auxtrace_queues__grow(struct auxtrace_queues
*queues
,
177 unsigned int new_nr_queues
)
179 unsigned int nr_queues
= queues
->nr_queues
;
180 struct auxtrace_queue
*queue_array
;
184 nr_queues
= AUXTRACE_INIT_NR_QUEUES
;
186 while (nr_queues
&& nr_queues
< new_nr_queues
)
189 if (nr_queues
< queues
->nr_queues
|| nr_queues
< new_nr_queues
)
192 queue_array
= auxtrace_alloc_queue_array(nr_queues
);
196 for (i
= 0; i
< queues
->nr_queues
; i
++) {
197 list_splice_tail(&queues
->queue_array
[i
].head
,
198 &queue_array
[i
].head
);
199 queue_array
[i
].tid
= queues
->queue_array
[i
].tid
;
200 queue_array
[i
].cpu
= queues
->queue_array
[i
].cpu
;
201 queue_array
[i
].set
= queues
->queue_array
[i
].set
;
202 queue_array
[i
].priv
= queues
->queue_array
[i
].priv
;
205 queues
->nr_queues
= nr_queues
;
206 queues
->queue_array
= queue_array
;
211 static void *auxtrace_copy_data(u64 size
, struct perf_session
*session
)
213 int fd
= perf_data__fd(session
->data
);
217 if (size
> SSIZE_MAX
)
224 ret
= readn(fd
, p
, size
);
225 if (ret
!= (ssize_t
)size
) {
233 static int auxtrace_queues__queue_buffer(struct auxtrace_queues
*queues
,
235 struct auxtrace_buffer
*buffer
)
237 struct auxtrace_queue
*queue
;
240 if (idx
>= queues
->nr_queues
) {
241 err
= auxtrace_queues__grow(queues
, idx
+ 1);
246 queue
= &queues
->queue_array
[idx
];
250 queue
->tid
= buffer
->tid
;
251 queue
->cpu
= buffer
->cpu
;
252 } else if (buffer
->cpu
!= queue
->cpu
|| buffer
->tid
!= queue
->tid
) {
253 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
254 queue
->cpu
, queue
->tid
, buffer
->cpu
, buffer
->tid
);
258 buffer
->buffer_nr
= queues
->next_buffer_nr
++;
260 list_add_tail(&buffer
->list
, &queue
->head
);
262 queues
->new_data
= true;
263 queues
->populated
= true;
268 /* Limit buffers to 32MiB on 32-bit */
269 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
271 static int auxtrace_queues__split_buffer(struct auxtrace_queues
*queues
,
273 struct auxtrace_buffer
*buffer
)
275 u64 sz
= buffer
->size
;
276 bool consecutive
= false;
277 struct auxtrace_buffer
*b
;
280 while (sz
> BUFFER_LIMIT_FOR_32_BIT
) {
281 b
= memdup(buffer
, sizeof(struct auxtrace_buffer
));
284 b
->size
= BUFFER_LIMIT_FOR_32_BIT
;
285 b
->consecutive
= consecutive
;
286 err
= auxtrace_queues__queue_buffer(queues
, idx
, b
);
288 auxtrace_buffer__free(b
);
291 buffer
->data_offset
+= BUFFER_LIMIT_FOR_32_BIT
;
292 sz
-= BUFFER_LIMIT_FOR_32_BIT
;
297 buffer
->consecutive
= consecutive
;
302 static bool filter_cpu(struct perf_session
*session
, int cpu
)
304 unsigned long *cpu_bitmap
= session
->itrace_synth_opts
->cpu_bitmap
;
306 return cpu_bitmap
&& cpu
!= -1 && !test_bit(cpu
, cpu_bitmap
);
309 static int auxtrace_queues__add_buffer(struct auxtrace_queues
*queues
,
310 struct perf_session
*session
,
312 struct auxtrace_buffer
*buffer
,
313 struct auxtrace_buffer
**buffer_ptr
)
317 if (filter_cpu(session
, buffer
->cpu
))
320 buffer
= memdup(buffer
, sizeof(*buffer
));
324 if (session
->one_mmap
) {
325 buffer
->data
= buffer
->data_offset
- session
->one_mmap_offset
+
326 session
->one_mmap_addr
;
327 } else if (perf_data__is_pipe(session
->data
)) {
328 buffer
->data
= auxtrace_copy_data(buffer
->size
, session
);
331 buffer
->data_needs_freeing
= true;
332 } else if (BITS_PER_LONG
== 32 &&
333 buffer
->size
> BUFFER_LIMIT_FOR_32_BIT
) {
334 err
= auxtrace_queues__split_buffer(queues
, idx
, buffer
);
339 err
= auxtrace_queues__queue_buffer(queues
, idx
, buffer
);
343 /* FIXME: Doesn't work for split buffer */
345 *buffer_ptr
= buffer
;
350 auxtrace_buffer__free(buffer
);
354 int auxtrace_queues__add_event(struct auxtrace_queues
*queues
,
355 struct perf_session
*session
,
356 union perf_event
*event
, off_t data_offset
,
357 struct auxtrace_buffer
**buffer_ptr
)
359 struct auxtrace_buffer buffer
= {
361 .tid
= event
->auxtrace
.tid
,
362 .cpu
= event
->auxtrace
.cpu
,
363 .data_offset
= data_offset
,
364 .offset
= event
->auxtrace
.offset
,
365 .reference
= event
->auxtrace
.reference
,
366 .size
= event
->auxtrace
.size
,
368 unsigned int idx
= event
->auxtrace
.idx
;
370 return auxtrace_queues__add_buffer(queues
, session
, idx
, &buffer
,
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
)
378 union perf_event
*event
;
380 char buf
[PERF_SAMPLE_MAX_SIZE
];
382 err
= perf_session__peek_event(session
, file_offset
, buf
,
383 PERF_SAMPLE_MAX_SIZE
, &event
, NULL
);
387 if (event
->header
.type
== PERF_RECORD_AUXTRACE
) {
388 if (event
->header
.size
< sizeof(struct auxtrace_event
) ||
389 event
->header
.size
!= sz
) {
393 file_offset
+= event
->header
.size
;
394 err
= auxtrace_queues__add_event(queues
, session
, event
,
401 void auxtrace_queues__free(struct auxtrace_queues
*queues
)
405 for (i
= 0; i
< queues
->nr_queues
; i
++) {
406 while (!list_empty(&queues
->queue_array
[i
].head
)) {
407 struct auxtrace_buffer
*buffer
;
409 buffer
= list_entry(queues
->queue_array
[i
].head
.next
,
410 struct auxtrace_buffer
, list
);
411 list_del_init(&buffer
->list
);
412 auxtrace_buffer__free(buffer
);
416 zfree(&queues
->queue_array
);
417 queues
->nr_queues
= 0;
420 static void auxtrace_heapify(struct auxtrace_heap_item
*heap_array
,
421 unsigned int pos
, unsigned int queue_nr
,
427 parent
= (pos
- 1) >> 1;
428 if (heap_array
[parent
].ordinal
<= ordinal
)
430 heap_array
[pos
] = heap_array
[parent
];
433 heap_array
[pos
].queue_nr
= queue_nr
;
434 heap_array
[pos
].ordinal
= ordinal
;
437 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
440 struct auxtrace_heap_item
*heap_array
;
442 if (queue_nr
>= heap
->heap_sz
) {
443 unsigned int heap_sz
= AUXTRACE_INIT_NR_QUEUES
;
445 while (heap_sz
<= queue_nr
)
447 heap_array
= realloc(heap
->heap_array
,
448 heap_sz
* sizeof(struct auxtrace_heap_item
));
451 heap
->heap_array
= heap_array
;
452 heap
->heap_sz
= heap_sz
;
455 auxtrace_heapify(heap
->heap_array
, heap
->heap_cnt
++, queue_nr
, ordinal
);
460 void auxtrace_heap__free(struct auxtrace_heap
*heap
)
462 zfree(&heap
->heap_array
);
467 void auxtrace_heap__pop(struct auxtrace_heap
*heap
)
469 unsigned int pos
, last
, heap_cnt
= heap
->heap_cnt
;
470 struct auxtrace_heap_item
*heap_array
;
477 heap_array
= heap
->heap_array
;
481 unsigned int left
, right
;
483 left
= (pos
<< 1) + 1;
484 if (left
>= heap_cnt
)
487 if (right
>= heap_cnt
) {
488 heap_array
[pos
] = heap_array
[left
];
491 if (heap_array
[left
].ordinal
< heap_array
[right
].ordinal
) {
492 heap_array
[pos
] = heap_array
[left
];
495 heap_array
[pos
] = heap_array
[right
];
501 auxtrace_heapify(heap_array
, pos
, heap_array
[last
].queue_nr
,
502 heap_array
[last
].ordinal
);
505 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
,
506 struct evlist
*evlist
)
509 return itr
->info_priv_size(itr
, evlist
);
513 static int auxtrace_not_supported(void)
515 pr_err("AUX area tracing is not supported on this architecture\n");
519 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
520 struct perf_session
*session
,
521 struct auxtrace_info_event
*auxtrace_info
,
525 return itr
->info_fill(itr
, session
, auxtrace_info
, priv_size
);
526 return auxtrace_not_supported();
529 void auxtrace_record__free(struct auxtrace_record
*itr
)
535 int auxtrace_record__snapshot_start(struct auxtrace_record
*itr
)
537 if (itr
&& itr
->snapshot_start
)
538 return itr
->snapshot_start(itr
);
542 int auxtrace_record__snapshot_finish(struct auxtrace_record
*itr
)
544 if (itr
&& itr
->snapshot_finish
)
545 return itr
->snapshot_finish(itr
);
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
)
553 if (itr
&& itr
->find_snapshot
)
554 return itr
->find_snapshot(itr
, idx
, mm
, data
, head
, old
);
558 int auxtrace_record__options(struct auxtrace_record
*itr
,
559 struct evlist
*evlist
,
560 struct record_opts
*opts
)
563 return itr
->recording_options(itr
, evlist
, opts
);
567 u64
auxtrace_record__reference(struct auxtrace_record
*itr
)
570 return itr
->reference(itr
);
574 int auxtrace_parse_snapshot_options(struct auxtrace_record
*itr
,
575 struct record_opts
*opts
, const char *str
)
581 return itr
->parse_snapshot_options(itr
, opts
, str
);
583 pr_err("No AUX area tracing to snapshot\n");
587 struct auxtrace_record
*__weak
588 auxtrace_record__init(struct evlist
*evlist __maybe_unused
, int *err
)
594 static int auxtrace_index__alloc(struct list_head
*head
)
596 struct auxtrace_index
*auxtrace_index
;
598 auxtrace_index
= malloc(sizeof(struct auxtrace_index
));
602 auxtrace_index
->nr
= 0;
603 INIT_LIST_HEAD(&auxtrace_index
->list
);
605 list_add_tail(&auxtrace_index
->list
, head
);
610 void auxtrace_index__free(struct list_head
*head
)
612 struct auxtrace_index
*auxtrace_index
, *n
;
614 list_for_each_entry_safe(auxtrace_index
, n
, head
, list
) {
615 list_del_init(&auxtrace_index
->list
);
616 free(auxtrace_index
);
620 static struct auxtrace_index
*auxtrace_index__last(struct list_head
*head
)
622 struct auxtrace_index
*auxtrace_index
;
625 if (list_empty(head
)) {
626 err
= auxtrace_index__alloc(head
);
631 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
, list
);
633 if (auxtrace_index
->nr
>= PERF_AUXTRACE_INDEX_ENTRY_COUNT
) {
634 err
= auxtrace_index__alloc(head
);
637 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
,
641 return auxtrace_index
;
644 int auxtrace_index__auxtrace_event(struct list_head
*head
,
645 union perf_event
*event
, off_t file_offset
)
647 struct auxtrace_index
*auxtrace_index
;
650 auxtrace_index
= auxtrace_index__last(head
);
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;
662 static int auxtrace_index__do_write(int fd
,
663 struct auxtrace_index
*auxtrace_index
)
665 struct auxtrace_index_entry ent
;
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
))
677 int auxtrace_index__write(int fd
, struct list_head
*head
)
679 struct auxtrace_index
*auxtrace_index
;
683 list_for_each_entry(auxtrace_index
, head
, list
)
684 total
+= auxtrace_index
->nr
;
686 if (writen(fd
, &total
, sizeof(total
)) != sizeof(total
))
689 list_for_each_entry(auxtrace_index
, head
, list
) {
690 err
= auxtrace_index__do_write(fd
, auxtrace_index
);
698 static int auxtrace_index__process_entry(int fd
, struct list_head
*head
,
701 struct auxtrace_index
*auxtrace_index
;
702 struct auxtrace_index_entry ent
;
705 if (readn(fd
, &ent
, sizeof(ent
)) != sizeof(ent
))
708 auxtrace_index
= auxtrace_index__last(head
);
712 nr
= auxtrace_index
->nr
;
714 auxtrace_index
->entries
[nr
].file_offset
=
715 bswap_64(ent
.file_offset
);
716 auxtrace_index
->entries
[nr
].sz
= bswap_64(ent
.sz
);
718 auxtrace_index
->entries
[nr
].file_offset
= ent
.file_offset
;
719 auxtrace_index
->entries
[nr
].sz
= ent
.sz
;
722 auxtrace_index
->nr
= nr
+ 1;
727 int auxtrace_index__process(int fd
, u64 size
, struct perf_session
*session
,
730 struct list_head
*head
= &session
->auxtrace_index
;
733 if (readn(fd
, &nr
, sizeof(u64
)) != sizeof(u64
))
739 if (sizeof(u64
) + nr
* sizeof(struct auxtrace_index_entry
) > size
)
745 err
= auxtrace_index__process_entry(fd
, head
, needs_swap
);
753 static int auxtrace_queues__process_index_entry(struct auxtrace_queues
*queues
,
754 struct perf_session
*session
,
755 struct auxtrace_index_entry
*ent
)
757 return auxtrace_queues__add_indexed_event(queues
, session
,
758 ent
->file_offset
, ent
->sz
);
761 int auxtrace_queues__process_index(struct auxtrace_queues
*queues
,
762 struct perf_session
*session
)
764 struct auxtrace_index
*auxtrace_index
;
765 struct auxtrace_index_entry
*ent
;
769 if (auxtrace__dont_decode(session
))
772 list_for_each_entry(auxtrace_index
, &session
->auxtrace_index
, list
) {
773 for (i
= 0; i
< auxtrace_index
->nr
; i
++) {
774 ent
= &auxtrace_index
->entries
[i
];
775 err
= auxtrace_queues__process_index_entry(queues
,
785 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
786 struct auxtrace_buffer
*buffer
)
789 if (list_is_last(&buffer
->list
, &queue
->head
))
791 return list_entry(buffer
->list
.next
, struct auxtrace_buffer
,
794 if (list_empty(&queue
->head
))
796 return list_entry(queue
->head
.next
, struct auxtrace_buffer
,
801 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
)
803 size_t adj
= buffer
->data_offset
& (page_size
- 1);
804 size_t size
= buffer
->size
+ adj
;
805 off_t file_offset
= buffer
->data_offset
- adj
;
811 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, file_offset
);
812 if (addr
== MAP_FAILED
)
815 buffer
->mmap_addr
= addr
;
816 buffer
->mmap_size
= size
;
818 buffer
->data
= addr
+ adj
;
823 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
)
825 if (!buffer
->data
|| !buffer
->mmap_addr
)
827 munmap(buffer
->mmap_addr
, buffer
->mmap_size
);
828 buffer
->mmap_addr
= NULL
;
829 buffer
->mmap_size
= 0;
831 buffer
->use_data
= NULL
;
834 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
)
836 auxtrace_buffer__put_data(buffer
);
837 if (buffer
->data_needs_freeing
) {
838 buffer
->data_needs_freeing
= false;
839 zfree(&buffer
->data
);
840 buffer
->use_data
= NULL
;
845 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
)
847 auxtrace_buffer__drop_data(buffer
);
851 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
852 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
853 const char *msg
, u64 timestamp
)
857 memset(auxtrace_error
, 0, sizeof(struct auxtrace_error_event
));
859 auxtrace_error
->header
.type
= PERF_RECORD_AUXTRACE_ERROR
;
860 auxtrace_error
->type
= type
;
861 auxtrace_error
->code
= code
;
862 auxtrace_error
->cpu
= cpu
;
863 auxtrace_error
->pid
= pid
;
864 auxtrace_error
->tid
= tid
;
865 auxtrace_error
->fmt
= 1;
866 auxtrace_error
->ip
= ip
;
867 auxtrace_error
->time
= timestamp
;
868 strlcpy(auxtrace_error
->msg
, msg
, MAX_AUXTRACE_ERROR_MSG
);
870 size
= (void *)auxtrace_error
->msg
- (void *)auxtrace_error
+
871 strlen(auxtrace_error
->msg
) + 1;
872 auxtrace_error
->header
.size
= PERF_ALIGN(size
, sizeof(u64
));
875 int perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr
,
876 struct perf_tool
*tool
,
877 struct perf_session
*session
,
878 perf_event__handler_t process
)
880 union perf_event
*ev
;
884 pr_debug2("Synthesizing auxtrace information\n");
885 priv_size
= auxtrace_record__info_priv_size(itr
, session
->evlist
);
886 ev
= zalloc(sizeof(struct auxtrace_info_event
) + priv_size
);
890 ev
->auxtrace_info
.header
.type
= PERF_RECORD_AUXTRACE_INFO
;
891 ev
->auxtrace_info
.header
.size
= sizeof(struct auxtrace_info_event
) +
893 err
= auxtrace_record__info_fill(itr
, session
, &ev
->auxtrace_info
,
898 err
= process(tool
, ev
, NULL
, NULL
);
904 int perf_event__process_auxtrace_info(struct perf_session
*session
,
905 union perf_event
*event
)
907 enum auxtrace_type type
= event
->auxtrace_info
.type
;
910 fprintf(stdout
, " type: %u\n", type
);
913 case PERF_AUXTRACE_INTEL_PT
:
914 return intel_pt_process_auxtrace_info(event
, session
);
915 case PERF_AUXTRACE_INTEL_BTS
:
916 return intel_bts_process_auxtrace_info(event
, session
);
917 case PERF_AUXTRACE_ARM_SPE
:
918 return arm_spe_process_auxtrace_info(event
, session
);
919 case PERF_AUXTRACE_CS_ETM
:
920 return cs_etm__process_auxtrace_info(event
, session
);
921 case PERF_AUXTRACE_S390_CPUMSF
:
922 return s390_cpumsf_process_auxtrace_info(event
, session
);
923 case PERF_AUXTRACE_UNKNOWN
:
929 s64
perf_event__process_auxtrace(struct perf_session
*session
,
930 union perf_event
*event
)
935 fprintf(stdout
, " size: %#"PRIx64
" offset: %#"PRIx64
" ref: %#"PRIx64
" idx: %u tid: %d cpu: %d\n",
936 event
->auxtrace
.size
, event
->auxtrace
.offset
,
937 event
->auxtrace
.reference
, event
->auxtrace
.idx
,
938 event
->auxtrace
.tid
, event
->auxtrace
.cpu
);
940 if (auxtrace__dont_decode(session
))
941 return event
->auxtrace
.size
;
943 if (!session
->auxtrace
|| event
->header
.type
!= PERF_RECORD_AUXTRACE
)
946 err
= session
->auxtrace
->process_auxtrace_event(session
, event
, session
->tool
);
950 return event
->auxtrace
.size
;
953 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
954 #define PERF_ITRACE_DEFAULT_PERIOD 100000
955 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
956 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
957 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
958 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
960 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
,
963 synth_opts
->branches
= true;
964 synth_opts
->transactions
= true;
965 synth_opts
->ptwrites
= true;
966 synth_opts
->pwr_events
= true;
967 synth_opts
->errors
= true;
969 synth_opts
->period_type
= PERF_ITRACE_PERIOD_INSTRUCTIONS
;
970 synth_opts
->period
= 1;
971 synth_opts
->calls
= true;
973 synth_opts
->instructions
= true;
974 synth_opts
->period_type
= PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
975 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
977 synth_opts
->callchain_sz
= PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
978 synth_opts
->last_branch_sz
= PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ
;
979 synth_opts
->initial_skip
= 0;
983 * Please check tools/perf/Documentation/perf-script.txt for information
984 * about the options parsed here, which is introduced after this cset,
985 * when support in 'perf script' for these options is introduced.
987 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
990 struct itrace_synth_opts
*synth_opts
= opt
->value
;
993 bool period_type_set
= false;
994 bool period_set
= false;
996 synth_opts
->set
= true;
999 synth_opts
->dont_decode
= true;
1004 itrace_synth_opts__set_default(synth_opts
,
1005 synth_opts
->default_no_sample
);
1009 for (p
= str
; *p
;) {
1012 synth_opts
->instructions
= true;
1013 while (*p
== ' ' || *p
== ',')
1016 synth_opts
->period
= strtoull(p
, &endptr
, 10);
1019 while (*p
== ' ' || *p
== ',')
1023 synth_opts
->period_type
=
1024 PERF_ITRACE_PERIOD_INSTRUCTIONS
;
1025 period_type_set
= true;
1028 synth_opts
->period_type
=
1029 PERF_ITRACE_PERIOD_TICKS
;
1030 period_type_set
= true;
1033 synth_opts
->period
*= 1000;
1036 synth_opts
->period
*= 1000;
1041 synth_opts
->period_type
=
1042 PERF_ITRACE_PERIOD_NANOSECS
;
1043 period_type_set
= true;
1053 synth_opts
->branches
= true;
1056 synth_opts
->transactions
= true;
1059 synth_opts
->ptwrites
= true;
1062 synth_opts
->pwr_events
= true;
1065 synth_opts
->errors
= true;
1068 synth_opts
->log
= true;
1071 synth_opts
->branches
= true;
1072 synth_opts
->calls
= true;
1075 synth_opts
->branches
= true;
1076 synth_opts
->returns
= true;
1079 synth_opts
->callchain
= true;
1080 synth_opts
->callchain_sz
=
1081 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
1082 while (*p
== ' ' || *p
== ',')
1087 val
= strtoul(p
, &endptr
, 10);
1089 if (!val
|| val
> PERF_ITRACE_MAX_CALLCHAIN_SZ
)
1091 synth_opts
->callchain_sz
= val
;
1095 synth_opts
->last_branch
= true;
1096 synth_opts
->last_branch_sz
=
1097 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ
;
1098 while (*p
== ' ' || *p
== ',')
1103 val
= strtoul(p
, &endptr
, 10);
1106 val
> PERF_ITRACE_MAX_LAST_BRANCH_SZ
)
1108 synth_opts
->last_branch_sz
= val
;
1112 synth_opts
->initial_skip
= strtoul(p
, &endptr
, 10);
1125 if (synth_opts
->instructions
) {
1126 if (!period_type_set
)
1127 synth_opts
->period_type
=
1128 PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
1130 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
1136 pr_err("Bad Instruction Tracing options '%s'\n", str
);
1140 static const char * const auxtrace_error_type_name
[] = {
1141 [PERF_AUXTRACE_ERROR_ITRACE
] = "instruction trace",
1144 static const char *auxtrace_error_name(int type
)
1146 const char *error_type_name
= NULL
;
1148 if (type
< PERF_AUXTRACE_ERROR_MAX
)
1149 error_type_name
= auxtrace_error_type_name
[type
];
1150 if (!error_type_name
)
1151 error_type_name
= "unknown AUX";
1152 return error_type_name
;
1155 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
)
1157 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1158 unsigned long long nsecs
= e
->time
;
1159 const char *msg
= e
->msg
;
1162 ret
= fprintf(fp
, " %s error type %u",
1163 auxtrace_error_name(e
->type
), e
->type
);
1165 if (e
->fmt
&& nsecs
) {
1166 unsigned long secs
= nsecs
/ NSEC_PER_SEC
;
1168 nsecs
-= secs
* NSEC_PER_SEC
;
1169 ret
+= fprintf(fp
, " time %lu.%09llu", secs
, nsecs
);
1171 ret
+= fprintf(fp
, " time 0");
1175 msg
= (const char *)&e
->time
;
1177 ret
+= fprintf(fp
, " cpu %d pid %d tid %d ip %#"PRIx64
" code %u: %s\n",
1178 e
->cpu
, e
->pid
, e
->tid
, e
->ip
, e
->code
, msg
);
1182 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
1183 union perf_event
*event
)
1185 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1187 if (e
->type
< PERF_AUXTRACE_ERROR_MAX
)
1188 session
->evlist
->stats
.nr_auxtrace_errors
[e
->type
] += 1;
1191 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
)
1195 for (i
= 0; i
< PERF_AUXTRACE_ERROR_MAX
; i
++) {
1196 if (!stats
->nr_auxtrace_errors
[i
])
1198 ui__warning("%u %s errors\n",
1199 stats
->nr_auxtrace_errors
[i
],
1200 auxtrace_error_name(i
));
1204 int perf_event__process_auxtrace_error(struct perf_session
*session
,
1205 union perf_event
*event
)
1207 if (auxtrace__dont_decode(session
))
1210 perf_event__fprintf_auxtrace_error(event
, stdout
);
1214 static int __auxtrace_mmap__read(struct perf_mmap
*map
,
1215 struct auxtrace_record
*itr
,
1216 struct perf_tool
*tool
, process_auxtrace_t fn
,
1217 bool snapshot
, size_t snapshot_size
)
1219 struct auxtrace_mmap
*mm
= &map
->auxtrace_mmap
;
1220 u64 head
, old
= mm
->prev
, offset
, ref
;
1221 unsigned char *data
= mm
->base
;
1222 size_t size
, head_off
, old_off
, len1
, len2
, padding
;
1223 union perf_event ev
;
1224 void *data1
, *data2
;
1227 head
= auxtrace_mmap__read_snapshot_head(mm
);
1228 if (auxtrace_record__find_snapshot(itr
, mm
->idx
, mm
, data
,
1232 head
= auxtrace_mmap__read_head(mm
);
1238 pr_debug3("auxtrace idx %d old %#"PRIx64
" head %#"PRIx64
" diff %#"PRIx64
"\n",
1239 mm
->idx
, old
, head
, head
- old
);
1242 head_off
= head
& mm
->mask
;
1243 old_off
= old
& mm
->mask
;
1245 head_off
= head
% mm
->len
;
1246 old_off
= old
% mm
->len
;
1249 if (head_off
> old_off
)
1250 size
= head_off
- old_off
;
1252 size
= mm
->len
- (old_off
- head_off
);
1254 if (snapshot
&& size
> snapshot_size
)
1255 size
= snapshot_size
;
1257 ref
= auxtrace_record__reference(itr
);
1259 if (head
> old
|| size
<= head
|| mm
->mask
) {
1260 offset
= head
- size
;
1263 * When the buffer size is not a power of 2, 'head' wraps at the
1264 * highest multiple of the buffer size, so we have to subtract
1265 * the remainder here.
1267 u64 rem
= (0ULL - mm
->len
) % mm
->len
;
1269 offset
= head
- size
- rem
;
1272 if (size
> head_off
) {
1273 len1
= size
- head_off
;
1274 data1
= &data
[mm
->len
- len1
];
1279 data1
= &data
[head_off
- len1
];
1284 if (itr
->alignment
) {
1285 unsigned int unwanted
= len1
% itr
->alignment
;
1291 /* padding must be written by fn() e.g. record__process_auxtrace() */
1292 padding
= size
& (PERF_AUXTRACE_RECORD_ALIGNMENT
- 1);
1294 padding
= PERF_AUXTRACE_RECORD_ALIGNMENT
- padding
;
1296 memset(&ev
, 0, sizeof(ev
));
1297 ev
.auxtrace
.header
.type
= PERF_RECORD_AUXTRACE
;
1298 ev
.auxtrace
.header
.size
= sizeof(ev
.auxtrace
);
1299 ev
.auxtrace
.size
= size
+ padding
;
1300 ev
.auxtrace
.offset
= offset
;
1301 ev
.auxtrace
.reference
= ref
;
1302 ev
.auxtrace
.idx
= mm
->idx
;
1303 ev
.auxtrace
.tid
= mm
->tid
;
1304 ev
.auxtrace
.cpu
= mm
->cpu
;
1306 if (fn(tool
, map
, &ev
, data1
, len1
, data2
, len2
))
1312 auxtrace_mmap__write_tail(mm
, head
);
1313 if (itr
->read_finish
) {
1316 err
= itr
->read_finish(itr
, mm
->idx
);
1325 int auxtrace_mmap__read(struct perf_mmap
*map
, struct auxtrace_record
*itr
,
1326 struct perf_tool
*tool
, process_auxtrace_t fn
)
1328 return __auxtrace_mmap__read(map
, itr
, tool
, fn
, false, 0);
1331 int auxtrace_mmap__read_snapshot(struct perf_mmap
*map
,
1332 struct auxtrace_record
*itr
,
1333 struct perf_tool
*tool
, process_auxtrace_t fn
,
1334 size_t snapshot_size
)
1336 return __auxtrace_mmap__read(map
, itr
, tool
, fn
, true, snapshot_size
);
1340 * struct auxtrace_cache - hash table to implement a cache
1341 * @hashtable: the hashtable
1342 * @sz: hashtable size (number of hlists)
1343 * @entry_size: size of an entry
1344 * @limit: limit the number of entries to this maximum, when reached the cache
1345 * is dropped and caching begins again with an empty cache
1346 * @cnt: current number of entries
1347 * @bits: hashtable size (@sz = 2^@bits)
1349 struct auxtrace_cache
{
1350 struct hlist_head
*hashtable
;
1358 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
1359 unsigned int limit_percent
)
1361 struct auxtrace_cache
*c
;
1362 struct hlist_head
*ht
;
1365 c
= zalloc(sizeof(struct auxtrace_cache
));
1371 ht
= calloc(sz
, sizeof(struct hlist_head
));
1375 for (i
= 0; i
< sz
; i
++)
1376 INIT_HLIST_HEAD(&ht
[i
]);
1380 c
->entry_size
= entry_size
;
1381 c
->limit
= (c
->sz
* limit_percent
) / 100;
1391 static void auxtrace_cache__drop(struct auxtrace_cache
*c
)
1393 struct auxtrace_cache_entry
*entry
;
1394 struct hlist_node
*tmp
;
1400 for (i
= 0; i
< c
->sz
; i
++) {
1401 hlist_for_each_entry_safe(entry
, tmp
, &c
->hashtable
[i
], hash
) {
1402 hlist_del(&entry
->hash
);
1403 auxtrace_cache__free_entry(c
, entry
);
1410 void auxtrace_cache__free(struct auxtrace_cache
*c
)
1415 auxtrace_cache__drop(c
);
1416 zfree(&c
->hashtable
);
1420 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
)
1422 return malloc(c
->entry_size
);
1425 void auxtrace_cache__free_entry(struct auxtrace_cache
*c __maybe_unused
,
1431 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
1432 struct auxtrace_cache_entry
*entry
)
1434 if (c
->limit
&& ++c
->cnt
> c
->limit
)
1435 auxtrace_cache__drop(c
);
1438 hlist_add_head(&entry
->hash
, &c
->hashtable
[hash_32(key
, c
->bits
)]);
1443 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
)
1445 struct auxtrace_cache_entry
*entry
;
1446 struct hlist_head
*hlist
;
1451 hlist
= &c
->hashtable
[hash_32(key
, c
->bits
)];
1452 hlist_for_each_entry(entry
, hlist
, hash
) {
1453 if (entry
->key
== key
)
1460 static void addr_filter__free_str(struct addr_filter
*filt
)
1463 filt
->action
= NULL
;
1464 filt
->sym_from
= NULL
;
1465 filt
->sym_to
= NULL
;
1466 filt
->filename
= NULL
;
1469 static struct addr_filter
*addr_filter__new(void)
1471 struct addr_filter
*filt
= zalloc(sizeof(*filt
));
1474 INIT_LIST_HEAD(&filt
->list
);
1479 static void addr_filter__free(struct addr_filter
*filt
)
1482 addr_filter__free_str(filt
);
1486 static void addr_filters__add(struct addr_filters
*filts
,
1487 struct addr_filter
*filt
)
1489 list_add_tail(&filt
->list
, &filts
->head
);
1493 static void addr_filters__del(struct addr_filters
*filts
,
1494 struct addr_filter
*filt
)
1496 list_del_init(&filt
->list
);
1500 void addr_filters__init(struct addr_filters
*filts
)
1502 INIT_LIST_HEAD(&filts
->head
);
1506 void addr_filters__exit(struct addr_filters
*filts
)
1508 struct addr_filter
*filt
, *n
;
1510 list_for_each_entry_safe(filt
, n
, &filts
->head
, list
) {
1511 addr_filters__del(filts
, filt
);
1512 addr_filter__free(filt
);
1516 static int parse_num_or_str(char **inp
, u64
*num
, const char **str
,
1517 const char *str_delim
)
1519 *inp
+= strspn(*inp
, " ");
1521 if (isdigit(**inp
)) {
1527 *num
= strtoull(*inp
, &endptr
, 0);
1538 *inp
+= strspn(*inp
, " ");
1540 n
= strcspn(*inp
, str_delim
);
1552 static int parse_action(struct addr_filter
*filt
)
1554 if (!strcmp(filt
->action
, "filter")) {
1557 } else if (!strcmp(filt
->action
, "start")) {
1559 } else if (!strcmp(filt
->action
, "stop")) {
1560 filt
->start
= false;
1561 } else if (!strcmp(filt
->action
, "tracestop")) {
1562 filt
->start
= false;
1564 filt
->action
+= 5; /* Change 'tracestop' to 'stop' */
1571 static int parse_sym_idx(char **inp
, int *idx
)
1575 *inp
+= strspn(*inp
, " ");
1582 if (**inp
== 'g' || **inp
== 'G') {
1590 num
= strtoul(*inp
, &endptr
, 0);
1593 if (endptr
== *inp
|| num
> INT_MAX
)
1602 static int parse_addr_size(char **inp
, u64
*num
, const char **str
, int *idx
)
1604 int err
= parse_num_or_str(inp
, num
, str
, " ");
1607 err
= parse_sym_idx(inp
, idx
);
1612 static int parse_one_filter(struct addr_filter
*filt
, const char **filter_inp
)
1617 filt
->str
= fstr
= strdup(*filter_inp
);
1621 err
= parse_num_or_str(&fstr
, NULL
, &filt
->action
, " ");
1625 err
= parse_action(filt
);
1629 err
= parse_addr_size(&fstr
, &filt
->addr
, &filt
->sym_from
,
1630 &filt
->sym_from_idx
);
1634 fstr
+= strspn(fstr
, " ");
1638 err
= parse_addr_size(&fstr
, &filt
->size
, &filt
->sym_to
,
1645 fstr
+= strspn(fstr
, " ");
1649 err
= parse_num_or_str(&fstr
, NULL
, &filt
->filename
, " ,");
1654 fstr
+= strspn(fstr
, " ,");
1656 *filter_inp
+= fstr
- filt
->str
;
1661 addr_filter__free_str(filt
);
1666 int addr_filters__parse_bare_filter(struct addr_filters
*filts
,
1669 struct addr_filter
*filt
;
1670 const char *fstr
= filter
;
1674 filt
= addr_filter__new();
1675 err
= parse_one_filter(filt
, &fstr
);
1677 addr_filter__free(filt
);
1678 addr_filters__exit(filts
);
1681 addr_filters__add(filts
, filt
);
1700 static bool kern_sym_match(struct sym_args
*args
, const char *name
, char type
)
1702 /* A function with the same name, and global or the n'th found or any */
1703 return kallsyms__is_function(type
) &&
1704 !strcmp(name
, args
->name
) &&
1705 ((args
->global
&& isupper(type
)) ||
1706 (args
->selected
&& ++(args
->cnt
) == args
->idx
) ||
1707 (!args
->global
&& !args
->selected
));
1710 static int find_kern_sym_cb(void *arg
, const char *name
, char type
, u64 start
)
1712 struct sym_args
*args
= arg
;
1714 if (args
->started
) {
1716 args
->size
= start
- args
->start
;
1717 if (args
->selected
) {
1720 } else if (kern_sym_match(args
, name
, type
)) {
1721 args
->duplicate
= true;
1724 } else if (kern_sym_match(args
, name
, type
)) {
1725 args
->started
= true;
1726 args
->start
= start
;
1732 static int print_kern_sym_cb(void *arg
, const char *name
, char type
, u64 start
)
1734 struct sym_args
*args
= arg
;
1736 if (kern_sym_match(args
, name
, type
)) {
1737 pr_err("#%d\t0x%"PRIx64
"\t%c\t%s\n",
1738 ++args
->cnt
, start
, type
, name
);
1740 } else if (args
->near
) {
1742 pr_err("\t\twhich is near\t\t%s\n", name
);
1748 static int sym_not_found_error(const char *sym_name
, int idx
)
1751 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1754 pr_err("Global symbol '%s' not found.\n", sym_name
);
1756 pr_err("Symbol '%s' not found.\n", sym_name
);
1758 pr_err("Note that symbols must be functions.\n");
1763 static int find_kern_sym(const char *sym_name
, u64
*start
, u64
*size
, int idx
)
1765 struct sym_args args
= {
1769 .selected
= idx
> 0,
1776 err
= kallsyms__parse("/proc/kallsyms", &args
, find_kern_sym_cb
);
1778 pr_err("Failed to parse /proc/kallsyms\n");
1782 if (args
.duplicate
) {
1783 pr_err("Multiple kernel symbols with name '%s'\n", sym_name
);
1785 kallsyms__parse("/proc/kallsyms", &args
, print_kern_sym_cb
);
1786 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1788 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1792 if (!args
.started
) {
1793 pr_err("Kernel symbol lookup: ");
1794 return sym_not_found_error(sym_name
, idx
);
1797 *start
= args
.start
;
1803 static int find_entire_kern_cb(void *arg
, const char *name __maybe_unused
,
1804 char type
, u64 start
)
1806 struct sym_args
*args
= arg
;
1808 if (!kallsyms__is_function(type
))
1811 if (!args
->started
) {
1812 args
->started
= true;
1813 args
->start
= start
;
1815 /* Don't know exactly where the kernel ends, so we add a page */
1816 args
->size
= round_up(start
, page_size
) + page_size
- args
->start
;
1821 static int addr_filter__entire_kernel(struct addr_filter
*filt
)
1823 struct sym_args args
= { .started
= false };
1826 err
= kallsyms__parse("/proc/kallsyms", &args
, find_entire_kern_cb
);
1827 if (err
< 0 || !args
.started
) {
1828 pr_err("Failed to parse /proc/kallsyms\n");
1832 filt
->addr
= args
.start
;
1833 filt
->size
= args
.size
;
1838 static int check_end_after_start(struct addr_filter
*filt
, u64 start
, u64 size
)
1840 if (start
+ size
>= filt
->addr
)
1843 if (filt
->sym_from
) {
1844 pr_err("Symbol '%s' (0x%"PRIx64
") comes before '%s' (0x%"PRIx64
")\n",
1845 filt
->sym_to
, start
, filt
->sym_from
, filt
->addr
);
1847 pr_err("Symbol '%s' (0x%"PRIx64
") comes before address 0x%"PRIx64
")\n",
1848 filt
->sym_to
, start
, filt
->addr
);
1854 static int addr_filter__resolve_kernel_syms(struct addr_filter
*filt
)
1856 bool no_size
= false;
1860 if (symbol_conf
.kptr_restrict
) {
1861 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1865 if (filt
->sym_from
&& !strcmp(filt
->sym_from
, "*"))
1866 return addr_filter__entire_kernel(filt
);
1868 if (filt
->sym_from
) {
1869 err
= find_kern_sym(filt
->sym_from
, &start
, &size
,
1870 filt
->sym_from_idx
);
1874 if (filt
->range
&& !filt
->size
&& !filt
->sym_to
) {
1881 err
= find_kern_sym(filt
->sym_to
, &start
, &size
,
1886 err
= check_end_after_start(filt
, start
, size
);
1889 filt
->size
= start
+ size
- filt
->addr
;
1893 /* The very last symbol in kallsyms does not imply a particular size */
1895 pr_err("Cannot determine size of symbol '%s'\n",
1896 filt
->sym_to
? filt
->sym_to
: filt
->sym_from
);
1903 static struct dso
*load_dso(const char *name
)
1908 map
= dso__new_map(name
);
1912 if (map__load(map
) < 0)
1913 pr_err("File '%s' not found or has no symbols.\n", name
);
1915 dso
= dso__get(map
->dso
);
1922 static bool dso_sym_match(struct symbol
*sym
, const char *name
, int *cnt
,
1925 /* Same name, and global or the n'th found or any */
1926 return !arch__compare_symbol_names(name
, sym
->name
) &&
1927 ((!idx
&& sym
->binding
== STB_GLOBAL
) ||
1928 (idx
> 0 && ++*cnt
== idx
) ||
1932 static void print_duplicate_syms(struct dso
*dso
, const char *sym_name
)
1938 pr_err("Multiple symbols with name '%s'\n", sym_name
);
1940 sym
= dso__first_symbol(dso
);
1942 if (dso_sym_match(sym
, sym_name
, &cnt
, -1)) {
1943 pr_err("#%d\t0x%"PRIx64
"\t%c\t%s\n",
1945 sym
->binding
== STB_GLOBAL
? 'g' :
1946 sym
->binding
== STB_LOCAL
? 'l' : 'w',
1951 pr_err("\t\twhich is near\t\t%s\n", sym
->name
);
1953 sym
= dso__next_symbol(sym
);
1956 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1958 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1961 static int find_dso_sym(struct dso
*dso
, const char *sym_name
, u64
*start
,
1970 sym
= dso__first_symbol(dso
);
1974 *size
= sym
->start
- *start
;
1978 } else if (dso_sym_match(sym
, sym_name
, &cnt
, idx
)) {
1979 print_duplicate_syms(dso
, sym_name
);
1982 } else if (dso_sym_match(sym
, sym_name
, &cnt
, idx
)) {
1983 *start
= sym
->start
;
1984 *size
= sym
->end
- sym
->start
;
1986 sym
= dso__next_symbol(sym
);
1990 return sym_not_found_error(sym_name
, idx
);
1995 static int addr_filter__entire_dso(struct addr_filter
*filt
, struct dso
*dso
)
1997 if (dso__data_file_size(dso
, NULL
)) {
1998 pr_err("Failed to determine filter for %s\nCannot determine file size.\n",
2004 filt
->size
= dso
->data
.file_size
;
2009 static int addr_filter__resolve_syms(struct addr_filter
*filt
)
2015 if (!filt
->sym_from
&& !filt
->sym_to
)
2018 if (!filt
->filename
)
2019 return addr_filter__resolve_kernel_syms(filt
);
2021 dso
= load_dso(filt
->filename
);
2023 pr_err("Failed to load symbols from: %s\n", filt
->filename
);
2027 if (filt
->sym_from
&& !strcmp(filt
->sym_from
, "*")) {
2028 err
= addr_filter__entire_dso(filt
, dso
);
2032 if (filt
->sym_from
) {
2033 err
= find_dso_sym(dso
, filt
->sym_from
, &start
, &size
,
2034 filt
->sym_from_idx
);
2038 if (filt
->range
&& !filt
->size
&& !filt
->sym_to
)
2043 err
= find_dso_sym(dso
, filt
->sym_to
, &start
, &size
,
2048 err
= check_end_after_start(filt
, start
, size
);
2052 filt
->size
= start
+ size
- filt
->addr
;
2061 static char *addr_filter__to_str(struct addr_filter
*filt
)
2063 char filename_buf
[PATH_MAX
];
2064 const char *at
= "";
2065 const char *fn
= "";
2069 if (filt
->filename
) {
2071 fn
= realpath(filt
->filename
, filename_buf
);
2077 err
= asprintf(&filter
, "%s 0x%"PRIx64
"/0x%"PRIx64
"%s%s",
2078 filt
->action
, filt
->addr
, filt
->size
, at
, fn
);
2080 err
= asprintf(&filter
, "%s 0x%"PRIx64
"%s%s",
2081 filt
->action
, filt
->addr
, at
, fn
);
2084 return err
< 0 ? NULL
: filter
;
2087 static int parse_addr_filter(struct evsel
*evsel
, const char *filter
,
2090 struct addr_filters filts
;
2091 struct addr_filter
*filt
;
2094 addr_filters__init(&filts
);
2096 err
= addr_filters__parse_bare_filter(&filts
, filter
);
2100 if (filts
.cnt
> max_nr
) {
2101 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2107 list_for_each_entry(filt
, &filts
.head
, list
) {
2110 err
= addr_filter__resolve_syms(filt
);
2114 new_filter
= addr_filter__to_str(filt
);
2120 if (perf_evsel__append_addr_filter(evsel
, new_filter
)) {
2127 addr_filters__exit(&filts
);
2130 pr_err("Failed to parse address filter: '%s'\n", filter
);
2131 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2132 pr_err("Where multiple filters are separated by space or comma.\n");
2138 static struct perf_pmu
*perf_evsel__find_pmu(struct evsel
*evsel
)
2140 struct perf_pmu
*pmu
= NULL
;
2142 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
2143 if (pmu
->type
== evsel
->core
.attr
.type
)
2150 static int perf_evsel__nr_addr_filter(struct evsel
*evsel
)
2152 struct perf_pmu
*pmu
= perf_evsel__find_pmu(evsel
);
2153 int nr_addr_filters
= 0;
2158 perf_pmu__scan_file(pmu
, "nr_addr_filters", "%d", &nr_addr_filters
);
2160 return nr_addr_filters
;
2163 int auxtrace_parse_filters(struct evlist
*evlist
)
2165 struct evsel
*evsel
;
2169 evlist__for_each_entry(evlist
, evsel
) {
2170 filter
= evsel
->filter
;
2171 max_nr
= perf_evsel__nr_addr_filter(evsel
);
2172 if (!filter
|| !max_nr
)
2174 evsel
->filter
= NULL
;
2175 err
= parse_addr_filter(evsel
, filter
, max_nr
);
2179 pr_debug("Address filter: %s\n", evsel
->filter
);