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