2 * auxtrace.c: AUX area trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
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.
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
17 #include <sys/types.h>
25 #include <linux/kernel.h>
26 #include <linux/perf_event.h>
27 #include <linux/types.h>
28 #include <linux/bitops.h>
29 #include <linux/log2.h>
30 #include <linux/string.h>
32 #include <sys/param.h>
38 #include <linux/list.h>
48 #include "thread_map.h"
50 #include "symbol/kallsyms.h"
53 #include <linux/hash.h>
58 #include <subcmd/parse-options.h>
61 #include "intel-bts.h"
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 perf_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
].priv
= queues
->queue_array
[i
].priv
;
202 queues
->nr_queues
= nr_queues
;
203 queues
->queue_array
= queue_array
;
208 static void *auxtrace_copy_data(u64 size
, struct perf_session
*session
)
210 int fd
= perf_data_file__fd(session
->file
);
214 if (size
> SSIZE_MAX
)
221 ret
= readn(fd
, p
, size
);
222 if (ret
!= (ssize_t
)size
) {
230 static int auxtrace_queues__add_buffer(struct auxtrace_queues
*queues
,
232 struct auxtrace_buffer
*buffer
)
234 struct auxtrace_queue
*queue
;
237 if (idx
>= queues
->nr_queues
) {
238 err
= auxtrace_queues__grow(queues
, idx
+ 1);
243 queue
= &queues
->queue_array
[idx
];
247 queue
->tid
= buffer
->tid
;
248 queue
->cpu
= buffer
->cpu
;
249 } else if (buffer
->cpu
!= queue
->cpu
|| buffer
->tid
!= queue
->tid
) {
250 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
251 queue
->cpu
, queue
->tid
, buffer
->cpu
, buffer
->tid
);
255 buffer
->buffer_nr
= queues
->next_buffer_nr
++;
257 list_add_tail(&buffer
->list
, &queue
->head
);
259 queues
->new_data
= true;
260 queues
->populated
= true;
265 /* Limit buffers to 32MiB on 32-bit */
266 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
268 static int auxtrace_queues__split_buffer(struct auxtrace_queues
*queues
,
270 struct auxtrace_buffer
*buffer
)
272 u64 sz
= buffer
->size
;
273 bool consecutive
= false;
274 struct auxtrace_buffer
*b
;
277 while (sz
> BUFFER_LIMIT_FOR_32_BIT
) {
278 b
= memdup(buffer
, sizeof(struct auxtrace_buffer
));
281 b
->size
= BUFFER_LIMIT_FOR_32_BIT
;
282 b
->consecutive
= consecutive
;
283 err
= auxtrace_queues__add_buffer(queues
, idx
, b
);
285 auxtrace_buffer__free(b
);
288 buffer
->data_offset
+= BUFFER_LIMIT_FOR_32_BIT
;
289 sz
-= BUFFER_LIMIT_FOR_32_BIT
;
294 buffer
->consecutive
= consecutive
;
299 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues
*queues
,
300 struct perf_session
*session
,
302 struct auxtrace_buffer
*buffer
)
304 if (session
->one_mmap
) {
305 buffer
->data
= buffer
->data_offset
- session
->one_mmap_offset
+
306 session
->one_mmap_addr
;
307 } else if (perf_data_file__is_pipe(session
->file
)) {
308 buffer
->data
= auxtrace_copy_data(buffer
->size
, session
);
311 buffer
->data_needs_freeing
= true;
312 } else if (BITS_PER_LONG
== 32 &&
313 buffer
->size
> BUFFER_LIMIT_FOR_32_BIT
) {
316 err
= auxtrace_queues__split_buffer(queues
, idx
, buffer
);
321 return auxtrace_queues__add_buffer(queues
, idx
, buffer
);
324 int auxtrace_queues__add_event(struct auxtrace_queues
*queues
,
325 struct perf_session
*session
,
326 union perf_event
*event
, off_t data_offset
,
327 struct auxtrace_buffer
**buffer_ptr
)
329 struct auxtrace_buffer
*buffer
;
333 buffer
= zalloc(sizeof(struct auxtrace_buffer
));
338 buffer
->tid
= event
->auxtrace
.tid
;
339 buffer
->cpu
= event
->auxtrace
.cpu
;
340 buffer
->data_offset
= data_offset
;
341 buffer
->offset
= event
->auxtrace
.offset
;
342 buffer
->reference
= event
->auxtrace
.reference
;
343 buffer
->size
= event
->auxtrace
.size
;
344 idx
= event
->auxtrace
.idx
;
346 err
= auxtrace_queues__add_event_buffer(queues
, session
, idx
, buffer
);
351 *buffer_ptr
= buffer
;
356 auxtrace_buffer__free(buffer
);
360 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues
*queues
,
361 struct perf_session
*session
,
362 off_t file_offset
, size_t sz
)
364 union perf_event
*event
;
366 char buf
[PERF_SAMPLE_MAX_SIZE
];
368 err
= perf_session__peek_event(session
, file_offset
, buf
,
369 PERF_SAMPLE_MAX_SIZE
, &event
, NULL
);
373 if (event
->header
.type
== PERF_RECORD_AUXTRACE
) {
374 if (event
->header
.size
< sizeof(struct auxtrace_event
) ||
375 event
->header
.size
!= sz
) {
379 file_offset
+= event
->header
.size
;
380 err
= auxtrace_queues__add_event(queues
, session
, event
,
387 void auxtrace_queues__free(struct auxtrace_queues
*queues
)
391 for (i
= 0; i
< queues
->nr_queues
; i
++) {
392 while (!list_empty(&queues
->queue_array
[i
].head
)) {
393 struct auxtrace_buffer
*buffer
;
395 buffer
= list_entry(queues
->queue_array
[i
].head
.next
,
396 struct auxtrace_buffer
, list
);
397 list_del(&buffer
->list
);
398 auxtrace_buffer__free(buffer
);
402 zfree(&queues
->queue_array
);
403 queues
->nr_queues
= 0;
406 static void auxtrace_heapify(struct auxtrace_heap_item
*heap_array
,
407 unsigned int pos
, unsigned int queue_nr
,
413 parent
= (pos
- 1) >> 1;
414 if (heap_array
[parent
].ordinal
<= ordinal
)
416 heap_array
[pos
] = heap_array
[parent
];
419 heap_array
[pos
].queue_nr
= queue_nr
;
420 heap_array
[pos
].ordinal
= ordinal
;
423 int auxtrace_heap__add(struct auxtrace_heap
*heap
, unsigned int queue_nr
,
426 struct auxtrace_heap_item
*heap_array
;
428 if (queue_nr
>= heap
->heap_sz
) {
429 unsigned int heap_sz
= AUXTRACE_INIT_NR_QUEUES
;
431 while (heap_sz
<= queue_nr
)
433 heap_array
= realloc(heap
->heap_array
,
434 heap_sz
* sizeof(struct auxtrace_heap_item
));
437 heap
->heap_array
= heap_array
;
438 heap
->heap_sz
= heap_sz
;
441 auxtrace_heapify(heap
->heap_array
, heap
->heap_cnt
++, queue_nr
, ordinal
);
446 void auxtrace_heap__free(struct auxtrace_heap
*heap
)
448 zfree(&heap
->heap_array
);
453 void auxtrace_heap__pop(struct auxtrace_heap
*heap
)
455 unsigned int pos
, last
, heap_cnt
= heap
->heap_cnt
;
456 struct auxtrace_heap_item
*heap_array
;
463 heap_array
= heap
->heap_array
;
467 unsigned int left
, right
;
469 left
= (pos
<< 1) + 1;
470 if (left
>= heap_cnt
)
473 if (right
>= heap_cnt
) {
474 heap_array
[pos
] = heap_array
[left
];
477 if (heap_array
[left
].ordinal
< heap_array
[right
].ordinal
) {
478 heap_array
[pos
] = heap_array
[left
];
481 heap_array
[pos
] = heap_array
[right
];
487 auxtrace_heapify(heap_array
, pos
, heap_array
[last
].queue_nr
,
488 heap_array
[last
].ordinal
);
491 size_t auxtrace_record__info_priv_size(struct auxtrace_record
*itr
,
492 struct perf_evlist
*evlist
)
495 return itr
->info_priv_size(itr
, evlist
);
499 static int auxtrace_not_supported(void)
501 pr_err("AUX area tracing is not supported on this architecture\n");
505 int auxtrace_record__info_fill(struct auxtrace_record
*itr
,
506 struct perf_session
*session
,
507 struct auxtrace_info_event
*auxtrace_info
,
511 return itr
->info_fill(itr
, session
, auxtrace_info
, priv_size
);
512 return auxtrace_not_supported();
515 void auxtrace_record__free(struct auxtrace_record
*itr
)
521 int auxtrace_record__snapshot_start(struct auxtrace_record
*itr
)
523 if (itr
&& itr
->snapshot_start
)
524 return itr
->snapshot_start(itr
);
528 int auxtrace_record__snapshot_finish(struct auxtrace_record
*itr
)
530 if (itr
&& itr
->snapshot_finish
)
531 return itr
->snapshot_finish(itr
);
535 int auxtrace_record__find_snapshot(struct auxtrace_record
*itr
, int idx
,
536 struct auxtrace_mmap
*mm
,
537 unsigned char *data
, u64
*head
, u64
*old
)
539 if (itr
&& itr
->find_snapshot
)
540 return itr
->find_snapshot(itr
, idx
, mm
, data
, head
, old
);
544 int auxtrace_record__options(struct auxtrace_record
*itr
,
545 struct perf_evlist
*evlist
,
546 struct record_opts
*opts
)
549 return itr
->recording_options(itr
, evlist
, opts
);
553 u64
auxtrace_record__reference(struct auxtrace_record
*itr
)
556 return itr
->reference(itr
);
560 int auxtrace_parse_snapshot_options(struct auxtrace_record
*itr
,
561 struct record_opts
*opts
, const char *str
)
567 return itr
->parse_snapshot_options(itr
, opts
, str
);
569 pr_err("No AUX area tracing to snapshot\n");
573 struct auxtrace_record
*__weak
574 auxtrace_record__init(struct perf_evlist
*evlist __maybe_unused
, int *err
)
580 static int auxtrace_index__alloc(struct list_head
*head
)
582 struct auxtrace_index
*auxtrace_index
;
584 auxtrace_index
= malloc(sizeof(struct auxtrace_index
));
588 auxtrace_index
->nr
= 0;
589 INIT_LIST_HEAD(&auxtrace_index
->list
);
591 list_add_tail(&auxtrace_index
->list
, head
);
596 void auxtrace_index__free(struct list_head
*head
)
598 struct auxtrace_index
*auxtrace_index
, *n
;
600 list_for_each_entry_safe(auxtrace_index
, n
, head
, list
) {
601 list_del(&auxtrace_index
->list
);
602 free(auxtrace_index
);
606 static struct auxtrace_index
*auxtrace_index__last(struct list_head
*head
)
608 struct auxtrace_index
*auxtrace_index
;
611 if (list_empty(head
)) {
612 err
= auxtrace_index__alloc(head
);
617 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
, list
);
619 if (auxtrace_index
->nr
>= PERF_AUXTRACE_INDEX_ENTRY_COUNT
) {
620 err
= auxtrace_index__alloc(head
);
623 auxtrace_index
= list_entry(head
->prev
, struct auxtrace_index
,
627 return auxtrace_index
;
630 int auxtrace_index__auxtrace_event(struct list_head
*head
,
631 union perf_event
*event
, off_t file_offset
)
633 struct auxtrace_index
*auxtrace_index
;
636 auxtrace_index
= auxtrace_index__last(head
);
640 nr
= auxtrace_index
->nr
;
641 auxtrace_index
->entries
[nr
].file_offset
= file_offset
;
642 auxtrace_index
->entries
[nr
].sz
= event
->header
.size
;
643 auxtrace_index
->nr
+= 1;
648 static int auxtrace_index__do_write(int fd
,
649 struct auxtrace_index
*auxtrace_index
)
651 struct auxtrace_index_entry ent
;
654 for (i
= 0; i
< auxtrace_index
->nr
; i
++) {
655 ent
.file_offset
= auxtrace_index
->entries
[i
].file_offset
;
656 ent
.sz
= auxtrace_index
->entries
[i
].sz
;
657 if (writen(fd
, &ent
, sizeof(ent
)) != sizeof(ent
))
663 int auxtrace_index__write(int fd
, struct list_head
*head
)
665 struct auxtrace_index
*auxtrace_index
;
669 list_for_each_entry(auxtrace_index
, head
, list
)
670 total
+= auxtrace_index
->nr
;
672 if (writen(fd
, &total
, sizeof(total
)) != sizeof(total
))
675 list_for_each_entry(auxtrace_index
, head
, list
) {
676 err
= auxtrace_index__do_write(fd
, auxtrace_index
);
684 static int auxtrace_index__process_entry(int fd
, struct list_head
*head
,
687 struct auxtrace_index
*auxtrace_index
;
688 struct auxtrace_index_entry ent
;
691 if (readn(fd
, &ent
, sizeof(ent
)) != sizeof(ent
))
694 auxtrace_index
= auxtrace_index__last(head
);
698 nr
= auxtrace_index
->nr
;
700 auxtrace_index
->entries
[nr
].file_offset
=
701 bswap_64(ent
.file_offset
);
702 auxtrace_index
->entries
[nr
].sz
= bswap_64(ent
.sz
);
704 auxtrace_index
->entries
[nr
].file_offset
= ent
.file_offset
;
705 auxtrace_index
->entries
[nr
].sz
= ent
.sz
;
708 auxtrace_index
->nr
= nr
+ 1;
713 int auxtrace_index__process(int fd
, u64 size
, struct perf_session
*session
,
716 struct list_head
*head
= &session
->auxtrace_index
;
719 if (readn(fd
, &nr
, sizeof(u64
)) != sizeof(u64
))
725 if (sizeof(u64
) + nr
* sizeof(struct auxtrace_index_entry
) > size
)
731 err
= auxtrace_index__process_entry(fd
, head
, needs_swap
);
739 static int auxtrace_queues__process_index_entry(struct auxtrace_queues
*queues
,
740 struct perf_session
*session
,
741 struct auxtrace_index_entry
*ent
)
743 return auxtrace_queues__add_indexed_event(queues
, session
,
744 ent
->file_offset
, ent
->sz
);
747 int auxtrace_queues__process_index(struct auxtrace_queues
*queues
,
748 struct perf_session
*session
)
750 struct auxtrace_index
*auxtrace_index
;
751 struct auxtrace_index_entry
*ent
;
755 list_for_each_entry(auxtrace_index
, &session
->auxtrace_index
, list
) {
756 for (i
= 0; i
< auxtrace_index
->nr
; i
++) {
757 ent
= &auxtrace_index
->entries
[i
];
758 err
= auxtrace_queues__process_index_entry(queues
,
768 struct auxtrace_buffer
*auxtrace_buffer__next(struct auxtrace_queue
*queue
,
769 struct auxtrace_buffer
*buffer
)
772 if (list_is_last(&buffer
->list
, &queue
->head
))
774 return list_entry(buffer
->list
.next
, struct auxtrace_buffer
,
777 if (list_empty(&queue
->head
))
779 return list_entry(queue
->head
.next
, struct auxtrace_buffer
,
784 void *auxtrace_buffer__get_data(struct auxtrace_buffer
*buffer
, int fd
)
786 size_t adj
= buffer
->data_offset
& (page_size
- 1);
787 size_t size
= buffer
->size
+ adj
;
788 off_t file_offset
= buffer
->data_offset
- adj
;
794 addr
= mmap(NULL
, size
, PROT_READ
, MAP_SHARED
, fd
, file_offset
);
795 if (addr
== MAP_FAILED
)
798 buffer
->mmap_addr
= addr
;
799 buffer
->mmap_size
= size
;
801 buffer
->data
= addr
+ adj
;
806 void auxtrace_buffer__put_data(struct auxtrace_buffer
*buffer
)
808 if (!buffer
->data
|| !buffer
->mmap_addr
)
810 munmap(buffer
->mmap_addr
, buffer
->mmap_size
);
811 buffer
->mmap_addr
= NULL
;
812 buffer
->mmap_size
= 0;
814 buffer
->use_data
= NULL
;
817 void auxtrace_buffer__drop_data(struct auxtrace_buffer
*buffer
)
819 auxtrace_buffer__put_data(buffer
);
820 if (buffer
->data_needs_freeing
) {
821 buffer
->data_needs_freeing
= false;
822 zfree(&buffer
->data
);
823 buffer
->use_data
= NULL
;
828 void auxtrace_buffer__free(struct auxtrace_buffer
*buffer
)
830 auxtrace_buffer__drop_data(buffer
);
834 void auxtrace_synth_error(struct auxtrace_error_event
*auxtrace_error
, int type
,
835 int code
, int cpu
, pid_t pid
, pid_t tid
, u64 ip
,
840 memset(auxtrace_error
, 0, sizeof(struct auxtrace_error_event
));
842 auxtrace_error
->header
.type
= PERF_RECORD_AUXTRACE_ERROR
;
843 auxtrace_error
->type
= type
;
844 auxtrace_error
->code
= code
;
845 auxtrace_error
->cpu
= cpu
;
846 auxtrace_error
->pid
= pid
;
847 auxtrace_error
->tid
= tid
;
848 auxtrace_error
->ip
= ip
;
849 strlcpy(auxtrace_error
->msg
, msg
, MAX_AUXTRACE_ERROR_MSG
);
851 size
= (void *)auxtrace_error
->msg
- (void *)auxtrace_error
+
852 strlen(auxtrace_error
->msg
) + 1;
853 auxtrace_error
->header
.size
= PERF_ALIGN(size
, sizeof(u64
));
856 int perf_event__synthesize_auxtrace_info(struct auxtrace_record
*itr
,
857 struct perf_tool
*tool
,
858 struct perf_session
*session
,
859 perf_event__handler_t process
)
861 union perf_event
*ev
;
865 pr_debug2("Synthesizing auxtrace information\n");
866 priv_size
= auxtrace_record__info_priv_size(itr
, session
->evlist
);
867 ev
= zalloc(sizeof(struct auxtrace_info_event
) + priv_size
);
871 ev
->auxtrace_info
.header
.type
= PERF_RECORD_AUXTRACE_INFO
;
872 ev
->auxtrace_info
.header
.size
= sizeof(struct auxtrace_info_event
) +
874 err
= auxtrace_record__info_fill(itr
, session
, &ev
->auxtrace_info
,
879 err
= process(tool
, ev
, NULL
, NULL
);
885 static bool auxtrace__dont_decode(struct perf_session
*session
)
887 return !session
->itrace_synth_opts
||
888 session
->itrace_synth_opts
->dont_decode
;
891 int perf_event__process_auxtrace_info(struct perf_tool
*tool __maybe_unused
,
892 union perf_event
*event
,
893 struct perf_session
*session
)
895 enum auxtrace_type type
= event
->auxtrace_info
.type
;
898 fprintf(stdout
, " type: %u\n", type
);
901 case PERF_AUXTRACE_INTEL_PT
:
902 return intel_pt_process_auxtrace_info(event
, session
);
903 case PERF_AUXTRACE_INTEL_BTS
:
904 return intel_bts_process_auxtrace_info(event
, session
);
905 case PERF_AUXTRACE_CS_ETM
:
906 case PERF_AUXTRACE_UNKNOWN
:
912 s64
perf_event__process_auxtrace(struct perf_tool
*tool
,
913 union perf_event
*event
,
914 struct perf_session
*session
)
919 fprintf(stdout
, " size: %#"PRIx64
" offset: %#"PRIx64
" ref: %#"PRIx64
" idx: %u tid: %d cpu: %d\n",
920 event
->auxtrace
.size
, event
->auxtrace
.offset
,
921 event
->auxtrace
.reference
, event
->auxtrace
.idx
,
922 event
->auxtrace
.tid
, event
->auxtrace
.cpu
);
924 if (auxtrace__dont_decode(session
))
925 return event
->auxtrace
.size
;
927 if (!session
->auxtrace
|| event
->header
.type
!= PERF_RECORD_AUXTRACE
)
930 err
= session
->auxtrace
->process_auxtrace_event(session
, event
, tool
);
934 return event
->auxtrace
.size
;
937 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
938 #define PERF_ITRACE_DEFAULT_PERIOD 100000
939 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
940 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
941 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
942 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
944 void itrace_synth_opts__set_default(struct itrace_synth_opts
*synth_opts
)
946 synth_opts
->instructions
= true;
947 synth_opts
->branches
= true;
948 synth_opts
->transactions
= true;
949 synth_opts
->errors
= true;
950 synth_opts
->period_type
= PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
951 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
952 synth_opts
->callchain_sz
= PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
953 synth_opts
->last_branch_sz
= PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ
;
954 synth_opts
->initial_skip
= 0;
958 * Please check tools/perf/Documentation/perf-script.txt for information
959 * about the options parsed here, which is introduced after this cset,
960 * when support in 'perf script' for these options is introduced.
962 int itrace_parse_synth_opts(const struct option
*opt
, const char *str
,
965 struct itrace_synth_opts
*synth_opts
= opt
->value
;
968 bool period_type_set
= false;
969 bool period_set
= false;
971 synth_opts
->set
= true;
974 synth_opts
->dont_decode
= true;
979 itrace_synth_opts__set_default(synth_opts
);
986 synth_opts
->instructions
= true;
987 while (*p
== ' ' || *p
== ',')
990 synth_opts
->period
= strtoull(p
, &endptr
, 10);
993 while (*p
== ' ' || *p
== ',')
997 synth_opts
->period_type
=
998 PERF_ITRACE_PERIOD_INSTRUCTIONS
;
999 period_type_set
= true;
1002 synth_opts
->period_type
=
1003 PERF_ITRACE_PERIOD_TICKS
;
1004 period_type_set
= true;
1007 synth_opts
->period
*= 1000;
1010 synth_opts
->period
*= 1000;
1015 synth_opts
->period_type
=
1016 PERF_ITRACE_PERIOD_NANOSECS
;
1017 period_type_set
= true;
1027 synth_opts
->branches
= true;
1030 synth_opts
->transactions
= true;
1033 synth_opts
->errors
= true;
1036 synth_opts
->log
= true;
1039 synth_opts
->branches
= true;
1040 synth_opts
->calls
= true;
1043 synth_opts
->branches
= true;
1044 synth_opts
->returns
= true;
1047 synth_opts
->callchain
= true;
1048 synth_opts
->callchain_sz
=
1049 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ
;
1050 while (*p
== ' ' || *p
== ',')
1055 val
= strtoul(p
, &endptr
, 10);
1057 if (!val
|| val
> PERF_ITRACE_MAX_CALLCHAIN_SZ
)
1059 synth_opts
->callchain_sz
= val
;
1063 synth_opts
->last_branch
= true;
1064 synth_opts
->last_branch_sz
=
1065 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ
;
1066 while (*p
== ' ' || *p
== ',')
1071 val
= strtoul(p
, &endptr
, 10);
1074 val
> PERF_ITRACE_MAX_LAST_BRANCH_SZ
)
1076 synth_opts
->last_branch_sz
= val
;
1080 synth_opts
->initial_skip
= strtoul(p
, &endptr
, 10);
1093 if (synth_opts
->instructions
) {
1094 if (!period_type_set
)
1095 synth_opts
->period_type
=
1096 PERF_ITRACE_DEFAULT_PERIOD_TYPE
;
1098 synth_opts
->period
= PERF_ITRACE_DEFAULT_PERIOD
;
1104 pr_err("Bad Instruction Tracing options '%s'\n", str
);
1108 static const char * const auxtrace_error_type_name
[] = {
1109 [PERF_AUXTRACE_ERROR_ITRACE
] = "instruction trace",
1112 static const char *auxtrace_error_name(int type
)
1114 const char *error_type_name
= NULL
;
1116 if (type
< PERF_AUXTRACE_ERROR_MAX
)
1117 error_type_name
= auxtrace_error_type_name
[type
];
1118 if (!error_type_name
)
1119 error_type_name
= "unknown AUX";
1120 return error_type_name
;
1123 size_t perf_event__fprintf_auxtrace_error(union perf_event
*event
, FILE *fp
)
1125 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1128 ret
= fprintf(fp
, " %s error type %u",
1129 auxtrace_error_name(e
->type
), e
->type
);
1130 ret
+= fprintf(fp
, " cpu %d pid %d tid %d ip %#"PRIx64
" code %u: %s\n",
1131 e
->cpu
, e
->pid
, e
->tid
, e
->ip
, e
->code
, e
->msg
);
1135 void perf_session__auxtrace_error_inc(struct perf_session
*session
,
1136 union perf_event
*event
)
1138 struct auxtrace_error_event
*e
= &event
->auxtrace_error
;
1140 if (e
->type
< PERF_AUXTRACE_ERROR_MAX
)
1141 session
->evlist
->stats
.nr_auxtrace_errors
[e
->type
] += 1;
1144 void events_stats__auxtrace_error_warn(const struct events_stats
*stats
)
1148 for (i
= 0; i
< PERF_AUXTRACE_ERROR_MAX
; i
++) {
1149 if (!stats
->nr_auxtrace_errors
[i
])
1151 ui__warning("%u %s errors\n",
1152 stats
->nr_auxtrace_errors
[i
],
1153 auxtrace_error_name(i
));
1157 int perf_event__process_auxtrace_error(struct perf_tool
*tool __maybe_unused
,
1158 union perf_event
*event
,
1159 struct perf_session
*session
)
1161 if (auxtrace__dont_decode(session
))
1164 perf_event__fprintf_auxtrace_error(event
, stdout
);
1168 static int __auxtrace_mmap__read(struct auxtrace_mmap
*mm
,
1169 struct auxtrace_record
*itr
,
1170 struct perf_tool
*tool
, process_auxtrace_t fn
,
1171 bool snapshot
, size_t snapshot_size
)
1173 u64 head
, old
= mm
->prev
, offset
, ref
;
1174 unsigned char *data
= mm
->base
;
1175 size_t size
, head_off
, old_off
, len1
, len2
, padding
;
1176 union perf_event ev
;
1177 void *data1
, *data2
;
1180 head
= auxtrace_mmap__read_snapshot_head(mm
);
1181 if (auxtrace_record__find_snapshot(itr
, mm
->idx
, mm
, data
,
1185 head
= auxtrace_mmap__read_head(mm
);
1191 pr_debug3("auxtrace idx %d old %#"PRIx64
" head %#"PRIx64
" diff %#"PRIx64
"\n",
1192 mm
->idx
, old
, head
, head
- old
);
1195 head_off
= head
& mm
->mask
;
1196 old_off
= old
& mm
->mask
;
1198 head_off
= head
% mm
->len
;
1199 old_off
= old
% mm
->len
;
1202 if (head_off
> old_off
)
1203 size
= head_off
- old_off
;
1205 size
= mm
->len
- (old_off
- head_off
);
1207 if (snapshot
&& size
> snapshot_size
)
1208 size
= snapshot_size
;
1210 ref
= auxtrace_record__reference(itr
);
1212 if (head
> old
|| size
<= head
|| mm
->mask
) {
1213 offset
= head
- size
;
1216 * When the buffer size is not a power of 2, 'head' wraps at the
1217 * highest multiple of the buffer size, so we have to subtract
1218 * the remainder here.
1220 u64 rem
= (0ULL - mm
->len
) % mm
->len
;
1222 offset
= head
- size
- rem
;
1225 if (size
> head_off
) {
1226 len1
= size
- head_off
;
1227 data1
= &data
[mm
->len
- len1
];
1232 data1
= &data
[head_off
- len1
];
1237 if (itr
->alignment
) {
1238 unsigned int unwanted
= len1
% itr
->alignment
;
1244 /* padding must be written by fn() e.g. record__process_auxtrace() */
1247 padding
= 8 - padding
;
1249 memset(&ev
, 0, sizeof(ev
));
1250 ev
.auxtrace
.header
.type
= PERF_RECORD_AUXTRACE
;
1251 ev
.auxtrace
.header
.size
= sizeof(ev
.auxtrace
);
1252 ev
.auxtrace
.size
= size
+ padding
;
1253 ev
.auxtrace
.offset
= offset
;
1254 ev
.auxtrace
.reference
= ref
;
1255 ev
.auxtrace
.idx
= mm
->idx
;
1256 ev
.auxtrace
.tid
= mm
->tid
;
1257 ev
.auxtrace
.cpu
= mm
->cpu
;
1259 if (fn(tool
, &ev
, data1
, len1
, data2
, len2
))
1265 auxtrace_mmap__write_tail(mm
, head
);
1266 if (itr
->read_finish
) {
1269 err
= itr
->read_finish(itr
, mm
->idx
);
1278 int auxtrace_mmap__read(struct auxtrace_mmap
*mm
, struct auxtrace_record
*itr
,
1279 struct perf_tool
*tool
, process_auxtrace_t fn
)
1281 return __auxtrace_mmap__read(mm
, itr
, tool
, fn
, false, 0);
1284 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap
*mm
,
1285 struct auxtrace_record
*itr
,
1286 struct perf_tool
*tool
, process_auxtrace_t fn
,
1287 size_t snapshot_size
)
1289 return __auxtrace_mmap__read(mm
, itr
, tool
, fn
, true, snapshot_size
);
1293 * struct auxtrace_cache - hash table to implement a cache
1294 * @hashtable: the hashtable
1295 * @sz: hashtable size (number of hlists)
1296 * @entry_size: size of an entry
1297 * @limit: limit the number of entries to this maximum, when reached the cache
1298 * is dropped and caching begins again with an empty cache
1299 * @cnt: current number of entries
1300 * @bits: hashtable size (@sz = 2^@bits)
1302 struct auxtrace_cache
{
1303 struct hlist_head
*hashtable
;
1311 struct auxtrace_cache
*auxtrace_cache__new(unsigned int bits
, size_t entry_size
,
1312 unsigned int limit_percent
)
1314 struct auxtrace_cache
*c
;
1315 struct hlist_head
*ht
;
1318 c
= zalloc(sizeof(struct auxtrace_cache
));
1324 ht
= calloc(sz
, sizeof(struct hlist_head
));
1328 for (i
= 0; i
< sz
; i
++)
1329 INIT_HLIST_HEAD(&ht
[i
]);
1333 c
->entry_size
= entry_size
;
1334 c
->limit
= (c
->sz
* limit_percent
) / 100;
1344 static void auxtrace_cache__drop(struct auxtrace_cache
*c
)
1346 struct auxtrace_cache_entry
*entry
;
1347 struct hlist_node
*tmp
;
1353 for (i
= 0; i
< c
->sz
; i
++) {
1354 hlist_for_each_entry_safe(entry
, tmp
, &c
->hashtable
[i
], hash
) {
1355 hlist_del(&entry
->hash
);
1356 auxtrace_cache__free_entry(c
, entry
);
1363 void auxtrace_cache__free(struct auxtrace_cache
*c
)
1368 auxtrace_cache__drop(c
);
1373 void *auxtrace_cache__alloc_entry(struct auxtrace_cache
*c
)
1375 return malloc(c
->entry_size
);
1378 void auxtrace_cache__free_entry(struct auxtrace_cache
*c __maybe_unused
,
1384 int auxtrace_cache__add(struct auxtrace_cache
*c
, u32 key
,
1385 struct auxtrace_cache_entry
*entry
)
1387 if (c
->limit
&& ++c
->cnt
> c
->limit
)
1388 auxtrace_cache__drop(c
);
1391 hlist_add_head(&entry
->hash
, &c
->hashtable
[hash_32(key
, c
->bits
)]);
1396 void *auxtrace_cache__lookup(struct auxtrace_cache
*c
, u32 key
)
1398 struct auxtrace_cache_entry
*entry
;
1399 struct hlist_head
*hlist
;
1404 hlist
= &c
->hashtable
[hash_32(key
, c
->bits
)];
1405 hlist_for_each_entry(entry
, hlist
, hash
) {
1406 if (entry
->key
== key
)
1413 static void addr_filter__free_str(struct addr_filter
*filt
)
1416 filt
->action
= NULL
;
1417 filt
->sym_from
= NULL
;
1418 filt
->sym_to
= NULL
;
1419 filt
->filename
= NULL
;
1423 static struct addr_filter
*addr_filter__new(void)
1425 struct addr_filter
*filt
= zalloc(sizeof(*filt
));
1428 INIT_LIST_HEAD(&filt
->list
);
1433 static void addr_filter__free(struct addr_filter
*filt
)
1436 addr_filter__free_str(filt
);
1440 static void addr_filters__add(struct addr_filters
*filts
,
1441 struct addr_filter
*filt
)
1443 list_add_tail(&filt
->list
, &filts
->head
);
1447 static void addr_filters__del(struct addr_filters
*filts
,
1448 struct addr_filter
*filt
)
1450 list_del_init(&filt
->list
);
1454 void addr_filters__init(struct addr_filters
*filts
)
1456 INIT_LIST_HEAD(&filts
->head
);
1460 void addr_filters__exit(struct addr_filters
*filts
)
1462 struct addr_filter
*filt
, *n
;
1464 list_for_each_entry_safe(filt
, n
, &filts
->head
, list
) {
1465 addr_filters__del(filts
, filt
);
1466 addr_filter__free(filt
);
1470 static int parse_num_or_str(char **inp
, u64
*num
, const char **str
,
1471 const char *str_delim
)
1473 *inp
+= strspn(*inp
, " ");
1475 if (isdigit(**inp
)) {
1481 *num
= strtoull(*inp
, &endptr
, 0);
1492 *inp
+= strspn(*inp
, " ");
1494 n
= strcspn(*inp
, str_delim
);
1506 static int parse_action(struct addr_filter
*filt
)
1508 if (!strcmp(filt
->action
, "filter")) {
1511 } else if (!strcmp(filt
->action
, "start")) {
1513 } else if (!strcmp(filt
->action
, "stop")) {
1514 filt
->start
= false;
1515 } else if (!strcmp(filt
->action
, "tracestop")) {
1516 filt
->start
= false;
1518 filt
->action
+= 5; /* Change 'tracestop' to 'stop' */
1525 static int parse_sym_idx(char **inp
, int *idx
)
1529 *inp
+= strspn(*inp
, " ");
1536 if (**inp
== 'g' || **inp
== 'G') {
1544 num
= strtoul(*inp
, &endptr
, 0);
1547 if (endptr
== *inp
|| num
> INT_MAX
)
1556 static int parse_addr_size(char **inp
, u64
*num
, const char **str
, int *idx
)
1558 int err
= parse_num_or_str(inp
, num
, str
, " ");
1561 err
= parse_sym_idx(inp
, idx
);
1566 static int parse_one_filter(struct addr_filter
*filt
, const char **filter_inp
)
1571 filt
->str
= fstr
= strdup(*filter_inp
);
1575 err
= parse_num_or_str(&fstr
, NULL
, &filt
->action
, " ");
1579 err
= parse_action(filt
);
1583 err
= parse_addr_size(&fstr
, &filt
->addr
, &filt
->sym_from
,
1584 &filt
->sym_from_idx
);
1588 fstr
+= strspn(fstr
, " ");
1592 err
= parse_addr_size(&fstr
, &filt
->size
, &filt
->sym_to
,
1599 fstr
+= strspn(fstr
, " ");
1603 err
= parse_num_or_str(&fstr
, NULL
, &filt
->filename
, " ,");
1608 fstr
+= strspn(fstr
, " ,");
1610 *filter_inp
+= fstr
- filt
->str
;
1615 addr_filter__free_str(filt
);
1620 int addr_filters__parse_bare_filter(struct addr_filters
*filts
,
1623 struct addr_filter
*filt
;
1624 const char *fstr
= filter
;
1628 filt
= addr_filter__new();
1629 err
= parse_one_filter(filt
, &fstr
);
1631 addr_filter__free(filt
);
1632 addr_filters__exit(filts
);
1635 addr_filters__add(filts
, filt
);
1654 static bool kern_sym_match(struct sym_args
*args
, const char *name
, char type
)
1656 /* A function with the same name, and global or the n'th found or any */
1657 return symbol_type__is_a(type
, MAP__FUNCTION
) &&
1658 !strcmp(name
, args
->name
) &&
1659 ((args
->global
&& isupper(type
)) ||
1660 (args
->selected
&& ++(args
->cnt
) == args
->idx
) ||
1661 (!args
->global
&& !args
->selected
));
1664 static int find_kern_sym_cb(void *arg
, const char *name
, char type
, u64 start
)
1666 struct sym_args
*args
= arg
;
1668 if (args
->started
) {
1670 args
->size
= start
- args
->start
;
1671 if (args
->selected
) {
1674 } else if (kern_sym_match(args
, name
, type
)) {
1675 args
->duplicate
= true;
1678 } else if (kern_sym_match(args
, name
, type
)) {
1679 args
->started
= true;
1680 args
->start
= start
;
1686 static int print_kern_sym_cb(void *arg
, const char *name
, char type
, u64 start
)
1688 struct sym_args
*args
= arg
;
1690 if (kern_sym_match(args
, name
, type
)) {
1691 pr_err("#%d\t0x%"PRIx64
"\t%c\t%s\n",
1692 ++args
->cnt
, start
, type
, name
);
1694 } else if (args
->near
) {
1696 pr_err("\t\twhich is near\t\t%s\n", name
);
1702 static int sym_not_found_error(const char *sym_name
, int idx
)
1705 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1708 pr_err("Global symbol '%s' not found.\n", sym_name
);
1710 pr_err("Symbol '%s' not found.\n", sym_name
);
1712 pr_err("Note that symbols must be functions.\n");
1717 static int find_kern_sym(const char *sym_name
, u64
*start
, u64
*size
, int idx
)
1719 struct sym_args args
= {
1723 .selected
= idx
> 0,
1730 err
= kallsyms__parse("/proc/kallsyms", &args
, find_kern_sym_cb
);
1732 pr_err("Failed to parse /proc/kallsyms\n");
1736 if (args
.duplicate
) {
1737 pr_err("Multiple kernel symbols with name '%s'\n", sym_name
);
1739 kallsyms__parse("/proc/kallsyms", &args
, print_kern_sym_cb
);
1740 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1742 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1746 if (!args
.started
) {
1747 pr_err("Kernel symbol lookup: ");
1748 return sym_not_found_error(sym_name
, idx
);
1751 *start
= args
.start
;
1757 static int find_entire_kern_cb(void *arg
, const char *name __maybe_unused
,
1758 char type
, u64 start
)
1760 struct sym_args
*args
= arg
;
1762 if (!symbol_type__is_a(type
, MAP__FUNCTION
))
1765 if (!args
->started
) {
1766 args
->started
= true;
1767 args
->start
= start
;
1769 /* Don't know exactly where the kernel ends, so we add a page */
1770 args
->size
= round_up(start
, page_size
) + page_size
- args
->start
;
1775 static int addr_filter__entire_kernel(struct addr_filter
*filt
)
1777 struct sym_args args
= { .started
= false };
1780 err
= kallsyms__parse("/proc/kallsyms", &args
, find_entire_kern_cb
);
1781 if (err
< 0 || !args
.started
) {
1782 pr_err("Failed to parse /proc/kallsyms\n");
1786 filt
->addr
= args
.start
;
1787 filt
->size
= args
.size
;
1792 static int check_end_after_start(struct addr_filter
*filt
, u64 start
, u64 size
)
1794 if (start
+ size
>= filt
->addr
)
1797 if (filt
->sym_from
) {
1798 pr_err("Symbol '%s' (0x%"PRIx64
") comes before '%s' (0x%"PRIx64
")\n",
1799 filt
->sym_to
, start
, filt
->sym_from
, filt
->addr
);
1801 pr_err("Symbol '%s' (0x%"PRIx64
") comes before address 0x%"PRIx64
")\n",
1802 filt
->sym_to
, start
, filt
->addr
);
1808 static int addr_filter__resolve_kernel_syms(struct addr_filter
*filt
)
1810 bool no_size
= false;
1814 if (symbol_conf
.kptr_restrict
) {
1815 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1819 if (filt
->sym_from
&& !strcmp(filt
->sym_from
, "*"))
1820 return addr_filter__entire_kernel(filt
);
1822 if (filt
->sym_from
) {
1823 err
= find_kern_sym(filt
->sym_from
, &start
, &size
,
1824 filt
->sym_from_idx
);
1828 if (filt
->range
&& !filt
->size
&& !filt
->sym_to
) {
1835 err
= find_kern_sym(filt
->sym_to
, &start
, &size
,
1840 err
= check_end_after_start(filt
, start
, size
);
1843 filt
->size
= start
+ size
- filt
->addr
;
1847 /* The very last symbol in kallsyms does not imply a particular size */
1849 pr_err("Cannot determine size of symbol '%s'\n",
1850 filt
->sym_to
? filt
->sym_to
: filt
->sym_from
);
1857 static struct dso
*load_dso(const char *name
)
1862 map
= dso__new_map(name
);
1868 dso
= dso__get(map
->dso
);
1875 static bool dso_sym_match(struct symbol
*sym
, const char *name
, int *cnt
,
1878 /* Same name, and global or the n'th found or any */
1879 return !arch__compare_symbol_names(name
, sym
->name
) &&
1880 ((!idx
&& sym
->binding
== STB_GLOBAL
) ||
1881 (idx
> 0 && ++*cnt
== idx
) ||
1885 static void print_duplicate_syms(struct dso
*dso
, const char *sym_name
)
1891 pr_err("Multiple symbols with name '%s'\n", sym_name
);
1893 sym
= dso__first_symbol(dso
, MAP__FUNCTION
);
1895 if (dso_sym_match(sym
, sym_name
, &cnt
, -1)) {
1896 pr_err("#%d\t0x%"PRIx64
"\t%c\t%s\n",
1898 sym
->binding
== STB_GLOBAL
? 'g' :
1899 sym
->binding
== STB_LOCAL
? 'l' : 'w',
1904 pr_err("\t\twhich is near\t\t%s\n", sym
->name
);
1906 sym
= dso__next_symbol(sym
);
1909 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1911 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1914 static int find_dso_sym(struct dso
*dso
, const char *sym_name
, u64
*start
,
1923 sym
= dso__first_symbol(dso
, MAP__FUNCTION
);
1927 *size
= sym
->start
- *start
;
1931 } else if (dso_sym_match(sym
, sym_name
, &cnt
, idx
)) {
1932 print_duplicate_syms(dso
, sym_name
);
1935 } else if (dso_sym_match(sym
, sym_name
, &cnt
, idx
)) {
1936 *start
= sym
->start
;
1937 *size
= sym
->end
- sym
->start
;
1939 sym
= dso__next_symbol(sym
);
1943 return sym_not_found_error(sym_name
, idx
);
1948 static int addr_filter__entire_dso(struct addr_filter
*filt
, struct dso
*dso
)
1950 struct symbol
*first_sym
= dso__first_symbol(dso
, MAP__FUNCTION
);
1951 struct symbol
*last_sym
= dso__last_symbol(dso
, MAP__FUNCTION
);
1953 if (!first_sym
|| !last_sym
) {
1954 pr_err("Failed to determine filter for %s\nNo symbols found.\n",
1959 filt
->addr
= first_sym
->start
;
1960 filt
->size
= last_sym
->end
- first_sym
->start
;
1965 static int addr_filter__resolve_syms(struct addr_filter
*filt
)
1971 if (!filt
->sym_from
&& !filt
->sym_to
)
1974 if (!filt
->filename
)
1975 return addr_filter__resolve_kernel_syms(filt
);
1977 dso
= load_dso(filt
->filename
);
1979 pr_err("Failed to load symbols from: %s\n", filt
->filename
);
1983 if (filt
->sym_from
&& !strcmp(filt
->sym_from
, "*")) {
1984 err
= addr_filter__entire_dso(filt
, dso
);
1988 if (filt
->sym_from
) {
1989 err
= find_dso_sym(dso
, filt
->sym_from
, &start
, &size
,
1990 filt
->sym_from_idx
);
1994 if (filt
->range
&& !filt
->size
&& !filt
->sym_to
)
1999 err
= find_dso_sym(dso
, filt
->sym_to
, &start
, &size
,
2004 err
= check_end_after_start(filt
, start
, size
);
2008 filt
->size
= start
+ size
- filt
->addr
;
2017 static char *addr_filter__to_str(struct addr_filter
*filt
)
2019 char filename_buf
[PATH_MAX
];
2020 const char *at
= "";
2021 const char *fn
= "";
2025 if (filt
->filename
) {
2027 fn
= realpath(filt
->filename
, filename_buf
);
2033 err
= asprintf(&filter
, "%s 0x%"PRIx64
"/0x%"PRIx64
"%s%s",
2034 filt
->action
, filt
->addr
, filt
->size
, at
, fn
);
2036 err
= asprintf(&filter
, "%s 0x%"PRIx64
"%s%s",
2037 filt
->action
, filt
->addr
, at
, fn
);
2040 return err
< 0 ? NULL
: filter
;
2043 static int parse_addr_filter(struct perf_evsel
*evsel
, const char *filter
,
2046 struct addr_filters filts
;
2047 struct addr_filter
*filt
;
2050 addr_filters__init(&filts
);
2052 err
= addr_filters__parse_bare_filter(&filts
, filter
);
2056 if (filts
.cnt
> max_nr
) {
2057 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2063 list_for_each_entry(filt
, &filts
.head
, list
) {
2066 err
= addr_filter__resolve_syms(filt
);
2070 new_filter
= addr_filter__to_str(filt
);
2076 if (perf_evsel__append_addr_filter(evsel
, new_filter
)) {
2083 addr_filters__exit(&filts
);
2086 pr_err("Failed to parse address filter: '%s'\n", filter
);
2087 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2088 pr_err("Where multiple filters are separated by space or comma.\n");
2094 static struct perf_pmu
*perf_evsel__find_pmu(struct perf_evsel
*evsel
)
2096 struct perf_pmu
*pmu
= NULL
;
2098 while ((pmu
= perf_pmu__scan(pmu
)) != NULL
) {
2099 if (pmu
->type
== evsel
->attr
.type
)
2106 static int perf_evsel__nr_addr_filter(struct perf_evsel
*evsel
)
2108 struct perf_pmu
*pmu
= perf_evsel__find_pmu(evsel
);
2109 int nr_addr_filters
= 0;
2114 perf_pmu__scan_file(pmu
, "nr_addr_filters", "%d", &nr_addr_filters
);
2116 return nr_addr_filters
;
2119 int auxtrace_parse_filters(struct perf_evlist
*evlist
)
2121 struct perf_evsel
*evsel
;
2125 evlist__for_each_entry(evlist
, evsel
) {
2126 filter
= evsel
->filter
;
2127 max_nr
= perf_evsel__nr_addr_filter(evsel
);
2128 if (!filter
|| !max_nr
)
2130 evsel
->filter
= NULL
;
2131 err
= parse_addr_filter(evsel
, filter
, max_nr
);
2135 pr_debug("Address filter: %s\n", evsel
->filter
);