2 * intel_pt.c: Intel Processor 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
20 #include <linux/kernel.h>
21 #include <linux/types.h>
36 #include "thread-stack.h"
38 #include "callchain.h"
46 #include "intel-pt-decoder/intel-pt-log.h"
47 #include "intel-pt-decoder/intel-pt-decoder.h"
48 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
49 #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
51 #define MAX_TIMESTAMP (~0ULL)
54 struct auxtrace auxtrace;
55 struct auxtrace_queues queues;
56 struct auxtrace_heap heap;
58 struct perf_session *session;
59 struct machine *machine;
60 struct perf_evsel *switch_evsel;
61 struct thread *unknown_thread;
62 bool timeless_decoding;
71 int have_sched_switch;
77 struct perf_tsc_conversion tc;
78 bool cap_user_time_zero;
80 struct itrace_synth_opts synth_opts;
82 bool sample_instructions;
83 u64 instructions_sample_type;
88 u64 branches_sample_type;
91 bool sample_transactions;
92 u64 transactions_sample_type;
96 u64 ptwrites_sample_type;
99 bool sample_pwr_events;
100 u64 pwr_events_sample_type;
114 unsigned max_non_turbo_ratio;
117 unsigned long num_events;
120 struct addr_filters filts;
124 INTEL_PT_SS_NOT_TRACING,
127 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
128 INTEL_PT_SS_EXPECTING_SWITCH_IP,
131 struct intel_pt_queue {
133 unsigned int queue_nr;
134 struct auxtrace_buffer *buffer;
135 struct auxtrace_buffer *old_buffer;
137 const struct intel_pt_state *state;
138 struct ip_callchain *chain;
139 struct branch_stack *last_branch;
140 struct branch_stack *last_branch_rb;
141 size_t last_branch_pos;
142 union perf_event *event_buf;
145 bool step_through_buffers;
146 bool use_buffer_pid_tid;
152 struct thread *thread;
160 char insn[INTEL_PT_INSN_BUF_SZ];
163 static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
164 unsigned char *buf, size_t len)
166 struct intel_pt_pkt packet;
169 char desc[INTEL_PT_PKT_DESC_MAX];
170 const char *color = PERF_COLOR_BLUE;
172 color_fprintf(stdout, color,
173 ". ... Intel Processor Trace data: size %zu bytes\n",
177 ret = intel_pt_get_packet(buf, len, &packet);
183 color_fprintf(stdout, color, " %08x: ", pos);
184 for (i = 0; i < pkt_len; i++)
185 color_fprintf(stdout, color, " %02x", buf[i]);
187 color_fprintf(stdout, color, " ");
189 ret = intel_pt_pkt_desc(&packet, desc,
190 INTEL_PT_PKT_DESC_MAX);
192 color_fprintf(stdout, color, " %s\n", desc);
194 color_fprintf(stdout, color, " Bad packet!\n");
202 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
206 intel_pt_dump(pt, buf, len);
209 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
210 struct auxtrace_buffer *b)
212 bool consecutive = false;
215 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
216 pt->have_tsc, &consecutive);
219 b->use_size = b->data + b->size - start;
221 if (b->use_size && consecutive)
222 b->consecutive = true;
226 /* This function assumes data is processed sequentially only */
227 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
229 struct intel_pt_queue *ptq = data;
230 struct auxtrace_buffer *buffer = ptq->buffer;
231 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
232 struct auxtrace_queue *queue;
240 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
242 buffer = auxtrace_buffer__next(queue, buffer);
245 auxtrace_buffer__drop_data(old_buffer);
250 ptq->buffer = buffer;
253 int fd = perf_data__fd(ptq->pt->session->data);
255 buffer->data = auxtrace_buffer__get_data(buffer, fd);
260 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
261 if (might_overlap && !buffer->consecutive && old_buffer &&
262 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
265 if (buffer->use_data) {
266 b->len = buffer->use_size;
267 b->buf = buffer->use_data;
269 b->len = buffer->size;
270 b->buf = buffer->data;
272 b->ref_timestamp = buffer->reference;
274 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
275 b->consecutive = false;
276 b->trace_nr = buffer->buffer_nr + 1;
278 b->consecutive = true;
281 if (ptq->step_through_buffers)
286 auxtrace_buffer__drop_data(old_buffer);
287 ptq->old_buffer = buffer;
289 auxtrace_buffer__drop_data(buffer);
290 return intel_pt_get_trace(b, data);
296 struct intel_pt_cache_entry {
297 struct auxtrace_cache_entry entry;
300 enum intel_pt_insn_op op;
301 enum intel_pt_insn_branch branch;
304 char insn[INTEL_PT_INSN_BUF_SZ];
307 static int intel_pt_config_div(const char *var, const char *value, void *data)
312 if (!strcmp(var, "intel-pt.cache-divisor")) {
313 val = strtol(value, NULL, 0);
314 if (val > 0 && val <= INT_MAX)
321 static int intel_pt_cache_divisor(void)
328 perf_config(intel_pt_config_div, &d);
336 static unsigned int intel_pt_cache_size(struct dso *dso,
337 struct machine *machine)
341 size = dso__data_size(dso, machine);
342 size /= intel_pt_cache_divisor();
345 if (size > (1 << 21))
347 return 32 - __builtin_clz(size);
350 static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
351 struct machine *machine)
353 struct auxtrace_cache *c;
356 if (dso->auxtrace_cache)
357 return dso->auxtrace_cache;
359 bits = intel_pt_cache_size(dso, machine);
361 /* Ignoring cache creation failure */
362 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
364 dso->auxtrace_cache = c;
369 static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
370 u64 offset, u64 insn_cnt, u64 byte_cnt,
371 struct intel_pt_insn *intel_pt_insn)
373 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
374 struct intel_pt_cache_entry *e;
380 e = auxtrace_cache__alloc_entry(c);
384 e->insn_cnt = insn_cnt;
385 e->byte_cnt = byte_cnt;
386 e->op = intel_pt_insn->op;
387 e->branch = intel_pt_insn->branch;
388 e->length = intel_pt_insn->length;
389 e->rel = intel_pt_insn->rel;
390 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
392 err = auxtrace_cache__add(c, offset, &e->entry);
394 auxtrace_cache__free_entry(c, e);
399 static struct intel_pt_cache_entry *
400 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
402 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
407 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
410 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
411 uint64_t *insn_cnt_ptr, uint64_t *ip,
412 uint64_t to_ip, uint64_t max_insn_cnt,
415 struct intel_pt_queue *ptq = data;
416 struct machine *machine = ptq->pt->machine;
417 struct thread *thread;
418 struct addr_location al;
419 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
423 u64 offset, start_offset, start_ip;
427 intel_pt_insn->length = 0;
429 if (to_ip && *ip == to_ip)
432 if (*ip >= ptq->pt->kernel_start)
433 cpumode = PERF_RECORD_MISC_KERNEL;
435 cpumode = PERF_RECORD_MISC_USER;
437 thread = ptq->thread;
439 if (cpumode != PERF_RECORD_MISC_KERNEL)
441 thread = ptq->pt->unknown_thread;
445 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
448 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
449 dso__data_status_seen(al.map->dso,
450 DSO_DATA_STATUS_SEEN_ITRACE))
453 offset = al.map->map_ip(al.map, *ip);
455 if (!to_ip && one_map) {
456 struct intel_pt_cache_entry *e;
458 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
460 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
461 *insn_cnt_ptr = e->insn_cnt;
463 intel_pt_insn->op = e->op;
464 intel_pt_insn->branch = e->branch;
465 intel_pt_insn->length = e->length;
466 intel_pt_insn->rel = e->rel;
467 memcpy(intel_pt_insn->buf, e->insn,
468 INTEL_PT_INSN_BUF_SZ);
469 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
474 start_offset = offset;
477 /* Load maps to ensure dso->is_64_bit has been updated */
480 x86_64 = al.map->dso->is_64_bit;
483 len = dso__data_read_offset(al.map->dso, machine,
485 INTEL_PT_INSN_BUF_SZ);
489 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
492 intel_pt_log_insn(intel_pt_insn, *ip);
496 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
499 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
502 *ip += intel_pt_insn->length;
504 if (to_ip && *ip == to_ip)
507 if (*ip >= al.map->end)
510 offset += intel_pt_insn->length;
515 *insn_cnt_ptr = insn_cnt;
521 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
525 struct intel_pt_cache_entry *e;
527 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
532 /* Ignore cache errors */
533 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
534 *ip - start_ip, intel_pt_insn);
539 *insn_cnt_ptr = insn_cnt;
543 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
544 uint64_t offset, const char *filename)
546 struct addr_filter *filt;
547 bool have_filter = false;
548 bool hit_tracestop = false;
549 bool hit_filter = false;
551 list_for_each_entry(filt, &pt->filts.head, list) {
555 if ((filename && !filt->filename) ||
556 (!filename && filt->filename) ||
557 (filename && strcmp(filename, filt->filename)))
560 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
563 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
564 ip, offset, filename ? filename : "[kernel]",
565 filt->start ? "filter" : "stop",
566 filt->addr, filt->size);
571 hit_tracestop = true;
574 if (!hit_tracestop && !hit_filter)
575 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
576 ip, offset, filename ? filename : "[kernel]");
578 return hit_tracestop || (have_filter && !hit_filter);
581 static int __intel_pt_pgd_ip(uint64_t ip, void *data)
583 struct intel_pt_queue *ptq = data;
584 struct thread *thread;
585 struct addr_location al;
589 if (ip >= ptq->pt->kernel_start)
590 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
592 cpumode = PERF_RECORD_MISC_USER;
594 thread = ptq->thread;
598 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
601 offset = al.map->map_ip(al.map, ip);
603 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
604 al.map->dso->long_name);
607 static bool intel_pt_pgd_ip(uint64_t ip, void *data)
609 return __intel_pt_pgd_ip(ip, data) > 0;
612 static bool intel_pt_get_config(struct intel_pt *pt,
613 struct perf_event_attr *attr, u64 *config)
615 if (attr->type == pt->pmu_type) {
617 *config = attr->config;
624 static bool intel_pt_exclude_kernel(struct intel_pt *pt)
626 struct perf_evsel *evsel;
628 evlist__for_each_entry(pt->session->evlist, evsel) {
629 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
630 !evsel->attr.exclude_kernel)
636 static bool intel_pt_return_compression(struct intel_pt *pt)
638 struct perf_evsel *evsel;
641 if (!pt->noretcomp_bit)
644 evlist__for_each_entry(pt->session->evlist, evsel) {
645 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
646 (config & pt->noretcomp_bit))
652 static bool intel_pt_branch_enable(struct intel_pt *pt)
654 struct perf_evsel *evsel;
657 evlist__for_each_entry(pt->session->evlist, evsel) {
658 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
659 (config & 1) && !(config & 0x2000))
665 static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
667 struct perf_evsel *evsel;
671 if (!pt->mtc_freq_bits)
674 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
677 evlist__for_each_entry(pt->session->evlist, evsel) {
678 if (intel_pt_get_config(pt, &evsel->attr, &config))
679 return (config & pt->mtc_freq_bits) >> shift;
684 static bool intel_pt_timeless_decoding(struct intel_pt *pt)
686 struct perf_evsel *evsel;
687 bool timeless_decoding = true;
690 if (!pt->tsc_bit || !pt->cap_user_time_zero)
693 evlist__for_each_entry(pt->session->evlist, evsel) {
694 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
696 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
697 if (config & pt->tsc_bit)
698 timeless_decoding = false;
703 return timeless_decoding;
706 static bool intel_pt_tracing_kernel(struct intel_pt *pt)
708 struct perf_evsel *evsel;
710 evlist__for_each_entry(pt->session->evlist, evsel) {
711 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
712 !evsel->attr.exclude_kernel)
718 static bool intel_pt_have_tsc(struct intel_pt *pt)
720 struct perf_evsel *evsel;
721 bool have_tsc = false;
727 evlist__for_each_entry(pt->session->evlist, evsel) {
728 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
729 if (config & pt->tsc_bit)
738 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
742 quot = ns / pt->tc.time_mult;
743 rem = ns % pt->tc.time_mult;
744 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
748 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
749 unsigned int queue_nr)
751 struct intel_pt_params params = { .get_trace = 0, };
752 struct perf_env *env = pt->machine->env;
753 struct intel_pt_queue *ptq;
755 ptq = zalloc(sizeof(struct intel_pt_queue));
759 if (pt->synth_opts.callchain) {
760 size_t sz = sizeof(struct ip_callchain);
762 sz += pt->synth_opts.callchain_sz * sizeof(u64);
763 ptq->chain = zalloc(sz);
768 if (pt->synth_opts.last_branch) {
769 size_t sz = sizeof(struct branch_stack);
771 sz += pt->synth_opts.last_branch_sz *
772 sizeof(struct branch_entry);
773 ptq->last_branch = zalloc(sz);
774 if (!ptq->last_branch)
776 ptq->last_branch_rb = zalloc(sz);
777 if (!ptq->last_branch_rb)
781 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
786 ptq->queue_nr = queue_nr;
787 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
793 params.get_trace = intel_pt_get_trace;
794 params.walk_insn = intel_pt_walk_next_insn;
796 params.return_compression = intel_pt_return_compression(pt);
797 params.branch_enable = intel_pt_branch_enable(pt);
798 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
799 params.mtc_period = intel_pt_mtc_period(pt);
800 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
801 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
803 if (pt->filts.cnt > 0)
804 params.pgd_ip = intel_pt_pgd_ip;
806 if (pt->synth_opts.instructions) {
807 if (pt->synth_opts.period) {
808 switch (pt->synth_opts.period_type) {
809 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
811 INTEL_PT_PERIOD_INSTRUCTIONS;
812 params.period = pt->synth_opts.period;
814 case PERF_ITRACE_PERIOD_TICKS:
815 params.period_type = INTEL_PT_PERIOD_TICKS;
816 params.period = pt->synth_opts.period;
818 case PERF_ITRACE_PERIOD_NANOSECS:
819 params.period_type = INTEL_PT_PERIOD_TICKS;
820 params.period = intel_pt_ns_to_ticks(pt,
821 pt->synth_opts.period);
828 if (!params.period) {
829 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
834 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
835 params.flags |= INTEL_PT_FUP_WITH_NLIP;
837 ptq->decoder = intel_pt_decoder_new(¶ms);
844 zfree(&ptq->event_buf);
845 zfree(&ptq->last_branch);
846 zfree(&ptq->last_branch_rb);
852 static void intel_pt_free_queue(void *priv)
854 struct intel_pt_queue *ptq = priv;
858 thread__zput(ptq->thread);
859 intel_pt_decoder_free(ptq->decoder);
860 zfree(&ptq->event_buf);
861 zfree(&ptq->last_branch);
862 zfree(&ptq->last_branch_rb);
867 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
868 struct auxtrace_queue *queue)
870 struct intel_pt_queue *ptq = queue->priv;
872 if (queue->tid == -1 || pt->have_sched_switch) {
873 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
874 thread__zput(ptq->thread);
877 if (!ptq->thread && ptq->tid != -1)
878 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
881 ptq->pid = ptq->thread->pid_;
882 if (queue->cpu == -1)
883 ptq->cpu = ptq->thread->cpu;
887 static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
889 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
890 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
891 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
892 if (ptq->state->to_ip)
893 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
895 PERF_IP_FLAG_INTERRUPT;
897 ptq->flags = PERF_IP_FLAG_BRANCH |
898 PERF_IP_FLAG_TRACE_END;
901 if (ptq->state->from_ip)
902 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
904 ptq->flags = PERF_IP_FLAG_BRANCH |
905 PERF_IP_FLAG_TRACE_BEGIN;
906 if (ptq->state->flags & INTEL_PT_IN_TX)
907 ptq->flags |= PERF_IP_FLAG_IN_TX;
908 ptq->insn_len = ptq->state->insn_len;
909 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
913 static int intel_pt_setup_queue(struct intel_pt *pt,
914 struct auxtrace_queue *queue,
915 unsigned int queue_nr)
917 struct intel_pt_queue *ptq = queue->priv;
919 if (list_empty(&queue->head))
923 ptq = intel_pt_alloc_queue(pt, queue_nr);
928 if (queue->cpu != -1)
929 ptq->cpu = queue->cpu;
930 ptq->tid = queue->tid;
932 if (pt->sampling_mode && !pt->snapshot_mode &&
933 pt->timeless_decoding)
934 ptq->step_through_buffers = true;
936 ptq->sync_switch = pt->sync_switch;
940 (!ptq->sync_switch ||
941 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
942 const struct intel_pt_state *state;
945 if (pt->timeless_decoding)
948 intel_pt_log("queue %u getting timestamp\n", queue_nr);
949 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
950 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
952 state = intel_pt_decode(ptq->decoder);
954 if (state->err == INTEL_PT_ERR_NODATA) {
955 intel_pt_log("queue %u has no timestamp\n",
961 if (state->timestamp)
965 ptq->timestamp = state->timestamp;
966 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
967 queue_nr, ptq->timestamp);
969 ptq->have_sample = true;
970 intel_pt_sample_flags(ptq);
971 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
980 static int intel_pt_setup_queues(struct intel_pt *pt)
985 for (i = 0; i < pt->queues.nr_queues; i++) {
986 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
993 static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
995 struct branch_stack *bs_src = ptq->last_branch_rb;
996 struct branch_stack *bs_dst = ptq->last_branch;
999 bs_dst->nr = bs_src->nr;
1004 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1005 memcpy(&bs_dst->entries[0],
1006 &bs_src->entries[ptq->last_branch_pos],
1007 sizeof(struct branch_entry) * nr);
1009 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1010 memcpy(&bs_dst->entries[nr],
1011 &bs_src->entries[0],
1012 sizeof(struct branch_entry) * ptq->last_branch_pos);
1016 static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1018 ptq->last_branch_pos = 0;
1019 ptq->last_branch_rb->nr = 0;
1022 static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1024 const struct intel_pt_state *state = ptq->state;
1025 struct branch_stack *bs = ptq->last_branch_rb;
1026 struct branch_entry *be;
1028 if (!ptq->last_branch_pos)
1029 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1031 ptq->last_branch_pos -= 1;
1033 be = &bs->entries[ptq->last_branch_pos];
1034 be->from = state->from_ip;
1035 be->to = state->to_ip;
1036 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1037 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1038 /* No support for mispredict */
1039 be->flags.mispred = ptq->pt->mispred_all;
1041 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1045 static inline bool intel_pt_skip_event(struct intel_pt *pt)
1047 return pt->synth_opts.initial_skip &&
1048 pt->num_events++ < pt->synth_opts.initial_skip;
1051 static void intel_pt_prep_b_sample(struct intel_pt *pt,
1052 struct intel_pt_queue *ptq,
1053 union perf_event *event,
1054 struct perf_sample *sample)
1056 event->sample.header.type = PERF_RECORD_SAMPLE;
1057 event->sample.header.misc = PERF_RECORD_MISC_USER;
1058 event->sample.header.size = sizeof(struct perf_event_header);
1060 if (!pt->timeless_decoding)
1061 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1063 sample->cpumode = PERF_RECORD_MISC_USER;
1064 sample->ip = ptq->state->from_ip;
1065 sample->pid = ptq->pid;
1066 sample->tid = ptq->tid;
1067 sample->addr = ptq->state->to_ip;
1069 sample->cpu = ptq->cpu;
1070 sample->flags = ptq->flags;
1071 sample->insn_len = ptq->insn_len;
1072 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1075 static int intel_pt_inject_event(union perf_event *event,
1076 struct perf_sample *sample, u64 type)
1078 event->header.size = perf_event__sample_event_size(sample, type, 0);
1079 return perf_event__synthesize_sample(event, type, 0, sample);
1082 static inline int intel_pt_opt_inject(struct intel_pt *pt,
1083 union perf_event *event,
1084 struct perf_sample *sample, u64 type)
1086 if (!pt->synth_opts.inject)
1089 return intel_pt_inject_event(event, sample, type);
1092 static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1093 union perf_event *event,
1094 struct perf_sample *sample, u64 type)
1098 ret = intel_pt_opt_inject(pt, event, sample, type);
1102 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1104 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1109 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1111 struct intel_pt *pt = ptq->pt;
1112 union perf_event *event = ptq->event_buf;
1113 struct perf_sample sample = { .ip = 0, };
1114 struct dummy_branch_stack {
1116 struct branch_entry entries;
1119 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1122 if (intel_pt_skip_event(pt))
1125 intel_pt_prep_b_sample(pt, ptq, event, &sample);
1127 sample.id = ptq->pt->branches_id;
1128 sample.stream_id = ptq->pt->branches_id;
1131 * perf report cannot handle events without a branch stack when using
1132 * SORT_MODE__BRANCH so make a dummy one.
1134 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1135 dummy_bs = (struct dummy_branch_stack){
1142 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1145 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1146 pt->branches_sample_type);
1149 static void intel_pt_prep_sample(struct intel_pt *pt,
1150 struct intel_pt_queue *ptq,
1151 union perf_event *event,
1152 struct perf_sample *sample)
1154 intel_pt_prep_b_sample(pt, ptq, event, sample);
1156 if (pt->synth_opts.callchain) {
1157 thread_stack__sample(ptq->thread, ptq->chain,
1158 pt->synth_opts.callchain_sz, sample->ip);
1159 sample->callchain = ptq->chain;
1162 if (pt->synth_opts.last_branch) {
1163 intel_pt_copy_last_branch_rb(ptq);
1164 sample->branch_stack = ptq->last_branch;
1168 static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1169 struct intel_pt_queue *ptq,
1170 union perf_event *event,
1171 struct perf_sample *sample,
1176 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1178 if (pt->synth_opts.last_branch)
1179 intel_pt_reset_last_branch_rb(ptq);
1184 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1186 struct intel_pt *pt = ptq->pt;
1187 union perf_event *event = ptq->event_buf;
1188 struct perf_sample sample = { .ip = 0, };
1190 if (intel_pt_skip_event(pt))
1193 intel_pt_prep_sample(pt, ptq, event, &sample);
1195 sample.id = ptq->pt->instructions_id;
1196 sample.stream_id = ptq->pt->instructions_id;
1197 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1199 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1201 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1202 pt->instructions_sample_type);
1205 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1207 struct intel_pt *pt = ptq->pt;
1208 union perf_event *event = ptq->event_buf;
1209 struct perf_sample sample = { .ip = 0, };
1211 if (intel_pt_skip_event(pt))
1214 intel_pt_prep_sample(pt, ptq, event, &sample);
1216 sample.id = ptq->pt->transactions_id;
1217 sample.stream_id = ptq->pt->transactions_id;
1219 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1220 pt->transactions_sample_type);
1223 static void intel_pt_prep_p_sample(struct intel_pt *pt,
1224 struct intel_pt_queue *ptq,
1225 union perf_event *event,
1226 struct perf_sample *sample)
1228 intel_pt_prep_sample(pt, ptq, event, sample);
1231 * Zero IP is used to mean "trace start" but that is not the case for
1232 * power or PTWRITE events with no IP, so clear the flags.
1238 static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1240 struct intel_pt *pt = ptq->pt;
1241 union perf_event *event = ptq->event_buf;
1242 struct perf_sample sample = { .ip = 0, };
1243 struct perf_synth_intel_ptwrite raw;
1245 if (intel_pt_skip_event(pt))
1248 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1250 sample.id = ptq->pt->ptwrites_id;
1251 sample.stream_id = ptq->pt->ptwrites_id;
1254 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1255 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1257 sample.raw_size = perf_synth__raw_size(raw);
1258 sample.raw_data = perf_synth__raw_data(&raw);
1260 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1261 pt->ptwrites_sample_type);
1264 static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1266 struct intel_pt *pt = ptq->pt;
1267 union perf_event *event = ptq->event_buf;
1268 struct perf_sample sample = { .ip = 0, };
1269 struct perf_synth_intel_cbr raw;
1272 if (intel_pt_skip_event(pt))
1275 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1277 sample.id = ptq->pt->cbr_id;
1278 sample.stream_id = ptq->pt->cbr_id;
1280 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1281 raw.flags = cpu_to_le32(flags);
1282 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1285 sample.raw_size = perf_synth__raw_size(raw);
1286 sample.raw_data = perf_synth__raw_data(&raw);
1288 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1289 pt->pwr_events_sample_type);
1292 static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1294 struct intel_pt *pt = ptq->pt;
1295 union perf_event *event = ptq->event_buf;
1296 struct perf_sample sample = { .ip = 0, };
1297 struct perf_synth_intel_mwait raw;
1299 if (intel_pt_skip_event(pt))
1302 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1304 sample.id = ptq->pt->mwait_id;
1305 sample.stream_id = ptq->pt->mwait_id;
1308 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1310 sample.raw_size = perf_synth__raw_size(raw);
1311 sample.raw_data = perf_synth__raw_data(&raw);
1313 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1314 pt->pwr_events_sample_type);
1317 static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1319 struct intel_pt *pt = ptq->pt;
1320 union perf_event *event = ptq->event_buf;
1321 struct perf_sample sample = { .ip = 0, };
1322 struct perf_synth_intel_pwre raw;
1324 if (intel_pt_skip_event(pt))
1327 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1329 sample.id = ptq->pt->pwre_id;
1330 sample.stream_id = ptq->pt->pwre_id;
1333 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1335 sample.raw_size = perf_synth__raw_size(raw);
1336 sample.raw_data = perf_synth__raw_data(&raw);
1338 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1339 pt->pwr_events_sample_type);
1342 static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1344 struct intel_pt *pt = ptq->pt;
1345 union perf_event *event = ptq->event_buf;
1346 struct perf_sample sample = { .ip = 0, };
1347 struct perf_synth_intel_exstop raw;
1349 if (intel_pt_skip_event(pt))
1352 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1354 sample.id = ptq->pt->exstop_id;
1355 sample.stream_id = ptq->pt->exstop_id;
1358 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1360 sample.raw_size = perf_synth__raw_size(raw);
1361 sample.raw_data = perf_synth__raw_data(&raw);
1363 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1364 pt->pwr_events_sample_type);
1367 static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1369 struct intel_pt *pt = ptq->pt;
1370 union perf_event *event = ptq->event_buf;
1371 struct perf_sample sample = { .ip = 0, };
1372 struct perf_synth_intel_pwrx raw;
1374 if (intel_pt_skip_event(pt))
1377 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1379 sample.id = ptq->pt->pwrx_id;
1380 sample.stream_id = ptq->pt->pwrx_id;
1383 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1385 sample.raw_size = perf_synth__raw_size(raw);
1386 sample.raw_data = perf_synth__raw_data(&raw);
1388 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1389 pt->pwr_events_sample_type);
1392 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1393 pid_t pid, pid_t tid, u64 ip)
1395 union perf_event event;
1396 char msg[MAX_AUXTRACE_ERROR_MSG];
1399 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1401 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1402 code, cpu, pid, tid, ip, msg);
1404 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1406 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1412 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1414 struct auxtrace_queue *queue;
1415 pid_t tid = ptq->next_tid;
1421 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1423 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1425 queue = &pt->queues.queue_array[ptq->queue_nr];
1426 intel_pt_set_pid_tid_cpu(pt, queue);
1433 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1435 struct intel_pt *pt = ptq->pt;
1437 return ip == pt->switch_ip &&
1438 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1439 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1440 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1443 #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
1444 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT | \
1447 static int intel_pt_sample(struct intel_pt_queue *ptq)
1449 const struct intel_pt_state *state = ptq->state;
1450 struct intel_pt *pt = ptq->pt;
1453 if (!ptq->have_sample)
1456 ptq->have_sample = false;
1458 if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) {
1459 if (state->type & INTEL_PT_CBR_CHG) {
1460 err = intel_pt_synth_cbr_sample(ptq);
1464 if (state->type & INTEL_PT_MWAIT_OP) {
1465 err = intel_pt_synth_mwait_sample(ptq);
1469 if (state->type & INTEL_PT_PWR_ENTRY) {
1470 err = intel_pt_synth_pwre_sample(ptq);
1474 if (state->type & INTEL_PT_EX_STOP) {
1475 err = intel_pt_synth_exstop_sample(ptq);
1479 if (state->type & INTEL_PT_PWR_EXIT) {
1480 err = intel_pt_synth_pwrx_sample(ptq);
1486 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
1487 err = intel_pt_synth_instruction_sample(ptq);
1492 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
1493 err = intel_pt_synth_transaction_sample(ptq);
1498 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
1499 err = intel_pt_synth_ptwrite_sample(ptq);
1504 if (!(state->type & INTEL_PT_BRANCH))
1507 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
1508 thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
1509 state->to_ip, ptq->insn_len,
1512 thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
1514 if (pt->sample_branches) {
1515 err = intel_pt_synth_branch_sample(ptq);
1520 if (pt->synth_opts.last_branch)
1521 intel_pt_update_last_branch_rb(ptq);
1523 if (!ptq->sync_switch)
1526 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1527 switch (ptq->switch_state) {
1528 case INTEL_PT_SS_NOT_TRACING:
1529 case INTEL_PT_SS_UNKNOWN:
1530 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1531 err = intel_pt_next_tid(pt, ptq);
1534 ptq->switch_state = INTEL_PT_SS_TRACING;
1537 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1540 } else if (!state->to_ip) {
1541 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1542 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1543 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1544 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1545 state->to_ip == pt->ptss_ip &&
1546 (ptq->flags & PERF_IP_FLAG_CALL)) {
1547 ptq->switch_state = INTEL_PT_SS_TRACING;
1553 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
1555 struct machine *machine = pt->machine;
1557 struct symbol *sym, *start;
1558 u64 ip, switch_ip = 0;
1564 map = machine__kernel_map(machine);
1571 start = dso__first_symbol(map->dso);
1573 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1574 if (sym->binding == STB_GLOBAL &&
1575 !strcmp(sym->name, "__switch_to")) {
1576 ip = map->unmap_ip(map, sym->start);
1577 if (ip >= map->start && ip < map->end) {
1584 if (!switch_ip || !ptss_ip)
1587 if (pt->have_sched_switch == 1)
1588 ptss = "perf_trace_sched_switch";
1590 ptss = "__perf_event_task_sched_out";
1592 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1593 if (!strcmp(sym->name, ptss)) {
1594 ip = map->unmap_ip(map, sym->start);
1595 if (ip >= map->start && ip < map->end) {
1605 static void intel_pt_enable_sync_switch(struct intel_pt *pt)
1609 pt->sync_switch = true;
1611 for (i = 0; i < pt->queues.nr_queues; i++) {
1612 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1613 struct intel_pt_queue *ptq = queue->priv;
1616 ptq->sync_switch = true;
1620 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
1622 const struct intel_pt_state *state = ptq->state;
1623 struct intel_pt *pt = ptq->pt;
1626 if (!pt->kernel_start) {
1627 pt->kernel_start = machine__kernel_start(pt->machine);
1628 if (pt->per_cpu_mmaps &&
1629 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
1630 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
1631 !pt->sampling_mode) {
1632 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
1633 if (pt->switch_ip) {
1634 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
1635 pt->switch_ip, pt->ptss_ip);
1636 intel_pt_enable_sync_switch(pt);
1641 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1642 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1644 err = intel_pt_sample(ptq);
1648 state = intel_pt_decode(ptq->decoder);
1650 if (state->err == INTEL_PT_ERR_NODATA)
1652 if (ptq->sync_switch &&
1653 state->from_ip >= pt->kernel_start) {
1654 ptq->sync_switch = false;
1655 intel_pt_next_tid(pt, ptq);
1657 if (pt->synth_opts.errors) {
1658 err = intel_pt_synth_error(pt, state->err,
1669 ptq->have_sample = true;
1670 intel_pt_sample_flags(ptq);
1672 /* Use estimated TSC upon return to user space */
1674 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
1675 state->to_ip && state->to_ip < pt->kernel_start) {
1676 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1677 state->timestamp, state->est_timestamp);
1678 ptq->timestamp = state->est_timestamp;
1679 /* Use estimated TSC in unknown switch state */
1680 } else if (ptq->sync_switch &&
1681 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1682 intel_pt_is_switch_ip(ptq, state->to_ip) &&
1683 ptq->next_tid == -1) {
1684 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1685 state->timestamp, state->est_timestamp);
1686 ptq->timestamp = state->est_timestamp;
1687 } else if (state->timestamp > ptq->timestamp) {
1688 ptq->timestamp = state->timestamp;
1691 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
1692 *timestamp = ptq->timestamp;
1699 static inline int intel_pt_update_queues(struct intel_pt *pt)
1701 if (pt->queues.new_data) {
1702 pt->queues.new_data = false;
1703 return intel_pt_setup_queues(pt);
1708 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
1710 unsigned int queue_nr;
1715 struct auxtrace_queue *queue;
1716 struct intel_pt_queue *ptq;
1718 if (!pt->heap.heap_cnt)
1721 if (pt->heap.heap_array[0].ordinal >= timestamp)
1724 queue_nr = pt->heap.heap_array[0].queue_nr;
1725 queue = &pt->queues.queue_array[queue_nr];
1728 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
1729 queue_nr, pt->heap.heap_array[0].ordinal,
1732 auxtrace_heap__pop(&pt->heap);
1734 if (pt->heap.heap_cnt) {
1735 ts = pt->heap.heap_array[0].ordinal + 1;
1742 intel_pt_set_pid_tid_cpu(pt, queue);
1744 ret = intel_pt_run_decoder(ptq, &ts);
1747 auxtrace_heap__add(&pt->heap, queue_nr, ts);
1752 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
1756 ptq->on_heap = false;
1763 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
1766 struct auxtrace_queues *queues = &pt->queues;
1770 for (i = 0; i < queues->nr_queues; i++) {
1771 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1772 struct intel_pt_queue *ptq = queue->priv;
1774 if (ptq && (tid == -1 || ptq->tid == tid)) {
1776 intel_pt_set_pid_tid_cpu(pt, queue);
1777 intel_pt_run_decoder(ptq, &ts);
1783 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
1785 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
1786 sample->pid, sample->tid, 0);
1789 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
1793 if (cpu < 0 || !pt->queues.nr_queues)
1796 if ((unsigned)cpu >= pt->queues.nr_queues)
1797 i = pt->queues.nr_queues - 1;
1801 if (pt->queues.queue_array[i].cpu == cpu)
1802 return pt->queues.queue_array[i].priv;
1804 for (j = 0; i > 0; j++) {
1805 if (pt->queues.queue_array[--i].cpu == cpu)
1806 return pt->queues.queue_array[i].priv;
1809 for (; j < pt->queues.nr_queues; j++) {
1810 if (pt->queues.queue_array[j].cpu == cpu)
1811 return pt->queues.queue_array[j].priv;
1817 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
1820 struct intel_pt_queue *ptq;
1823 if (!pt->sync_switch)
1826 ptq = intel_pt_cpu_to_ptq(pt, cpu);
1827 if (!ptq || !ptq->sync_switch)
1830 switch (ptq->switch_state) {
1831 case INTEL_PT_SS_NOT_TRACING:
1834 case INTEL_PT_SS_UNKNOWN:
1835 case INTEL_PT_SS_TRACING:
1836 ptq->next_tid = tid;
1837 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
1839 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
1840 if (!ptq->on_heap) {
1841 ptq->timestamp = perf_time_to_tsc(timestamp,
1843 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
1847 ptq->on_heap = true;
1849 ptq->switch_state = INTEL_PT_SS_TRACING;
1851 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1852 ptq->next_tid = tid;
1853 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
1862 static int intel_pt_process_switch(struct intel_pt *pt,
1863 struct perf_sample *sample)
1865 struct perf_evsel *evsel;
1869 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
1870 if (evsel != pt->switch_evsel)
1873 tid = perf_evsel__intval(evsel, sample, "next_pid");
1876 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1877 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
1880 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1884 return machine__set_current_tid(pt->machine, cpu, -1, tid);
1887 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
1888 struct perf_sample *sample)
1890 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1896 if (pt->have_sched_switch == 3) {
1899 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
1900 pr_err("Expecting CPU-wide context switch event\n");
1903 pid = event->context_switch.next_prev_pid;
1904 tid = event->context_switch.next_prev_tid;
1913 pr_err("context_switch event has no tid\n");
1917 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1918 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
1921 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1925 return machine__set_current_tid(pt->machine, cpu, pid, tid);
1928 static int intel_pt_process_itrace_start(struct intel_pt *pt,
1929 union perf_event *event,
1930 struct perf_sample *sample)
1932 if (!pt->per_cpu_mmaps)
1935 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1936 sample->cpu, event->itrace_start.pid,
1937 event->itrace_start.tid, sample->time,
1938 perf_time_to_tsc(sample->time, &pt->tc));
1940 return machine__set_current_tid(pt->machine, sample->cpu,
1941 event->itrace_start.pid,
1942 event->itrace_start.tid);
1945 static int intel_pt_process_event(struct perf_session *session,
1946 union perf_event *event,
1947 struct perf_sample *sample,
1948 struct perf_tool *tool)
1950 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1958 if (!tool->ordered_events) {
1959 pr_err("Intel Processor Trace requires ordered events\n");
1963 if (sample->time && sample->time != (u64)-1)
1964 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
1968 if (timestamp || pt->timeless_decoding) {
1969 err = intel_pt_update_queues(pt);
1974 if (pt->timeless_decoding) {
1975 if (event->header.type == PERF_RECORD_EXIT) {
1976 err = intel_pt_process_timeless_queues(pt,
1980 } else if (timestamp) {
1981 err = intel_pt_process_queues(pt, timestamp);
1986 if (event->header.type == PERF_RECORD_AUX &&
1987 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
1988 pt->synth_opts.errors) {
1989 err = intel_pt_lost(pt, sample);
1994 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
1995 err = intel_pt_process_switch(pt, sample);
1996 else if (event->header.type == PERF_RECORD_ITRACE_START)
1997 err = intel_pt_process_itrace_start(pt, event, sample);
1998 else if (event->header.type == PERF_RECORD_SWITCH ||
1999 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2000 err = intel_pt_context_switch(pt, event, sample);
2002 intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
2003 perf_event__name(event->header.type), event->header.type,
2004 sample->cpu, sample->time, timestamp);
2009 static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
2011 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2018 if (!tool->ordered_events)
2021 ret = intel_pt_update_queues(pt);
2025 if (pt->timeless_decoding)
2026 return intel_pt_process_timeless_queues(pt, -1,
2029 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
2032 static void intel_pt_free_events(struct perf_session *session)
2034 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2036 struct auxtrace_queues *queues = &pt->queues;
2039 for (i = 0; i < queues->nr_queues; i++) {
2040 intel_pt_free_queue(queues->queue_array[i].priv);
2041 queues->queue_array[i].priv = NULL;
2043 intel_pt_log_disable();
2044 auxtrace_queues__free(queues);
2047 static void intel_pt_free(struct perf_session *session)
2049 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2052 auxtrace_heap__free(&pt->heap);
2053 intel_pt_free_events(session);
2054 session->auxtrace = NULL;
2055 thread__put(pt->unknown_thread);
2056 addr_filters__exit(&pt->filts);
2061 static int intel_pt_process_auxtrace_event(struct perf_session *session,
2062 union perf_event *event,
2063 struct perf_tool *tool __maybe_unused)
2065 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2068 if (!pt->data_queued) {
2069 struct auxtrace_buffer *buffer;
2071 int fd = perf_data__fd(session->data);
2074 if (perf_data__is_pipe(session->data)) {
2077 data_offset = lseek(fd, 0, SEEK_CUR);
2078 if (data_offset == -1)
2082 err = auxtrace_queues__add_event(&pt->queues, session, event,
2083 data_offset, &buffer);
2087 /* Dump here now we have copied a piped trace out of the pipe */
2089 if (auxtrace_buffer__get_data(buffer, fd)) {
2090 intel_pt_dump_event(pt, buffer->data,
2092 auxtrace_buffer__put_data(buffer);
2100 struct intel_pt_synth {
2101 struct perf_tool dummy_tool;
2102 struct perf_session *session;
2105 static int intel_pt_event_synth(struct perf_tool *tool,
2106 union perf_event *event,
2107 struct perf_sample *sample __maybe_unused,
2108 struct machine *machine __maybe_unused)
2110 struct intel_pt_synth *intel_pt_synth =
2111 container_of(tool, struct intel_pt_synth, dummy_tool);
2113 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
2117 static int intel_pt_synth_event(struct perf_session *session, const char *name,
2118 struct perf_event_attr *attr, u64 id)
2120 struct intel_pt_synth intel_pt_synth;
2123 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2124 name, id, (u64)attr->sample_type);
2126 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
2127 intel_pt_synth.session = session;
2129 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
2130 &id, intel_pt_event_synth);
2132 pr_err("%s: failed to synthesize '%s' event type\n",
2138 static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id,
2141 struct perf_evsel *evsel;
2143 evlist__for_each_entry(evlist, evsel) {
2144 if (evsel->id && evsel->id[0] == id) {
2146 zfree(&evsel->name);
2147 evsel->name = strdup(name);
2153 static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt,
2154 struct perf_evlist *evlist)
2156 struct perf_evsel *evsel;
2158 evlist__for_each_entry(evlist, evsel) {
2159 if (evsel->attr.type == pt->pmu_type && evsel->ids)
2166 static int intel_pt_synth_events(struct intel_pt *pt,
2167 struct perf_session *session)
2169 struct perf_evlist *evlist = session->evlist;
2170 struct perf_evsel *evsel = intel_pt_evsel(pt, evlist);
2171 struct perf_event_attr attr;
2176 pr_debug("There are no selected events with Intel Processor Trace data\n");
2180 memset(&attr, 0, sizeof(struct perf_event_attr));
2181 attr.size = sizeof(struct perf_event_attr);
2182 attr.type = PERF_TYPE_HARDWARE;
2183 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
2184 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
2186 if (pt->timeless_decoding)
2187 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
2189 attr.sample_type |= PERF_SAMPLE_TIME;
2190 if (!pt->per_cpu_mmaps)
2191 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
2192 attr.exclude_user = evsel->attr.exclude_user;
2193 attr.exclude_kernel = evsel->attr.exclude_kernel;
2194 attr.exclude_hv = evsel->attr.exclude_hv;
2195 attr.exclude_host = evsel->attr.exclude_host;
2196 attr.exclude_guest = evsel->attr.exclude_guest;
2197 attr.sample_id_all = evsel->attr.sample_id_all;
2198 attr.read_format = evsel->attr.read_format;
2200 id = evsel->id[0] + 1000000000;
2204 if (pt->synth_opts.branches) {
2205 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2206 attr.sample_period = 1;
2207 attr.sample_type |= PERF_SAMPLE_ADDR;
2208 err = intel_pt_synth_event(session, "branches", &attr, id);
2211 pt->sample_branches = true;
2212 pt->branches_sample_type = attr.sample_type;
2213 pt->branches_id = id;
2215 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
2218 if (pt->synth_opts.callchain)
2219 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2220 if (pt->synth_opts.last_branch)
2221 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2223 if (pt->synth_opts.instructions) {
2224 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2225 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2226 attr.sample_period =
2227 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2229 attr.sample_period = pt->synth_opts.period;
2230 err = intel_pt_synth_event(session, "instructions", &attr, id);
2233 pt->sample_instructions = true;
2234 pt->instructions_sample_type = attr.sample_type;
2235 pt->instructions_id = id;
2239 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
2240 attr.sample_period = 1;
2242 if (pt->synth_opts.transactions) {
2243 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2244 err = intel_pt_synth_event(session, "transactions", &attr, id);
2247 pt->sample_transactions = true;
2248 pt->transactions_sample_type = attr.sample_type;
2249 pt->transactions_id = id;
2250 intel_pt_set_event_name(evlist, id, "transactions");
2254 attr.type = PERF_TYPE_SYNTH;
2255 attr.sample_type |= PERF_SAMPLE_RAW;
2257 if (pt->synth_opts.ptwrites) {
2258 attr.config = PERF_SYNTH_INTEL_PTWRITE;
2259 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
2262 pt->sample_ptwrites = true;
2263 pt->ptwrites_sample_type = attr.sample_type;
2264 pt->ptwrites_id = id;
2265 intel_pt_set_event_name(evlist, id, "ptwrite");
2269 if (pt->synth_opts.pwr_events) {
2270 pt->sample_pwr_events = true;
2271 pt->pwr_events_sample_type = attr.sample_type;
2273 attr.config = PERF_SYNTH_INTEL_CBR;
2274 err = intel_pt_synth_event(session, "cbr", &attr, id);
2278 intel_pt_set_event_name(evlist, id, "cbr");
2282 if (pt->synth_opts.pwr_events && (evsel->attr.config & 0x10)) {
2283 attr.config = PERF_SYNTH_INTEL_MWAIT;
2284 err = intel_pt_synth_event(session, "mwait", &attr, id);
2288 intel_pt_set_event_name(evlist, id, "mwait");
2291 attr.config = PERF_SYNTH_INTEL_PWRE;
2292 err = intel_pt_synth_event(session, "pwre", &attr, id);
2296 intel_pt_set_event_name(evlist, id, "pwre");
2299 attr.config = PERF_SYNTH_INTEL_EXSTOP;
2300 err = intel_pt_synth_event(session, "exstop", &attr, id);
2304 intel_pt_set_event_name(evlist, id, "exstop");
2307 attr.config = PERF_SYNTH_INTEL_PWRX;
2308 err = intel_pt_synth_event(session, "pwrx", &attr, id);
2312 intel_pt_set_event_name(evlist, id, "pwrx");
2319 static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2321 struct perf_evsel *evsel;
2323 evlist__for_each_entry_reverse(evlist, evsel) {
2324 const char *name = perf_evsel__name(evsel);
2326 if (!strcmp(name, "sched:sched_switch"))
2333 static bool intel_pt_find_switch(struct perf_evlist *evlist)
2335 struct perf_evsel *evsel;
2337 evlist__for_each_entry(evlist, evsel) {
2338 if (evsel->attr.context_switch)
2345 static int intel_pt_perf_config(const char *var, const char *value, void *data)
2347 struct intel_pt *pt = data;
2349 if (!strcmp(var, "intel-pt.mispred-all"))
2350 pt->mispred_all = perf_config_bool(var, value);
2355 static const char * const intel_pt_info_fmts[] = {
2356 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2357 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2358 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2359 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
2360 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
2361 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
2362 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
2363 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
2364 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
2365 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
2366 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
2367 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
2368 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
2369 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
2370 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
2371 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
2374 static void intel_pt_print_info(u64 *arr, int start, int finish)
2381 for (i = start; i <= finish; i++)
2382 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
2385 static void intel_pt_print_info_str(const char *name, const char *str)
2390 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
2393 static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
2395 return auxtrace_info->header.size >=
2396 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
2399 int intel_pt_process_auxtrace_info(union perf_event *event,
2400 struct perf_session *session)
2402 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2403 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
2404 struct intel_pt *pt;
2409 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
2413 pt = zalloc(sizeof(struct intel_pt));
2417 addr_filters__init(&pt->filts);
2419 err = perf_config(intel_pt_perf_config, pt);
2423 err = auxtrace_queues__init(&pt->queues);
2427 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
2429 pt->session = session;
2430 pt->machine = &session->machines.host; /* No kvm support */
2431 pt->auxtrace_type = auxtrace_info->type;
2432 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
2433 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
2434 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
2435 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
2436 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
2437 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
2438 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
2439 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
2440 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
2441 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
2442 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
2443 INTEL_PT_PER_CPU_MMAPS);
2445 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
2446 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
2447 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
2448 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
2449 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
2450 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
2451 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
2455 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
2456 pt->max_non_turbo_ratio =
2457 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
2458 intel_pt_print_info(&auxtrace_info->priv[0],
2459 INTEL_PT_MAX_NONTURBO_RATIO,
2460 INTEL_PT_MAX_NONTURBO_RATIO);
2463 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
2464 info_end = (void *)info + auxtrace_info->header.size;
2466 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
2469 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
2470 intel_pt_print_info(&auxtrace_info->priv[0],
2471 INTEL_PT_FILTER_STR_LEN,
2472 INTEL_PT_FILTER_STR_LEN);
2474 const char *filter = (const char *)info;
2476 len = roundup(len + 1, 8);
2478 if ((void *)info > info_end) {
2479 pr_err("%s: bad filter string length\n", __func__);
2481 goto err_free_queues;
2483 pt->filter = memdup(filter, len);
2486 goto err_free_queues;
2488 if (session->header.needs_swap)
2489 mem_bswap_64(pt->filter, len);
2490 if (pt->filter[len - 1]) {
2491 pr_err("%s: filter string not null terminated\n", __func__);
2493 goto err_free_queues;
2495 err = addr_filters__parse_bare_filter(&pt->filts,
2498 goto err_free_queues;
2500 intel_pt_print_info_str("Filter string", pt->filter);
2503 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
2504 pt->have_tsc = intel_pt_have_tsc(pt);
2505 pt->sampling_mode = false;
2506 pt->est_tsc = !pt->timeless_decoding;
2508 pt->unknown_thread = thread__new(999999999, 999999999);
2509 if (!pt->unknown_thread) {
2511 goto err_free_queues;
2515 * Since this thread will not be kept in any rbtree not in a
2516 * list, initialize its list node so that at thread__put() the
2517 * current thread lifetime assuption is kept and we don't segfault
2518 * at list_del_init().
2520 INIT_LIST_HEAD(&pt->unknown_thread->node);
2522 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
2524 goto err_delete_thread;
2525 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
2527 goto err_delete_thread;
2530 pt->auxtrace.process_event = intel_pt_process_event;
2531 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
2532 pt->auxtrace.flush_events = intel_pt_flush;
2533 pt->auxtrace.free_events = intel_pt_free_events;
2534 pt->auxtrace.free = intel_pt_free;
2535 session->auxtrace = &pt->auxtrace;
2540 if (pt->have_sched_switch == 1) {
2541 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
2542 if (!pt->switch_evsel) {
2543 pr_err("%s: missing sched_switch event\n", __func__);
2545 goto err_delete_thread;
2547 } else if (pt->have_sched_switch == 2 &&
2548 !intel_pt_find_switch(session->evlist)) {
2549 pr_err("%s: missing context_switch attribute flag\n", __func__);
2551 goto err_delete_thread;
2554 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2555 pt->synth_opts = *session->itrace_synth_opts;
2557 itrace_synth_opts__set_default(&pt->synth_opts);
2558 if (use_browser != -1) {
2559 pt->synth_opts.branches = false;
2560 pt->synth_opts.callchain = true;
2562 if (session->itrace_synth_opts)
2563 pt->synth_opts.thread_stack =
2564 session->itrace_synth_opts->thread_stack;
2567 if (pt->synth_opts.log)
2568 intel_pt_log_enable();
2570 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
2571 if (pt->tc.time_mult) {
2572 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
2574 if (!pt->max_non_turbo_ratio)
2575 pt->max_non_turbo_ratio =
2576 (tsc_freq + 50000000) / 100000000;
2577 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
2578 intel_pt_log("Maximum non-turbo ratio %u\n",
2579 pt->max_non_turbo_ratio);
2580 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
2583 if (pt->synth_opts.calls)
2584 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
2585 PERF_IP_FLAG_TRACE_END;
2586 if (pt->synth_opts.returns)
2587 pt->branches_filter |= PERF_IP_FLAG_RETURN |
2588 PERF_IP_FLAG_TRACE_BEGIN;
2590 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
2591 symbol_conf.use_callchain = true;
2592 if (callchain_register_param(&callchain_param) < 0) {
2593 symbol_conf.use_callchain = false;
2594 pt->synth_opts.callchain = false;
2598 err = intel_pt_synth_events(pt, session);
2600 goto err_delete_thread;
2602 err = auxtrace_queues__process_index(&pt->queues, session);
2604 goto err_delete_thread;
2606 if (pt->queues.populated)
2607 pt->data_queued = true;
2609 if (pt->timeless_decoding)
2610 pr_debug2("Intel PT decoding without timestamps\n");
2615 thread__zput(pt->unknown_thread);
2617 intel_pt_log_disable();
2618 auxtrace_queues__free(&pt->queues);
2619 session->auxtrace = NULL;
2621 addr_filters__exit(&pt->filts);