1 // SPDX-License-Identifier: GPL-2.0-only
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
11 #include <linux/kernel.h>
12 #include <linux/string.h>
13 #include <linux/types.h>
14 #include <linux/zalloc.h>
27 #include "thread-stack.h"
29 #include "callchain.h"
36 #include "util/perf_api_probe.h"
37 #include "util/synthetic-events.h"
38 #include "time-utils.h"
40 #include "../arch/x86/include/uapi/asm/perf_regs.h"
42 #include "intel-pt-decoder/intel-pt-log.h"
43 #include "intel-pt-decoder/intel-pt-decoder.h"
44 #include "intel-pt-decoder/intel-pt-insn-decoder.h"
45 #include "intel-pt-decoder/intel-pt-pkt-decoder.h"
47 #define MAX_TIMESTAMP (~0ULL)
49 #define INTEL_PT_CFG_PASS_THRU BIT_ULL(0)
50 #define INTEL_PT_CFG_PWR_EVT_EN BIT_ULL(4)
51 #define INTEL_PT_CFG_BRANCH_EN BIT_ULL(13)
52 #define INTEL_PT_CFG_EVT_EN BIT_ULL(31)
53 #define INTEL_PT_CFG_TNT_DIS BIT_ULL(55)
61 struct auxtrace auxtrace;
62 struct auxtrace_queues queues;
63 struct auxtrace_heap heap;
65 struct perf_session *session;
66 struct machine *machine;
67 struct evsel *switch_evsel;
68 struct thread *unknown_thread;
69 bool timeless_decoding;
78 bool use_thread_stack;
81 unsigned int br_stack_sz;
82 unsigned int br_stack_sz_plus;
83 int have_sched_switch;
90 struct perf_tsc_conversion tc;
91 bool cap_user_time_zero;
93 struct itrace_synth_opts synth_opts;
95 bool sample_instructions;
96 u64 instructions_sample_type;
101 u64 branches_sample_type;
104 bool sample_transactions;
105 u64 transactions_sample_type;
108 bool sample_ptwrites;
109 u64 ptwrites_sample_type;
112 bool sample_pwr_events;
113 u64 pwr_events_sample_type;
123 struct evsel *pebs_evsel;
128 u64 iflag_chg_sample_type;
138 unsigned max_non_turbo_ratio;
142 unsigned long num_events;
145 struct addr_filters filts;
147 struct range *time_ranges;
148 unsigned int range_cnt;
150 struct ip_callchain *chain;
151 struct branch_stack *br_stack;
154 struct rb_root vmcs_info;
158 INTEL_PT_SS_NOT_TRACING,
161 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
162 INTEL_PT_SS_EXPECTING_SWITCH_IP,
165 /* applicable_counters is 64-bits */
166 #define INTEL_PT_MAX_PEBS 64
168 struct intel_pt_pebs_event {
173 struct intel_pt_queue {
175 unsigned int queue_nr;
176 struct auxtrace_buffer *buffer;
177 struct auxtrace_buffer *old_buffer;
179 const struct intel_pt_state *state;
180 struct ip_callchain *chain;
181 struct branch_stack *last_branch;
182 union perf_event *event_buf;
185 bool step_through_buffers;
186 bool use_buffer_pid_tid;
193 struct thread *thread;
194 struct machine *guest_machine;
195 struct thread *guest_thread;
196 struct thread *unknown_guest_thread;
197 pid_t guest_machine_pid;
204 unsigned int sel_idx;
210 u64 last_in_insn_cnt;
212 u64 last_br_insn_cnt;
214 unsigned int cbr_seen;
215 char insn[INTEL_PT_INSN_BUF_SZ];
216 struct intel_pt_pebs_event pebs[INTEL_PT_MAX_PEBS];
219 static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
220 unsigned char *buf, size_t len)
222 struct intel_pt_pkt packet;
225 char desc[INTEL_PT_PKT_DESC_MAX];
226 const char *color = PERF_COLOR_BLUE;
227 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
229 color_fprintf(stdout, color,
230 ". ... Intel Processor Trace data: size %zu bytes\n",
234 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
240 color_fprintf(stdout, color, " %08x: ", pos);
241 for (i = 0; i < pkt_len; i++)
242 color_fprintf(stdout, color, " %02x", buf[i]);
244 color_fprintf(stdout, color, " ");
246 ret = intel_pt_pkt_desc(&packet, desc,
247 INTEL_PT_PKT_DESC_MAX);
249 color_fprintf(stdout, color, " %s\n", desc);
251 color_fprintf(stdout, color, " Bad packet!\n");
259 static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
263 intel_pt_dump(pt, buf, len);
266 static void intel_pt_log_event(union perf_event *event)
268 FILE *f = intel_pt_log_fp();
270 if (!intel_pt_enable_logging || !f)
273 perf_event__fprintf(event, NULL, f);
276 static void intel_pt_dump_sample(struct perf_session *session,
277 struct perf_sample *sample)
279 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
283 intel_pt_dump(pt, sample->aux_sample.data, sample->aux_sample.size);
286 static bool intel_pt_log_events(struct intel_pt *pt, u64 tm)
288 struct perf_time_interval *range = pt->synth_opts.ptime_range;
289 int n = pt->synth_opts.range_num;
291 if (pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
294 if (pt->synth_opts.log_minus_flags & AUXTRACE_LOG_FLG_ALL_PERF_EVTS)
297 /* perf_time__ranges_skip_sample does not work if time is zero */
301 return !n || !perf_time__ranges_skip_sample(range, n, tm);
304 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs(struct rb_root *rb_root,
308 struct rb_node **p = &rb_root->rb_node;
309 struct rb_node *parent = NULL;
310 struct intel_pt_vmcs_info *v;
314 v = rb_entry(parent, struct intel_pt_vmcs_info, rb_node);
325 v = zalloc(sizeof(*v));
328 v->tsc_offset = dflt_tsc_offset;
329 v->reliable = dflt_tsc_offset;
331 rb_link_node(&v->rb_node, parent, p);
332 rb_insert_color(&v->rb_node, rb_root);
338 static struct intel_pt_vmcs_info *intel_pt_findnew_vmcs_info(void *data, uint64_t vmcs)
340 struct intel_pt_queue *ptq = data;
341 struct intel_pt *pt = ptq->pt;
343 if (!vmcs && !pt->dflt_tsc_offset)
346 return intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, pt->dflt_tsc_offset);
349 static void intel_pt_free_vmcs_info(struct intel_pt *pt)
351 struct intel_pt_vmcs_info *v;
354 n = rb_first(&pt->vmcs_info);
356 v = rb_entry(n, struct intel_pt_vmcs_info, rb_node);
358 rb_erase(&v->rb_node, &pt->vmcs_info);
363 static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
364 struct auxtrace_buffer *b)
366 bool consecutive = false;
369 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
370 pt->have_tsc, &consecutive,
371 pt->synth_opts.vm_time_correlation);
375 * In the case of vm_time_correlation, the overlap might contain TSC
376 * packets that will not be fixed, and that will then no longer work for
377 * overlap detection. Avoid that by zeroing out the overlap.
379 if (pt->synth_opts.vm_time_correlation)
380 memset(b->data, 0, start - b->data);
381 b->use_size = b->data + b->size - start;
383 if (b->use_size && consecutive)
384 b->consecutive = true;
388 static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
389 struct auxtrace_buffer *buffer,
390 struct auxtrace_buffer *old_buffer,
391 struct intel_pt_buffer *b)
396 int fd = perf_data__fd(ptq->pt->session->data);
398 buffer->data = auxtrace_buffer__get_data(buffer, fd);
403 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
404 if (might_overlap && !buffer->consecutive && old_buffer &&
405 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
408 if (buffer->use_data) {
409 b->len = buffer->use_size;
410 b->buf = buffer->use_data;
412 b->len = buffer->size;
413 b->buf = buffer->data;
415 b->ref_timestamp = buffer->reference;
417 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
418 b->consecutive = false;
419 b->trace_nr = buffer->buffer_nr + 1;
421 b->consecutive = true;
427 /* Do not drop buffers with references - refer intel_pt_get_trace() */
428 static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
429 struct auxtrace_buffer *buffer)
431 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
434 auxtrace_buffer__drop_data(buffer);
437 /* Must be serialized with respect to intel_pt_get_trace() */
438 static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
441 struct intel_pt_queue *ptq = data;
442 struct auxtrace_buffer *buffer = ptq->buffer;
443 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
444 struct auxtrace_queue *queue;
447 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
450 struct intel_pt_buffer b = { .len = 0 };
452 buffer = auxtrace_buffer__next(queue, buffer);
456 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
461 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
464 intel_pt_lookahead_drop_buffer(ptq, buffer);
468 err = cb(&b, cb_data);
473 if (buffer != old_buffer)
474 intel_pt_lookahead_drop_buffer(ptq, buffer);
475 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
481 * This function assumes data is processed sequentially only.
482 * Must be serialized with respect to intel_pt_lookahead()
484 static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
486 struct intel_pt_queue *ptq = data;
487 struct auxtrace_buffer *buffer = ptq->buffer;
488 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
489 struct auxtrace_queue *queue;
497 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
499 buffer = auxtrace_buffer__next(queue, buffer);
502 auxtrace_buffer__drop_data(old_buffer);
507 ptq->buffer = buffer;
509 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
513 if (ptq->step_through_buffers)
518 auxtrace_buffer__drop_data(old_buffer);
519 ptq->old_buffer = buffer;
521 auxtrace_buffer__drop_data(buffer);
522 return intel_pt_get_trace(b, data);
528 struct intel_pt_cache_entry {
529 struct auxtrace_cache_entry entry;
532 enum intel_pt_insn_op op;
533 enum intel_pt_insn_branch branch;
534 bool emulated_ptwrite;
537 char insn[INTEL_PT_INSN_BUF_SZ];
540 static int intel_pt_config_div(const char *var, const char *value, void *data)
545 if (!strcmp(var, "intel-pt.cache-divisor")) {
546 val = strtol(value, NULL, 0);
547 if (val > 0 && val <= INT_MAX)
554 static int intel_pt_cache_divisor(void)
561 perf_config(intel_pt_config_div, &d);
569 static unsigned int intel_pt_cache_size(struct dso *dso,
570 struct machine *machine)
574 size = dso__data_size(dso, machine);
575 size /= intel_pt_cache_divisor();
578 if (size > (1 << 21))
580 return 32 - __builtin_clz(size);
583 static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
584 struct machine *machine)
586 struct auxtrace_cache *c;
589 if (dso->auxtrace_cache)
590 return dso->auxtrace_cache;
592 bits = intel_pt_cache_size(dso, machine);
594 /* Ignoring cache creation failure */
595 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
597 dso->auxtrace_cache = c;
602 static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
603 u64 offset, u64 insn_cnt, u64 byte_cnt,
604 struct intel_pt_insn *intel_pt_insn)
606 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
607 struct intel_pt_cache_entry *e;
613 e = auxtrace_cache__alloc_entry(c);
617 e->insn_cnt = insn_cnt;
618 e->byte_cnt = byte_cnt;
619 e->op = intel_pt_insn->op;
620 e->branch = intel_pt_insn->branch;
621 e->emulated_ptwrite = intel_pt_insn->emulated_ptwrite;
622 e->length = intel_pt_insn->length;
623 e->rel = intel_pt_insn->rel;
624 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
626 err = auxtrace_cache__add(c, offset, &e->entry);
628 auxtrace_cache__free_entry(c, e);
633 static struct intel_pt_cache_entry *
634 intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
636 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
641 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
644 static void intel_pt_cache_invalidate(struct dso *dso, struct machine *machine,
647 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
652 auxtrace_cache__remove(dso->auxtrace_cache, offset);
655 static inline bool intel_pt_guest_kernel_ip(uint64_t ip)
657 /* Assumes 64-bit kernel */
658 return ip & (1ULL << 63);
661 static inline u8 intel_pt_nr_cpumode(struct intel_pt_queue *ptq, uint64_t ip, bool nr)
664 return intel_pt_guest_kernel_ip(ip) ?
665 PERF_RECORD_MISC_GUEST_KERNEL :
666 PERF_RECORD_MISC_GUEST_USER;
669 return ip >= ptq->pt->kernel_start ?
670 PERF_RECORD_MISC_KERNEL :
671 PERF_RECORD_MISC_USER;
674 static inline u8 intel_pt_cpumode(struct intel_pt_queue *ptq, uint64_t from_ip, uint64_t to_ip)
676 /* No support for non-zero CS base */
678 return intel_pt_nr_cpumode(ptq, from_ip, ptq->state->from_nr);
679 return intel_pt_nr_cpumode(ptq, to_ip, ptq->state->to_nr);
682 static int intel_pt_get_guest(struct intel_pt_queue *ptq)
684 struct machines *machines = &ptq->pt->session->machines;
685 struct machine *machine;
686 pid_t pid = ptq->pid <= 0 ? DEFAULT_GUEST_KERNEL_ID : ptq->pid;
688 if (ptq->guest_machine && pid == ptq->guest_machine_pid)
691 ptq->guest_machine = NULL;
692 thread__zput(ptq->unknown_guest_thread);
694 if (symbol_conf.guest_code) {
695 thread__zput(ptq->guest_thread);
696 ptq->guest_thread = machines__findnew_guest_code(machines, pid);
699 machine = machines__find_guest(machines, pid);
703 ptq->unknown_guest_thread = machine__idle_thread(machine);
704 if (!ptq->unknown_guest_thread)
707 ptq->guest_machine = machine;
708 ptq->guest_machine_pid = pid;
713 static inline bool intel_pt_jmp_16(struct intel_pt_insn *intel_pt_insn)
715 return intel_pt_insn->rel == 16 && intel_pt_insn->branch == INTEL_PT_BR_UNCONDITIONAL;
718 #define PTWRITE_MAGIC "\x0f\x0bperf,ptwrite "
719 #define PTWRITE_MAGIC_LEN 16
721 static bool intel_pt_emulated_ptwrite(struct dso *dso, struct machine *machine, u64 offset)
723 unsigned char buf[PTWRITE_MAGIC_LEN];
726 len = dso__data_read_offset(dso, machine, offset, buf, PTWRITE_MAGIC_LEN);
727 if (len == PTWRITE_MAGIC_LEN && !memcmp(buf, PTWRITE_MAGIC, PTWRITE_MAGIC_LEN)) {
728 intel_pt_log("Emulated ptwrite signature found\n");
731 intel_pt_log("Emulated ptwrite signature not found\n");
735 static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
736 uint64_t *insn_cnt_ptr, uint64_t *ip,
737 uint64_t to_ip, uint64_t max_insn_cnt,
740 struct intel_pt_queue *ptq = data;
741 struct machine *machine = ptq->pt->machine;
742 struct thread *thread;
743 struct addr_location al;
744 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
748 u64 offset, start_offset, start_ip;
753 intel_pt_insn->length = 0;
755 if (to_ip && *ip == to_ip)
758 nr = ptq->state->to_nr;
759 cpumode = intel_pt_nr_cpumode(ptq, *ip, nr);
762 if ((!symbol_conf.guest_code && cpumode != PERF_RECORD_MISC_GUEST_KERNEL) ||
763 intel_pt_get_guest(ptq))
765 machine = ptq->guest_machine;
766 thread = ptq->guest_thread;
768 if (cpumode != PERF_RECORD_MISC_GUEST_KERNEL)
770 thread = ptq->unknown_guest_thread;
773 thread = ptq->thread;
775 if (cpumode != PERF_RECORD_MISC_KERNEL)
777 thread = ptq->pt->unknown_thread;
782 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
785 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
786 dso__data_status_seen(al.map->dso,
787 DSO_DATA_STATUS_SEEN_ITRACE))
790 offset = al.map->map_ip(al.map, *ip);
792 if (!to_ip && one_map) {
793 struct intel_pt_cache_entry *e;
795 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
797 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
798 *insn_cnt_ptr = e->insn_cnt;
800 intel_pt_insn->op = e->op;
801 intel_pt_insn->branch = e->branch;
802 intel_pt_insn->emulated_ptwrite = e->emulated_ptwrite;
803 intel_pt_insn->length = e->length;
804 intel_pt_insn->rel = e->rel;
805 memcpy(intel_pt_insn->buf, e->insn,
806 INTEL_PT_INSN_BUF_SZ);
807 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
812 start_offset = offset;
815 /* Load maps to ensure dso->is_64_bit has been updated */
818 x86_64 = al.map->dso->is_64_bit;
821 len = dso__data_read_offset(al.map->dso, machine,
823 INTEL_PT_INSN_BUF_SZ);
827 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
830 intel_pt_log_insn(intel_pt_insn, *ip);
834 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH) {
838 if (!intel_pt_jmp_16(intel_pt_insn))
840 /* Check for emulated ptwrite */
841 offs = offset + intel_pt_insn->length;
842 eptw = intel_pt_emulated_ptwrite(al.map->dso, machine, offs);
843 intel_pt_insn->emulated_ptwrite = eptw;
847 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
850 *ip += intel_pt_insn->length;
852 if (to_ip && *ip == to_ip) {
853 intel_pt_insn->length = 0;
857 if (*ip >= al.map->end)
860 offset += intel_pt_insn->length;
865 *insn_cnt_ptr = insn_cnt;
871 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
875 struct intel_pt_cache_entry *e;
877 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
882 /* Ignore cache errors */
883 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
884 *ip - start_ip, intel_pt_insn);
889 *insn_cnt_ptr = insn_cnt;
893 static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
894 uint64_t offset, const char *filename)
896 struct addr_filter *filt;
897 bool have_filter = false;
898 bool hit_tracestop = false;
899 bool hit_filter = false;
901 list_for_each_entry(filt, &pt->filts.head, list) {
905 if ((filename && !filt->filename) ||
906 (!filename && filt->filename) ||
907 (filename && strcmp(filename, filt->filename)))
910 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
913 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
914 ip, offset, filename ? filename : "[kernel]",
915 filt->start ? "filter" : "stop",
916 filt->addr, filt->size);
921 hit_tracestop = true;
924 if (!hit_tracestop && !hit_filter)
925 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
926 ip, offset, filename ? filename : "[kernel]");
928 return hit_tracestop || (have_filter && !hit_filter);
931 static int __intel_pt_pgd_ip(uint64_t ip, void *data)
933 struct intel_pt_queue *ptq = data;
934 struct thread *thread;
935 struct addr_location al;
939 if (ptq->state->to_nr) {
940 if (intel_pt_guest_kernel_ip(ip))
941 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
942 /* No support for decoding guest user space */
944 } else if (ip >= ptq->pt->kernel_start) {
945 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
948 cpumode = PERF_RECORD_MISC_USER;
950 thread = ptq->thread;
954 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
957 offset = al.map->map_ip(al.map, ip);
959 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
960 al.map->dso->long_name);
963 static bool intel_pt_pgd_ip(uint64_t ip, void *data)
965 return __intel_pt_pgd_ip(ip, data) > 0;
968 static bool intel_pt_get_config(struct intel_pt *pt,
969 struct perf_event_attr *attr, u64 *config)
971 if (attr->type == pt->pmu_type) {
973 *config = attr->config;
980 static bool intel_pt_exclude_kernel(struct intel_pt *pt)
984 evlist__for_each_entry(pt->session->evlist, evsel) {
985 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
986 !evsel->core.attr.exclude_kernel)
992 static bool intel_pt_return_compression(struct intel_pt *pt)
997 if (!pt->noretcomp_bit)
1000 evlist__for_each_entry(pt->session->evlist, evsel) {
1001 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1002 (config & pt->noretcomp_bit))
1008 static bool intel_pt_branch_enable(struct intel_pt *pt)
1010 struct evsel *evsel;
1013 evlist__for_each_entry(pt->session->evlist, evsel) {
1014 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1015 (config & INTEL_PT_CFG_PASS_THRU) &&
1016 !(config & INTEL_PT_CFG_BRANCH_EN))
1022 static bool intel_pt_disabled_tnt(struct intel_pt *pt)
1024 struct evsel *evsel;
1027 evlist__for_each_entry(pt->session->evlist, evsel) {
1028 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1029 config & INTEL_PT_CFG_TNT_DIS)
1035 static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
1037 struct evsel *evsel;
1041 if (!pt->mtc_freq_bits)
1044 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
1047 evlist__for_each_entry(pt->session->evlist, evsel) {
1048 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
1049 return (config & pt->mtc_freq_bits) >> shift;
1054 static bool intel_pt_timeless_decoding(struct intel_pt *pt)
1056 struct evsel *evsel;
1057 bool timeless_decoding = true;
1060 if (!pt->tsc_bit || !pt->cap_user_time_zero || pt->synth_opts.timeless_decoding)
1063 evlist__for_each_entry(pt->session->evlist, evsel) {
1064 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_TIME))
1066 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
1067 if (config & pt->tsc_bit)
1068 timeless_decoding = false;
1073 return timeless_decoding;
1076 static bool intel_pt_tracing_kernel(struct intel_pt *pt)
1078 struct evsel *evsel;
1080 evlist__for_each_entry(pt->session->evlist, evsel) {
1081 if (intel_pt_get_config(pt, &evsel->core.attr, NULL) &&
1082 !evsel->core.attr.exclude_kernel)
1088 static bool intel_pt_have_tsc(struct intel_pt *pt)
1090 struct evsel *evsel;
1091 bool have_tsc = false;
1097 evlist__for_each_entry(pt->session->evlist, evsel) {
1098 if (intel_pt_get_config(pt, &evsel->core.attr, &config)) {
1099 if (config & pt->tsc_bit)
1108 static bool intel_pt_have_mtc(struct intel_pt *pt)
1110 struct evsel *evsel;
1113 evlist__for_each_entry(pt->session->evlist, evsel) {
1114 if (intel_pt_get_config(pt, &evsel->core.attr, &config) &&
1115 (config & pt->mtc_bit))
1121 static bool intel_pt_sampling_mode(struct intel_pt *pt)
1123 struct evsel *evsel;
1125 evlist__for_each_entry(pt->session->evlist, evsel) {
1126 if ((evsel->core.attr.sample_type & PERF_SAMPLE_AUX) &&
1127 evsel->core.attr.aux_sample_size)
1133 static u64 intel_pt_ctl(struct intel_pt *pt)
1135 struct evsel *evsel;
1138 evlist__for_each_entry(pt->session->evlist, evsel) {
1139 if (intel_pt_get_config(pt, &evsel->core.attr, &config))
1145 static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
1149 quot = ns / pt->tc.time_mult;
1150 rem = ns % pt->tc.time_mult;
1151 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
1155 static struct ip_callchain *intel_pt_alloc_chain(struct intel_pt *pt)
1157 size_t sz = sizeof(struct ip_callchain);
1159 /* Add 1 to callchain_sz for callchain context */
1160 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
1164 static int intel_pt_callchain_init(struct intel_pt *pt)
1166 struct evsel *evsel;
1168 evlist__for_each_entry(pt->session->evlist, evsel) {
1169 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_CALLCHAIN))
1170 evsel->synth_sample_type |= PERF_SAMPLE_CALLCHAIN;
1173 pt->chain = intel_pt_alloc_chain(pt);
1180 static void intel_pt_add_callchain(struct intel_pt *pt,
1181 struct perf_sample *sample)
1183 struct thread *thread = machine__findnew_thread(pt->machine,
1187 thread_stack__sample_late(thread, sample->cpu, pt->chain,
1188 pt->synth_opts.callchain_sz + 1, sample->ip,
1191 sample->callchain = pt->chain;
1194 static struct branch_stack *intel_pt_alloc_br_stack(unsigned int entry_cnt)
1196 size_t sz = sizeof(struct branch_stack);
1198 sz += entry_cnt * sizeof(struct branch_entry);
1202 static int intel_pt_br_stack_init(struct intel_pt *pt)
1204 struct evsel *evsel;
1206 evlist__for_each_entry(pt->session->evlist, evsel) {
1207 if (!(evsel->core.attr.sample_type & PERF_SAMPLE_BRANCH_STACK))
1208 evsel->synth_sample_type |= PERF_SAMPLE_BRANCH_STACK;
1211 pt->br_stack = intel_pt_alloc_br_stack(pt->br_stack_sz);
1218 static void intel_pt_add_br_stack(struct intel_pt *pt,
1219 struct perf_sample *sample)
1221 struct thread *thread = machine__findnew_thread(pt->machine,
1225 thread_stack__br_sample_late(thread, sample->cpu, pt->br_stack,
1226 pt->br_stack_sz, sample->ip,
1229 sample->branch_stack = pt->br_stack;
1232 /* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1233 #define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3U)
1235 static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
1236 unsigned int queue_nr)
1238 struct intel_pt_params params = { .get_trace = 0, };
1239 struct perf_env *env = pt->machine->env;
1240 struct intel_pt_queue *ptq;
1242 ptq = zalloc(sizeof(struct intel_pt_queue));
1246 if (pt->synth_opts.callchain) {
1247 ptq->chain = intel_pt_alloc_chain(pt);
1252 if (pt->synth_opts.last_branch || pt->synth_opts.other_events) {
1253 unsigned int entry_cnt = max(LBRS_MAX, pt->br_stack_sz);
1255 ptq->last_branch = intel_pt_alloc_br_stack(entry_cnt);
1256 if (!ptq->last_branch)
1260 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
1261 if (!ptq->event_buf)
1265 ptq->queue_nr = queue_nr;
1266 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
1272 params.get_trace = intel_pt_get_trace;
1273 params.walk_insn = intel_pt_walk_next_insn;
1274 params.lookahead = intel_pt_lookahead;
1275 params.findnew_vmcs_info = intel_pt_findnew_vmcs_info;
1277 params.return_compression = intel_pt_return_compression(pt);
1278 params.branch_enable = intel_pt_branch_enable(pt);
1279 params.ctl = intel_pt_ctl(pt);
1280 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
1281 params.mtc_period = intel_pt_mtc_period(pt);
1282 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
1283 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
1284 params.quick = pt->synth_opts.quick;
1285 params.vm_time_correlation = pt->synth_opts.vm_time_correlation;
1286 params.vm_tm_corr_dry_run = pt->synth_opts.vm_tm_corr_dry_run;
1287 params.first_timestamp = pt->first_timestamp;
1288 params.max_loops = pt->max_loops;
1290 /* Cannot walk code without TNT, so force 'quick' mode */
1291 if (params.branch_enable && intel_pt_disabled_tnt(pt) && !params.quick)
1294 if (pt->filts.cnt > 0)
1295 params.pgd_ip = intel_pt_pgd_ip;
1297 if (pt->synth_opts.instructions) {
1298 if (pt->synth_opts.period) {
1299 switch (pt->synth_opts.period_type) {
1300 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
1301 params.period_type =
1302 INTEL_PT_PERIOD_INSTRUCTIONS;
1303 params.period = pt->synth_opts.period;
1305 case PERF_ITRACE_PERIOD_TICKS:
1306 params.period_type = INTEL_PT_PERIOD_TICKS;
1307 params.period = pt->synth_opts.period;
1309 case PERF_ITRACE_PERIOD_NANOSECS:
1310 params.period_type = INTEL_PT_PERIOD_TICKS;
1311 params.period = intel_pt_ns_to_ticks(pt,
1312 pt->synth_opts.period);
1319 if (!params.period) {
1320 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
1325 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
1326 params.flags |= INTEL_PT_FUP_WITH_NLIP;
1328 ptq->decoder = intel_pt_decoder_new(¶ms);
1335 zfree(&ptq->event_buf);
1336 zfree(&ptq->last_branch);
1342 static void intel_pt_free_queue(void *priv)
1344 struct intel_pt_queue *ptq = priv;
1348 thread__zput(ptq->thread);
1349 thread__zput(ptq->guest_thread);
1350 thread__zput(ptq->unknown_guest_thread);
1351 intel_pt_decoder_free(ptq->decoder);
1352 zfree(&ptq->event_buf);
1353 zfree(&ptq->last_branch);
1358 static void intel_pt_first_timestamp(struct intel_pt *pt, u64 timestamp)
1362 pt->first_timestamp = timestamp;
1364 for (i = 0; i < pt->queues.nr_queues; i++) {
1365 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1366 struct intel_pt_queue *ptq = queue->priv;
1368 if (ptq && ptq->decoder)
1369 intel_pt_set_first_timestamp(ptq->decoder, timestamp);
1373 static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
1374 struct auxtrace_queue *queue)
1376 struct intel_pt_queue *ptq = queue->priv;
1378 if (queue->tid == -1 || pt->have_sched_switch) {
1379 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
1382 thread__zput(ptq->thread);
1385 if (!ptq->thread && ptq->tid != -1)
1386 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
1389 ptq->pid = ptq->thread->pid_;
1390 if (queue->cpu == -1)
1391 ptq->cpu = ptq->thread->cpu;
1395 static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
1397 struct intel_pt *pt = ptq->pt;
1400 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
1401 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
1402 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
1403 if (!ptq->state->to_ip)
1404 ptq->flags = PERF_IP_FLAG_BRANCH |
1405 PERF_IP_FLAG_TRACE_END;
1406 else if (ptq->state->from_nr && !ptq->state->to_nr)
1407 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1408 PERF_IP_FLAG_VMEXIT;
1410 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
1411 PERF_IP_FLAG_ASYNC |
1412 PERF_IP_FLAG_INTERRUPT;
1414 if (ptq->state->from_ip)
1415 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1417 ptq->flags = PERF_IP_FLAG_BRANCH |
1418 PERF_IP_FLAG_TRACE_BEGIN;
1419 if (ptq->state->flags & INTEL_PT_IN_TX)
1420 ptq->flags |= PERF_IP_FLAG_IN_TX;
1421 ptq->insn_len = ptq->state->insn_len;
1422 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
1425 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1426 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1427 if (ptq->state->type & INTEL_PT_TRACE_END)
1428 ptq->flags |= PERF_IP_FLAG_TRACE_END;
1430 if (pt->cap_event_trace) {
1431 if (ptq->state->type & INTEL_PT_IFLAG_CHG) {
1432 if (!ptq->state->from_iflag)
1433 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE;
1434 if (ptq->state->from_iflag != ptq->state->to_iflag)
1435 ptq->flags |= PERF_IP_FLAG_INTR_TOGGLE;
1436 } else if (!ptq->state->to_iflag) {
1437 ptq->flags |= PERF_IP_FLAG_INTR_DISABLE;
1442 static void intel_pt_setup_time_range(struct intel_pt *pt,
1443 struct intel_pt_queue *ptq)
1448 ptq->sel_timestamp = pt->time_ranges[0].start;
1451 if (ptq->sel_timestamp) {
1452 ptq->sel_start = true;
1454 ptq->sel_timestamp = pt->time_ranges[0].end;
1455 ptq->sel_start = false;
1459 static int intel_pt_setup_queue(struct intel_pt *pt,
1460 struct auxtrace_queue *queue,
1461 unsigned int queue_nr)
1463 struct intel_pt_queue *ptq = queue->priv;
1465 if (list_empty(&queue->head))
1469 ptq = intel_pt_alloc_queue(pt, queue_nr);
1474 if (queue->cpu != -1)
1475 ptq->cpu = queue->cpu;
1476 ptq->tid = queue->tid;
1478 ptq->cbr_seen = UINT_MAX;
1480 if (pt->sampling_mode && !pt->snapshot_mode &&
1481 pt->timeless_decoding)
1482 ptq->step_through_buffers = true;
1484 ptq->sync_switch = pt->sync_switch;
1486 intel_pt_setup_time_range(pt, ptq);
1489 if (!ptq->on_heap &&
1490 (!ptq->sync_switch ||
1491 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1492 const struct intel_pt_state *state;
1495 if (pt->timeless_decoding)
1498 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1499 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1500 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1502 if (ptq->sel_start && ptq->sel_timestamp) {
1503 ret = intel_pt_fast_forward(ptq->decoder,
1504 ptq->sel_timestamp);
1510 state = intel_pt_decode(ptq->decoder);
1512 if (state->err == INTEL_PT_ERR_NODATA) {
1513 intel_pt_log("queue %u has no timestamp\n",
1519 if (state->timestamp)
1523 ptq->timestamp = state->timestamp;
1524 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1525 queue_nr, ptq->timestamp);
1527 ptq->have_sample = true;
1528 if (ptq->sel_start && ptq->sel_timestamp &&
1529 ptq->timestamp < ptq->sel_timestamp)
1530 ptq->have_sample = false;
1531 intel_pt_sample_flags(ptq);
1532 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1535 ptq->on_heap = true;
1541 static int intel_pt_setup_queues(struct intel_pt *pt)
1546 for (i = 0; i < pt->queues.nr_queues; i++) {
1547 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1554 static inline bool intel_pt_skip_event(struct intel_pt *pt)
1556 return pt->synth_opts.initial_skip &&
1557 pt->num_events++ < pt->synth_opts.initial_skip;
1561 * Cannot count CBR as skipped because it won't go away until cbr == cbr_seen.
1562 * Also ensure CBR is first non-skipped event by allowing for 4 more samples
1563 * from this decoder state.
1565 static inline bool intel_pt_skip_cbr_event(struct intel_pt *pt)
1567 return pt->synth_opts.initial_skip &&
1568 pt->num_events + 4 < pt->synth_opts.initial_skip;
1571 static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1572 union perf_event *event,
1573 struct perf_sample *sample)
1575 event->sample.header.type = PERF_RECORD_SAMPLE;
1576 event->sample.header.size = sizeof(struct perf_event_header);
1578 sample->pid = ptq->pid;
1579 sample->tid = ptq->tid;
1580 sample->cpu = ptq->cpu;
1581 sample->insn_len = ptq->insn_len;
1582 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1585 static void intel_pt_prep_b_sample(struct intel_pt *pt,
1586 struct intel_pt_queue *ptq,
1587 union perf_event *event,
1588 struct perf_sample *sample)
1590 intel_pt_prep_a_sample(ptq, event, sample);
1592 if (!pt->timeless_decoding)
1593 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1595 sample->ip = ptq->state->from_ip;
1596 sample->addr = ptq->state->to_ip;
1597 sample->cpumode = intel_pt_cpumode(ptq, sample->ip, sample->addr);
1599 sample->flags = ptq->flags;
1601 event->sample.header.misc = sample->cpumode;
1604 static int intel_pt_inject_event(union perf_event *event,
1605 struct perf_sample *sample, u64 type)
1607 event->header.size = perf_event__sample_event_size(sample, type, 0);
1608 return perf_event__synthesize_sample(event, type, 0, sample);
1611 static inline int intel_pt_opt_inject(struct intel_pt *pt,
1612 union perf_event *event,
1613 struct perf_sample *sample, u64 type)
1615 if (!pt->synth_opts.inject)
1618 return intel_pt_inject_event(event, sample, type);
1621 static int intel_pt_deliver_synth_event(struct intel_pt *pt,
1622 union perf_event *event,
1623 struct perf_sample *sample, u64 type)
1627 ret = intel_pt_opt_inject(pt, event, sample, type);
1631 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1633 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1638 static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1640 struct intel_pt *pt = ptq->pt;
1641 union perf_event *event = ptq->event_buf;
1642 struct perf_sample sample = { .ip = 0, };
1643 struct dummy_branch_stack {
1646 struct branch_entry entries;
1649 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1652 if (intel_pt_skip_event(pt))
1655 intel_pt_prep_b_sample(pt, ptq, event, &sample);
1657 sample.id = ptq->pt->branches_id;
1658 sample.stream_id = ptq->pt->branches_id;
1661 * perf report cannot handle events without a branch stack when using
1662 * SORT_MODE__BRANCH so make a dummy one.
1664 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1665 dummy_bs = (struct dummy_branch_stack){
1673 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1676 if (ptq->sample_ipc)
1677 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
1678 if (sample.cyc_cnt) {
1679 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1680 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1681 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1684 return intel_pt_deliver_synth_event(pt, event, &sample,
1685 pt->branches_sample_type);
1688 static void intel_pt_prep_sample(struct intel_pt *pt,
1689 struct intel_pt_queue *ptq,
1690 union perf_event *event,
1691 struct perf_sample *sample)
1693 intel_pt_prep_b_sample(pt, ptq, event, sample);
1695 if (pt->synth_opts.callchain) {
1696 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1697 pt->synth_opts.callchain_sz + 1,
1698 sample->ip, pt->kernel_start);
1699 sample->callchain = ptq->chain;
1702 if (pt->synth_opts.last_branch) {
1703 thread_stack__br_sample(ptq->thread, ptq->cpu, ptq->last_branch,
1705 sample->branch_stack = ptq->last_branch;
1709 static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1711 struct intel_pt *pt = ptq->pt;
1712 union perf_event *event = ptq->event_buf;
1713 struct perf_sample sample = { .ip = 0, };
1715 if (intel_pt_skip_event(pt))
1718 intel_pt_prep_sample(pt, ptq, event, &sample);
1720 sample.id = ptq->pt->instructions_id;
1721 sample.stream_id = ptq->pt->instructions_id;
1722 if (pt->synth_opts.quick)
1725 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
1727 if (ptq->sample_ipc)
1728 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
1729 if (sample.cyc_cnt) {
1730 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1731 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1732 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1735 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1737 return intel_pt_deliver_synth_event(pt, event, &sample,
1738 pt->instructions_sample_type);
1741 static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1743 struct intel_pt *pt = ptq->pt;
1744 union perf_event *event = ptq->event_buf;
1745 struct perf_sample sample = { .ip = 0, };
1747 if (intel_pt_skip_event(pt))
1750 intel_pt_prep_sample(pt, ptq, event, &sample);
1752 sample.id = ptq->pt->transactions_id;
1753 sample.stream_id = ptq->pt->transactions_id;
1755 return intel_pt_deliver_synth_event(pt, event, &sample,
1756 pt->transactions_sample_type);
1759 static void intel_pt_prep_p_sample(struct intel_pt *pt,
1760 struct intel_pt_queue *ptq,
1761 union perf_event *event,
1762 struct perf_sample *sample)
1764 intel_pt_prep_sample(pt, ptq, event, sample);
1767 * Zero IP is used to mean "trace start" but that is not the case for
1768 * power or PTWRITE events with no IP, so clear the flags.
1774 static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1776 struct intel_pt *pt = ptq->pt;
1777 union perf_event *event = ptq->event_buf;
1778 struct perf_sample sample = { .ip = 0, };
1779 struct perf_synth_intel_ptwrite raw;
1781 if (intel_pt_skip_event(pt))
1784 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1786 sample.id = ptq->pt->ptwrites_id;
1787 sample.stream_id = ptq->pt->ptwrites_id;
1790 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1791 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1793 sample.raw_size = perf_synth__raw_size(raw);
1794 sample.raw_data = perf_synth__raw_data(&raw);
1796 return intel_pt_deliver_synth_event(pt, event, &sample,
1797 pt->ptwrites_sample_type);
1800 static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1802 struct intel_pt *pt = ptq->pt;
1803 union perf_event *event = ptq->event_buf;
1804 struct perf_sample sample = { .ip = 0, };
1805 struct perf_synth_intel_cbr raw;
1808 if (intel_pt_skip_cbr_event(pt))
1811 ptq->cbr_seen = ptq->state->cbr;
1813 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1815 sample.id = ptq->pt->cbr_id;
1816 sample.stream_id = ptq->pt->cbr_id;
1818 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1819 raw.flags = cpu_to_le32(flags);
1820 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1823 sample.raw_size = perf_synth__raw_size(raw);
1824 sample.raw_data = perf_synth__raw_data(&raw);
1826 return intel_pt_deliver_synth_event(pt, event, &sample,
1827 pt->pwr_events_sample_type);
1830 static int intel_pt_synth_psb_sample(struct intel_pt_queue *ptq)
1832 struct intel_pt *pt = ptq->pt;
1833 union perf_event *event = ptq->event_buf;
1834 struct perf_sample sample = { .ip = 0, };
1835 struct perf_synth_intel_psb raw;
1837 if (intel_pt_skip_event(pt))
1840 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1842 sample.id = ptq->pt->psb_id;
1843 sample.stream_id = ptq->pt->psb_id;
1847 raw.offset = ptq->state->psb_offset;
1849 sample.raw_size = perf_synth__raw_size(raw);
1850 sample.raw_data = perf_synth__raw_data(&raw);
1852 return intel_pt_deliver_synth_event(pt, event, &sample,
1853 pt->pwr_events_sample_type);
1856 static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1858 struct intel_pt *pt = ptq->pt;
1859 union perf_event *event = ptq->event_buf;
1860 struct perf_sample sample = { .ip = 0, };
1861 struct perf_synth_intel_mwait raw;
1863 if (intel_pt_skip_event(pt))
1866 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1868 sample.id = ptq->pt->mwait_id;
1869 sample.stream_id = ptq->pt->mwait_id;
1872 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1874 sample.raw_size = perf_synth__raw_size(raw);
1875 sample.raw_data = perf_synth__raw_data(&raw);
1877 return intel_pt_deliver_synth_event(pt, event, &sample,
1878 pt->pwr_events_sample_type);
1881 static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1883 struct intel_pt *pt = ptq->pt;
1884 union perf_event *event = ptq->event_buf;
1885 struct perf_sample sample = { .ip = 0, };
1886 struct perf_synth_intel_pwre raw;
1888 if (intel_pt_skip_event(pt))
1891 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1893 sample.id = ptq->pt->pwre_id;
1894 sample.stream_id = ptq->pt->pwre_id;
1897 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1899 sample.raw_size = perf_synth__raw_size(raw);
1900 sample.raw_data = perf_synth__raw_data(&raw);
1902 return intel_pt_deliver_synth_event(pt, event, &sample,
1903 pt->pwr_events_sample_type);
1906 static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1908 struct intel_pt *pt = ptq->pt;
1909 union perf_event *event = ptq->event_buf;
1910 struct perf_sample sample = { .ip = 0, };
1911 struct perf_synth_intel_exstop raw;
1913 if (intel_pt_skip_event(pt))
1916 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1918 sample.id = ptq->pt->exstop_id;
1919 sample.stream_id = ptq->pt->exstop_id;
1922 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1924 sample.raw_size = perf_synth__raw_size(raw);
1925 sample.raw_data = perf_synth__raw_data(&raw);
1927 return intel_pt_deliver_synth_event(pt, event, &sample,
1928 pt->pwr_events_sample_type);
1931 static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1933 struct intel_pt *pt = ptq->pt;
1934 union perf_event *event = ptq->event_buf;
1935 struct perf_sample sample = { .ip = 0, };
1936 struct perf_synth_intel_pwrx raw;
1938 if (intel_pt_skip_event(pt))
1941 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1943 sample.id = ptq->pt->pwrx_id;
1944 sample.stream_id = ptq->pt->pwrx_id;
1947 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1949 sample.raw_size = perf_synth__raw_size(raw);
1950 sample.raw_data = perf_synth__raw_data(&raw);
1952 return intel_pt_deliver_synth_event(pt, event, &sample,
1953 pt->pwr_events_sample_type);
1957 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
1958 * intel_pt_add_gp_regs().
1960 static const int pebs_gp_regs[] = {
1961 [PERF_REG_X86_FLAGS] = 1,
1962 [PERF_REG_X86_IP] = 2,
1963 [PERF_REG_X86_AX] = 3,
1964 [PERF_REG_X86_CX] = 4,
1965 [PERF_REG_X86_DX] = 5,
1966 [PERF_REG_X86_BX] = 6,
1967 [PERF_REG_X86_SP] = 7,
1968 [PERF_REG_X86_BP] = 8,
1969 [PERF_REG_X86_SI] = 9,
1970 [PERF_REG_X86_DI] = 10,
1971 [PERF_REG_X86_R8] = 11,
1972 [PERF_REG_X86_R9] = 12,
1973 [PERF_REG_X86_R10] = 13,
1974 [PERF_REG_X86_R11] = 14,
1975 [PERF_REG_X86_R12] = 15,
1976 [PERF_REG_X86_R13] = 16,
1977 [PERF_REG_X86_R14] = 17,
1978 [PERF_REG_X86_R15] = 18,
1981 static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
1982 const struct intel_pt_blk_items *items,
1985 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
1986 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
1990 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
1991 /* Get the PEBS gp_regs array index */
1992 int n = pebs_gp_regs[i] - 1;
1997 * Add only registers that were requested (i.e. 'regs_mask') and
1998 * that were provided (i.e. 'mask'), and update the resulting
1999 * mask (i.e. 'intr_regs->mask') accordingly.
2001 if (mask & 1 << n && regs_mask & bit) {
2002 intr_regs->mask |= bit;
2003 *pos++ = gp_regs[n];
2010 #ifndef PERF_REG_X86_XMM0
2011 #define PERF_REG_X86_XMM0 32
2014 static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
2015 const struct intel_pt_blk_items *items,
2018 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
2019 const u64 *xmm = items->xmm;
2022 * If there are any XMM registers, then there should be all of them.
2023 * Nevertheless, follow the logic to add only registers that were
2024 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
2025 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
2027 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
2029 for (; mask; mask >>= 1, xmm++) {
2035 #define LBR_INFO_MISPRED (1ULL << 63)
2036 #define LBR_INFO_IN_TX (1ULL << 62)
2037 #define LBR_INFO_ABORT (1ULL << 61)
2038 #define LBR_INFO_CYCLES 0xffff
2040 /* Refer kernel's intel_pmu_store_pebs_lbrs() */
2041 static u64 intel_pt_lbr_flags(u64 info)
2044 struct branch_flags flags;
2049 u.flags.mispred = !!(info & LBR_INFO_MISPRED);
2050 u.flags.predicted = !(info & LBR_INFO_MISPRED);
2051 u.flags.in_tx = !!(info & LBR_INFO_IN_TX);
2052 u.flags.abort = !!(info & LBR_INFO_ABORT);
2053 u.flags.cycles = info & LBR_INFO_CYCLES;
2058 static void intel_pt_add_lbrs(struct branch_stack *br_stack,
2059 const struct intel_pt_blk_items *items)
2066 to = &br_stack->entries[0].from;
2068 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
2069 u32 mask = items->mask[i];
2070 const u64 *from = items->val[i];
2072 for (; mask; mask >>= 3, from += 3) {
2073 if ((mask & 7) == 7) {
2076 *to++ = intel_pt_lbr_flags(from[2]);
2083 static int intel_pt_do_synth_pebs_sample(struct intel_pt_queue *ptq, struct evsel *evsel, u64 id)
2085 const struct intel_pt_blk_items *items = &ptq->state->items;
2086 struct perf_sample sample = { .ip = 0, };
2087 union perf_event *event = ptq->event_buf;
2088 struct intel_pt *pt = ptq->pt;
2089 u64 sample_type = evsel->core.attr.sample_type;
2091 u64 regs[8 * sizeof(sample.intr_regs.mask)];
2093 if (intel_pt_skip_event(pt))
2096 intel_pt_prep_a_sample(ptq, event, &sample);
2099 sample.stream_id = id;
2101 if (!evsel->core.attr.freq)
2102 sample.period = evsel->core.attr.sample_period;
2104 /* No support for non-zero CS base */
2106 sample.ip = items->ip;
2107 else if (items->has_rip)
2108 sample.ip = items->rip;
2110 sample.ip = ptq->state->from_ip;
2112 cpumode = intel_pt_cpumode(ptq, sample.ip, 0);
2114 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
2116 sample.cpumode = cpumode;
2118 if (sample_type & PERF_SAMPLE_TIME) {
2121 if (items->has_timestamp)
2122 timestamp = items->timestamp;
2123 else if (!pt->timeless_decoding)
2124 timestamp = ptq->timestamp;
2126 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
2129 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
2130 pt->synth_opts.callchain) {
2131 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
2132 pt->synth_opts.callchain_sz, sample.ip,
2134 sample.callchain = ptq->chain;
2137 if (sample_type & PERF_SAMPLE_REGS_INTR &&
2138 (items->mask[INTEL_PT_GP_REGS_POS] ||
2139 items->mask[INTEL_PT_XMM_POS])) {
2140 u64 regs_mask = evsel->core.attr.sample_regs_intr;
2143 sample.intr_regs.abi = items->is_32_bit ?
2144 PERF_SAMPLE_REGS_ABI_32 :
2145 PERF_SAMPLE_REGS_ABI_64;
2146 sample.intr_regs.regs = regs;
2148 pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
2150 intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
2153 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
2154 if (items->mask[INTEL_PT_LBR_0_POS] ||
2155 items->mask[INTEL_PT_LBR_1_POS] ||
2156 items->mask[INTEL_PT_LBR_2_POS]) {
2157 intel_pt_add_lbrs(ptq->last_branch, items);
2158 } else if (pt->synth_opts.last_branch) {
2159 thread_stack__br_sample(ptq->thread, ptq->cpu,
2163 ptq->last_branch->nr = 0;
2165 sample.branch_stack = ptq->last_branch;
2168 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
2169 sample.addr = items->mem_access_address;
2171 if (sample_type & PERF_SAMPLE_WEIGHT_TYPE) {
2173 * Refer kernel's setup_pebs_adaptive_sample_data() and
2174 * intel_hsw_weight().
2176 if (items->has_mem_access_latency) {
2177 u64 weight = items->mem_access_latency >> 32;
2180 * Starts from SPR, the mem access latency field
2181 * contains both cache latency [47:32] and instruction
2182 * latency [15:0]. The cache latency is the same as the
2183 * mem access latency on previous platforms.
2185 * In practice, no memory access could last than 4G
2186 * cycles. Use latency >> 32 to distinguish the
2187 * different format of the mem access latency field.
2190 sample.weight = weight & 0xffff;
2191 sample.ins_lat = items->mem_access_latency & 0xffff;
2193 sample.weight = items->mem_access_latency;
2195 if (!sample.weight && items->has_tsx_aux_info) {
2196 /* Cycles last block */
2197 sample.weight = (u32)items->tsx_aux_info;
2201 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
2202 u64 ax = items->has_rax ? items->rax : 0;
2203 /* Refer kernel's intel_hsw_transaction() */
2204 u64 txn = (u8)(items->tsx_aux_info >> 32);
2206 /* For RTM XABORTs also log the abort code from AX */
2207 if (txn & PERF_TXN_TRANSACTION && ax & 1)
2208 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
2209 sample.transaction = txn;
2212 return intel_pt_deliver_synth_event(pt, event, &sample, sample_type);
2215 static int intel_pt_synth_single_pebs_sample(struct intel_pt_queue *ptq)
2217 struct intel_pt *pt = ptq->pt;
2218 struct evsel *evsel = pt->pebs_evsel;
2219 u64 id = evsel->core.id[0];
2221 return intel_pt_do_synth_pebs_sample(ptq, evsel, id);
2224 static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
2226 const struct intel_pt_blk_items *items = &ptq->state->items;
2227 struct intel_pt_pebs_event *pe;
2228 struct intel_pt *pt = ptq->pt;
2232 if (!items->has_applicable_counters || !items->applicable_counters) {
2233 if (!pt->single_pebs)
2234 pr_err("PEBS-via-PT record with no applicable_counters\n");
2235 return intel_pt_synth_single_pebs_sample(ptq);
2238 for_each_set_bit(hw_id, (unsigned long *)&items->applicable_counters, INTEL_PT_MAX_PEBS) {
2239 pe = &ptq->pebs[hw_id];
2241 if (!pt->single_pebs)
2242 pr_err("PEBS-via-PT record with no matching event, hw_id %d\n",
2244 return intel_pt_synth_single_pebs_sample(ptq);
2246 err = intel_pt_do_synth_pebs_sample(ptq, pe->evsel, pe->id);
2254 static int intel_pt_synth_events_sample(struct intel_pt_queue *ptq)
2256 struct intel_pt *pt = ptq->pt;
2257 union perf_event *event = ptq->event_buf;
2258 struct perf_sample sample = { .ip = 0, };
2260 struct perf_synth_intel_evt cfe;
2261 struct perf_synth_intel_evd evd[INTEL_PT_MAX_EVDS];
2265 if (intel_pt_skip_event(pt))
2268 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2270 sample.id = ptq->pt->evt_id;
2271 sample.stream_id = ptq->pt->evt_id;
2273 raw.cfe.type = ptq->state->cfe_type;
2274 raw.cfe.reserved = 0;
2275 raw.cfe.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
2276 raw.cfe.vector = ptq->state->cfe_vector;
2277 raw.cfe.evd_cnt = ptq->state->evd_cnt;
2279 for (i = 0; i < ptq->state->evd_cnt; i++) {
2281 raw.evd[i].evd_type = ptq->state->evd[i].type;
2282 raw.evd[i].payload = ptq->state->evd[i].payload;
2285 sample.raw_size = perf_synth__raw_size(raw) +
2286 ptq->state->evd_cnt * sizeof(struct perf_synth_intel_evd);
2287 sample.raw_data = perf_synth__raw_data(&raw);
2289 return intel_pt_deliver_synth_event(pt, event, &sample,
2290 pt->evt_sample_type);
2293 static int intel_pt_synth_iflag_chg_sample(struct intel_pt_queue *ptq)
2295 struct intel_pt *pt = ptq->pt;
2296 union perf_event *event = ptq->event_buf;
2297 struct perf_sample sample = { .ip = 0, };
2298 struct perf_synth_intel_iflag_chg raw;
2300 if (intel_pt_skip_event(pt))
2303 intel_pt_prep_p_sample(pt, ptq, event, &sample);
2305 sample.id = ptq->pt->iflag_chg_id;
2306 sample.stream_id = ptq->pt->iflag_chg_id;
2309 raw.iflag = ptq->state->to_iflag;
2311 if (ptq->state->type & INTEL_PT_BRANCH) {
2313 raw.branch_ip = ptq->state->to_ip;
2317 sample.flags = ptq->flags;
2319 sample.raw_size = perf_synth__raw_size(raw);
2320 sample.raw_data = perf_synth__raw_data(&raw);
2322 return intel_pt_deliver_synth_event(pt, event, &sample,
2323 pt->iflag_chg_sample_type);
2326 static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
2327 pid_t pid, pid_t tid, u64 ip, u64 timestamp)
2329 union perf_event event;
2330 char msg[MAX_AUXTRACE_ERROR_MSG];
2333 if (pt->synth_opts.error_minus_flags) {
2334 if (code == INTEL_PT_ERR_OVR &&
2335 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_OVERFLOW)
2337 if (code == INTEL_PT_ERR_LOST &&
2338 pt->synth_opts.error_minus_flags & AUXTRACE_ERR_FLG_DATA_LOST)
2342 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
2344 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
2345 code, cpu, pid, tid, ip, msg, timestamp);
2347 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
2349 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
2355 static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
2356 const struct intel_pt_state *state)
2358 struct intel_pt *pt = ptq->pt;
2359 u64 tm = ptq->timestamp;
2361 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
2363 return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
2364 ptq->tid, state->from_ip, tm);
2367 static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
2369 struct auxtrace_queue *queue;
2370 pid_t tid = ptq->next_tid;
2376 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
2378 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
2380 queue = &pt->queues.queue_array[ptq->queue_nr];
2381 intel_pt_set_pid_tid_cpu(pt, queue);
2388 static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
2390 struct intel_pt *pt = ptq->pt;
2392 return ip == pt->switch_ip &&
2393 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
2394 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
2395 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
2398 #define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
2399 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT)
2401 static int intel_pt_sample(struct intel_pt_queue *ptq)
2403 const struct intel_pt_state *state = ptq->state;
2404 struct intel_pt *pt = ptq->pt;
2407 if (!ptq->have_sample)
2410 ptq->have_sample = false;
2412 if (pt->synth_opts.approx_ipc) {
2413 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2414 ptq->ipc_cyc_cnt = ptq->state->cycles;
2415 ptq->sample_ipc = true;
2417 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
2418 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
2419 ptq->sample_ipc = ptq->state->flags & INTEL_PT_SAMPLE_IPC;
2422 /* Ensure guest code maps are set up */
2423 if (symbol_conf.guest_code && (state->from_nr || state->to_nr))
2424 intel_pt_get_guest(ptq);
2427 * Do PEBS first to allow for the possibility that the PEBS timestamp
2428 * precedes the current timestamp.
2430 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
2431 err = intel_pt_synth_pebs_sample(ptq);
2436 if (pt->synth_opts.intr_events) {
2437 if (state->type & INTEL_PT_EVT) {
2438 err = intel_pt_synth_events_sample(ptq);
2442 if (state->type & INTEL_PT_IFLAG_CHG) {
2443 err = intel_pt_synth_iflag_chg_sample(ptq);
2449 if (pt->sample_pwr_events) {
2450 if (state->type & INTEL_PT_PSB_EVT) {
2451 err = intel_pt_synth_psb_sample(ptq);
2455 if (ptq->state->cbr != ptq->cbr_seen) {
2456 err = intel_pt_synth_cbr_sample(ptq);
2460 if (state->type & INTEL_PT_PWR_EVT) {
2461 if (state->type & INTEL_PT_MWAIT_OP) {
2462 err = intel_pt_synth_mwait_sample(ptq);
2466 if (state->type & INTEL_PT_PWR_ENTRY) {
2467 err = intel_pt_synth_pwre_sample(ptq);
2471 if (state->type & INTEL_PT_EX_STOP) {
2472 err = intel_pt_synth_exstop_sample(ptq);
2476 if (state->type & INTEL_PT_PWR_EXIT) {
2477 err = intel_pt_synth_pwrx_sample(ptq);
2484 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
2485 err = intel_pt_synth_instruction_sample(ptq);
2490 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
2491 err = intel_pt_synth_transaction_sample(ptq);
2496 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
2497 err = intel_pt_synth_ptwrite_sample(ptq);
2502 if (!(state->type & INTEL_PT_BRANCH))
2505 if (pt->use_thread_stack) {
2506 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags,
2507 state->from_ip, state->to_ip, ptq->insn_len,
2508 state->trace_nr, pt->callstack,
2509 pt->br_stack_sz_plus,
2512 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
2515 if (pt->sample_branches) {
2516 if (state->from_nr != state->to_nr &&
2517 state->from_ip && state->to_ip) {
2518 struct intel_pt_state *st = (struct intel_pt_state *)state;
2519 u64 to_ip = st->to_ip;
2520 u64 from_ip = st->from_ip;
2523 * perf cannot handle having different machines for ip
2524 * and addr, so create 2 branches.
2527 err = intel_pt_synth_branch_sample(ptq);
2532 err = intel_pt_synth_branch_sample(ptq);
2533 st->from_ip = from_ip;
2535 err = intel_pt_synth_branch_sample(ptq);
2541 if (!ptq->sync_switch)
2544 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
2545 switch (ptq->switch_state) {
2546 case INTEL_PT_SS_NOT_TRACING:
2547 case INTEL_PT_SS_UNKNOWN:
2548 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2549 err = intel_pt_next_tid(pt, ptq);
2552 ptq->switch_state = INTEL_PT_SS_TRACING;
2555 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
2558 } else if (!state->to_ip) {
2559 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2560 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
2561 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2562 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2563 state->to_ip == pt->ptss_ip &&
2564 (ptq->flags & PERF_IP_FLAG_CALL)) {
2565 ptq->switch_state = INTEL_PT_SS_TRACING;
2571 static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
2573 struct machine *machine = pt->machine;
2575 struct symbol *sym, *start;
2576 u64 ip, switch_ip = 0;
2582 map = machine__kernel_map(machine);
2589 start = dso__first_symbol(map->dso);
2591 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2592 if (sym->binding == STB_GLOBAL &&
2593 !strcmp(sym->name, "__switch_to")) {
2594 ip = map->unmap_ip(map, sym->start);
2595 if (ip >= map->start && ip < map->end) {
2602 if (!switch_ip || !ptss_ip)
2605 if (pt->have_sched_switch == 1)
2606 ptss = "perf_trace_sched_switch";
2608 ptss = "__perf_event_task_sched_out";
2610 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2611 if (!strcmp(sym->name, ptss)) {
2612 ip = map->unmap_ip(map, sym->start);
2613 if (ip >= map->start && ip < map->end) {
2623 static void intel_pt_enable_sync_switch(struct intel_pt *pt)
2627 pt->sync_switch = true;
2629 for (i = 0; i < pt->queues.nr_queues; i++) {
2630 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2631 struct intel_pt_queue *ptq = queue->priv;
2634 ptq->sync_switch = true;
2639 * To filter against time ranges, it is only necessary to look at the next start
2642 static bool intel_pt_next_time(struct intel_pt_queue *ptq)
2644 struct intel_pt *pt = ptq->pt;
2646 if (ptq->sel_start) {
2647 /* Next time is an end time */
2648 ptq->sel_start = false;
2649 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
2651 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
2652 /* Next time is a start time */
2653 ptq->sel_start = true;
2655 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
2663 static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
2668 if (ptq->sel_start) {
2669 if (ptq->timestamp >= ptq->sel_timestamp) {
2670 /* After start time, so consider next time */
2671 intel_pt_next_time(ptq);
2672 if (!ptq->sel_timestamp) {
2676 /* Check against end time */
2679 /* Before start time, so fast forward */
2680 ptq->have_sample = false;
2681 if (ptq->sel_timestamp > *ff_timestamp) {
2682 if (ptq->sync_switch) {
2683 intel_pt_next_tid(ptq->pt, ptq);
2684 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2686 *ff_timestamp = ptq->sel_timestamp;
2687 err = intel_pt_fast_forward(ptq->decoder,
2688 ptq->sel_timestamp);
2693 } else if (ptq->timestamp > ptq->sel_timestamp) {
2694 /* After end time, so consider next time */
2695 if (!intel_pt_next_time(ptq)) {
2696 /* No next time range, so stop decoding */
2697 ptq->have_sample = false;
2698 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2701 /* Check against next start time */
2704 /* Before end time */
2710 static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
2712 const struct intel_pt_state *state = ptq->state;
2713 struct intel_pt *pt = ptq->pt;
2714 u64 ff_timestamp = 0;
2717 if (!pt->kernel_start) {
2718 pt->kernel_start = machine__kernel_start(pt->machine);
2719 if (pt->per_cpu_mmaps &&
2720 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
2721 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
2722 !pt->sampling_mode && !pt->synth_opts.vm_time_correlation) {
2723 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
2724 if (pt->switch_ip) {
2725 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
2726 pt->switch_ip, pt->ptss_ip);
2727 intel_pt_enable_sync_switch(pt);
2732 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
2733 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2735 err = intel_pt_sample(ptq);
2739 state = intel_pt_decode(ptq->decoder);
2741 if (state->err == INTEL_PT_ERR_NODATA)
2743 if (ptq->sync_switch &&
2744 state->from_ip >= pt->kernel_start) {
2745 ptq->sync_switch = false;
2746 intel_pt_next_tid(pt, ptq);
2748 ptq->timestamp = state->est_timestamp;
2749 if (pt->synth_opts.errors) {
2750 err = intel_ptq_synth_error(ptq, state);
2758 ptq->have_sample = true;
2759 intel_pt_sample_flags(ptq);
2761 /* Use estimated TSC upon return to user space */
2763 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
2764 state->to_ip && state->to_ip < pt->kernel_start) {
2765 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2766 state->timestamp, state->est_timestamp);
2767 ptq->timestamp = state->est_timestamp;
2768 /* Use estimated TSC in unknown switch state */
2769 } else if (ptq->sync_switch &&
2770 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2771 intel_pt_is_switch_ip(ptq, state->to_ip) &&
2772 ptq->next_tid == -1) {
2773 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2774 state->timestamp, state->est_timestamp);
2775 ptq->timestamp = state->est_timestamp;
2776 } else if (state->timestamp > ptq->timestamp) {
2777 ptq->timestamp = state->timestamp;
2780 if (ptq->sel_timestamp) {
2781 err = intel_pt_time_filter(ptq, &ff_timestamp);
2786 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
2787 *timestamp = ptq->timestamp;
2794 static inline int intel_pt_update_queues(struct intel_pt *pt)
2796 if (pt->queues.new_data) {
2797 pt->queues.new_data = false;
2798 return intel_pt_setup_queues(pt);
2803 static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
2805 unsigned int queue_nr;
2810 struct auxtrace_queue *queue;
2811 struct intel_pt_queue *ptq;
2813 if (!pt->heap.heap_cnt)
2816 if (pt->heap.heap_array[0].ordinal >= timestamp)
2819 queue_nr = pt->heap.heap_array[0].queue_nr;
2820 queue = &pt->queues.queue_array[queue_nr];
2823 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
2824 queue_nr, pt->heap.heap_array[0].ordinal,
2827 auxtrace_heap__pop(&pt->heap);
2829 if (pt->heap.heap_cnt) {
2830 ts = pt->heap.heap_array[0].ordinal + 1;
2837 intel_pt_set_pid_tid_cpu(pt, queue);
2839 ret = intel_pt_run_decoder(ptq, &ts);
2842 auxtrace_heap__add(&pt->heap, queue_nr, ts);
2847 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
2851 ptq->on_heap = false;
2858 static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
2861 struct auxtrace_queues *queues = &pt->queues;
2865 for (i = 0; i < queues->nr_queues; i++) {
2866 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2867 struct intel_pt_queue *ptq = queue->priv;
2869 if (ptq && (tid == -1 || ptq->tid == tid)) {
2871 intel_pt_set_pid_tid_cpu(pt, queue);
2872 intel_pt_run_decoder(ptq, &ts);
2878 static void intel_pt_sample_set_pid_tid_cpu(struct intel_pt_queue *ptq,
2879 struct auxtrace_queue *queue,
2880 struct perf_sample *sample)
2882 struct machine *m = ptq->pt->machine;
2884 ptq->pid = sample->pid;
2885 ptq->tid = sample->tid;
2886 ptq->cpu = queue->cpu;
2888 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
2889 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2891 thread__zput(ptq->thread);
2896 if (ptq->pid == -1) {
2897 ptq->thread = machine__find_thread(m, -1, ptq->tid);
2899 ptq->pid = ptq->thread->pid_;
2903 ptq->thread = machine__findnew_thread(m, ptq->pid, ptq->tid);
2906 static int intel_pt_process_timeless_sample(struct intel_pt *pt,
2907 struct perf_sample *sample)
2909 struct auxtrace_queue *queue;
2910 struct intel_pt_queue *ptq;
2913 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
2922 ptq->time = sample->time;
2923 intel_pt_sample_set_pid_tid_cpu(ptq, queue, sample);
2924 intel_pt_run_decoder(ptq, &ts);
2928 static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
2930 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
2931 sample->pid, sample->tid, 0, sample->time);
2934 static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
2938 if (cpu < 0 || !pt->queues.nr_queues)
2941 if ((unsigned)cpu >= pt->queues.nr_queues)
2942 i = pt->queues.nr_queues - 1;
2946 if (pt->queues.queue_array[i].cpu == cpu)
2947 return pt->queues.queue_array[i].priv;
2949 for (j = 0; i > 0; j++) {
2950 if (pt->queues.queue_array[--i].cpu == cpu)
2951 return pt->queues.queue_array[i].priv;
2954 for (; j < pt->queues.nr_queues; j++) {
2955 if (pt->queues.queue_array[j].cpu == cpu)
2956 return pt->queues.queue_array[j].priv;
2962 static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
2965 struct intel_pt_queue *ptq;
2968 if (!pt->sync_switch)
2971 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2972 if (!ptq || !ptq->sync_switch)
2975 switch (ptq->switch_state) {
2976 case INTEL_PT_SS_NOT_TRACING:
2978 case INTEL_PT_SS_UNKNOWN:
2979 case INTEL_PT_SS_TRACING:
2980 ptq->next_tid = tid;
2981 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
2983 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2984 if (!ptq->on_heap) {
2985 ptq->timestamp = perf_time_to_tsc(timestamp,
2987 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
2991 ptq->on_heap = true;
2993 ptq->switch_state = INTEL_PT_SS_TRACING;
2995 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2996 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
3007 static int intel_pt_process_switch(struct intel_pt *pt,
3008 struct perf_sample *sample)
3012 struct evsel *evsel = evlist__id2evsel(pt->session->evlist, sample->id);
3014 if (evsel != pt->switch_evsel)
3017 tid = evsel__intval(evsel, sample, "next_pid");
3020 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
3021 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
3024 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
3028 return machine__set_current_tid(pt->machine, cpu, -1, tid);
3031 static int intel_pt_context_switch_in(struct intel_pt *pt,
3032 struct perf_sample *sample)
3034 pid_t pid = sample->pid;
3035 pid_t tid = sample->tid;
3036 int cpu = sample->cpu;
3038 if (pt->sync_switch) {
3039 struct intel_pt_queue *ptq;
3041 ptq = intel_pt_cpu_to_ptq(pt, cpu);
3042 if (ptq && ptq->sync_switch) {
3044 switch (ptq->switch_state) {
3045 case INTEL_PT_SS_NOT_TRACING:
3046 case INTEL_PT_SS_UNKNOWN:
3047 case INTEL_PT_SS_TRACING:
3049 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
3050 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
3051 ptq->switch_state = INTEL_PT_SS_TRACING;
3060 * If the current tid has not been updated yet, ensure it is now that
3061 * a "switch in" event has occurred.
3063 if (machine__get_current_tid(pt->machine, cpu) == tid)
3066 return machine__set_current_tid(pt->machine, cpu, pid, tid);
3069 static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
3070 struct perf_sample *sample)
3072 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
3078 if (pt->have_sched_switch == 3) {
3080 return intel_pt_context_switch_in(pt, sample);
3081 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
3082 pr_err("Expecting CPU-wide context switch event\n");
3085 pid = event->context_switch.next_prev_pid;
3086 tid = event->context_switch.next_prev_tid;
3095 intel_pt_log("context_switch event has no tid\n");
3097 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
3101 return machine__set_current_tid(pt->machine, cpu, pid, tid);
3104 static int intel_pt_process_itrace_start(struct intel_pt *pt,
3105 union perf_event *event,
3106 struct perf_sample *sample)
3108 if (!pt->per_cpu_mmaps)
3111 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
3112 sample->cpu, event->itrace_start.pid,
3113 event->itrace_start.tid, sample->time,
3114 perf_time_to_tsc(sample->time, &pt->tc));
3116 return machine__set_current_tid(pt->machine, sample->cpu,
3117 event->itrace_start.pid,
3118 event->itrace_start.tid);
3121 static int intel_pt_process_aux_output_hw_id(struct intel_pt *pt,
3122 union perf_event *event,
3123 struct perf_sample *sample)
3125 u64 hw_id = event->aux_output_hw_id.hw_id;
3126 struct auxtrace_queue *queue;
3127 struct intel_pt_queue *ptq;
3128 struct evsel *evsel;
3130 queue = auxtrace_queues__sample_queue(&pt->queues, sample, pt->session);
3131 evsel = evlist__id2evsel_strict(pt->session->evlist, sample->id);
3132 if (!queue || !queue->priv || !evsel || hw_id > INTEL_PT_MAX_PEBS) {
3133 pr_err("Bad AUX output hardware ID\n");
3139 ptq->pebs[hw_id].evsel = evsel;
3140 ptq->pebs[hw_id].id = sample->id;
3145 static int intel_pt_find_map(struct thread *thread, u8 cpumode, u64 addr,
3146 struct addr_location *al)
3148 if (!al->map || addr < al->map->start || addr >= al->map->end) {
3149 if (!thread__find_map(thread, cpumode, addr, al))
3156 /* Invalidate all instruction cache entries that overlap the text poke */
3157 static int intel_pt_text_poke(struct intel_pt *pt, union perf_event *event)
3159 u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
3160 u64 addr = event->text_poke.addr + event->text_poke.new_len - 1;
3161 /* Assume text poke begins in a basic block no more than 4096 bytes */
3162 int cnt = 4096 + event->text_poke.new_len;
3163 struct thread *thread = pt->unknown_thread;
3164 struct addr_location al = { .map = NULL };
3165 struct machine *machine = pt->machine;
3166 struct intel_pt_cache_entry *e;
3169 if (!event->text_poke.new_len)
3172 for (; cnt; cnt--, addr--) {
3173 if (intel_pt_find_map(thread, cpumode, addr, &al)) {
3174 if (addr < event->text_poke.addr)
3179 if (!al.map->dso || !al.map->dso->auxtrace_cache)
3182 offset = al.map->map_ip(al.map, addr);
3184 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
3188 if (addr + e->byte_cnt + e->length <= event->text_poke.addr) {
3190 * No overlap. Working backwards there cannot be another
3191 * basic block that overlaps the text poke if there is a
3192 * branch instruction before the text poke address.
3194 if (e->branch != INTEL_PT_BR_NO_BRANCH)
3197 intel_pt_cache_invalidate(al.map->dso, machine, offset);
3198 intel_pt_log("Invalidated instruction cache for %s at %#"PRIx64"\n",
3199 al.map->dso->long_name, addr);
3206 static int intel_pt_process_event(struct perf_session *session,
3207 union perf_event *event,
3208 struct perf_sample *sample,
3209 struct perf_tool *tool)
3211 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3219 if (!tool->ordered_events) {
3220 pr_err("Intel Processor Trace requires ordered events\n");
3224 if (sample->time && sample->time != (u64)-1)
3225 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3229 if (timestamp || pt->timeless_decoding) {
3230 err = intel_pt_update_queues(pt);
3235 if (pt->timeless_decoding) {
3236 if (pt->sampling_mode) {
3237 if (sample->aux_sample.size)
3238 err = intel_pt_process_timeless_sample(pt,
3240 } else if (event->header.type == PERF_RECORD_EXIT) {
3241 err = intel_pt_process_timeless_queues(pt,
3245 } else if (timestamp) {
3246 if (!pt->first_timestamp)
3247 intel_pt_first_timestamp(pt, timestamp);
3248 err = intel_pt_process_queues(pt, timestamp);
3253 if (event->header.type == PERF_RECORD_SAMPLE) {
3254 if (pt->synth_opts.add_callchain && !sample->callchain)
3255 intel_pt_add_callchain(pt, sample);
3256 if (pt->synth_opts.add_last_branch && !sample->branch_stack)
3257 intel_pt_add_br_stack(pt, sample);
3260 if (event->header.type == PERF_RECORD_AUX &&
3261 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
3262 pt->synth_opts.errors) {
3263 err = intel_pt_lost(pt, sample);
3268 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
3269 err = intel_pt_process_switch(pt, sample);
3270 else if (event->header.type == PERF_RECORD_ITRACE_START)
3271 err = intel_pt_process_itrace_start(pt, event, sample);
3272 else if (event->header.type == PERF_RECORD_AUX_OUTPUT_HW_ID)
3273 err = intel_pt_process_aux_output_hw_id(pt, event, sample);
3274 else if (event->header.type == PERF_RECORD_SWITCH ||
3275 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
3276 err = intel_pt_context_switch(pt, event, sample);
3278 if (!err && event->header.type == PERF_RECORD_TEXT_POKE)
3279 err = intel_pt_text_poke(pt, event);
3281 if (intel_pt_enable_logging && intel_pt_log_events(pt, sample->time)) {
3282 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
3283 event->header.type, sample->cpu, sample->time, timestamp);
3284 intel_pt_log_event(event);
3290 static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
3292 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3299 if (!tool->ordered_events)
3302 ret = intel_pt_update_queues(pt);
3306 if (pt->timeless_decoding)
3307 return intel_pt_process_timeless_queues(pt, -1,
3310 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
3313 static void intel_pt_free_events(struct perf_session *session)
3315 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3317 struct auxtrace_queues *queues = &pt->queues;
3320 for (i = 0; i < queues->nr_queues; i++) {
3321 intel_pt_free_queue(queues->queue_array[i].priv);
3322 queues->queue_array[i].priv = NULL;
3324 intel_pt_log_disable();
3325 auxtrace_queues__free(queues);
3328 static void intel_pt_free(struct perf_session *session)
3330 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3333 auxtrace_heap__free(&pt->heap);
3334 intel_pt_free_events(session);
3335 session->auxtrace = NULL;
3336 intel_pt_free_vmcs_info(pt);
3337 thread__put(pt->unknown_thread);
3338 addr_filters__exit(&pt->filts);
3341 zfree(&pt->time_ranges);
3345 static bool intel_pt_evsel_is_auxtrace(struct perf_session *session,
3346 struct evsel *evsel)
3348 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3351 return evsel->core.attr.type == pt->pmu_type;
3354 static int intel_pt_process_auxtrace_event(struct perf_session *session,
3355 union perf_event *event,
3356 struct perf_tool *tool __maybe_unused)
3358 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3361 if (!pt->data_queued) {
3362 struct auxtrace_buffer *buffer;
3364 int fd = perf_data__fd(session->data);
3367 if (perf_data__is_pipe(session->data)) {
3370 data_offset = lseek(fd, 0, SEEK_CUR);
3371 if (data_offset == -1)
3375 err = auxtrace_queues__add_event(&pt->queues, session, event,
3376 data_offset, &buffer);
3380 /* Dump here now we have copied a piped trace out of the pipe */
3382 if (auxtrace_buffer__get_data(buffer, fd)) {
3383 intel_pt_dump_event(pt, buffer->data,
3385 auxtrace_buffer__put_data(buffer);
3393 static int intel_pt_queue_data(struct perf_session *session,
3394 struct perf_sample *sample,
3395 union perf_event *event, u64 data_offset)
3397 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
3402 return auxtrace_queues__add_event(&pt->queues, session, event,
3406 if (sample->time && sample->time != (u64)-1)
3407 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
3411 return auxtrace_queues__add_sample(&pt->queues, session, sample,
3412 data_offset, timestamp);
3415 struct intel_pt_synth {
3416 struct perf_tool dummy_tool;
3417 struct perf_session *session;
3420 static int intel_pt_event_synth(struct perf_tool *tool,
3421 union perf_event *event,
3422 struct perf_sample *sample __maybe_unused,
3423 struct machine *machine __maybe_unused)
3425 struct intel_pt_synth *intel_pt_synth =
3426 container_of(tool, struct intel_pt_synth, dummy_tool);
3428 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
3432 static int intel_pt_synth_event(struct perf_session *session, const char *name,
3433 struct perf_event_attr *attr, u64 id)
3435 struct intel_pt_synth intel_pt_synth;
3438 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
3439 name, id, (u64)attr->sample_type);
3441 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
3442 intel_pt_synth.session = session;
3444 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
3445 &id, intel_pt_event_synth);
3447 pr_err("%s: failed to synthesize '%s' event type\n",
3453 static void intel_pt_set_event_name(struct evlist *evlist, u64 id,
3456 struct evsel *evsel;
3458 evlist__for_each_entry(evlist, evsel) {
3459 if (evsel->core.id && evsel->core.id[0] == id) {
3461 zfree(&evsel->name);
3462 evsel->name = strdup(name);
3468 static struct evsel *intel_pt_evsel(struct intel_pt *pt,
3469 struct evlist *evlist)
3471 struct evsel *evsel;
3473 evlist__for_each_entry(evlist, evsel) {
3474 if (evsel->core.attr.type == pt->pmu_type && evsel->core.ids)
3481 static int intel_pt_synth_events(struct intel_pt *pt,
3482 struct perf_session *session)
3484 struct evlist *evlist = session->evlist;
3485 struct evsel *evsel = intel_pt_evsel(pt, evlist);
3486 struct perf_event_attr attr;
3491 pr_debug("There are no selected events with Intel Processor Trace data\n");
3495 memset(&attr, 0, sizeof(struct perf_event_attr));
3496 attr.size = sizeof(struct perf_event_attr);
3497 attr.type = PERF_TYPE_HARDWARE;
3498 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK;
3499 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
3501 if (pt->timeless_decoding)
3502 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
3504 attr.sample_type |= PERF_SAMPLE_TIME;
3505 if (!pt->per_cpu_mmaps)
3506 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
3507 attr.exclude_user = evsel->core.attr.exclude_user;
3508 attr.exclude_kernel = evsel->core.attr.exclude_kernel;
3509 attr.exclude_hv = evsel->core.attr.exclude_hv;
3510 attr.exclude_host = evsel->core.attr.exclude_host;
3511 attr.exclude_guest = evsel->core.attr.exclude_guest;
3512 attr.sample_id_all = evsel->core.attr.sample_id_all;
3513 attr.read_format = evsel->core.attr.read_format;
3515 id = evsel->core.id[0] + 1000000000;
3519 if (pt->synth_opts.branches) {
3520 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
3521 attr.sample_period = 1;
3522 attr.sample_type |= PERF_SAMPLE_ADDR;
3523 err = intel_pt_synth_event(session, "branches", &attr, id);
3526 pt->sample_branches = true;
3527 pt->branches_sample_type = attr.sample_type;
3528 pt->branches_id = id;
3530 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
3533 if (pt->synth_opts.callchain)
3534 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
3535 if (pt->synth_opts.last_branch) {
3536 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
3538 * We don't use the hardware index, but the sample generation
3539 * code uses the new format branch_stack with this field,
3540 * so the event attributes must indicate that it's present.
3542 attr.branch_sample_type |= PERF_SAMPLE_BRANCH_HW_INDEX;
3545 if (pt->synth_opts.instructions) {
3546 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
3547 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
3548 attr.sample_period =
3549 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
3551 attr.sample_period = pt->synth_opts.period;
3552 err = intel_pt_synth_event(session, "instructions", &attr, id);
3555 pt->sample_instructions = true;
3556 pt->instructions_sample_type = attr.sample_type;
3557 pt->instructions_id = id;
3561 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
3562 attr.sample_period = 1;
3564 if (pt->synth_opts.transactions) {
3565 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
3566 err = intel_pt_synth_event(session, "transactions", &attr, id);
3569 pt->sample_transactions = true;
3570 pt->transactions_sample_type = attr.sample_type;
3571 pt->transactions_id = id;
3572 intel_pt_set_event_name(evlist, id, "transactions");
3576 attr.type = PERF_TYPE_SYNTH;
3577 attr.sample_type |= PERF_SAMPLE_RAW;
3579 if (pt->synth_opts.ptwrites) {
3580 attr.config = PERF_SYNTH_INTEL_PTWRITE;
3581 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
3584 pt->sample_ptwrites = true;
3585 pt->ptwrites_sample_type = attr.sample_type;
3586 pt->ptwrites_id = id;
3587 intel_pt_set_event_name(evlist, id, "ptwrite");
3591 if (pt->synth_opts.pwr_events) {
3592 pt->sample_pwr_events = true;
3593 pt->pwr_events_sample_type = attr.sample_type;
3595 attr.config = PERF_SYNTH_INTEL_CBR;
3596 err = intel_pt_synth_event(session, "cbr", &attr, id);
3600 intel_pt_set_event_name(evlist, id, "cbr");
3603 attr.config = PERF_SYNTH_INTEL_PSB;
3604 err = intel_pt_synth_event(session, "psb", &attr, id);
3608 intel_pt_set_event_name(evlist, id, "psb");
3612 if (pt->synth_opts.pwr_events && (evsel->core.attr.config & INTEL_PT_CFG_PWR_EVT_EN)) {
3613 attr.config = PERF_SYNTH_INTEL_MWAIT;
3614 err = intel_pt_synth_event(session, "mwait", &attr, id);
3618 intel_pt_set_event_name(evlist, id, "mwait");
3621 attr.config = PERF_SYNTH_INTEL_PWRE;
3622 err = intel_pt_synth_event(session, "pwre", &attr, id);
3626 intel_pt_set_event_name(evlist, id, "pwre");
3629 attr.config = PERF_SYNTH_INTEL_EXSTOP;
3630 err = intel_pt_synth_event(session, "exstop", &attr, id);
3634 intel_pt_set_event_name(evlist, id, "exstop");
3637 attr.config = PERF_SYNTH_INTEL_PWRX;
3638 err = intel_pt_synth_event(session, "pwrx", &attr, id);
3642 intel_pt_set_event_name(evlist, id, "pwrx");
3646 if (pt->synth_opts.intr_events && (evsel->core.attr.config & INTEL_PT_CFG_EVT_EN)) {
3647 attr.config = PERF_SYNTH_INTEL_EVT;
3648 err = intel_pt_synth_event(session, "evt", &attr, id);
3651 pt->evt_sample_type = attr.sample_type;
3653 intel_pt_set_event_name(evlist, id, "evt");
3657 if (pt->synth_opts.intr_events && pt->cap_event_trace) {
3658 attr.config = PERF_SYNTH_INTEL_IFLAG_CHG;
3659 err = intel_pt_synth_event(session, "iflag", &attr, id);
3662 pt->iflag_chg_sample_type = attr.sample_type;
3663 pt->iflag_chg_id = id;
3664 intel_pt_set_event_name(evlist, id, "iflag");
3671 static void intel_pt_setup_pebs_events(struct intel_pt *pt)
3673 struct evsel *evsel;
3675 if (!pt->synth_opts.other_events)
3678 evlist__for_each_entry(pt->session->evlist, evsel) {
3679 if (evsel->core.attr.aux_output && evsel->core.id) {
3680 if (pt->single_pebs) {
3681 pt->single_pebs = false;
3684 pt->single_pebs = true;
3685 pt->sample_pebs = true;
3686 pt->pebs_evsel = evsel;
3691 static struct evsel *intel_pt_find_sched_switch(struct evlist *evlist)
3693 struct evsel *evsel;
3695 evlist__for_each_entry_reverse(evlist, evsel) {
3696 const char *name = evsel__name(evsel);
3698 if (!strcmp(name, "sched:sched_switch"))
3705 static bool intel_pt_find_switch(struct evlist *evlist)
3707 struct evsel *evsel;
3709 evlist__for_each_entry(evlist, evsel) {
3710 if (evsel->core.attr.context_switch)
3717 static int intel_pt_perf_config(const char *var, const char *value, void *data)
3719 struct intel_pt *pt = data;
3721 if (!strcmp(var, "intel-pt.mispred-all"))
3722 pt->mispred_all = perf_config_bool(var, value);
3724 if (!strcmp(var, "intel-pt.max-loops"))
3725 perf_config_int(&pt->max_loops, var, value);
3730 /* Find least TSC which converts to ns or later */
3731 static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
3735 tsc = perf_time_to_tsc(ns, &pt->tc);
3738 tm = tsc_to_perf_time(tsc, &pt->tc);
3745 tm = tsc_to_perf_time(++tsc, &pt->tc);
3750 /* Find greatest TSC which converts to ns or earlier */
3751 static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
3755 tsc = perf_time_to_tsc(ns, &pt->tc);
3758 tm = tsc_to_perf_time(tsc, &pt->tc);
3765 tm = tsc_to_perf_time(--tsc, &pt->tc);
3770 static int intel_pt_setup_time_ranges(struct intel_pt *pt,
3771 struct itrace_synth_opts *opts)
3773 struct perf_time_interval *p = opts->ptime_range;
3774 int n = opts->range_num;
3777 if (!n || !p || pt->timeless_decoding)
3780 pt->time_ranges = calloc(n, sizeof(struct range));
3781 if (!pt->time_ranges)
3786 intel_pt_log("%s: %u range(s)\n", __func__, n);
3788 for (i = 0; i < n; i++) {
3789 struct range *r = &pt->time_ranges[i];
3790 u64 ts = p[i].start;
3794 * Take care to ensure the TSC range matches the perf-time range
3795 * when converted back to perf-time.
3797 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
3798 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
3800 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
3802 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
3803 i, r->start, r->end);
3809 static int intel_pt_parse_vm_tm_corr_arg(struct intel_pt *pt, char **args)
3811 struct intel_pt_vmcs_info *vmcs_info;
3812 u64 tsc_offset, vmcs;
3821 tsc_offset = strtoull(p, &p, 0);
3826 pt->dflt_tsc_offset = tsc_offset;
3832 vmcs = strtoull(p, &p, 0);
3837 vmcs_info = intel_pt_findnew_vmcs(&pt->vmcs_info, vmcs, tsc_offset);
3849 static int intel_pt_parse_vm_tm_corr_args(struct intel_pt *pt)
3851 char *args = pt->synth_opts.vm_tm_corr_args;
3858 ret = intel_pt_parse_vm_tm_corr_arg(pt, &args);
3862 pr_err("Failed to parse VM Time Correlation options\n");
3869 static const char * const intel_pt_info_fmts[] = {
3870 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
3871 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
3872 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
3873 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
3874 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
3875 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
3876 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
3877 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
3878 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
3879 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
3880 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
3881 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
3882 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
3883 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
3884 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
3885 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
3888 static void intel_pt_print_info(__u64 *arr, int start, int finish)
3895 for (i = start; i <= finish; i++)
3896 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
3899 static void intel_pt_print_info_str(const char *name, const char *str)
3904 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
3907 static bool intel_pt_has(struct perf_record_auxtrace_info *auxtrace_info, int pos)
3909 return auxtrace_info->header.size >=
3910 sizeof(struct perf_record_auxtrace_info) + (sizeof(u64) * (pos + 1));
3913 int intel_pt_process_auxtrace_info(union perf_event *event,
3914 struct perf_session *session)
3916 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info;
3917 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
3918 struct intel_pt *pt;
3923 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) +
3927 pt = zalloc(sizeof(struct intel_pt));
3931 pt->vmcs_info = RB_ROOT;
3933 addr_filters__init(&pt->filts);
3935 err = perf_config(intel_pt_perf_config, pt);
3939 err = auxtrace_queues__init(&pt->queues);
3943 if (session->itrace_synth_opts->set) {
3944 pt->synth_opts = *session->itrace_synth_opts;
3946 struct itrace_synth_opts *opts = session->itrace_synth_opts;
3948 itrace_synth_opts__set_default(&pt->synth_opts, opts->default_no_sample);
3949 if (!opts->default_no_sample && !opts->inject) {
3950 pt->synth_opts.branches = false;
3951 pt->synth_opts.callchain = true;
3952 pt->synth_opts.add_callchain = true;
3954 pt->synth_opts.thread_stack = opts->thread_stack;
3957 if (!(pt->synth_opts.log_plus_flags & AUXTRACE_LOG_FLG_USE_STDOUT))
3958 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
3960 pt->session = session;
3961 pt->machine = &session->machines.host; /* No kvm support */
3962 pt->auxtrace_type = auxtrace_info->type;
3963 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
3964 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
3965 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
3966 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
3967 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
3968 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
3969 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
3970 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
3971 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
3972 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
3973 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
3974 INTEL_PT_PER_CPU_MMAPS);
3976 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
3977 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
3978 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
3979 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
3980 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
3981 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
3982 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
3986 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
3987 pt->max_non_turbo_ratio =
3988 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
3989 intel_pt_print_info(&auxtrace_info->priv[0],
3990 INTEL_PT_MAX_NONTURBO_RATIO,
3991 INTEL_PT_MAX_NONTURBO_RATIO);
3994 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
3995 info_end = (void *)auxtrace_info + auxtrace_info->header.size;
3997 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
4000 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
4001 intel_pt_print_info(&auxtrace_info->priv[0],
4002 INTEL_PT_FILTER_STR_LEN,
4003 INTEL_PT_FILTER_STR_LEN);
4005 const char *filter = (const char *)info;
4007 len = roundup(len + 1, 8);
4009 if ((void *)info > info_end) {
4010 pr_err("%s: bad filter string length\n", __func__);
4012 goto err_free_queues;
4014 pt->filter = memdup(filter, len);
4017 goto err_free_queues;
4019 if (session->header.needs_swap)
4020 mem_bswap_64(pt->filter, len);
4021 if (pt->filter[len - 1]) {
4022 pr_err("%s: filter string not null terminated\n", __func__);
4024 goto err_free_queues;
4026 err = addr_filters__parse_bare_filter(&pt->filts,
4029 goto err_free_queues;
4031 intel_pt_print_info_str("Filter string", pt->filter);
4034 if ((void *)info < info_end) {
4035 pt->cap_event_trace = *info++;
4037 fprintf(stdout, " Cap Event Trace %d\n",
4038 pt->cap_event_trace);
4041 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
4042 if (pt->timeless_decoding && !pt->tc.time_mult)
4043 pt->tc.time_mult = 1;
4044 pt->have_tsc = intel_pt_have_tsc(pt);
4045 pt->sampling_mode = intel_pt_sampling_mode(pt);
4046 pt->est_tsc = !pt->timeless_decoding;
4048 if (pt->synth_opts.vm_time_correlation) {
4049 if (pt->timeless_decoding) {
4050 pr_err("Intel PT has no time information for VM Time Correlation\n");
4052 goto err_free_queues;
4054 if (session->itrace_synth_opts->ptime_range) {
4055 pr_err("Time ranges cannot be specified with VM Time Correlation\n");
4057 goto err_free_queues;
4059 /* Currently TSC Offset is calculated using MTC packets */
4060 if (!intel_pt_have_mtc(pt)) {
4061 pr_err("MTC packets must have been enabled for VM Time Correlation\n");
4063 goto err_free_queues;
4065 err = intel_pt_parse_vm_tm_corr_args(pt);
4067 goto err_free_queues;
4070 pt->unknown_thread = thread__new(999999999, 999999999);
4071 if (!pt->unknown_thread) {
4073 goto err_free_queues;
4077 * Since this thread will not be kept in any rbtree not in a
4078 * list, initialize its list node so that at thread__put() the
4079 * current thread lifetime assumption is kept and we don't segfault
4080 * at list_del_init().
4082 INIT_LIST_HEAD(&pt->unknown_thread->node);
4084 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
4086 goto err_delete_thread;
4087 if (thread__init_maps(pt->unknown_thread, pt->machine)) {
4089 goto err_delete_thread;
4092 pt->auxtrace.process_event = intel_pt_process_event;
4093 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
4094 pt->auxtrace.queue_data = intel_pt_queue_data;
4095 pt->auxtrace.dump_auxtrace_sample = intel_pt_dump_sample;
4096 pt->auxtrace.flush_events = intel_pt_flush;
4097 pt->auxtrace.free_events = intel_pt_free_events;
4098 pt->auxtrace.free = intel_pt_free;
4099 pt->auxtrace.evsel_is_auxtrace = intel_pt_evsel_is_auxtrace;
4100 session->auxtrace = &pt->auxtrace;
4105 if (pt->have_sched_switch == 1) {
4106 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
4107 if (!pt->switch_evsel) {
4108 pr_err("%s: missing sched_switch event\n", __func__);
4110 goto err_delete_thread;
4112 } else if (pt->have_sched_switch == 2 &&
4113 !intel_pt_find_switch(session->evlist)) {
4114 pr_err("%s: missing context_switch attribute flag\n", __func__);
4116 goto err_delete_thread;
4119 if (pt->synth_opts.log)
4120 intel_pt_log_enable();
4122 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
4123 if (pt->tc.time_mult) {
4124 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
4126 if (!pt->max_non_turbo_ratio)
4127 pt->max_non_turbo_ratio =
4128 (tsc_freq + 50000000) / 100000000;
4129 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
4130 intel_pt_log("Maximum non-turbo ratio %u\n",
4131 pt->max_non_turbo_ratio);
4132 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
4135 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
4137 goto err_delete_thread;
4139 if (pt->synth_opts.calls)
4140 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
4141 PERF_IP_FLAG_TRACE_END;
4142 if (pt->synth_opts.returns)
4143 pt->branches_filter |= PERF_IP_FLAG_RETURN |
4144 PERF_IP_FLAG_TRACE_BEGIN;
4146 if ((pt->synth_opts.callchain || pt->synth_opts.add_callchain) &&
4147 !symbol_conf.use_callchain) {
4148 symbol_conf.use_callchain = true;
4149 if (callchain_register_param(&callchain_param) < 0) {
4150 symbol_conf.use_callchain = false;
4151 pt->synth_opts.callchain = false;
4152 pt->synth_opts.add_callchain = false;
4156 if (pt->synth_opts.add_callchain) {
4157 err = intel_pt_callchain_init(pt);
4159 goto err_delete_thread;
4162 if (pt->synth_opts.last_branch || pt->synth_opts.add_last_branch) {
4163 pt->br_stack_sz = pt->synth_opts.last_branch_sz;
4164 pt->br_stack_sz_plus = pt->br_stack_sz;
4167 if (pt->synth_opts.add_last_branch) {
4168 err = intel_pt_br_stack_init(pt);
4170 goto err_delete_thread;
4172 * Additional branch stack size to cater for tracing from the
4173 * actual sample ip to where the sample time is recorded.
4174 * Measured at about 200 branches, but generously set to 1024.
4175 * If kernel space is not being traced, then add just 1 for the
4176 * branch to kernel space.
4178 if (intel_pt_tracing_kernel(pt))
4179 pt->br_stack_sz_plus += 1024;
4181 pt->br_stack_sz_plus += 1;
4184 pt->use_thread_stack = pt->synth_opts.callchain ||
4185 pt->synth_opts.add_callchain ||
4186 pt->synth_opts.thread_stack ||
4187 pt->synth_opts.last_branch ||
4188 pt->synth_opts.add_last_branch;
4190 pt->callstack = pt->synth_opts.callchain ||
4191 pt->synth_opts.add_callchain ||
4192 pt->synth_opts.thread_stack;
4194 err = intel_pt_synth_events(pt, session);
4196 goto err_delete_thread;
4198 intel_pt_setup_pebs_events(pt);
4200 if (pt->sampling_mode || list_empty(&session->auxtrace_index))
4201 err = auxtrace_queue_data(session, true, true);
4203 err = auxtrace_queues__process_index(&pt->queues, session);
4205 goto err_delete_thread;
4207 if (pt->queues.populated)
4208 pt->data_queued = true;
4210 if (pt->timeless_decoding)
4211 pr_debug2("Intel PT decoding without timestamps\n");
4217 thread__zput(pt->unknown_thread);
4219 intel_pt_log_disable();
4220 auxtrace_queues__free(&pt->queues);
4221 session->auxtrace = NULL;
4223 addr_filters__exit(&pt->filts);
4225 zfree(&pt->time_ranges);