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