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