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>
37 #include <linux/list.h>
47 #include "thread_map.h"
51 #include <linux/hash.h>
56 #include <subcmd/parse-options.h>
59 #include "intel-bts.h"
61 #include "sane_ctype.h"
62 #include "symbol/kallsyms.h"
64 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
65 struct auxtrace_mmap_params *mp,
68 struct perf_event_mmap_page *pc = userpg;
70 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
85 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
86 pr_err("Cannot use AUX area tracing mmaps\n");
90 pc->aux_offset = mp->offset;
91 pc->aux_size = mp->len;
93 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
94 if (mm->base == MAP_FAILED) {
95 pr_debug2("failed to mmap AUX area\n");
103 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
106 munmap(mm->base, mm->len);
111 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
112 off_t auxtrace_offset,
113 unsigned int auxtrace_pages,
114 bool auxtrace_overwrite)
116 if (auxtrace_pages) {
117 mp->offset = auxtrace_offset;
118 mp->len = auxtrace_pages * (size_t)page_size;
119 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
120 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
121 pr_debug2("AUX area mmap length %zu\n", mp->len);
127 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
128 struct perf_evlist *evlist, int idx,
134 mp->cpu = evlist->cpus->map[idx];
136 mp->tid = thread_map__pid(evlist->threads, 0);
141 mp->tid = thread_map__pid(evlist->threads, idx);
145 #define AUXTRACE_INIT_NR_QUEUES 32
147 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
149 struct auxtrace_queue *queue_array;
150 unsigned int max_nr_queues, i;
152 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
153 if (nr_queues > max_nr_queues)
156 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
160 for (i = 0; i < nr_queues; i++) {
161 INIT_LIST_HEAD(&queue_array[i].head);
162 queue_array[i].priv = NULL;
168 int auxtrace_queues__init(struct auxtrace_queues *queues)
170 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
171 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
172 if (!queues->queue_array)
177 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
178 unsigned int new_nr_queues)
180 unsigned int nr_queues = queues->nr_queues;
181 struct auxtrace_queue *queue_array;
185 nr_queues = AUXTRACE_INIT_NR_QUEUES;
187 while (nr_queues && nr_queues < new_nr_queues)
190 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
193 queue_array = auxtrace_alloc_queue_array(nr_queues);
197 for (i = 0; i < queues->nr_queues; i++) {
198 list_splice_tail(&queues->queue_array[i].head,
199 &queue_array[i].head);
200 queue_array[i].tid = queues->queue_array[i].tid;
201 queue_array[i].cpu = queues->queue_array[i].cpu;
202 queue_array[i].set = queues->queue_array[i].set;
203 queue_array[i].priv = queues->queue_array[i].priv;
206 queues->nr_queues = nr_queues;
207 queues->queue_array = queue_array;
212 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
214 int fd = perf_data_file__fd(session->file);
218 if (size > SSIZE_MAX)
225 ret = readn(fd, p, size);
226 if (ret != (ssize_t)size) {
234 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
236 struct auxtrace_buffer *buffer)
238 struct auxtrace_queue *queue;
241 if (idx >= queues->nr_queues) {
242 err = auxtrace_queues__grow(queues, idx + 1);
247 queue = &queues->queue_array[idx];
251 queue->tid = buffer->tid;
252 queue->cpu = buffer->cpu;
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 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_CS_ETM:
916 case PERF_AUXTRACE_UNKNOWN:
922 s64 perf_event__process_auxtrace(struct perf_tool *tool,
923 union perf_event *event,
924 struct perf_session *session)
929 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
930 event->auxtrace.size, event->auxtrace.offset,
931 event->auxtrace.reference, event->auxtrace.idx,
932 event->auxtrace.tid, event->auxtrace.cpu);
934 if (auxtrace__dont_decode(session))
935 return event->auxtrace.size;
937 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
940 err = session->auxtrace->process_auxtrace_event(session, event, tool);
944 return event->auxtrace.size;
947 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
948 #define PERF_ITRACE_DEFAULT_PERIOD 100000
949 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
950 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
951 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
952 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
954 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
956 synth_opts->instructions = true;
957 synth_opts->branches = true;
958 synth_opts->transactions = true;
959 synth_opts->ptwrites = true;
960 synth_opts->pwr_events = true;
961 synth_opts->errors = true;
962 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
963 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
964 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
965 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
966 synth_opts->initial_skip = 0;
970 * Please check tools/perf/Documentation/perf-script.txt for information
971 * about the options parsed here, which is introduced after this cset,
972 * when support in 'perf script' for these options is introduced.
974 int itrace_parse_synth_opts(const struct option *opt, const char *str,
977 struct itrace_synth_opts *synth_opts = opt->value;
980 bool period_type_set = false;
981 bool period_set = false;
983 synth_opts->set = true;
986 synth_opts->dont_decode = true;
991 itrace_synth_opts__set_default(synth_opts);
998 synth_opts->instructions = true;
999 while (*p == ' ' || *p == ',')
1002 synth_opts->period = strtoull(p, &endptr, 10);
1005 while (*p == ' ' || *p == ',')
1009 synth_opts->period_type =
1010 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1011 period_type_set = true;
1014 synth_opts->period_type =
1015 PERF_ITRACE_PERIOD_TICKS;
1016 period_type_set = true;
1019 synth_opts->period *= 1000;
1022 synth_opts->period *= 1000;
1027 synth_opts->period_type =
1028 PERF_ITRACE_PERIOD_NANOSECS;
1029 period_type_set = true;
1039 synth_opts->branches = true;
1042 synth_opts->transactions = true;
1045 synth_opts->ptwrites = true;
1048 synth_opts->pwr_events = true;
1051 synth_opts->errors = true;
1054 synth_opts->log = true;
1057 synth_opts->branches = true;
1058 synth_opts->calls = true;
1061 synth_opts->branches = true;
1062 synth_opts->returns = true;
1065 synth_opts->callchain = true;
1066 synth_opts->callchain_sz =
1067 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1068 while (*p == ' ' || *p == ',')
1073 val = strtoul(p, &endptr, 10);
1075 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1077 synth_opts->callchain_sz = val;
1081 synth_opts->last_branch = true;
1082 synth_opts->last_branch_sz =
1083 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1084 while (*p == ' ' || *p == ',')
1089 val = strtoul(p, &endptr, 10);
1092 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1094 synth_opts->last_branch_sz = val;
1098 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1111 if (synth_opts->instructions) {
1112 if (!period_type_set)
1113 synth_opts->period_type =
1114 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1116 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1122 pr_err("Bad Instruction Tracing options '%s'\n", str);
1126 static const char * const auxtrace_error_type_name[] = {
1127 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1130 static const char *auxtrace_error_name(int type)
1132 const char *error_type_name = NULL;
1134 if (type < PERF_AUXTRACE_ERROR_MAX)
1135 error_type_name = auxtrace_error_type_name[type];
1136 if (!error_type_name)
1137 error_type_name = "unknown AUX";
1138 return error_type_name;
1141 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1143 struct auxtrace_error_event *e = &event->auxtrace_error;
1146 ret = fprintf(fp, " %s error type %u",
1147 auxtrace_error_name(e->type), e->type);
1148 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1149 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1153 void perf_session__auxtrace_error_inc(struct perf_session *session,
1154 union perf_event *event)
1156 struct auxtrace_error_event *e = &event->auxtrace_error;
1158 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1159 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1162 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1166 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1167 if (!stats->nr_auxtrace_errors[i])
1169 ui__warning("%u %s errors\n",
1170 stats->nr_auxtrace_errors[i],
1171 auxtrace_error_name(i));
1175 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1176 union perf_event *event,
1177 struct perf_session *session)
1179 if (auxtrace__dont_decode(session))
1182 perf_event__fprintf_auxtrace_error(event, stdout);
1186 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1187 struct auxtrace_record *itr,
1188 struct perf_tool *tool, process_auxtrace_t fn,
1189 bool snapshot, size_t snapshot_size)
1191 u64 head, old = mm->prev, offset, ref;
1192 unsigned char *data = mm->base;
1193 size_t size, head_off, old_off, len1, len2, padding;
1194 union perf_event ev;
1195 void *data1, *data2;
1198 head = auxtrace_mmap__read_snapshot_head(mm);
1199 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1203 head = auxtrace_mmap__read_head(mm);
1209 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1210 mm->idx, old, head, head - old);
1213 head_off = head & mm->mask;
1214 old_off = old & mm->mask;
1216 head_off = head % mm->len;
1217 old_off = old % mm->len;
1220 if (head_off > old_off)
1221 size = head_off - old_off;
1223 size = mm->len - (old_off - head_off);
1225 if (snapshot && size > snapshot_size)
1226 size = snapshot_size;
1228 ref = auxtrace_record__reference(itr);
1230 if (head > old || size <= head || mm->mask) {
1231 offset = head - size;
1234 * When the buffer size is not a power of 2, 'head' wraps at the
1235 * highest multiple of the buffer size, so we have to subtract
1236 * the remainder here.
1238 u64 rem = (0ULL - mm->len) % mm->len;
1240 offset = head - size - rem;
1243 if (size > head_off) {
1244 len1 = size - head_off;
1245 data1 = &data[mm->len - len1];
1250 data1 = &data[head_off - len1];
1255 if (itr->alignment) {
1256 unsigned int unwanted = len1 % itr->alignment;
1262 /* padding must be written by fn() e.g. record__process_auxtrace() */
1263 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1265 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1267 memset(&ev, 0, sizeof(ev));
1268 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1269 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1270 ev.auxtrace.size = size + padding;
1271 ev.auxtrace.offset = offset;
1272 ev.auxtrace.reference = ref;
1273 ev.auxtrace.idx = mm->idx;
1274 ev.auxtrace.tid = mm->tid;
1275 ev.auxtrace.cpu = mm->cpu;
1277 if (fn(tool, &ev, data1, len1, data2, len2))
1283 auxtrace_mmap__write_tail(mm, head);
1284 if (itr->read_finish) {
1287 err = itr->read_finish(itr, mm->idx);
1296 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1297 struct perf_tool *tool, process_auxtrace_t fn)
1299 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1302 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1303 struct auxtrace_record *itr,
1304 struct perf_tool *tool, process_auxtrace_t fn,
1305 size_t snapshot_size)
1307 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1311 * struct auxtrace_cache - hash table to implement a cache
1312 * @hashtable: the hashtable
1313 * @sz: hashtable size (number of hlists)
1314 * @entry_size: size of an entry
1315 * @limit: limit the number of entries to this maximum, when reached the cache
1316 * is dropped and caching begins again with an empty cache
1317 * @cnt: current number of entries
1318 * @bits: hashtable size (@sz = 2^@bits)
1320 struct auxtrace_cache {
1321 struct hlist_head *hashtable;
1329 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1330 unsigned int limit_percent)
1332 struct auxtrace_cache *c;
1333 struct hlist_head *ht;
1336 c = zalloc(sizeof(struct auxtrace_cache));
1342 ht = calloc(sz, sizeof(struct hlist_head));
1346 for (i = 0; i < sz; i++)
1347 INIT_HLIST_HEAD(&ht[i]);
1351 c->entry_size = entry_size;
1352 c->limit = (c->sz * limit_percent) / 100;
1362 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1364 struct auxtrace_cache_entry *entry;
1365 struct hlist_node *tmp;
1371 for (i = 0; i < c->sz; i++) {
1372 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1373 hlist_del(&entry->hash);
1374 auxtrace_cache__free_entry(c, entry);
1381 void auxtrace_cache__free(struct auxtrace_cache *c)
1386 auxtrace_cache__drop(c);
1391 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1393 return malloc(c->entry_size);
1396 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1402 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1403 struct auxtrace_cache_entry *entry)
1405 if (c->limit && ++c->cnt > c->limit)
1406 auxtrace_cache__drop(c);
1409 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1414 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1416 struct auxtrace_cache_entry *entry;
1417 struct hlist_head *hlist;
1422 hlist = &c->hashtable[hash_32(key, c->bits)];
1423 hlist_for_each_entry(entry, hlist, hash) {
1424 if (entry->key == key)
1431 static void addr_filter__free_str(struct addr_filter *filt)
1434 filt->action = NULL;
1435 filt->sym_from = NULL;
1436 filt->sym_to = NULL;
1437 filt->filename = NULL;
1441 static struct addr_filter *addr_filter__new(void)
1443 struct addr_filter *filt = zalloc(sizeof(*filt));
1446 INIT_LIST_HEAD(&filt->list);
1451 static void addr_filter__free(struct addr_filter *filt)
1454 addr_filter__free_str(filt);
1458 static void addr_filters__add(struct addr_filters *filts,
1459 struct addr_filter *filt)
1461 list_add_tail(&filt->list, &filts->head);
1465 static void addr_filters__del(struct addr_filters *filts,
1466 struct addr_filter *filt)
1468 list_del_init(&filt->list);
1472 void addr_filters__init(struct addr_filters *filts)
1474 INIT_LIST_HEAD(&filts->head);
1478 void addr_filters__exit(struct addr_filters *filts)
1480 struct addr_filter *filt, *n;
1482 list_for_each_entry_safe(filt, n, &filts->head, list) {
1483 addr_filters__del(filts, filt);
1484 addr_filter__free(filt);
1488 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1489 const char *str_delim)
1491 *inp += strspn(*inp, " ");
1493 if (isdigit(**inp)) {
1499 *num = strtoull(*inp, &endptr, 0);
1510 *inp += strspn(*inp, " ");
1512 n = strcspn(*inp, str_delim);
1524 static int parse_action(struct addr_filter *filt)
1526 if (!strcmp(filt->action, "filter")) {
1529 } else if (!strcmp(filt->action, "start")) {
1531 } else if (!strcmp(filt->action, "stop")) {
1532 filt->start = false;
1533 } else if (!strcmp(filt->action, "tracestop")) {
1534 filt->start = false;
1536 filt->action += 5; /* Change 'tracestop' to 'stop' */
1543 static int parse_sym_idx(char **inp, int *idx)
1547 *inp += strspn(*inp, " ");
1554 if (**inp == 'g' || **inp == 'G') {
1562 num = strtoul(*inp, &endptr, 0);
1565 if (endptr == *inp || num > INT_MAX)
1574 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
1576 int err = parse_num_or_str(inp, num, str, " ");
1579 err = parse_sym_idx(inp, idx);
1584 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
1589 filt->str = fstr = strdup(*filter_inp);
1593 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
1597 err = parse_action(filt);
1601 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
1602 &filt->sym_from_idx);
1606 fstr += strspn(fstr, " ");
1610 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
1617 fstr += strspn(fstr, " ");
1621 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
1626 fstr += strspn(fstr, " ,");
1628 *filter_inp += fstr - filt->str;
1633 addr_filter__free_str(filt);
1638 int addr_filters__parse_bare_filter(struct addr_filters *filts,
1641 struct addr_filter *filt;
1642 const char *fstr = filter;
1646 filt = addr_filter__new();
1647 err = parse_one_filter(filt, &fstr);
1649 addr_filter__free(filt);
1650 addr_filters__exit(filts);
1653 addr_filters__add(filts, filt);
1672 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
1674 /* A function with the same name, and global or the n'th found or any */
1675 return symbol_type__is_a(type, MAP__FUNCTION) &&
1676 !strcmp(name, args->name) &&
1677 ((args->global && isupper(type)) ||
1678 (args->selected && ++(args->cnt) == args->idx) ||
1679 (!args->global && !args->selected));
1682 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1684 struct sym_args *args = arg;
1686 if (args->started) {
1688 args->size = start - args->start;
1689 if (args->selected) {
1692 } else if (kern_sym_match(args, name, type)) {
1693 args->duplicate = true;
1696 } else if (kern_sym_match(args, name, type)) {
1697 args->started = true;
1698 args->start = start;
1704 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1706 struct sym_args *args = arg;
1708 if (kern_sym_match(args, name, type)) {
1709 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1710 ++args->cnt, start, type, name);
1712 } else if (args->near) {
1714 pr_err("\t\twhich is near\t\t%s\n", name);
1720 static int sym_not_found_error(const char *sym_name, int idx)
1723 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1726 pr_err("Global symbol '%s' not found.\n", sym_name);
1728 pr_err("Symbol '%s' not found.\n", sym_name);
1730 pr_err("Note that symbols must be functions.\n");
1735 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
1737 struct sym_args args = {
1741 .selected = idx > 0,
1748 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
1750 pr_err("Failed to parse /proc/kallsyms\n");
1754 if (args.duplicate) {
1755 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
1757 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
1758 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1760 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1764 if (!args.started) {
1765 pr_err("Kernel symbol lookup: ");
1766 return sym_not_found_error(sym_name, idx);
1769 *start = args.start;
1775 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
1776 char type, u64 start)
1778 struct sym_args *args = arg;
1781 if (!symbol_type__is_a(type, MAP__FUNCTION))
1784 if (!args->started) {
1785 args->started = true;
1786 args->start = start;
1788 /* Don't know exactly where the kernel ends, so we add a page */
1789 size = round_up(start, page_size) + page_size - args->start;
1790 if (size > args->size)
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);