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>
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>
31 #include <sys/param.h>
34 #include <linux/list.h>
44 #include "thread_map.h"
48 #include <linux/hash.h>
53 #include <subcmd/parse-options.h>
57 #include "intel-bts.h"
60 #include "sane_ctype.h"
61 #include "symbol/kallsyms.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__fd(session->data);
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__is_pipe(session->data)) {
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 static bool filter_cpu(struct perf_session *session, int cpu)
326 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
328 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap);
331 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
332 struct perf_session *session,
333 union perf_event *event, off_t data_offset,
334 struct auxtrace_buffer **buffer_ptr)
336 struct auxtrace_buffer *buffer;
340 if (filter_cpu(session, event->auxtrace.cpu))
343 buffer = zalloc(sizeof(struct auxtrace_buffer));
348 buffer->tid = event->auxtrace.tid;
349 buffer->cpu = event->auxtrace.cpu;
350 buffer->data_offset = data_offset;
351 buffer->offset = event->auxtrace.offset;
352 buffer->reference = event->auxtrace.reference;
353 buffer->size = event->auxtrace.size;
354 idx = event->auxtrace.idx;
356 err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
361 *buffer_ptr = buffer;
366 auxtrace_buffer__free(buffer);
370 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
371 struct perf_session *session,
372 off_t file_offset, size_t sz)
374 union perf_event *event;
376 char buf[PERF_SAMPLE_MAX_SIZE];
378 err = perf_session__peek_event(session, file_offset, buf,
379 PERF_SAMPLE_MAX_SIZE, &event, NULL);
383 if (event->header.type == PERF_RECORD_AUXTRACE) {
384 if (event->header.size < sizeof(struct auxtrace_event) ||
385 event->header.size != sz) {
389 file_offset += event->header.size;
390 err = auxtrace_queues__add_event(queues, session, event,
397 void auxtrace_queues__free(struct auxtrace_queues *queues)
401 for (i = 0; i < queues->nr_queues; i++) {
402 while (!list_empty(&queues->queue_array[i].head)) {
403 struct auxtrace_buffer *buffer;
405 buffer = list_entry(queues->queue_array[i].head.next,
406 struct auxtrace_buffer, list);
407 list_del(&buffer->list);
408 auxtrace_buffer__free(buffer);
412 zfree(&queues->queue_array);
413 queues->nr_queues = 0;
416 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
417 unsigned int pos, unsigned int queue_nr,
423 parent = (pos - 1) >> 1;
424 if (heap_array[parent].ordinal <= ordinal)
426 heap_array[pos] = heap_array[parent];
429 heap_array[pos].queue_nr = queue_nr;
430 heap_array[pos].ordinal = ordinal;
433 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
436 struct auxtrace_heap_item *heap_array;
438 if (queue_nr >= heap->heap_sz) {
439 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
441 while (heap_sz <= queue_nr)
443 heap_array = realloc(heap->heap_array,
444 heap_sz * sizeof(struct auxtrace_heap_item));
447 heap->heap_array = heap_array;
448 heap->heap_sz = heap_sz;
451 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
456 void auxtrace_heap__free(struct auxtrace_heap *heap)
458 zfree(&heap->heap_array);
463 void auxtrace_heap__pop(struct auxtrace_heap *heap)
465 unsigned int pos, last, heap_cnt = heap->heap_cnt;
466 struct auxtrace_heap_item *heap_array;
473 heap_array = heap->heap_array;
477 unsigned int left, right;
479 left = (pos << 1) + 1;
480 if (left >= heap_cnt)
483 if (right >= heap_cnt) {
484 heap_array[pos] = heap_array[left];
487 if (heap_array[left].ordinal < heap_array[right].ordinal) {
488 heap_array[pos] = heap_array[left];
491 heap_array[pos] = heap_array[right];
497 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
498 heap_array[last].ordinal);
501 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
502 struct perf_evlist *evlist)
505 return itr->info_priv_size(itr, evlist);
509 static int auxtrace_not_supported(void)
511 pr_err("AUX area tracing is not supported on this architecture\n");
515 int auxtrace_record__info_fill(struct auxtrace_record *itr,
516 struct perf_session *session,
517 struct auxtrace_info_event *auxtrace_info,
521 return itr->info_fill(itr, session, auxtrace_info, priv_size);
522 return auxtrace_not_supported();
525 void auxtrace_record__free(struct auxtrace_record *itr)
531 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
533 if (itr && itr->snapshot_start)
534 return itr->snapshot_start(itr);
538 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
540 if (itr && itr->snapshot_finish)
541 return itr->snapshot_finish(itr);
545 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
546 struct auxtrace_mmap *mm,
547 unsigned char *data, u64 *head, u64 *old)
549 if (itr && itr->find_snapshot)
550 return itr->find_snapshot(itr, idx, mm, data, head, old);
554 int auxtrace_record__options(struct auxtrace_record *itr,
555 struct perf_evlist *evlist,
556 struct record_opts *opts)
559 return itr->recording_options(itr, evlist, opts);
563 u64 auxtrace_record__reference(struct auxtrace_record *itr)
566 return itr->reference(itr);
570 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
571 struct record_opts *opts, const char *str)
577 return itr->parse_snapshot_options(itr, opts, str);
579 pr_err("No AUX area tracing to snapshot\n");
583 struct auxtrace_record *__weak
584 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
590 static int auxtrace_index__alloc(struct list_head *head)
592 struct auxtrace_index *auxtrace_index;
594 auxtrace_index = malloc(sizeof(struct auxtrace_index));
598 auxtrace_index->nr = 0;
599 INIT_LIST_HEAD(&auxtrace_index->list);
601 list_add_tail(&auxtrace_index->list, head);
606 void auxtrace_index__free(struct list_head *head)
608 struct auxtrace_index *auxtrace_index, *n;
610 list_for_each_entry_safe(auxtrace_index, n, head, list) {
611 list_del(&auxtrace_index->list);
612 free(auxtrace_index);
616 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
618 struct auxtrace_index *auxtrace_index;
621 if (list_empty(head)) {
622 err = auxtrace_index__alloc(head);
627 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
629 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
630 err = auxtrace_index__alloc(head);
633 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
637 return auxtrace_index;
640 int auxtrace_index__auxtrace_event(struct list_head *head,
641 union perf_event *event, off_t file_offset)
643 struct auxtrace_index *auxtrace_index;
646 auxtrace_index = auxtrace_index__last(head);
650 nr = auxtrace_index->nr;
651 auxtrace_index->entries[nr].file_offset = file_offset;
652 auxtrace_index->entries[nr].sz = event->header.size;
653 auxtrace_index->nr += 1;
658 static int auxtrace_index__do_write(int fd,
659 struct auxtrace_index *auxtrace_index)
661 struct auxtrace_index_entry ent;
664 for (i = 0; i < auxtrace_index->nr; i++) {
665 ent.file_offset = auxtrace_index->entries[i].file_offset;
666 ent.sz = auxtrace_index->entries[i].sz;
667 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
673 int auxtrace_index__write(int fd, struct list_head *head)
675 struct auxtrace_index *auxtrace_index;
679 list_for_each_entry(auxtrace_index, head, list)
680 total += auxtrace_index->nr;
682 if (writen(fd, &total, sizeof(total)) != sizeof(total))
685 list_for_each_entry(auxtrace_index, head, list) {
686 err = auxtrace_index__do_write(fd, auxtrace_index);
694 static int auxtrace_index__process_entry(int fd, struct list_head *head,
697 struct auxtrace_index *auxtrace_index;
698 struct auxtrace_index_entry ent;
701 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
704 auxtrace_index = auxtrace_index__last(head);
708 nr = auxtrace_index->nr;
710 auxtrace_index->entries[nr].file_offset =
711 bswap_64(ent.file_offset);
712 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
714 auxtrace_index->entries[nr].file_offset = ent.file_offset;
715 auxtrace_index->entries[nr].sz = ent.sz;
718 auxtrace_index->nr = nr + 1;
723 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
726 struct list_head *head = &session->auxtrace_index;
729 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
735 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
741 err = auxtrace_index__process_entry(fd, head, needs_swap);
749 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
750 struct perf_session *session,
751 struct auxtrace_index_entry *ent)
753 return auxtrace_queues__add_indexed_event(queues, session,
754 ent->file_offset, ent->sz);
757 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
758 struct perf_session *session)
760 struct auxtrace_index *auxtrace_index;
761 struct auxtrace_index_entry *ent;
765 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
766 for (i = 0; i < auxtrace_index->nr; i++) {
767 ent = &auxtrace_index->entries[i];
768 err = auxtrace_queues__process_index_entry(queues,
778 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
779 struct auxtrace_buffer *buffer)
782 if (list_is_last(&buffer->list, &queue->head))
784 return list_entry(buffer->list.next, struct auxtrace_buffer,
787 if (list_empty(&queue->head))
789 return list_entry(queue->head.next, struct auxtrace_buffer,
794 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
796 size_t adj = buffer->data_offset & (page_size - 1);
797 size_t size = buffer->size + adj;
798 off_t file_offset = buffer->data_offset - adj;
804 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
805 if (addr == MAP_FAILED)
808 buffer->mmap_addr = addr;
809 buffer->mmap_size = size;
811 buffer->data = addr + adj;
816 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
818 if (!buffer->data || !buffer->mmap_addr)
820 munmap(buffer->mmap_addr, buffer->mmap_size);
821 buffer->mmap_addr = NULL;
822 buffer->mmap_size = 0;
824 buffer->use_data = NULL;
827 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
829 auxtrace_buffer__put_data(buffer);
830 if (buffer->data_needs_freeing) {
831 buffer->data_needs_freeing = false;
832 zfree(&buffer->data);
833 buffer->use_data = NULL;
838 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
840 auxtrace_buffer__drop_data(buffer);
844 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
845 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
850 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
852 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
853 auxtrace_error->type = type;
854 auxtrace_error->code = code;
855 auxtrace_error->cpu = cpu;
856 auxtrace_error->pid = pid;
857 auxtrace_error->tid = tid;
858 auxtrace_error->ip = ip;
859 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
861 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
862 strlen(auxtrace_error->msg) + 1;
863 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
866 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
867 struct perf_tool *tool,
868 struct perf_session *session,
869 perf_event__handler_t process)
871 union perf_event *ev;
875 pr_debug2("Synthesizing auxtrace information\n");
876 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
877 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
881 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
882 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
884 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
889 err = process(tool, ev, NULL, NULL);
895 static bool auxtrace__dont_decode(struct perf_session *session)
897 return !session->itrace_synth_opts ||
898 session->itrace_synth_opts->dont_decode;
901 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
902 union perf_event *event,
903 struct perf_session *session)
905 enum auxtrace_type type = event->auxtrace_info.type;
908 fprintf(stdout, " type: %u\n", type);
911 case PERF_AUXTRACE_INTEL_PT:
912 return intel_pt_process_auxtrace_info(event, session);
913 case PERF_AUXTRACE_INTEL_BTS:
914 return intel_bts_process_auxtrace_info(event, session);
915 case PERF_AUXTRACE_ARM_SPE:
916 return arm_spe_process_auxtrace_info(event, session);
917 case PERF_AUXTRACE_CS_ETM:
918 return cs_etm__process_auxtrace_info(event, session);
919 case PERF_AUXTRACE_UNKNOWN:
925 s64 perf_event__process_auxtrace(struct perf_tool *tool,
926 union perf_event *event,
927 struct perf_session *session)
932 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
933 event->auxtrace.size, event->auxtrace.offset,
934 event->auxtrace.reference, event->auxtrace.idx,
935 event->auxtrace.tid, event->auxtrace.cpu);
937 if (auxtrace__dont_decode(session))
938 return event->auxtrace.size;
940 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
943 err = session->auxtrace->process_auxtrace_event(session, event, tool);
947 return event->auxtrace.size;
950 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
951 #define PERF_ITRACE_DEFAULT_PERIOD 100000
952 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
953 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
954 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
955 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
957 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
959 synth_opts->instructions = true;
960 synth_opts->branches = true;
961 synth_opts->transactions = true;
962 synth_opts->ptwrites = true;
963 synth_opts->pwr_events = true;
964 synth_opts->errors = true;
965 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
966 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
967 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
968 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
969 synth_opts->initial_skip = 0;
973 * Please check tools/perf/Documentation/perf-script.txt for information
974 * about the options parsed here, which is introduced after this cset,
975 * when support in 'perf script' for these options is introduced.
977 int itrace_parse_synth_opts(const struct option *opt, const char *str,
980 struct itrace_synth_opts *synth_opts = opt->value;
983 bool period_type_set = false;
984 bool period_set = false;
986 synth_opts->set = true;
989 synth_opts->dont_decode = true;
994 itrace_synth_opts__set_default(synth_opts);
1001 synth_opts->instructions = true;
1002 while (*p == ' ' || *p == ',')
1005 synth_opts->period = strtoull(p, &endptr, 10);
1008 while (*p == ' ' || *p == ',')
1012 synth_opts->period_type =
1013 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1014 period_type_set = true;
1017 synth_opts->period_type =
1018 PERF_ITRACE_PERIOD_TICKS;
1019 period_type_set = true;
1022 synth_opts->period *= 1000;
1025 synth_opts->period *= 1000;
1030 synth_opts->period_type =
1031 PERF_ITRACE_PERIOD_NANOSECS;
1032 period_type_set = true;
1042 synth_opts->branches = true;
1045 synth_opts->transactions = true;
1048 synth_opts->ptwrites = true;
1051 synth_opts->pwr_events = true;
1054 synth_opts->errors = true;
1057 synth_opts->log = true;
1060 synth_opts->branches = true;
1061 synth_opts->calls = true;
1064 synth_opts->branches = true;
1065 synth_opts->returns = true;
1068 synth_opts->callchain = true;
1069 synth_opts->callchain_sz =
1070 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1071 while (*p == ' ' || *p == ',')
1076 val = strtoul(p, &endptr, 10);
1078 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1080 synth_opts->callchain_sz = val;
1084 synth_opts->last_branch = true;
1085 synth_opts->last_branch_sz =
1086 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1087 while (*p == ' ' || *p == ',')
1092 val = strtoul(p, &endptr, 10);
1095 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1097 synth_opts->last_branch_sz = val;
1101 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1114 if (synth_opts->instructions) {
1115 if (!period_type_set)
1116 synth_opts->period_type =
1117 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1119 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1125 pr_err("Bad Instruction Tracing options '%s'\n", str);
1129 static const char * const auxtrace_error_type_name[] = {
1130 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1133 static const char *auxtrace_error_name(int type)
1135 const char *error_type_name = NULL;
1137 if (type < PERF_AUXTRACE_ERROR_MAX)
1138 error_type_name = auxtrace_error_type_name[type];
1139 if (!error_type_name)
1140 error_type_name = "unknown AUX";
1141 return error_type_name;
1144 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1146 struct auxtrace_error_event *e = &event->auxtrace_error;
1149 ret = fprintf(fp, " %s error type %u",
1150 auxtrace_error_name(e->type), e->type);
1151 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1152 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1156 void perf_session__auxtrace_error_inc(struct perf_session *session,
1157 union perf_event *event)
1159 struct auxtrace_error_event *e = &event->auxtrace_error;
1161 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1162 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1165 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1169 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1170 if (!stats->nr_auxtrace_errors[i])
1172 ui__warning("%u %s errors\n",
1173 stats->nr_auxtrace_errors[i],
1174 auxtrace_error_name(i));
1178 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1179 union perf_event *event,
1180 struct perf_session *session)
1182 if (auxtrace__dont_decode(session))
1185 perf_event__fprintf_auxtrace_error(event, stdout);
1189 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1190 struct auxtrace_record *itr,
1191 struct perf_tool *tool, process_auxtrace_t fn,
1192 bool snapshot, size_t snapshot_size)
1194 u64 head, old = mm->prev, offset, ref;
1195 unsigned char *data = mm->base;
1196 size_t size, head_off, old_off, len1, len2, padding;
1197 union perf_event ev;
1198 void *data1, *data2;
1201 head = auxtrace_mmap__read_snapshot_head(mm);
1202 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1206 head = auxtrace_mmap__read_head(mm);
1212 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1213 mm->idx, old, head, head - old);
1216 head_off = head & mm->mask;
1217 old_off = old & mm->mask;
1219 head_off = head % mm->len;
1220 old_off = old % mm->len;
1223 if (head_off > old_off)
1224 size = head_off - old_off;
1226 size = mm->len - (old_off - head_off);
1228 if (snapshot && size > snapshot_size)
1229 size = snapshot_size;
1231 ref = auxtrace_record__reference(itr);
1233 if (head > old || size <= head || mm->mask) {
1234 offset = head - size;
1237 * When the buffer size is not a power of 2, 'head' wraps at the
1238 * highest multiple of the buffer size, so we have to subtract
1239 * the remainder here.
1241 u64 rem = (0ULL - mm->len) % mm->len;
1243 offset = head - size - rem;
1246 if (size > head_off) {
1247 len1 = size - head_off;
1248 data1 = &data[mm->len - len1];
1253 data1 = &data[head_off - len1];
1258 if (itr->alignment) {
1259 unsigned int unwanted = len1 % itr->alignment;
1265 /* padding must be written by fn() e.g. record__process_auxtrace() */
1268 padding = 8 - padding;
1270 memset(&ev, 0, sizeof(ev));
1271 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1272 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1273 ev.auxtrace.size = size + padding;
1274 ev.auxtrace.offset = offset;
1275 ev.auxtrace.reference = ref;
1276 ev.auxtrace.idx = mm->idx;
1277 ev.auxtrace.tid = mm->tid;
1278 ev.auxtrace.cpu = mm->cpu;
1280 if (fn(tool, &ev, data1, len1, data2, len2))
1286 auxtrace_mmap__write_tail(mm, head);
1287 if (itr->read_finish) {
1290 err = itr->read_finish(itr, mm->idx);
1299 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1300 struct perf_tool *tool, process_auxtrace_t fn)
1302 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1305 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1306 struct auxtrace_record *itr,
1307 struct perf_tool *tool, process_auxtrace_t fn,
1308 size_t snapshot_size)
1310 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1314 * struct auxtrace_cache - hash table to implement a cache
1315 * @hashtable: the hashtable
1316 * @sz: hashtable size (number of hlists)
1317 * @entry_size: size of an entry
1318 * @limit: limit the number of entries to this maximum, when reached the cache
1319 * is dropped and caching begins again with an empty cache
1320 * @cnt: current number of entries
1321 * @bits: hashtable size (@sz = 2^@bits)
1323 struct auxtrace_cache {
1324 struct hlist_head *hashtable;
1332 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1333 unsigned int limit_percent)
1335 struct auxtrace_cache *c;
1336 struct hlist_head *ht;
1339 c = zalloc(sizeof(struct auxtrace_cache));
1345 ht = calloc(sz, sizeof(struct hlist_head));
1349 for (i = 0; i < sz; i++)
1350 INIT_HLIST_HEAD(&ht[i]);
1354 c->entry_size = entry_size;
1355 c->limit = (c->sz * limit_percent) / 100;
1365 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1367 struct auxtrace_cache_entry *entry;
1368 struct hlist_node *tmp;
1374 for (i = 0; i < c->sz; i++) {
1375 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1376 hlist_del(&entry->hash);
1377 auxtrace_cache__free_entry(c, entry);
1384 void auxtrace_cache__free(struct auxtrace_cache *c)
1389 auxtrace_cache__drop(c);
1394 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1396 return malloc(c->entry_size);
1399 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1405 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1406 struct auxtrace_cache_entry *entry)
1408 if (c->limit && ++c->cnt > c->limit)
1409 auxtrace_cache__drop(c);
1412 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1417 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1419 struct auxtrace_cache_entry *entry;
1420 struct hlist_head *hlist;
1425 hlist = &c->hashtable[hash_32(key, c->bits)];
1426 hlist_for_each_entry(entry, hlist, hash) {
1427 if (entry->key == key)
1434 static void addr_filter__free_str(struct addr_filter *filt)
1437 filt->action = NULL;
1438 filt->sym_from = NULL;
1439 filt->sym_to = NULL;
1440 filt->filename = NULL;
1444 static struct addr_filter *addr_filter__new(void)
1446 struct addr_filter *filt = zalloc(sizeof(*filt));
1449 INIT_LIST_HEAD(&filt->list);
1454 static void addr_filter__free(struct addr_filter *filt)
1457 addr_filter__free_str(filt);
1461 static void addr_filters__add(struct addr_filters *filts,
1462 struct addr_filter *filt)
1464 list_add_tail(&filt->list, &filts->head);
1468 static void addr_filters__del(struct addr_filters *filts,
1469 struct addr_filter *filt)
1471 list_del_init(&filt->list);
1475 void addr_filters__init(struct addr_filters *filts)
1477 INIT_LIST_HEAD(&filts->head);
1481 void addr_filters__exit(struct addr_filters *filts)
1483 struct addr_filter *filt, *n;
1485 list_for_each_entry_safe(filt, n, &filts->head, list) {
1486 addr_filters__del(filts, filt);
1487 addr_filter__free(filt);
1491 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1492 const char *str_delim)
1494 *inp += strspn(*inp, " ");
1496 if (isdigit(**inp)) {
1502 *num = strtoull(*inp, &endptr, 0);
1513 *inp += strspn(*inp, " ");
1515 n = strcspn(*inp, str_delim);
1527 static int parse_action(struct addr_filter *filt)
1529 if (!strcmp(filt->action, "filter")) {
1532 } else if (!strcmp(filt->action, "start")) {
1534 } else if (!strcmp(filt->action, "stop")) {
1535 filt->start = false;
1536 } else if (!strcmp(filt->action, "tracestop")) {
1537 filt->start = false;
1539 filt->action += 5; /* Change 'tracestop' to 'stop' */
1546 static int parse_sym_idx(char **inp, int *idx)
1550 *inp += strspn(*inp, " ");
1557 if (**inp == 'g' || **inp == 'G') {
1565 num = strtoul(*inp, &endptr, 0);
1568 if (endptr == *inp || num > INT_MAX)
1577 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
1579 int err = parse_num_or_str(inp, num, str, " ");
1582 err = parse_sym_idx(inp, idx);
1587 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
1592 filt->str = fstr = strdup(*filter_inp);
1596 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
1600 err = parse_action(filt);
1604 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
1605 &filt->sym_from_idx);
1609 fstr += strspn(fstr, " ");
1613 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
1620 fstr += strspn(fstr, " ");
1624 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
1629 fstr += strspn(fstr, " ,");
1631 *filter_inp += fstr - filt->str;
1636 addr_filter__free_str(filt);
1641 int addr_filters__parse_bare_filter(struct addr_filters *filts,
1644 struct addr_filter *filt;
1645 const char *fstr = filter;
1649 filt = addr_filter__new();
1650 err = parse_one_filter(filt, &fstr);
1652 addr_filter__free(filt);
1653 addr_filters__exit(filts);
1656 addr_filters__add(filts, filt);
1675 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
1677 /* A function with the same name, and global or the n'th found or any */
1678 return symbol_type__is_a(type, MAP__FUNCTION) &&
1679 !strcmp(name, args->name) &&
1680 ((args->global && isupper(type)) ||
1681 (args->selected && ++(args->cnt) == args->idx) ||
1682 (!args->global && !args->selected));
1685 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1687 struct sym_args *args = arg;
1689 if (args->started) {
1691 args->size = start - args->start;
1692 if (args->selected) {
1695 } else if (kern_sym_match(args, name, type)) {
1696 args->duplicate = true;
1699 } else if (kern_sym_match(args, name, type)) {
1700 args->started = true;
1701 args->start = start;
1707 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1709 struct sym_args *args = arg;
1711 if (kern_sym_match(args, name, type)) {
1712 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1713 ++args->cnt, start, type, name);
1715 } else if (args->near) {
1717 pr_err("\t\twhich is near\t\t%s\n", name);
1723 static int sym_not_found_error(const char *sym_name, int idx)
1726 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1729 pr_err("Global symbol '%s' not found.\n", sym_name);
1731 pr_err("Symbol '%s' not found.\n", sym_name);
1733 pr_err("Note that symbols must be functions.\n");
1738 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
1740 struct sym_args args = {
1744 .selected = idx > 0,
1751 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
1753 pr_err("Failed to parse /proc/kallsyms\n");
1757 if (args.duplicate) {
1758 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
1760 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
1761 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1763 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1767 if (!args.started) {
1768 pr_err("Kernel symbol lookup: ");
1769 return sym_not_found_error(sym_name, idx);
1772 *start = args.start;
1778 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
1779 char type, u64 start)
1781 struct sym_args *args = arg;
1783 if (!symbol_type__is_a(type, MAP__FUNCTION))
1786 if (!args->started) {
1787 args->started = true;
1788 args->start = start;
1790 /* Don't know exactly where the kernel ends, so we add a page */
1791 args->size = round_up(start, page_size) + page_size - args->start;
1796 static int addr_filter__entire_kernel(struct addr_filter *filt)
1798 struct sym_args args = { .started = false };
1801 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
1802 if (err < 0 || !args.started) {
1803 pr_err("Failed to parse /proc/kallsyms\n");
1807 filt->addr = args.start;
1808 filt->size = args.size;
1813 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
1815 if (start + size >= filt->addr)
1818 if (filt->sym_from) {
1819 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
1820 filt->sym_to, start, filt->sym_from, filt->addr);
1822 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
1823 filt->sym_to, start, filt->addr);
1829 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
1831 bool no_size = false;
1835 if (symbol_conf.kptr_restrict) {
1836 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1840 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
1841 return addr_filter__entire_kernel(filt);
1843 if (filt->sym_from) {
1844 err = find_kern_sym(filt->sym_from, &start, &size,
1845 filt->sym_from_idx);
1849 if (filt->range && !filt->size && !filt->sym_to) {
1856 err = find_kern_sym(filt->sym_to, &start, &size,
1861 err = check_end_after_start(filt, start, size);
1864 filt->size = start + size - filt->addr;
1868 /* The very last symbol in kallsyms does not imply a particular size */
1870 pr_err("Cannot determine size of symbol '%s'\n",
1871 filt->sym_to ? filt->sym_to : filt->sym_from);
1878 static struct dso *load_dso(const char *name)
1883 map = dso__new_map(name);
1889 dso = dso__get(map->dso);
1896 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
1899 /* Same name, and global or the n'th found or any */
1900 return !arch__compare_symbol_names(name, sym->name) &&
1901 ((!idx && sym->binding == STB_GLOBAL) ||
1902 (idx > 0 && ++*cnt == idx) ||
1906 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
1912 pr_err("Multiple symbols with name '%s'\n", sym_name);
1914 sym = dso__first_symbol(dso, MAP__FUNCTION);
1916 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
1917 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1919 sym->binding == STB_GLOBAL ? 'g' :
1920 sym->binding == STB_LOCAL ? 'l' : 'w',
1925 pr_err("\t\twhich is near\t\t%s\n", sym->name);
1927 sym = dso__next_symbol(sym);
1930 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1932 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1935 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
1944 sym = dso__first_symbol(dso, MAP__FUNCTION);
1948 *size = sym->start - *start;
1952 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1953 print_duplicate_syms(dso, sym_name);
1956 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1957 *start = sym->start;
1958 *size = sym->end - sym->start;
1960 sym = dso__next_symbol(sym);
1964 return sym_not_found_error(sym_name, idx);
1969 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
1971 struct symbol *first_sym = dso__first_symbol(dso, MAP__FUNCTION);
1972 struct symbol *last_sym = dso__last_symbol(dso, MAP__FUNCTION);
1974 if (!first_sym || !last_sym) {
1975 pr_err("Failed to determine filter for %s\nNo symbols found.\n",
1980 filt->addr = first_sym->start;
1981 filt->size = last_sym->end - first_sym->start;
1986 static int addr_filter__resolve_syms(struct addr_filter *filt)
1992 if (!filt->sym_from && !filt->sym_to)
1995 if (!filt->filename)
1996 return addr_filter__resolve_kernel_syms(filt);
1998 dso = load_dso(filt->filename);
2000 pr_err("Failed to load symbols from: %s\n", filt->filename);
2004 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2005 err = addr_filter__entire_dso(filt, dso);
2009 if (filt->sym_from) {
2010 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2011 filt->sym_from_idx);
2015 if (filt->range && !filt->size && !filt->sym_to)
2020 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2025 err = check_end_after_start(filt, start, size);
2029 filt->size = start + size - filt->addr;
2038 static char *addr_filter__to_str(struct addr_filter *filt)
2040 char filename_buf[PATH_MAX];
2041 const char *at = "";
2042 const char *fn = "";
2046 if (filt->filename) {
2048 fn = realpath(filt->filename, filename_buf);
2054 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2055 filt->action, filt->addr, filt->size, at, fn);
2057 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2058 filt->action, filt->addr, at, fn);
2061 return err < 0 ? NULL : filter;
2064 static int parse_addr_filter(struct perf_evsel *evsel, const char *filter,
2067 struct addr_filters filts;
2068 struct addr_filter *filt;
2071 addr_filters__init(&filts);
2073 err = addr_filters__parse_bare_filter(&filts, filter);
2077 if (filts.cnt > max_nr) {
2078 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2084 list_for_each_entry(filt, &filts.head, list) {
2087 err = addr_filter__resolve_syms(filt);
2091 new_filter = addr_filter__to_str(filt);
2097 if (perf_evsel__append_addr_filter(evsel, new_filter)) {
2104 addr_filters__exit(&filts);
2107 pr_err("Failed to parse address filter: '%s'\n", filter);
2108 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2109 pr_err("Where multiple filters are separated by space or comma.\n");
2115 static struct perf_pmu *perf_evsel__find_pmu(struct perf_evsel *evsel)
2117 struct perf_pmu *pmu = NULL;
2119 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2120 if (pmu->type == evsel->attr.type)
2127 static int perf_evsel__nr_addr_filter(struct perf_evsel *evsel)
2129 struct perf_pmu *pmu = perf_evsel__find_pmu(evsel);
2130 int nr_addr_filters = 0;
2135 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2137 return nr_addr_filters;
2140 int auxtrace_parse_filters(struct perf_evlist *evlist)
2142 struct perf_evsel *evsel;
2146 evlist__for_each_entry(evlist, evsel) {
2147 filter = evsel->filter;
2148 max_nr = perf_evsel__nr_addr_filter(evsel);
2149 if (!filter || !max_nr)
2151 evsel->filter = NULL;
2152 err = parse_addr_filter(evsel, filter, max_nr);
2156 pr_debug("Address filter: %s\n", evsel->filter);