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 static bool auxtrace__dont_decode(struct perf_session *session)
65 return !session->itrace_synth_opts ||
66 session->itrace_synth_opts->dont_decode;
69 int auxtrace_mmap__mmap(struct auxtrace_mmap *mm,
70 struct auxtrace_mmap_params *mp,
73 struct perf_event_mmap_page *pc = userpg;
75 WARN_ONCE(mm->base, "Uninitialized auxtrace_mmap\n");
90 #if BITS_PER_LONG != 64 && !defined(HAVE_SYNC_COMPARE_AND_SWAP_SUPPORT)
91 pr_err("Cannot use AUX area tracing mmaps\n");
95 pc->aux_offset = mp->offset;
96 pc->aux_size = mp->len;
98 mm->base = mmap(NULL, mp->len, mp->prot, MAP_SHARED, fd, mp->offset);
99 if (mm->base == MAP_FAILED) {
100 pr_debug2("failed to mmap AUX area\n");
108 void auxtrace_mmap__munmap(struct auxtrace_mmap *mm)
111 munmap(mm->base, mm->len);
116 void auxtrace_mmap_params__init(struct auxtrace_mmap_params *mp,
117 off_t auxtrace_offset,
118 unsigned int auxtrace_pages,
119 bool auxtrace_overwrite)
121 if (auxtrace_pages) {
122 mp->offset = auxtrace_offset;
123 mp->len = auxtrace_pages * (size_t)page_size;
124 mp->mask = is_power_of_2(mp->len) ? mp->len - 1 : 0;
125 mp->prot = PROT_READ | (auxtrace_overwrite ? 0 : PROT_WRITE);
126 pr_debug2("AUX area mmap length %zu\n", mp->len);
132 void auxtrace_mmap_params__set_idx(struct auxtrace_mmap_params *mp,
133 struct perf_evlist *evlist, int idx,
139 mp->cpu = evlist->cpus->map[idx];
141 mp->tid = thread_map__pid(evlist->threads, 0);
146 mp->tid = thread_map__pid(evlist->threads, idx);
150 #define AUXTRACE_INIT_NR_QUEUES 32
152 static struct auxtrace_queue *auxtrace_alloc_queue_array(unsigned int nr_queues)
154 struct auxtrace_queue *queue_array;
155 unsigned int max_nr_queues, i;
157 max_nr_queues = UINT_MAX / sizeof(struct auxtrace_queue);
158 if (nr_queues > max_nr_queues)
161 queue_array = calloc(nr_queues, sizeof(struct auxtrace_queue));
165 for (i = 0; i < nr_queues; i++) {
166 INIT_LIST_HEAD(&queue_array[i].head);
167 queue_array[i].priv = NULL;
173 int auxtrace_queues__init(struct auxtrace_queues *queues)
175 queues->nr_queues = AUXTRACE_INIT_NR_QUEUES;
176 queues->queue_array = auxtrace_alloc_queue_array(queues->nr_queues);
177 if (!queues->queue_array)
182 static int auxtrace_queues__grow(struct auxtrace_queues *queues,
183 unsigned int new_nr_queues)
185 unsigned int nr_queues = queues->nr_queues;
186 struct auxtrace_queue *queue_array;
190 nr_queues = AUXTRACE_INIT_NR_QUEUES;
192 while (nr_queues && nr_queues < new_nr_queues)
195 if (nr_queues < queues->nr_queues || nr_queues < new_nr_queues)
198 queue_array = auxtrace_alloc_queue_array(nr_queues);
202 for (i = 0; i < queues->nr_queues; i++) {
203 list_splice_tail(&queues->queue_array[i].head,
204 &queue_array[i].head);
205 queue_array[i].priv = queues->queue_array[i].priv;
208 queues->nr_queues = nr_queues;
209 queues->queue_array = queue_array;
214 static void *auxtrace_copy_data(u64 size, struct perf_session *session)
216 int fd = perf_data__fd(session->data);
220 if (size > SSIZE_MAX)
227 ret = readn(fd, p, size);
228 if (ret != (ssize_t)size) {
236 static int auxtrace_queues__add_buffer(struct auxtrace_queues *queues,
238 struct auxtrace_buffer *buffer)
240 struct auxtrace_queue *queue;
243 if (idx >= queues->nr_queues) {
244 err = auxtrace_queues__grow(queues, idx + 1);
249 queue = &queues->queue_array[idx];
253 queue->tid = buffer->tid;
254 queue->cpu = buffer->cpu;
255 } else if (buffer->cpu != queue->cpu || buffer->tid != queue->tid) {
256 pr_err("auxtrace queue conflict: cpu %d, tid %d vs cpu %d, tid %d\n",
257 queue->cpu, queue->tid, buffer->cpu, buffer->tid);
261 buffer->buffer_nr = queues->next_buffer_nr++;
263 list_add_tail(&buffer->list, &queue->head);
265 queues->new_data = true;
266 queues->populated = true;
271 /* Limit buffers to 32MiB on 32-bit */
272 #define BUFFER_LIMIT_FOR_32_BIT (32 * 1024 * 1024)
274 static int auxtrace_queues__split_buffer(struct auxtrace_queues *queues,
276 struct auxtrace_buffer *buffer)
278 u64 sz = buffer->size;
279 bool consecutive = false;
280 struct auxtrace_buffer *b;
283 while (sz > BUFFER_LIMIT_FOR_32_BIT) {
284 b = memdup(buffer, sizeof(struct auxtrace_buffer));
287 b->size = BUFFER_LIMIT_FOR_32_BIT;
288 b->consecutive = consecutive;
289 err = auxtrace_queues__add_buffer(queues, idx, b);
291 auxtrace_buffer__free(b);
294 buffer->data_offset += BUFFER_LIMIT_FOR_32_BIT;
295 sz -= BUFFER_LIMIT_FOR_32_BIT;
300 buffer->consecutive = consecutive;
305 static int auxtrace_queues__add_event_buffer(struct auxtrace_queues *queues,
306 struct perf_session *session,
308 struct auxtrace_buffer *buffer)
310 if (session->one_mmap) {
311 buffer->data = buffer->data_offset - session->one_mmap_offset +
312 session->one_mmap_addr;
313 } else if (perf_data__is_pipe(session->data)) {
314 buffer->data = auxtrace_copy_data(buffer->size, session);
317 buffer->data_needs_freeing = true;
318 } else if (BITS_PER_LONG == 32 &&
319 buffer->size > BUFFER_LIMIT_FOR_32_BIT) {
322 err = auxtrace_queues__split_buffer(queues, idx, buffer);
327 return auxtrace_queues__add_buffer(queues, idx, buffer);
330 static bool filter_cpu(struct perf_session *session, int cpu)
332 unsigned long *cpu_bitmap = session->itrace_synth_opts->cpu_bitmap;
334 return cpu_bitmap && cpu != -1 && !test_bit(cpu, cpu_bitmap);
337 int auxtrace_queues__add_event(struct auxtrace_queues *queues,
338 struct perf_session *session,
339 union perf_event *event, off_t data_offset,
340 struct auxtrace_buffer **buffer_ptr)
342 struct auxtrace_buffer *buffer;
346 if (filter_cpu(session, event->auxtrace.cpu))
349 buffer = zalloc(sizeof(struct auxtrace_buffer));
354 buffer->tid = event->auxtrace.tid;
355 buffer->cpu = event->auxtrace.cpu;
356 buffer->data_offset = data_offset;
357 buffer->offset = event->auxtrace.offset;
358 buffer->reference = event->auxtrace.reference;
359 buffer->size = event->auxtrace.size;
360 idx = event->auxtrace.idx;
362 err = auxtrace_queues__add_event_buffer(queues, session, idx, buffer);
367 *buffer_ptr = buffer;
372 auxtrace_buffer__free(buffer);
376 static int auxtrace_queues__add_indexed_event(struct auxtrace_queues *queues,
377 struct perf_session *session,
378 off_t file_offset, size_t sz)
380 union perf_event *event;
382 char buf[PERF_SAMPLE_MAX_SIZE];
384 err = perf_session__peek_event(session, file_offset, buf,
385 PERF_SAMPLE_MAX_SIZE, &event, NULL);
389 if (event->header.type == PERF_RECORD_AUXTRACE) {
390 if (event->header.size < sizeof(struct auxtrace_event) ||
391 event->header.size != sz) {
395 file_offset += event->header.size;
396 err = auxtrace_queues__add_event(queues, session, event,
403 void auxtrace_queues__free(struct auxtrace_queues *queues)
407 for (i = 0; i < queues->nr_queues; i++) {
408 while (!list_empty(&queues->queue_array[i].head)) {
409 struct auxtrace_buffer *buffer;
411 buffer = list_entry(queues->queue_array[i].head.next,
412 struct auxtrace_buffer, list);
413 list_del(&buffer->list);
414 auxtrace_buffer__free(buffer);
418 zfree(&queues->queue_array);
419 queues->nr_queues = 0;
422 static void auxtrace_heapify(struct auxtrace_heap_item *heap_array,
423 unsigned int pos, unsigned int queue_nr,
429 parent = (pos - 1) >> 1;
430 if (heap_array[parent].ordinal <= ordinal)
432 heap_array[pos] = heap_array[parent];
435 heap_array[pos].queue_nr = queue_nr;
436 heap_array[pos].ordinal = ordinal;
439 int auxtrace_heap__add(struct auxtrace_heap *heap, unsigned int queue_nr,
442 struct auxtrace_heap_item *heap_array;
444 if (queue_nr >= heap->heap_sz) {
445 unsigned int heap_sz = AUXTRACE_INIT_NR_QUEUES;
447 while (heap_sz <= queue_nr)
449 heap_array = realloc(heap->heap_array,
450 heap_sz * sizeof(struct auxtrace_heap_item));
453 heap->heap_array = heap_array;
454 heap->heap_sz = heap_sz;
457 auxtrace_heapify(heap->heap_array, heap->heap_cnt++, queue_nr, ordinal);
462 void auxtrace_heap__free(struct auxtrace_heap *heap)
464 zfree(&heap->heap_array);
469 void auxtrace_heap__pop(struct auxtrace_heap *heap)
471 unsigned int pos, last, heap_cnt = heap->heap_cnt;
472 struct auxtrace_heap_item *heap_array;
479 heap_array = heap->heap_array;
483 unsigned int left, right;
485 left = (pos << 1) + 1;
486 if (left >= heap_cnt)
489 if (right >= heap_cnt) {
490 heap_array[pos] = heap_array[left];
493 if (heap_array[left].ordinal < heap_array[right].ordinal) {
494 heap_array[pos] = heap_array[left];
497 heap_array[pos] = heap_array[right];
503 auxtrace_heapify(heap_array, pos, heap_array[last].queue_nr,
504 heap_array[last].ordinal);
507 size_t auxtrace_record__info_priv_size(struct auxtrace_record *itr,
508 struct perf_evlist *evlist)
511 return itr->info_priv_size(itr, evlist);
515 static int auxtrace_not_supported(void)
517 pr_err("AUX area tracing is not supported on this architecture\n");
521 int auxtrace_record__info_fill(struct auxtrace_record *itr,
522 struct perf_session *session,
523 struct auxtrace_info_event *auxtrace_info,
527 return itr->info_fill(itr, session, auxtrace_info, priv_size);
528 return auxtrace_not_supported();
531 void auxtrace_record__free(struct auxtrace_record *itr)
537 int auxtrace_record__snapshot_start(struct auxtrace_record *itr)
539 if (itr && itr->snapshot_start)
540 return itr->snapshot_start(itr);
544 int auxtrace_record__snapshot_finish(struct auxtrace_record *itr)
546 if (itr && itr->snapshot_finish)
547 return itr->snapshot_finish(itr);
551 int auxtrace_record__find_snapshot(struct auxtrace_record *itr, int idx,
552 struct auxtrace_mmap *mm,
553 unsigned char *data, u64 *head, u64 *old)
555 if (itr && itr->find_snapshot)
556 return itr->find_snapshot(itr, idx, mm, data, head, old);
560 int auxtrace_record__options(struct auxtrace_record *itr,
561 struct perf_evlist *evlist,
562 struct record_opts *opts)
565 return itr->recording_options(itr, evlist, opts);
569 u64 auxtrace_record__reference(struct auxtrace_record *itr)
572 return itr->reference(itr);
576 int auxtrace_parse_snapshot_options(struct auxtrace_record *itr,
577 struct record_opts *opts, const char *str)
583 return itr->parse_snapshot_options(itr, opts, str);
585 pr_err("No AUX area tracing to snapshot\n");
589 struct auxtrace_record *__weak
590 auxtrace_record__init(struct perf_evlist *evlist __maybe_unused, int *err)
596 static int auxtrace_index__alloc(struct list_head *head)
598 struct auxtrace_index *auxtrace_index;
600 auxtrace_index = malloc(sizeof(struct auxtrace_index));
604 auxtrace_index->nr = 0;
605 INIT_LIST_HEAD(&auxtrace_index->list);
607 list_add_tail(&auxtrace_index->list, head);
612 void auxtrace_index__free(struct list_head *head)
614 struct auxtrace_index *auxtrace_index, *n;
616 list_for_each_entry_safe(auxtrace_index, n, head, list) {
617 list_del(&auxtrace_index->list);
618 free(auxtrace_index);
622 static struct auxtrace_index *auxtrace_index__last(struct list_head *head)
624 struct auxtrace_index *auxtrace_index;
627 if (list_empty(head)) {
628 err = auxtrace_index__alloc(head);
633 auxtrace_index = list_entry(head->prev, struct auxtrace_index, list);
635 if (auxtrace_index->nr >= PERF_AUXTRACE_INDEX_ENTRY_COUNT) {
636 err = auxtrace_index__alloc(head);
639 auxtrace_index = list_entry(head->prev, struct auxtrace_index,
643 return auxtrace_index;
646 int auxtrace_index__auxtrace_event(struct list_head *head,
647 union perf_event *event, off_t file_offset)
649 struct auxtrace_index *auxtrace_index;
652 auxtrace_index = auxtrace_index__last(head);
656 nr = auxtrace_index->nr;
657 auxtrace_index->entries[nr].file_offset = file_offset;
658 auxtrace_index->entries[nr].sz = event->header.size;
659 auxtrace_index->nr += 1;
664 static int auxtrace_index__do_write(int fd,
665 struct auxtrace_index *auxtrace_index)
667 struct auxtrace_index_entry ent;
670 for (i = 0; i < auxtrace_index->nr; i++) {
671 ent.file_offset = auxtrace_index->entries[i].file_offset;
672 ent.sz = auxtrace_index->entries[i].sz;
673 if (writen(fd, &ent, sizeof(ent)) != sizeof(ent))
679 int auxtrace_index__write(int fd, struct list_head *head)
681 struct auxtrace_index *auxtrace_index;
685 list_for_each_entry(auxtrace_index, head, list)
686 total += auxtrace_index->nr;
688 if (writen(fd, &total, sizeof(total)) != sizeof(total))
691 list_for_each_entry(auxtrace_index, head, list) {
692 err = auxtrace_index__do_write(fd, auxtrace_index);
700 static int auxtrace_index__process_entry(int fd, struct list_head *head,
703 struct auxtrace_index *auxtrace_index;
704 struct auxtrace_index_entry ent;
707 if (readn(fd, &ent, sizeof(ent)) != sizeof(ent))
710 auxtrace_index = auxtrace_index__last(head);
714 nr = auxtrace_index->nr;
716 auxtrace_index->entries[nr].file_offset =
717 bswap_64(ent.file_offset);
718 auxtrace_index->entries[nr].sz = bswap_64(ent.sz);
720 auxtrace_index->entries[nr].file_offset = ent.file_offset;
721 auxtrace_index->entries[nr].sz = ent.sz;
724 auxtrace_index->nr = nr + 1;
729 int auxtrace_index__process(int fd, u64 size, struct perf_session *session,
732 struct list_head *head = &session->auxtrace_index;
735 if (readn(fd, &nr, sizeof(u64)) != sizeof(u64))
741 if (sizeof(u64) + nr * sizeof(struct auxtrace_index_entry) > size)
747 err = auxtrace_index__process_entry(fd, head, needs_swap);
755 static int auxtrace_queues__process_index_entry(struct auxtrace_queues *queues,
756 struct perf_session *session,
757 struct auxtrace_index_entry *ent)
759 return auxtrace_queues__add_indexed_event(queues, session,
760 ent->file_offset, ent->sz);
763 int auxtrace_queues__process_index(struct auxtrace_queues *queues,
764 struct perf_session *session)
766 struct auxtrace_index *auxtrace_index;
767 struct auxtrace_index_entry *ent;
771 if (auxtrace__dont_decode(session))
774 list_for_each_entry(auxtrace_index, &session->auxtrace_index, list) {
775 for (i = 0; i < auxtrace_index->nr; i++) {
776 ent = &auxtrace_index->entries[i];
777 err = auxtrace_queues__process_index_entry(queues,
787 struct auxtrace_buffer *auxtrace_buffer__next(struct auxtrace_queue *queue,
788 struct auxtrace_buffer *buffer)
791 if (list_is_last(&buffer->list, &queue->head))
793 return list_entry(buffer->list.next, struct auxtrace_buffer,
796 if (list_empty(&queue->head))
798 return list_entry(queue->head.next, struct auxtrace_buffer,
803 void *auxtrace_buffer__get_data(struct auxtrace_buffer *buffer, int fd)
805 size_t adj = buffer->data_offset & (page_size - 1);
806 size_t size = buffer->size + adj;
807 off_t file_offset = buffer->data_offset - adj;
813 addr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, file_offset);
814 if (addr == MAP_FAILED)
817 buffer->mmap_addr = addr;
818 buffer->mmap_size = size;
820 buffer->data = addr + adj;
825 void auxtrace_buffer__put_data(struct auxtrace_buffer *buffer)
827 if (!buffer->data || !buffer->mmap_addr)
829 munmap(buffer->mmap_addr, buffer->mmap_size);
830 buffer->mmap_addr = NULL;
831 buffer->mmap_size = 0;
833 buffer->use_data = NULL;
836 void auxtrace_buffer__drop_data(struct auxtrace_buffer *buffer)
838 auxtrace_buffer__put_data(buffer);
839 if (buffer->data_needs_freeing) {
840 buffer->data_needs_freeing = false;
841 zfree(&buffer->data);
842 buffer->use_data = NULL;
847 void auxtrace_buffer__free(struct auxtrace_buffer *buffer)
849 auxtrace_buffer__drop_data(buffer);
853 void auxtrace_synth_error(struct auxtrace_error_event *auxtrace_error, int type,
854 int code, int cpu, pid_t pid, pid_t tid, u64 ip,
859 memset(auxtrace_error, 0, sizeof(struct auxtrace_error_event));
861 auxtrace_error->header.type = PERF_RECORD_AUXTRACE_ERROR;
862 auxtrace_error->type = type;
863 auxtrace_error->code = code;
864 auxtrace_error->cpu = cpu;
865 auxtrace_error->pid = pid;
866 auxtrace_error->tid = tid;
867 auxtrace_error->ip = ip;
868 strlcpy(auxtrace_error->msg, msg, MAX_AUXTRACE_ERROR_MSG);
870 size = (void *)auxtrace_error->msg - (void *)auxtrace_error +
871 strlen(auxtrace_error->msg) + 1;
872 auxtrace_error->header.size = PERF_ALIGN(size, sizeof(u64));
875 int perf_event__synthesize_auxtrace_info(struct auxtrace_record *itr,
876 struct perf_tool *tool,
877 struct perf_session *session,
878 perf_event__handler_t process)
880 union perf_event *ev;
884 pr_debug2("Synthesizing auxtrace information\n");
885 priv_size = auxtrace_record__info_priv_size(itr, session->evlist);
886 ev = zalloc(sizeof(struct auxtrace_info_event) + priv_size);
890 ev->auxtrace_info.header.type = PERF_RECORD_AUXTRACE_INFO;
891 ev->auxtrace_info.header.size = sizeof(struct auxtrace_info_event) +
893 err = auxtrace_record__info_fill(itr, session, &ev->auxtrace_info,
898 err = process(tool, ev, NULL, NULL);
904 int perf_event__process_auxtrace_info(struct perf_tool *tool __maybe_unused,
905 union perf_event *event,
906 struct perf_session *session)
908 enum auxtrace_type type = event->auxtrace_info.type;
911 fprintf(stdout, " type: %u\n", type);
914 case PERF_AUXTRACE_INTEL_PT:
915 return intel_pt_process_auxtrace_info(event, session);
916 case PERF_AUXTRACE_INTEL_BTS:
917 return intel_bts_process_auxtrace_info(event, session);
918 case PERF_AUXTRACE_ARM_SPE:
919 return arm_spe_process_auxtrace_info(event, session);
920 case PERF_AUXTRACE_CS_ETM:
921 return cs_etm__process_auxtrace_info(event, session);
922 case PERF_AUXTRACE_UNKNOWN:
928 s64 perf_event__process_auxtrace(struct perf_tool *tool,
929 union perf_event *event,
930 struct perf_session *session)
935 fprintf(stdout, " size: %#"PRIx64" offset: %#"PRIx64" ref: %#"PRIx64" idx: %u tid: %d cpu: %d\n",
936 event->auxtrace.size, event->auxtrace.offset,
937 event->auxtrace.reference, event->auxtrace.idx,
938 event->auxtrace.tid, event->auxtrace.cpu);
940 if (auxtrace__dont_decode(session))
941 return event->auxtrace.size;
943 if (!session->auxtrace || event->header.type != PERF_RECORD_AUXTRACE)
946 err = session->auxtrace->process_auxtrace_event(session, event, tool);
950 return event->auxtrace.size;
953 #define PERF_ITRACE_DEFAULT_PERIOD_TYPE PERF_ITRACE_PERIOD_NANOSECS
954 #define PERF_ITRACE_DEFAULT_PERIOD 100000
955 #define PERF_ITRACE_DEFAULT_CALLCHAIN_SZ 16
956 #define PERF_ITRACE_MAX_CALLCHAIN_SZ 1024
957 #define PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ 64
958 #define PERF_ITRACE_MAX_LAST_BRANCH_SZ 1024
960 void itrace_synth_opts__set_default(struct itrace_synth_opts *synth_opts)
962 synth_opts->instructions = true;
963 synth_opts->branches = true;
964 synth_opts->transactions = true;
965 synth_opts->ptwrites = true;
966 synth_opts->pwr_events = true;
967 synth_opts->errors = true;
968 synth_opts->period_type = PERF_ITRACE_DEFAULT_PERIOD_TYPE;
969 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
970 synth_opts->callchain_sz = PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
971 synth_opts->last_branch_sz = PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
972 synth_opts->initial_skip = 0;
976 * Please check tools/perf/Documentation/perf-script.txt for information
977 * about the options parsed here, which is introduced after this cset,
978 * when support in 'perf script' for these options is introduced.
980 int itrace_parse_synth_opts(const struct option *opt, const char *str,
983 struct itrace_synth_opts *synth_opts = opt->value;
986 bool period_type_set = false;
987 bool period_set = false;
989 synth_opts->set = true;
992 synth_opts->dont_decode = true;
997 itrace_synth_opts__set_default(synth_opts);
1001 for (p = str; *p;) {
1004 synth_opts->instructions = true;
1005 while (*p == ' ' || *p == ',')
1008 synth_opts->period = strtoull(p, &endptr, 10);
1011 while (*p == ' ' || *p == ',')
1015 synth_opts->period_type =
1016 PERF_ITRACE_PERIOD_INSTRUCTIONS;
1017 period_type_set = true;
1020 synth_opts->period_type =
1021 PERF_ITRACE_PERIOD_TICKS;
1022 period_type_set = true;
1025 synth_opts->period *= 1000;
1028 synth_opts->period *= 1000;
1033 synth_opts->period_type =
1034 PERF_ITRACE_PERIOD_NANOSECS;
1035 period_type_set = true;
1045 synth_opts->branches = true;
1048 synth_opts->transactions = true;
1051 synth_opts->ptwrites = true;
1054 synth_opts->pwr_events = true;
1057 synth_opts->errors = true;
1060 synth_opts->log = true;
1063 synth_opts->branches = true;
1064 synth_opts->calls = true;
1067 synth_opts->branches = true;
1068 synth_opts->returns = true;
1071 synth_opts->callchain = true;
1072 synth_opts->callchain_sz =
1073 PERF_ITRACE_DEFAULT_CALLCHAIN_SZ;
1074 while (*p == ' ' || *p == ',')
1079 val = strtoul(p, &endptr, 10);
1081 if (!val || val > PERF_ITRACE_MAX_CALLCHAIN_SZ)
1083 synth_opts->callchain_sz = val;
1087 synth_opts->last_branch = true;
1088 synth_opts->last_branch_sz =
1089 PERF_ITRACE_DEFAULT_LAST_BRANCH_SZ;
1090 while (*p == ' ' || *p == ',')
1095 val = strtoul(p, &endptr, 10);
1098 val > PERF_ITRACE_MAX_LAST_BRANCH_SZ)
1100 synth_opts->last_branch_sz = val;
1104 synth_opts->initial_skip = strtoul(p, &endptr, 10);
1117 if (synth_opts->instructions) {
1118 if (!period_type_set)
1119 synth_opts->period_type =
1120 PERF_ITRACE_DEFAULT_PERIOD_TYPE;
1122 synth_opts->period = PERF_ITRACE_DEFAULT_PERIOD;
1128 pr_err("Bad Instruction Tracing options '%s'\n", str);
1132 static const char * const auxtrace_error_type_name[] = {
1133 [PERF_AUXTRACE_ERROR_ITRACE] = "instruction trace",
1136 static const char *auxtrace_error_name(int type)
1138 const char *error_type_name = NULL;
1140 if (type < PERF_AUXTRACE_ERROR_MAX)
1141 error_type_name = auxtrace_error_type_name[type];
1142 if (!error_type_name)
1143 error_type_name = "unknown AUX";
1144 return error_type_name;
1147 size_t perf_event__fprintf_auxtrace_error(union perf_event *event, FILE *fp)
1149 struct auxtrace_error_event *e = &event->auxtrace_error;
1152 ret = fprintf(fp, " %s error type %u",
1153 auxtrace_error_name(e->type), e->type);
1154 ret += fprintf(fp, " cpu %d pid %d tid %d ip %#"PRIx64" code %u: %s\n",
1155 e->cpu, e->pid, e->tid, e->ip, e->code, e->msg);
1159 void perf_session__auxtrace_error_inc(struct perf_session *session,
1160 union perf_event *event)
1162 struct auxtrace_error_event *e = &event->auxtrace_error;
1164 if (e->type < PERF_AUXTRACE_ERROR_MAX)
1165 session->evlist->stats.nr_auxtrace_errors[e->type] += 1;
1168 void events_stats__auxtrace_error_warn(const struct events_stats *stats)
1172 for (i = 0; i < PERF_AUXTRACE_ERROR_MAX; i++) {
1173 if (!stats->nr_auxtrace_errors[i])
1175 ui__warning("%u %s errors\n",
1176 stats->nr_auxtrace_errors[i],
1177 auxtrace_error_name(i));
1181 int perf_event__process_auxtrace_error(struct perf_tool *tool __maybe_unused,
1182 union perf_event *event,
1183 struct perf_session *session)
1185 if (auxtrace__dont_decode(session))
1188 perf_event__fprintf_auxtrace_error(event, stdout);
1192 static int __auxtrace_mmap__read(struct auxtrace_mmap *mm,
1193 struct auxtrace_record *itr,
1194 struct perf_tool *tool, process_auxtrace_t fn,
1195 bool snapshot, size_t snapshot_size)
1197 u64 head, old = mm->prev, offset, ref;
1198 unsigned char *data = mm->base;
1199 size_t size, head_off, old_off, len1, len2, padding;
1200 union perf_event ev;
1201 void *data1, *data2;
1204 head = auxtrace_mmap__read_snapshot_head(mm);
1205 if (auxtrace_record__find_snapshot(itr, mm->idx, mm, data,
1209 head = auxtrace_mmap__read_head(mm);
1215 pr_debug3("auxtrace idx %d old %#"PRIx64" head %#"PRIx64" diff %#"PRIx64"\n",
1216 mm->idx, old, head, head - old);
1219 head_off = head & mm->mask;
1220 old_off = old & mm->mask;
1222 head_off = head % mm->len;
1223 old_off = old % mm->len;
1226 if (head_off > old_off)
1227 size = head_off - old_off;
1229 size = mm->len - (old_off - head_off);
1231 if (snapshot && size > snapshot_size)
1232 size = snapshot_size;
1234 ref = auxtrace_record__reference(itr);
1236 if (head > old || size <= head || mm->mask) {
1237 offset = head - size;
1240 * When the buffer size is not a power of 2, 'head' wraps at the
1241 * highest multiple of the buffer size, so we have to subtract
1242 * the remainder here.
1244 u64 rem = (0ULL - mm->len) % mm->len;
1246 offset = head - size - rem;
1249 if (size > head_off) {
1250 len1 = size - head_off;
1251 data1 = &data[mm->len - len1];
1256 data1 = &data[head_off - len1];
1261 if (itr->alignment) {
1262 unsigned int unwanted = len1 % itr->alignment;
1268 /* padding must be written by fn() e.g. record__process_auxtrace() */
1271 padding = 8 - padding;
1273 memset(&ev, 0, sizeof(ev));
1274 ev.auxtrace.header.type = PERF_RECORD_AUXTRACE;
1275 ev.auxtrace.header.size = sizeof(ev.auxtrace);
1276 ev.auxtrace.size = size + padding;
1277 ev.auxtrace.offset = offset;
1278 ev.auxtrace.reference = ref;
1279 ev.auxtrace.idx = mm->idx;
1280 ev.auxtrace.tid = mm->tid;
1281 ev.auxtrace.cpu = mm->cpu;
1283 if (fn(tool, &ev, data1, len1, data2, len2))
1289 auxtrace_mmap__write_tail(mm, head);
1290 if (itr->read_finish) {
1293 err = itr->read_finish(itr, mm->idx);
1302 int auxtrace_mmap__read(struct auxtrace_mmap *mm, struct auxtrace_record *itr,
1303 struct perf_tool *tool, process_auxtrace_t fn)
1305 return __auxtrace_mmap__read(mm, itr, tool, fn, false, 0);
1308 int auxtrace_mmap__read_snapshot(struct auxtrace_mmap *mm,
1309 struct auxtrace_record *itr,
1310 struct perf_tool *tool, process_auxtrace_t fn,
1311 size_t snapshot_size)
1313 return __auxtrace_mmap__read(mm, itr, tool, fn, true, snapshot_size);
1317 * struct auxtrace_cache - hash table to implement a cache
1318 * @hashtable: the hashtable
1319 * @sz: hashtable size (number of hlists)
1320 * @entry_size: size of an entry
1321 * @limit: limit the number of entries to this maximum, when reached the cache
1322 * is dropped and caching begins again with an empty cache
1323 * @cnt: current number of entries
1324 * @bits: hashtable size (@sz = 2^@bits)
1326 struct auxtrace_cache {
1327 struct hlist_head *hashtable;
1335 struct auxtrace_cache *auxtrace_cache__new(unsigned int bits, size_t entry_size,
1336 unsigned int limit_percent)
1338 struct auxtrace_cache *c;
1339 struct hlist_head *ht;
1342 c = zalloc(sizeof(struct auxtrace_cache));
1348 ht = calloc(sz, sizeof(struct hlist_head));
1352 for (i = 0; i < sz; i++)
1353 INIT_HLIST_HEAD(&ht[i]);
1357 c->entry_size = entry_size;
1358 c->limit = (c->sz * limit_percent) / 100;
1368 static void auxtrace_cache__drop(struct auxtrace_cache *c)
1370 struct auxtrace_cache_entry *entry;
1371 struct hlist_node *tmp;
1377 for (i = 0; i < c->sz; i++) {
1378 hlist_for_each_entry_safe(entry, tmp, &c->hashtable[i], hash) {
1379 hlist_del(&entry->hash);
1380 auxtrace_cache__free_entry(c, entry);
1387 void auxtrace_cache__free(struct auxtrace_cache *c)
1392 auxtrace_cache__drop(c);
1397 void *auxtrace_cache__alloc_entry(struct auxtrace_cache *c)
1399 return malloc(c->entry_size);
1402 void auxtrace_cache__free_entry(struct auxtrace_cache *c __maybe_unused,
1408 int auxtrace_cache__add(struct auxtrace_cache *c, u32 key,
1409 struct auxtrace_cache_entry *entry)
1411 if (c->limit && ++c->cnt > c->limit)
1412 auxtrace_cache__drop(c);
1415 hlist_add_head(&entry->hash, &c->hashtable[hash_32(key, c->bits)]);
1420 void *auxtrace_cache__lookup(struct auxtrace_cache *c, u32 key)
1422 struct auxtrace_cache_entry *entry;
1423 struct hlist_head *hlist;
1428 hlist = &c->hashtable[hash_32(key, c->bits)];
1429 hlist_for_each_entry(entry, hlist, hash) {
1430 if (entry->key == key)
1437 static void addr_filter__free_str(struct addr_filter *filt)
1440 filt->action = NULL;
1441 filt->sym_from = NULL;
1442 filt->sym_to = NULL;
1443 filt->filename = NULL;
1447 static struct addr_filter *addr_filter__new(void)
1449 struct addr_filter *filt = zalloc(sizeof(*filt));
1452 INIT_LIST_HEAD(&filt->list);
1457 static void addr_filter__free(struct addr_filter *filt)
1460 addr_filter__free_str(filt);
1464 static void addr_filters__add(struct addr_filters *filts,
1465 struct addr_filter *filt)
1467 list_add_tail(&filt->list, &filts->head);
1471 static void addr_filters__del(struct addr_filters *filts,
1472 struct addr_filter *filt)
1474 list_del_init(&filt->list);
1478 void addr_filters__init(struct addr_filters *filts)
1480 INIT_LIST_HEAD(&filts->head);
1484 void addr_filters__exit(struct addr_filters *filts)
1486 struct addr_filter *filt, *n;
1488 list_for_each_entry_safe(filt, n, &filts->head, list) {
1489 addr_filters__del(filts, filt);
1490 addr_filter__free(filt);
1494 static int parse_num_or_str(char **inp, u64 *num, const char **str,
1495 const char *str_delim)
1497 *inp += strspn(*inp, " ");
1499 if (isdigit(**inp)) {
1505 *num = strtoull(*inp, &endptr, 0);
1516 *inp += strspn(*inp, " ");
1518 n = strcspn(*inp, str_delim);
1530 static int parse_action(struct addr_filter *filt)
1532 if (!strcmp(filt->action, "filter")) {
1535 } else if (!strcmp(filt->action, "start")) {
1537 } else if (!strcmp(filt->action, "stop")) {
1538 filt->start = false;
1539 } else if (!strcmp(filt->action, "tracestop")) {
1540 filt->start = false;
1542 filt->action += 5; /* Change 'tracestop' to 'stop' */
1549 static int parse_sym_idx(char **inp, int *idx)
1553 *inp += strspn(*inp, " ");
1560 if (**inp == 'g' || **inp == 'G') {
1568 num = strtoul(*inp, &endptr, 0);
1571 if (endptr == *inp || num > INT_MAX)
1580 static int parse_addr_size(char **inp, u64 *num, const char **str, int *idx)
1582 int err = parse_num_or_str(inp, num, str, " ");
1585 err = parse_sym_idx(inp, idx);
1590 static int parse_one_filter(struct addr_filter *filt, const char **filter_inp)
1595 filt->str = fstr = strdup(*filter_inp);
1599 err = parse_num_or_str(&fstr, NULL, &filt->action, " ");
1603 err = parse_action(filt);
1607 err = parse_addr_size(&fstr, &filt->addr, &filt->sym_from,
1608 &filt->sym_from_idx);
1612 fstr += strspn(fstr, " ");
1616 err = parse_addr_size(&fstr, &filt->size, &filt->sym_to,
1623 fstr += strspn(fstr, " ");
1627 err = parse_num_or_str(&fstr, NULL, &filt->filename, " ,");
1632 fstr += strspn(fstr, " ,");
1634 *filter_inp += fstr - filt->str;
1639 addr_filter__free_str(filt);
1644 int addr_filters__parse_bare_filter(struct addr_filters *filts,
1647 struct addr_filter *filt;
1648 const char *fstr = filter;
1652 filt = addr_filter__new();
1653 err = parse_one_filter(filt, &fstr);
1655 addr_filter__free(filt);
1656 addr_filters__exit(filts);
1659 addr_filters__add(filts, filt);
1678 static bool kern_sym_match(struct sym_args *args, const char *name, char type)
1680 /* A function with the same name, and global or the n'th found or any */
1681 return symbol_type__is_a(type, MAP__FUNCTION) &&
1682 !strcmp(name, args->name) &&
1683 ((args->global && isupper(type)) ||
1684 (args->selected && ++(args->cnt) == args->idx) ||
1685 (!args->global && !args->selected));
1688 static int find_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1690 struct sym_args *args = arg;
1692 if (args->started) {
1694 args->size = start - args->start;
1695 if (args->selected) {
1698 } else if (kern_sym_match(args, name, type)) {
1699 args->duplicate = true;
1702 } else if (kern_sym_match(args, name, type)) {
1703 args->started = true;
1704 args->start = start;
1710 static int print_kern_sym_cb(void *arg, const char *name, char type, u64 start)
1712 struct sym_args *args = arg;
1714 if (kern_sym_match(args, name, type)) {
1715 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1716 ++args->cnt, start, type, name);
1718 } else if (args->near) {
1720 pr_err("\t\twhich is near\t\t%s\n", name);
1726 static int sym_not_found_error(const char *sym_name, int idx)
1729 pr_err("N'th occurrence (N=%d) of symbol '%s' not found.\n",
1732 pr_err("Global symbol '%s' not found.\n", sym_name);
1734 pr_err("Symbol '%s' not found.\n", sym_name);
1736 pr_err("Note that symbols must be functions.\n");
1741 static int find_kern_sym(const char *sym_name, u64 *start, u64 *size, int idx)
1743 struct sym_args args = {
1747 .selected = idx > 0,
1754 err = kallsyms__parse("/proc/kallsyms", &args, find_kern_sym_cb);
1756 pr_err("Failed to parse /proc/kallsyms\n");
1760 if (args.duplicate) {
1761 pr_err("Multiple kernel symbols with name '%s'\n", sym_name);
1763 kallsyms__parse("/proc/kallsyms", &args, print_kern_sym_cb);
1764 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1766 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1770 if (!args.started) {
1771 pr_err("Kernel symbol lookup: ");
1772 return sym_not_found_error(sym_name, idx);
1775 *start = args.start;
1781 static int find_entire_kern_cb(void *arg, const char *name __maybe_unused,
1782 char type, u64 start)
1784 struct sym_args *args = arg;
1786 if (!symbol_type__is_a(type, MAP__FUNCTION))
1789 if (!args->started) {
1790 args->started = true;
1791 args->start = start;
1793 /* Don't know exactly where the kernel ends, so we add a page */
1794 args->size = round_up(start, page_size) + page_size - args->start;
1799 static int addr_filter__entire_kernel(struct addr_filter *filt)
1801 struct sym_args args = { .started = false };
1804 err = kallsyms__parse("/proc/kallsyms", &args, find_entire_kern_cb);
1805 if (err < 0 || !args.started) {
1806 pr_err("Failed to parse /proc/kallsyms\n");
1810 filt->addr = args.start;
1811 filt->size = args.size;
1816 static int check_end_after_start(struct addr_filter *filt, u64 start, u64 size)
1818 if (start + size >= filt->addr)
1821 if (filt->sym_from) {
1822 pr_err("Symbol '%s' (0x%"PRIx64") comes before '%s' (0x%"PRIx64")\n",
1823 filt->sym_to, start, filt->sym_from, filt->addr);
1825 pr_err("Symbol '%s' (0x%"PRIx64") comes before address 0x%"PRIx64")\n",
1826 filt->sym_to, start, filt->addr);
1832 static int addr_filter__resolve_kernel_syms(struct addr_filter *filt)
1834 bool no_size = false;
1838 if (symbol_conf.kptr_restrict) {
1839 pr_err("Kernel addresses are restricted. Unable to resolve kernel symbols.\n");
1843 if (filt->sym_from && !strcmp(filt->sym_from, "*"))
1844 return addr_filter__entire_kernel(filt);
1846 if (filt->sym_from) {
1847 err = find_kern_sym(filt->sym_from, &start, &size,
1848 filt->sym_from_idx);
1852 if (filt->range && !filt->size && !filt->sym_to) {
1859 err = find_kern_sym(filt->sym_to, &start, &size,
1864 err = check_end_after_start(filt, start, size);
1867 filt->size = start + size - filt->addr;
1871 /* The very last symbol in kallsyms does not imply a particular size */
1873 pr_err("Cannot determine size of symbol '%s'\n",
1874 filt->sym_to ? filt->sym_to : filt->sym_from);
1881 static struct dso *load_dso(const char *name)
1886 map = dso__new_map(name);
1892 dso = dso__get(map->dso);
1899 static bool dso_sym_match(struct symbol *sym, const char *name, int *cnt,
1902 /* Same name, and global or the n'th found or any */
1903 return !arch__compare_symbol_names(name, sym->name) &&
1904 ((!idx && sym->binding == STB_GLOBAL) ||
1905 (idx > 0 && ++*cnt == idx) ||
1909 static void print_duplicate_syms(struct dso *dso, const char *sym_name)
1915 pr_err("Multiple symbols with name '%s'\n", sym_name);
1917 sym = dso__first_symbol(dso, MAP__FUNCTION);
1919 if (dso_sym_match(sym, sym_name, &cnt, -1)) {
1920 pr_err("#%d\t0x%"PRIx64"\t%c\t%s\n",
1922 sym->binding == STB_GLOBAL ? 'g' :
1923 sym->binding == STB_LOCAL ? 'l' : 'w',
1928 pr_err("\t\twhich is near\t\t%s\n", sym->name);
1930 sym = dso__next_symbol(sym);
1933 pr_err("Disambiguate symbol name by inserting #n after the name e.g. %s #2\n",
1935 pr_err("Or select a global symbol by inserting #0 or #g or #G\n");
1938 static int find_dso_sym(struct dso *dso, const char *sym_name, u64 *start,
1947 sym = dso__first_symbol(dso, MAP__FUNCTION);
1951 *size = sym->start - *start;
1955 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1956 print_duplicate_syms(dso, sym_name);
1959 } else if (dso_sym_match(sym, sym_name, &cnt, idx)) {
1960 *start = sym->start;
1961 *size = sym->end - sym->start;
1963 sym = dso__next_symbol(sym);
1967 return sym_not_found_error(sym_name, idx);
1972 static int addr_filter__entire_dso(struct addr_filter *filt, struct dso *dso)
1974 struct symbol *first_sym = dso__first_symbol(dso, MAP__FUNCTION);
1975 struct symbol *last_sym = dso__last_symbol(dso, MAP__FUNCTION);
1977 if (!first_sym || !last_sym) {
1978 pr_err("Failed to determine filter for %s\nNo symbols found.\n",
1983 filt->addr = first_sym->start;
1984 filt->size = last_sym->end - first_sym->start;
1989 static int addr_filter__resolve_syms(struct addr_filter *filt)
1995 if (!filt->sym_from && !filt->sym_to)
1998 if (!filt->filename)
1999 return addr_filter__resolve_kernel_syms(filt);
2001 dso = load_dso(filt->filename);
2003 pr_err("Failed to load symbols from: %s\n", filt->filename);
2007 if (filt->sym_from && !strcmp(filt->sym_from, "*")) {
2008 err = addr_filter__entire_dso(filt, dso);
2012 if (filt->sym_from) {
2013 err = find_dso_sym(dso, filt->sym_from, &start, &size,
2014 filt->sym_from_idx);
2018 if (filt->range && !filt->size && !filt->sym_to)
2023 err = find_dso_sym(dso, filt->sym_to, &start, &size,
2028 err = check_end_after_start(filt, start, size);
2032 filt->size = start + size - filt->addr;
2041 static char *addr_filter__to_str(struct addr_filter *filt)
2043 char filename_buf[PATH_MAX];
2044 const char *at = "";
2045 const char *fn = "";
2049 if (filt->filename) {
2051 fn = realpath(filt->filename, filename_buf);
2057 err = asprintf(&filter, "%s 0x%"PRIx64"/0x%"PRIx64"%s%s",
2058 filt->action, filt->addr, filt->size, at, fn);
2060 err = asprintf(&filter, "%s 0x%"PRIx64"%s%s",
2061 filt->action, filt->addr, at, fn);
2064 return err < 0 ? NULL : filter;
2067 static int parse_addr_filter(struct perf_evsel *evsel, const char *filter,
2070 struct addr_filters filts;
2071 struct addr_filter *filt;
2074 addr_filters__init(&filts);
2076 err = addr_filters__parse_bare_filter(&filts, filter);
2080 if (filts.cnt > max_nr) {
2081 pr_err("Error: number of address filters (%d) exceeds maximum (%d)\n",
2087 list_for_each_entry(filt, &filts.head, list) {
2090 err = addr_filter__resolve_syms(filt);
2094 new_filter = addr_filter__to_str(filt);
2100 if (perf_evsel__append_addr_filter(evsel, new_filter)) {
2107 addr_filters__exit(&filts);
2110 pr_err("Failed to parse address filter: '%s'\n", filter);
2111 pr_err("Filter format is: filter|start|stop|tracestop <start symbol or address> [/ <end symbol or size>] [@<file name>]\n");
2112 pr_err("Where multiple filters are separated by space or comma.\n");
2118 static struct perf_pmu *perf_evsel__find_pmu(struct perf_evsel *evsel)
2120 struct perf_pmu *pmu = NULL;
2122 while ((pmu = perf_pmu__scan(pmu)) != NULL) {
2123 if (pmu->type == evsel->attr.type)
2130 static int perf_evsel__nr_addr_filter(struct perf_evsel *evsel)
2132 struct perf_pmu *pmu = perf_evsel__find_pmu(evsel);
2133 int nr_addr_filters = 0;
2138 perf_pmu__scan_file(pmu, "nr_addr_filters", "%d", &nr_addr_filters);
2140 return nr_addr_filters;
2143 int auxtrace_parse_filters(struct perf_evlist *evlist)
2145 struct perf_evsel *evsel;
2149 evlist__for_each_entry(evlist, evsel) {
2150 filter = evsel->filter;
2151 max_nr = perf_evsel__nr_addr_filter(evsel);
2152 if (!filter || !max_nr)
2154 evsel->filter = NULL;
2155 err = parse_addr_filter(evsel, filter, max_nr);
2159 pr_debug("Address filter: %s\n", evsel->filter);