blob: 550db6e779685c42c9100780a706dd8914fedf38 [file] [log] [blame]
Thomas Gleixner2025cf92019-05-29 07:18:02 -07001// SPDX-License-Identifier: GPL-2.0-only
Adrian Hunter90e457f2015-07-17 19:33:41 +03002/*
3 * intel_pt.c: Intel Processor Trace support
4 * Copyright (c) 2013-2015, Intel Corporation.
Adrian Hunter90e457f2015-07-17 19:33:41 +03005 */
6
Arnaldo Carvalho de Melofd20e812017-04-17 15:23:08 -03007#include <inttypes.h>
Adrian Hunter90e457f2015-07-17 19:33:41 +03008#include <stdio.h>
9#include <stdbool.h>
10#include <errno.h>
11#include <linux/kernel.h>
12#include <linux/types.h>
13
14#include "../perf.h"
15#include "session.h"
16#include "machine.h"
Arnaldo Carvalho de Melo98521b32017-04-25 15:45:35 -030017#include "memswap.h"
Adrian Hunterf14445e2015-09-25 16:15:45 +030018#include "sort.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030019#include "tool.h"
20#include "event.h"
21#include "evlist.h"
22#include "evsel.h"
23#include "map.h"
24#include "color.h"
25#include "util.h"
26#include "thread.h"
27#include "thread-stack.h"
28#include "symbol.h"
29#include "callchain.h"
30#include "dso.h"
31#include "debug.h"
32#include "auxtrace.h"
33#include "tsc.h"
34#include "intel-pt.h"
Taeung Song41840d22016-06-23 17:55:17 +090035#include "config.h"
Adrian Hunter2c47db92019-06-04 16:00:09 +030036#include "time-utils.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030037
Adrian Hunter9e9a6182019-06-10 10:27:59 +030038#include "../arch/x86/include/uapi/asm/perf_regs.h"
39
Adrian Hunter90e457f2015-07-17 19:33:41 +030040#include "intel-pt-decoder/intel-pt-log.h"
41#include "intel-pt-decoder/intel-pt-decoder.h"
42#include "intel-pt-decoder/intel-pt-insn-decoder.h"
43#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
44
45#define MAX_TIMESTAMP (~0ULL)
46
Adrian Hunter2c47db92019-06-04 16:00:09 +030047struct range {
48 u64 start;
49 u64 end;
50};
51
Adrian Hunter90e457f2015-07-17 19:33:41 +030052struct intel_pt {
53 struct auxtrace auxtrace;
54 struct auxtrace_queues queues;
55 struct auxtrace_heap heap;
56 u32 auxtrace_type;
57 struct perf_session *session;
58 struct machine *machine;
59 struct perf_evsel *switch_evsel;
60 struct thread *unknown_thread;
61 bool timeless_decoding;
62 bool sampling_mode;
63 bool snapshot_mode;
64 bool per_cpu_mmaps;
65 bool have_tsc;
66 bool data_queued;
67 bool est_tsc;
68 bool sync_switch;
Adrian Hunterba11ba62015-09-25 16:15:56 +030069 bool mispred_all;
Adrian Hunter90e457f2015-07-17 19:33:41 +030070 int have_sched_switch;
71 u32 pmu_type;
72 u64 kernel_start;
73 u64 switch_ip;
74 u64 ptss_ip;
75
76 struct perf_tsc_conversion tc;
77 bool cap_user_time_zero;
78
79 struct itrace_synth_opts synth_opts;
80
81 bool sample_instructions;
82 u64 instructions_sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +030083 u64 instructions_id;
84
85 bool sample_branches;
86 u32 branches_filter;
87 u64 branches_sample_type;
88 u64 branches_id;
89
90 bool sample_transactions;
91 u64 transactions_sample_type;
92 u64 transactions_id;
93
Adrian Hunter37973072017-06-30 11:36:45 +030094 bool sample_ptwrites;
95 u64 ptwrites_sample_type;
96 u64 ptwrites_id;
97
98 bool sample_pwr_events;
99 u64 pwr_events_sample_type;
100 u64 mwait_id;
101 u64 pwre_id;
102 u64 exstop_id;
103 u64 pwrx_id;
104 u64 cbr_id;
105
Adrian Huntere62ca652019-06-10 10:27:56 +0300106 bool sample_pebs;
107 struct perf_evsel *pebs_evsel;
108
Adrian Hunter90e457f2015-07-17 19:33:41 +0300109 u64 tsc_bit;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300110 u64 mtc_bit;
111 u64 mtc_freq_bits;
112 u32 tsc_ctc_ratio_n;
113 u32 tsc_ctc_ratio_d;
114 u64 cyc_bit;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300115 u64 noretcomp_bit;
116 unsigned max_non_turbo_ratio;
Adrian Hunter37973072017-06-30 11:36:45 +0300117 unsigned cbr2khz;
Andi Kleend1706b32016-03-28 10:45:38 -0700118
119 unsigned long num_events;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +0300120
121 char *filter;
Adrian Hunter2acee102016-09-23 17:38:48 +0300122 struct addr_filters filts;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300123
124 struct range *time_ranges;
125 unsigned int range_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300126};
127
128enum switch_state {
129 INTEL_PT_SS_NOT_TRACING,
130 INTEL_PT_SS_UNKNOWN,
131 INTEL_PT_SS_TRACING,
132 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
133 INTEL_PT_SS_EXPECTING_SWITCH_IP,
134};
135
136struct intel_pt_queue {
137 struct intel_pt *pt;
138 unsigned int queue_nr;
139 struct auxtrace_buffer *buffer;
Adrian Hunter9c665062018-03-07 16:02:27 +0200140 struct auxtrace_buffer *old_buffer;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300141 void *decoder;
142 const struct intel_pt_state *state;
143 struct ip_callchain *chain;
Adrian Hunterf14445e2015-09-25 16:15:45 +0300144 struct branch_stack *last_branch;
145 struct branch_stack *last_branch_rb;
146 size_t last_branch_pos;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300147 union perf_event *event_buf;
148 bool on_heap;
149 bool stop;
150 bool step_through_buffers;
151 bool use_buffer_pid_tid;
Adrian Hunter63d8e382018-03-07 16:02:22 +0200152 bool sync_switch;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300153 pid_t pid, tid;
154 int cpu;
155 int switch_state;
156 pid_t next_tid;
157 struct thread *thread;
158 bool exclude_kernel;
159 bool have_sample;
160 u64 time;
161 u64 timestamp;
Adrian Hunter2c47db92019-06-04 16:00:09 +0300162 u64 sel_timestamp;
163 bool sel_start;
164 unsigned int sel_idx;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300165 u32 flags;
166 u16 insn_len;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300167 u64 last_insn_cnt;
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +0300168 u64 ipc_insn_cnt;
169 u64 ipc_cyc_cnt;
170 u64 last_in_insn_cnt;
171 u64 last_in_cyc_cnt;
172 u64 last_br_insn_cnt;
173 u64 last_br_cyc_cnt;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300174 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300175};
176
177static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
178 unsigned char *buf, size_t len)
179{
180 struct intel_pt_pkt packet;
181 size_t pos = 0;
182 int ret, pkt_len, i;
183 char desc[INTEL_PT_PKT_DESC_MAX];
184 const char *color = PERF_COLOR_BLUE;
Adrian Hunteredff7802019-06-10 10:27:53 +0300185 enum intel_pt_pkt_ctx ctx = INTEL_PT_NO_CTX;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300186
187 color_fprintf(stdout, color,
188 ". ... Intel Processor Trace data: size %zu bytes\n",
189 len);
190
191 while (len) {
Adrian Hunteredff7802019-06-10 10:27:53 +0300192 ret = intel_pt_get_packet(buf, len, &packet, &ctx);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300193 if (ret > 0)
194 pkt_len = ret;
195 else
196 pkt_len = 1;
197 printf(".");
198 color_fprintf(stdout, color, " %08x: ", pos);
199 for (i = 0; i < pkt_len; i++)
200 color_fprintf(stdout, color, " %02x", buf[i]);
201 for (; i < 16; i++)
202 color_fprintf(stdout, color, " ");
203 if (ret > 0) {
204 ret = intel_pt_pkt_desc(&packet, desc,
205 INTEL_PT_PKT_DESC_MAX);
206 if (ret > 0)
207 color_fprintf(stdout, color, " %s\n", desc);
208 } else {
209 color_fprintf(stdout, color, " Bad packet!\n");
210 }
211 pos += pkt_len;
212 buf += pkt_len;
213 len -= pkt_len;
214 }
215}
216
217static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
218 size_t len)
219{
220 printf(".\n");
221 intel_pt_dump(pt, buf, len);
222}
223
Adrian Hunter93f8be22018-11-05 09:35:04 +0200224static void intel_pt_log_event(union perf_event *event)
225{
226 FILE *f = intel_pt_log_fp();
227
228 if (!intel_pt_enable_logging || !f)
229 return;
230
231 perf_event__fprintf(event, f);
232}
233
Adrian Hunter90e457f2015-07-17 19:33:41 +0300234static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
235 struct auxtrace_buffer *b)
236{
Adrian Hunter117db4b2018-03-07 16:02:21 +0200237 bool consecutive = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300238 void *start;
239
240 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
Adrian Hunter117db4b2018-03-07 16:02:21 +0200241 pt->have_tsc, &consecutive);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300242 if (!start)
243 return -EINVAL;
244 b->use_size = b->data + b->size - start;
245 b->use_data = start;
Adrian Hunter117db4b2018-03-07 16:02:21 +0200246 if (b->use_size && consecutive)
247 b->consecutive = true;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300248 return 0;
249}
250
Adrian Huntere96f7df2019-06-04 16:00:07 +0300251static int intel_pt_get_buffer(struct intel_pt_queue *ptq,
252 struct auxtrace_buffer *buffer,
253 struct auxtrace_buffer *old_buffer,
254 struct intel_pt_buffer *b)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300255{
Adrian Hunter599a5be2018-03-07 16:02:29 +0200256 bool might_overlap;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300257
Adrian Hunter90e457f2015-07-17 19:33:41 +0300258 if (!buffer->data) {
Jiri Olsa8ceb41d2017-01-23 22:07:59 +0100259 int fd = perf_data__fd(ptq->pt->session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300260
261 buffer->data = auxtrace_buffer__get_data(buffer, fd);
262 if (!buffer->data)
263 return -ENOMEM;
264 }
265
Adrian Hunter599a5be2018-03-07 16:02:29 +0200266 might_overlap = ptq->pt->snapshot_mode || ptq->pt->sampling_mode;
267 if (might_overlap && !buffer->consecutive && old_buffer &&
Adrian Hunter90e457f2015-07-17 19:33:41 +0300268 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
269 return -ENOMEM;
270
Adrian Hunter90e457f2015-07-17 19:33:41 +0300271 if (buffer->use_data) {
272 b->len = buffer->use_size;
273 b->buf = buffer->use_data;
274 } else {
275 b->len = buffer->size;
276 b->buf = buffer->data;
277 }
278 b->ref_timestamp = buffer->reference;
279
Adrian Hunter599a5be2018-03-07 16:02:29 +0200280 if (!old_buffer || (might_overlap && !buffer->consecutive)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300281 b->consecutive = false;
282 b->trace_nr = buffer->buffer_nr + 1;
283 } else {
284 b->consecutive = true;
285 }
286
Adrian Huntere96f7df2019-06-04 16:00:07 +0300287 return 0;
288}
289
Adrian Hunterda9000a2019-06-04 16:00:08 +0300290/* Do not drop buffers with references - refer intel_pt_get_trace() */
291static void intel_pt_lookahead_drop_buffer(struct intel_pt_queue *ptq,
292 struct auxtrace_buffer *buffer)
293{
294 if (!buffer || buffer == ptq->buffer || buffer == ptq->old_buffer)
295 return;
296
297 auxtrace_buffer__drop_data(buffer);
298}
299
300/* Must be serialized with respect to intel_pt_get_trace() */
301static int intel_pt_lookahead(void *data, intel_pt_lookahead_cb_t cb,
302 void *cb_data)
303{
304 struct intel_pt_queue *ptq = data;
305 struct auxtrace_buffer *buffer = ptq->buffer;
306 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
307 struct auxtrace_queue *queue;
308 int err = 0;
309
310 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
311
312 while (1) {
313 struct intel_pt_buffer b = { .len = 0 };
314
315 buffer = auxtrace_buffer__next(queue, buffer);
316 if (!buffer)
317 break;
318
319 err = intel_pt_get_buffer(ptq, buffer, old_buffer, &b);
320 if (err)
321 break;
322
323 if (b.len) {
324 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
325 old_buffer = buffer;
326 } else {
327 intel_pt_lookahead_drop_buffer(ptq, buffer);
328 continue;
329 }
330
331 err = cb(&b, cb_data);
332 if (err)
333 break;
334 }
335
336 if (buffer != old_buffer)
337 intel_pt_lookahead_drop_buffer(ptq, buffer);
338 intel_pt_lookahead_drop_buffer(ptq, old_buffer);
339
340 return err;
341}
342
343/*
344 * This function assumes data is processed sequentially only.
345 * Must be serialized with respect to intel_pt_lookahead()
346 */
Adrian Huntere96f7df2019-06-04 16:00:07 +0300347static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
348{
349 struct intel_pt_queue *ptq = data;
350 struct auxtrace_buffer *buffer = ptq->buffer;
351 struct auxtrace_buffer *old_buffer = ptq->old_buffer;
352 struct auxtrace_queue *queue;
353 int err;
354
355 if (ptq->stop) {
356 b->len = 0;
357 return 0;
358 }
359
360 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
361
362 buffer = auxtrace_buffer__next(queue, buffer);
363 if (!buffer) {
364 if (old_buffer)
365 auxtrace_buffer__drop_data(old_buffer);
366 b->len = 0;
367 return 0;
368 }
369
370 ptq->buffer = buffer;
371
372 err = intel_pt_get_buffer(ptq, buffer, old_buffer, b);
373 if (err)
374 return err;
375
Adrian Hunter90e457f2015-07-17 19:33:41 +0300376 if (ptq->step_through_buffers)
377 ptq->stop = true;
378
Adrian Hunter9c665062018-03-07 16:02:27 +0200379 if (b->len) {
380 if (old_buffer)
381 auxtrace_buffer__drop_data(old_buffer);
382 ptq->old_buffer = buffer;
383 } else {
384 auxtrace_buffer__drop_data(buffer);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300385 return intel_pt_get_trace(b, data);
Adrian Hunter9c665062018-03-07 16:02:27 +0200386 }
Adrian Hunter90e457f2015-07-17 19:33:41 +0300387
388 return 0;
389}
390
391struct intel_pt_cache_entry {
392 struct auxtrace_cache_entry entry;
393 u64 insn_cnt;
394 u64 byte_cnt;
395 enum intel_pt_insn_op op;
396 enum intel_pt_insn_branch branch;
397 int length;
398 int32_t rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300399 char insn[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300400};
401
402static int intel_pt_config_div(const char *var, const char *value, void *data)
403{
404 int *d = data;
405 long val;
406
407 if (!strcmp(var, "intel-pt.cache-divisor")) {
408 val = strtol(value, NULL, 0);
409 if (val > 0 && val <= INT_MAX)
410 *d = val;
411 }
412
413 return 0;
414}
415
416static int intel_pt_cache_divisor(void)
417{
418 static int d;
419
420 if (d)
421 return d;
422
423 perf_config(intel_pt_config_div, &d);
424
425 if (!d)
426 d = 64;
427
428 return d;
429}
430
431static unsigned int intel_pt_cache_size(struct dso *dso,
432 struct machine *machine)
433{
434 off_t size;
435
436 size = dso__data_size(dso, machine);
437 size /= intel_pt_cache_divisor();
438 if (size < 1000)
439 return 10;
440 if (size > (1 << 21))
441 return 21;
442 return 32 - __builtin_clz(size);
443}
444
445static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
446 struct machine *machine)
447{
448 struct auxtrace_cache *c;
449 unsigned int bits;
450
451 if (dso->auxtrace_cache)
452 return dso->auxtrace_cache;
453
454 bits = intel_pt_cache_size(dso, machine);
455
456 /* Ignoring cache creation failure */
457 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
458
459 dso->auxtrace_cache = c;
460
461 return c;
462}
463
464static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
465 u64 offset, u64 insn_cnt, u64 byte_cnt,
466 struct intel_pt_insn *intel_pt_insn)
467{
468 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
469 struct intel_pt_cache_entry *e;
470 int err;
471
472 if (!c)
473 return -ENOMEM;
474
475 e = auxtrace_cache__alloc_entry(c);
476 if (!e)
477 return -ENOMEM;
478
479 e->insn_cnt = insn_cnt;
480 e->byte_cnt = byte_cnt;
481 e->op = intel_pt_insn->op;
482 e->branch = intel_pt_insn->branch;
483 e->length = intel_pt_insn->length;
484 e->rel = intel_pt_insn->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300485 memcpy(e->insn, intel_pt_insn->buf, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300486
487 err = auxtrace_cache__add(c, offset, &e->entry);
488 if (err)
489 auxtrace_cache__free_entry(c, e);
490
491 return err;
492}
493
494static struct intel_pt_cache_entry *
495intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
496{
497 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
498
499 if (!c)
500 return NULL;
501
502 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
503}
504
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200505static inline u8 intel_pt_cpumode(struct intel_pt *pt, uint64_t ip)
506{
507 return ip >= pt->kernel_start ?
508 PERF_RECORD_MISC_KERNEL :
509 PERF_RECORD_MISC_USER;
510}
511
Adrian Hunter90e457f2015-07-17 19:33:41 +0300512static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
513 uint64_t *insn_cnt_ptr, uint64_t *ip,
514 uint64_t to_ip, uint64_t max_insn_cnt,
515 void *data)
516{
517 struct intel_pt_queue *ptq = data;
518 struct machine *machine = ptq->pt->machine;
519 struct thread *thread;
520 struct addr_location al;
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300521 unsigned char buf[INTEL_PT_INSN_BUF_SZ];
Adrian Hunter90e457f2015-07-17 19:33:41 +0300522 ssize_t len;
523 int x86_64;
524 u8 cpumode;
525 u64 offset, start_offset, start_ip;
526 u64 insn_cnt = 0;
527 bool one_map = true;
528
Andi Kleenfaaa8762016-10-07 16:42:26 +0300529 intel_pt_insn->length = 0;
530
Adrian Hunter90e457f2015-07-17 19:33:41 +0300531 if (to_ip && *ip == to_ip)
532 goto out_no_cache;
533
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +0200534 cpumode = intel_pt_cpumode(ptq->pt, *ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300535
536 thread = ptq->thread;
537 if (!thread) {
538 if (cpumode != PERF_RECORD_MISC_KERNEL)
539 return -EINVAL;
540 thread = ptq->pt->unknown_thread;
541 }
542
543 while (1) {
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300544 if (!thread__find_map(thread, cpumode, *ip, &al) || !al.map->dso)
Adrian Hunter90e457f2015-07-17 19:33:41 +0300545 return -EINVAL;
546
547 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
548 dso__data_status_seen(al.map->dso,
549 DSO_DATA_STATUS_SEEN_ITRACE))
550 return -ENOENT;
551
552 offset = al.map->map_ip(al.map, *ip);
553
554 if (!to_ip && one_map) {
555 struct intel_pt_cache_entry *e;
556
557 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
558 if (e &&
559 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
560 *insn_cnt_ptr = e->insn_cnt;
561 *ip += e->byte_cnt;
562 intel_pt_insn->op = e->op;
563 intel_pt_insn->branch = e->branch;
564 intel_pt_insn->length = e->length;
565 intel_pt_insn->rel = e->rel;
Andi Kleenfaaa8762016-10-07 16:42:26 +0300566 memcpy(intel_pt_insn->buf, e->insn,
567 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300568 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
569 return 0;
570 }
571 }
572
573 start_offset = offset;
574 start_ip = *ip;
575
576 /* Load maps to ensure dso->is_64_bit has been updated */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300577 map__load(al.map);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300578
579 x86_64 = al.map->dso->is_64_bit;
580
581 while (1) {
582 len = dso__data_read_offset(al.map->dso, machine,
Adrian Hunter32f98aa2016-10-07 16:42:25 +0300583 offset, buf,
584 INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300585 if (len <= 0)
586 return -EINVAL;
587
588 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
589 return -EINVAL;
590
591 intel_pt_log_insn(intel_pt_insn, *ip);
592
593 insn_cnt += 1;
594
595 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
596 goto out;
597
598 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
599 goto out_no_cache;
600
601 *ip += intel_pt_insn->length;
602
603 if (to_ip && *ip == to_ip)
604 goto out_no_cache;
605
606 if (*ip >= al.map->end)
607 break;
608
609 offset += intel_pt_insn->length;
610 }
611 one_map = false;
612 }
613out:
614 *insn_cnt_ptr = insn_cnt;
615
616 if (!one_map)
617 goto out_no_cache;
618
619 /*
620 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
621 * entries.
622 */
623 if (to_ip) {
624 struct intel_pt_cache_entry *e;
625
626 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
627 if (e)
628 return 0;
629 }
630
631 /* Ignore cache errors */
632 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
633 *ip - start_ip, intel_pt_insn);
634
635 return 0;
636
637out_no_cache:
638 *insn_cnt_ptr = insn_cnt;
639 return 0;
640}
641
Adrian Hunter2acee102016-09-23 17:38:48 +0300642static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
643 uint64_t offset, const char *filename)
644{
645 struct addr_filter *filt;
646 bool have_filter = false;
647 bool hit_tracestop = false;
648 bool hit_filter = false;
649
650 list_for_each_entry(filt, &pt->filts.head, list) {
651 if (filt->start)
652 have_filter = true;
653
654 if ((filename && !filt->filename) ||
655 (!filename && filt->filename) ||
656 (filename && strcmp(filename, filt->filename)))
657 continue;
658
659 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
660 continue;
661
662 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
663 ip, offset, filename ? filename : "[kernel]",
664 filt->start ? "filter" : "stop",
665 filt->addr, filt->size);
666
667 if (filt->start)
668 hit_filter = true;
669 else
670 hit_tracestop = true;
671 }
672
673 if (!hit_tracestop && !hit_filter)
674 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
675 ip, offset, filename ? filename : "[kernel]");
676
677 return hit_tracestop || (have_filter && !hit_filter);
678}
679
680static int __intel_pt_pgd_ip(uint64_t ip, void *data)
681{
682 struct intel_pt_queue *ptq = data;
683 struct thread *thread;
684 struct addr_location al;
685 u8 cpumode;
686 u64 offset;
687
688 if (ip >= ptq->pt->kernel_start)
689 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
690
691 cpumode = PERF_RECORD_MISC_USER;
692
693 thread = ptq->thread;
694 if (!thread)
695 return -EINVAL;
696
Arnaldo Carvalho de Melo71a84b52018-04-24 11:58:56 -0300697 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso)
Adrian Hunter2acee102016-09-23 17:38:48 +0300698 return -EINVAL;
699
700 offset = al.map->map_ip(al.map, ip);
701
702 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
703 al.map->dso->long_name);
704}
705
706static bool intel_pt_pgd_ip(uint64_t ip, void *data)
707{
708 return __intel_pt_pgd_ip(ip, data) > 0;
709}
710
Adrian Hunter90e457f2015-07-17 19:33:41 +0300711static bool intel_pt_get_config(struct intel_pt *pt,
712 struct perf_event_attr *attr, u64 *config)
713{
714 if (attr->type == pt->pmu_type) {
715 if (config)
716 *config = attr->config;
717 return true;
718 }
719
720 return false;
721}
722
723static bool intel_pt_exclude_kernel(struct intel_pt *pt)
724{
725 struct perf_evsel *evsel;
726
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300727 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300728 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
729 !evsel->attr.exclude_kernel)
730 return false;
731 }
732 return true;
733}
734
735static bool intel_pt_return_compression(struct intel_pt *pt)
736{
737 struct perf_evsel *evsel;
738 u64 config;
739
740 if (!pt->noretcomp_bit)
741 return true;
742
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300743 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300744 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
745 (config & pt->noretcomp_bit))
746 return false;
747 }
748 return true;
749}
750
Adrian Hunter83959812017-05-26 11:17:11 +0300751static bool intel_pt_branch_enable(struct intel_pt *pt)
752{
753 struct perf_evsel *evsel;
754 u64 config;
755
756 evlist__for_each_entry(pt->session->evlist, evsel) {
757 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
758 (config & 1) && !(config & 0x2000))
759 return false;
760 }
761 return true;
762}
763
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300764static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
765{
766 struct perf_evsel *evsel;
767 unsigned int shift;
768 u64 config;
769
770 if (!pt->mtc_freq_bits)
771 return 0;
772
773 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
774 config >>= 1;
775
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300776 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300777 if (intel_pt_get_config(pt, &evsel->attr, &config))
778 return (config & pt->mtc_freq_bits) >> shift;
779 }
780 return 0;
781}
782
Adrian Hunter90e457f2015-07-17 19:33:41 +0300783static bool intel_pt_timeless_decoding(struct intel_pt *pt)
784{
785 struct perf_evsel *evsel;
786 bool timeless_decoding = true;
787 u64 config;
788
789 if (!pt->tsc_bit || !pt->cap_user_time_zero)
790 return true;
791
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300792 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300793 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
794 return true;
795 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
796 if (config & pt->tsc_bit)
797 timeless_decoding = false;
798 else
799 return true;
800 }
801 }
802 return timeless_decoding;
803}
804
805static bool intel_pt_tracing_kernel(struct intel_pt *pt)
806{
807 struct perf_evsel *evsel;
808
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300809 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300810 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
811 !evsel->attr.exclude_kernel)
812 return true;
813 }
814 return false;
815}
816
817static bool intel_pt_have_tsc(struct intel_pt *pt)
818{
819 struct perf_evsel *evsel;
820 bool have_tsc = false;
821 u64 config;
822
823 if (!pt->tsc_bit)
824 return false;
825
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300826 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300827 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
828 if (config & pt->tsc_bit)
829 have_tsc = true;
830 else
831 return false;
832 }
833 }
834 return have_tsc;
835}
836
837static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
838{
839 u64 quot, rem;
840
841 quot = ns / pt->tc.time_mult;
842 rem = ns % pt->tc.time_mult;
843 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
844 pt->tc.time_mult;
845}
846
847static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
848 unsigned int queue_nr)
849{
850 struct intel_pt_params params = { .get_trace = 0, };
Adrian Hunter9fb52332018-05-31 13:23:45 +0300851 struct perf_env *env = pt->machine->env;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300852 struct intel_pt_queue *ptq;
853
854 ptq = zalloc(sizeof(struct intel_pt_queue));
855 if (!ptq)
856 return NULL;
857
858 if (pt->synth_opts.callchain) {
859 size_t sz = sizeof(struct ip_callchain);
860
Adrian Hunter24248302018-10-31 11:10:42 +0200861 /* Add 1 to callchain_sz for callchain context */
862 sz += (pt->synth_opts.callchain_sz + 1) * sizeof(u64);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300863 ptq->chain = zalloc(sz);
864 if (!ptq->chain)
865 goto out_free;
866 }
867
Adrian Hunterf14445e2015-09-25 16:15:45 +0300868 if (pt->synth_opts.last_branch) {
869 size_t sz = sizeof(struct branch_stack);
870
871 sz += pt->synth_opts.last_branch_sz *
872 sizeof(struct branch_entry);
873 ptq->last_branch = zalloc(sz);
874 if (!ptq->last_branch)
875 goto out_free;
876 ptq->last_branch_rb = zalloc(sz);
877 if (!ptq->last_branch_rb)
878 goto out_free;
879 }
880
Adrian Hunter90e457f2015-07-17 19:33:41 +0300881 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
882 if (!ptq->event_buf)
883 goto out_free;
884
885 ptq->pt = pt;
886 ptq->queue_nr = queue_nr;
887 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
888 ptq->pid = -1;
889 ptq->tid = -1;
890 ptq->cpu = -1;
891 ptq->next_tid = -1;
892
893 params.get_trace = intel_pt_get_trace;
894 params.walk_insn = intel_pt_walk_next_insn;
Adrian Hunterda9000a2019-06-04 16:00:08 +0300895 params.lookahead = intel_pt_lookahead;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300896 params.data = ptq;
897 params.return_compression = intel_pt_return_compression(pt);
Adrian Hunter83959812017-05-26 11:17:11 +0300898 params.branch_enable = intel_pt_branch_enable(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300899 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300900 params.mtc_period = intel_pt_mtc_period(pt);
901 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
902 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300903
Adrian Hunter2acee102016-09-23 17:38:48 +0300904 if (pt->filts.cnt > 0)
905 params.pgd_ip = intel_pt_pgd_ip;
906
Adrian Hunter90e457f2015-07-17 19:33:41 +0300907 if (pt->synth_opts.instructions) {
908 if (pt->synth_opts.period) {
909 switch (pt->synth_opts.period_type) {
910 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
911 params.period_type =
912 INTEL_PT_PERIOD_INSTRUCTIONS;
913 params.period = pt->synth_opts.period;
914 break;
915 case PERF_ITRACE_PERIOD_TICKS:
916 params.period_type = INTEL_PT_PERIOD_TICKS;
917 params.period = pt->synth_opts.period;
918 break;
919 case PERF_ITRACE_PERIOD_NANOSECS:
920 params.period_type = INTEL_PT_PERIOD_TICKS;
921 params.period = intel_pt_ns_to_ticks(pt,
922 pt->synth_opts.period);
923 break;
924 default:
925 break;
926 }
927 }
928
929 if (!params.period) {
930 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
Adrian Huntere1791342015-09-25 16:15:32 +0300931 params.period = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300932 }
933 }
934
Adrian Hunter9fb52332018-05-31 13:23:45 +0300935 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
936 params.flags |= INTEL_PT_FUP_WITH_NLIP;
937
Adrian Hunter90e457f2015-07-17 19:33:41 +0300938 ptq->decoder = intel_pt_decoder_new(&params);
939 if (!ptq->decoder)
940 goto out_free;
941
942 return ptq;
943
944out_free:
945 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300946 zfree(&ptq->last_branch);
947 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300948 zfree(&ptq->chain);
949 free(ptq);
950 return NULL;
951}
952
953static void intel_pt_free_queue(void *priv)
954{
955 struct intel_pt_queue *ptq = priv;
956
957 if (!ptq)
958 return;
959 thread__zput(ptq->thread);
960 intel_pt_decoder_free(ptq->decoder);
961 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300962 zfree(&ptq->last_branch);
963 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300964 zfree(&ptq->chain);
965 free(ptq);
966}
967
968static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
969 struct auxtrace_queue *queue)
970{
971 struct intel_pt_queue *ptq = queue->priv;
972
973 if (queue->tid == -1 || pt->have_sched_switch) {
974 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
975 thread__zput(ptq->thread);
976 }
977
978 if (!ptq->thread && ptq->tid != -1)
979 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
980
981 if (ptq->thread) {
982 ptq->pid = ptq->thread->pid_;
983 if (queue->cpu == -1)
984 ptq->cpu = ptq->thread->cpu;
985 }
986}
987
988static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
989{
990 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
991 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
992 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
993 if (ptq->state->to_ip)
994 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
995 PERF_IP_FLAG_ASYNC |
996 PERF_IP_FLAG_INTERRUPT;
997 else
998 ptq->flags = PERF_IP_FLAG_BRANCH |
999 PERF_IP_FLAG_TRACE_END;
1000 ptq->insn_len = 0;
1001 } else {
1002 if (ptq->state->from_ip)
1003 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
1004 else
1005 ptq->flags = PERF_IP_FLAG_BRANCH |
1006 PERF_IP_FLAG_TRACE_BEGIN;
1007 if (ptq->state->flags & INTEL_PT_IN_TX)
1008 ptq->flags |= PERF_IP_FLAG_IN_TX;
1009 ptq->insn_len = ptq->state->insn_len;
Andi Kleenfaaa8762016-10-07 16:42:26 +03001010 memcpy(ptq->insn, ptq->state->insn, INTEL_PT_INSN_BUF_SZ);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001011 }
Adrian Hunterc6b5da02018-09-20 16:00:47 +03001012
1013 if (ptq->state->type & INTEL_PT_TRACE_BEGIN)
1014 ptq->flags |= PERF_IP_FLAG_TRACE_BEGIN;
1015 if (ptq->state->type & INTEL_PT_TRACE_END)
1016 ptq->flags |= PERF_IP_FLAG_TRACE_END;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001017}
1018
Adrian Hunter2c47db92019-06-04 16:00:09 +03001019static void intel_pt_setup_time_range(struct intel_pt *pt,
1020 struct intel_pt_queue *ptq)
1021{
1022 if (!pt->range_cnt)
1023 return;
1024
1025 ptq->sel_timestamp = pt->time_ranges[0].start;
1026 ptq->sel_idx = 0;
1027
1028 if (ptq->sel_timestamp) {
1029 ptq->sel_start = true;
1030 } else {
1031 ptq->sel_timestamp = pt->time_ranges[0].end;
1032 ptq->sel_start = false;
1033 }
1034}
1035
Adrian Hunter90e457f2015-07-17 19:33:41 +03001036static int intel_pt_setup_queue(struct intel_pt *pt,
1037 struct auxtrace_queue *queue,
1038 unsigned int queue_nr)
1039{
1040 struct intel_pt_queue *ptq = queue->priv;
1041
1042 if (list_empty(&queue->head))
1043 return 0;
1044
1045 if (!ptq) {
1046 ptq = intel_pt_alloc_queue(pt, queue_nr);
1047 if (!ptq)
1048 return -ENOMEM;
1049 queue->priv = ptq;
1050
1051 if (queue->cpu != -1)
1052 ptq->cpu = queue->cpu;
1053 ptq->tid = queue->tid;
1054
Adrian Hunter1c071c82018-03-07 16:02:26 +02001055 if (pt->sampling_mode && !pt->snapshot_mode &&
1056 pt->timeless_decoding)
1057 ptq->step_through_buffers = true;
Adrian Hunter63d8e382018-03-07 16:02:22 +02001058
1059 ptq->sync_switch = pt->sync_switch;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001060
1061 intel_pt_setup_time_range(pt, ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001062 }
1063
1064 if (!ptq->on_heap &&
Adrian Hunter63d8e382018-03-07 16:02:22 +02001065 (!ptq->sync_switch ||
Adrian Hunter90e457f2015-07-17 19:33:41 +03001066 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
1067 const struct intel_pt_state *state;
1068 int ret;
1069
1070 if (pt->timeless_decoding)
1071 return 0;
1072
1073 intel_pt_log("queue %u getting timestamp\n", queue_nr);
1074 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1075 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
Adrian Hunter2c47db92019-06-04 16:00:09 +03001076
1077 if (ptq->sel_start && ptq->sel_timestamp) {
1078 ret = intel_pt_fast_forward(ptq->decoder,
1079 ptq->sel_timestamp);
1080 if (ret)
1081 return ret;
1082 }
1083
Adrian Hunter90e457f2015-07-17 19:33:41 +03001084 while (1) {
1085 state = intel_pt_decode(ptq->decoder);
1086 if (state->err) {
1087 if (state->err == INTEL_PT_ERR_NODATA) {
1088 intel_pt_log("queue %u has no timestamp\n",
1089 queue_nr);
1090 return 0;
1091 }
1092 continue;
1093 }
1094 if (state->timestamp)
1095 break;
1096 }
1097
1098 ptq->timestamp = state->timestamp;
1099 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
1100 queue_nr, ptq->timestamp);
1101 ptq->state = state;
1102 ptq->have_sample = true;
Adrian Hunter2c47db92019-06-04 16:00:09 +03001103 if (ptq->sel_start && ptq->sel_timestamp &&
1104 ptq->timestamp < ptq->sel_timestamp)
1105 ptq->have_sample = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001106 intel_pt_sample_flags(ptq);
1107 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
1108 if (ret)
1109 return ret;
1110 ptq->on_heap = true;
1111 }
1112
1113 return 0;
1114}
1115
1116static int intel_pt_setup_queues(struct intel_pt *pt)
1117{
1118 unsigned int i;
1119 int ret;
1120
1121 for (i = 0; i < pt->queues.nr_queues; i++) {
1122 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
1123 if (ret)
1124 return ret;
1125 }
1126 return 0;
1127}
1128
Adrian Hunterf14445e2015-09-25 16:15:45 +03001129static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
1130{
1131 struct branch_stack *bs_src = ptq->last_branch_rb;
1132 struct branch_stack *bs_dst = ptq->last_branch;
1133 size_t nr = 0;
1134
1135 bs_dst->nr = bs_src->nr;
1136
1137 if (!bs_src->nr)
1138 return;
1139
1140 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1141 memcpy(&bs_dst->entries[0],
1142 &bs_src->entries[ptq->last_branch_pos],
1143 sizeof(struct branch_entry) * nr);
1144
1145 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1146 memcpy(&bs_dst->entries[nr],
1147 &bs_src->entries[0],
1148 sizeof(struct branch_entry) * ptq->last_branch_pos);
1149 }
1150}
1151
1152static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1153{
1154 ptq->last_branch_pos = 0;
1155 ptq->last_branch_rb->nr = 0;
1156}
1157
1158static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1159{
1160 const struct intel_pt_state *state = ptq->state;
1161 struct branch_stack *bs = ptq->last_branch_rb;
1162 struct branch_entry *be;
1163
1164 if (!ptq->last_branch_pos)
1165 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1166
1167 ptq->last_branch_pos -= 1;
1168
1169 be = &bs->entries[ptq->last_branch_pos];
1170 be->from = state->from_ip;
1171 be->to = state->to_ip;
1172 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1173 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1174 /* No support for mispredict */
Adrian Hunterba11ba62015-09-25 16:15:56 +03001175 be->flags.mispred = ptq->pt->mispred_all;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001176
1177 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1178 bs->nr += 1;
1179}
1180
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001181static inline bool intel_pt_skip_event(struct intel_pt *pt)
1182{
1183 return pt->synth_opts.initial_skip &&
1184 pt->num_events++ < pt->synth_opts.initial_skip;
1185}
1186
Adrian Hunter0dfded32019-06-10 10:27:57 +03001187static void intel_pt_prep_a_sample(struct intel_pt_queue *ptq,
1188 union perf_event *event,
1189 struct perf_sample *sample)
1190{
1191 event->sample.header.type = PERF_RECORD_SAMPLE;
1192 event->sample.header.size = sizeof(struct perf_event_header);
1193
1194 sample->pid = ptq->pid;
1195 sample->tid = ptq->tid;
1196 sample->cpu = ptq->cpu;
1197 sample->insn_len = ptq->insn_len;
1198 memcpy(sample->insn, ptq->insn, INTEL_PT_INSN_BUF_SZ);
1199}
1200
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001201static void intel_pt_prep_b_sample(struct intel_pt *pt,
1202 struct intel_pt_queue *ptq,
1203 union perf_event *event,
1204 struct perf_sample *sample)
1205{
Adrian Hunter0dfded32019-06-10 10:27:57 +03001206 intel_pt_prep_a_sample(ptq, event, sample);
1207
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001208 if (!pt->timeless_decoding)
1209 sample->time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1210
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001211 sample->ip = ptq->state->from_ip;
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001212 sample->cpumode = intel_pt_cpumode(pt, sample->ip);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001213 sample->addr = ptq->state->to_ip;
1214 sample->period = 1;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001215 sample->flags = ptq->flags;
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001216
Adrian Hunter5d4f0ed2018-10-31 11:10:43 +02001217 event->sample.header.misc = sample->cpumode;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001218}
1219
Adrian Hunter90e457f2015-07-17 19:33:41 +03001220static int intel_pt_inject_event(union perf_event *event,
Adrian Huntera10eb532018-01-16 15:14:50 +02001221 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001222{
1223 event->header.size = perf_event__sample_event_size(sample, type, 0);
Adrian Hunter936f1f32018-01-16 15:14:52 +02001224 return perf_event__synthesize_sample(event, type, 0, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001225}
1226
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001227static inline int intel_pt_opt_inject(struct intel_pt *pt,
1228 union perf_event *event,
1229 struct perf_sample *sample, u64 type)
1230{
1231 if (!pt->synth_opts.inject)
1232 return 0;
1233
Adrian Huntera10eb532018-01-16 15:14:50 +02001234 return intel_pt_inject_event(event, sample, type);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001235}
1236
1237static int intel_pt_deliver_synth_b_event(struct intel_pt *pt,
1238 union perf_event *event,
1239 struct perf_sample *sample, u64 type)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001240{
1241 int ret;
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001242
1243 ret = intel_pt_opt_inject(pt, event, sample, type);
1244 if (ret)
1245 return ret;
1246
1247 ret = perf_session__deliver_synth_event(pt->session, event, sample);
1248 if (ret)
1249 pr_err("Intel PT: failed to deliver event, error %d\n", ret);
1250
1251 return ret;
1252}
1253
1254static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1255{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001256 struct intel_pt *pt = ptq->pt;
1257 union perf_event *event = ptq->event_buf;
1258 struct perf_sample sample = { .ip = 0, };
Adrian Hunterf14445e2015-09-25 16:15:45 +03001259 struct dummy_branch_stack {
1260 u64 nr;
1261 struct branch_entry entries;
1262 } dummy_bs;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001263
Adrian Hunter385e3302015-09-25 16:15:44 +03001264 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1265 return 0;
1266
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001267 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001268 return 0;
1269
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001270 intel_pt_prep_b_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001271
Adrian Hunter90e457f2015-07-17 19:33:41 +03001272 sample.id = ptq->pt->branches_id;
1273 sample.stream_id = ptq->pt->branches_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001274
Adrian Hunterf14445e2015-09-25 16:15:45 +03001275 /*
1276 * perf report cannot handle events without a branch stack when using
1277 * SORT_MODE__BRANCH so make a dummy one.
1278 */
1279 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1280 dummy_bs = (struct dummy_branch_stack){
1281 .nr = 1,
1282 .entries = {
1283 .from = sample.ip,
1284 .to = sample.addr,
1285 },
1286 };
1287 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1288 }
1289
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001290 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_br_cyc_cnt;
1291 if (sample.cyc_cnt) {
1292 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_br_insn_cnt;
1293 ptq->last_br_insn_cnt = ptq->ipc_insn_cnt;
1294 ptq->last_br_cyc_cnt = ptq->ipc_cyc_cnt;
1295 }
1296
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001297 return intel_pt_deliver_synth_b_event(pt, event, &sample,
1298 pt->branches_sample_type);
1299}
1300
1301static void intel_pt_prep_sample(struct intel_pt *pt,
1302 struct intel_pt_queue *ptq,
1303 union perf_event *event,
1304 struct perf_sample *sample)
1305{
1306 intel_pt_prep_b_sample(pt, ptq, event, sample);
1307
1308 if (pt->synth_opts.callchain) {
Adrian Hunter256d92b2018-12-21 14:06:19 +02001309 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
Adrian Hunter24248302018-10-31 11:10:42 +02001310 pt->synth_opts.callchain_sz + 1,
1311 sample->ip, pt->kernel_start);
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001312 sample->callchain = ptq->chain;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001313 }
1314
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001315 if (pt->synth_opts.last_branch) {
1316 intel_pt_copy_last_branch_rb(ptq);
1317 sample->branch_stack = ptq->last_branch;
1318 }
1319}
1320
1321static inline int intel_pt_deliver_synth_event(struct intel_pt *pt,
1322 struct intel_pt_queue *ptq,
1323 union perf_event *event,
1324 struct perf_sample *sample,
1325 u64 type)
1326{
1327 int ret;
1328
1329 ret = intel_pt_deliver_synth_b_event(pt, event, sample, type);
1330
1331 if (pt->synth_opts.last_branch)
1332 intel_pt_reset_last_branch_rb(ptq);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001333
1334 return ret;
1335}
1336
1337static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1338{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001339 struct intel_pt *pt = ptq->pt;
1340 union perf_event *event = ptq->event_buf;
1341 struct perf_sample sample = { .ip = 0, };
1342
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001343 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001344 return 0;
1345
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001346 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001347
Adrian Hunter90e457f2015-07-17 19:33:41 +03001348 sample.id = ptq->pt->instructions_id;
1349 sample.stream_id = ptq->pt->instructions_id;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001350 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001351
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001352 sample.cyc_cnt = ptq->ipc_cyc_cnt - ptq->last_in_cyc_cnt;
1353 if (sample.cyc_cnt) {
1354 sample.insn_cnt = ptq->ipc_insn_cnt - ptq->last_in_insn_cnt;
1355 ptq->last_in_insn_cnt = ptq->ipc_insn_cnt;
1356 ptq->last_in_cyc_cnt = ptq->ipc_cyc_cnt;
1357 }
1358
Adrian Hunter2a21d032015-07-17 19:33:48 +03001359 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1360
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001361 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1362 pt->instructions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001363}
1364
1365static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1366{
Adrian Hunter90e457f2015-07-17 19:33:41 +03001367 struct intel_pt *pt = ptq->pt;
1368 union perf_event *event = ptq->event_buf;
1369 struct perf_sample sample = { .ip = 0, };
1370
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001371 if (intel_pt_skip_event(pt))
Andi Kleend1706b32016-03-28 10:45:38 -07001372 return 0;
1373
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001374 intel_pt_prep_sample(pt, ptq, event, &sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001375
Adrian Hunter90e457f2015-07-17 19:33:41 +03001376 sample.id = ptq->pt->transactions_id;
1377 sample.stream_id = ptq->pt->transactions_id;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001378
Adrian Hunter0f3e5372017-05-26 11:17:27 +03001379 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1380 pt->transactions_sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001381}
1382
Adrian Hunter37973072017-06-30 11:36:45 +03001383static void intel_pt_prep_p_sample(struct intel_pt *pt,
1384 struct intel_pt_queue *ptq,
1385 union perf_event *event,
1386 struct perf_sample *sample)
1387{
1388 intel_pt_prep_sample(pt, ptq, event, sample);
1389
1390 /*
1391 * Zero IP is used to mean "trace start" but that is not the case for
1392 * power or PTWRITE events with no IP, so clear the flags.
1393 */
1394 if (!sample->ip)
1395 sample->flags = 0;
1396}
1397
1398static int intel_pt_synth_ptwrite_sample(struct intel_pt_queue *ptq)
1399{
1400 struct intel_pt *pt = ptq->pt;
1401 union perf_event *event = ptq->event_buf;
1402 struct perf_sample sample = { .ip = 0, };
1403 struct perf_synth_intel_ptwrite raw;
1404
1405 if (intel_pt_skip_event(pt))
1406 return 0;
1407
1408 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1409
1410 sample.id = ptq->pt->ptwrites_id;
1411 sample.stream_id = ptq->pt->ptwrites_id;
1412
1413 raw.flags = 0;
1414 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1415 raw.payload = cpu_to_le64(ptq->state->ptw_payload);
1416
1417 sample.raw_size = perf_synth__raw_size(raw);
1418 sample.raw_data = perf_synth__raw_data(&raw);
1419
1420 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1421 pt->ptwrites_sample_type);
1422}
1423
1424static int intel_pt_synth_cbr_sample(struct intel_pt_queue *ptq)
1425{
1426 struct intel_pt *pt = ptq->pt;
1427 union perf_event *event = ptq->event_buf;
1428 struct perf_sample sample = { .ip = 0, };
1429 struct perf_synth_intel_cbr raw;
1430 u32 flags;
1431
1432 if (intel_pt_skip_event(pt))
1433 return 0;
1434
1435 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1436
1437 sample.id = ptq->pt->cbr_id;
1438 sample.stream_id = ptq->pt->cbr_id;
1439
1440 flags = (u16)ptq->state->cbr_payload | (pt->max_non_turbo_ratio << 16);
1441 raw.flags = cpu_to_le32(flags);
1442 raw.freq = cpu_to_le32(raw.cbr * pt->cbr2khz);
1443 raw.reserved3 = 0;
1444
1445 sample.raw_size = perf_synth__raw_size(raw);
1446 sample.raw_data = perf_synth__raw_data(&raw);
1447
1448 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1449 pt->pwr_events_sample_type);
1450}
1451
1452static int intel_pt_synth_mwait_sample(struct intel_pt_queue *ptq)
1453{
1454 struct intel_pt *pt = ptq->pt;
1455 union perf_event *event = ptq->event_buf;
1456 struct perf_sample sample = { .ip = 0, };
1457 struct perf_synth_intel_mwait raw;
1458
1459 if (intel_pt_skip_event(pt))
1460 return 0;
1461
1462 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1463
1464 sample.id = ptq->pt->mwait_id;
1465 sample.stream_id = ptq->pt->mwait_id;
1466
1467 raw.reserved = 0;
1468 raw.payload = cpu_to_le64(ptq->state->mwait_payload);
1469
1470 sample.raw_size = perf_synth__raw_size(raw);
1471 sample.raw_data = perf_synth__raw_data(&raw);
1472
1473 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1474 pt->pwr_events_sample_type);
1475}
1476
1477static int intel_pt_synth_pwre_sample(struct intel_pt_queue *ptq)
1478{
1479 struct intel_pt *pt = ptq->pt;
1480 union perf_event *event = ptq->event_buf;
1481 struct perf_sample sample = { .ip = 0, };
1482 struct perf_synth_intel_pwre raw;
1483
1484 if (intel_pt_skip_event(pt))
1485 return 0;
1486
1487 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1488
1489 sample.id = ptq->pt->pwre_id;
1490 sample.stream_id = ptq->pt->pwre_id;
1491
1492 raw.reserved = 0;
1493 raw.payload = cpu_to_le64(ptq->state->pwre_payload);
1494
1495 sample.raw_size = perf_synth__raw_size(raw);
1496 sample.raw_data = perf_synth__raw_data(&raw);
1497
1498 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1499 pt->pwr_events_sample_type);
1500}
1501
1502static int intel_pt_synth_exstop_sample(struct intel_pt_queue *ptq)
1503{
1504 struct intel_pt *pt = ptq->pt;
1505 union perf_event *event = ptq->event_buf;
1506 struct perf_sample sample = { .ip = 0, };
1507 struct perf_synth_intel_exstop raw;
1508
1509 if (intel_pt_skip_event(pt))
1510 return 0;
1511
1512 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1513
1514 sample.id = ptq->pt->exstop_id;
1515 sample.stream_id = ptq->pt->exstop_id;
1516
1517 raw.flags = 0;
1518 raw.ip = !!(ptq->state->flags & INTEL_PT_FUP_IP);
1519
1520 sample.raw_size = perf_synth__raw_size(raw);
1521 sample.raw_data = perf_synth__raw_data(&raw);
1522
1523 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1524 pt->pwr_events_sample_type);
1525}
1526
1527static int intel_pt_synth_pwrx_sample(struct intel_pt_queue *ptq)
1528{
1529 struct intel_pt *pt = ptq->pt;
1530 union perf_event *event = ptq->event_buf;
1531 struct perf_sample sample = { .ip = 0, };
1532 struct perf_synth_intel_pwrx raw;
1533
1534 if (intel_pt_skip_event(pt))
1535 return 0;
1536
1537 intel_pt_prep_p_sample(pt, ptq, event, &sample);
1538
1539 sample.id = ptq->pt->pwrx_id;
1540 sample.stream_id = ptq->pt->pwrx_id;
1541
1542 raw.reserved = 0;
1543 raw.payload = cpu_to_le64(ptq->state->pwrx_payload);
1544
1545 sample.raw_size = perf_synth__raw_size(raw);
1546 sample.raw_data = perf_synth__raw_data(&raw);
1547
1548 return intel_pt_deliver_synth_event(pt, ptq, event, &sample,
1549 pt->pwr_events_sample_type);
1550}
1551
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001552/*
1553 * PEBS gp_regs array indexes plus 1 so that 0 means not present. Refer
1554 * intel_pt_add_gp_regs().
1555 */
1556static const int pebs_gp_regs[] = {
1557 [PERF_REG_X86_FLAGS] = 1,
1558 [PERF_REG_X86_IP] = 2,
1559 [PERF_REG_X86_AX] = 3,
1560 [PERF_REG_X86_CX] = 4,
1561 [PERF_REG_X86_DX] = 5,
1562 [PERF_REG_X86_BX] = 6,
1563 [PERF_REG_X86_SP] = 7,
1564 [PERF_REG_X86_BP] = 8,
1565 [PERF_REG_X86_SI] = 9,
1566 [PERF_REG_X86_DI] = 10,
1567 [PERF_REG_X86_R8] = 11,
1568 [PERF_REG_X86_R9] = 12,
1569 [PERF_REG_X86_R10] = 13,
1570 [PERF_REG_X86_R11] = 14,
1571 [PERF_REG_X86_R12] = 15,
1572 [PERF_REG_X86_R13] = 16,
1573 [PERF_REG_X86_R14] = 17,
1574 [PERF_REG_X86_R15] = 18,
1575};
1576
1577static u64 *intel_pt_add_gp_regs(struct regs_dump *intr_regs, u64 *pos,
1578 const struct intel_pt_blk_items *items,
1579 u64 regs_mask)
1580{
1581 const u64 *gp_regs = items->val[INTEL_PT_GP_REGS_POS];
1582 u32 mask = items->mask[INTEL_PT_GP_REGS_POS];
1583 u32 bit;
1584 int i;
1585
1586 for (i = 0, bit = 1; i < PERF_REG_X86_64_MAX; i++, bit <<= 1) {
1587 /* Get the PEBS gp_regs array index */
1588 int n = pebs_gp_regs[i] - 1;
1589
1590 if (n < 0)
1591 continue;
1592 /*
1593 * Add only registers that were requested (i.e. 'regs_mask') and
1594 * that were provided (i.e. 'mask'), and update the resulting
1595 * mask (i.e. 'intr_regs->mask') accordingly.
1596 */
1597 if (mask & 1 << n && regs_mask & bit) {
1598 intr_regs->mask |= bit;
1599 *pos++ = gp_regs[n];
1600 }
1601 }
1602
1603 return pos;
1604}
1605
Adrian Hunter143d34a2019-06-10 10:28:00 +03001606#ifndef PERF_REG_X86_XMM0
1607#define PERF_REG_X86_XMM0 32
1608#endif
1609
1610static void intel_pt_add_xmm(struct regs_dump *intr_regs, u64 *pos,
1611 const struct intel_pt_blk_items *items,
1612 u64 regs_mask)
1613{
1614 u32 mask = items->has_xmm & (regs_mask >> PERF_REG_X86_XMM0);
1615 const u64 *xmm = items->xmm;
1616
1617 /*
1618 * If there are any XMM registers, then there should be all of them.
1619 * Nevertheless, follow the logic to add only registers that were
1620 * requested (i.e. 'regs_mask') and that were provided (i.e. 'mask'),
1621 * and update the resulting mask (i.e. 'intr_regs->mask') accordingly.
1622 */
1623 intr_regs->mask |= (u64)mask << PERF_REG_X86_XMM0;
1624
1625 for (; mask; mask >>= 1, xmm++) {
1626 if (mask & 1)
1627 *pos++ = *xmm;
1628 }
1629}
1630
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001631#define LBR_INFO_MISPRED (1ULL << 63)
1632#define LBR_INFO_IN_TX (1ULL << 62)
1633#define LBR_INFO_ABORT (1ULL << 61)
1634#define LBR_INFO_CYCLES 0xffff
1635
1636/* Refer kernel's intel_pmu_store_pebs_lbrs() */
1637static u64 intel_pt_lbr_flags(u64 info)
1638{
1639 union {
1640 struct branch_flags flags;
1641 u64 result;
1642 } u = {
1643 .flags = {
1644 .mispred = !!(info & LBR_INFO_MISPRED),
1645 .predicted = !(info & LBR_INFO_MISPRED),
1646 .in_tx = !!(info & LBR_INFO_IN_TX),
1647 .abort = !!(info & LBR_INFO_ABORT),
1648 .cycles = info & LBR_INFO_CYCLES,
1649 }
1650 };
1651
1652 return u.result;
1653}
1654
1655static void intel_pt_add_lbrs(struct branch_stack *br_stack,
1656 const struct intel_pt_blk_items *items)
1657{
1658 u64 *to;
1659 int i;
1660
1661 br_stack->nr = 0;
1662
1663 to = &br_stack->entries[0].from;
1664
1665 for (i = INTEL_PT_LBR_0_POS; i <= INTEL_PT_LBR_2_POS; i++) {
1666 u32 mask = items->mask[i];
1667 const u64 *from = items->val[i];
1668
1669 for (; mask; mask >>= 3, from += 3) {
1670 if ((mask & 7) == 7) {
1671 *to++ = from[0];
1672 *to++ = from[1];
1673 *to++ = intel_pt_lbr_flags(from[2]);
1674 br_stack->nr += 1;
1675 }
1676 }
1677 }
1678}
1679
1680/* INTEL_PT_LBR_0, INTEL_PT_LBR_1 and INTEL_PT_LBR_2 */
1681#define LBRS_MAX (INTEL_PT_BLK_ITEM_ID_CNT * 3)
1682
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001683static int intel_pt_synth_pebs_sample(struct intel_pt_queue *ptq)
Adrian Huntere62ca652019-06-10 10:27:56 +03001684{
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001685 const struct intel_pt_blk_items *items = &ptq->state->items;
1686 struct perf_sample sample = { .ip = 0, };
1687 union perf_event *event = ptq->event_buf;
1688 struct intel_pt *pt = ptq->pt;
1689 struct perf_evsel *evsel = pt->pebs_evsel;
1690 u64 sample_type = evsel->attr.sample_type;
1691 u64 id = evsel->id[0];
1692 u8 cpumode;
1693
1694 if (intel_pt_skip_event(pt))
1695 return 0;
1696
1697 intel_pt_prep_a_sample(ptq, event, &sample);
1698
1699 sample.id = id;
1700 sample.stream_id = id;
1701
1702 if (!evsel->attr.freq)
1703 sample.period = evsel->attr.sample_period;
1704
1705 /* No support for non-zero CS base */
1706 if (items->has_ip)
1707 sample.ip = items->ip;
1708 else if (items->has_rip)
1709 sample.ip = items->rip;
1710 else
1711 sample.ip = ptq->state->from_ip;
1712
1713 /* No support for guest mode at this time */
1714 cpumode = sample.ip < ptq->pt->kernel_start ?
1715 PERF_RECORD_MISC_USER :
1716 PERF_RECORD_MISC_KERNEL;
1717
1718 event->sample.header.misc = cpumode | PERF_RECORD_MISC_EXACT_IP;
1719
1720 sample.cpumode = cpumode;
1721
1722 if (sample_type & PERF_SAMPLE_TIME) {
1723 u64 timestamp = 0;
1724
1725 if (items->has_timestamp)
1726 timestamp = items->timestamp;
1727 else if (!pt->timeless_decoding)
1728 timestamp = ptq->timestamp;
1729 if (timestamp)
1730 sample.time = tsc_to_perf_time(timestamp, &pt->tc);
1731 }
1732
Adrian Huntere01f0ef2019-06-10 10:28:03 +03001733 if (sample_type & PERF_SAMPLE_CALLCHAIN &&
1734 pt->synth_opts.callchain) {
1735 thread_stack__sample(ptq->thread, ptq->cpu, ptq->chain,
1736 pt->synth_opts.callchain_sz, sample.ip,
1737 pt->kernel_start);
1738 sample.callchain = ptq->chain;
1739 }
1740
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001741 if (sample_type & PERF_SAMPLE_REGS_INTR &&
1742 items->mask[INTEL_PT_GP_REGS_POS]) {
1743 u64 regs[sizeof(sample.intr_regs.mask)];
1744 u64 regs_mask = evsel->attr.sample_regs_intr;
Adrian Hunter143d34a2019-06-10 10:28:00 +03001745 u64 *pos;
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001746
1747 sample.intr_regs.abi = items->is_32_bit ?
1748 PERF_SAMPLE_REGS_ABI_32 :
1749 PERF_SAMPLE_REGS_ABI_64;
1750 sample.intr_regs.regs = regs;
1751
Adrian Hunter143d34a2019-06-10 10:28:00 +03001752 pos = intel_pt_add_gp_regs(&sample.intr_regs, regs, items, regs_mask);
1753
1754 intel_pt_add_xmm(&sample.intr_regs, pos, items, regs_mask);
Adrian Hunter9e9a6182019-06-10 10:27:59 +03001755 }
1756
Adrian Hunteraa62afd2019-06-10 10:28:01 +03001757 if (sample_type & PERF_SAMPLE_BRANCH_STACK) {
1758 struct {
1759 struct branch_stack br_stack;
1760 struct branch_entry entries[LBRS_MAX];
1761 } br;
1762
1763 if (items->mask[INTEL_PT_LBR_0_POS] ||
1764 items->mask[INTEL_PT_LBR_1_POS] ||
1765 items->mask[INTEL_PT_LBR_2_POS]) {
1766 intel_pt_add_lbrs(&br.br_stack, items);
1767 sample.branch_stack = &br.br_stack;
1768 } else if (pt->synth_opts.last_branch) {
1769 intel_pt_copy_last_branch_rb(ptq);
1770 sample.branch_stack = ptq->last_branch;
1771 } else {
1772 br.br_stack.nr = 0;
1773 sample.branch_stack = &br.br_stack;
1774 }
1775 }
1776
Adrian Hunter975846e2019-06-10 10:28:02 +03001777 if (sample_type & PERF_SAMPLE_ADDR && items->has_mem_access_address)
1778 sample.addr = items->mem_access_address;
1779
1780 if (sample_type & PERF_SAMPLE_WEIGHT) {
1781 /*
1782 * Refer kernel's setup_pebs_adaptive_sample_data() and
1783 * intel_hsw_weight().
1784 */
1785 if (items->has_mem_access_latency)
1786 sample.weight = items->mem_access_latency;
1787 if (!sample.weight && items->has_tsx_aux_info) {
1788 /* Cycles last block */
1789 sample.weight = (u32)items->tsx_aux_info;
1790 }
1791 }
1792
1793 if (sample_type & PERF_SAMPLE_TRANSACTION && items->has_tsx_aux_info) {
1794 u64 ax = items->has_rax ? items->rax : 0;
1795 /* Refer kernel's intel_hsw_transaction() */
1796 u64 txn = (u8)(items->tsx_aux_info >> 32);
1797
1798 /* For RTM XABORTs also log the abort code from AX */
1799 if (txn & PERF_TXN_TRANSACTION && ax & 1)
1800 txn |= ((ax >> 24) & 0xff) << PERF_TXN_ABORT_SHIFT;
1801 sample.transaction = txn;
1802 }
1803
Adrian Hunter9d0bc532019-06-10 10:27:58 +03001804 return intel_pt_deliver_synth_event(pt, ptq, event, &sample, sample_type);
Adrian Huntere62ca652019-06-10 10:27:56 +03001805}
1806
Adrian Hunter90e457f2015-07-17 19:33:41 +03001807static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02001808 pid_t pid, pid_t tid, u64 ip, u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001809{
1810 union perf_event event;
1811 char msg[MAX_AUXTRACE_ERROR_MSG];
1812 int err;
1813
1814 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1815
1816 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
Adrian Hunter16bd4322019-02-06 12:39:47 +02001817 code, cpu, pid, tid, ip, msg, timestamp);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001818
1819 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1820 if (err)
1821 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1822 err);
1823
1824 return err;
1825}
1826
Adrian Hunter16bd4322019-02-06 12:39:47 +02001827static int intel_ptq_synth_error(struct intel_pt_queue *ptq,
1828 const struct intel_pt_state *state)
1829{
1830 struct intel_pt *pt = ptq->pt;
1831 u64 tm = ptq->timestamp;
1832
1833 tm = pt->timeless_decoding ? 0 : tsc_to_perf_time(tm, &pt->tc);
1834
1835 return intel_pt_synth_error(pt, state->err, ptq->cpu, ptq->pid,
1836 ptq->tid, state->from_ip, tm);
1837}
1838
Adrian Hunter90e457f2015-07-17 19:33:41 +03001839static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1840{
1841 struct auxtrace_queue *queue;
1842 pid_t tid = ptq->next_tid;
1843 int err;
1844
1845 if (tid == -1)
1846 return 0;
1847
1848 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1849
1850 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1851
1852 queue = &pt->queues.queue_array[ptq->queue_nr];
1853 intel_pt_set_pid_tid_cpu(pt, queue);
1854
1855 ptq->next_tid = -1;
1856
1857 return err;
1858}
1859
1860static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1861{
1862 struct intel_pt *pt = ptq->pt;
1863
1864 return ip == pt->switch_ip &&
1865 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1866 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1867 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1868}
1869
Adrian Hunter37973072017-06-30 11:36:45 +03001870#define INTEL_PT_PWR_EVT (INTEL_PT_MWAIT_OP | INTEL_PT_PWR_ENTRY | \
1871 INTEL_PT_EX_STOP | INTEL_PT_PWR_EXIT | \
1872 INTEL_PT_CBR_CHG)
1873
Adrian Hunter90e457f2015-07-17 19:33:41 +03001874static int intel_pt_sample(struct intel_pt_queue *ptq)
1875{
1876 const struct intel_pt_state *state = ptq->state;
1877 struct intel_pt *pt = ptq->pt;
1878 int err;
1879
1880 if (!ptq->have_sample)
1881 return 0;
1882
1883 ptq->have_sample = false;
1884
Adrian Hunter5b1dc0f2019-05-20 14:37:13 +03001885 if (ptq->state->tot_cyc_cnt > ptq->ipc_cyc_cnt) {
1886 /*
1887 * Cycle count and instruction count only go together to create
1888 * a valid IPC ratio when the cycle count changes.
1889 */
1890 ptq->ipc_insn_cnt = ptq->state->tot_insn_cnt;
1891 ptq->ipc_cyc_cnt = ptq->state->tot_cyc_cnt;
1892 }
1893
Adrian Huntere62ca652019-06-10 10:27:56 +03001894 /*
1895 * Do PEBS first to allow for the possibility that the PEBS timestamp
1896 * precedes the current timestamp.
1897 */
1898 if (pt->sample_pebs && state->type & INTEL_PT_BLK_ITEMS) {
1899 err = intel_pt_synth_pebs_sample(ptq);
1900 if (err)
1901 return err;
1902 }
1903
Adrian Hunter37973072017-06-30 11:36:45 +03001904 if (pt->sample_pwr_events && (state->type & INTEL_PT_PWR_EVT)) {
1905 if (state->type & INTEL_PT_CBR_CHG) {
1906 err = intel_pt_synth_cbr_sample(ptq);
1907 if (err)
1908 return err;
1909 }
1910 if (state->type & INTEL_PT_MWAIT_OP) {
1911 err = intel_pt_synth_mwait_sample(ptq);
1912 if (err)
1913 return err;
1914 }
1915 if (state->type & INTEL_PT_PWR_ENTRY) {
1916 err = intel_pt_synth_pwre_sample(ptq);
1917 if (err)
1918 return err;
1919 }
1920 if (state->type & INTEL_PT_EX_STOP) {
1921 err = intel_pt_synth_exstop_sample(ptq);
1922 if (err)
1923 return err;
1924 }
1925 if (state->type & INTEL_PT_PWR_EXIT) {
1926 err = intel_pt_synth_pwrx_sample(ptq);
1927 if (err)
1928 return err;
1929 }
1930 }
1931
Adrian Hunter406a1802017-05-26 11:17:29 +03001932 if (pt->sample_instructions && (state->type & INTEL_PT_INSTRUCTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001933 err = intel_pt_synth_instruction_sample(ptq);
1934 if (err)
1935 return err;
1936 }
1937
Adrian Hunter406a1802017-05-26 11:17:29 +03001938 if (pt->sample_transactions && (state->type & INTEL_PT_TRANSACTION)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001939 err = intel_pt_synth_transaction_sample(ptq);
1940 if (err)
1941 return err;
1942 }
1943
Adrian Hunter37973072017-06-30 11:36:45 +03001944 if (pt->sample_ptwrites && (state->type & INTEL_PT_PTW)) {
1945 err = intel_pt_synth_ptwrite_sample(ptq);
1946 if (err)
1947 return err;
1948 }
1949
Adrian Hunter90e457f2015-07-17 19:33:41 +03001950 if (!(state->type & INTEL_PT_BRANCH))
1951 return 0;
1952
Adrian Hunter50f736372016-06-23 16:40:57 +03001953 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
Adrian Hunter256d92b2018-12-21 14:06:19 +02001954 thread_stack__event(ptq->thread, ptq->cpu, ptq->flags, state->from_ip,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001955 state->to_ip, ptq->insn_len,
1956 state->trace_nr);
1957 else
Adrian Hunter256d92b2018-12-21 14:06:19 +02001958 thread_stack__set_trace_nr(ptq->thread, ptq->cpu, state->trace_nr);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001959
1960 if (pt->sample_branches) {
1961 err = intel_pt_synth_branch_sample(ptq);
1962 if (err)
1963 return err;
1964 }
1965
Adrian Hunterf14445e2015-09-25 16:15:45 +03001966 if (pt->synth_opts.last_branch)
1967 intel_pt_update_last_branch_rb(ptq);
1968
Adrian Hunter63d8e382018-03-07 16:02:22 +02001969 if (!ptq->sync_switch)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001970 return 0;
1971
1972 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1973 switch (ptq->switch_state) {
Adrian Hunterdbcb82b2018-05-31 13:23:42 +03001974 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03001975 case INTEL_PT_SS_UNKNOWN:
1976 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1977 err = intel_pt_next_tid(pt, ptq);
1978 if (err)
1979 return err;
1980 ptq->switch_state = INTEL_PT_SS_TRACING;
1981 break;
1982 default:
1983 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1984 return 1;
1985 }
1986 } else if (!state->to_ip) {
1987 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1988 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1989 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1990 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1991 state->to_ip == pt->ptss_ip &&
1992 (ptq->flags & PERF_IP_FLAG_CALL)) {
1993 ptq->switch_state = INTEL_PT_SS_TRACING;
1994 }
1995
1996 return 0;
1997}
1998
Adrian Hunter86c27862015-08-13 12:40:57 +03001999static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002000{
Adrian Hunter86c27862015-08-13 12:40:57 +03002001 struct machine *machine = pt->machine;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002002 struct map *map;
2003 struct symbol *sym, *start;
2004 u64 ip, switch_ip = 0;
Adrian Hunter86c27862015-08-13 12:40:57 +03002005 const char *ptss;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002006
2007 if (ptss_ip)
2008 *ptss_ip = 0;
2009
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03002010 map = machine__kernel_map(machine);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002011 if (!map)
2012 return 0;
2013
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03002014 if (map__load(map))
Adrian Hunter90e457f2015-07-17 19:33:41 +03002015 return 0;
2016
Arnaldo Carvalho de Melo5cf88a62018-04-25 17:01:46 -03002017 start = dso__first_symbol(map->dso);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002018
2019 for (sym = start; sym; sym = dso__next_symbol(sym)) {
2020 if (sym->binding == STB_GLOBAL &&
2021 !strcmp(sym->name, "__switch_to")) {
2022 ip = map->unmap_ip(map, sym->start);
2023 if (ip >= map->start && ip < map->end) {
2024 switch_ip = ip;
2025 break;
2026 }
2027 }
2028 }
2029
2030 if (!switch_ip || !ptss_ip)
2031 return 0;
2032
Adrian Hunter86c27862015-08-13 12:40:57 +03002033 if (pt->have_sched_switch == 1)
2034 ptss = "perf_trace_sched_switch";
2035 else
2036 ptss = "__perf_event_task_sched_out";
2037
Adrian Hunter90e457f2015-07-17 19:33:41 +03002038 for (sym = start; sym; sym = dso__next_symbol(sym)) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002039 if (!strcmp(sym->name, ptss)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002040 ip = map->unmap_ip(map, sym->start);
2041 if (ip >= map->start && ip < map->end) {
2042 *ptss_ip = ip;
2043 break;
2044 }
2045 }
2046 }
2047
2048 return switch_ip;
2049}
2050
Adrian Hunter63d8e382018-03-07 16:02:22 +02002051static void intel_pt_enable_sync_switch(struct intel_pt *pt)
2052{
2053 unsigned int i;
2054
2055 pt->sync_switch = true;
2056
2057 for (i = 0; i < pt->queues.nr_queues; i++) {
2058 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2059 struct intel_pt_queue *ptq = queue->priv;
2060
2061 if (ptq)
2062 ptq->sync_switch = true;
2063 }
2064}
2065
Adrian Hunter2c47db92019-06-04 16:00:09 +03002066/*
2067 * To filter against time ranges, it is only necessary to look at the next start
2068 * or end time.
2069 */
2070static bool intel_pt_next_time(struct intel_pt_queue *ptq)
2071{
2072 struct intel_pt *pt = ptq->pt;
2073
2074 if (ptq->sel_start) {
2075 /* Next time is an end time */
2076 ptq->sel_start = false;
2077 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].end;
2078 return true;
2079 } else if (ptq->sel_idx + 1 < pt->range_cnt) {
2080 /* Next time is a start time */
2081 ptq->sel_start = true;
2082 ptq->sel_idx += 1;
2083 ptq->sel_timestamp = pt->time_ranges[ptq->sel_idx].start;
2084 return true;
2085 }
2086
2087 /* No next time */
2088 return false;
2089}
2090
2091static int intel_pt_time_filter(struct intel_pt_queue *ptq, u64 *ff_timestamp)
2092{
2093 int err;
2094
2095 while (1) {
2096 if (ptq->sel_start) {
2097 if (ptq->timestamp >= ptq->sel_timestamp) {
2098 /* After start time, so consider next time */
2099 intel_pt_next_time(ptq);
2100 if (!ptq->sel_timestamp) {
2101 /* No end time */
2102 return 0;
2103 }
2104 /* Check against end time */
2105 continue;
2106 }
2107 /* Before start time, so fast forward */
2108 ptq->have_sample = false;
2109 if (ptq->sel_timestamp > *ff_timestamp) {
2110 if (ptq->sync_switch) {
2111 intel_pt_next_tid(ptq->pt, ptq);
2112 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
2113 }
2114 *ff_timestamp = ptq->sel_timestamp;
2115 err = intel_pt_fast_forward(ptq->decoder,
2116 ptq->sel_timestamp);
2117 if (err)
2118 return err;
2119 }
2120 return 0;
2121 } else if (ptq->timestamp > ptq->sel_timestamp) {
2122 /* After end time, so consider next time */
2123 if (!intel_pt_next_time(ptq)) {
2124 /* No next time range, so stop decoding */
2125 ptq->have_sample = false;
2126 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
2127 return 1;
2128 }
2129 /* Check against next start time */
2130 continue;
2131 } else {
2132 /* Before end time */
2133 return 0;
2134 }
2135 }
2136}
2137
Adrian Hunter90e457f2015-07-17 19:33:41 +03002138static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
2139{
2140 const struct intel_pt_state *state = ptq->state;
2141 struct intel_pt *pt = ptq->pt;
Adrian Hunter2c47db92019-06-04 16:00:09 +03002142 u64 ff_timestamp = 0;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002143 int err;
2144
2145 if (!pt->kernel_start) {
2146 pt->kernel_start = machine__kernel_start(pt->machine);
Adrian Hunter86c27862015-08-13 12:40:57 +03002147 if (pt->per_cpu_mmaps &&
2148 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002149 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
2150 !pt->sampling_mode) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002151 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002152 if (pt->switch_ip) {
2153 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
2154 pt->switch_ip, pt->ptss_ip);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002155 intel_pt_enable_sync_switch(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002156 }
2157 }
2158 }
2159
2160 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
2161 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
2162 while (1) {
2163 err = intel_pt_sample(ptq);
2164 if (err)
2165 return err;
2166
2167 state = intel_pt_decode(ptq->decoder);
2168 if (state->err) {
2169 if (state->err == INTEL_PT_ERR_NODATA)
2170 return 1;
Adrian Hunter63d8e382018-03-07 16:02:22 +02002171 if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002172 state->from_ip >= pt->kernel_start) {
Adrian Hunter63d8e382018-03-07 16:02:22 +02002173 ptq->sync_switch = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002174 intel_pt_next_tid(pt, ptq);
2175 }
2176 if (pt->synth_opts.errors) {
Adrian Hunter16bd4322019-02-06 12:39:47 +02002177 err = intel_ptq_synth_error(ptq, state);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002178 if (err)
2179 return err;
2180 }
2181 continue;
2182 }
2183
2184 ptq->state = state;
2185 ptq->have_sample = true;
2186 intel_pt_sample_flags(ptq);
2187
2188 /* Use estimated TSC upon return to user space */
2189 if (pt->est_tsc &&
2190 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
2191 state->to_ip && state->to_ip < pt->kernel_start) {
2192 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2193 state->timestamp, state->est_timestamp);
2194 ptq->timestamp = state->est_timestamp;
2195 /* Use estimated TSC in unknown switch state */
Adrian Hunter63d8e382018-03-07 16:02:22 +02002196 } else if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03002197 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
2198 intel_pt_is_switch_ip(ptq, state->to_ip) &&
2199 ptq->next_tid == -1) {
2200 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
2201 state->timestamp, state->est_timestamp);
2202 ptq->timestamp = state->est_timestamp;
2203 } else if (state->timestamp > ptq->timestamp) {
2204 ptq->timestamp = state->timestamp;
2205 }
2206
Adrian Hunter2c47db92019-06-04 16:00:09 +03002207 if (ptq->sel_timestamp) {
2208 err = intel_pt_time_filter(ptq, &ff_timestamp);
2209 if (err)
2210 return err;
2211 }
2212
Adrian Hunter90e457f2015-07-17 19:33:41 +03002213 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
2214 *timestamp = ptq->timestamp;
2215 return 0;
2216 }
2217 }
2218 return 0;
2219}
2220
2221static inline int intel_pt_update_queues(struct intel_pt *pt)
2222{
2223 if (pt->queues.new_data) {
2224 pt->queues.new_data = false;
2225 return intel_pt_setup_queues(pt);
2226 }
2227 return 0;
2228}
2229
2230static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
2231{
2232 unsigned int queue_nr;
2233 u64 ts;
2234 int ret;
2235
2236 while (1) {
2237 struct auxtrace_queue *queue;
2238 struct intel_pt_queue *ptq;
2239
2240 if (!pt->heap.heap_cnt)
2241 return 0;
2242
2243 if (pt->heap.heap_array[0].ordinal >= timestamp)
2244 return 0;
2245
2246 queue_nr = pt->heap.heap_array[0].queue_nr;
2247 queue = &pt->queues.queue_array[queue_nr];
2248 ptq = queue->priv;
2249
2250 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
2251 queue_nr, pt->heap.heap_array[0].ordinal,
2252 timestamp);
2253
2254 auxtrace_heap__pop(&pt->heap);
2255
2256 if (pt->heap.heap_cnt) {
2257 ts = pt->heap.heap_array[0].ordinal + 1;
2258 if (ts > timestamp)
2259 ts = timestamp;
2260 } else {
2261 ts = timestamp;
2262 }
2263
2264 intel_pt_set_pid_tid_cpu(pt, queue);
2265
2266 ret = intel_pt_run_decoder(ptq, &ts);
2267
2268 if (ret < 0) {
2269 auxtrace_heap__add(&pt->heap, queue_nr, ts);
2270 return ret;
2271 }
2272
2273 if (!ret) {
2274 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
2275 if (ret < 0)
2276 return ret;
2277 } else {
2278 ptq->on_heap = false;
2279 }
2280 }
2281
2282 return 0;
2283}
2284
2285static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
2286 u64 time_)
2287{
2288 struct auxtrace_queues *queues = &pt->queues;
2289 unsigned int i;
2290 u64 ts = 0;
2291
2292 for (i = 0; i < queues->nr_queues; i++) {
2293 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
2294 struct intel_pt_queue *ptq = queue->priv;
2295
2296 if (ptq && (tid == -1 || ptq->tid == tid)) {
2297 ptq->time = time_;
2298 intel_pt_set_pid_tid_cpu(pt, queue);
2299 intel_pt_run_decoder(ptq, &ts);
2300 }
2301 }
2302 return 0;
2303}
2304
2305static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
2306{
2307 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
Adrian Hunter16bd4322019-02-06 12:39:47 +02002308 sample->pid, sample->tid, 0, sample->time);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002309}
2310
2311static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
2312{
2313 unsigned i, j;
2314
2315 if (cpu < 0 || !pt->queues.nr_queues)
2316 return NULL;
2317
2318 if ((unsigned)cpu >= pt->queues.nr_queues)
2319 i = pt->queues.nr_queues - 1;
2320 else
2321 i = cpu;
2322
2323 if (pt->queues.queue_array[i].cpu == cpu)
2324 return pt->queues.queue_array[i].priv;
2325
2326 for (j = 0; i > 0; j++) {
2327 if (pt->queues.queue_array[--i].cpu == cpu)
2328 return pt->queues.queue_array[i].priv;
2329 }
2330
2331 for (; j < pt->queues.nr_queues; j++) {
2332 if (pt->queues.queue_array[j].cpu == cpu)
2333 return pt->queues.queue_array[j].priv;
2334 }
2335
2336 return NULL;
2337}
2338
Adrian Hunter86c27862015-08-13 12:40:57 +03002339static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
2340 u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002341{
2342 struct intel_pt_queue *ptq;
Adrian Hunter86c27862015-08-13 12:40:57 +03002343 int err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002344
2345 if (!pt->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002346 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002347
2348 ptq = intel_pt_cpu_to_ptq(pt, cpu);
Adrian Hunter63d8e382018-03-07 16:02:22 +02002349 if (!ptq || !ptq->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03002350 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002351
2352 switch (ptq->switch_state) {
2353 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002354 break;
2355 case INTEL_PT_SS_UNKNOWN:
2356 case INTEL_PT_SS_TRACING:
2357 ptq->next_tid = tid;
2358 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
2359 return 0;
2360 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2361 if (!ptq->on_heap) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002362 ptq->timestamp = perf_time_to_tsc(timestamp,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002363 &pt->tc);
2364 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
2365 ptq->timestamp);
2366 if (err)
2367 return err;
2368 ptq->on_heap = true;
2369 }
2370 ptq->switch_state = INTEL_PT_SS_TRACING;
2371 break;
2372 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
Adrian Hunter90e457f2015-07-17 19:33:41 +03002373 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
2374 break;
2375 default:
2376 break;
2377 }
Adrian Hunter86c27862015-08-13 12:40:57 +03002378
Adrian Hunter14f1cfd2019-04-12 14:38:30 +03002379 ptq->next_tid = -1;
2380
Adrian Hunter86c27862015-08-13 12:40:57 +03002381 return 1;
2382}
2383
2384static int intel_pt_process_switch(struct intel_pt *pt,
2385 struct perf_sample *sample)
2386{
2387 struct perf_evsel *evsel;
2388 pid_t tid;
2389 int cpu, ret;
2390
2391 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
2392 if (evsel != pt->switch_evsel)
2393 return 0;
2394
2395 tid = perf_evsel__intval(evsel, sample, "next_pid");
2396 cpu = sample->cpu;
2397
2398 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2399 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
2400 &pt->tc));
2401
2402 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2403 if (ret <= 0)
2404 return ret;
2405
Adrian Hunter90e457f2015-07-17 19:33:41 +03002406 return machine__set_current_tid(pt->machine, cpu, -1, tid);
2407}
2408
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002409static int intel_pt_context_switch_in(struct intel_pt *pt,
2410 struct perf_sample *sample)
2411{
2412 pid_t pid = sample->pid;
2413 pid_t tid = sample->tid;
2414 int cpu = sample->cpu;
2415
2416 if (pt->sync_switch) {
2417 struct intel_pt_queue *ptq;
2418
2419 ptq = intel_pt_cpu_to_ptq(pt, cpu);
2420 if (ptq && ptq->sync_switch) {
2421 ptq->next_tid = -1;
2422 switch (ptq->switch_state) {
2423 case INTEL_PT_SS_NOT_TRACING:
2424 case INTEL_PT_SS_UNKNOWN:
2425 case INTEL_PT_SS_TRACING:
2426 break;
2427 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
2428 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
2429 ptq->switch_state = INTEL_PT_SS_TRACING;
2430 break;
2431 default:
2432 break;
2433 }
2434 }
2435 }
2436
2437 /*
2438 * If the current tid has not been updated yet, ensure it is now that
2439 * a "switch in" event has occurred.
2440 */
2441 if (machine__get_current_tid(pt->machine, cpu) == tid)
2442 return 0;
2443
2444 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2445}
2446
Adrian Hunter86c27862015-08-13 12:40:57 +03002447static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
2448 struct perf_sample *sample)
2449{
2450 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
2451 pid_t pid, tid;
2452 int cpu, ret;
2453
2454 cpu = sample->cpu;
2455
2456 if (pt->have_sched_switch == 3) {
2457 if (!out)
Adrian Hunterc7b4f15f2019-04-12 14:38:29 +03002458 return intel_pt_context_switch_in(pt, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002459 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
2460 pr_err("Expecting CPU-wide context switch event\n");
2461 return -EINVAL;
2462 }
2463 pid = event->context_switch.next_prev_pid;
2464 tid = event->context_switch.next_prev_tid;
2465 } else {
2466 if (out)
2467 return 0;
2468 pid = sample->pid;
2469 tid = sample->tid;
2470 }
2471
2472 if (tid == -1) {
2473 pr_err("context_switch event has no tid\n");
2474 return -EINVAL;
2475 }
2476
2477 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2478 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
2479 &pt->tc));
2480
2481 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
2482 if (ret <= 0)
2483 return ret;
2484
2485 return machine__set_current_tid(pt->machine, cpu, pid, tid);
2486}
2487
Adrian Hunter90e457f2015-07-17 19:33:41 +03002488static int intel_pt_process_itrace_start(struct intel_pt *pt,
2489 union perf_event *event,
2490 struct perf_sample *sample)
2491{
2492 if (!pt->per_cpu_mmaps)
2493 return 0;
2494
2495 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
2496 sample->cpu, event->itrace_start.pid,
2497 event->itrace_start.tid, sample->time,
2498 perf_time_to_tsc(sample->time, &pt->tc));
2499
2500 return machine__set_current_tid(pt->machine, sample->cpu,
2501 event->itrace_start.pid,
2502 event->itrace_start.tid);
2503}
2504
2505static int intel_pt_process_event(struct perf_session *session,
2506 union perf_event *event,
2507 struct perf_sample *sample,
2508 struct perf_tool *tool)
2509{
2510 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2511 auxtrace);
2512 u64 timestamp;
2513 int err = 0;
2514
2515 if (dump_trace)
2516 return 0;
2517
2518 if (!tool->ordered_events) {
2519 pr_err("Intel Processor Trace requires ordered events\n");
2520 return -EINVAL;
2521 }
2522
Adrian Hunter81cd60c2015-08-20 11:51:32 +03002523 if (sample->time && sample->time != (u64)-1)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002524 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
2525 else
2526 timestamp = 0;
2527
2528 if (timestamp || pt->timeless_decoding) {
2529 err = intel_pt_update_queues(pt);
2530 if (err)
2531 return err;
2532 }
2533
2534 if (pt->timeless_decoding) {
2535 if (event->header.type == PERF_RECORD_EXIT) {
2536 err = intel_pt_process_timeless_queues(pt,
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03002537 event->fork.tid,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002538 sample->time);
2539 }
2540 } else if (timestamp) {
2541 err = intel_pt_process_queues(pt, timestamp);
2542 }
2543 if (err)
2544 return err;
2545
2546 if (event->header.type == PERF_RECORD_AUX &&
2547 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
2548 pt->synth_opts.errors) {
2549 err = intel_pt_lost(pt, sample);
2550 if (err)
2551 return err;
2552 }
2553
2554 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
2555 err = intel_pt_process_switch(pt, sample);
2556 else if (event->header.type == PERF_RECORD_ITRACE_START)
2557 err = intel_pt_process_itrace_start(pt, event, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03002558 else if (event->header.type == PERF_RECORD_SWITCH ||
2559 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
2560 err = intel_pt_context_switch(pt, event, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002561
Adrian Hunter93f8be22018-11-05 09:35:04 +02002562 intel_pt_log("event %u: cpu %d time %"PRIu64" tsc %#"PRIx64" ",
2563 event->header.type, sample->cpu, sample->time, timestamp);
2564 intel_pt_log_event(event);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002565
2566 return err;
2567}
2568
2569static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
2570{
2571 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2572 auxtrace);
2573 int ret;
2574
2575 if (dump_trace)
2576 return 0;
2577
2578 if (!tool->ordered_events)
2579 return -EINVAL;
2580
2581 ret = intel_pt_update_queues(pt);
2582 if (ret < 0)
2583 return ret;
2584
2585 if (pt->timeless_decoding)
2586 return intel_pt_process_timeless_queues(pt, -1,
2587 MAX_TIMESTAMP - 1);
2588
2589 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
2590}
2591
2592static void intel_pt_free_events(struct perf_session *session)
2593{
2594 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2595 auxtrace);
2596 struct auxtrace_queues *queues = &pt->queues;
2597 unsigned int i;
2598
2599 for (i = 0; i < queues->nr_queues; i++) {
2600 intel_pt_free_queue(queues->queue_array[i].priv);
2601 queues->queue_array[i].priv = NULL;
2602 }
2603 intel_pt_log_disable();
2604 auxtrace_queues__free(queues);
2605}
2606
2607static void intel_pt_free(struct perf_session *session)
2608{
2609 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2610 auxtrace);
2611
2612 auxtrace_heap__free(&pt->heap);
2613 intel_pt_free_events(session);
2614 session->auxtrace = NULL;
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03002615 thread__put(pt->unknown_thread);
Adrian Hunter2acee102016-09-23 17:38:48 +03002616 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002617 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03002618 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002619 free(pt);
2620}
2621
2622static int intel_pt_process_auxtrace_event(struct perf_session *session,
2623 union perf_event *event,
2624 struct perf_tool *tool __maybe_unused)
2625{
2626 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
2627 auxtrace);
2628
Adrian Hunter90e457f2015-07-17 19:33:41 +03002629 if (!pt->data_queued) {
2630 struct auxtrace_buffer *buffer;
2631 off_t data_offset;
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002632 int fd = perf_data__fd(session->data);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002633 int err;
2634
Jiri Olsa8ceb41d2017-01-23 22:07:59 +01002635 if (perf_data__is_pipe(session->data)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002636 data_offset = 0;
2637 } else {
2638 data_offset = lseek(fd, 0, SEEK_CUR);
2639 if (data_offset == -1)
2640 return -errno;
2641 }
2642
2643 err = auxtrace_queues__add_event(&pt->queues, session, event,
2644 data_offset, &buffer);
2645 if (err)
2646 return err;
2647
2648 /* Dump here now we have copied a piped trace out of the pipe */
2649 if (dump_trace) {
2650 if (auxtrace_buffer__get_data(buffer, fd)) {
2651 intel_pt_dump_event(pt, buffer->data,
2652 buffer->size);
2653 auxtrace_buffer__put_data(buffer);
2654 }
2655 }
2656 }
2657
2658 return 0;
2659}
2660
2661struct intel_pt_synth {
2662 struct perf_tool dummy_tool;
2663 struct perf_session *session;
2664};
2665
2666static int intel_pt_event_synth(struct perf_tool *tool,
2667 union perf_event *event,
2668 struct perf_sample *sample __maybe_unused,
2669 struct machine *machine __maybe_unused)
2670{
2671 struct intel_pt_synth *intel_pt_synth =
2672 container_of(tool, struct intel_pt_synth, dummy_tool);
2673
2674 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
2675 NULL);
2676}
2677
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002678static int intel_pt_synth_event(struct perf_session *session, const char *name,
Adrian Hunter90e457f2015-07-17 19:33:41 +03002679 struct perf_event_attr *attr, u64 id)
2680{
2681 struct intel_pt_synth intel_pt_synth;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002682 int err;
2683
2684 pr_debug("Synthesizing '%s' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2685 name, id, (u64)attr->sample_type);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002686
2687 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
2688 intel_pt_synth.session = session;
2689
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002690 err = perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
2691 &id, intel_pt_event_synth);
2692 if (err)
2693 pr_err("%s: failed to synthesize '%s' event type\n",
2694 __func__, name);
2695
2696 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002697}
2698
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03002699static void intel_pt_set_event_name(struct perf_evlist *evlist, u64 id,
2700 const char *name)
2701{
2702 struct perf_evsel *evsel;
2703
2704 evlist__for_each_entry(evlist, evsel) {
2705 if (evsel->id && evsel->id[0] == id) {
2706 if (evsel->name)
2707 zfree(&evsel->name);
2708 evsel->name = strdup(name);
2709 break;
2710 }
2711 }
2712}
2713
Adrian Hunter85a564d2017-05-26 11:17:30 +03002714static struct perf_evsel *intel_pt_evsel(struct intel_pt *pt,
2715 struct perf_evlist *evlist)
2716{
2717 struct perf_evsel *evsel;
2718
2719 evlist__for_each_entry(evlist, evsel) {
2720 if (evsel->attr.type == pt->pmu_type && evsel->ids)
2721 return evsel;
2722 }
2723
2724 return NULL;
2725}
2726
Adrian Hunter90e457f2015-07-17 19:33:41 +03002727static int intel_pt_synth_events(struct intel_pt *pt,
2728 struct perf_session *session)
2729{
2730 struct perf_evlist *evlist = session->evlist;
Adrian Hunter85a564d2017-05-26 11:17:30 +03002731 struct perf_evsel *evsel = intel_pt_evsel(pt, evlist);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002732 struct perf_event_attr attr;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002733 u64 id;
2734 int err;
2735
Adrian Hunter85a564d2017-05-26 11:17:30 +03002736 if (!evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002737 pr_debug("There are no selected events with Intel Processor Trace data\n");
2738 return 0;
2739 }
2740
2741 memset(&attr, 0, sizeof(struct perf_event_attr));
2742 attr.size = sizeof(struct perf_event_attr);
2743 attr.type = PERF_TYPE_HARDWARE;
2744 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
2745 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
2746 PERF_SAMPLE_PERIOD;
2747 if (pt->timeless_decoding)
2748 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
2749 else
2750 attr.sample_type |= PERF_SAMPLE_TIME;
2751 if (!pt->per_cpu_mmaps)
2752 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
2753 attr.exclude_user = evsel->attr.exclude_user;
2754 attr.exclude_kernel = evsel->attr.exclude_kernel;
2755 attr.exclude_hv = evsel->attr.exclude_hv;
2756 attr.exclude_host = evsel->attr.exclude_host;
2757 attr.exclude_guest = evsel->attr.exclude_guest;
2758 attr.sample_id_all = evsel->attr.sample_id_all;
2759 attr.read_format = evsel->attr.read_format;
2760
2761 id = evsel->id[0] + 1000000000;
2762 if (!id)
2763 id = 1;
2764
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002765 if (pt->synth_opts.branches) {
2766 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2767 attr.sample_period = 1;
2768 attr.sample_type |= PERF_SAMPLE_ADDR;
2769 err = intel_pt_synth_event(session, "branches", &attr, id);
2770 if (err)
2771 return err;
2772 pt->sample_branches = true;
2773 pt->branches_sample_type = attr.sample_type;
2774 pt->branches_id = id;
2775 id += 1;
2776 attr.sample_type &= ~(u64)PERF_SAMPLE_ADDR;
2777 }
2778
2779 if (pt->synth_opts.callchain)
2780 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
2781 if (pt->synth_opts.last_branch)
2782 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
2783
Adrian Hunter90e457f2015-07-17 19:33:41 +03002784 if (pt->synth_opts.instructions) {
2785 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2786 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2787 attr.sample_period =
2788 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2789 else
2790 attr.sample_period = pt->synth_opts.period;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002791 err = intel_pt_synth_event(session, "instructions", &attr, id);
2792 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002793 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002794 pt->sample_instructions = true;
2795 pt->instructions_sample_type = attr.sample_type;
2796 pt->instructions_id = id;
2797 id += 1;
2798 }
2799
Adrian Hunter4a9fd4e2017-05-26 11:17:33 +03002800 attr.sample_type &= ~(u64)PERF_SAMPLE_PERIOD;
2801 attr.sample_period = 1;
2802
Adrian Hunter90e457f2015-07-17 19:33:41 +03002803 if (pt->synth_opts.transactions) {
2804 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
Adrian Hunter63a22cd2017-05-26 11:17:31 +03002805 err = intel_pt_synth_event(session, "transactions", &attr, id);
2806 if (err)
Adrian Hunter90e457f2015-07-17 19:33:41 +03002807 return err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002808 pt->sample_transactions = true;
Adrian Hunter21160742017-05-26 11:17:18 +03002809 pt->transactions_sample_type = attr.sample_type;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002810 pt->transactions_id = id;
Adrian Hunterbbac88ed2017-05-26 11:17:32 +03002811 intel_pt_set_event_name(evlist, id, "transactions");
Adrian Hunter90e457f2015-07-17 19:33:41 +03002812 id += 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002813 }
2814
Adrian Hunter37973072017-06-30 11:36:45 +03002815 attr.type = PERF_TYPE_SYNTH;
2816 attr.sample_type |= PERF_SAMPLE_RAW;
2817
2818 if (pt->synth_opts.ptwrites) {
2819 attr.config = PERF_SYNTH_INTEL_PTWRITE;
2820 err = intel_pt_synth_event(session, "ptwrite", &attr, id);
2821 if (err)
2822 return err;
2823 pt->sample_ptwrites = true;
2824 pt->ptwrites_sample_type = attr.sample_type;
2825 pt->ptwrites_id = id;
2826 intel_pt_set_event_name(evlist, id, "ptwrite");
2827 id += 1;
2828 }
2829
2830 if (pt->synth_opts.pwr_events) {
2831 pt->sample_pwr_events = true;
2832 pt->pwr_events_sample_type = attr.sample_type;
2833
2834 attr.config = PERF_SYNTH_INTEL_CBR;
2835 err = intel_pt_synth_event(session, "cbr", &attr, id);
2836 if (err)
2837 return err;
2838 pt->cbr_id = id;
2839 intel_pt_set_event_name(evlist, id, "cbr");
2840 id += 1;
2841 }
2842
2843 if (pt->synth_opts.pwr_events && (evsel->attr.config & 0x10)) {
2844 attr.config = PERF_SYNTH_INTEL_MWAIT;
2845 err = intel_pt_synth_event(session, "mwait", &attr, id);
2846 if (err)
2847 return err;
2848 pt->mwait_id = id;
2849 intel_pt_set_event_name(evlist, id, "mwait");
2850 id += 1;
2851
2852 attr.config = PERF_SYNTH_INTEL_PWRE;
2853 err = intel_pt_synth_event(session, "pwre", &attr, id);
2854 if (err)
2855 return err;
2856 pt->pwre_id = id;
2857 intel_pt_set_event_name(evlist, id, "pwre");
2858 id += 1;
2859
2860 attr.config = PERF_SYNTH_INTEL_EXSTOP;
2861 err = intel_pt_synth_event(session, "exstop", &attr, id);
2862 if (err)
2863 return err;
2864 pt->exstop_id = id;
2865 intel_pt_set_event_name(evlist, id, "exstop");
2866 id += 1;
2867
2868 attr.config = PERF_SYNTH_INTEL_PWRX;
2869 err = intel_pt_synth_event(session, "pwrx", &attr, id);
2870 if (err)
2871 return err;
2872 pt->pwrx_id = id;
2873 intel_pt_set_event_name(evlist, id, "pwrx");
2874 id += 1;
2875 }
2876
Adrian Hunter90e457f2015-07-17 19:33:41 +03002877 return 0;
2878}
2879
2880static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2881{
2882 struct perf_evsel *evsel;
2883
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002884 evlist__for_each_entry_reverse(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002885 const char *name = perf_evsel__name(evsel);
2886
2887 if (!strcmp(name, "sched:sched_switch"))
2888 return evsel;
2889 }
2890
2891 return NULL;
2892}
2893
Adrian Hunter86c27862015-08-13 12:40:57 +03002894static bool intel_pt_find_switch(struct perf_evlist *evlist)
2895{
2896 struct perf_evsel *evsel;
2897
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002898 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002899 if (evsel->attr.context_switch)
2900 return true;
2901 }
2902
2903 return false;
2904}
2905
Adrian Hunterba11ba62015-09-25 16:15:56 +03002906static int intel_pt_perf_config(const char *var, const char *value, void *data)
2907{
2908 struct intel_pt *pt = data;
2909
2910 if (!strcmp(var, "intel-pt.mispred-all"))
2911 pt->mispred_all = perf_config_bool(var, value);
2912
2913 return 0;
2914}
2915
Adrian Hunter2c47db92019-06-04 16:00:09 +03002916/* Find least TSC which converts to ns or later */
2917static u64 intel_pt_tsc_start(u64 ns, struct intel_pt *pt)
2918{
2919 u64 tsc, tm;
2920
2921 tsc = perf_time_to_tsc(ns, &pt->tc);
2922
2923 while (1) {
2924 tm = tsc_to_perf_time(tsc, &pt->tc);
2925 if (tm < ns)
2926 break;
2927 tsc -= 1;
2928 }
2929
2930 while (tm < ns)
2931 tm = tsc_to_perf_time(++tsc, &pt->tc);
2932
2933 return tsc;
2934}
2935
2936/* Find greatest TSC which converts to ns or earlier */
2937static u64 intel_pt_tsc_end(u64 ns, struct intel_pt *pt)
2938{
2939 u64 tsc, tm;
2940
2941 tsc = perf_time_to_tsc(ns, &pt->tc);
2942
2943 while (1) {
2944 tm = tsc_to_perf_time(tsc, &pt->tc);
2945 if (tm > ns)
2946 break;
2947 tsc += 1;
2948 }
2949
2950 while (tm > ns)
2951 tm = tsc_to_perf_time(--tsc, &pt->tc);
2952
2953 return tsc;
2954}
2955
2956static int intel_pt_setup_time_ranges(struct intel_pt *pt,
2957 struct itrace_synth_opts *opts)
2958{
2959 struct perf_time_interval *p = opts->ptime_range;
2960 int n = opts->range_num;
2961 int i;
2962
2963 if (!n || !p || pt->timeless_decoding)
2964 return 0;
2965
2966 pt->time_ranges = calloc(n, sizeof(struct range));
2967 if (!pt->time_ranges)
2968 return -ENOMEM;
2969
2970 pt->range_cnt = n;
2971
2972 intel_pt_log("%s: %u range(s)\n", __func__, n);
2973
2974 for (i = 0; i < n; i++) {
2975 struct range *r = &pt->time_ranges[i];
2976 u64 ts = p[i].start;
2977 u64 te = p[i].end;
2978
2979 /*
2980 * Take care to ensure the TSC range matches the perf-time range
2981 * when converted back to perf-time.
2982 */
2983 r->start = ts ? intel_pt_tsc_start(ts, pt) : 0;
2984 r->end = te ? intel_pt_tsc_end(te, pt) : 0;
2985
2986 intel_pt_log("range %d: perf time interval: %"PRIu64" to %"PRIu64"\n",
2987 i, ts, te);
2988 intel_pt_log("range %d: TSC time interval: %#"PRIx64" to %#"PRIx64"\n",
2989 i, r->start, r->end);
2990 }
2991
2992 return 0;
2993}
2994
Adrian Hunter90e457f2015-07-17 19:33:41 +03002995static const char * const intel_pt_info_fmts[] = {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03002996 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2997 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2998 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2999 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
3000 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
3001 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
3002 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
3003 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
3004 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
3005 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
3006 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
3007 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
3008 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
3009 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003010 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003011 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
Adrian Hunter90e457f2015-07-17 19:33:41 +03003012};
3013
3014static void intel_pt_print_info(u64 *arr, int start, int finish)
3015{
3016 int i;
3017
3018 if (!dump_trace)
3019 return;
3020
3021 for (i = start; i <= finish; i++)
3022 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
3023}
3024
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003025static void intel_pt_print_info_str(const char *name, const char *str)
3026{
3027 if (!dump_trace)
3028 return;
3029
3030 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
3031}
3032
Adrian Hunter40b746a2016-09-23 17:38:44 +03003033static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
3034{
3035 return auxtrace_info->header.size >=
3036 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
3037}
3038
Adrian Hunter90e457f2015-07-17 19:33:41 +03003039int intel_pt_process_auxtrace_info(union perf_event *event,
3040 struct perf_session *session)
3041{
3042 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
3043 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
3044 struct intel_pt *pt;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003045 void *info_end;
3046 u64 *info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003047 int err;
3048
3049 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
3050 min_sz)
3051 return -EINVAL;
3052
3053 pt = zalloc(sizeof(struct intel_pt));
3054 if (!pt)
3055 return -ENOMEM;
3056
Adrian Hunter2acee102016-09-23 17:38:48 +03003057 addr_filters__init(&pt->filts);
3058
Arnaldo Carvalho de Meloecc4c562017-01-24 13:44:10 -03003059 err = perf_config(intel_pt_perf_config, pt);
3060 if (err)
3061 goto err_free;
Adrian Hunterba11ba62015-09-25 16:15:56 +03003062
Adrian Hunter90e457f2015-07-17 19:33:41 +03003063 err = auxtrace_queues__init(&pt->queues);
3064 if (err)
3065 goto err_free;
3066
3067 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
3068
3069 pt->session = session;
3070 pt->machine = &session->machines.host; /* No kvm support */
3071 pt->auxtrace_type = auxtrace_info->type;
3072 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
3073 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
3074 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
3075 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
3076 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
3077 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
3078 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
3079 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
3080 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
3081 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
3082 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
3083 INTEL_PT_PER_CPU_MMAPS);
3084
Adrian Hunter40b746a2016-09-23 17:38:44 +03003085 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03003086 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
3087 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
3088 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
3089 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
3090 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
3091 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
3092 INTEL_PT_CYC_BIT);
3093 }
3094
Adrian Hunter40b746a2016-09-23 17:38:44 +03003095 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003096 pt->max_non_turbo_ratio =
3097 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
3098 intel_pt_print_info(&auxtrace_info->priv[0],
3099 INTEL_PT_MAX_NONTURBO_RATIO,
3100 INTEL_PT_MAX_NONTURBO_RATIO);
3101 }
3102
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003103 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
3104 info_end = (void *)info + auxtrace_info->header.size;
3105
3106 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
3107 size_t len;
3108
3109 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
3110 intel_pt_print_info(&auxtrace_info->priv[0],
3111 INTEL_PT_FILTER_STR_LEN,
3112 INTEL_PT_FILTER_STR_LEN);
3113 if (len) {
3114 const char *filter = (const char *)info;
3115
3116 len = roundup(len + 1, 8);
3117 info += len >> 3;
3118 if ((void *)info > info_end) {
3119 pr_err("%s: bad filter string length\n", __func__);
3120 err = -EINVAL;
3121 goto err_free_queues;
3122 }
3123 pt->filter = memdup(filter, len);
3124 if (!pt->filter) {
3125 err = -ENOMEM;
3126 goto err_free_queues;
3127 }
3128 if (session->header.needs_swap)
3129 mem_bswap_64(pt->filter, len);
3130 if (pt->filter[len - 1]) {
3131 pr_err("%s: filter string not null terminated\n", __func__);
3132 err = -EINVAL;
3133 goto err_free_queues;
3134 }
Adrian Hunter2acee102016-09-23 17:38:48 +03003135 err = addr_filters__parse_bare_filter(&pt->filts,
3136 filter);
3137 if (err)
3138 goto err_free_queues;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003139 }
3140 intel_pt_print_info_str("Filter string", pt->filter);
3141 }
3142
Adrian Hunter90e457f2015-07-17 19:33:41 +03003143 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
Adrian Hunter07633382019-03-01 12:35:36 +02003144 if (pt->timeless_decoding && !pt->tc.time_mult)
3145 pt->tc.time_mult = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003146 pt->have_tsc = intel_pt_have_tsc(pt);
3147 pt->sampling_mode = false;
3148 pt->est_tsc = !pt->timeless_decoding;
3149
3150 pt->unknown_thread = thread__new(999999999, 999999999);
3151 if (!pt->unknown_thread) {
3152 err = -ENOMEM;
3153 goto err_free_queues;
3154 }
Adrian Hunter3a4acda2016-02-01 03:21:04 +00003155
3156 /*
3157 * Since this thread will not be kept in any rbtree not in a
3158 * list, initialize its list node so that at thread__put() the
3159 * current thread lifetime assuption is kept and we don't segfault
3160 * at list_del_init().
3161 */
3162 INIT_LIST_HEAD(&pt->unknown_thread->node);
3163
Adrian Hunter90e457f2015-07-17 19:33:41 +03003164 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
3165 if (err)
3166 goto err_delete_thread;
3167 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
3168 err = -ENOMEM;
3169 goto err_delete_thread;
3170 }
3171
3172 pt->auxtrace.process_event = intel_pt_process_event;
3173 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
3174 pt->auxtrace.flush_events = intel_pt_flush;
3175 pt->auxtrace.free_events = intel_pt_free_events;
3176 pt->auxtrace.free = intel_pt_free;
3177 session->auxtrace = &pt->auxtrace;
3178
3179 if (dump_trace)
3180 return 0;
3181
3182 if (pt->have_sched_switch == 1) {
3183 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
3184 if (!pt->switch_evsel) {
3185 pr_err("%s: missing sched_switch event\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003186 err = -EINVAL;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003187 goto err_delete_thread;
3188 }
Adrian Hunter86c27862015-08-13 12:40:57 +03003189 } else if (pt->have_sched_switch == 2 &&
3190 !intel_pt_find_switch(session->evlist)) {
3191 pr_err("%s: missing context_switch attribute flag\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03003192 err = -EINVAL;
Adrian Hunter86c27862015-08-13 12:40:57 +03003193 goto err_delete_thread;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003194 }
3195
3196 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
3197 pt->synth_opts = *session->itrace_synth_opts;
3198 } else {
Andi Kleen4eb06812018-09-20 11:05:37 -07003199 itrace_synth_opts__set_default(&pt->synth_opts,
3200 session->itrace_synth_opts->default_no_sample);
Adrian Hunter26f19c22019-05-20 14:37:07 +03003201 if (!session->itrace_synth_opts->default_no_sample &&
3202 !session->itrace_synth_opts->inject) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03003203 pt->synth_opts.branches = false;
3204 pt->synth_opts.callchain = true;
3205 }
Adrian Hunter50f736372016-06-23 16:40:57 +03003206 if (session->itrace_synth_opts)
3207 pt->synth_opts.thread_stack =
3208 session->itrace_synth_opts->thread_stack;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003209 }
3210
3211 if (pt->synth_opts.log)
3212 intel_pt_log_enable();
3213
3214 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
3215 if (pt->tc.time_mult) {
3216 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
3217
Adrian Hunterfa8025c2016-09-23 17:38:42 +03003218 if (!pt->max_non_turbo_ratio)
3219 pt->max_non_turbo_ratio =
3220 (tsc_freq + 50000000) / 100000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003221 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
3222 intel_pt_log("Maximum non-turbo ratio %u\n",
3223 pt->max_non_turbo_ratio);
Adrian Hunter37973072017-06-30 11:36:45 +03003224 pt->cbr2khz = tsc_freq / pt->max_non_turbo_ratio / 1000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03003225 }
3226
Adrian Hunter2c47db92019-06-04 16:00:09 +03003227 if (session->itrace_synth_opts) {
3228 err = intel_pt_setup_time_ranges(pt, session->itrace_synth_opts);
3229 if (err)
3230 goto err_delete_thread;
3231 }
3232
Adrian Hunter90e457f2015-07-17 19:33:41 +03003233 if (pt->synth_opts.calls)
3234 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
3235 PERF_IP_FLAG_TRACE_END;
3236 if (pt->synth_opts.returns)
3237 pt->branches_filter |= PERF_IP_FLAG_RETURN |
3238 PERF_IP_FLAG_TRACE_BEGIN;
3239
3240 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
3241 symbol_conf.use_callchain = true;
3242 if (callchain_register_param(&callchain_param) < 0) {
3243 symbol_conf.use_callchain = false;
3244 pt->synth_opts.callchain = false;
3245 }
3246 }
3247
3248 err = intel_pt_synth_events(pt, session);
3249 if (err)
3250 goto err_delete_thread;
3251
3252 err = auxtrace_queues__process_index(&pt->queues, session);
3253 if (err)
3254 goto err_delete_thread;
3255
3256 if (pt->queues.populated)
3257 pt->data_queued = true;
3258
3259 if (pt->timeless_decoding)
3260 pr_debug2("Intel PT decoding without timestamps\n");
3261
3262 return 0;
3263
3264err_delete_thread:
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03003265 thread__zput(pt->unknown_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003266err_free_queues:
3267 intel_pt_log_disable();
3268 auxtrace_queues__free(&pt->queues);
3269 session->auxtrace = NULL;
3270err_free:
Adrian Hunter2acee102016-09-23 17:38:48 +03003271 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03003272 zfree(&pt->filter);
Adrian Hunter2c47db92019-06-04 16:00:09 +03003273 zfree(&pt->time_ranges);
Adrian Hunter90e457f2015-07-17 19:33:41 +03003274 free(pt);
3275 return err;
3276}