1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * intel-bts.c: Intel Processor Trace support 4 * Copyright (c) 2013-2015, Intel Corporation. 5 */ 6 7 #include <endian.h> 8 #include <errno.h> 9 #include <byteswap.h> 10 #include <inttypes.h> 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/bitops.h> 14 #include <linux/log2.h> 15 #include <linux/zalloc.h> 16 17 #include "color.h" 18 #include "evsel.h" 19 #include "evlist.h" 20 #include "machine.h" 21 #include "symbol.h" 22 #include "session.h" 23 #include "tool.h" 24 #include "thread.h" 25 #include "thread-stack.h" 26 #include "debug.h" 27 #include "tsc.h" 28 #include "auxtrace.h" 29 #include "intel-pt-decoder/intel-pt-insn-decoder.h" 30 #include "intel-bts.h" 31 #include "util/synthetic-events.h" 32 33 #define MAX_TIMESTAMP (~0ULL) 34 35 #define INTEL_BTS_ERR_NOINSN 5 36 #define INTEL_BTS_ERR_LOST 9 37 38 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 39 #define le64_to_cpu bswap_64 40 #else 41 #define le64_to_cpu 42 #endif 43 44 struct intel_bts { 45 struct auxtrace auxtrace; 46 struct auxtrace_queues queues; 47 struct auxtrace_heap heap; 48 u32 auxtrace_type; 49 struct perf_session *session; 50 struct machine *machine; 51 bool sampling_mode; 52 bool snapshot_mode; 53 bool data_queued; 54 u32 pmu_type; 55 struct perf_tsc_conversion tc; 56 bool cap_user_time_zero; 57 struct itrace_synth_opts synth_opts; 58 bool sample_branches; 59 u32 branches_filter; 60 u64 branches_sample_type; 61 u64 branches_id; 62 size_t branches_event_size; 63 unsigned long num_events; 64 }; 65 66 struct intel_bts_queue { 67 struct intel_bts *bts; 68 unsigned int queue_nr; 69 struct auxtrace_buffer *buffer; 70 bool on_heap; 71 bool done; 72 pid_t pid; 73 pid_t tid; 74 int cpu; 75 u64 time; 76 struct intel_pt_insn intel_pt_insn; 77 u32 sample_flags; 78 }; 79 80 struct branch { 81 u64 from; 82 u64 to; 83 u64 misc; 84 }; 85 86 static void intel_bts_dump(struct intel_bts *bts __maybe_unused, 87 unsigned char *buf, size_t len) 88 { 89 struct branch *branch; 90 size_t i, pos = 0, br_sz = sizeof(struct branch), sz; 91 const char *color = PERF_COLOR_BLUE; 92 93 color_fprintf(stdout, color, 94 ". ... Intel BTS data: size %zu bytes\n", 95 len); 96 97 while (len) { 98 if (len >= br_sz) 99 sz = br_sz; 100 else 101 sz = len; 102 printf("."); 103 color_fprintf(stdout, color, " %08zx: ", pos); 104 for (i = 0; i < sz; i++) 105 color_fprintf(stdout, color, " %02x", buf[i]); 106 for (; i < br_sz; i++) 107 color_fprintf(stdout, color, " "); 108 if (len >= br_sz) { 109 branch = (struct branch *)buf; 110 color_fprintf(stdout, color, " %"PRIx64" -> %"PRIx64" %s\n", 111 le64_to_cpu(branch->from), 112 le64_to_cpu(branch->to), 113 le64_to_cpu(branch->misc) & 0x10 ? 114 "pred" : "miss"); 115 } else { 116 color_fprintf(stdout, color, " Bad record!\n"); 117 } 118 pos += sz; 119 buf += sz; 120 len -= sz; 121 } 122 } 123 124 static void intel_bts_dump_event(struct intel_bts *bts, unsigned char *buf, 125 size_t len) 126 { 127 printf(".\n"); 128 intel_bts_dump(bts, buf, len); 129 } 130 131 static int intel_bts_lost(struct intel_bts *bts, struct perf_sample *sample) 132 { 133 union perf_event event; 134 int err; 135 136 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE, 137 INTEL_BTS_ERR_LOST, sample->cpu, sample->pid, 138 sample->tid, 0, "Lost trace data", sample->time); 139 140 err = perf_session__deliver_synth_event(bts->session, &event, NULL); 141 if (err) 142 pr_err("Intel BTS: failed to deliver error event, error %d\n", 143 err); 144 145 return err; 146 } 147 148 static struct intel_bts_queue *intel_bts_alloc_queue(struct intel_bts *bts, 149 unsigned int queue_nr) 150 { 151 struct intel_bts_queue *btsq; 152 153 btsq = zalloc(sizeof(struct intel_bts_queue)); 154 if (!btsq) 155 return NULL; 156 157 btsq->bts = bts; 158 btsq->queue_nr = queue_nr; 159 btsq->pid = -1; 160 btsq->tid = -1; 161 btsq->cpu = -1; 162 163 return btsq; 164 } 165 166 static int intel_bts_setup_queue(struct intel_bts *bts, 167 struct auxtrace_queue *queue, 168 unsigned int queue_nr) 169 { 170 struct intel_bts_queue *btsq = queue->priv; 171 172 if (list_empty(&queue->head)) 173 return 0; 174 175 if (!btsq) { 176 btsq = intel_bts_alloc_queue(bts, queue_nr); 177 if (!btsq) 178 return -ENOMEM; 179 queue->priv = btsq; 180 181 if (queue->cpu != -1) 182 btsq->cpu = queue->cpu; 183 btsq->tid = queue->tid; 184 } 185 186 if (bts->sampling_mode) 187 return 0; 188 189 if (!btsq->on_heap && !btsq->buffer) { 190 int ret; 191 192 btsq->buffer = auxtrace_buffer__next(queue, NULL); 193 if (!btsq->buffer) 194 return 0; 195 196 ret = auxtrace_heap__add(&bts->heap, queue_nr, 197 btsq->buffer->reference); 198 if (ret) 199 return ret; 200 btsq->on_heap = true; 201 } 202 203 return 0; 204 } 205 206 static int intel_bts_setup_queues(struct intel_bts *bts) 207 { 208 unsigned int i; 209 int ret; 210 211 for (i = 0; i < bts->queues.nr_queues; i++) { 212 ret = intel_bts_setup_queue(bts, &bts->queues.queue_array[i], 213 i); 214 if (ret) 215 return ret; 216 } 217 return 0; 218 } 219 220 static inline int intel_bts_update_queues(struct intel_bts *bts) 221 { 222 if (bts->queues.new_data) { 223 bts->queues.new_data = false; 224 return intel_bts_setup_queues(bts); 225 } 226 return 0; 227 } 228 229 static unsigned char *intel_bts_find_overlap(unsigned char *buf_a, size_t len_a, 230 unsigned char *buf_b, size_t len_b) 231 { 232 size_t offs, len; 233 234 if (len_a > len_b) 235 offs = len_a - len_b; 236 else 237 offs = 0; 238 239 for (; offs < len_a; offs += sizeof(struct branch)) { 240 len = len_a - offs; 241 if (!memcmp(buf_a + offs, buf_b, len)) 242 return buf_b + len; 243 } 244 245 return buf_b; 246 } 247 248 static int intel_bts_do_fix_overlap(struct auxtrace_queue *queue, 249 struct auxtrace_buffer *b) 250 { 251 struct auxtrace_buffer *a; 252 void *start; 253 254 if (b->list.prev == &queue->head) 255 return 0; 256 a = list_entry(b->list.prev, struct auxtrace_buffer, list); 257 start = intel_bts_find_overlap(a->data, a->size, b->data, b->size); 258 if (!start) 259 return -EINVAL; 260 b->use_size = b->data + b->size - start; 261 b->use_data = start; 262 return 0; 263 } 264 265 static inline u8 intel_bts_cpumode(struct intel_bts *bts, uint64_t ip) 266 { 267 return machine__kernel_ip(bts->machine, ip) ? 268 PERF_RECORD_MISC_KERNEL : 269 PERF_RECORD_MISC_USER; 270 } 271 272 static int intel_bts_synth_branch_sample(struct intel_bts_queue *btsq, 273 struct branch *branch) 274 { 275 int ret; 276 struct intel_bts *bts = btsq->bts; 277 union perf_event event; 278 struct perf_sample sample; 279 280 if (bts->synth_opts.initial_skip && 281 bts->num_events++ <= bts->synth_opts.initial_skip) 282 return 0; 283 284 perf_sample__init(&sample, /*all=*/true); 285 sample.ip = le64_to_cpu(branch->from); 286 sample.cpumode = intel_bts_cpumode(bts, sample.ip); 287 sample.pid = btsq->pid; 288 sample.tid = btsq->tid; 289 sample.addr = le64_to_cpu(branch->to); 290 sample.id = btsq->bts->branches_id; 291 sample.stream_id = btsq->bts->branches_id; 292 sample.period = 1; 293 sample.cpu = btsq->cpu; 294 sample.flags = btsq->sample_flags; 295 sample.insn_len = btsq->intel_pt_insn.length; 296 memcpy(sample.insn, btsq->intel_pt_insn.buf, INTEL_PT_INSN_BUF_SZ); 297 298 event.sample.header.type = PERF_RECORD_SAMPLE; 299 event.sample.header.misc = sample.cpumode; 300 event.sample.header.size = sizeof(struct perf_event_header); 301 302 if (bts->synth_opts.inject) { 303 event.sample.header.size = bts->branches_event_size; 304 ret = perf_event__synthesize_sample(&event, 305 bts->branches_sample_type, 306 0, &sample); 307 if (ret) 308 return ret; 309 } 310 311 ret = perf_session__deliver_synth_event(bts->session, &event, &sample); 312 if (ret) 313 pr_err("Intel BTS: failed to deliver branch event, error %d\n", 314 ret); 315 316 perf_sample__exit(&sample); 317 return ret; 318 } 319 320 static int intel_bts_get_next_insn(struct intel_bts_queue *btsq, u64 ip) 321 { 322 struct machine *machine = btsq->bts->machine; 323 struct thread *thread; 324 unsigned char buf[INTEL_PT_INSN_BUF_SZ]; 325 ssize_t len; 326 bool x86_64; 327 int err = -1; 328 329 thread = machine__find_thread(machine, -1, btsq->tid); 330 if (!thread) 331 return -1; 332 333 len = thread__memcpy(thread, machine, buf, ip, INTEL_PT_INSN_BUF_SZ, &x86_64); 334 if (len <= 0) 335 goto out_put; 336 337 if (intel_pt_get_insn(buf, len, x86_64, &btsq->intel_pt_insn)) 338 goto out_put; 339 340 err = 0; 341 out_put: 342 thread__put(thread); 343 return err; 344 } 345 346 static int intel_bts_synth_error(struct intel_bts *bts, int cpu, pid_t pid, 347 pid_t tid, u64 ip) 348 { 349 union perf_event event; 350 int err; 351 352 auxtrace_synth_error(&event.auxtrace_error, PERF_AUXTRACE_ERROR_ITRACE, 353 INTEL_BTS_ERR_NOINSN, cpu, pid, tid, ip, 354 "Failed to get instruction", 0); 355 356 err = perf_session__deliver_synth_event(bts->session, &event, NULL); 357 if (err) 358 pr_err("Intel BTS: failed to deliver error event, error %d\n", 359 err); 360 361 return err; 362 } 363 364 static int intel_bts_get_branch_type(struct intel_bts_queue *btsq, 365 struct branch *branch) 366 { 367 int err; 368 369 if (!branch->from) { 370 if (branch->to) 371 btsq->sample_flags = PERF_IP_FLAG_BRANCH | 372 PERF_IP_FLAG_TRACE_BEGIN; 373 else 374 btsq->sample_flags = 0; 375 btsq->intel_pt_insn.length = 0; 376 } else if (!branch->to) { 377 btsq->sample_flags = PERF_IP_FLAG_BRANCH | 378 PERF_IP_FLAG_TRACE_END; 379 btsq->intel_pt_insn.length = 0; 380 } else { 381 err = intel_bts_get_next_insn(btsq, branch->from); 382 if (err) { 383 btsq->sample_flags = 0; 384 btsq->intel_pt_insn.length = 0; 385 if (!btsq->bts->synth_opts.errors) 386 return 0; 387 err = intel_bts_synth_error(btsq->bts, btsq->cpu, 388 btsq->pid, btsq->tid, 389 branch->from); 390 return err; 391 } 392 btsq->sample_flags = intel_pt_insn_type(btsq->intel_pt_insn.op); 393 /* Check for an async branch into the kernel */ 394 if (!machine__kernel_ip(btsq->bts->machine, branch->from) && 395 machine__kernel_ip(btsq->bts->machine, branch->to) && 396 btsq->sample_flags != (PERF_IP_FLAG_BRANCH | 397 PERF_IP_FLAG_CALL | 398 PERF_IP_FLAG_SYSCALLRET)) 399 btsq->sample_flags = PERF_IP_FLAG_BRANCH | 400 PERF_IP_FLAG_CALL | 401 PERF_IP_FLAG_ASYNC | 402 PERF_IP_FLAG_INTERRUPT; 403 } 404 405 return 0; 406 } 407 408 static int intel_bts_process_buffer(struct intel_bts_queue *btsq, 409 struct auxtrace_buffer *buffer, 410 struct thread *thread) 411 { 412 struct branch *branch; 413 size_t sz, bsz = sizeof(struct branch); 414 u32 filter = btsq->bts->branches_filter; 415 int err = 0; 416 417 if (buffer->use_data) { 418 sz = buffer->use_size; 419 branch = buffer->use_data; 420 } else { 421 sz = buffer->size; 422 branch = buffer->data; 423 } 424 425 if (!btsq->bts->sample_branches) 426 return 0; 427 428 for (; sz > bsz; branch += 1, sz -= bsz) { 429 if (!branch->from && !branch->to) 430 continue; 431 intel_bts_get_branch_type(btsq, branch); 432 if (btsq->bts->synth_opts.thread_stack) 433 thread_stack__event(thread, btsq->cpu, btsq->sample_flags, 434 le64_to_cpu(branch->from), 435 le64_to_cpu(branch->to), 436 btsq->intel_pt_insn.length, 437 buffer->buffer_nr + 1, true, 0, 0); 438 if (filter && !(filter & btsq->sample_flags)) 439 continue; 440 err = intel_bts_synth_branch_sample(btsq, branch); 441 if (err) 442 break; 443 } 444 return err; 445 } 446 447 static int intel_bts_process_queue(struct intel_bts_queue *btsq, u64 *timestamp) 448 { 449 struct auxtrace_buffer *buffer = btsq->buffer, *old_buffer = buffer; 450 struct auxtrace_queue *queue; 451 struct thread *thread; 452 int err; 453 454 if (btsq->done) 455 return 1; 456 457 if (btsq->pid == -1) { 458 thread = machine__find_thread(btsq->bts->machine, -1, 459 btsq->tid); 460 if (thread) 461 btsq->pid = thread__pid(thread); 462 } else { 463 thread = machine__findnew_thread(btsq->bts->machine, btsq->pid, 464 btsq->tid); 465 } 466 467 queue = &btsq->bts->queues.queue_array[btsq->queue_nr]; 468 469 if (!buffer) 470 buffer = auxtrace_buffer__next(queue, NULL); 471 472 if (!buffer) { 473 if (!btsq->bts->sampling_mode) 474 btsq->done = 1; 475 err = 1; 476 goto out_put; 477 } 478 479 /* Currently there is no support for split buffers */ 480 if (buffer->consecutive) { 481 err = -EINVAL; 482 goto out_put; 483 } 484 485 if (!buffer->data) { 486 int fd = perf_data__fd(btsq->bts->session->data); 487 488 buffer->data = auxtrace_buffer__get_data(buffer, fd); 489 if (!buffer->data) { 490 err = -ENOMEM; 491 goto out_put; 492 } 493 } 494 495 if (btsq->bts->snapshot_mode && !buffer->consecutive && 496 intel_bts_do_fix_overlap(queue, buffer)) { 497 err = -ENOMEM; 498 goto out_put; 499 } 500 501 if (!btsq->bts->synth_opts.callchain && 502 !btsq->bts->synth_opts.thread_stack && thread && 503 (!old_buffer || btsq->bts->sampling_mode || 504 (btsq->bts->snapshot_mode && !buffer->consecutive))) 505 thread_stack__set_trace_nr(thread, btsq->cpu, buffer->buffer_nr + 1); 506 507 err = intel_bts_process_buffer(btsq, buffer, thread); 508 509 auxtrace_buffer__drop_data(buffer); 510 511 btsq->buffer = auxtrace_buffer__next(queue, buffer); 512 if (btsq->buffer) { 513 if (timestamp) 514 *timestamp = btsq->buffer->reference; 515 } else { 516 if (!btsq->bts->sampling_mode) 517 btsq->done = 1; 518 } 519 out_put: 520 thread__put(thread); 521 return err; 522 } 523 524 static int intel_bts_flush_queue(struct intel_bts_queue *btsq) 525 { 526 u64 ts = 0; 527 int ret; 528 529 while (1) { 530 ret = intel_bts_process_queue(btsq, &ts); 531 if (ret < 0) 532 return ret; 533 if (ret) 534 break; 535 } 536 return 0; 537 } 538 539 static int intel_bts_process_tid_exit(struct intel_bts *bts, pid_t tid) 540 { 541 struct auxtrace_queues *queues = &bts->queues; 542 unsigned int i; 543 544 for (i = 0; i < queues->nr_queues; i++) { 545 struct auxtrace_queue *queue = &bts->queues.queue_array[i]; 546 struct intel_bts_queue *btsq = queue->priv; 547 548 if (btsq && btsq->tid == tid) 549 return intel_bts_flush_queue(btsq); 550 } 551 return 0; 552 } 553 554 static int intel_bts_process_queues(struct intel_bts *bts, u64 timestamp) 555 { 556 while (1) { 557 unsigned int queue_nr; 558 struct auxtrace_queue *queue; 559 struct intel_bts_queue *btsq; 560 u64 ts = 0; 561 int ret; 562 563 if (!bts->heap.heap_cnt) 564 return 0; 565 566 if (bts->heap.heap_array[0].ordinal > timestamp) 567 return 0; 568 569 queue_nr = bts->heap.heap_array[0].queue_nr; 570 queue = &bts->queues.queue_array[queue_nr]; 571 btsq = queue->priv; 572 573 auxtrace_heap__pop(&bts->heap); 574 575 ret = intel_bts_process_queue(btsq, &ts); 576 if (ret < 0) { 577 auxtrace_heap__add(&bts->heap, queue_nr, ts); 578 return ret; 579 } 580 581 if (!ret) { 582 ret = auxtrace_heap__add(&bts->heap, queue_nr, ts); 583 if (ret < 0) 584 return ret; 585 } else { 586 btsq->on_heap = false; 587 } 588 } 589 590 return 0; 591 } 592 593 static int intel_bts_process_event(struct perf_session *session, 594 union perf_event *event, 595 struct perf_sample *sample, 596 const struct perf_tool *tool) 597 { 598 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 599 auxtrace); 600 u64 timestamp; 601 int err; 602 603 if (dump_trace) 604 return 0; 605 606 if (!tool->ordered_events) { 607 pr_err("Intel BTS requires ordered events\n"); 608 return -EINVAL; 609 } 610 611 if (sample->time && sample->time != (u64)-1) 612 timestamp = perf_time_to_tsc(sample->time, &bts->tc); 613 else 614 timestamp = 0; 615 616 err = intel_bts_update_queues(bts); 617 if (err) 618 return err; 619 620 err = intel_bts_process_queues(bts, timestamp); 621 if (err) 622 return err; 623 if (event->header.type == PERF_RECORD_EXIT) { 624 err = intel_bts_process_tid_exit(bts, event->fork.tid); 625 if (err) 626 return err; 627 } 628 629 if (event->header.type == PERF_RECORD_AUX && 630 (event->aux.flags & PERF_AUX_FLAG_TRUNCATED) && 631 bts->synth_opts.errors) 632 err = intel_bts_lost(bts, sample); 633 634 return err; 635 } 636 637 static int intel_bts_process_auxtrace_event(struct perf_session *session, 638 union perf_event *event, 639 const struct perf_tool *tool __maybe_unused) 640 { 641 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 642 auxtrace); 643 644 if (bts->sampling_mode) 645 return 0; 646 647 if (!bts->data_queued) { 648 struct auxtrace_buffer *buffer; 649 off_t data_offset; 650 int fd = perf_data__fd(session->data); 651 int err; 652 653 if (perf_data__is_pipe(session->data)) { 654 data_offset = 0; 655 } else { 656 data_offset = lseek(fd, 0, SEEK_CUR); 657 if (data_offset == -1) 658 return -errno; 659 } 660 661 err = auxtrace_queues__add_event(&bts->queues, session, event, 662 data_offset, &buffer); 663 if (err) 664 return err; 665 666 /* Dump here now we have copied a piped trace out of the pipe */ 667 if (dump_trace) { 668 if (auxtrace_buffer__get_data(buffer, fd)) { 669 intel_bts_dump_event(bts, buffer->data, 670 buffer->size); 671 auxtrace_buffer__put_data(buffer); 672 } 673 } 674 } 675 676 return 0; 677 } 678 679 static int intel_bts_flush(struct perf_session *session, 680 const struct perf_tool *tool __maybe_unused) 681 { 682 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 683 auxtrace); 684 int ret; 685 686 if (dump_trace || bts->sampling_mode) 687 return 0; 688 689 if (!tool->ordered_events) 690 return -EINVAL; 691 692 ret = intel_bts_update_queues(bts); 693 if (ret < 0) 694 return ret; 695 696 return intel_bts_process_queues(bts, MAX_TIMESTAMP); 697 } 698 699 static void intel_bts_free_queue(void *priv) 700 { 701 struct intel_bts_queue *btsq = priv; 702 703 if (!btsq) 704 return; 705 free(btsq); 706 } 707 708 static void intel_bts_free_events(struct perf_session *session) 709 { 710 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 711 auxtrace); 712 struct auxtrace_queues *queues = &bts->queues; 713 unsigned int i; 714 715 for (i = 0; i < queues->nr_queues; i++) { 716 intel_bts_free_queue(queues->queue_array[i].priv); 717 queues->queue_array[i].priv = NULL; 718 } 719 auxtrace_queues__free(queues); 720 } 721 722 static void intel_bts_free(struct perf_session *session) 723 { 724 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 725 auxtrace); 726 727 auxtrace_heap__free(&bts->heap); 728 intel_bts_free_events(session); 729 session->auxtrace = NULL; 730 free(bts); 731 } 732 733 static bool intel_bts_evsel_is_auxtrace(struct perf_session *session, 734 struct evsel *evsel) 735 { 736 struct intel_bts *bts = container_of(session->auxtrace, struct intel_bts, 737 auxtrace); 738 739 return evsel->core.attr.type == bts->pmu_type; 740 } 741 742 static int intel_bts_synth_events(struct intel_bts *bts, 743 struct perf_session *session) 744 { 745 struct evlist *evlist = session->evlist; 746 struct evsel *evsel; 747 struct perf_event_attr attr; 748 bool found = false; 749 u64 id; 750 int err; 751 752 evlist__for_each_entry(evlist, evsel) { 753 if (evsel->core.attr.type == bts->pmu_type && evsel->core.ids) { 754 found = true; 755 break; 756 } 757 } 758 759 if (!found) { 760 pr_debug("There are no selected events with Intel BTS data\n"); 761 return 0; 762 } 763 764 memset(&attr, 0, sizeof(struct perf_event_attr)); 765 attr.size = sizeof(struct perf_event_attr); 766 attr.type = PERF_TYPE_HARDWARE; 767 attr.sample_type = evsel->core.attr.sample_type & PERF_SAMPLE_MASK; 768 attr.sample_type |= PERF_SAMPLE_IP | PERF_SAMPLE_TID | 769 PERF_SAMPLE_PERIOD; 770 attr.sample_type &= ~(u64)PERF_SAMPLE_TIME; 771 attr.sample_type &= ~(u64)PERF_SAMPLE_CPU; 772 attr.exclude_user = evsel->core.attr.exclude_user; 773 attr.exclude_kernel = evsel->core.attr.exclude_kernel; 774 attr.exclude_hv = evsel->core.attr.exclude_hv; 775 attr.exclude_host = evsel->core.attr.exclude_host; 776 attr.exclude_guest = evsel->core.attr.exclude_guest; 777 attr.sample_id_all = evsel->core.attr.sample_id_all; 778 attr.read_format = evsel->core.attr.read_format; 779 780 id = evsel->core.id[0] + 1000000000; 781 if (!id) 782 id = 1; 783 784 if (bts->synth_opts.branches) { 785 attr.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS; 786 attr.sample_period = 1; 787 attr.sample_type |= PERF_SAMPLE_ADDR; 788 pr_debug("Synthesizing 'branches' event with id %" PRIu64 " sample type %#" PRIx64 "\n", 789 id, (u64)attr.sample_type); 790 err = perf_session__deliver_synth_attr_event(session, &attr, id); 791 if (err) { 792 pr_err("%s: failed to synthesize 'branches' event type\n", 793 __func__); 794 return err; 795 } 796 bts->sample_branches = true; 797 bts->branches_sample_type = attr.sample_type; 798 bts->branches_id = id; 799 /* 800 * We only use sample types from PERF_SAMPLE_MASK so we can use 801 * __evsel__sample_size() here. 802 */ 803 bts->branches_event_size = sizeof(struct perf_record_sample) + 804 __evsel__sample_size(attr.sample_type); 805 } 806 807 return 0; 808 } 809 810 static const char * const intel_bts_info_fmts[] = { 811 [INTEL_BTS_PMU_TYPE] = " PMU Type %"PRId64"\n", 812 [INTEL_BTS_TIME_SHIFT] = " Time Shift %"PRIu64"\n", 813 [INTEL_BTS_TIME_MULT] = " Time Multiplier %"PRIu64"\n", 814 [INTEL_BTS_TIME_ZERO] = " Time Zero %"PRIu64"\n", 815 [INTEL_BTS_CAP_USER_TIME_ZERO] = " Cap Time Zero %"PRId64"\n", 816 [INTEL_BTS_SNAPSHOT_MODE] = " Snapshot mode %"PRId64"\n", 817 }; 818 819 static void intel_bts_print_info(__u64 *arr, int start, int finish) 820 { 821 int i; 822 823 if (!dump_trace) 824 return; 825 826 for (i = start; i <= finish; i++) 827 fprintf(stdout, intel_bts_info_fmts[i], arr[i]); 828 } 829 830 int intel_bts_process_auxtrace_info(union perf_event *event, 831 struct perf_session *session) 832 { 833 struct perf_record_auxtrace_info *auxtrace_info = &event->auxtrace_info; 834 size_t min_sz = sizeof(u64) * INTEL_BTS_SNAPSHOT_MODE; 835 struct intel_bts *bts; 836 int err; 837 838 if (auxtrace_info->header.size < sizeof(struct perf_record_auxtrace_info) + 839 min_sz) 840 return -EINVAL; 841 842 bts = zalloc(sizeof(struct intel_bts)); 843 if (!bts) 844 return -ENOMEM; 845 846 err = auxtrace_queues__init(&bts->queues); 847 if (err) 848 goto err_free; 849 850 bts->session = session; 851 bts->machine = &session->machines.host; /* No kvm support */ 852 bts->auxtrace_type = auxtrace_info->type; 853 bts->pmu_type = auxtrace_info->priv[INTEL_BTS_PMU_TYPE]; 854 bts->tc.time_shift = auxtrace_info->priv[INTEL_BTS_TIME_SHIFT]; 855 bts->tc.time_mult = auxtrace_info->priv[INTEL_BTS_TIME_MULT]; 856 bts->tc.time_zero = auxtrace_info->priv[INTEL_BTS_TIME_ZERO]; 857 bts->cap_user_time_zero = 858 auxtrace_info->priv[INTEL_BTS_CAP_USER_TIME_ZERO]; 859 bts->snapshot_mode = auxtrace_info->priv[INTEL_BTS_SNAPSHOT_MODE]; 860 861 bts->sampling_mode = false; 862 863 bts->auxtrace.process_event = intel_bts_process_event; 864 bts->auxtrace.process_auxtrace_event = intel_bts_process_auxtrace_event; 865 bts->auxtrace.flush_events = intel_bts_flush; 866 bts->auxtrace.free_events = intel_bts_free_events; 867 bts->auxtrace.free = intel_bts_free; 868 bts->auxtrace.evsel_is_auxtrace = intel_bts_evsel_is_auxtrace; 869 session->auxtrace = &bts->auxtrace; 870 871 intel_bts_print_info(&auxtrace_info->priv[0], INTEL_BTS_PMU_TYPE, 872 INTEL_BTS_SNAPSHOT_MODE); 873 874 if (dump_trace) 875 return 0; 876 877 if (session->itrace_synth_opts->set) { 878 bts->synth_opts = *session->itrace_synth_opts; 879 } else { 880 itrace_synth_opts__set_default(&bts->synth_opts, 881 session->itrace_synth_opts->default_no_sample); 882 bts->synth_opts.thread_stack = 883 session->itrace_synth_opts->thread_stack; 884 } 885 886 if (bts->synth_opts.calls) 887 bts->branches_filter |= PERF_IP_FLAG_CALL | PERF_IP_FLAG_ASYNC | 888 PERF_IP_FLAG_TRACE_END; 889 if (bts->synth_opts.returns) 890 bts->branches_filter |= PERF_IP_FLAG_RETURN | 891 PERF_IP_FLAG_TRACE_BEGIN; 892 893 err = intel_bts_synth_events(bts, session); 894 if (err) 895 goto err_free_queues; 896 897 err = auxtrace_queues__process_index(&bts->queues, session); 898 if (err) 899 goto err_free_queues; 900 901 if (bts->queues.populated) 902 bts->data_queued = true; 903 904 return 0; 905 906 err_free_queues: 907 auxtrace_queues__free(&bts->queues); 908 session->auxtrace = NULL; 909 err_free: 910 free(bts); 911 return err; 912 } 913