1 /* 2 * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com> 3 * 4 * Parts came from builtin-annotate.c, see those files for further 5 * copyright notes. 6 * 7 * Released under the GPL v2. (and only v2, not any later version) 8 */ 9 10 #include <errno.h> 11 #include <inttypes.h> 12 #include "util.h" 13 #include "ui/ui.h" 14 #include "sort.h" 15 #include "build-id.h" 16 #include "color.h" 17 #include "config.h" 18 #include "cache.h" 19 #include "symbol.h" 20 #include "units.h" 21 #include "debug.h" 22 #include "annotate.h" 23 #include "evsel.h" 24 #include "evlist.h" 25 #include "block-range.h" 26 #include "string2.h" 27 #include "arch/common.h" 28 #include <regex.h> 29 #include <pthread.h> 30 #include <linux/bitops.h> 31 #include <linux/kernel.h> 32 33 /* FIXME: For the HE_COLORSET */ 34 #include "ui/browser.h" 35 36 /* 37 * FIXME: Using the same values as slang.h, 38 * but that header may not be available everywhere 39 */ 40 #define LARROW_CHAR ((unsigned char)',') 41 #define RARROW_CHAR ((unsigned char)'+') 42 #define DARROW_CHAR ((unsigned char)'.') 43 #define UARROW_CHAR ((unsigned char)'-') 44 45 #include "sane_ctype.h" 46 47 struct annotation_options annotation__default_options = { 48 .use_offset = true, 49 .jump_arrows = true, 50 .annotate_src = true, 51 .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS, 52 .percent_type = PERCENT_PERIOD_LOCAL, 53 }; 54 55 static regex_t file_lineno; 56 57 static struct ins_ops *ins__find(struct arch *arch, const char *name); 58 static void ins__sort(struct arch *arch); 59 static int disasm_line__parse(char *line, const char **namep, char **rawp); 60 61 struct arch { 62 const char *name; 63 struct ins *instructions; 64 size_t nr_instructions; 65 size_t nr_instructions_allocated; 66 struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name); 67 bool sorted_instructions; 68 bool initialized; 69 void *priv; 70 unsigned int model; 71 unsigned int family; 72 int (*init)(struct arch *arch, char *cpuid); 73 bool (*ins_is_fused)(struct arch *arch, const char *ins1, 74 const char *ins2); 75 struct { 76 char comment_char; 77 char skip_functions_char; 78 } objdump; 79 }; 80 81 static struct ins_ops call_ops; 82 static struct ins_ops dec_ops; 83 static struct ins_ops jump_ops; 84 static struct ins_ops mov_ops; 85 static struct ins_ops nop_ops; 86 static struct ins_ops lock_ops; 87 static struct ins_ops ret_ops; 88 89 static int arch__grow_instructions(struct arch *arch) 90 { 91 struct ins *new_instructions; 92 size_t new_nr_allocated; 93 94 if (arch->nr_instructions_allocated == 0 && arch->instructions) 95 goto grow_from_non_allocated_table; 96 97 new_nr_allocated = arch->nr_instructions_allocated + 128; 98 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); 99 if (new_instructions == NULL) 100 return -1; 101 102 out_update_instructions: 103 arch->instructions = new_instructions; 104 arch->nr_instructions_allocated = new_nr_allocated; 105 return 0; 106 107 grow_from_non_allocated_table: 108 new_nr_allocated = arch->nr_instructions + 128; 109 new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); 110 if (new_instructions == NULL) 111 return -1; 112 113 memcpy(new_instructions, arch->instructions, arch->nr_instructions); 114 goto out_update_instructions; 115 } 116 117 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) 118 { 119 struct ins *ins; 120 121 if (arch->nr_instructions == arch->nr_instructions_allocated && 122 arch__grow_instructions(arch)) 123 return -1; 124 125 ins = &arch->instructions[arch->nr_instructions]; 126 ins->name = strdup(name); 127 if (!ins->name) 128 return -1; 129 130 ins->ops = ops; 131 arch->nr_instructions++; 132 133 ins__sort(arch); 134 return 0; 135 } 136 137 #include "arch/arm/annotate/instructions.c" 138 #include "arch/arm64/annotate/instructions.c" 139 #include "arch/x86/annotate/instructions.c" 140 #include "arch/powerpc/annotate/instructions.c" 141 #include "arch/s390/annotate/instructions.c" 142 143 static struct arch architectures[] = { 144 { 145 .name = "arm", 146 .init = arm__annotate_init, 147 }, 148 { 149 .name = "arm64", 150 .init = arm64__annotate_init, 151 }, 152 { 153 .name = "x86", 154 .init = x86__annotate_init, 155 .instructions = x86__instructions, 156 .nr_instructions = ARRAY_SIZE(x86__instructions), 157 .ins_is_fused = x86__ins_is_fused, 158 .objdump = { 159 .comment_char = '#', 160 }, 161 }, 162 { 163 .name = "powerpc", 164 .init = powerpc__annotate_init, 165 }, 166 { 167 .name = "s390", 168 .init = s390__annotate_init, 169 .objdump = { 170 .comment_char = '#', 171 }, 172 }, 173 }; 174 175 static void ins__delete(struct ins_operands *ops) 176 { 177 if (ops == NULL) 178 return; 179 zfree(&ops->source.raw); 180 zfree(&ops->source.name); 181 zfree(&ops->target.raw); 182 zfree(&ops->target.name); 183 } 184 185 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, 186 struct ins_operands *ops) 187 { 188 return scnprintf(bf, size, "%-6s %s", ins->name, ops->raw); 189 } 190 191 int ins__scnprintf(struct ins *ins, char *bf, size_t size, 192 struct ins_operands *ops) 193 { 194 if (ins->ops->scnprintf) 195 return ins->ops->scnprintf(ins, bf, size, ops); 196 197 return ins__raw_scnprintf(ins, bf, size, ops); 198 } 199 200 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 201 { 202 if (!arch || !arch->ins_is_fused) 203 return false; 204 205 return arch->ins_is_fused(arch, ins1, ins2); 206 } 207 208 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 209 { 210 char *endptr, *tok, *name; 211 struct map *map = ms->map; 212 struct addr_map_symbol target = { 213 .map = map, 214 }; 215 216 ops->target.addr = strtoull(ops->raw, &endptr, 16); 217 218 name = strchr(endptr, '<'); 219 if (name == NULL) 220 goto indirect_call; 221 222 name++; 223 224 if (arch->objdump.skip_functions_char && 225 strchr(name, arch->objdump.skip_functions_char)) 226 return -1; 227 228 tok = strchr(name, '>'); 229 if (tok == NULL) 230 return -1; 231 232 *tok = '\0'; 233 ops->target.name = strdup(name); 234 *tok = '>'; 235 236 if (ops->target.name == NULL) 237 return -1; 238 find_target: 239 target.addr = map__objdump_2mem(map, ops->target.addr); 240 241 if (map_groups__find_ams(&target) == 0 && 242 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) 243 ops->target.sym = target.sym; 244 245 return 0; 246 247 indirect_call: 248 tok = strchr(endptr, '*'); 249 if (tok != NULL) { 250 endptr++; 251 252 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx). 253 * Do not parse such instruction. */ 254 if (strstr(endptr, "(%r") == NULL) 255 ops->target.addr = strtoull(endptr, NULL, 16); 256 } 257 goto find_target; 258 } 259 260 static int call__scnprintf(struct ins *ins, char *bf, size_t size, 261 struct ins_operands *ops) 262 { 263 if (ops->target.sym) 264 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name); 265 266 if (ops->target.addr == 0) 267 return ins__raw_scnprintf(ins, bf, size, ops); 268 269 if (ops->target.name) 270 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.name); 271 272 return scnprintf(bf, size, "%-6s *%" PRIx64, ins->name, ops->target.addr); 273 } 274 275 static struct ins_ops call_ops = { 276 .parse = call__parse, 277 .scnprintf = call__scnprintf, 278 }; 279 280 bool ins__is_call(const struct ins *ins) 281 { 282 return ins->ops == &call_ops || ins->ops == &s390_call_ops; 283 } 284 285 static int jump__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms) 286 { 287 struct map *map = ms->map; 288 struct symbol *sym = ms->sym; 289 struct addr_map_symbol target = { 290 .map = map, 291 }; 292 const char *c = strchr(ops->raw, ','); 293 u64 start, end; 294 /* 295 * Examples of lines to parse for the _cpp_lex_token@@Base 296 * function: 297 * 298 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> 299 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> 300 * 301 * The first is a jump to an offset inside the same function, 302 * the second is to another function, i.e. that 0xa72 is an 303 * offset in the cpp_named_operator2name@@base function. 304 */ 305 /* 306 * skip over possible up to 2 operands to get to address, e.g.: 307 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> 308 */ 309 if (c++ != NULL) { 310 ops->target.addr = strtoull(c, NULL, 16); 311 if (!ops->target.addr) { 312 c = strchr(c, ','); 313 if (c++ != NULL) 314 ops->target.addr = strtoull(c, NULL, 16); 315 } 316 } else { 317 ops->target.addr = strtoull(ops->raw, NULL, 16); 318 } 319 320 target.addr = map__objdump_2mem(map, ops->target.addr); 321 start = map->unmap_ip(map, sym->start), 322 end = map->unmap_ip(map, sym->end); 323 324 ops->target.outside = target.addr < start || target.addr > end; 325 326 /* 327 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): 328 329 cpp_named_operator2name@@Base+0xa72 330 331 * Point to a place that is after the cpp_named_operator2name 332 * boundaries, i.e. in the ELF symbol table for cc1 333 * cpp_named_operator2name is marked as being 32-bytes long, but it in 334 * fact is much larger than that, so we seem to need a symbols__find() 335 * routine that looks for >= current->start and < next_symbol->start, 336 * possibly just for C++ objects? 337 * 338 * For now lets just make some progress by marking jumps to outside the 339 * current function as call like. 340 * 341 * Actual navigation will come next, with further understanding of how 342 * the symbol searching and disassembly should be done. 343 */ 344 if (map_groups__find_ams(&target) == 0 && 345 map__rip_2objdump(target.map, map->map_ip(target.map, target.addr)) == ops->target.addr) 346 ops->target.sym = target.sym; 347 348 if (!ops->target.outside) { 349 ops->target.offset = target.addr - start; 350 ops->target.offset_avail = true; 351 } else { 352 ops->target.offset_avail = false; 353 } 354 355 return 0; 356 } 357 358 static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 359 struct ins_operands *ops) 360 { 361 const char *c; 362 363 if (!ops->target.addr || ops->target.offset < 0) 364 return ins__raw_scnprintf(ins, bf, size, ops); 365 366 if (ops->target.outside && ops->target.sym != NULL) 367 return scnprintf(bf, size, "%-6s %s", ins->name, ops->target.sym->name); 368 369 c = strchr(ops->raw, ','); 370 if (c != NULL) { 371 const char *c2 = strchr(c + 1, ','); 372 373 /* check for 3-op insn */ 374 if (c2 != NULL) 375 c = c2; 376 c++; 377 378 /* mirror arch objdump's space-after-comma style */ 379 if (*c == ' ') 380 c++; 381 } 382 383 return scnprintf(bf, size, "%-6s %.*s%" PRIx64, 384 ins->name, c ? c - ops->raw : 0, ops->raw, 385 ops->target.offset); 386 } 387 388 static struct ins_ops jump_ops = { 389 .parse = jump__parse, 390 .scnprintf = jump__scnprintf, 391 }; 392 393 bool ins__is_jump(const struct ins *ins) 394 { 395 return ins->ops == &jump_ops; 396 } 397 398 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) 399 { 400 char *endptr, *name, *t; 401 402 if (strstr(raw, "(%rip)") == NULL) 403 return 0; 404 405 *addrp = strtoull(comment, &endptr, 16); 406 if (endptr == comment) 407 return 0; 408 name = strchr(endptr, '<'); 409 if (name == NULL) 410 return -1; 411 412 name++; 413 414 t = strchr(name, '>'); 415 if (t == NULL) 416 return 0; 417 418 *t = '\0'; 419 *namep = strdup(name); 420 *t = '>'; 421 422 return 0; 423 } 424 425 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms) 426 { 427 ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); 428 if (ops->locked.ops == NULL) 429 return 0; 430 431 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) 432 goto out_free_ops; 433 434 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name); 435 436 if (ops->locked.ins.ops == NULL) 437 goto out_free_ops; 438 439 if (ops->locked.ins.ops->parse && 440 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0) 441 goto out_free_ops; 442 443 return 0; 444 445 out_free_ops: 446 zfree(&ops->locked.ops); 447 return 0; 448 } 449 450 static int lock__scnprintf(struct ins *ins, char *bf, size_t size, 451 struct ins_operands *ops) 452 { 453 int printed; 454 455 if (ops->locked.ins.ops == NULL) 456 return ins__raw_scnprintf(ins, bf, size, ops); 457 458 printed = scnprintf(bf, size, "%-6s ", ins->name); 459 return printed + ins__scnprintf(&ops->locked.ins, bf + printed, 460 size - printed, ops->locked.ops); 461 } 462 463 static void lock__delete(struct ins_operands *ops) 464 { 465 struct ins *ins = &ops->locked.ins; 466 467 if (ins->ops && ins->ops->free) 468 ins->ops->free(ops->locked.ops); 469 else 470 ins__delete(ops->locked.ops); 471 472 zfree(&ops->locked.ops); 473 zfree(&ops->target.raw); 474 zfree(&ops->target.name); 475 } 476 477 static struct ins_ops lock_ops = { 478 .free = lock__delete, 479 .parse = lock__parse, 480 .scnprintf = lock__scnprintf, 481 }; 482 483 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 484 { 485 char *s = strchr(ops->raw, ','), *target, *comment, prev; 486 487 if (s == NULL) 488 return -1; 489 490 *s = '\0'; 491 ops->source.raw = strdup(ops->raw); 492 *s = ','; 493 494 if (ops->source.raw == NULL) 495 return -1; 496 497 target = ++s; 498 comment = strchr(s, arch->objdump.comment_char); 499 500 if (comment != NULL) 501 s = comment - 1; 502 else 503 s = strchr(s, '\0') - 1; 504 505 while (s > target && isspace(s[0])) 506 --s; 507 s++; 508 prev = *s; 509 *s = '\0'; 510 511 ops->target.raw = strdup(target); 512 *s = prev; 513 514 if (ops->target.raw == NULL) 515 goto out_free_source; 516 517 if (comment == NULL) 518 return 0; 519 520 comment = ltrim(comment); 521 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); 522 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 523 524 return 0; 525 526 out_free_source: 527 zfree(&ops->source.raw); 528 return -1; 529 } 530 531 static int mov__scnprintf(struct ins *ins, char *bf, size_t size, 532 struct ins_operands *ops) 533 { 534 return scnprintf(bf, size, "%-6s %s,%s", ins->name, 535 ops->source.name ?: ops->source.raw, 536 ops->target.name ?: ops->target.raw); 537 } 538 539 static struct ins_ops mov_ops = { 540 .parse = mov__parse, 541 .scnprintf = mov__scnprintf, 542 }; 543 544 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused) 545 { 546 char *target, *comment, *s, prev; 547 548 target = s = ops->raw; 549 550 while (s[0] != '\0' && !isspace(s[0])) 551 ++s; 552 prev = *s; 553 *s = '\0'; 554 555 ops->target.raw = strdup(target); 556 *s = prev; 557 558 if (ops->target.raw == NULL) 559 return -1; 560 561 comment = strchr(s, arch->objdump.comment_char); 562 if (comment == NULL) 563 return 0; 564 565 comment = ltrim(comment); 566 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 567 568 return 0; 569 } 570 571 static int dec__scnprintf(struct ins *ins, char *bf, size_t size, 572 struct ins_operands *ops) 573 { 574 return scnprintf(bf, size, "%-6s %s", ins->name, 575 ops->target.name ?: ops->target.raw); 576 } 577 578 static struct ins_ops dec_ops = { 579 .parse = dec__parse, 580 .scnprintf = dec__scnprintf, 581 }; 582 583 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, 584 struct ins_operands *ops __maybe_unused) 585 { 586 return scnprintf(bf, size, "%-6s", "nop"); 587 } 588 589 static struct ins_ops nop_ops = { 590 .scnprintf = nop__scnprintf, 591 }; 592 593 static struct ins_ops ret_ops = { 594 .scnprintf = ins__raw_scnprintf, 595 }; 596 597 bool ins__is_ret(const struct ins *ins) 598 { 599 return ins->ops == &ret_ops; 600 } 601 602 bool ins__is_lock(const struct ins *ins) 603 { 604 return ins->ops == &lock_ops; 605 } 606 607 static int ins__key_cmp(const void *name, const void *insp) 608 { 609 const struct ins *ins = insp; 610 611 return strcmp(name, ins->name); 612 } 613 614 static int ins__cmp(const void *a, const void *b) 615 { 616 const struct ins *ia = a; 617 const struct ins *ib = b; 618 619 return strcmp(ia->name, ib->name); 620 } 621 622 static void ins__sort(struct arch *arch) 623 { 624 const int nmemb = arch->nr_instructions; 625 626 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); 627 } 628 629 static struct ins_ops *__ins__find(struct arch *arch, const char *name) 630 { 631 struct ins *ins; 632 const int nmemb = arch->nr_instructions; 633 634 if (!arch->sorted_instructions) { 635 ins__sort(arch); 636 arch->sorted_instructions = true; 637 } 638 639 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 640 return ins ? ins->ops : NULL; 641 } 642 643 static struct ins_ops *ins__find(struct arch *arch, const char *name) 644 { 645 struct ins_ops *ops = __ins__find(arch, name); 646 647 if (!ops && arch->associate_instruction_ops) 648 ops = arch->associate_instruction_ops(arch, name); 649 650 return ops; 651 } 652 653 static int arch__key_cmp(const void *name, const void *archp) 654 { 655 const struct arch *arch = archp; 656 657 return strcmp(name, arch->name); 658 } 659 660 static int arch__cmp(const void *a, const void *b) 661 { 662 const struct arch *aa = a; 663 const struct arch *ab = b; 664 665 return strcmp(aa->name, ab->name); 666 } 667 668 static void arch__sort(void) 669 { 670 const int nmemb = ARRAY_SIZE(architectures); 671 672 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); 673 } 674 675 static struct arch *arch__find(const char *name) 676 { 677 const int nmemb = ARRAY_SIZE(architectures); 678 static bool sorted; 679 680 if (!sorted) { 681 arch__sort(); 682 sorted = true; 683 } 684 685 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); 686 } 687 688 static struct annotated_source *annotated_source__new(void) 689 { 690 struct annotated_source *src = zalloc(sizeof(*src)); 691 692 if (src != NULL) 693 INIT_LIST_HEAD(&src->source); 694 695 return src; 696 } 697 698 static __maybe_unused void annotated_source__delete(struct annotated_source *src) 699 { 700 if (src == NULL) 701 return; 702 zfree(&src->histograms); 703 zfree(&src->cycles_hist); 704 free(src); 705 } 706 707 static int annotated_source__alloc_histograms(struct annotated_source *src, 708 size_t size, int nr_hists) 709 { 710 size_t sizeof_sym_hist; 711 712 /* 713 * Add buffer of one element for zero length symbol. 714 * When sample is taken from first instruction of 715 * zero length symbol, perf still resolves it and 716 * shows symbol name in perf report and allows to 717 * annotate it. 718 */ 719 if (size == 0) 720 size = 1; 721 722 /* Check for overflow when calculating sizeof_sym_hist */ 723 if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry)) 724 return -1; 725 726 sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry)); 727 728 /* Check for overflow in zalloc argument */ 729 if (sizeof_sym_hist > SIZE_MAX / nr_hists) 730 return -1; 731 732 src->sizeof_sym_hist = sizeof_sym_hist; 733 src->nr_histograms = nr_hists; 734 src->histograms = calloc(nr_hists, sizeof_sym_hist) ; 735 return src->histograms ? 0 : -1; 736 } 737 738 /* The cycles histogram is lazily allocated. */ 739 static int symbol__alloc_hist_cycles(struct symbol *sym) 740 { 741 struct annotation *notes = symbol__annotation(sym); 742 const size_t size = symbol__size(sym); 743 744 notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist)); 745 if (notes->src->cycles_hist == NULL) 746 return -1; 747 return 0; 748 } 749 750 void symbol__annotate_zero_histograms(struct symbol *sym) 751 { 752 struct annotation *notes = symbol__annotation(sym); 753 754 pthread_mutex_lock(¬es->lock); 755 if (notes->src != NULL) { 756 memset(notes->src->histograms, 0, 757 notes->src->nr_histograms * notes->src->sizeof_sym_hist); 758 if (notes->src->cycles_hist) 759 memset(notes->src->cycles_hist, 0, 760 symbol__size(sym) * sizeof(struct cyc_hist)); 761 } 762 pthread_mutex_unlock(¬es->lock); 763 } 764 765 static int __symbol__account_cycles(struct cyc_hist *ch, 766 u64 start, 767 unsigned offset, unsigned cycles, 768 unsigned have_start) 769 { 770 /* 771 * For now we can only account one basic block per 772 * final jump. But multiple could be overlapping. 773 * Always account the longest one. So when 774 * a shorter one has been already seen throw it away. 775 * 776 * We separately always account the full cycles. 777 */ 778 ch[offset].num_aggr++; 779 ch[offset].cycles_aggr += cycles; 780 781 if (cycles > ch[offset].cycles_max) 782 ch[offset].cycles_max = cycles; 783 784 if (ch[offset].cycles_min) { 785 if (cycles && cycles < ch[offset].cycles_min) 786 ch[offset].cycles_min = cycles; 787 } else 788 ch[offset].cycles_min = cycles; 789 790 if (!have_start && ch[offset].have_start) 791 return 0; 792 if (ch[offset].num) { 793 if (have_start && (!ch[offset].have_start || 794 ch[offset].start > start)) { 795 ch[offset].have_start = 0; 796 ch[offset].cycles = 0; 797 ch[offset].num = 0; 798 if (ch[offset].reset < 0xffff) 799 ch[offset].reset++; 800 } else if (have_start && 801 ch[offset].start < start) 802 return 0; 803 } 804 ch[offset].have_start = have_start; 805 ch[offset].start = start; 806 ch[offset].cycles += cycles; 807 ch[offset].num++; 808 return 0; 809 } 810 811 static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map, 812 struct annotated_source *src, int evidx, u64 addr, 813 struct perf_sample *sample) 814 { 815 unsigned offset; 816 struct sym_hist *h; 817 818 pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr)); 819 820 if ((addr < sym->start || addr >= sym->end) && 821 (addr != sym->end || sym->start != sym->end)) { 822 pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n", 823 __func__, __LINE__, sym->name, sym->start, addr, sym->end); 824 return -ERANGE; 825 } 826 827 offset = addr - sym->start; 828 h = annotated_source__histogram(src, evidx); 829 if (h == NULL) { 830 pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n", 831 __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC); 832 return -ENOMEM; 833 } 834 h->nr_samples++; 835 h->addr[offset].nr_samples++; 836 h->period += sample->period; 837 h->addr[offset].period += sample->period; 838 839 pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64 840 ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n", 841 sym->start, sym->name, addr, addr - sym->start, evidx, 842 h->addr[offset].nr_samples, h->addr[offset].period); 843 return 0; 844 } 845 846 static struct cyc_hist *symbol__cycles_hist(struct symbol *sym) 847 { 848 struct annotation *notes = symbol__annotation(sym); 849 850 if (notes->src == NULL) { 851 notes->src = annotated_source__new(); 852 if (notes->src == NULL) 853 return NULL; 854 goto alloc_cycles_hist; 855 } 856 857 if (!notes->src->cycles_hist) { 858 alloc_cycles_hist: 859 symbol__alloc_hist_cycles(sym); 860 } 861 862 return notes->src->cycles_hist; 863 } 864 865 struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists) 866 { 867 struct annotation *notes = symbol__annotation(sym); 868 869 if (notes->src == NULL) { 870 notes->src = annotated_source__new(); 871 if (notes->src == NULL) 872 return NULL; 873 goto alloc_histograms; 874 } 875 876 if (notes->src->histograms == NULL) { 877 alloc_histograms: 878 annotated_source__alloc_histograms(notes->src, symbol__size(sym), 879 nr_hists); 880 } 881 882 return notes->src; 883 } 884 885 static int symbol__inc_addr_samples(struct symbol *sym, struct map *map, 886 struct perf_evsel *evsel, u64 addr, 887 struct perf_sample *sample) 888 { 889 struct annotated_source *src; 890 891 if (sym == NULL) 892 return 0; 893 src = symbol__hists(sym, evsel->evlist->nr_entries); 894 if (src == NULL) 895 return -ENOMEM; 896 return __symbol__inc_addr_samples(sym, map, src, evsel->idx, addr, sample); 897 } 898 899 static int symbol__account_cycles(u64 addr, u64 start, 900 struct symbol *sym, unsigned cycles) 901 { 902 struct cyc_hist *cycles_hist; 903 unsigned offset; 904 905 if (sym == NULL) 906 return 0; 907 cycles_hist = symbol__cycles_hist(sym); 908 if (cycles_hist == NULL) 909 return -ENOMEM; 910 if (addr < sym->start || addr >= sym->end) 911 return -ERANGE; 912 913 if (start) { 914 if (start < sym->start || start >= sym->end) 915 return -ERANGE; 916 if (start >= addr) 917 start = 0; 918 } 919 offset = addr - sym->start; 920 return __symbol__account_cycles(cycles_hist, 921 start ? start - sym->start : 0, 922 offset, cycles, 923 !!start); 924 } 925 926 int addr_map_symbol__account_cycles(struct addr_map_symbol *ams, 927 struct addr_map_symbol *start, 928 unsigned cycles) 929 { 930 u64 saddr = 0; 931 int err; 932 933 if (!cycles) 934 return 0; 935 936 /* 937 * Only set start when IPC can be computed. We can only 938 * compute it when the basic block is completely in a single 939 * function. 940 * Special case the case when the jump is elsewhere, but 941 * it starts on the function start. 942 */ 943 if (start && 944 (start->sym == ams->sym || 945 (ams->sym && 946 start->addr == ams->sym->start + ams->map->start))) 947 saddr = start->al_addr; 948 if (saddr == 0) 949 pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n", 950 ams->addr, 951 start ? start->addr : 0, 952 ams->sym ? ams->sym->start + ams->map->start : 0, 953 saddr); 954 err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles); 955 if (err) 956 pr_debug2("account_cycles failed %d\n", err); 957 return err; 958 } 959 960 static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end) 961 { 962 unsigned n_insn = 0; 963 u64 offset; 964 965 for (offset = start; offset <= end; offset++) { 966 if (notes->offsets[offset]) 967 n_insn++; 968 } 969 return n_insn; 970 } 971 972 static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch) 973 { 974 unsigned n_insn; 975 u64 offset; 976 977 n_insn = annotation__count_insn(notes, start, end); 978 if (n_insn && ch->num && ch->cycles) { 979 float ipc = n_insn / ((double)ch->cycles / (double)ch->num); 980 981 /* Hide data when there are too many overlaps. */ 982 if (ch->reset >= 0x7fff || ch->reset >= ch->num / 2) 983 return; 984 985 for (offset = start; offset <= end; offset++) { 986 struct annotation_line *al = notes->offsets[offset]; 987 988 if (al) 989 al->ipc = ipc; 990 } 991 } 992 } 993 994 void annotation__compute_ipc(struct annotation *notes, size_t size) 995 { 996 u64 offset; 997 998 if (!notes->src || !notes->src->cycles_hist) 999 return; 1000 1001 pthread_mutex_lock(¬es->lock); 1002 for (offset = 0; offset < size; ++offset) { 1003 struct cyc_hist *ch; 1004 1005 ch = ¬es->src->cycles_hist[offset]; 1006 if (ch && ch->cycles) { 1007 struct annotation_line *al; 1008 1009 if (ch->have_start) 1010 annotation__count_and_fill(notes, ch->start, offset, ch); 1011 al = notes->offsets[offset]; 1012 if (al && ch->num_aggr) { 1013 al->cycles = ch->cycles_aggr / ch->num_aggr; 1014 al->cycles_max = ch->cycles_max; 1015 al->cycles_min = ch->cycles_min; 1016 } 1017 notes->have_cycles = true; 1018 } 1019 } 1020 pthread_mutex_unlock(¬es->lock); 1021 } 1022 1023 int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample, 1024 struct perf_evsel *evsel) 1025 { 1026 return symbol__inc_addr_samples(ams->sym, ams->map, evsel, ams->al_addr, sample); 1027 } 1028 1029 int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample, 1030 struct perf_evsel *evsel, u64 ip) 1031 { 1032 return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evsel, ip, sample); 1033 } 1034 1035 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) 1036 { 1037 dl->ins.ops = ins__find(arch, dl->ins.name); 1038 1039 if (!dl->ins.ops) 1040 return; 1041 1042 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0) 1043 dl->ins.ops = NULL; 1044 } 1045 1046 static int disasm_line__parse(char *line, const char **namep, char **rawp) 1047 { 1048 char tmp, *name = ltrim(line); 1049 1050 if (name[0] == '\0') 1051 return -1; 1052 1053 *rawp = name + 1; 1054 1055 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) 1056 ++*rawp; 1057 1058 tmp = (*rawp)[0]; 1059 (*rawp)[0] = '\0'; 1060 *namep = strdup(name); 1061 1062 if (*namep == NULL) 1063 goto out_free_name; 1064 1065 (*rawp)[0] = tmp; 1066 *rawp = ltrim(*rawp); 1067 1068 return 0; 1069 1070 out_free_name: 1071 free((void *)namep); 1072 *namep = NULL; 1073 return -1; 1074 } 1075 1076 struct annotate_args { 1077 size_t privsize; 1078 struct arch *arch; 1079 struct map_symbol ms; 1080 struct perf_evsel *evsel; 1081 struct annotation_options *options; 1082 s64 offset; 1083 char *line; 1084 int line_nr; 1085 }; 1086 1087 static void annotation_line__delete(struct annotation_line *al) 1088 { 1089 void *ptr = (void *) al - al->privsize; 1090 1091 free_srcline(al->path); 1092 zfree(&al->line); 1093 free(ptr); 1094 } 1095 1096 /* 1097 * Allocating the annotation line data with following 1098 * structure: 1099 * 1100 * -------------------------------------- 1101 * private space | struct annotation_line 1102 * -------------------------------------- 1103 * 1104 * Size of the private space is stored in 'struct annotation_line'. 1105 * 1106 */ 1107 static struct annotation_line * 1108 annotation_line__new(struct annotate_args *args, size_t privsize) 1109 { 1110 struct annotation_line *al; 1111 struct perf_evsel *evsel = args->evsel; 1112 size_t size = privsize + sizeof(*al); 1113 int nr = 1; 1114 1115 if (perf_evsel__is_group_event(evsel)) 1116 nr = evsel->nr_members; 1117 1118 size += sizeof(al->data[0]) * nr; 1119 1120 al = zalloc(size); 1121 if (al) { 1122 al = (void *) al + privsize; 1123 al->privsize = privsize; 1124 al->offset = args->offset; 1125 al->line = strdup(args->line); 1126 al->line_nr = args->line_nr; 1127 al->data_nr = nr; 1128 } 1129 1130 return al; 1131 } 1132 1133 /* 1134 * Allocating the disasm annotation line data with 1135 * following structure: 1136 * 1137 * ------------------------------------------------------------ 1138 * privsize space | struct disasm_line | struct annotation_line 1139 * ------------------------------------------------------------ 1140 * 1141 * We have 'struct annotation_line' member as last member 1142 * of 'struct disasm_line' to have an easy access. 1143 * 1144 */ 1145 static struct disasm_line *disasm_line__new(struct annotate_args *args) 1146 { 1147 struct disasm_line *dl = NULL; 1148 struct annotation_line *al; 1149 size_t privsize = args->privsize + offsetof(struct disasm_line, al); 1150 1151 al = annotation_line__new(args, privsize); 1152 if (al != NULL) { 1153 dl = disasm_line(al); 1154 1155 if (dl->al.line == NULL) 1156 goto out_delete; 1157 1158 if (args->offset != -1) { 1159 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1160 goto out_free_line; 1161 1162 disasm_line__init_ins(dl, args->arch, &args->ms); 1163 } 1164 } 1165 1166 return dl; 1167 1168 out_free_line: 1169 zfree(&dl->al.line); 1170 out_delete: 1171 free(dl); 1172 return NULL; 1173 } 1174 1175 void disasm_line__free(struct disasm_line *dl) 1176 { 1177 if (dl->ins.ops && dl->ins.ops->free) 1178 dl->ins.ops->free(&dl->ops); 1179 else 1180 ins__delete(&dl->ops); 1181 free((void *)dl->ins.name); 1182 dl->ins.name = NULL; 1183 annotation_line__delete(&dl->al); 1184 } 1185 1186 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw) 1187 { 1188 if (raw || !dl->ins.ops) 1189 return scnprintf(bf, size, "%-6s %s", dl->ins.name, dl->ops.raw); 1190 1191 return ins__scnprintf(&dl->ins, bf, size, &dl->ops); 1192 } 1193 1194 static void annotation_line__add(struct annotation_line *al, struct list_head *head) 1195 { 1196 list_add_tail(&al->node, head); 1197 } 1198 1199 struct annotation_line * 1200 annotation_line__next(struct annotation_line *pos, struct list_head *head) 1201 { 1202 list_for_each_entry_continue(pos, head, node) 1203 if (pos->offset >= 0) 1204 return pos; 1205 1206 return NULL; 1207 } 1208 1209 static const char *annotate__address_color(struct block_range *br) 1210 { 1211 double cov = block_range__coverage(br); 1212 1213 if (cov >= 0) { 1214 /* mark red for >75% coverage */ 1215 if (cov > 0.75) 1216 return PERF_COLOR_RED; 1217 1218 /* mark dull for <1% coverage */ 1219 if (cov < 0.01) 1220 return PERF_COLOR_NORMAL; 1221 } 1222 1223 return PERF_COLOR_MAGENTA; 1224 } 1225 1226 static const char *annotate__asm_color(struct block_range *br) 1227 { 1228 double cov = block_range__coverage(br); 1229 1230 if (cov >= 0) { 1231 /* mark dull for <1% coverage */ 1232 if (cov < 0.01) 1233 return PERF_COLOR_NORMAL; 1234 } 1235 1236 return PERF_COLOR_BLUE; 1237 } 1238 1239 static void annotate__branch_printf(struct block_range *br, u64 addr) 1240 { 1241 bool emit_comment = true; 1242 1243 if (!br) 1244 return; 1245 1246 #if 1 1247 if (br->is_target && br->start == addr) { 1248 struct block_range *branch = br; 1249 double p; 1250 1251 /* 1252 * Find matching branch to our target. 1253 */ 1254 while (!branch->is_branch) 1255 branch = block_range__next(branch); 1256 1257 p = 100 *(double)br->entry / branch->coverage; 1258 1259 if (p > 0.1) { 1260 if (emit_comment) { 1261 emit_comment = false; 1262 printf("\t#"); 1263 } 1264 1265 /* 1266 * The percentage of coverage joined at this target in relation 1267 * to the next branch. 1268 */ 1269 printf(" +%.2f%%", p); 1270 } 1271 } 1272 #endif 1273 if (br->is_branch && br->end == addr) { 1274 double p = 100*(double)br->taken / br->coverage; 1275 1276 if (p > 0.1) { 1277 if (emit_comment) { 1278 emit_comment = false; 1279 printf("\t#"); 1280 } 1281 1282 /* 1283 * The percentage of coverage leaving at this branch, and 1284 * its prediction ratio. 1285 */ 1286 printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken); 1287 } 1288 } 1289 } 1290 1291 static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width) 1292 { 1293 s64 offset = dl->al.offset; 1294 const u64 addr = start + offset; 1295 struct block_range *br; 1296 1297 br = block_range__find(addr); 1298 color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr); 1299 color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line); 1300 annotate__branch_printf(br, addr); 1301 return 0; 1302 } 1303 1304 static int 1305 annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start, 1306 struct perf_evsel *evsel, u64 len, int min_pcnt, int printed, 1307 int max_lines, struct annotation_line *queue, int addr_fmt_width, 1308 int percent_type) 1309 { 1310 struct disasm_line *dl = container_of(al, struct disasm_line, al); 1311 static const char *prev_line; 1312 static const char *prev_color; 1313 1314 if (al->offset != -1) { 1315 double max_percent = 0.0; 1316 int i, nr_percent = 1; 1317 const char *color; 1318 struct annotation *notes = symbol__annotation(sym); 1319 1320 for (i = 0; i < al->data_nr; i++) { 1321 double percent; 1322 1323 percent = annotation_data__percent(&al->data[i], 1324 percent_type); 1325 1326 if (percent > max_percent) 1327 max_percent = percent; 1328 } 1329 1330 if (al->data_nr > nr_percent) 1331 nr_percent = al->data_nr; 1332 1333 if (max_percent < min_pcnt) 1334 return -1; 1335 1336 if (max_lines && printed >= max_lines) 1337 return 1; 1338 1339 if (queue != NULL) { 1340 list_for_each_entry_from(queue, ¬es->src->source, node) { 1341 if (queue == al) 1342 break; 1343 annotation_line__print(queue, sym, start, evsel, len, 1344 0, 0, 1, NULL, addr_fmt_width, 1345 percent_type); 1346 } 1347 } 1348 1349 color = get_percent_color(max_percent); 1350 1351 /* 1352 * Also color the filename and line if needed, with 1353 * the same color than the percentage. Don't print it 1354 * twice for close colored addr with the same filename:line 1355 */ 1356 if (al->path) { 1357 if (!prev_line || strcmp(prev_line, al->path) 1358 || color != prev_color) { 1359 color_fprintf(stdout, color, " %s", al->path); 1360 prev_line = al->path; 1361 prev_color = color; 1362 } 1363 } 1364 1365 for (i = 0; i < nr_percent; i++) { 1366 struct annotation_data *data = &al->data[i]; 1367 double percent; 1368 1369 percent = annotation_data__percent(data, percent_type); 1370 color = get_percent_color(percent); 1371 1372 if (symbol_conf.show_total_period) 1373 color_fprintf(stdout, color, " %11" PRIu64, 1374 data->he.period); 1375 else if (symbol_conf.show_nr_samples) 1376 color_fprintf(stdout, color, " %7" PRIu64, 1377 data->he.nr_samples); 1378 else 1379 color_fprintf(stdout, color, " %7.2f", percent); 1380 } 1381 1382 printf(" : "); 1383 1384 disasm_line__print(dl, start, addr_fmt_width); 1385 printf("\n"); 1386 } else if (max_lines && printed >= max_lines) 1387 return 1; 1388 else { 1389 int width = symbol_conf.show_total_period ? 12 : 8; 1390 1391 if (queue) 1392 return -1; 1393 1394 if (perf_evsel__is_group_event(evsel)) 1395 width *= evsel->nr_members; 1396 1397 if (!*al->line) 1398 printf(" %*s:\n", width, " "); 1399 else 1400 printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line); 1401 } 1402 1403 return 0; 1404 } 1405 1406 /* 1407 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1408 * which looks like following 1409 * 1410 * 0000000000415500 <_init>: 1411 * 415500: sub $0x8,%rsp 1412 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1413 * 41550b: test %rax,%rax 1414 * 41550e: je 415515 <_init+0x15> 1415 * 415510: callq 416e70 <__gmon_start__@plt> 1416 * 415515: add $0x8,%rsp 1417 * 415519: retq 1418 * 1419 * it will be parsed and saved into struct disasm_line as 1420 * <offset> <name> <ops.raw> 1421 * 1422 * The offset will be a relative offset from the start of the symbol and -1 1423 * means that it's not a disassembly line so should be treated differently. 1424 * The ops.raw part will be parsed further according to type of the instruction. 1425 */ 1426 static int symbol__parse_objdump_line(struct symbol *sym, FILE *file, 1427 struct annotate_args *args, 1428 int *line_nr) 1429 { 1430 struct map *map = args->ms.map; 1431 struct annotation *notes = symbol__annotation(sym); 1432 struct disasm_line *dl; 1433 char *line = NULL, *parsed_line, *tmp, *tmp2; 1434 size_t line_len; 1435 s64 line_ip, offset = -1; 1436 regmatch_t match[2]; 1437 1438 if (getline(&line, &line_len, file) < 0) 1439 return -1; 1440 1441 if (!line) 1442 return -1; 1443 1444 line_ip = -1; 1445 parsed_line = rtrim(line); 1446 1447 /* /filename:linenr ? Save line number and ignore. */ 1448 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1449 *line_nr = atoi(parsed_line + match[1].rm_so); 1450 return 0; 1451 } 1452 1453 tmp = ltrim(parsed_line); 1454 if (*tmp) { 1455 /* 1456 * Parse hexa addresses followed by ':' 1457 */ 1458 line_ip = strtoull(tmp, &tmp2, 16); 1459 if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0') 1460 line_ip = -1; 1461 } 1462 1463 if (line_ip != -1) { 1464 u64 start = map__rip_2objdump(map, sym->start), 1465 end = map__rip_2objdump(map, sym->end); 1466 1467 offset = line_ip - start; 1468 if ((u64)line_ip < start || (u64)line_ip >= end) 1469 offset = -1; 1470 else 1471 parsed_line = tmp2 + 1; 1472 } 1473 1474 args->offset = offset; 1475 args->line = parsed_line; 1476 args->line_nr = *line_nr; 1477 args->ms.sym = sym; 1478 1479 dl = disasm_line__new(args); 1480 free(line); 1481 (*line_nr)++; 1482 1483 if (dl == NULL) 1484 return -1; 1485 1486 if (!disasm_line__has_local_offset(dl)) { 1487 dl->ops.target.offset = dl->ops.target.addr - 1488 map__rip_2objdump(map, sym->start); 1489 dl->ops.target.offset_avail = true; 1490 } 1491 1492 /* kcore has no symbols, so add the call target symbol */ 1493 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1494 struct addr_map_symbol target = { 1495 .map = map, 1496 .addr = dl->ops.target.addr, 1497 }; 1498 1499 if (!map_groups__find_ams(&target) && 1500 target.sym->start == target.al_addr) 1501 dl->ops.target.sym = target.sym; 1502 } 1503 1504 annotation_line__add(&dl->al, ¬es->src->source); 1505 1506 return 0; 1507 } 1508 1509 static __attribute__((constructor)) void symbol__init_regexpr(void) 1510 { 1511 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); 1512 } 1513 1514 static void delete_last_nop(struct symbol *sym) 1515 { 1516 struct annotation *notes = symbol__annotation(sym); 1517 struct list_head *list = ¬es->src->source; 1518 struct disasm_line *dl; 1519 1520 while (!list_empty(list)) { 1521 dl = list_entry(list->prev, struct disasm_line, al.node); 1522 1523 if (dl->ins.ops) { 1524 if (dl->ins.ops != &nop_ops) 1525 return; 1526 } else { 1527 if (!strstr(dl->al.line, " nop ") && 1528 !strstr(dl->al.line, " nopl ") && 1529 !strstr(dl->al.line, " nopw ")) 1530 return; 1531 } 1532 1533 list_del(&dl->al.node); 1534 disasm_line__free(dl); 1535 } 1536 } 1537 1538 int symbol__strerror_disassemble(struct symbol *sym __maybe_unused, struct map *map, 1539 int errnum, char *buf, size_t buflen) 1540 { 1541 struct dso *dso = map->dso; 1542 1543 BUG_ON(buflen == 0); 1544 1545 if (errnum >= 0) { 1546 str_error_r(errnum, buf, buflen); 1547 return 0; 1548 } 1549 1550 switch (errnum) { 1551 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1552 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1553 char *build_id_msg = NULL; 1554 1555 if (dso->has_build_id) { 1556 build_id__sprintf(dso->build_id, 1557 sizeof(dso->build_id), bf + 15); 1558 build_id_msg = bf; 1559 } 1560 scnprintf(buf, buflen, 1561 "No vmlinux file%s\nwas found in the path.\n\n" 1562 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1563 "Please use:\n\n" 1564 " perf buildid-cache -vu vmlinux\n\n" 1565 "or:\n\n" 1566 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1567 } 1568 break; 1569 default: 1570 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1571 break; 1572 } 1573 1574 return 0; 1575 } 1576 1577 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1578 { 1579 char linkname[PATH_MAX]; 1580 char *build_id_filename; 1581 char *build_id_path = NULL; 1582 char *pos; 1583 1584 if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS && 1585 !dso__is_kcore(dso)) 1586 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1587 1588 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1589 if (build_id_filename) { 1590 __symbol__join_symfs(filename, filename_size, build_id_filename); 1591 free(build_id_filename); 1592 } else { 1593 if (dso->has_build_id) 1594 return ENOMEM; 1595 goto fallback; 1596 } 1597 1598 build_id_path = strdup(filename); 1599 if (!build_id_path) 1600 return -1; 1601 1602 /* 1603 * old style build-id cache has name of XX/XXXXXXX.. while 1604 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1605 * extract the build-id part of dirname in the new style only. 1606 */ 1607 pos = strrchr(build_id_path, '/'); 1608 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1609 dirname(build_id_path); 1610 1611 if (dso__is_kcore(dso) || 1612 readlink(build_id_path, linkname, sizeof(linkname)) < 0 || 1613 strstr(linkname, DSO__NAME_KALLSYMS) || 1614 access(filename, R_OK)) { 1615 fallback: 1616 /* 1617 * If we don't have build-ids or the build-id file isn't in the 1618 * cache, or is just a kallsyms file, well, lets hope that this 1619 * DSO is the same as when 'perf record' ran. 1620 */ 1621 __symbol__join_symfs(filename, filename_size, dso->long_name); 1622 } 1623 1624 free(build_id_path); 1625 return 0; 1626 } 1627 1628 static int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 1629 { 1630 struct annotation_options *opts = args->options; 1631 struct map *map = args->ms.map; 1632 struct dso *dso = map->dso; 1633 char *command; 1634 FILE *file; 1635 char symfs_filename[PATH_MAX]; 1636 struct kcore_extract kce; 1637 bool delete_extract = false; 1638 bool decomp = false; 1639 int stdout_fd[2]; 1640 int lineno = 0; 1641 int nline; 1642 pid_t pid; 1643 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 1644 1645 if (err) 1646 return err; 1647 1648 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 1649 symfs_filename, sym->name, map->unmap_ip(map, sym->start), 1650 map->unmap_ip(map, sym->end)); 1651 1652 pr_debug("annotating [%p] %30s : [%p] %30s\n", 1653 dso, dso->long_name, sym, sym->name); 1654 1655 if (dso__is_kcore(dso)) { 1656 kce.kcore_filename = symfs_filename; 1657 kce.addr = map__rip_2objdump(map, sym->start); 1658 kce.offs = sym->start; 1659 kce.len = sym->end - sym->start; 1660 if (!kcore_extract__create(&kce)) { 1661 delete_extract = true; 1662 strlcpy(symfs_filename, kce.extract_filename, 1663 sizeof(symfs_filename)); 1664 } 1665 } else if (dso__needs_decompress(dso)) { 1666 char tmp[KMOD_DECOMP_LEN]; 1667 1668 if (dso__decompress_kmodule_path(dso, symfs_filename, 1669 tmp, sizeof(tmp)) < 0) 1670 goto out; 1671 1672 decomp = true; 1673 strcpy(symfs_filename, tmp); 1674 } 1675 1676 err = asprintf(&command, 1677 "%s %s%s --start-address=0x%016" PRIx64 1678 " --stop-address=0x%016" PRIx64 1679 " -l -d %s %s -C \"%s\" 2>/dev/null|grep -v \"%s:\"|expand", 1680 opts->objdump_path ?: "objdump", 1681 opts->disassembler_style ? "-M " : "", 1682 opts->disassembler_style ?: "", 1683 map__rip_2objdump(map, sym->start), 1684 map__rip_2objdump(map, sym->end), 1685 opts->show_asm_raw ? "" : "--no-show-raw", 1686 opts->annotate_src ? "-S" : "", 1687 symfs_filename, symfs_filename); 1688 1689 if (err < 0) { 1690 pr_err("Failure allocating memory for the command to run\n"); 1691 goto out_remove_tmp; 1692 } 1693 1694 pr_debug("Executing: %s\n", command); 1695 1696 err = -1; 1697 if (pipe(stdout_fd) < 0) { 1698 pr_err("Failure creating the pipe to run %s\n", command); 1699 goto out_free_command; 1700 } 1701 1702 pid = fork(); 1703 if (pid < 0) { 1704 pr_err("Failure forking to run %s\n", command); 1705 goto out_close_stdout; 1706 } 1707 1708 if (pid == 0) { 1709 close(stdout_fd[0]); 1710 dup2(stdout_fd[1], 1); 1711 close(stdout_fd[1]); 1712 execl("/bin/sh", "sh", "-c", command, NULL); 1713 perror(command); 1714 exit(-1); 1715 } 1716 1717 close(stdout_fd[1]); 1718 1719 file = fdopen(stdout_fd[0], "r"); 1720 if (!file) { 1721 pr_err("Failure creating FILE stream for %s\n", command); 1722 /* 1723 * If we were using debug info should retry with 1724 * original binary. 1725 */ 1726 goto out_free_command; 1727 } 1728 1729 nline = 0; 1730 while (!feof(file)) { 1731 /* 1732 * The source code line number (lineno) needs to be kept in 1733 * accross calls to symbol__parse_objdump_line(), so that it 1734 * can associate it with the instructions till the next one. 1735 * See disasm_line__new() and struct disasm_line::line_nr. 1736 */ 1737 if (symbol__parse_objdump_line(sym, file, args, &lineno) < 0) 1738 break; 1739 nline++; 1740 } 1741 1742 if (nline == 0) 1743 pr_err("No output from %s\n", command); 1744 1745 /* 1746 * kallsyms does not have symbol sizes so there may a nop at the end. 1747 * Remove it. 1748 */ 1749 if (dso__is_kcore(dso)) 1750 delete_last_nop(sym); 1751 1752 fclose(file); 1753 err = 0; 1754 out_free_command: 1755 free(command); 1756 out_remove_tmp: 1757 close(stdout_fd[0]); 1758 1759 if (decomp) 1760 unlink(symfs_filename); 1761 1762 if (delete_extract) 1763 kcore_extract__delete(&kce); 1764 out: 1765 return err; 1766 1767 out_close_stdout: 1768 close(stdout_fd[1]); 1769 goto out_free_command; 1770 } 1771 1772 static void calc_percent(struct sym_hist *sym_hist, 1773 struct hists *hists, 1774 struct annotation_data *data, 1775 s64 offset, s64 end) 1776 { 1777 unsigned int hits = 0; 1778 u64 period = 0; 1779 1780 while (offset < end) { 1781 hits += sym_hist->addr[offset].nr_samples; 1782 period += sym_hist->addr[offset].period; 1783 ++offset; 1784 } 1785 1786 if (sym_hist->nr_samples) { 1787 data->he.period = period; 1788 data->he.nr_samples = hits; 1789 data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples; 1790 } 1791 1792 if (hists->stats.nr_non_filtered_samples) 1793 data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples; 1794 1795 if (sym_hist->period) 1796 data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period; 1797 1798 if (hists->stats.total_period) 1799 data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period; 1800 } 1801 1802 static void annotation__calc_percent(struct annotation *notes, 1803 struct perf_evsel *leader, s64 len) 1804 { 1805 struct annotation_line *al, *next; 1806 struct perf_evsel *evsel; 1807 1808 list_for_each_entry(al, ¬es->src->source, node) { 1809 s64 end; 1810 int i = 0; 1811 1812 if (al->offset == -1) 1813 continue; 1814 1815 next = annotation_line__next(al, ¬es->src->source); 1816 end = next ? next->offset : len; 1817 1818 for_each_group_evsel(evsel, leader) { 1819 struct hists *hists = evsel__hists(evsel); 1820 struct annotation_data *data; 1821 struct sym_hist *sym_hist; 1822 1823 BUG_ON(i >= al->data_nr); 1824 1825 sym_hist = annotation__histogram(notes, evsel->idx); 1826 data = &al->data[i++]; 1827 1828 calc_percent(sym_hist, hists, data, al->offset, end); 1829 } 1830 } 1831 } 1832 1833 void symbol__calc_percent(struct symbol *sym, struct perf_evsel *evsel) 1834 { 1835 struct annotation *notes = symbol__annotation(sym); 1836 1837 annotation__calc_percent(notes, evsel, symbol__size(sym)); 1838 } 1839 1840 int symbol__annotate(struct symbol *sym, struct map *map, 1841 struct perf_evsel *evsel, size_t privsize, 1842 struct annotation_options *options, 1843 struct arch **parch) 1844 { 1845 struct annotate_args args = { 1846 .privsize = privsize, 1847 .evsel = evsel, 1848 .options = options, 1849 }; 1850 struct perf_env *env = perf_evsel__env(evsel); 1851 const char *arch_name = perf_env__arch(env); 1852 struct arch *arch; 1853 int err; 1854 1855 if (!arch_name) 1856 return -1; 1857 1858 args.arch = arch = arch__find(arch_name); 1859 if (arch == NULL) 1860 return -ENOTSUP; 1861 1862 if (parch) 1863 *parch = arch; 1864 1865 if (arch->init) { 1866 err = arch->init(arch, env ? env->cpuid : NULL); 1867 if (err) { 1868 pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name); 1869 return err; 1870 } 1871 } 1872 1873 args.ms.map = map; 1874 args.ms.sym = sym; 1875 1876 return symbol__disassemble(sym, &args); 1877 } 1878 1879 static void insert_source_line(struct rb_root *root, struct annotation_line *al, 1880 struct annotation_options *opts) 1881 { 1882 struct annotation_line *iter; 1883 struct rb_node **p = &root->rb_node; 1884 struct rb_node *parent = NULL; 1885 int i, ret; 1886 1887 while (*p != NULL) { 1888 parent = *p; 1889 iter = rb_entry(parent, struct annotation_line, rb_node); 1890 1891 ret = strcmp(iter->path, al->path); 1892 if (ret == 0) { 1893 for (i = 0; i < al->data_nr; i++) { 1894 iter->data[i].percent_sum += annotation_data__percent(&al->data[i], 1895 opts->percent_type); 1896 } 1897 return; 1898 } 1899 1900 if (ret < 0) 1901 p = &(*p)->rb_left; 1902 else 1903 p = &(*p)->rb_right; 1904 } 1905 1906 for (i = 0; i < al->data_nr; i++) { 1907 al->data[i].percent_sum = annotation_data__percent(&al->data[i], 1908 opts->percent_type); 1909 } 1910 1911 rb_link_node(&al->rb_node, parent, p); 1912 rb_insert_color(&al->rb_node, root); 1913 } 1914 1915 static int cmp_source_line(struct annotation_line *a, struct annotation_line *b) 1916 { 1917 int i; 1918 1919 for (i = 0; i < a->data_nr; i++) { 1920 if (a->data[i].percent_sum == b->data[i].percent_sum) 1921 continue; 1922 return a->data[i].percent_sum > b->data[i].percent_sum; 1923 } 1924 1925 return 0; 1926 } 1927 1928 static void __resort_source_line(struct rb_root *root, struct annotation_line *al) 1929 { 1930 struct annotation_line *iter; 1931 struct rb_node **p = &root->rb_node; 1932 struct rb_node *parent = NULL; 1933 1934 while (*p != NULL) { 1935 parent = *p; 1936 iter = rb_entry(parent, struct annotation_line, rb_node); 1937 1938 if (cmp_source_line(al, iter)) 1939 p = &(*p)->rb_left; 1940 else 1941 p = &(*p)->rb_right; 1942 } 1943 1944 rb_link_node(&al->rb_node, parent, p); 1945 rb_insert_color(&al->rb_node, root); 1946 } 1947 1948 static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root) 1949 { 1950 struct annotation_line *al; 1951 struct rb_node *node; 1952 1953 node = rb_first(src_root); 1954 while (node) { 1955 struct rb_node *next; 1956 1957 al = rb_entry(node, struct annotation_line, rb_node); 1958 next = rb_next(node); 1959 rb_erase(node, src_root); 1960 1961 __resort_source_line(dest_root, al); 1962 node = next; 1963 } 1964 } 1965 1966 static void print_summary(struct rb_root *root, const char *filename) 1967 { 1968 struct annotation_line *al; 1969 struct rb_node *node; 1970 1971 printf("\nSorted summary for file %s\n", filename); 1972 printf("----------------------------------------------\n\n"); 1973 1974 if (RB_EMPTY_ROOT(root)) { 1975 printf(" Nothing higher than %1.1f%%\n", MIN_GREEN); 1976 return; 1977 } 1978 1979 node = rb_first(root); 1980 while (node) { 1981 double percent, percent_max = 0.0; 1982 const char *color; 1983 char *path; 1984 int i; 1985 1986 al = rb_entry(node, struct annotation_line, rb_node); 1987 for (i = 0; i < al->data_nr; i++) { 1988 percent = al->data[i].percent_sum; 1989 color = get_percent_color(percent); 1990 color_fprintf(stdout, color, " %7.2f", percent); 1991 1992 if (percent > percent_max) 1993 percent_max = percent; 1994 } 1995 1996 path = al->path; 1997 color = get_percent_color(percent_max); 1998 color_fprintf(stdout, color, " %s\n", path); 1999 2000 node = rb_next(node); 2001 } 2002 } 2003 2004 static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel) 2005 { 2006 struct annotation *notes = symbol__annotation(sym); 2007 struct sym_hist *h = annotation__histogram(notes, evsel->idx); 2008 u64 len = symbol__size(sym), offset; 2009 2010 for (offset = 0; offset < len; ++offset) 2011 if (h->addr[offset].nr_samples != 0) 2012 printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2, 2013 sym->start + offset, h->addr[offset].nr_samples); 2014 printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples); 2015 } 2016 2017 static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start) 2018 { 2019 char bf[32]; 2020 struct annotation_line *line; 2021 2022 list_for_each_entry_reverse(line, lines, node) { 2023 if (line->offset != -1) 2024 return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset); 2025 } 2026 2027 return 0; 2028 } 2029 2030 int symbol__annotate_printf(struct symbol *sym, struct map *map, 2031 struct perf_evsel *evsel, 2032 struct annotation_options *opts) 2033 { 2034 struct dso *dso = map->dso; 2035 char *filename; 2036 const char *d_filename; 2037 const char *evsel_name = perf_evsel__name(evsel); 2038 struct annotation *notes = symbol__annotation(sym); 2039 struct sym_hist *h = annotation__histogram(notes, evsel->idx); 2040 struct annotation_line *pos, *queue = NULL; 2041 u64 start = map__rip_2objdump(map, sym->start); 2042 int printed = 2, queue_len = 0, addr_fmt_width; 2043 int more = 0; 2044 bool context = opts->context; 2045 u64 len; 2046 int width = symbol_conf.show_total_period ? 12 : 8; 2047 int graph_dotted_len; 2048 char buf[512]; 2049 2050 filename = strdup(dso->long_name); 2051 if (!filename) 2052 return -ENOMEM; 2053 2054 if (opts->full_path) 2055 d_filename = filename; 2056 else 2057 d_filename = basename(filename); 2058 2059 len = symbol__size(sym); 2060 2061 if (perf_evsel__is_group_event(evsel)) { 2062 width *= evsel->nr_members; 2063 perf_evsel__group_desc(evsel, buf, sizeof(buf)); 2064 evsel_name = buf; 2065 } 2066 2067 graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, " 2068 "percent: %s)\n", 2069 width, width, symbol_conf.show_total_period ? "Period" : 2070 symbol_conf.show_nr_samples ? "Samples" : "Percent", 2071 d_filename, evsel_name, h->nr_samples, 2072 percent_type_str(opts->percent_type)); 2073 2074 printf("%-*.*s----\n", 2075 graph_dotted_len, graph_dotted_len, graph_dotted_line); 2076 2077 if (verbose > 0) 2078 symbol__annotate_hits(sym, evsel); 2079 2080 addr_fmt_width = annotated_source__addr_fmt_width(¬es->src->source, start); 2081 2082 list_for_each_entry(pos, ¬es->src->source, node) { 2083 int err; 2084 2085 if (context && queue == NULL) { 2086 queue = pos; 2087 queue_len = 0; 2088 } 2089 2090 err = annotation_line__print(pos, sym, start, evsel, len, 2091 opts->min_pcnt, printed, opts->max_lines, 2092 queue, addr_fmt_width, opts->percent_type); 2093 2094 switch (err) { 2095 case 0: 2096 ++printed; 2097 if (context) { 2098 printed += queue_len; 2099 queue = NULL; 2100 queue_len = 0; 2101 } 2102 break; 2103 case 1: 2104 /* filtered by max_lines */ 2105 ++more; 2106 break; 2107 case -1: 2108 default: 2109 /* 2110 * Filtered by min_pcnt or non IP lines when 2111 * context != 0 2112 */ 2113 if (!context) 2114 break; 2115 if (queue_len == context) 2116 queue = list_entry(queue->node.next, typeof(*queue), node); 2117 else 2118 ++queue_len; 2119 break; 2120 } 2121 } 2122 2123 free(filename); 2124 2125 return more; 2126 } 2127 2128 static void FILE__set_percent_color(void *fp __maybe_unused, 2129 double percent __maybe_unused, 2130 bool current __maybe_unused) 2131 { 2132 } 2133 2134 static int FILE__set_jumps_percent_color(void *fp __maybe_unused, 2135 int nr __maybe_unused, bool current __maybe_unused) 2136 { 2137 return 0; 2138 } 2139 2140 static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused) 2141 { 2142 return 0; 2143 } 2144 2145 static void FILE__printf(void *fp, const char *fmt, ...) 2146 { 2147 va_list args; 2148 2149 va_start(args, fmt); 2150 vfprintf(fp, fmt, args); 2151 va_end(args); 2152 } 2153 2154 static void FILE__write_graph(void *fp, int graph) 2155 { 2156 const char *s; 2157 switch (graph) { 2158 2159 case DARROW_CHAR: s = "↓"; break; 2160 case UARROW_CHAR: s = "↑"; break; 2161 case LARROW_CHAR: s = "←"; break; 2162 case RARROW_CHAR: s = "→"; break; 2163 default: s = "?"; break; 2164 } 2165 2166 fputs(s, fp); 2167 } 2168 2169 static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp, 2170 struct annotation_options *opts) 2171 { 2172 struct annotation *notes = symbol__annotation(sym); 2173 struct annotation_write_ops wops = { 2174 .first_line = true, 2175 .obj = fp, 2176 .set_color = FILE__set_color, 2177 .set_percent_color = FILE__set_percent_color, 2178 .set_jumps_percent_color = FILE__set_jumps_percent_color, 2179 .printf = FILE__printf, 2180 .write_graph = FILE__write_graph, 2181 }; 2182 struct annotation_line *al; 2183 2184 list_for_each_entry(al, ¬es->src->source, node) { 2185 if (annotation_line__filter(al, notes)) 2186 continue; 2187 annotation_line__write(al, notes, &wops, opts); 2188 fputc('\n', fp); 2189 wops.first_line = false; 2190 } 2191 2192 return 0; 2193 } 2194 2195 int map_symbol__annotation_dump(struct map_symbol *ms, struct perf_evsel *evsel, 2196 struct annotation_options *opts) 2197 { 2198 const char *ev_name = perf_evsel__name(evsel); 2199 char buf[1024]; 2200 char *filename; 2201 int err = -1; 2202 FILE *fp; 2203 2204 if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0) 2205 return -1; 2206 2207 fp = fopen(filename, "w"); 2208 if (fp == NULL) 2209 goto out_free_filename; 2210 2211 if (perf_evsel__is_group_event(evsel)) { 2212 perf_evsel__group_desc(evsel, buf, sizeof(buf)); 2213 ev_name = buf; 2214 } 2215 2216 fprintf(fp, "%s() %s\nEvent: %s\n\n", 2217 ms->sym->name, ms->map->dso->long_name, ev_name); 2218 symbol__annotate_fprintf2(ms->sym, fp, opts); 2219 2220 fclose(fp); 2221 err = 0; 2222 out_free_filename: 2223 free(filename); 2224 return err; 2225 } 2226 2227 void symbol__annotate_zero_histogram(struct symbol *sym, int evidx) 2228 { 2229 struct annotation *notes = symbol__annotation(sym); 2230 struct sym_hist *h = annotation__histogram(notes, evidx); 2231 2232 memset(h, 0, notes->src->sizeof_sym_hist); 2233 } 2234 2235 void symbol__annotate_decay_histogram(struct symbol *sym, int evidx) 2236 { 2237 struct annotation *notes = symbol__annotation(sym); 2238 struct sym_hist *h = annotation__histogram(notes, evidx); 2239 int len = symbol__size(sym), offset; 2240 2241 h->nr_samples = 0; 2242 for (offset = 0; offset < len; ++offset) { 2243 h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8; 2244 h->nr_samples += h->addr[offset].nr_samples; 2245 } 2246 } 2247 2248 void annotated_source__purge(struct annotated_source *as) 2249 { 2250 struct annotation_line *al, *n; 2251 2252 list_for_each_entry_safe(al, n, &as->source, node) { 2253 list_del(&al->node); 2254 disasm_line__free(disasm_line(al)); 2255 } 2256 } 2257 2258 static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp) 2259 { 2260 size_t printed; 2261 2262 if (dl->al.offset == -1) 2263 return fprintf(fp, "%s\n", dl->al.line); 2264 2265 printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name); 2266 2267 if (dl->ops.raw[0] != '\0') { 2268 printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ", 2269 dl->ops.raw); 2270 } 2271 2272 return printed + fprintf(fp, "\n"); 2273 } 2274 2275 size_t disasm__fprintf(struct list_head *head, FILE *fp) 2276 { 2277 struct disasm_line *pos; 2278 size_t printed = 0; 2279 2280 list_for_each_entry(pos, head, al.node) 2281 printed += disasm_line__fprintf(pos, fp); 2282 2283 return printed; 2284 } 2285 2286 bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym) 2287 { 2288 if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) || 2289 !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 || 2290 dl->ops.target.offset >= (s64)symbol__size(sym)) 2291 return false; 2292 2293 return true; 2294 } 2295 2296 void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym) 2297 { 2298 u64 offset, size = symbol__size(sym); 2299 2300 /* PLT symbols contain external offsets */ 2301 if (strstr(sym->name, "@plt")) 2302 return; 2303 2304 for (offset = 0; offset < size; ++offset) { 2305 struct annotation_line *al = notes->offsets[offset]; 2306 struct disasm_line *dl; 2307 2308 dl = disasm_line(al); 2309 2310 if (!disasm_line__is_valid_local_jump(dl, sym)) 2311 continue; 2312 2313 al = notes->offsets[dl->ops.target.offset]; 2314 2315 /* 2316 * FIXME: Oops, no jump target? Buggy disassembler? Or do we 2317 * have to adjust to the previous offset? 2318 */ 2319 if (al == NULL) 2320 continue; 2321 2322 if (++al->jump_sources > notes->max_jump_sources) 2323 notes->max_jump_sources = al->jump_sources; 2324 2325 ++notes->nr_jumps; 2326 } 2327 } 2328 2329 void annotation__set_offsets(struct annotation *notes, s64 size) 2330 { 2331 struct annotation_line *al; 2332 2333 notes->max_line_len = 0; 2334 2335 list_for_each_entry(al, ¬es->src->source, node) { 2336 size_t line_len = strlen(al->line); 2337 2338 if (notes->max_line_len < line_len) 2339 notes->max_line_len = line_len; 2340 al->idx = notes->nr_entries++; 2341 if (al->offset != -1) { 2342 al->idx_asm = notes->nr_asm_entries++; 2343 /* 2344 * FIXME: short term bandaid to cope with assembly 2345 * routines that comes with labels in the same column 2346 * as the address in objdump, sigh. 2347 * 2348 * E.g. copy_user_generic_unrolled 2349 */ 2350 if (al->offset < size) 2351 notes->offsets[al->offset] = al; 2352 } else 2353 al->idx_asm = -1; 2354 } 2355 } 2356 2357 static inline int width_jumps(int n) 2358 { 2359 if (n >= 100) 2360 return 5; 2361 if (n / 10) 2362 return 2; 2363 return 1; 2364 } 2365 2366 void annotation__init_column_widths(struct annotation *notes, struct symbol *sym) 2367 { 2368 notes->widths.addr = notes->widths.target = 2369 notes->widths.min_addr = hex_width(symbol__size(sym)); 2370 notes->widths.max_addr = hex_width(sym->end); 2371 notes->widths.jumps = width_jumps(notes->max_jump_sources); 2372 } 2373 2374 void annotation__update_column_widths(struct annotation *notes) 2375 { 2376 if (notes->options->use_offset) 2377 notes->widths.target = notes->widths.min_addr; 2378 else 2379 notes->widths.target = notes->widths.max_addr; 2380 2381 notes->widths.addr = notes->widths.target; 2382 2383 if (notes->options->show_nr_jumps) 2384 notes->widths.addr += notes->widths.jumps + 1; 2385 } 2386 2387 static void annotation__calc_lines(struct annotation *notes, struct map *map, 2388 struct rb_root *root, 2389 struct annotation_options *opts) 2390 { 2391 struct annotation_line *al; 2392 struct rb_root tmp_root = RB_ROOT; 2393 2394 list_for_each_entry(al, ¬es->src->source, node) { 2395 double percent_max = 0.0; 2396 int i; 2397 2398 for (i = 0; i < al->data_nr; i++) { 2399 double percent; 2400 2401 percent = annotation_data__percent(&al->data[i], 2402 opts->percent_type); 2403 2404 if (percent > percent_max) 2405 percent_max = percent; 2406 } 2407 2408 if (percent_max <= 0.5) 2409 continue; 2410 2411 al->path = get_srcline(map->dso, notes->start + al->offset, NULL, 2412 false, true, notes->start + al->offset); 2413 insert_source_line(&tmp_root, al, opts); 2414 } 2415 2416 resort_source_line(root, &tmp_root); 2417 } 2418 2419 static void symbol__calc_lines(struct symbol *sym, struct map *map, 2420 struct rb_root *root, 2421 struct annotation_options *opts) 2422 { 2423 struct annotation *notes = symbol__annotation(sym); 2424 2425 annotation__calc_lines(notes, map, root, opts); 2426 } 2427 2428 int symbol__tty_annotate2(struct symbol *sym, struct map *map, 2429 struct perf_evsel *evsel, 2430 struct annotation_options *opts) 2431 { 2432 struct dso *dso = map->dso; 2433 struct rb_root source_line = RB_ROOT; 2434 struct hists *hists = evsel__hists(evsel); 2435 char buf[1024]; 2436 2437 if (symbol__annotate2(sym, map, evsel, opts, NULL) < 0) 2438 return -1; 2439 2440 if (opts->print_lines) { 2441 srcline_full_filename = opts->full_path; 2442 symbol__calc_lines(sym, map, &source_line, opts); 2443 print_summary(&source_line, dso->long_name); 2444 } 2445 2446 hists__scnprintf_title(hists, buf, sizeof(buf)); 2447 fprintf(stdout, "%s, [percent: %s]\n%s() %s\n", 2448 buf, percent_type_str(opts->percent_type), sym->name, dso->long_name); 2449 symbol__annotate_fprintf2(sym, stdout, opts); 2450 2451 annotated_source__purge(symbol__annotation(sym)->src); 2452 2453 return 0; 2454 } 2455 2456 int symbol__tty_annotate(struct symbol *sym, struct map *map, 2457 struct perf_evsel *evsel, 2458 struct annotation_options *opts) 2459 { 2460 struct dso *dso = map->dso; 2461 struct rb_root source_line = RB_ROOT; 2462 2463 if (symbol__annotate(sym, map, evsel, 0, opts, NULL) < 0) 2464 return -1; 2465 2466 symbol__calc_percent(sym, evsel); 2467 2468 if (opts->print_lines) { 2469 srcline_full_filename = opts->full_path; 2470 symbol__calc_lines(sym, map, &source_line, opts); 2471 print_summary(&source_line, dso->long_name); 2472 } 2473 2474 symbol__annotate_printf(sym, map, evsel, opts); 2475 2476 annotated_source__purge(symbol__annotation(sym)->src); 2477 2478 return 0; 2479 } 2480 2481 bool ui__has_annotation(void) 2482 { 2483 return use_browser == 1 && perf_hpp_list.sym; 2484 } 2485 2486 2487 static double annotation_line__max_percent(struct annotation_line *al, 2488 struct annotation *notes, 2489 unsigned int percent_type) 2490 { 2491 double percent_max = 0.0; 2492 int i; 2493 2494 for (i = 0; i < notes->nr_events; i++) { 2495 double percent; 2496 2497 percent = annotation_data__percent(&al->data[i], 2498 percent_type); 2499 2500 if (percent > percent_max) 2501 percent_max = percent; 2502 } 2503 2504 return percent_max; 2505 } 2506 2507 static void disasm_line__write(struct disasm_line *dl, struct annotation *notes, 2508 void *obj, char *bf, size_t size, 2509 void (*obj__printf)(void *obj, const char *fmt, ...), 2510 void (*obj__write_graph)(void *obj, int graph)) 2511 { 2512 if (dl->ins.ops && dl->ins.ops->scnprintf) { 2513 if (ins__is_jump(&dl->ins)) { 2514 bool fwd; 2515 2516 if (dl->ops.target.outside) 2517 goto call_like; 2518 fwd = dl->ops.target.offset > dl->al.offset; 2519 obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR); 2520 obj__printf(obj, " "); 2521 } else if (ins__is_call(&dl->ins)) { 2522 call_like: 2523 obj__write_graph(obj, RARROW_CHAR); 2524 obj__printf(obj, " "); 2525 } else if (ins__is_ret(&dl->ins)) { 2526 obj__write_graph(obj, LARROW_CHAR); 2527 obj__printf(obj, " "); 2528 } else { 2529 obj__printf(obj, " "); 2530 } 2531 } else { 2532 obj__printf(obj, " "); 2533 } 2534 2535 disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset); 2536 } 2537 2538 static void __annotation_line__write(struct annotation_line *al, struct annotation *notes, 2539 bool first_line, bool current_entry, bool change_color, int width, 2540 void *obj, unsigned int percent_type, 2541 int (*obj__set_color)(void *obj, int color), 2542 void (*obj__set_percent_color)(void *obj, double percent, bool current), 2543 int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current), 2544 void (*obj__printf)(void *obj, const char *fmt, ...), 2545 void (*obj__write_graph)(void *obj, int graph)) 2546 2547 { 2548 double percent_max = annotation_line__max_percent(al, notes, percent_type); 2549 int pcnt_width = annotation__pcnt_width(notes), 2550 cycles_width = annotation__cycles_width(notes); 2551 bool show_title = false; 2552 char bf[256]; 2553 int printed; 2554 2555 if (first_line && (al->offset == -1 || percent_max == 0.0)) { 2556 if (notes->have_cycles) { 2557 if (al->ipc == 0.0 && al->cycles == 0) 2558 show_title = true; 2559 } else 2560 show_title = true; 2561 } 2562 2563 if (al->offset != -1 && percent_max != 0.0) { 2564 int i; 2565 2566 for (i = 0; i < notes->nr_events; i++) { 2567 double percent; 2568 2569 percent = annotation_data__percent(&al->data[i], percent_type); 2570 2571 obj__set_percent_color(obj, percent, current_entry); 2572 if (notes->options->show_total_period) { 2573 obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period); 2574 } else if (notes->options->show_nr_samples) { 2575 obj__printf(obj, "%6" PRIu64 " ", 2576 al->data[i].he.nr_samples); 2577 } else { 2578 obj__printf(obj, "%6.2f ", percent); 2579 } 2580 } 2581 } else { 2582 obj__set_percent_color(obj, 0, current_entry); 2583 2584 if (!show_title) 2585 obj__printf(obj, "%-*s", pcnt_width, " "); 2586 else { 2587 obj__printf(obj, "%-*s", pcnt_width, 2588 notes->options->show_total_period ? "Period" : 2589 notes->options->show_nr_samples ? "Samples" : "Percent"); 2590 } 2591 } 2592 2593 if (notes->have_cycles) { 2594 if (al->ipc) 2595 obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc); 2596 else if (!show_title) 2597 obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " "); 2598 else 2599 obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC"); 2600 2601 if (!notes->options->show_minmax_cycle) { 2602 if (al->cycles) 2603 obj__printf(obj, "%*" PRIu64 " ", 2604 ANNOTATION__CYCLES_WIDTH - 1, al->cycles); 2605 else if (!show_title) 2606 obj__printf(obj, "%*s", 2607 ANNOTATION__CYCLES_WIDTH, " "); 2608 else 2609 obj__printf(obj, "%*s ", 2610 ANNOTATION__CYCLES_WIDTH - 1, 2611 "Cycle"); 2612 } else { 2613 if (al->cycles) { 2614 char str[32]; 2615 2616 scnprintf(str, sizeof(str), 2617 "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")", 2618 al->cycles, al->cycles_min, 2619 al->cycles_max); 2620 2621 obj__printf(obj, "%*s ", 2622 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2623 str); 2624 } else if (!show_title) 2625 obj__printf(obj, "%*s", 2626 ANNOTATION__MINMAX_CYCLES_WIDTH, 2627 " "); 2628 else 2629 obj__printf(obj, "%*s ", 2630 ANNOTATION__MINMAX_CYCLES_WIDTH - 1, 2631 "Cycle(min/max)"); 2632 } 2633 } 2634 2635 obj__printf(obj, " "); 2636 2637 if (!*al->line) 2638 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " "); 2639 else if (al->offset == -1) { 2640 if (al->line_nr && notes->options->show_linenr) 2641 printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr); 2642 else 2643 printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " "); 2644 obj__printf(obj, bf); 2645 obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line); 2646 } else { 2647 u64 addr = al->offset; 2648 int color = -1; 2649 2650 if (!notes->options->use_offset) 2651 addr += notes->start; 2652 2653 if (!notes->options->use_offset) { 2654 printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr); 2655 } else { 2656 if (al->jump_sources && 2657 notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) { 2658 if (notes->options->show_nr_jumps) { 2659 int prev; 2660 printed = scnprintf(bf, sizeof(bf), "%*d ", 2661 notes->widths.jumps, 2662 al->jump_sources); 2663 prev = obj__set_jumps_percent_color(obj, al->jump_sources, 2664 current_entry); 2665 obj__printf(obj, bf); 2666 obj__set_color(obj, prev); 2667 } 2668 print_addr: 2669 printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ", 2670 notes->widths.target, addr); 2671 } else if (ins__is_call(&disasm_line(al)->ins) && 2672 notes->options->offset_level >= ANNOTATION__OFFSET_CALL) { 2673 goto print_addr; 2674 } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) { 2675 goto print_addr; 2676 } else { 2677 printed = scnprintf(bf, sizeof(bf), "%-*s ", 2678 notes->widths.addr, " "); 2679 } 2680 } 2681 2682 if (change_color) 2683 color = obj__set_color(obj, HE_COLORSET_ADDR); 2684 obj__printf(obj, bf); 2685 if (change_color) 2686 obj__set_color(obj, color); 2687 2688 disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph); 2689 2690 obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf); 2691 } 2692 2693 } 2694 2695 void annotation_line__write(struct annotation_line *al, struct annotation *notes, 2696 struct annotation_write_ops *wops, 2697 struct annotation_options *opts) 2698 { 2699 __annotation_line__write(al, notes, wops->first_line, wops->current_entry, 2700 wops->change_color, wops->width, wops->obj, 2701 opts->percent_type, 2702 wops->set_color, wops->set_percent_color, 2703 wops->set_jumps_percent_color, wops->printf, 2704 wops->write_graph); 2705 } 2706 2707 int symbol__annotate2(struct symbol *sym, struct map *map, struct perf_evsel *evsel, 2708 struct annotation_options *options, struct arch **parch) 2709 { 2710 struct annotation *notes = symbol__annotation(sym); 2711 size_t size = symbol__size(sym); 2712 int nr_pcnt = 1, err; 2713 2714 notes->offsets = zalloc(size * sizeof(struct annotation_line *)); 2715 if (notes->offsets == NULL) 2716 return -1; 2717 2718 if (perf_evsel__is_group_event(evsel)) 2719 nr_pcnt = evsel->nr_members; 2720 2721 err = symbol__annotate(sym, map, evsel, 0, options, parch); 2722 if (err) 2723 goto out_free_offsets; 2724 2725 notes->options = options; 2726 2727 symbol__calc_percent(sym, evsel); 2728 2729 notes->start = map__rip_2objdump(map, sym->start); 2730 2731 annotation__set_offsets(notes, size); 2732 annotation__mark_jump_targets(notes, sym); 2733 annotation__compute_ipc(notes, size); 2734 annotation__init_column_widths(notes, sym); 2735 notes->nr_events = nr_pcnt; 2736 2737 annotation__update_column_widths(notes); 2738 2739 return 0; 2740 2741 out_free_offsets: 2742 zfree(¬es->offsets); 2743 return -1; 2744 } 2745 2746 #define ANNOTATION__CFG(n) \ 2747 { .name = #n, .value = &annotation__default_options.n, } 2748 2749 /* 2750 * Keep the entries sorted, they are bsearch'ed 2751 */ 2752 static struct annotation_config { 2753 const char *name; 2754 void *value; 2755 } annotation__configs[] = { 2756 ANNOTATION__CFG(hide_src_code), 2757 ANNOTATION__CFG(jump_arrows), 2758 ANNOTATION__CFG(offset_level), 2759 ANNOTATION__CFG(show_linenr), 2760 ANNOTATION__CFG(show_nr_jumps), 2761 ANNOTATION__CFG(show_nr_samples), 2762 ANNOTATION__CFG(show_total_period), 2763 ANNOTATION__CFG(use_offset), 2764 }; 2765 2766 #undef ANNOTATION__CFG 2767 2768 static int annotation_config__cmp(const void *name, const void *cfgp) 2769 { 2770 const struct annotation_config *cfg = cfgp; 2771 2772 return strcmp(name, cfg->name); 2773 } 2774 2775 static int annotation__config(const char *var, const char *value, 2776 void *data __maybe_unused) 2777 { 2778 struct annotation_config *cfg; 2779 const char *name; 2780 2781 if (!strstarts(var, "annotate.")) 2782 return 0; 2783 2784 name = var + 9; 2785 cfg = bsearch(name, annotation__configs, ARRAY_SIZE(annotation__configs), 2786 sizeof(struct annotation_config), annotation_config__cmp); 2787 2788 if (cfg == NULL) 2789 pr_debug("%s variable unknown, ignoring...", var); 2790 else if (strcmp(var, "annotate.offset_level") == 0) { 2791 perf_config_int(cfg->value, name, value); 2792 2793 if (*(int *)cfg->value > ANNOTATION__MAX_OFFSET_LEVEL) 2794 *(int *)cfg->value = ANNOTATION__MAX_OFFSET_LEVEL; 2795 else if (*(int *)cfg->value < ANNOTATION__MIN_OFFSET_LEVEL) 2796 *(int *)cfg->value = ANNOTATION__MIN_OFFSET_LEVEL; 2797 } else { 2798 *(bool *)cfg->value = perf_config_bool(name, value); 2799 } 2800 return 0; 2801 } 2802 2803 void annotation_config__init(void) 2804 { 2805 perf_config(annotation__config, NULL); 2806 2807 annotation__default_options.show_total_period = symbol_conf.show_total_period; 2808 annotation__default_options.show_nr_samples = symbol_conf.show_nr_samples; 2809 } 2810 2811 static unsigned int parse_percent_type(char *str1, char *str2) 2812 { 2813 unsigned int type = (unsigned int) -1; 2814 2815 if (!strcmp("period", str1)) { 2816 if (!strcmp("local", str2)) 2817 type = PERCENT_PERIOD_LOCAL; 2818 else if (!strcmp("global", str2)) 2819 type = PERCENT_PERIOD_GLOBAL; 2820 } 2821 2822 if (!strcmp("hits", str1)) { 2823 if (!strcmp("local", str2)) 2824 type = PERCENT_HITS_LOCAL; 2825 else if (!strcmp("global", str2)) 2826 type = PERCENT_HITS_GLOBAL; 2827 } 2828 2829 return type; 2830 } 2831 2832 int annotate_parse_percent_type(const struct option *opt, const char *_str, 2833 int unset __maybe_unused) 2834 { 2835 struct annotation_options *opts = opt->value; 2836 unsigned int type; 2837 char *str1, *str2; 2838 int err = -1; 2839 2840 str1 = strdup(_str); 2841 if (!str1) 2842 return -ENOMEM; 2843 2844 str2 = strchr(str1, '-'); 2845 if (!str2) 2846 goto out; 2847 2848 *str2++ = 0; 2849 2850 type = parse_percent_type(str1, str2); 2851 if (type == (unsigned int) -1) 2852 type = parse_percent_type(str2, str1); 2853 if (type != (unsigned int) -1) { 2854 opts->percent_type = type; 2855 err = 0; 2856 } 2857 2858 out: 2859 free(str1); 2860 return err; 2861 } 2862