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
16 #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"
49 #include "symbol/kallsyms.h"
52 #include <linux/hash.h>
57 #include <subcmd/parse-options.h>
60 #include "intel-bts.h"
62 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
63 struct auxtrace_mmap_params *mp,
66 struct perf_event_mmap_page *pc = userpg;
68 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
83 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
84 pr_err("Cannot use AUX area tracing mmaps\n");
88 pc->aux_offset = mp->offset;
89 pc->aux_size = mp->len;
91 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
92 if (mm->base == MAP_FAILED) {
93 pr_debug2("failed to mmap AUX area\n");
101 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
104 munmap(mm->base, mm->len);
109 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
110 off_t auxtrace_offset,
111 unsigned int auxtrace_pages,
112 bool auxtrace_overwrite)
114 if (auxtrace_pages) {
115 mp->offset = auxtrace_offset;
116 mp->len = auxtrace_pages * (size_t)page_size;
117 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
118 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
119 pr_debug2("AUX area mmap length %zu\n", mp->len);
125 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
126 struct perf_evlist *evlist, int idx,
132 mp->cpu = evlist->cpus->map[idx];
134 mp->tid = thread_map__pid(evlist->threads, 0);
139 mp->tid = thread_map__pid(evlist->threads, idx);
143 #define AUXTRACE_INIT_NR_QUEUES 32
145 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
147 struct auxtrace_queue *queue_array;
148 unsigned int max_nr_queues, i;
150 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
151 if (nr_queues > max_nr_queues)
154 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
158 for (i = 0; i < nr_queues; i++) {
159 INIT_LIST_HEAD(&queue_array[i].head);
160 queue_array[i].priv = NULL;
166 int auxtrace_queues__init(struct auxtrace_queues *queues)
168 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
169 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
170 if (!queues->queue_array)
175 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
176 unsigned int new_nr_queues)
178 unsigned int nr_queues = queues->nr_queues;
179 struct auxtrace_queue *queue_array;
183 nr_queues = AUXTRACE_INIT_NR_QUEUES;
185 while (nr_queues && nr_queues < new_nr_queues)
188 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
191 queue_array = auxtrace_alloc_queue_array(nr_queues);
195 for (i = 0; i < queues->nr_queues; i++) {
196 list_splice_tail(&queues->queue_array[i].head,
197 &queue_array[i].head);
198 queue_array[i].tid = queues->queue_array[i].tid;
199 queue_array[i].cpu = queues->queue_array[i].cpu;
200 queue_array[i].set = queues->queue_array[i].set;
201 queue_array[i].priv = queues->queue_array[i].priv;
204 queues->nr_queues = nr_queues;
205 queues->queue_array = queue_array;
210 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
212 int fd = perf_data_file__fd(session->file);
216 if (size > SSIZE_MAX)
223 ret = readn(fd, p, size);
224 if (ret != (ssize_t)size) {
232 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
234 struct auxtrace_buffer *buffer)
236 struct auxtrace_queue *queue;
239 if (idx >= queues->nr_queues) {
240 err = auxtrace_queues__grow(queues, idx + 1);
245 queue = &queues->queue_array[idx];
249 queue->tid = buffer->tid;
250 queue->cpu = buffer->cpu;
253 buffer->buffer_nr = queues->next_buffer_nr++;
255 list_add_tail(&buffer->list, &queue->head);
257 queues->new_data = true;
258 queues->populated = true;
263 /* Limit buffers to 32MiB on 32-bit */
264 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
266 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
268 struct auxtrace_buffer *buffer)
270 u64 sz = buffer->size;
271 bool consecutive = false;
272 struct auxtrace_buffer *b;
275 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
276 b = memdup(buffer, sizeof(struct auxtrace_buffer));
279 b->size = BUFFER_LIMIT_FOR_32_BIT;
280 b->consecutive = consecutive;
281 err = auxtrace_queues__add_buffer(queues, idx, b);
283 auxtrace_buffer__free(b);
286 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
287 sz -= BUFFER_LIMIT_FOR_32_BIT;
292 buffer->consecutive = consecutive;
297 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
298 struct perf_session *session,
300 struct auxtrace_buffer *buffer)
302 if (session->one_mmap) {
303 buffer->data = buffer->data_offset - session->one_mmap_offset +
304 session->one_mmap_addr;
305 } else if (perf_data_file__is_pipe(session->file)) {
306 buffer->data = auxtrace_copy_data(buffer->size, session);
309 buffer->data_needs_freeing = true;
310 } else if (BITS_PER_LONG == 32 &&
311 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
314 err = auxtrace_queues__split_buffer(queues, idx, buffer);
319 return auxtrace_queues__add_buffer(queues, idx, buffer);
322 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
323 struct perf_session *session,
324 union perf_event *event, off_t data_offset,
325 struct auxtrace_buffer **buffer_ptr)
327 struct auxtrace_buffer *buffer;
331 buffer = zalloc(sizeof(struct auxtrace_buffer));
336 buffer->tid = event->auxtrace.tid;
337 buffer->cpu = event->auxtrace.cpu;
338 buffer->data_offset = data_offset;
339 buffer->offset = event->auxtrace.offset;
340 buffer->reference = event->auxtrace.reference;
341 buffer->size = event->auxtrace.size;
342 idx = event->auxtrace.idx;
344 err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
349 *buffer_ptr = buffer;
354 auxtrace_buffer__free(buffer);
358 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
359 struct perf_session *session,
360 off_t file_offset, size_t sz)
362 union perf_event *event;
364 char buf[PERF_SAMPLE_MAX_SIZE];
366 err = perf_session__peek_event(session, file_offset, buf,
367 PERF_SAMPLE_MAX_SIZE, &event, NULL);
371 if (event->header.type == PERF_RECORD_AUXTRACE) {
372 if (event->header.size < sizeof(struct auxtrace_event) ||
373 event->header.size != sz) {
377 file_offset += event->header.size;
378 err = auxtrace_queues__add_event(queues, session, event,
385 void auxtrace_queues__free(struct auxtrace_queues *queues)
389 for (i = 0; i < queues->nr_queues; i++) {
390 while (!list_empty(&queues->queue_array[i].head)) {
391 struct auxtrace_buffer *buffer;
393 buffer = list_entry(queues->queue_array[i].head.next,
394 struct auxtrace_buffer, list);
395 list_del(&buffer->list);
396 auxtrace_buffer__free(buffer);
400 zfree(&queues->queue_array);
401 queues->nr_queues = 0;
404 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
405 unsigned int pos, unsigned int queue_nr,
411 parent = (pos - 1) >> 1;
412 if (heap_array[parent].ordinal <= ordinal)
414 heap_array[pos] = heap_array[parent];
417 heap_array[pos].queue_nr = queue_nr;
418 heap_array[pos].ordinal = ordinal;
421 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
424 struct auxtrace_heap_item *heap_array;
426 if (queue_nr >= heap->heap_sz) {
427 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
429 while (heap_sz <= queue_nr)
431 heap_array = realloc(heap->heap_array,
432 heap_sz * sizeof(struct auxtrace_heap_item));
435 heap->heap_array = heap_array;
436 heap->heap_sz = heap_sz;
439 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
444 void auxtrace_heap__free(struct auxtrace_heap *heap)
446 zfree(&heap->heap_array);
451 void auxtrace_heap__pop(struct auxtrace_heap *heap)
453 unsigned int pos, last, heap_cnt = heap->heap_cnt;
454 struct auxtrace_heap_item *heap_array;
461 heap_array = heap->heap_array;
465 unsigned int left, right;
467 left = (pos << 1) + 1;
468 if (left >= heap_cnt)
471 if (right >= heap_cnt) {
472 heap_array[pos] = heap_array[left];
475 if (heap_array[left].ordinal < heap_array[right].ordinal) {
476 heap_array[pos] = heap_array[left];
479 heap_array[pos] = heap_array[right];
485 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
486 heap_array[last].ordinal);
489 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
490 struct perf_evlist *evlist)
493 return itr->info_priv_size(itr, evlist);
497 static int auxtrace_not_supported(void)
499 pr_err("AUX area tracing is not supported on this architecture\n");
503 int auxtrace_record__info_fill(struct auxtrace_record *itr,
504 struct perf_session *session,
505 struct auxtrace_info_event *auxtrace_info,
509 return itr->info_fill(itr, session, auxtrace_info, priv_size);
510 return auxtrace_not_supported();
513 void auxtrace_record__free(struct auxtrace_record *itr)
519 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
521 if (itr && itr->snapshot_start)
522 return itr->snapshot_start(itr);
526 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
528 if (itr && itr->snapshot_finish)
529 return itr->snapshot_finish(itr);
533 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
534 struct auxtrace_mmap *mm,
535 unsigned char *data, u64 *head, u64 *old)
537 if (itr && itr->find_snapshot)
538 return itr->find_snapshot(itr, idx, mm, data, head, old);
542 int auxtrace_record__options(struct auxtrace_record *itr,
543 struct perf_evlist *evlist,
544 struct record_opts *opts)
547 return itr->recording_options(itr, evlist, opts);
551 u64 auxtrace_record__reference(struct auxtrace_record *itr)
554 return itr->reference(itr);
558 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
559 struct record_opts *opts, const char *str)
565 return itr->parse_snapshot_options(itr, opts, str);
567 pr_err("No AUX area tracing to snapshot\n");
571 struct auxtrace_record *__weak
572 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
578 static int auxtrace_index__alloc(struct list_head *head)
580 struct auxtrace_index *auxtrace_index;
582 auxtrace_index = malloc(sizeof(struct auxtrace_index));
586 auxtrace_index->nr = 0;
587 INIT_LIST_HEAD(&auxtrace_index->list);
589 list_add_tail(&auxtrace_index->list, head);
594 void auxtrace_index__free(struct list_head *head)
596 struct auxtrace_index *auxtrace_index, *n;
598 list_for_each_entry_safe(auxtrace_index, n, head, list) {
599 list_del(&auxtrace_index->list);
600 free(auxtrace_index);
604 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
606 struct auxtrace_index *auxtrace_index;
609 if (list_empty(head)) {
610 err = auxtrace_index__alloc(head);
615 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
617 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
618 err = auxtrace_index__alloc(head);
621 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
625 return auxtrace_index;
628 int auxtrace_index__auxtrace_event(struct list_head *head,
629 union perf_event *event, off_t file_offset)
631 struct auxtrace_index *auxtrace_index;
634 auxtrace_index = auxtrace_index__last(head);
638 nr = auxtrace_index->nr;
639 auxtrace_index->entries[nr].file_offset = file_offset;
640 auxtrace_index->entries[nr].sz = event->header.size;
641 auxtrace_index->nr += 1;
646 static int auxtrace_index__do_write(int fd,
647 struct auxtrace_index *auxtrace_index)
649 struct auxtrace_index_entry ent;
652 for (i = 0; i < auxtrace_index->nr; i++) {
653 ent.file_offset = auxtrace_index->entries[i].file_offset;
654 ent.sz = auxtrace_index->entries[i].sz;
655 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
661 int auxtrace_index__write(int fd, struct list_head *head)
663 struct auxtrace_index *auxtrace_index;
667 list_for_each_entry(auxtrace_index, head, list)
668 total += auxtrace_index->nr;
670 if (writen(fd, &total, sizeof(total)) != sizeof(total))
673 list_for_each_entry(auxtrace_index, head, list) {
674 err = auxtrace_index__do_write(fd, auxtrace_index);
682 static int auxtrace_index__process_entry(int fd, struct list_head *head,
685 struct auxtrace_index *auxtrace_index;
686 struct auxtrace_index_entry ent;
689 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
692 auxtrace_index = auxtrace_index__last(head);
696 nr = auxtrace_index->nr;
698 auxtrace_index->entries[nr].file_offset =
699 bswap_64(ent.file_offset);
700 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
702 auxtrace_index->entries[nr].file_offset = ent.file_offset;
703 auxtrace_index->entries[nr].sz = ent.sz;
706 auxtrace_index->nr = nr + 1;
711 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
714 struct list_head *head = &session->auxtrace_index;
717 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
723 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
729 err = auxtrace_index__process_entry(fd, head, needs_swap);
737 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
738 struct perf_session *session,
739 struct auxtrace_index_entry *ent)
741 return auxtrace_queues__add_indexed_event(queues, session,
742 ent->file_offset, ent->sz);
745 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
746 struct perf_session *session)
748 struct auxtrace_index *auxtrace_index;
749 struct auxtrace_index_entry *ent;
753 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
754 for (i = 0; i < auxtrace_index->nr; i++) {
755 ent = &auxtrace_index->entries[i];
756 err = auxtrace_queues__process_index_entry(queues,
766 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
767 struct auxtrace_buffer *buffer)
770 if (list_is_last(&buffer->list, &queue->head))
772 return list_entry(buffer->list.next, struct auxtrace_buffer,
775 if (list_empty(&queue->head))
777 return list_entry(queue->head.next, struct auxtrace_buffer,
782 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
784 size_t adj = buffer->data_offset & (page_size - 1);
785 size_t size = buffer->size + adj;
786 off_t file_offset = buffer->data_offset - adj;
792 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
793 if (addr == MAP_FAILED)
796 buffer->mmap_addr = addr;
797 buffer->mmap_size = size;
799 buffer->data = addr + adj;
804 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
806 if (!buffer->data || !buffer->mmap_addr)
808 munmap(buffer->mmap_addr, buffer->mmap_size);
809 buffer->mmap_addr = NULL;
810 buffer->mmap_size = 0;
812 buffer->use_data = NULL;
815 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
817 auxtrace_buffer__put_data(buffer);
818 if (buffer->data_needs_freeing) {
819 buffer->data_needs_freeing = false;
820 zfree(&buffer->data);
821 buffer->use_data = NULL;
826 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
828 auxtrace_buffer__drop_data(buffer);
832 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
833 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
838 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
840 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
841 auxtrace_error->type = type;
842 auxtrace_error->code = code;
843 auxtrace_error->cpu = cpu;
844 auxtrace_error->pid = pid;
845 auxtrace_error->tid = tid;
846 auxtrace_error->ip = ip;
847 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
849 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
850 strlen(auxtrace_error->msg) + 1;
851 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
854 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
855 struct perf_tool *tool,
856 struct perf_session *session,
857 perf_event__handler_t process)
859 union perf_event *ev;
863 pr_debug2("Synthesizing auxtrace information\n");
864 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
865 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
869 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
870 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
872 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
877 err = process(tool, ev, NULL, NULL);
883 static bool auxtrace__dont_decode(struct perf_session *session)
885 return !session->itrace_synth_opts ||
886 session->itrace_synth_opts->dont_decode;
889 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
890 union perf_event *event,
891 struct perf_session *session)
893 enum auxtrace_type type = event->auxtrace_info.type;
896 fprintf(stdout, " type: %u\n", type);
899 case PERF_AUXTRACE_INTEL_PT:
900 return intel_pt_process_auxtrace_info(event, session);
901 case PERF_AUXTRACE_INTEL_BTS:
902 return intel_bts_process_auxtrace_info(event, session);
903 case PERF_AUXTRACE_CS_ETM:
904 case PERF_AUXTRACE_UNKNOWN:
910 s64 perf_event__process_auxtrace(struct perf_tool *tool,
911 union perf_event *event,
912 struct perf_session *session)
917 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
918 event->auxtrace.size, event->auxtrace.offset,
919 event->auxtrace.reference, event->auxtrace.idx,
920 event->auxtrace.tid, event->auxtrace.cpu);
922 if (auxtrace__dont_decode(session))
923 return event->auxtrace.size;
925 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
928 err = session->auxtrace->process_auxtrace_event(session, event, tool);
932 return event->auxtrace.size;
935 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
936 #define PERF_ITRACE_DEFAULT_PERIOD 100000
937 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
938 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
939 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
940 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
942 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
944 synth_opts->instructions = true;
945 synth_opts->branches = true;
946 synth_opts->transactions = true;
947 synth_opts->errors = true;
948 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
949 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
950 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
951 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
952 synth_opts->initial_skip = 0;
956 * Please check tools/perf/Documentation/perf-script.txt for information
957 * about the options parsed here, which is introduced after this cset,
958 * when support in 'perf script' for these options is introduced.
960 int itrace_parse_synth_opts(const struct option *opt, const char *str,
963 struct itrace_synth_opts *synth_opts = opt->value;
966 bool period_type_set = false;
967 bool period_set = false;
969 synth_opts->set = true;
972 synth_opts->dont_decode = true;
977 itrace_synth_opts__set_default(synth_opts);
984 synth_opts->instructions = true;
985 while (*p == ' ' || *p == ',')
988 synth_opts->period = strtoull(p, &endptr, 10);
991 while (*p == ' ' || *p == ',')
995 synth_opts->period_type =
996 PERF_ITRACE_PERIOD_INSTRUCTIONS;
997 period_type_set = true;
1000 synth_opts->period_type =
1001 PERF_ITRACE_PERIOD_TICKS;
1002 period_type_set = true;
1005 synth_opts->period *= 1000;
1008 synth_opts->period *= 1000;
1013 synth_opts->period_type =
1014 PERF_ITRACE_PERIOD_NANOSECS;
1015 period_type_set = true;
1025 synth_opts->branches = true;
1028 synth_opts->transactions = true;
1031 synth_opts->errors = true;
1034 synth_opts->log = true;
1037 synth_opts->branches = true;
1038 synth_opts->calls = true;
1041 synth_opts->branches = true;
1042 synth_opts->returns = true;
1045 synth_opts->callchain = true;
1046 synth_opts->callchain_sz =
1047 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1048 while (*p == ' ' || *p == ',')
1053 val = strtoul(p, &endptr, 10);
1055 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1057 synth_opts->callchain_sz = val;
1061 synth_opts->last_branch = true;
1062 synth_opts->last_branch_sz =
1063 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1064 while (*p == ' ' || *p == ',')
1069 val = strtoul(p, &endptr, 10);
1072 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1074 synth_opts->last_branch_sz = val;
1078 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1091 if (synth_opts->instructions) {
1092 if (!period_type_set)
1093 synth_opts->period_type =
1094 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1096 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1102 pr_err("Bad Instruction Tracing options '%s'\n", str);
1106 static const char * const auxtrace_error_type_name[] = {
1107 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1110 static const char *auxtrace_error_name(int type)
1112 const char *error_type_name = NULL;
1114 if (type < PERF_AUXTRACE_ERROR_MAX)
1115 error_type_name = auxtrace_error_type_name[type];
1116 if (!error_type_name)
1117 error_type_name = "unknown AUX";
1118 return error_type_name;
1121 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1123 struct auxtrace_error_event *e = &event->auxtrace_error;
1126 ret = fprintf(fp, " %s error type %u",
1127 auxtrace_error_name(e->type), e->type);
1128 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1129 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1133 void perf_session__auxtrace_error_inc(struct perf_session *session,
1134 union perf_event *event)
1136 struct auxtrace_error_event *e = &event->auxtrace_error;
1138 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1139 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1142 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1146 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1147 if (!stats->nr_auxtrace_errors[i])
1149 ui__warning("%u %s errors\n",
1150 stats->nr_auxtrace_errors[i],
1151 auxtrace_error_name(i));
1155 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1156 union perf_event *event,
1157 struct perf_session *session)
1159 if (auxtrace__dont_decode(session))
1162 perf_event__fprintf_auxtrace_error(event, stdout);
1166 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1167 struct auxtrace_record *itr,
1168 struct perf_tool *tool, process_auxtrace_t fn,
1169 bool snapshot, size_t snapshot_size)
1171 u64 head, old = mm->prev, offset, ref;
1172 unsigned char *data = mm->base;
1173 size_t size, head_off, old_off, len1, len2, padding;
1174 union perf_event ev;
1175 void *data1, *data2;
1178 head = auxtrace_mmap__read_snapshot_head(mm);
1179 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1183 head = auxtrace_mmap__read_head(mm);
1189 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1190 mm->idx, old, head, head - old);
1193 head_off = head & mm->mask;
1194 old_off = old & mm->mask;
1196 head_off = head % mm->len;
1197 old_off = old % mm->len;
1200 if (head_off > old_off)
1201 size = head_off - old_off;
1203 size = mm->len - (old_off - head_off);
1205 if (snapshot && size > snapshot_size)
1206 size = snapshot_size;
1208 ref = auxtrace_record__reference(itr);
1210 if (head > old || size <= head || mm->mask) {
1211 offset = head - size;
1214 * When the buffer size is not a power of 2, 'head' wraps at the
1215 * highest multiple of the buffer size, so we have to subtract
1216 * the remainder here.
1218 u64 rem = (0ULL - mm->len) % mm->len;
1220 offset = head - size - rem;
1223 if (size > head_off) {
1224 len1 = size - head_off;
1225 data1 = &data[mm->len - len1];
1230 data1 = &data[head_off - len1];
1235 if (itr->alignment) {
1236 unsigned int unwanted = len1 % itr->alignment;
1242 /* padding must be written by fn() e.g. record__process_auxtrace() */
1243 padding = size & (PERF_AUXTRACE_RECORD_ALIGNMENT - 1);
1245 padding = PERF_AUXTRACE_RECORD_ALIGNMENT - padding;
1247 memset(&ev, 0, sizeof(ev));
1248 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1249 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1250 ev.auxtrace.size = size + padding;
1251 ev.auxtrace.offset = offset;
1252 ev.auxtrace.reference = ref;
1253 ev.auxtrace.idx = mm->idx;
1254 ev.auxtrace.tid = mm->tid;
1255 ev.auxtrace.cpu = mm->cpu;
1257 if (fn(tool, &ev, data1, len1, data2, len2))
1263 auxtrace_mmap__write_tail(mm, head);
1264 if (itr->read_finish) {
1267 err = itr->read_finish(itr, mm->idx);
1276 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1277 struct perf_tool *tool, process_auxtrace_t fn)
1279 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1282 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1283 struct auxtrace_record *itr,
1284 struct perf_tool *tool, process_auxtrace_t fn,
1285 size_t snapshot_size)
1287 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1291 * struct auxtrace_cache - hash table to implement a cache
1292 * @hashtable: the hashtable
1293 * @sz: hashtable size (number of hlists)
1294 * @entry_size: size of an entry
1295 * @limit: limit the number of entries to this maximum, when reached the cache
1296 * is dropped and caching begins again with an empty cache
1297 * @cnt: current number of entries
1298 * @bits: hashtable size (@sz = 2^@bits)
1300 struct auxtrace_cache {
1301 struct hlist_head *hashtable;
1309 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1310 unsigned int limit_percent)
1312 struct auxtrace_cache *c;
1313 struct hlist_head *ht;
1316 c = zalloc(sizeof(struct auxtrace_cache));
1322 ht = calloc(sz, sizeof(struct hlist_head));
1326 for (i = 0; i < sz; i++)
1327 INIT_HLIST_HEAD(&ht[i]);
1331 c->entry_size = entry_size;
1332 c->limit = (c->sz * limit_percent) / 100;
1342 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1344 struct auxtrace_cache_entry *entry;
1345 struct hlist_node *tmp;
1351 for (i = 0; i < c->sz; i++) {
1352 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1353 hlist_del(&entry->hash);
1354 auxtrace_cache__free_entry(c, entry);
1361 void auxtrace_cache__free(struct auxtrace_cache *c)
1366 auxtrace_cache__drop(c);
1371 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1373 return malloc(c->entry_size);
1376 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1382 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1383 struct auxtrace_cache_entry *entry)
1385 if (c->limit && ++c->cnt > c->limit)
1386 auxtrace_cache__drop(c);
1389 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1394 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1396 struct auxtrace_cache_entry *entry;
1397 struct hlist_head *hlist;
1402 hlist = &c->hashtable[hash_32(key, c->bits)];
1403 hlist_for_each_entry(entry, hlist, hash) {
1404 if (entry->key == key)
1411 static void addr_filter__free_str(struct addr_filter *filt)
1414 filt->action = NULL;
1415 filt->sym_from = NULL;
1416 filt->sym_to = NULL;
1417 filt->filename = NULL;
1421 static struct addr_filter *addr_filter__new(void)
1423 struct addr_filter *filt = zalloc(sizeof(*filt));
1426 INIT_LIST_HEAD(&filt->list);
1431 static void addr_filter__free(struct addr_filter *filt)
1434 addr_filter__free_str(filt);
1438 static void addr_filters__add(struct addr_filters *filts,
1439 struct addr_filter *filt)
1441 list_add_tail(&filt->list, &filts->head);
1445 static void addr_filters__del(struct addr_filters *filts,
1446 struct addr_filter *filt)
1448 list_del_init(&filt->list);
1452 void addr_filters__init(struct addr_filters *filts)
1454 INIT_LIST_HEAD(&filts->head);
1458 void addr_filters__exit(struct addr_filters *filts)
1460 struct addr_filter *filt, *n;
1462 list_for_each_entry_safe(filt, n, &filts->head, list) {
1463 addr_filters__del(filts, filt);
1464 addr_filter__free(filt);
1468 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1469 const char *str_delim)
1471 *inp += strspn(*inp, " ");
1473 if (isdigit(**inp)) {
1479 *num = strtoull(*inp, &endptr, 0);
1490 *inp += strspn(*inp, " ");
1492 n = strcspn(*inp, str_delim);
1504 static int parse_action(struct addr_filter *filt)
1506 if (!strcmp(filt->action, "filter")) {
1509 } else if (!strcmp(filt->action, "start")) {
1511 } else if (!strcmp(filt->action, "stop")) {
1512 filt->start = false;
1513 } else if (!strcmp(filt->action, "tracestop")) {
1514 filt->start = false;
1516 filt->action += 5; /* Change 'tracestop' to 'stop' */
1523 static int parse_sym_idx(char **inp, int *idx)
1527 *inp += strspn(*inp, " ");
1534 if (**inp == 'g' || **inp == 'G') {
1542 num = strtoul(*inp, &endptr, 0);
1545 if (endptr == *inp || num > INT_MAX)
1554 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
1556 int err = parse_num_or_str(inp, num, str, " ");
1559 err = parse_sym_idx(inp, idx);
1564 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
1569 filt->str = fstr = strdup(*filter_inp);
1573 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
1577 err = parse_action(filt);
1581 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
1582 &filt->sym_from_idx);
1586 fstr += strspn(fstr, " ");
1590 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
1597 fstr += strspn(fstr, " ");
1601 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
1606 fstr += strspn(fstr, " ,");
1608 *filter_inp += fstr - filt->str;
1613 addr_filter__free_str(filt);
1618 int addr_filters__parse_bare_filter(struct addr_filters *filts,
1621 struct addr_filter *filt;
1622 const char *fstr = filter;
1626 filt = addr_filter__new();
1627 err = parse_one_filter(filt, &fstr);
1629 addr_filter__free(filt);
1630 addr_filters__exit(filts);
1633 addr_filters__add(filts, filt);
1652 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
1654 /* A function with the same name, and global or the n'th found or any */
1655 return symbol_type__is_a(type, MAP__FUNCTION) &&
1656 !strcmp(name, args->name) &&
1657 ((args->global && isupper(type)) ||
1658 (args->selected && ++(args->cnt) == args->idx) ||
1659 (!args->global && !args->selected));
1662 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1664 struct sym_args *args = arg;
1666 if (args->started) {
1668 args->size = start - args->start;
1669 if (args->selected) {
1672 } else if (kern_sym_match(args, name, type)) {
1673 args->duplicate = true;
1676 } else if (kern_sym_match(args, name, type)) {
1677 args->started = true;
1678 args->start = start;
1684 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1686 struct sym_args *args = arg;
1688 if (kern_sym_match(args, name, type)) {
1689 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1690 ++args->cnt, start, type, name);
1692 } else if (args->near) {
1694 pr_err("\t\twhich is near\t\t%s\n", name);
1700 static int sym_not_found_error(const char *sym_name, int idx)
1703 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1706 pr_err("Global symbol '%s' not found.\n", sym_name);
1708 pr_err("Symbol '%s' not found.\n", sym_name);
1710 pr_err("Note that symbols must be functions.\n");
1715 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
1717 struct sym_args args = {
1721 .selected = idx > 0,
1728 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
1730 pr_err("Failed to parse /proc/kallsyms\n");
1734 if (args.duplicate) {
1735 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
1737 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
1738 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1740 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1744 if (!args.started) {
1745 pr_err("Kernel symbol lookup: ");
1746 return sym_not_found_error(sym_name, idx);
1749 *start = args.start;
1755 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
1756 char type, u64 start)
1758 struct sym_args *args = arg;
1760 if (!symbol_type__is_a(type, MAP__FUNCTION))
1763 if (!args->started) {
1764 args->started = true;
1765 args->start = start;
1767 /* Don't know exactly where the kernel ends, so we add a page */
1768 args->size = round_up(start, page_size) + page_size - args->start;
1773 static int addr_filter__entire_kernel(struct addr_filter *filt)
1775 struct sym_args args = { .started = false };
1778 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
1779 if (err < 0 || !args.started) {
1780 pr_err("Failed to parse /proc/kallsyms\n");
1784 filt->addr = args.start;
1785 filt->size = args.size;
1790 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
1792 if (start + size >= filt->addr)
1795 if (filt->sym_from) {
1796 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
1797 filt->sym_to, start, filt->sym_from, filt->addr);
1799 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
1800 filt->sym_to, start, filt->addr);
1806 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
1808 bool no_size = false;
1812 if (symbol_conf.kptr_restrict) {
1813 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1817 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
1818 return addr_filter__entire_kernel(filt);
1820 if (filt->sym_from) {
1821 err = find_kern_sym(filt->sym_from, &start, &size,
1822 filt->sym_from_idx);
1826 if (filt->range && !filt->size && !filt->sym_to) {
1833 err = find_kern_sym(filt->sym_to, &start, &size,
1838 err = check_end_after_start(filt, start, size);
1841 filt->size = start + size - filt->addr;
1845 /* The very last symbol in kallsyms does not imply a particular size */
1847 pr_err("Cannot determine size of symbol '%s'\n",
1848 filt->sym_to ? filt->sym_to : filt->sym_from);
1855 static struct dso *load_dso(const char *name)
1860 map = dso__new_map(name);
1866 dso = dso__get(map->dso);
1873 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
1876 /* Same name, and global or the n'th found or any */
1877 return !arch__compare_symbol_names(name, sym->name) &&
1878 ((!idx && sym->binding == STB_GLOBAL) ||
1879 (idx > 0 && ++*cnt == idx) ||
1883 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
1889 pr_err("Multiple symbols with name '%s'\n", sym_name);
1891 sym = dso__first_symbol(dso, MAP__FUNCTION);
1893 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
1894 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1896 sym->binding == STB_GLOBAL ? 'g' :
1897 sym->binding == STB_LOCAL ? 'l' : 'w',
1902 pr_err("\t\twhich is near\t\t%s\n", sym->name);
1904 sym = dso__next_symbol(sym);
1907 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1909 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1912 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
1921 sym = dso__first_symbol(dso, MAP__FUNCTION);
1925 *size = sym->start - *start;
1929 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1930 print_duplicate_syms(dso, sym_name);
1933 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1934 *start = sym->start;
1935 *size = sym->end - sym->start;
1937 sym = dso__next_symbol(sym);
1941 return sym_not_found_error(sym_name, idx);
1946 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
1948 struct symbol *first_sym = dso__first_symbol(dso, MAP__FUNCTION);
1949 struct symbol *last_sym = dso__last_symbol(dso, MAP__FUNCTION);
1951 if (!first_sym || !last_sym) {
1952 pr_err("Failed to determine filter for %s\nNo symbols found.\n",
1957 filt->addr = first_sym->start;
1958 filt->size = last_sym->end - first_sym->start;
1963 static int addr_filter__resolve_syms(struct addr_filter *filt)
1969 if (!filt->sym_from && !filt->sym_to)
1972 if (!filt->filename)
1973 return addr_filter__resolve_kernel_syms(filt);
1975 dso = load_dso(filt->filename);
1977 pr_err("Failed to load symbols from: %s\n", filt->filename);
1981 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
1982 err = addr_filter__entire_dso(filt, dso);
1986 if (filt->sym_from) {
1987 err = find_dso_sym(dso, filt->sym_from, &start, &size,
1988 filt->sym_from_idx);
1992 if (filt->range && !filt->size && !filt->sym_to)
1997 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2002 err = check_end_after_start(filt, start, size);
2006 filt->size = start + size - filt->addr;
2015 static char *addr_filter__to_str(struct addr_filter *filt)
2017 char filename_buf[PATH_MAX];
2018 const char *at = "";
2019 const char *fn = "";
2023 if (filt->filename) {
2025 fn = realpath(filt->filename, filename_buf);
2031 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2032 filt->action, filt->addr, filt->size, at, fn);
2034 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2035 filt->action, filt->addr, at, fn);
2038 return err < 0 ? NULL : filter;
2041 static int parse_addr_filter(struct perf_evsel *evsel, const char *filter,
2044 struct addr_filters filts;
2045 struct addr_filter *filt;
2048 addr_filters__init(&filts);
2050 err = addr_filters__parse_bare_filter(&filts, filter);
2054 if (filts.cnt > max_nr) {
2055 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2061 list_for_each_entry(filt, &filts.head, list) {
2064 err = addr_filter__resolve_syms(filt);
2068 new_filter = addr_filter__to_str(filt);
2074 if (perf_evsel__append_addr_filter(evsel, new_filter)) {
2081 addr_filters__exit(&filts);
2084 pr_err("Failed to parse address filter: '%s'\n", filter);
2085 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2086 pr_err("Where multiple filters are separated by space or comma.\n");
2092 static struct perf_pmu *perf_evsel__find_pmu(struct perf_evsel *evsel)
2094 struct perf_pmu *pmu = NULL;
2096 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2097 if (pmu->type == evsel->attr.type)
2104 static int perf_evsel__nr_addr_filter(struct perf_evsel *evsel)
2106 struct perf_pmu *pmu = perf_evsel__find_pmu(evsel);
2107 int nr_addr_filters = 0;
2112 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2114 return nr_addr_filters;
2117 int auxtrace_parse_filters(struct perf_evlist *evlist)
2119 struct perf_evsel *evsel;
2123 evlist__for_each_entry(evlist, evsel) {
2124 filter = evsel->filter;
2125 max_nr = perf_evsel__nr_addr_filter(evsel);
2126 if (!filter || !max_nr)
2128 evsel->filter = NULL;
2129 err = parse_addr_filter(evsel, filter, max_nr);
2133 pr_debug("Address filter: %s\n", evsel->filter);