blob: d40ab4cf8932303ed5b2ad2b8375f87dba0c238a [file] [log] [blame]
Adrian Hunter90e457f2015-07-17 19:33:41 +03001/*
2 * intel_pt.c: Intel Processor Trace support
3 * Copyright (c) 2013-2015, Intel Corporation.
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 */
15
16#include <stdio.h>
17#include <stdbool.h>
18#include <errno.h>
19#include <linux/kernel.h>
20#include <linux/types.h>
21
22#include "../perf.h"
23#include "session.h"
24#include "machine.h"
Adrian Hunterf14445e2015-09-25 16:15:45 +030025#include "sort.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030026#include "tool.h"
27#include "event.h"
28#include "evlist.h"
29#include "evsel.h"
30#include "map.h"
31#include "color.h"
32#include "util.h"
33#include "thread.h"
34#include "thread-stack.h"
35#include "symbol.h"
36#include "callchain.h"
37#include "dso.h"
38#include "debug.h"
39#include "auxtrace.h"
40#include "tsc.h"
41#include "intel-pt.h"
Taeung Song41840d22016-06-23 17:55:17 +090042#include "config.h"
Adrian Hunter90e457f2015-07-17 19:33:41 +030043
44#include "intel-pt-decoder/intel-pt-log.h"
45#include "intel-pt-decoder/intel-pt-decoder.h"
46#include "intel-pt-decoder/intel-pt-insn-decoder.h"
47#include "intel-pt-decoder/intel-pt-pkt-decoder.h"
48
49#define MAX_TIMESTAMP (~0ULL)
50
51struct intel_pt {
52 struct auxtrace auxtrace;
53 struct auxtrace_queues queues;
54 struct auxtrace_heap heap;
55 u32 auxtrace_type;
56 struct perf_session *session;
57 struct machine *machine;
58 struct perf_evsel *switch_evsel;
59 struct thread *unknown_thread;
60 bool timeless_decoding;
61 bool sampling_mode;
62 bool snapshot_mode;
63 bool per_cpu_mmaps;
64 bool have_tsc;
65 bool data_queued;
66 bool est_tsc;
67 bool sync_switch;
Adrian Hunterba11ba62015-09-25 16:15:56 +030068 bool mispred_all;
Adrian Hunter90e457f2015-07-17 19:33:41 +030069 int have_sched_switch;
70 u32 pmu_type;
71 u64 kernel_start;
72 u64 switch_ip;
73 u64 ptss_ip;
74
75 struct perf_tsc_conversion tc;
76 bool cap_user_time_zero;
77
78 struct itrace_synth_opts synth_opts;
79
80 bool sample_instructions;
81 u64 instructions_sample_type;
82 u64 instructions_sample_period;
83 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
94 bool synth_needs_swap;
95
96 u64 tsc_bit;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +030097 u64 mtc_bit;
98 u64 mtc_freq_bits;
99 u32 tsc_ctc_ratio_n;
100 u32 tsc_ctc_ratio_d;
101 u64 cyc_bit;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300102 u64 noretcomp_bit;
103 unsigned max_non_turbo_ratio;
Andi Kleend1706b32016-03-28 10:45:38 -0700104
105 unsigned long num_events;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +0300106
107 char *filter;
Adrian Hunter2acee102016-09-23 17:38:48 +0300108 struct addr_filters filts;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300109};
110
111enum switch_state {
112 INTEL_PT_SS_NOT_TRACING,
113 INTEL_PT_SS_UNKNOWN,
114 INTEL_PT_SS_TRACING,
115 INTEL_PT_SS_EXPECTING_SWITCH_EVENT,
116 INTEL_PT_SS_EXPECTING_SWITCH_IP,
117};
118
119struct intel_pt_queue {
120 struct intel_pt *pt;
121 unsigned int queue_nr;
122 struct auxtrace_buffer *buffer;
123 void *decoder;
124 const struct intel_pt_state *state;
125 struct ip_callchain *chain;
Adrian Hunterf14445e2015-09-25 16:15:45 +0300126 struct branch_stack *last_branch;
127 struct branch_stack *last_branch_rb;
128 size_t last_branch_pos;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300129 union perf_event *event_buf;
130 bool on_heap;
131 bool stop;
132 bool step_through_buffers;
133 bool use_buffer_pid_tid;
Adrian Hunter6a330362018-03-07 16:02:22 +0200134 bool sync_switch;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300135 pid_t pid, tid;
136 int cpu;
137 int switch_state;
138 pid_t next_tid;
139 struct thread *thread;
140 bool exclude_kernel;
141 bool have_sample;
142 u64 time;
143 u64 timestamp;
144 u32 flags;
145 u16 insn_len;
Adrian Hunter2a21d032015-07-17 19:33:48 +0300146 u64 last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300147};
148
149static void intel_pt_dump(struct intel_pt *pt __maybe_unused,
150 unsigned char *buf, size_t len)
151{
152 struct intel_pt_pkt packet;
153 size_t pos = 0;
154 int ret, pkt_len, i;
155 char desc[INTEL_PT_PKT_DESC_MAX];
156 const char *color = PERF_COLOR_BLUE;
157
158 color_fprintf(stdout, color,
159 ". ... Intel Processor Trace data: size %zu bytes\n",
160 len);
161
162 while (len) {
163 ret = intel_pt_get_packet(buf, len, &packet);
164 if (ret > 0)
165 pkt_len = ret;
166 else
167 pkt_len = 1;
168 printf(".");
169 color_fprintf(stdout, color, " %08x: ", pos);
170 for (i = 0; i < pkt_len; i++)
171 color_fprintf(stdout, color, " %02x", buf[i]);
172 for (; i < 16; i++)
173 color_fprintf(stdout, color, " ");
174 if (ret > 0) {
175 ret = intel_pt_pkt_desc(&packet, desc,
176 INTEL_PT_PKT_DESC_MAX);
177 if (ret > 0)
178 color_fprintf(stdout, color, " %s\n", desc);
179 } else {
180 color_fprintf(stdout, color, " Bad packet!\n");
181 }
182 pos += pkt_len;
183 buf += pkt_len;
184 len -= pkt_len;
185 }
186}
187
188static void intel_pt_dump_event(struct intel_pt *pt, unsigned char *buf,
189 size_t len)
190{
191 printf(".\n");
192 intel_pt_dump(pt, buf, len);
193}
194
195static int intel_pt_do_fix_overlap(struct intel_pt *pt, struct auxtrace_buffer *a,
196 struct auxtrace_buffer *b)
197{
Adrian Hunteraad2ad62018-03-07 16:02:21 +0200198 bool consecutive = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300199 void *start;
200
201 start = intel_pt_find_overlap(a->data, a->size, b->data, b->size,
Adrian Hunteraad2ad62018-03-07 16:02:21 +0200202 pt->have_tsc, &consecutive);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300203 if (!start)
204 return -EINVAL;
205 b->use_size = b->data + b->size - start;
206 b->use_data = start;
Adrian Hunteraad2ad62018-03-07 16:02:21 +0200207 if (b->use_size && consecutive)
208 b->consecutive = true;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300209 return 0;
210}
211
212static void intel_pt_use_buffer_pid_tid(struct intel_pt_queue *ptq,
213 struct auxtrace_queue *queue,
214 struct auxtrace_buffer *buffer)
215{
216 if (queue->cpu == -1 && buffer->cpu != -1)
217 ptq->cpu = buffer->cpu;
218
219 ptq->pid = buffer->pid;
220 ptq->tid = buffer->tid;
221
222 intel_pt_log("queue %u cpu %d pid %d tid %d\n",
223 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
224
225 thread__zput(ptq->thread);
226
227 if (ptq->tid != -1) {
228 if (ptq->pid != -1)
229 ptq->thread = machine__findnew_thread(ptq->pt->machine,
230 ptq->pid,
231 ptq->tid);
232 else
233 ptq->thread = machine__find_thread(ptq->pt->machine, -1,
234 ptq->tid);
235 }
236}
237
238/* This function assumes data is processed sequentially only */
239static int intel_pt_get_trace(struct intel_pt_buffer *b, void *data)
240{
241 struct intel_pt_queue *ptq = data;
242 struct auxtrace_buffer *buffer = ptq->buffer, *old_buffer = buffer;
243 struct auxtrace_queue *queue;
244
245 if (ptq->stop) {
246 b->len = 0;
247 return 0;
248 }
249
250 queue = &ptq->pt->queues.queue_array[ptq->queue_nr];
Adrian Hunter810c3982016-09-23 17:38:41 +0300251next:
Adrian Hunter90e457f2015-07-17 19:33:41 +0300252 buffer = auxtrace_buffer__next(queue, buffer);
253 if (!buffer) {
254 if (old_buffer)
255 auxtrace_buffer__drop_data(old_buffer);
256 b->len = 0;
257 return 0;
258 }
259
260 ptq->buffer = buffer;
261
262 if (!buffer->data) {
263 int fd = perf_data_file__fd(ptq->pt->session->file);
264
265 buffer->data = auxtrace_buffer__get_data(buffer, fd);
266 if (!buffer->data)
267 return -ENOMEM;
268 }
269
270 if (ptq->pt->snapshot_mode && !buffer->consecutive && old_buffer &&
271 intel_pt_do_fix_overlap(ptq->pt, old_buffer, buffer))
272 return -ENOMEM;
273
Adrian Hunter90e457f2015-07-17 19:33:41 +0300274 if (buffer->use_data) {
275 b->len = buffer->use_size;
276 b->buf = buffer->use_data;
277 } else {
278 b->len = buffer->size;
279 b->buf = buffer->data;
280 }
281 b->ref_timestamp = buffer->reference;
282
Adrian Hunter810c3982016-09-23 17:38:41 +0300283 /*
284 * If in snapshot mode and the buffer has no usable data, get next
285 * buffer and again check overlap against old_buffer.
286 */
287 if (ptq->pt->snapshot_mode && !b->len)
288 goto next;
289
290 if (old_buffer)
291 auxtrace_buffer__drop_data(old_buffer);
292
Adrian Hunter90e457f2015-07-17 19:33:41 +0300293 if (!old_buffer || ptq->pt->sampling_mode || (ptq->pt->snapshot_mode &&
294 !buffer->consecutive)) {
295 b->consecutive = false;
296 b->trace_nr = buffer->buffer_nr + 1;
297 } else {
298 b->consecutive = true;
299 }
300
301 if (ptq->use_buffer_pid_tid && (ptq->pid != buffer->pid ||
302 ptq->tid != buffer->tid))
303 intel_pt_use_buffer_pid_tid(ptq, queue, buffer);
304
305 if (ptq->step_through_buffers)
306 ptq->stop = true;
307
308 if (!b->len)
309 return intel_pt_get_trace(b, data);
310
311 return 0;
312}
313
314struct intel_pt_cache_entry {
315 struct auxtrace_cache_entry entry;
316 u64 insn_cnt;
317 u64 byte_cnt;
318 enum intel_pt_insn_op op;
319 enum intel_pt_insn_branch branch;
320 int length;
321 int32_t rel;
322};
323
324static int intel_pt_config_div(const char *var, const char *value, void *data)
325{
326 int *d = data;
327 long val;
328
329 if (!strcmp(var, "intel-pt.cache-divisor")) {
330 val = strtol(value, NULL, 0);
331 if (val > 0 && val <= INT_MAX)
332 *d = val;
333 }
334
335 return 0;
336}
337
338static int intel_pt_cache_divisor(void)
339{
340 static int d;
341
342 if (d)
343 return d;
344
345 perf_config(intel_pt_config_div, &d);
346
347 if (!d)
348 d = 64;
349
350 return d;
351}
352
353static unsigned int intel_pt_cache_size(struct dso *dso,
354 struct machine *machine)
355{
356 off_t size;
357
358 size = dso__data_size(dso, machine);
359 size /= intel_pt_cache_divisor();
360 if (size < 1000)
361 return 10;
362 if (size > (1 << 21))
363 return 21;
364 return 32 - __builtin_clz(size);
365}
366
367static struct auxtrace_cache *intel_pt_cache(struct dso *dso,
368 struct machine *machine)
369{
370 struct auxtrace_cache *c;
371 unsigned int bits;
372
373 if (dso->auxtrace_cache)
374 return dso->auxtrace_cache;
375
376 bits = intel_pt_cache_size(dso, machine);
377
378 /* Ignoring cache creation failure */
379 c = auxtrace_cache__new(bits, sizeof(struct intel_pt_cache_entry), 200);
380
381 dso->auxtrace_cache = c;
382
383 return c;
384}
385
386static int intel_pt_cache_add(struct dso *dso, struct machine *machine,
387 u64 offset, u64 insn_cnt, u64 byte_cnt,
388 struct intel_pt_insn *intel_pt_insn)
389{
390 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
391 struct intel_pt_cache_entry *e;
392 int err;
393
394 if (!c)
395 return -ENOMEM;
396
397 e = auxtrace_cache__alloc_entry(c);
398 if (!e)
399 return -ENOMEM;
400
401 e->insn_cnt = insn_cnt;
402 e->byte_cnt = byte_cnt;
403 e->op = intel_pt_insn->op;
404 e->branch = intel_pt_insn->branch;
405 e->length = intel_pt_insn->length;
406 e->rel = intel_pt_insn->rel;
407
408 err = auxtrace_cache__add(c, offset, &e->entry);
409 if (err)
410 auxtrace_cache__free_entry(c, e);
411
412 return err;
413}
414
415static struct intel_pt_cache_entry *
416intel_pt_cache_lookup(struct dso *dso, struct machine *machine, u64 offset)
417{
418 struct auxtrace_cache *c = intel_pt_cache(dso, machine);
419
420 if (!c)
421 return NULL;
422
423 return auxtrace_cache__lookup(dso->auxtrace_cache, offset);
424}
425
426static int intel_pt_walk_next_insn(struct intel_pt_insn *intel_pt_insn,
427 uint64_t *insn_cnt_ptr, uint64_t *ip,
428 uint64_t to_ip, uint64_t max_insn_cnt,
429 void *data)
430{
431 struct intel_pt_queue *ptq = data;
432 struct machine *machine = ptq->pt->machine;
433 struct thread *thread;
434 struct addr_location al;
435 unsigned char buf[1024];
436 size_t bufsz;
437 ssize_t len;
438 int x86_64;
439 u8 cpumode;
440 u64 offset, start_offset, start_ip;
441 u64 insn_cnt = 0;
442 bool one_map = true;
443
444 if (to_ip && *ip == to_ip)
445 goto out_no_cache;
446
447 bufsz = intel_pt_insn_max_size();
448
449 if (*ip >= ptq->pt->kernel_start)
450 cpumode = PERF_RECORD_MISC_KERNEL;
451 else
452 cpumode = PERF_RECORD_MISC_USER;
453
454 thread = ptq->thread;
455 if (!thread) {
456 if (cpumode != PERF_RECORD_MISC_KERNEL)
457 return -EINVAL;
458 thread = ptq->pt->unknown_thread;
459 }
460
461 while (1) {
462 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, *ip, &al);
463 if (!al.map || !al.map->dso)
464 return -EINVAL;
465
466 if (al.map->dso->data.status == DSO_DATA_STATUS_ERROR &&
467 dso__data_status_seen(al.map->dso,
468 DSO_DATA_STATUS_SEEN_ITRACE))
469 return -ENOENT;
470
471 offset = al.map->map_ip(al.map, *ip);
472
473 if (!to_ip && one_map) {
474 struct intel_pt_cache_entry *e;
475
476 e = intel_pt_cache_lookup(al.map->dso, machine, offset);
477 if (e &&
478 (!max_insn_cnt || e->insn_cnt <= max_insn_cnt)) {
479 *insn_cnt_ptr = e->insn_cnt;
480 *ip += e->byte_cnt;
481 intel_pt_insn->op = e->op;
482 intel_pt_insn->branch = e->branch;
483 intel_pt_insn->length = e->length;
484 intel_pt_insn->rel = e->rel;
485 intel_pt_log_insn_no_data(intel_pt_insn, *ip);
486 return 0;
487 }
488 }
489
490 start_offset = offset;
491 start_ip = *ip;
492
493 /* Load maps to ensure dso->is_64_bit has been updated */
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -0300494 map__load(al.map);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300495
496 x86_64 = al.map->dso->is_64_bit;
497
498 while (1) {
499 len = dso__data_read_offset(al.map->dso, machine,
500 offset, buf, bufsz);
501 if (len <= 0)
502 return -EINVAL;
503
504 if (intel_pt_get_insn(buf, len, x86_64, intel_pt_insn))
505 return -EINVAL;
506
507 intel_pt_log_insn(intel_pt_insn, *ip);
508
509 insn_cnt += 1;
510
511 if (intel_pt_insn->branch != INTEL_PT_BR_NO_BRANCH)
512 goto out;
513
514 if (max_insn_cnt && insn_cnt >= max_insn_cnt)
515 goto out_no_cache;
516
517 *ip += intel_pt_insn->length;
518
519 if (to_ip && *ip == to_ip)
520 goto out_no_cache;
521
522 if (*ip >= al.map->end)
523 break;
524
525 offset += intel_pt_insn->length;
526 }
527 one_map = false;
528 }
529out:
530 *insn_cnt_ptr = insn_cnt;
531
532 if (!one_map)
533 goto out_no_cache;
534
535 /*
536 * Didn't lookup in the 'to_ip' case, so do it now to prevent duplicate
537 * entries.
538 */
539 if (to_ip) {
540 struct intel_pt_cache_entry *e;
541
542 e = intel_pt_cache_lookup(al.map->dso, machine, start_offset);
543 if (e)
544 return 0;
545 }
546
547 /* Ignore cache errors */
548 intel_pt_cache_add(al.map->dso, machine, start_offset, insn_cnt,
549 *ip - start_ip, intel_pt_insn);
550
551 return 0;
552
553out_no_cache:
554 *insn_cnt_ptr = insn_cnt;
555 return 0;
556}
557
Adrian Hunter2acee102016-09-23 17:38:48 +0300558static bool intel_pt_match_pgd_ip(struct intel_pt *pt, uint64_t ip,
559 uint64_t offset, const char *filename)
560{
561 struct addr_filter *filt;
562 bool have_filter = false;
563 bool hit_tracestop = false;
564 bool hit_filter = false;
565
566 list_for_each_entry(filt, &pt->filts.head, list) {
567 if (filt->start)
568 have_filter = true;
569
570 if ((filename && !filt->filename) ||
571 (!filename && filt->filename) ||
572 (filename && strcmp(filename, filt->filename)))
573 continue;
574
575 if (!(offset >= filt->addr && offset < filt->addr + filt->size))
576 continue;
577
578 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s hit filter: %s offset %#"PRIx64" size %#"PRIx64"\n",
579 ip, offset, filename ? filename : "[kernel]",
580 filt->start ? "filter" : "stop",
581 filt->addr, filt->size);
582
583 if (filt->start)
584 hit_filter = true;
585 else
586 hit_tracestop = true;
587 }
588
589 if (!hit_tracestop && !hit_filter)
590 intel_pt_log("TIP.PGD ip %#"PRIx64" offset %#"PRIx64" in %s is not in a filter region\n",
591 ip, offset, filename ? filename : "[kernel]");
592
593 return hit_tracestop || (have_filter && !hit_filter);
594}
595
596static int __intel_pt_pgd_ip(uint64_t ip, void *data)
597{
598 struct intel_pt_queue *ptq = data;
599 struct thread *thread;
600 struct addr_location al;
601 u8 cpumode;
602 u64 offset;
603
604 if (ip >= ptq->pt->kernel_start)
605 return intel_pt_match_pgd_ip(ptq->pt, ip, ip, NULL);
606
607 cpumode = PERF_RECORD_MISC_USER;
608
609 thread = ptq->thread;
610 if (!thread)
611 return -EINVAL;
612
613 thread__find_addr_map(thread, cpumode, MAP__FUNCTION, ip, &al);
614 if (!al.map || !al.map->dso)
615 return -EINVAL;
616
617 offset = al.map->map_ip(al.map, ip);
618
619 return intel_pt_match_pgd_ip(ptq->pt, ip, offset,
620 al.map->dso->long_name);
621}
622
623static bool intel_pt_pgd_ip(uint64_t ip, void *data)
624{
625 return __intel_pt_pgd_ip(ip, data) > 0;
626}
627
Adrian Hunter90e457f2015-07-17 19:33:41 +0300628static bool intel_pt_get_config(struct intel_pt *pt,
629 struct perf_event_attr *attr, u64 *config)
630{
631 if (attr->type == pt->pmu_type) {
632 if (config)
633 *config = attr->config;
634 return true;
635 }
636
637 return false;
638}
639
640static bool intel_pt_exclude_kernel(struct intel_pt *pt)
641{
642 struct perf_evsel *evsel;
643
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300644 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300645 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
646 !evsel->attr.exclude_kernel)
647 return false;
648 }
649 return true;
650}
651
652static bool intel_pt_return_compression(struct intel_pt *pt)
653{
654 struct perf_evsel *evsel;
655 u64 config;
656
657 if (!pt->noretcomp_bit)
658 return true;
659
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300660 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300661 if (intel_pt_get_config(pt, &evsel->attr, &config) &&
662 (config & pt->noretcomp_bit))
663 return false;
664 }
665 return true;
666}
667
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300668static unsigned int intel_pt_mtc_period(struct intel_pt *pt)
669{
670 struct perf_evsel *evsel;
671 unsigned int shift;
672 u64 config;
673
674 if (!pt->mtc_freq_bits)
675 return 0;
676
677 for (shift = 0, config = pt->mtc_freq_bits; !(config & 1); shift++)
678 config >>= 1;
679
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300680 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300681 if (intel_pt_get_config(pt, &evsel->attr, &config))
682 return (config & pt->mtc_freq_bits) >> shift;
683 }
684 return 0;
685}
686
Adrian Hunter90e457f2015-07-17 19:33:41 +0300687static bool intel_pt_timeless_decoding(struct intel_pt *pt)
688{
689 struct perf_evsel *evsel;
690 bool timeless_decoding = true;
691 u64 config;
692
693 if (!pt->tsc_bit || !pt->cap_user_time_zero)
694 return true;
695
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300696 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300697 if (!(evsel->attr.sample_type & PERF_SAMPLE_TIME))
698 return true;
699 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
700 if (config & pt->tsc_bit)
701 timeless_decoding = false;
702 else
703 return true;
704 }
705 }
706 return timeless_decoding;
707}
708
709static bool intel_pt_tracing_kernel(struct intel_pt *pt)
710{
711 struct perf_evsel *evsel;
712
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300713 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300714 if (intel_pt_get_config(pt, &evsel->attr, NULL) &&
715 !evsel->attr.exclude_kernel)
716 return true;
717 }
718 return false;
719}
720
721static bool intel_pt_have_tsc(struct intel_pt *pt)
722{
723 struct perf_evsel *evsel;
724 bool have_tsc = false;
725 u64 config;
726
727 if (!pt->tsc_bit)
728 return false;
729
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300730 evlist__for_each_entry(pt->session->evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +0300731 if (intel_pt_get_config(pt, &evsel->attr, &config)) {
732 if (config & pt->tsc_bit)
733 have_tsc = true;
734 else
735 return false;
736 }
737 }
738 return have_tsc;
739}
740
741static u64 intel_pt_ns_to_ticks(const struct intel_pt *pt, u64 ns)
742{
743 u64 quot, rem;
744
745 quot = ns / pt->tc.time_mult;
746 rem = ns % pt->tc.time_mult;
747 return (quot << pt->tc.time_shift) + (rem << pt->tc.time_shift) /
748 pt->tc.time_mult;
749}
750
751static struct intel_pt_queue *intel_pt_alloc_queue(struct intel_pt *pt,
752 unsigned int queue_nr)
753{
754 struct intel_pt_params params = { .get_trace = 0, };
Adrian Hunterd129ab72018-05-31 13:23:45 +0300755 struct perf_env *env = pt->machine->env;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300756 struct intel_pt_queue *ptq;
757
758 ptq = zalloc(sizeof(struct intel_pt_queue));
759 if (!ptq)
760 return NULL;
761
762 if (pt->synth_opts.callchain) {
763 size_t sz = sizeof(struct ip_callchain);
764
765 sz += pt->synth_opts.callchain_sz * sizeof(u64);
766 ptq->chain = zalloc(sz);
767 if (!ptq->chain)
768 goto out_free;
769 }
770
Adrian Hunterf14445e2015-09-25 16:15:45 +0300771 if (pt->synth_opts.last_branch) {
772 size_t sz = sizeof(struct branch_stack);
773
774 sz += pt->synth_opts.last_branch_sz *
775 sizeof(struct branch_entry);
776 ptq->last_branch = zalloc(sz);
777 if (!ptq->last_branch)
778 goto out_free;
779 ptq->last_branch_rb = zalloc(sz);
780 if (!ptq->last_branch_rb)
781 goto out_free;
782 }
783
Adrian Hunter90e457f2015-07-17 19:33:41 +0300784 ptq->event_buf = malloc(PERF_SAMPLE_MAX_SIZE);
785 if (!ptq->event_buf)
786 goto out_free;
787
788 ptq->pt = pt;
789 ptq->queue_nr = queue_nr;
790 ptq->exclude_kernel = intel_pt_exclude_kernel(pt);
791 ptq->pid = -1;
792 ptq->tid = -1;
793 ptq->cpu = -1;
794 ptq->next_tid = -1;
795
796 params.get_trace = intel_pt_get_trace;
797 params.walk_insn = intel_pt_walk_next_insn;
798 params.data = ptq;
799 params.return_compression = intel_pt_return_compression(pt);
800 params.max_non_turbo_ratio = pt->max_non_turbo_ratio;
Adrian Hunter11fa7cb2015-07-17 19:33:54 +0300801 params.mtc_period = intel_pt_mtc_period(pt);
802 params.tsc_ctc_ratio_n = pt->tsc_ctc_ratio_n;
803 params.tsc_ctc_ratio_d = pt->tsc_ctc_ratio_d;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300804
Adrian Hunter2acee102016-09-23 17:38:48 +0300805 if (pt->filts.cnt > 0)
806 params.pgd_ip = intel_pt_pgd_ip;
807
Adrian Hunter90e457f2015-07-17 19:33:41 +0300808 if (pt->synth_opts.instructions) {
809 if (pt->synth_opts.period) {
810 switch (pt->synth_opts.period_type) {
811 case PERF_ITRACE_PERIOD_INSTRUCTIONS:
812 params.period_type =
813 INTEL_PT_PERIOD_INSTRUCTIONS;
814 params.period = pt->synth_opts.period;
815 break;
816 case PERF_ITRACE_PERIOD_TICKS:
817 params.period_type = INTEL_PT_PERIOD_TICKS;
818 params.period = pt->synth_opts.period;
819 break;
820 case PERF_ITRACE_PERIOD_NANOSECS:
821 params.period_type = INTEL_PT_PERIOD_TICKS;
822 params.period = intel_pt_ns_to_ticks(pt,
823 pt->synth_opts.period);
824 break;
825 default:
826 break;
827 }
828 }
829
830 if (!params.period) {
831 params.period_type = INTEL_PT_PERIOD_INSTRUCTIONS;
Adrian Huntere1791342015-09-25 16:15:32 +0300832 params.period = 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300833 }
834 }
835
Adrian Hunterd129ab72018-05-31 13:23:45 +0300836 if (env->cpuid && !strncmp(env->cpuid, "GenuineIntel,6,92,", 18))
837 params.flags |= INTEL_PT_FUP_WITH_NLIP;
838
Adrian Hunter90e457f2015-07-17 19:33:41 +0300839 ptq->decoder = intel_pt_decoder_new(&params);
840 if (!ptq->decoder)
841 goto out_free;
842
843 return ptq;
844
845out_free:
846 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300847 zfree(&ptq->last_branch);
848 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300849 zfree(&ptq->chain);
850 free(ptq);
851 return NULL;
852}
853
854static void intel_pt_free_queue(void *priv)
855{
856 struct intel_pt_queue *ptq = priv;
857
858 if (!ptq)
859 return;
860 thread__zput(ptq->thread);
861 intel_pt_decoder_free(ptq->decoder);
862 zfree(&ptq->event_buf);
Adrian Hunterf14445e2015-09-25 16:15:45 +0300863 zfree(&ptq->last_branch);
864 zfree(&ptq->last_branch_rb);
Adrian Hunter90e457f2015-07-17 19:33:41 +0300865 zfree(&ptq->chain);
866 free(ptq);
867}
868
869static void intel_pt_set_pid_tid_cpu(struct intel_pt *pt,
870 struct auxtrace_queue *queue)
871{
872 struct intel_pt_queue *ptq = queue->priv;
873
874 if (queue->tid == -1 || pt->have_sched_switch) {
875 ptq->tid = machine__get_current_tid(pt->machine, ptq->cpu);
876 thread__zput(ptq->thread);
877 }
878
879 if (!ptq->thread && ptq->tid != -1)
880 ptq->thread = machine__find_thread(pt->machine, -1, ptq->tid);
881
882 if (ptq->thread) {
883 ptq->pid = ptq->thread->pid_;
884 if (queue->cpu == -1)
885 ptq->cpu = ptq->thread->cpu;
886 }
887}
888
889static void intel_pt_sample_flags(struct intel_pt_queue *ptq)
890{
891 if (ptq->state->flags & INTEL_PT_ABORT_TX) {
892 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_TX_ABORT;
893 } else if (ptq->state->flags & INTEL_PT_ASYNC) {
894 if (ptq->state->to_ip)
895 ptq->flags = PERF_IP_FLAG_BRANCH | PERF_IP_FLAG_CALL |
896 PERF_IP_FLAG_ASYNC |
897 PERF_IP_FLAG_INTERRUPT;
898 else
899 ptq->flags = PERF_IP_FLAG_BRANCH |
900 PERF_IP_FLAG_TRACE_END;
901 ptq->insn_len = 0;
902 } else {
903 if (ptq->state->from_ip)
904 ptq->flags = intel_pt_insn_type(ptq->state->insn_op);
905 else
906 ptq->flags = PERF_IP_FLAG_BRANCH |
907 PERF_IP_FLAG_TRACE_BEGIN;
908 if (ptq->state->flags & INTEL_PT_IN_TX)
909 ptq->flags |= PERF_IP_FLAG_IN_TX;
910 ptq->insn_len = ptq->state->insn_len;
911 }
912}
913
914static int intel_pt_setup_queue(struct intel_pt *pt,
915 struct auxtrace_queue *queue,
916 unsigned int queue_nr)
917{
918 struct intel_pt_queue *ptq = queue->priv;
919
920 if (list_empty(&queue->head))
921 return 0;
922
923 if (!ptq) {
924 ptq = intel_pt_alloc_queue(pt, queue_nr);
925 if (!ptq)
926 return -ENOMEM;
927 queue->priv = ptq;
928
929 if (queue->cpu != -1)
930 ptq->cpu = queue->cpu;
931 ptq->tid = queue->tid;
932
933 if (pt->sampling_mode) {
934 if (pt->timeless_decoding)
935 ptq->step_through_buffers = true;
936 if (pt->timeless_decoding || !pt->have_sched_switch)
937 ptq->use_buffer_pid_tid = true;
938 }
Adrian Hunter6a330362018-03-07 16:02:22 +0200939
940 ptq->sync_switch = pt->sync_switch;
Adrian Hunter90e457f2015-07-17 19:33:41 +0300941 }
942
943 if (!ptq->on_heap &&
Adrian Hunter6a330362018-03-07 16:02:22 +0200944 (!ptq->sync_switch ||
Adrian Hunter90e457f2015-07-17 19:33:41 +0300945 ptq->switch_state != INTEL_PT_SS_EXPECTING_SWITCH_EVENT)) {
946 const struct intel_pt_state *state;
947 int ret;
948
949 if (pt->timeless_decoding)
950 return 0;
951
952 intel_pt_log("queue %u getting timestamp\n", queue_nr);
953 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
954 queue_nr, ptq->cpu, ptq->pid, ptq->tid);
955 while (1) {
956 state = intel_pt_decode(ptq->decoder);
957 if (state->err) {
958 if (state->err == INTEL_PT_ERR_NODATA) {
959 intel_pt_log("queue %u has no timestamp\n",
960 queue_nr);
961 return 0;
962 }
963 continue;
964 }
965 if (state->timestamp)
966 break;
967 }
968
969 ptq->timestamp = state->timestamp;
970 intel_pt_log("queue %u timestamp 0x%" PRIx64 "\n",
971 queue_nr, ptq->timestamp);
972 ptq->state = state;
973 ptq->have_sample = true;
974 intel_pt_sample_flags(ptq);
975 ret = auxtrace_heap__add(&pt->heap, queue_nr, ptq->timestamp);
976 if (ret)
977 return ret;
978 ptq->on_heap = true;
979 }
980
981 return 0;
982}
983
984static int intel_pt_setup_queues(struct intel_pt *pt)
985{
986 unsigned int i;
987 int ret;
988
989 for (i = 0; i < pt->queues.nr_queues; i++) {
990 ret = intel_pt_setup_queue(pt, &pt->queues.queue_array[i], i);
991 if (ret)
992 return ret;
993 }
994 return 0;
995}
996
Adrian Hunterf14445e2015-09-25 16:15:45 +0300997static inline void intel_pt_copy_last_branch_rb(struct intel_pt_queue *ptq)
998{
999 struct branch_stack *bs_src = ptq->last_branch_rb;
1000 struct branch_stack *bs_dst = ptq->last_branch;
1001 size_t nr = 0;
1002
1003 bs_dst->nr = bs_src->nr;
1004
1005 if (!bs_src->nr)
1006 return;
1007
1008 nr = ptq->pt->synth_opts.last_branch_sz - ptq->last_branch_pos;
1009 memcpy(&bs_dst->entries[0],
1010 &bs_src->entries[ptq->last_branch_pos],
1011 sizeof(struct branch_entry) * nr);
1012
1013 if (bs_src->nr >= ptq->pt->synth_opts.last_branch_sz) {
1014 memcpy(&bs_dst->entries[nr],
1015 &bs_src->entries[0],
1016 sizeof(struct branch_entry) * ptq->last_branch_pos);
1017 }
1018}
1019
1020static inline void intel_pt_reset_last_branch_rb(struct intel_pt_queue *ptq)
1021{
1022 ptq->last_branch_pos = 0;
1023 ptq->last_branch_rb->nr = 0;
1024}
1025
1026static void intel_pt_update_last_branch_rb(struct intel_pt_queue *ptq)
1027{
1028 const struct intel_pt_state *state = ptq->state;
1029 struct branch_stack *bs = ptq->last_branch_rb;
1030 struct branch_entry *be;
1031
1032 if (!ptq->last_branch_pos)
1033 ptq->last_branch_pos = ptq->pt->synth_opts.last_branch_sz;
1034
1035 ptq->last_branch_pos -= 1;
1036
1037 be = &bs->entries[ptq->last_branch_pos];
1038 be->from = state->from_ip;
1039 be->to = state->to_ip;
1040 be->flags.abort = !!(state->flags & INTEL_PT_ABORT_TX);
1041 be->flags.in_tx = !!(state->flags & INTEL_PT_IN_TX);
1042 /* No support for mispredict */
Adrian Hunterba11ba62015-09-25 16:15:56 +03001043 be->flags.mispred = ptq->pt->mispred_all;
Adrian Hunterf14445e2015-09-25 16:15:45 +03001044
1045 if (bs->nr < ptq->pt->synth_opts.last_branch_sz)
1046 bs->nr += 1;
1047}
1048
Adrian Hunter90e457f2015-07-17 19:33:41 +03001049static int intel_pt_inject_event(union perf_event *event,
1050 struct perf_sample *sample, u64 type,
1051 bool swapped)
1052{
1053 event->header.size = perf_event__sample_event_size(sample, type, 0);
1054 return perf_event__synthesize_sample(event, type, 0, sample, swapped);
1055}
1056
1057static int intel_pt_synth_branch_sample(struct intel_pt_queue *ptq)
1058{
1059 int ret;
1060 struct intel_pt *pt = ptq->pt;
1061 union perf_event *event = ptq->event_buf;
1062 struct perf_sample sample = { .ip = 0, };
Adrian Hunterf14445e2015-09-25 16:15:45 +03001063 struct dummy_branch_stack {
1064 u64 nr;
1065 struct branch_entry entries;
1066 } dummy_bs;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001067
Adrian Hunter385e3302015-09-25 16:15:44 +03001068 if (pt->branches_filter && !(pt->branches_filter & ptq->flags))
1069 return 0;
1070
Andi Kleend1706b32016-03-28 10:45:38 -07001071 if (pt->synth_opts.initial_skip &&
1072 pt->num_events++ < pt->synth_opts.initial_skip)
1073 return 0;
1074
Adrian Hunter90e457f2015-07-17 19:33:41 +03001075 event->sample.header.type = PERF_RECORD_SAMPLE;
1076 event->sample.header.misc = PERF_RECORD_MISC_USER;
1077 event->sample.header.size = sizeof(struct perf_event_header);
1078
1079 if (!pt->timeless_decoding)
1080 sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1081
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -03001082 sample.cpumode = PERF_RECORD_MISC_USER;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001083 sample.ip = ptq->state->from_ip;
1084 sample.pid = ptq->pid;
1085 sample.tid = ptq->tid;
1086 sample.addr = ptq->state->to_ip;
1087 sample.id = ptq->pt->branches_id;
1088 sample.stream_id = ptq->pt->branches_id;
1089 sample.period = 1;
1090 sample.cpu = ptq->cpu;
1091 sample.flags = ptq->flags;
1092 sample.insn_len = ptq->insn_len;
1093
Adrian Hunterf14445e2015-09-25 16:15:45 +03001094 /*
1095 * perf report cannot handle events without a branch stack when using
1096 * SORT_MODE__BRANCH so make a dummy one.
1097 */
1098 if (pt->synth_opts.last_branch && sort__mode == SORT_MODE__BRANCH) {
1099 dummy_bs = (struct dummy_branch_stack){
1100 .nr = 1,
1101 .entries = {
1102 .from = sample.ip,
1103 .to = sample.addr,
1104 },
1105 };
1106 sample.branch_stack = (struct branch_stack *)&dummy_bs;
1107 }
1108
Adrian Hunter90e457f2015-07-17 19:33:41 +03001109 if (pt->synth_opts.inject) {
1110 ret = intel_pt_inject_event(event, &sample,
1111 pt->branches_sample_type,
1112 pt->synth_needs_swap);
1113 if (ret)
1114 return ret;
1115 }
1116
1117 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1118 if (ret)
1119 pr_err("Intel Processor Trace: failed to deliver branch event, error %d\n",
1120 ret);
1121
1122 return ret;
1123}
1124
1125static int intel_pt_synth_instruction_sample(struct intel_pt_queue *ptq)
1126{
1127 int ret;
1128 struct intel_pt *pt = ptq->pt;
1129 union perf_event *event = ptq->event_buf;
1130 struct perf_sample sample = { .ip = 0, };
1131
Andi Kleend1706b32016-03-28 10:45:38 -07001132 if (pt->synth_opts.initial_skip &&
1133 pt->num_events++ < pt->synth_opts.initial_skip)
1134 return 0;
1135
Adrian Hunter90e457f2015-07-17 19:33:41 +03001136 event->sample.header.type = PERF_RECORD_SAMPLE;
1137 event->sample.header.misc = PERF_RECORD_MISC_USER;
1138 event->sample.header.size = sizeof(struct perf_event_header);
1139
1140 if (!pt->timeless_decoding)
1141 sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1142
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -03001143 sample.cpumode = PERF_RECORD_MISC_USER;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001144 sample.ip = ptq->state->from_ip;
1145 sample.pid = ptq->pid;
1146 sample.tid = ptq->tid;
1147 sample.addr = ptq->state->to_ip;
1148 sample.id = ptq->pt->instructions_id;
1149 sample.stream_id = ptq->pt->instructions_id;
Adrian Hunter2a21d032015-07-17 19:33:48 +03001150 sample.period = ptq->state->tot_insn_cnt - ptq->last_insn_cnt;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001151 sample.cpu = ptq->cpu;
1152 sample.flags = ptq->flags;
1153 sample.insn_len = ptq->insn_len;
1154
Adrian Hunter2a21d032015-07-17 19:33:48 +03001155 ptq->last_insn_cnt = ptq->state->tot_insn_cnt;
1156
Adrian Hunter90e457f2015-07-17 19:33:41 +03001157 if (pt->synth_opts.callchain) {
1158 thread_stack__sample(ptq->thread, ptq->chain,
1159 pt->synth_opts.callchain_sz, sample.ip);
1160 sample.callchain = ptq->chain;
1161 }
1162
Adrian Hunterf14445e2015-09-25 16:15:45 +03001163 if (pt->synth_opts.last_branch) {
1164 intel_pt_copy_last_branch_rb(ptq);
1165 sample.branch_stack = ptq->last_branch;
1166 }
1167
Adrian Hunter90e457f2015-07-17 19:33:41 +03001168 if (pt->synth_opts.inject) {
1169 ret = intel_pt_inject_event(event, &sample,
1170 pt->instructions_sample_type,
1171 pt->synth_needs_swap);
1172 if (ret)
1173 return ret;
1174 }
1175
1176 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1177 if (ret)
1178 pr_err("Intel Processor Trace: failed to deliver instruction event, error %d\n",
1179 ret);
1180
Adrian Hunterf14445e2015-09-25 16:15:45 +03001181 if (pt->synth_opts.last_branch)
1182 intel_pt_reset_last_branch_rb(ptq);
1183
Adrian Hunter90e457f2015-07-17 19:33:41 +03001184 return ret;
1185}
1186
1187static int intel_pt_synth_transaction_sample(struct intel_pt_queue *ptq)
1188{
1189 int ret;
1190 struct intel_pt *pt = ptq->pt;
1191 union perf_event *event = ptq->event_buf;
1192 struct perf_sample sample = { .ip = 0, };
1193
Andi Kleend1706b32016-03-28 10:45:38 -07001194 if (pt->synth_opts.initial_skip &&
1195 pt->num_events++ < pt->synth_opts.initial_skip)
1196 return 0;
1197
Adrian Hunter90e457f2015-07-17 19:33:41 +03001198 event->sample.header.type = PERF_RECORD_SAMPLE;
1199 event->sample.header.misc = PERF_RECORD_MISC_USER;
1200 event->sample.header.size = sizeof(struct perf_event_header);
1201
1202 if (!pt->timeless_decoding)
1203 sample.time = tsc_to_perf_time(ptq->timestamp, &pt->tc);
1204
Arnaldo Carvalho de Melo3ea223a2016-03-29 18:46:04 -03001205 sample.cpumode = PERF_RECORD_MISC_USER;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001206 sample.ip = ptq->state->from_ip;
1207 sample.pid = ptq->pid;
1208 sample.tid = ptq->tid;
1209 sample.addr = ptq->state->to_ip;
1210 sample.id = ptq->pt->transactions_id;
1211 sample.stream_id = ptq->pt->transactions_id;
1212 sample.period = 1;
1213 sample.cpu = ptq->cpu;
1214 sample.flags = ptq->flags;
1215 sample.insn_len = ptq->insn_len;
1216
1217 if (pt->synth_opts.callchain) {
1218 thread_stack__sample(ptq->thread, ptq->chain,
1219 pt->synth_opts.callchain_sz, sample.ip);
1220 sample.callchain = ptq->chain;
1221 }
1222
Adrian Hunterf14445e2015-09-25 16:15:45 +03001223 if (pt->synth_opts.last_branch) {
1224 intel_pt_copy_last_branch_rb(ptq);
1225 sample.branch_stack = ptq->last_branch;
1226 }
1227
Adrian Hunter90e457f2015-07-17 19:33:41 +03001228 if (pt->synth_opts.inject) {
1229 ret = intel_pt_inject_event(event, &sample,
1230 pt->transactions_sample_type,
1231 pt->synth_needs_swap);
1232 if (ret)
1233 return ret;
1234 }
1235
1236 ret = perf_session__deliver_synth_event(pt->session, event, &sample);
1237 if (ret)
1238 pr_err("Intel Processor Trace: failed to deliver transaction event, error %d\n",
1239 ret);
1240
Adrian Hunter1342e0b72016-04-18 13:57:48 +03001241 if (pt->synth_opts.last_branch)
Adrian Hunterf14445e2015-09-25 16:15:45 +03001242 intel_pt_reset_last_branch_rb(ptq);
1243
Adrian Hunter90e457f2015-07-17 19:33:41 +03001244 return ret;
1245}
1246
1247static int intel_pt_synth_error(struct intel_pt *pt, int code, int cpu,
1248 pid_t pid, pid_t tid, u64 ip)
1249{
1250 union perf_event event;
1251 char msg[MAX_AUXTRACE_ERROR_MSG];
1252 int err;
1253
1254 intel_pt__strerror(code, msg, MAX_AUXTRACE_ERROR_MSG);
1255
1256 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE,
1257 code, cpu, pid, tid, ip, msg);
1258
1259 err = perf_session__deliver_synth_event(pt->session, &event, NULL);
1260 if (err)
1261 pr_err("Intel Processor Trace: failed to deliver error event, error %d\n",
1262 err);
1263
1264 return err;
1265}
1266
1267static int intel_pt_next_tid(struct intel_pt *pt, struct intel_pt_queue *ptq)
1268{
1269 struct auxtrace_queue *queue;
1270 pid_t tid = ptq->next_tid;
1271 int err;
1272
1273 if (tid == -1)
1274 return 0;
1275
1276 intel_pt_log("switch: cpu %d tid %d\n", ptq->cpu, tid);
1277
1278 err = machine__set_current_tid(pt->machine, ptq->cpu, -1, tid);
1279
1280 queue = &pt->queues.queue_array[ptq->queue_nr];
1281 intel_pt_set_pid_tid_cpu(pt, queue);
1282
1283 ptq->next_tid = -1;
1284
1285 return err;
1286}
1287
1288static inline bool intel_pt_is_switch_ip(struct intel_pt_queue *ptq, u64 ip)
1289{
1290 struct intel_pt *pt = ptq->pt;
1291
1292 return ip == pt->switch_ip &&
1293 (ptq->flags & PERF_IP_FLAG_BRANCH) &&
1294 !(ptq->flags & (PERF_IP_FLAG_CONDITIONAL | PERF_IP_FLAG_ASYNC |
1295 PERF_IP_FLAG_INTERRUPT | PERF_IP_FLAG_TX_ABORT));
1296}
1297
1298static int intel_pt_sample(struct intel_pt_queue *ptq)
1299{
1300 const struct intel_pt_state *state = ptq->state;
1301 struct intel_pt *pt = ptq->pt;
1302 int err;
1303
1304 if (!ptq->have_sample)
1305 return 0;
1306
1307 ptq->have_sample = false;
1308
1309 if (pt->sample_instructions &&
Andi Kleend1706b32016-03-28 10:45:38 -07001310 (state->type & INTEL_PT_INSTRUCTION) &&
1311 (!pt->synth_opts.initial_skip ||
1312 pt->num_events++ >= pt->synth_opts.initial_skip)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001313 err = intel_pt_synth_instruction_sample(ptq);
1314 if (err)
1315 return err;
1316 }
1317
1318 if (pt->sample_transactions &&
Andi Kleend1706b32016-03-28 10:45:38 -07001319 (state->type & INTEL_PT_TRANSACTION) &&
1320 (!pt->synth_opts.initial_skip ||
1321 pt->num_events++ >= pt->synth_opts.initial_skip)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001322 err = intel_pt_synth_transaction_sample(ptq);
1323 if (err)
1324 return err;
1325 }
1326
1327 if (!(state->type & INTEL_PT_BRANCH))
1328 return 0;
1329
Adrian Hunter50f736372016-06-23 16:40:57 +03001330 if (pt->synth_opts.callchain || pt->synth_opts.thread_stack)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001331 thread_stack__event(ptq->thread, ptq->flags, state->from_ip,
1332 state->to_ip, ptq->insn_len,
1333 state->trace_nr);
1334 else
1335 thread_stack__set_trace_nr(ptq->thread, state->trace_nr);
1336
1337 if (pt->sample_branches) {
1338 err = intel_pt_synth_branch_sample(ptq);
1339 if (err)
1340 return err;
1341 }
1342
Adrian Hunterf14445e2015-09-25 16:15:45 +03001343 if (pt->synth_opts.last_branch)
1344 intel_pt_update_last_branch_rb(ptq);
1345
Adrian Hunter6a330362018-03-07 16:02:22 +02001346 if (!ptq->sync_switch)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001347 return 0;
1348
1349 if (intel_pt_is_switch_ip(ptq, state->to_ip)) {
1350 switch (ptq->switch_state) {
Adrian Hunter31606f72018-05-31 13:23:42 +03001351 case INTEL_PT_SS_NOT_TRACING:
Adrian Hunter90e457f2015-07-17 19:33:41 +03001352 case INTEL_PT_SS_UNKNOWN:
1353 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1354 err = intel_pt_next_tid(pt, ptq);
1355 if (err)
1356 return err;
1357 ptq->switch_state = INTEL_PT_SS_TRACING;
1358 break;
1359 default:
1360 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_EVENT;
1361 return 1;
1362 }
1363 } else if (!state->to_ip) {
1364 ptq->switch_state = INTEL_PT_SS_NOT_TRACING;
1365 } else if (ptq->switch_state == INTEL_PT_SS_NOT_TRACING) {
1366 ptq->switch_state = INTEL_PT_SS_UNKNOWN;
1367 } else if (ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1368 state->to_ip == pt->ptss_ip &&
1369 (ptq->flags & PERF_IP_FLAG_CALL)) {
1370 ptq->switch_state = INTEL_PT_SS_TRACING;
1371 }
1372
1373 return 0;
1374}
1375
Adrian Hunter86c27862015-08-13 12:40:57 +03001376static u64 intel_pt_switch_ip(struct intel_pt *pt, u64 *ptss_ip)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001377{
Adrian Hunter86c27862015-08-13 12:40:57 +03001378 struct machine *machine = pt->machine;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001379 struct map *map;
1380 struct symbol *sym, *start;
1381 u64 ip, switch_ip = 0;
Adrian Hunter86c27862015-08-13 12:40:57 +03001382 const char *ptss;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001383
1384 if (ptss_ip)
1385 *ptss_ip = 0;
1386
Arnaldo Carvalho de Meloa5e813c2015-09-30 11:54:04 -03001387 map = machine__kernel_map(machine);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001388 if (!map)
1389 return 0;
1390
Arnaldo Carvalho de Melobe39db92016-09-01 19:25:52 -03001391 if (map__load(map))
Adrian Hunter90e457f2015-07-17 19:33:41 +03001392 return 0;
1393
1394 start = dso__first_symbol(map->dso, MAP__FUNCTION);
1395
1396 for (sym = start; sym; sym = dso__next_symbol(sym)) {
1397 if (sym->binding == STB_GLOBAL &&
1398 !strcmp(sym->name, "__switch_to")) {
1399 ip = map->unmap_ip(map, sym->start);
1400 if (ip >= map->start && ip < map->end) {
1401 switch_ip = ip;
1402 break;
1403 }
1404 }
1405 }
1406
1407 if (!switch_ip || !ptss_ip)
1408 return 0;
1409
Adrian Hunter86c27862015-08-13 12:40:57 +03001410 if (pt->have_sched_switch == 1)
1411 ptss = "perf_trace_sched_switch";
1412 else
1413 ptss = "__perf_event_task_sched_out";
1414
Adrian Hunter90e457f2015-07-17 19:33:41 +03001415 for (sym = start; sym; sym = dso__next_symbol(sym)) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001416 if (!strcmp(sym->name, ptss)) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001417 ip = map->unmap_ip(map, sym->start);
1418 if (ip >= map->start && ip < map->end) {
1419 *ptss_ip = ip;
1420 break;
1421 }
1422 }
1423 }
1424
1425 return switch_ip;
1426}
1427
Adrian Hunter6a330362018-03-07 16:02:22 +02001428static void intel_pt_enable_sync_switch(struct intel_pt *pt)
1429{
1430 unsigned int i;
1431
1432 pt->sync_switch = true;
1433
1434 for (i = 0; i < pt->queues.nr_queues; i++) {
1435 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1436 struct intel_pt_queue *ptq = queue->priv;
1437
1438 if (ptq)
1439 ptq->sync_switch = true;
1440 }
1441}
1442
Adrian Hunter90e457f2015-07-17 19:33:41 +03001443static int intel_pt_run_decoder(struct intel_pt_queue *ptq, u64 *timestamp)
1444{
1445 const struct intel_pt_state *state = ptq->state;
1446 struct intel_pt *pt = ptq->pt;
1447 int err;
1448
1449 if (!pt->kernel_start) {
1450 pt->kernel_start = machine__kernel_start(pt->machine);
Adrian Hunter86c27862015-08-13 12:40:57 +03001451 if (pt->per_cpu_mmaps &&
1452 (pt->have_sched_switch == 1 || pt->have_sched_switch == 3) &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03001453 !pt->timeless_decoding && intel_pt_tracing_kernel(pt) &&
1454 !pt->sampling_mode) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001455 pt->switch_ip = intel_pt_switch_ip(pt, &pt->ptss_ip);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001456 if (pt->switch_ip) {
1457 intel_pt_log("switch_ip: %"PRIx64" ptss_ip: %"PRIx64"\n",
1458 pt->switch_ip, pt->ptss_ip);
Adrian Hunter6a330362018-03-07 16:02:22 +02001459 intel_pt_enable_sync_switch(pt);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001460 }
1461 }
1462 }
1463
1464 intel_pt_log("queue %u decoding cpu %d pid %d tid %d\n",
1465 ptq->queue_nr, ptq->cpu, ptq->pid, ptq->tid);
1466 while (1) {
1467 err = intel_pt_sample(ptq);
1468 if (err)
1469 return err;
1470
1471 state = intel_pt_decode(ptq->decoder);
1472 if (state->err) {
1473 if (state->err == INTEL_PT_ERR_NODATA)
1474 return 1;
Adrian Hunter6a330362018-03-07 16:02:22 +02001475 if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03001476 state->from_ip >= pt->kernel_start) {
Adrian Hunter6a330362018-03-07 16:02:22 +02001477 ptq->sync_switch = false;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001478 intel_pt_next_tid(pt, ptq);
1479 }
1480 if (pt->synth_opts.errors) {
1481 err = intel_pt_synth_error(pt, state->err,
1482 ptq->cpu, ptq->pid,
1483 ptq->tid,
1484 state->from_ip);
1485 if (err)
1486 return err;
1487 }
1488 continue;
1489 }
1490
1491 ptq->state = state;
1492 ptq->have_sample = true;
1493 intel_pt_sample_flags(ptq);
1494
1495 /* Use estimated TSC upon return to user space */
1496 if (pt->est_tsc &&
1497 (state->from_ip >= pt->kernel_start || !state->from_ip) &&
1498 state->to_ip && state->to_ip < pt->kernel_start) {
1499 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1500 state->timestamp, state->est_timestamp);
1501 ptq->timestamp = state->est_timestamp;
1502 /* Use estimated TSC in unknown switch state */
Adrian Hunter6a330362018-03-07 16:02:22 +02001503 } else if (ptq->sync_switch &&
Adrian Hunter90e457f2015-07-17 19:33:41 +03001504 ptq->switch_state == INTEL_PT_SS_UNKNOWN &&
1505 intel_pt_is_switch_ip(ptq, state->to_ip) &&
1506 ptq->next_tid == -1) {
1507 intel_pt_log("TSC %"PRIx64" est. TSC %"PRIx64"\n",
1508 state->timestamp, state->est_timestamp);
1509 ptq->timestamp = state->est_timestamp;
1510 } else if (state->timestamp > ptq->timestamp) {
1511 ptq->timestamp = state->timestamp;
1512 }
1513
1514 if (!pt->timeless_decoding && ptq->timestamp >= *timestamp) {
1515 *timestamp = ptq->timestamp;
1516 return 0;
1517 }
1518 }
1519 return 0;
1520}
1521
1522static inline int intel_pt_update_queues(struct intel_pt *pt)
1523{
1524 if (pt->queues.new_data) {
1525 pt->queues.new_data = false;
1526 return intel_pt_setup_queues(pt);
1527 }
1528 return 0;
1529}
1530
1531static int intel_pt_process_queues(struct intel_pt *pt, u64 timestamp)
1532{
1533 unsigned int queue_nr;
1534 u64 ts;
1535 int ret;
1536
1537 while (1) {
1538 struct auxtrace_queue *queue;
1539 struct intel_pt_queue *ptq;
1540
1541 if (!pt->heap.heap_cnt)
1542 return 0;
1543
1544 if (pt->heap.heap_array[0].ordinal >= timestamp)
1545 return 0;
1546
1547 queue_nr = pt->heap.heap_array[0].queue_nr;
1548 queue = &pt->queues.queue_array[queue_nr];
1549 ptq = queue->priv;
1550
1551 intel_pt_log("queue %u processing 0x%" PRIx64 " to 0x%" PRIx64 "\n",
1552 queue_nr, pt->heap.heap_array[0].ordinal,
1553 timestamp);
1554
1555 auxtrace_heap__pop(&pt->heap);
1556
1557 if (pt->heap.heap_cnt) {
1558 ts = pt->heap.heap_array[0].ordinal + 1;
1559 if (ts > timestamp)
1560 ts = timestamp;
1561 } else {
1562 ts = timestamp;
1563 }
1564
1565 intel_pt_set_pid_tid_cpu(pt, queue);
1566
1567 ret = intel_pt_run_decoder(ptq, &ts);
1568
1569 if (ret < 0) {
1570 auxtrace_heap__add(&pt->heap, queue_nr, ts);
1571 return ret;
1572 }
1573
1574 if (!ret) {
1575 ret = auxtrace_heap__add(&pt->heap, queue_nr, ts);
1576 if (ret < 0)
1577 return ret;
1578 } else {
1579 ptq->on_heap = false;
1580 }
1581 }
1582
1583 return 0;
1584}
1585
1586static int intel_pt_process_timeless_queues(struct intel_pt *pt, pid_t tid,
1587 u64 time_)
1588{
1589 struct auxtrace_queues *queues = &pt->queues;
1590 unsigned int i;
1591 u64 ts = 0;
1592
1593 for (i = 0; i < queues->nr_queues; i++) {
1594 struct auxtrace_queue *queue = &pt->queues.queue_array[i];
1595 struct intel_pt_queue *ptq = queue->priv;
1596
1597 if (ptq && (tid == -1 || ptq->tid == tid)) {
1598 ptq->time = time_;
1599 intel_pt_set_pid_tid_cpu(pt, queue);
1600 intel_pt_run_decoder(ptq, &ts);
1601 }
1602 }
1603 return 0;
1604}
1605
1606static int intel_pt_lost(struct intel_pt *pt, struct perf_sample *sample)
1607{
1608 return intel_pt_synth_error(pt, INTEL_PT_ERR_LOST, sample->cpu,
1609 sample->pid, sample->tid, 0);
1610}
1611
1612static struct intel_pt_queue *intel_pt_cpu_to_ptq(struct intel_pt *pt, int cpu)
1613{
1614 unsigned i, j;
1615
1616 if (cpu < 0 || !pt->queues.nr_queues)
1617 return NULL;
1618
1619 if ((unsigned)cpu >= pt->queues.nr_queues)
1620 i = pt->queues.nr_queues - 1;
1621 else
1622 i = cpu;
1623
1624 if (pt->queues.queue_array[i].cpu == cpu)
1625 return pt->queues.queue_array[i].priv;
1626
1627 for (j = 0; i > 0; j++) {
1628 if (pt->queues.queue_array[--i].cpu == cpu)
1629 return pt->queues.queue_array[i].priv;
1630 }
1631
1632 for (; j < pt->queues.nr_queues; j++) {
1633 if (pt->queues.queue_array[j].cpu == cpu)
1634 return pt->queues.queue_array[j].priv;
1635 }
1636
1637 return NULL;
1638}
1639
Adrian Hunter86c27862015-08-13 12:40:57 +03001640static int intel_pt_sync_switch(struct intel_pt *pt, int cpu, pid_t tid,
1641 u64 timestamp)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001642{
1643 struct intel_pt_queue *ptq;
Adrian Hunter86c27862015-08-13 12:40:57 +03001644 int err;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001645
1646 if (!pt->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03001647 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001648
1649 ptq = intel_pt_cpu_to_ptq(pt, cpu);
Adrian Hunter6a330362018-03-07 16:02:22 +02001650 if (!ptq || !ptq->sync_switch)
Adrian Hunter86c27862015-08-13 12:40:57 +03001651 return 1;
Adrian Hunter90e457f2015-07-17 19:33:41 +03001652
1653 switch (ptq->switch_state) {
1654 case INTEL_PT_SS_NOT_TRACING:
1655 ptq->next_tid = -1;
1656 break;
1657 case INTEL_PT_SS_UNKNOWN:
1658 case INTEL_PT_SS_TRACING:
1659 ptq->next_tid = tid;
1660 ptq->switch_state = INTEL_PT_SS_EXPECTING_SWITCH_IP;
1661 return 0;
1662 case INTEL_PT_SS_EXPECTING_SWITCH_EVENT:
1663 if (!ptq->on_heap) {
Adrian Hunter86c27862015-08-13 12:40:57 +03001664 ptq->timestamp = perf_time_to_tsc(timestamp,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001665 &pt->tc);
1666 err = auxtrace_heap__add(&pt->heap, ptq->queue_nr,
1667 ptq->timestamp);
1668 if (err)
1669 return err;
1670 ptq->on_heap = true;
1671 }
1672 ptq->switch_state = INTEL_PT_SS_TRACING;
1673 break;
1674 case INTEL_PT_SS_EXPECTING_SWITCH_IP:
1675 ptq->next_tid = tid;
1676 intel_pt_log("ERROR: cpu %d expecting switch ip\n", cpu);
1677 break;
1678 default:
1679 break;
1680 }
Adrian Hunter86c27862015-08-13 12:40:57 +03001681
1682 return 1;
1683}
1684
1685static int intel_pt_process_switch(struct intel_pt *pt,
1686 struct perf_sample *sample)
1687{
1688 struct perf_evsel *evsel;
1689 pid_t tid;
1690 int cpu, ret;
1691
1692 evsel = perf_evlist__id2evsel(pt->session->evlist, sample->id);
1693 if (evsel != pt->switch_evsel)
1694 return 0;
1695
1696 tid = perf_evsel__intval(evsel, sample, "next_pid");
1697 cpu = sample->cpu;
1698
1699 intel_pt_log("sched_switch: cpu %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1700 cpu, tid, sample->time, perf_time_to_tsc(sample->time,
1701 &pt->tc));
1702
1703 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1704 if (ret <= 0)
1705 return ret;
1706
Adrian Hunter90e457f2015-07-17 19:33:41 +03001707 return machine__set_current_tid(pt->machine, cpu, -1, tid);
1708}
1709
Adrian Hunter86c27862015-08-13 12:40:57 +03001710static int intel_pt_context_switch(struct intel_pt *pt, union perf_event *event,
1711 struct perf_sample *sample)
1712{
1713 bool out = event->header.misc & PERF_RECORD_MISC_SWITCH_OUT;
1714 pid_t pid, tid;
1715 int cpu, ret;
1716
1717 cpu = sample->cpu;
1718
1719 if (pt->have_sched_switch == 3) {
1720 if (!out)
1721 return 0;
1722 if (event->header.type != PERF_RECORD_SWITCH_CPU_WIDE) {
1723 pr_err("Expecting CPU-wide context switch event\n");
1724 return -EINVAL;
1725 }
1726 pid = event->context_switch.next_prev_pid;
1727 tid = event->context_switch.next_prev_tid;
1728 } else {
1729 if (out)
1730 return 0;
1731 pid = sample->pid;
1732 tid = sample->tid;
1733 }
1734
1735 if (tid == -1) {
1736 pr_err("context_switch event has no tid\n");
1737 return -EINVAL;
1738 }
1739
1740 intel_pt_log("context_switch: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1741 cpu, pid, tid, sample->time, perf_time_to_tsc(sample->time,
1742 &pt->tc));
1743
1744 ret = intel_pt_sync_switch(pt, cpu, tid, sample->time);
1745 if (ret <= 0)
1746 return ret;
1747
1748 return machine__set_current_tid(pt->machine, cpu, pid, tid);
1749}
1750
Adrian Hunter90e457f2015-07-17 19:33:41 +03001751static int intel_pt_process_itrace_start(struct intel_pt *pt,
1752 union perf_event *event,
1753 struct perf_sample *sample)
1754{
1755 if (!pt->per_cpu_mmaps)
1756 return 0;
1757
1758 intel_pt_log("itrace_start: cpu %d pid %d tid %d time %"PRIu64" tsc %#"PRIx64"\n",
1759 sample->cpu, event->itrace_start.pid,
1760 event->itrace_start.tid, sample->time,
1761 perf_time_to_tsc(sample->time, &pt->tc));
1762
1763 return machine__set_current_tid(pt->machine, sample->cpu,
1764 event->itrace_start.pid,
1765 event->itrace_start.tid);
1766}
1767
1768static int intel_pt_process_event(struct perf_session *session,
1769 union perf_event *event,
1770 struct perf_sample *sample,
1771 struct perf_tool *tool)
1772{
1773 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1774 auxtrace);
1775 u64 timestamp;
1776 int err = 0;
1777
1778 if (dump_trace)
1779 return 0;
1780
1781 if (!tool->ordered_events) {
1782 pr_err("Intel Processor Trace requires ordered events\n");
1783 return -EINVAL;
1784 }
1785
Adrian Hunter81cd60c2015-08-20 11:51:32 +03001786 if (sample->time && sample->time != (u64)-1)
Adrian Hunter90e457f2015-07-17 19:33:41 +03001787 timestamp = perf_time_to_tsc(sample->time, &pt->tc);
1788 else
1789 timestamp = 0;
1790
1791 if (timestamp || pt->timeless_decoding) {
1792 err = intel_pt_update_queues(pt);
1793 if (err)
1794 return err;
1795 }
1796
1797 if (pt->timeless_decoding) {
1798 if (event->header.type == PERF_RECORD_EXIT) {
1799 err = intel_pt_process_timeless_queues(pt,
Adrian Hunter53ff6bc2015-08-18 12:07:05 +03001800 event->fork.tid,
Adrian Hunter90e457f2015-07-17 19:33:41 +03001801 sample->time);
1802 }
1803 } else if (timestamp) {
1804 err = intel_pt_process_queues(pt, timestamp);
1805 }
1806 if (err)
1807 return err;
1808
1809 if (event->header.type == PERF_RECORD_AUX &&
1810 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) &&
1811 pt->synth_opts.errors) {
1812 err = intel_pt_lost(pt, sample);
1813 if (err)
1814 return err;
1815 }
1816
1817 if (pt->switch_evsel && event->header.type == PERF_RECORD_SAMPLE)
1818 err = intel_pt_process_switch(pt, sample);
1819 else if (event->header.type == PERF_RECORD_ITRACE_START)
1820 err = intel_pt_process_itrace_start(pt, event, sample);
Adrian Hunter86c27862015-08-13 12:40:57 +03001821 else if (event->header.type == PERF_RECORD_SWITCH ||
1822 event->header.type == PERF_RECORD_SWITCH_CPU_WIDE)
1823 err = intel_pt_context_switch(pt, event, sample);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001824
1825 intel_pt_log("event %s (%u): cpu %d time %"PRIu64" tsc %#"PRIx64"\n",
1826 perf_event__name(event->header.type), event->header.type,
1827 sample->cpu, sample->time, timestamp);
1828
1829 return err;
1830}
1831
1832static int intel_pt_flush(struct perf_session *session, struct perf_tool *tool)
1833{
1834 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1835 auxtrace);
1836 int ret;
1837
1838 if (dump_trace)
1839 return 0;
1840
1841 if (!tool->ordered_events)
1842 return -EINVAL;
1843
1844 ret = intel_pt_update_queues(pt);
1845 if (ret < 0)
1846 return ret;
1847
1848 if (pt->timeless_decoding)
1849 return intel_pt_process_timeless_queues(pt, -1,
1850 MAX_TIMESTAMP - 1);
1851
1852 return intel_pt_process_queues(pt, MAX_TIMESTAMP);
1853}
1854
1855static void intel_pt_free_events(struct perf_session *session)
1856{
1857 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1858 auxtrace);
1859 struct auxtrace_queues *queues = &pt->queues;
1860 unsigned int i;
1861
1862 for (i = 0; i < queues->nr_queues; i++) {
1863 intel_pt_free_queue(queues->queue_array[i].priv);
1864 queues->queue_array[i].priv = NULL;
1865 }
1866 intel_pt_log_disable();
1867 auxtrace_queues__free(queues);
1868}
1869
1870static void intel_pt_free(struct perf_session *session)
1871{
1872 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1873 auxtrace);
1874
1875 auxtrace_heap__free(&pt->heap);
1876 intel_pt_free_events(session);
1877 session->auxtrace = NULL;
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03001878 thread__put(pt->unknown_thread);
Adrian Hunter2acee102016-09-23 17:38:48 +03001879 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03001880 zfree(&pt->filter);
Adrian Hunter90e457f2015-07-17 19:33:41 +03001881 free(pt);
1882}
1883
1884static int intel_pt_process_auxtrace_event(struct perf_session *session,
1885 union perf_event *event,
1886 struct perf_tool *tool __maybe_unused)
1887{
1888 struct intel_pt *pt = container_of(session->auxtrace, struct intel_pt,
1889 auxtrace);
1890
1891 if (pt->sampling_mode)
1892 return 0;
1893
1894 if (!pt->data_queued) {
1895 struct auxtrace_buffer *buffer;
1896 off_t data_offset;
1897 int fd = perf_data_file__fd(session->file);
1898 int err;
1899
1900 if (perf_data_file__is_pipe(session->file)) {
1901 data_offset = 0;
1902 } else {
1903 data_offset = lseek(fd, 0, SEEK_CUR);
1904 if (data_offset == -1)
1905 return -errno;
1906 }
1907
1908 err = auxtrace_queues__add_event(&pt->queues, session, event,
1909 data_offset, &buffer);
1910 if (err)
1911 return err;
1912
1913 /* Dump here now we have copied a piped trace out of the pipe */
1914 if (dump_trace) {
1915 if (auxtrace_buffer__get_data(buffer, fd)) {
1916 intel_pt_dump_event(pt, buffer->data,
1917 buffer->size);
1918 auxtrace_buffer__put_data(buffer);
1919 }
1920 }
1921 }
1922
1923 return 0;
1924}
1925
1926struct intel_pt_synth {
1927 struct perf_tool dummy_tool;
1928 struct perf_session *session;
1929};
1930
1931static int intel_pt_event_synth(struct perf_tool *tool,
1932 union perf_event *event,
1933 struct perf_sample *sample __maybe_unused,
1934 struct machine *machine __maybe_unused)
1935{
1936 struct intel_pt_synth *intel_pt_synth =
1937 container_of(tool, struct intel_pt_synth, dummy_tool);
1938
1939 return perf_session__deliver_synth_event(intel_pt_synth->session, event,
1940 NULL);
1941}
1942
1943static int intel_pt_synth_event(struct perf_session *session,
1944 struct perf_event_attr *attr, u64 id)
1945{
1946 struct intel_pt_synth intel_pt_synth;
1947
1948 memset(&intel_pt_synth, 0, sizeof(struct intel_pt_synth));
1949 intel_pt_synth.session = session;
1950
1951 return perf_event__synthesize_attr(&intel_pt_synth.dummy_tool, attr, 1,
1952 &id, intel_pt_event_synth);
1953}
1954
1955static int intel_pt_synth_events(struct intel_pt *pt,
1956 struct perf_session *session)
1957{
1958 struct perf_evlist *evlist = session->evlist;
1959 struct perf_evsel *evsel;
1960 struct perf_event_attr attr;
1961 bool found = false;
1962 u64 id;
1963 int err;
1964
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03001965 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03001966 if (evsel->attr.type == pt->pmu_type && evsel->ids) {
1967 found = true;
1968 break;
1969 }
1970 }
1971
1972 if (!found) {
1973 pr_debug("There are no selected events with Intel Processor Trace data\n");
1974 return 0;
1975 }
1976
1977 memset(&attr, 0, sizeof(struct perf_event_attr));
1978 attr.size = sizeof(struct perf_event_attr);
1979 attr.type = PERF_TYPE_HARDWARE;
1980 attr.sample_type = evsel->attr.sample_type & PERF_SAMPLE_MASK;
1981 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID |
1982 PERF_SAMPLE_PERIOD;
1983 if (pt->timeless_decoding)
1984 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME;
1985 else
1986 attr.sample_type |= PERF_SAMPLE_TIME;
1987 if (!pt->per_cpu_mmaps)
1988 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU;
1989 attr.exclude_user = evsel->attr.exclude_user;
1990 attr.exclude_kernel = evsel->attr.exclude_kernel;
1991 attr.exclude_hv = evsel->attr.exclude_hv;
1992 attr.exclude_host = evsel->attr.exclude_host;
1993 attr.exclude_guest = evsel->attr.exclude_guest;
1994 attr.sample_id_all = evsel->attr.sample_id_all;
1995 attr.read_format = evsel->attr.read_format;
1996
1997 id = evsel->id[0] + 1000000000;
1998 if (!id)
1999 id = 1;
2000
2001 if (pt->synth_opts.instructions) {
2002 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2003 if (pt->synth_opts.period_type == PERF_ITRACE_PERIOD_NANOSECS)
2004 attr.sample_period =
2005 intel_pt_ns_to_ticks(pt, pt->synth_opts.period);
2006 else
2007 attr.sample_period = pt->synth_opts.period;
2008 pt->instructions_sample_period = attr.sample_period;
2009 if (pt->synth_opts.callchain)
2010 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
Adrian Hunterf14445e2015-09-25 16:15:45 +03002011 if (pt->synth_opts.last_branch)
2012 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002013 pr_debug("Synthesizing 'instructions' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2014 id, (u64)attr.sample_type);
2015 err = intel_pt_synth_event(session, &attr, id);
2016 if (err) {
2017 pr_err("%s: failed to synthesize 'instructions' event type\n",
2018 __func__);
2019 return err;
2020 }
2021 pt->sample_instructions = true;
2022 pt->instructions_sample_type = attr.sample_type;
2023 pt->instructions_id = id;
2024 id += 1;
2025 }
2026
2027 if (pt->synth_opts.transactions) {
2028 attr.config = PERF_COUNT_HW_INSTRUCTIONS;
2029 attr.sample_period = 1;
2030 if (pt->synth_opts.callchain)
2031 attr.sample_type |= PERF_SAMPLE_CALLCHAIN;
Adrian Hunterf14445e2015-09-25 16:15:45 +03002032 if (pt->synth_opts.last_branch)
2033 attr.sample_type |= PERF_SAMPLE_BRANCH_STACK;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002034 pr_debug("Synthesizing 'transactions' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2035 id, (u64)attr.sample_type);
2036 err = intel_pt_synth_event(session, &attr, id);
2037 if (err) {
2038 pr_err("%s: failed to synthesize 'transactions' event type\n",
2039 __func__);
2040 return err;
2041 }
2042 pt->sample_transactions = true;
2043 pt->transactions_id = id;
2044 id += 1;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002045 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002046 if (evsel->id && evsel->id[0] == pt->transactions_id) {
2047 if (evsel->name)
2048 zfree(&evsel->name);
2049 evsel->name = strdup("transactions");
2050 break;
2051 }
2052 }
2053 }
2054
2055 if (pt->synth_opts.branches) {
2056 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
2057 attr.sample_period = 1;
2058 attr.sample_type |= PERF_SAMPLE_ADDR;
2059 attr.sample_type &= ~(u64)PERF_SAMPLE_CALLCHAIN;
Adrian Hunterf14445e2015-09-25 16:15:45 +03002060 attr.sample_type &= ~(u64)PERF_SAMPLE_BRANCH_STACK;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002061 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n",
2062 id, (u64)attr.sample_type);
2063 err = intel_pt_synth_event(session, &attr, id);
2064 if (err) {
2065 pr_err("%s: failed to synthesize 'branches' event type\n",
2066 __func__);
2067 return err;
2068 }
2069 pt->sample_branches = true;
2070 pt->branches_sample_type = attr.sample_type;
2071 pt->branches_id = id;
2072 }
2073
2074 pt->synth_needs_swap = evsel->needs_swap;
2075
2076 return 0;
2077}
2078
2079static struct perf_evsel *intel_pt_find_sched_switch(struct perf_evlist *evlist)
2080{
2081 struct perf_evsel *evsel;
2082
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002083 evlist__for_each_entry_reverse(evlist, evsel) {
Adrian Hunter90e457f2015-07-17 19:33:41 +03002084 const char *name = perf_evsel__name(evsel);
2085
2086 if (!strcmp(name, "sched:sched_switch"))
2087 return evsel;
2088 }
2089
2090 return NULL;
2091}
2092
Adrian Hunter86c27862015-08-13 12:40:57 +03002093static bool intel_pt_find_switch(struct perf_evlist *evlist)
2094{
2095 struct perf_evsel *evsel;
2096
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -03002097 evlist__for_each_entry(evlist, evsel) {
Adrian Hunter86c27862015-08-13 12:40:57 +03002098 if (evsel->attr.context_switch)
2099 return true;
2100 }
2101
2102 return false;
2103}
2104
Adrian Hunterba11ba62015-09-25 16:15:56 +03002105static int intel_pt_perf_config(const char *var, const char *value, void *data)
2106{
2107 struct intel_pt *pt = data;
2108
2109 if (!strcmp(var, "intel-pt.mispred-all"))
2110 pt->mispred_all = perf_config_bool(var, value);
2111
2112 return 0;
2113}
2114
Adrian Hunter90e457f2015-07-17 19:33:41 +03002115static const char * const intel_pt_info_fmts[] = {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03002116 [INTEL_PT_PMU_TYPE] = " PMU Type %"PRId64"\n",
2117 [INTEL_PT_TIME_SHIFT] = " Time Shift %"PRIu64"\n",
2118 [INTEL_PT_TIME_MULT] = " Time Muliplier %"PRIu64"\n",
2119 [INTEL_PT_TIME_ZERO] = " Time Zero %"PRIu64"\n",
2120 [INTEL_PT_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n",
2121 [INTEL_PT_TSC_BIT] = " TSC bit %#"PRIx64"\n",
2122 [INTEL_PT_NORETCOMP_BIT] = " NoRETComp bit %#"PRIx64"\n",
2123 [INTEL_PT_HAVE_SCHED_SWITCH] = " Have sched_switch %"PRId64"\n",
2124 [INTEL_PT_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n",
2125 [INTEL_PT_PER_CPU_MMAPS] = " Per-cpu maps %"PRId64"\n",
2126 [INTEL_PT_MTC_BIT] = " MTC bit %#"PRIx64"\n",
2127 [INTEL_PT_TSC_CTC_N] = " TSC:CTC numerator %"PRIu64"\n",
2128 [INTEL_PT_TSC_CTC_D] = " TSC:CTC denominator %"PRIu64"\n",
2129 [INTEL_PT_CYC_BIT] = " CYC bit %#"PRIx64"\n",
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002130 [INTEL_PT_MAX_NONTURBO_RATIO] = " Max non-turbo ratio %"PRIu64"\n",
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002131 [INTEL_PT_FILTER_STR_LEN] = " Filter string len. %"PRIu64"\n",
Adrian Hunter90e457f2015-07-17 19:33:41 +03002132};
2133
2134static void intel_pt_print_info(u64 *arr, int start, int finish)
2135{
2136 int i;
2137
2138 if (!dump_trace)
2139 return;
2140
2141 for (i = start; i <= finish; i++)
2142 fprintf(stdout, intel_pt_info_fmts[i], arr[i]);
2143}
2144
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002145static void intel_pt_print_info_str(const char *name, const char *str)
2146{
2147 if (!dump_trace)
2148 return;
2149
2150 fprintf(stdout, " %-20s%s\n", name, str ? str : "");
2151}
2152
Adrian Hunter40b746a2016-09-23 17:38:44 +03002153static bool intel_pt_has(struct auxtrace_info_event *auxtrace_info, int pos)
2154{
2155 return auxtrace_info->header.size >=
2156 sizeof(struct auxtrace_info_event) + (sizeof(u64) * (pos + 1));
2157}
2158
Adrian Hunter90e457f2015-07-17 19:33:41 +03002159int intel_pt_process_auxtrace_info(union perf_event *event,
2160 struct perf_session *session)
2161{
2162 struct auxtrace_info_event *auxtrace_info = &event->auxtrace_info;
2163 size_t min_sz = sizeof(u64) * INTEL_PT_PER_CPU_MMAPS;
2164 struct intel_pt *pt;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002165 void *info_end;
2166 u64 *info;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002167 int err;
2168
2169 if (auxtrace_info->header.size < sizeof(struct auxtrace_info_event) +
2170 min_sz)
2171 return -EINVAL;
2172
2173 pt = zalloc(sizeof(struct intel_pt));
2174 if (!pt)
2175 return -ENOMEM;
2176
Adrian Hunter2acee102016-09-23 17:38:48 +03002177 addr_filters__init(&pt->filts);
2178
Adrian Hunterba11ba62015-09-25 16:15:56 +03002179 perf_config(intel_pt_perf_config, pt);
2180
Adrian Hunter90e457f2015-07-17 19:33:41 +03002181 err = auxtrace_queues__init(&pt->queues);
2182 if (err)
2183 goto err_free;
2184
2185 intel_pt_log_set_name(INTEL_PT_PMU_NAME);
2186
2187 pt->session = session;
2188 pt->machine = &session->machines.host; /* No kvm support */
2189 pt->auxtrace_type = auxtrace_info->type;
2190 pt->pmu_type = auxtrace_info->priv[INTEL_PT_PMU_TYPE];
2191 pt->tc.time_shift = auxtrace_info->priv[INTEL_PT_TIME_SHIFT];
2192 pt->tc.time_mult = auxtrace_info->priv[INTEL_PT_TIME_MULT];
2193 pt->tc.time_zero = auxtrace_info->priv[INTEL_PT_TIME_ZERO];
2194 pt->cap_user_time_zero = auxtrace_info->priv[INTEL_PT_CAP_USER_TIME_ZERO];
2195 pt->tsc_bit = auxtrace_info->priv[INTEL_PT_TSC_BIT];
2196 pt->noretcomp_bit = auxtrace_info->priv[INTEL_PT_NORETCOMP_BIT];
2197 pt->have_sched_switch = auxtrace_info->priv[INTEL_PT_HAVE_SCHED_SWITCH];
2198 pt->snapshot_mode = auxtrace_info->priv[INTEL_PT_SNAPSHOT_MODE];
2199 pt->per_cpu_mmaps = auxtrace_info->priv[INTEL_PT_PER_CPU_MMAPS];
2200 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_PMU_TYPE,
2201 INTEL_PT_PER_CPU_MMAPS);
2202
Adrian Hunter40b746a2016-09-23 17:38:44 +03002203 if (intel_pt_has(auxtrace_info, INTEL_PT_CYC_BIT)) {
Adrian Hunter11fa7cb2015-07-17 19:33:54 +03002204 pt->mtc_bit = auxtrace_info->priv[INTEL_PT_MTC_BIT];
2205 pt->mtc_freq_bits = auxtrace_info->priv[INTEL_PT_MTC_FREQ_BITS];
2206 pt->tsc_ctc_ratio_n = auxtrace_info->priv[INTEL_PT_TSC_CTC_N];
2207 pt->tsc_ctc_ratio_d = auxtrace_info->priv[INTEL_PT_TSC_CTC_D];
2208 pt->cyc_bit = auxtrace_info->priv[INTEL_PT_CYC_BIT];
2209 intel_pt_print_info(&auxtrace_info->priv[0], INTEL_PT_MTC_BIT,
2210 INTEL_PT_CYC_BIT);
2211 }
2212
Adrian Hunter40b746a2016-09-23 17:38:44 +03002213 if (intel_pt_has(auxtrace_info, INTEL_PT_MAX_NONTURBO_RATIO)) {
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002214 pt->max_non_turbo_ratio =
2215 auxtrace_info->priv[INTEL_PT_MAX_NONTURBO_RATIO];
2216 intel_pt_print_info(&auxtrace_info->priv[0],
2217 INTEL_PT_MAX_NONTURBO_RATIO,
2218 INTEL_PT_MAX_NONTURBO_RATIO);
2219 }
2220
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002221 info = &auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN] + 1;
2222 info_end = (void *)info + auxtrace_info->header.size;
2223
2224 if (intel_pt_has(auxtrace_info, INTEL_PT_FILTER_STR_LEN)) {
2225 size_t len;
2226
2227 len = auxtrace_info->priv[INTEL_PT_FILTER_STR_LEN];
2228 intel_pt_print_info(&auxtrace_info->priv[0],
2229 INTEL_PT_FILTER_STR_LEN,
2230 INTEL_PT_FILTER_STR_LEN);
2231 if (len) {
2232 const char *filter = (const char *)info;
2233
2234 len = roundup(len + 1, 8);
2235 info += len >> 3;
2236 if ((void *)info > info_end) {
2237 pr_err("%s: bad filter string length\n", __func__);
2238 err = -EINVAL;
2239 goto err_free_queues;
2240 }
2241 pt->filter = memdup(filter, len);
2242 if (!pt->filter) {
2243 err = -ENOMEM;
2244 goto err_free_queues;
2245 }
2246 if (session->header.needs_swap)
2247 mem_bswap_64(pt->filter, len);
2248 if (pt->filter[len - 1]) {
2249 pr_err("%s: filter string not null terminated\n", __func__);
2250 err = -EINVAL;
2251 goto err_free_queues;
2252 }
Adrian Hunter2acee102016-09-23 17:38:48 +03002253 err = addr_filters__parse_bare_filter(&pt->filts,
2254 filter);
2255 if (err)
2256 goto err_free_queues;
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002257 }
2258 intel_pt_print_info_str("Filter string", pt->filter);
2259 }
2260
Adrian Hunter90e457f2015-07-17 19:33:41 +03002261 pt->timeless_decoding = intel_pt_timeless_decoding(pt);
2262 pt->have_tsc = intel_pt_have_tsc(pt);
2263 pt->sampling_mode = false;
2264 pt->est_tsc = !pt->timeless_decoding;
2265
2266 pt->unknown_thread = thread__new(999999999, 999999999);
2267 if (!pt->unknown_thread) {
2268 err = -ENOMEM;
2269 goto err_free_queues;
2270 }
Adrian Hunter3a4acda2016-02-01 03:21:04 +00002271
2272 /*
2273 * Since this thread will not be kept in any rbtree not in a
2274 * list, initialize its list node so that at thread__put() the
2275 * current thread lifetime assuption is kept and we don't segfault
2276 * at list_del_init().
2277 */
2278 INIT_LIST_HEAD(&pt->unknown_thread->node);
2279
Adrian Hunter90e457f2015-07-17 19:33:41 +03002280 err = thread__set_comm(pt->unknown_thread, "unknown", 0);
2281 if (err)
2282 goto err_delete_thread;
2283 if (thread__init_map_groups(pt->unknown_thread, pt->machine)) {
2284 err = -ENOMEM;
2285 goto err_delete_thread;
2286 }
2287
2288 pt->auxtrace.process_event = intel_pt_process_event;
2289 pt->auxtrace.process_auxtrace_event = intel_pt_process_auxtrace_event;
2290 pt->auxtrace.flush_events = intel_pt_flush;
2291 pt->auxtrace.free_events = intel_pt_free_events;
2292 pt->auxtrace.free = intel_pt_free;
2293 session->auxtrace = &pt->auxtrace;
2294
2295 if (dump_trace)
2296 return 0;
2297
2298 if (pt->have_sched_switch == 1) {
2299 pt->switch_evsel = intel_pt_find_sched_switch(session->evlist);
2300 if (!pt->switch_evsel) {
2301 pr_err("%s: missing sched_switch event\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03002302 err = -EINVAL;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002303 goto err_delete_thread;
2304 }
Adrian Hunter86c27862015-08-13 12:40:57 +03002305 } else if (pt->have_sched_switch == 2 &&
2306 !intel_pt_find_switch(session->evlist)) {
2307 pr_err("%s: missing context_switch attribute flag\n", __func__);
Adrian Hunter4d34e102016-09-23 17:38:43 +03002308 err = -EINVAL;
Adrian Hunter86c27862015-08-13 12:40:57 +03002309 goto err_delete_thread;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002310 }
2311
2312 if (session->itrace_synth_opts && session->itrace_synth_opts->set) {
2313 pt->synth_opts = *session->itrace_synth_opts;
2314 } else {
2315 itrace_synth_opts__set_default(&pt->synth_opts);
2316 if (use_browser != -1) {
2317 pt->synth_opts.branches = false;
2318 pt->synth_opts.callchain = true;
2319 }
Adrian Hunter50f736372016-06-23 16:40:57 +03002320 if (session->itrace_synth_opts)
2321 pt->synth_opts.thread_stack =
2322 session->itrace_synth_opts->thread_stack;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002323 }
2324
2325 if (pt->synth_opts.log)
2326 intel_pt_log_enable();
2327
2328 /* Maximum non-turbo ratio is TSC freq / 100 MHz */
2329 if (pt->tc.time_mult) {
2330 u64 tsc_freq = intel_pt_ns_to_ticks(pt, 1000000000);
2331
Adrian Hunterfa8025c2016-09-23 17:38:42 +03002332 if (!pt->max_non_turbo_ratio)
2333 pt->max_non_turbo_ratio =
2334 (tsc_freq + 50000000) / 100000000;
Adrian Hunter90e457f2015-07-17 19:33:41 +03002335 intel_pt_log("TSC frequency %"PRIu64"\n", tsc_freq);
2336 intel_pt_log("Maximum non-turbo ratio %u\n",
2337 pt->max_non_turbo_ratio);
2338 }
2339
2340 if (pt->synth_opts.calls)
2341 pt->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC |
2342 PERF_IP_FLAG_TRACE_END;
2343 if (pt->synth_opts.returns)
2344 pt->branches_filter |= PERF_IP_FLAG_RETURN |
2345 PERF_IP_FLAG_TRACE_BEGIN;
2346
2347 if (pt->synth_opts.callchain && !symbol_conf.use_callchain) {
2348 symbol_conf.use_callchain = true;
2349 if (callchain_register_param(&callchain_param) < 0) {
2350 symbol_conf.use_callchain = false;
2351 pt->synth_opts.callchain = false;
2352 }
2353 }
2354
2355 err = intel_pt_synth_events(pt, session);
2356 if (err)
2357 goto err_delete_thread;
2358
2359 err = auxtrace_queues__process_index(&pt->queues, session);
2360 if (err)
2361 goto err_delete_thread;
2362
2363 if (pt->queues.populated)
2364 pt->data_queued = true;
2365
2366 if (pt->timeless_decoding)
2367 pr_debug2("Intel PT decoding without timestamps\n");
2368
2369 return 0;
2370
2371err_delete_thread:
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -03002372 thread__zput(pt->unknown_thread);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002373err_free_queues:
2374 intel_pt_log_disable();
2375 auxtrace_queues__free(&pt->queues);
2376 session->auxtrace = NULL;
2377err_free:
Adrian Hunter2acee102016-09-23 17:38:48 +03002378 addr_filters__exit(&pt->filts);
Adrian Hunter2b9e32c2016-09-23 17:38:46 +03002379 zfree(&pt->filter);
Adrian Hunter90e457f2015-07-17 19:33:41 +03002380 free(pt);
2381 return err;
2382}