1 // SPDX-License-Identifier: GPL-2.0-only 2 #include <ctype.h> 3 #include <errno.h> 4 #include <fcntl.h> 5 #include <inttypes.h> 6 #include <libgen.h> 7 #include <regex.h> 8 #include <stdlib.h> 9 #include <unistd.h> 10 11 #include <linux/string.h> 12 #include <subcmd/run-command.h> 13 14 #include "annotate.h" 15 #include "annotate-data.h" 16 #include "build-id.h" 17 #include "debug.h" 18 #include "disasm.h" 19 #include "disasm_bpf.h" 20 #include "dso.h" 21 #include "dwarf-regs.h" 22 #include "env.h" 23 #include "evsel.h" 24 #include "map.h" 25 #include "maps.h" 26 #include "namespaces.h" 27 #include "srcline.h" 28 #include "symbol.h" 29 #include "util.h" 30 31 static regex_t file_lineno; 32 33 /* These can be referred from the arch-dependent code */ 34 static struct ins_ops call_ops; 35 static struct ins_ops dec_ops; 36 static struct ins_ops jump_ops; 37 static struct ins_ops mov_ops; 38 static struct ins_ops nop_ops; 39 static struct ins_ops lock_ops; 40 static struct ins_ops ret_ops; 41 static struct ins_ops load_store_ops; 42 static struct ins_ops arithmetic_ops; 43 44 static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 45 struct ins_operands *ops, int max_ins_name); 46 static int call__scnprintf(struct ins *ins, char *bf, size_t size, 47 struct ins_operands *ops, int max_ins_name); 48 49 static void ins__sort(struct arch *arch); 50 static int disasm_line__parse(char *line, const char **namep, char **rawp); 51 static int disasm_line__parse_powerpc(struct disasm_line *dl); 52 static char *expand_tabs(char *line, char **storage, size_t *storage_len); 53 54 static __attribute__((constructor)) void symbol__init_regexpr(void) 55 { 56 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED); 57 } 58 59 static int arch__grow_instructions(struct arch *arch) 60 { 61 struct ins *new_instructions; 62 size_t new_nr_allocated; 63 64 if (arch->nr_instructions_allocated == 0 && arch->instructions) 65 goto grow_from_non_allocated_table; 66 67 new_nr_allocated = arch->nr_instructions_allocated + 128; 68 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins)); 69 if (new_instructions == NULL) 70 return -1; 71 72 out_update_instructions: 73 arch->instructions = new_instructions; 74 arch->nr_instructions_allocated = new_nr_allocated; 75 return 0; 76 77 grow_from_non_allocated_table: 78 new_nr_allocated = arch->nr_instructions + 128; 79 new_instructions = calloc(new_nr_allocated, sizeof(struct ins)); 80 if (new_instructions == NULL) 81 return -1; 82 83 memcpy(new_instructions, arch->instructions, arch->nr_instructions); 84 goto out_update_instructions; 85 } 86 87 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops) 88 { 89 struct ins *ins; 90 91 if (arch->nr_instructions == arch->nr_instructions_allocated && 92 arch__grow_instructions(arch)) 93 return -1; 94 95 ins = &arch->instructions[arch->nr_instructions]; 96 ins->name = strdup(name); 97 if (!ins->name) 98 return -1; 99 100 ins->ops = ops; 101 arch->nr_instructions++; 102 103 ins__sort(arch); 104 return 0; 105 } 106 107 #include "arch/arc/annotate/instructions.c" 108 #include "arch/arm/annotate/instructions.c" 109 #include "arch/arm64/annotate/instructions.c" 110 #include "arch/csky/annotate/instructions.c" 111 #include "arch/loongarch/annotate/instructions.c" 112 #include "arch/mips/annotate/instructions.c" 113 #include "arch/x86/annotate/instructions.c" 114 #include "arch/powerpc/annotate/instructions.c" 115 #include "arch/riscv64/annotate/instructions.c" 116 #include "arch/s390/annotate/instructions.c" 117 #include "arch/sparc/annotate/instructions.c" 118 119 static struct arch architectures[] = { 120 { 121 .name = "arc", 122 .init = arc__annotate_init, 123 }, 124 { 125 .name = "arm", 126 .init = arm__annotate_init, 127 }, 128 { 129 .name = "arm64", 130 .init = arm64__annotate_init, 131 }, 132 { 133 .name = "csky", 134 .init = csky__annotate_init, 135 }, 136 { 137 .name = "mips", 138 .init = mips__annotate_init, 139 .objdump = { 140 .comment_char = '#', 141 }, 142 }, 143 { 144 .name = "x86", 145 .init = x86__annotate_init, 146 .instructions = x86__instructions, 147 .nr_instructions = ARRAY_SIZE(x86__instructions), 148 .insn_suffix = "bwlq", 149 .objdump = { 150 .comment_char = '#', 151 .register_char = '%', 152 .memory_ref_char = '(', 153 .imm_char = '$', 154 }, 155 #ifdef HAVE_LIBDW_SUPPORT 156 .update_insn_state = update_insn_state_x86, 157 #endif 158 }, 159 { 160 .name = "powerpc", 161 .init = powerpc__annotate_init, 162 #ifdef HAVE_LIBDW_SUPPORT 163 .update_insn_state = update_insn_state_powerpc, 164 #endif 165 }, 166 { 167 .name = "riscv64", 168 .init = riscv64__annotate_init, 169 }, 170 { 171 .name = "s390", 172 .init = s390__annotate_init, 173 .objdump = { 174 .comment_char = '#', 175 }, 176 }, 177 { 178 .name = "sparc", 179 .init = sparc__annotate_init, 180 .objdump = { 181 .comment_char = '#', 182 }, 183 }, 184 { 185 .name = "loongarch", 186 .init = loongarch__annotate_init, 187 .objdump = { 188 .comment_char = '#', 189 }, 190 }, 191 }; 192 193 static int arch__key_cmp(const void *name, const void *archp) 194 { 195 const struct arch *arch = archp; 196 197 return strcmp(name, arch->name); 198 } 199 200 static int arch__cmp(const void *a, const void *b) 201 { 202 const struct arch *aa = a; 203 const struct arch *ab = b; 204 205 return strcmp(aa->name, ab->name); 206 } 207 208 static void arch__sort(void) 209 { 210 const int nmemb = ARRAY_SIZE(architectures); 211 212 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp); 213 } 214 215 struct arch *arch__find(const char *name) 216 { 217 const int nmemb = ARRAY_SIZE(architectures); 218 static bool sorted; 219 220 if (!sorted) { 221 arch__sort(); 222 sorted = true; 223 } 224 225 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp); 226 } 227 228 bool arch__is(struct arch *arch, const char *name) 229 { 230 return !strcmp(arch->name, name); 231 } 232 233 static void ins_ops__delete(struct ins_operands *ops) 234 { 235 if (ops == NULL) 236 return; 237 zfree(&ops->source.raw); 238 zfree(&ops->source.name); 239 zfree(&ops->target.raw); 240 zfree(&ops->target.name); 241 } 242 243 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size, 244 struct ins_operands *ops, int max_ins_name) 245 { 246 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw); 247 } 248 249 int ins__scnprintf(struct ins *ins, char *bf, size_t size, 250 struct ins_operands *ops, int max_ins_name) 251 { 252 if (ins->ops->scnprintf) 253 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name); 254 255 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 256 } 257 258 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2) 259 { 260 if (!arch || !arch->ins_is_fused) 261 return false; 262 263 return arch->ins_is_fused(arch, ins1, ins2); 264 } 265 266 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 267 struct disasm_line *dl __maybe_unused) 268 { 269 char *endptr, *tok, *name; 270 struct map *map = ms->map; 271 struct addr_map_symbol target = { 272 .ms = { .map = map, }, 273 }; 274 275 ops->target.addr = strtoull(ops->raw, &endptr, 16); 276 277 name = strchr(endptr, '<'); 278 if (name == NULL) 279 goto indirect_call; 280 281 name++; 282 283 if (arch->objdump.skip_functions_char && 284 strchr(name, arch->objdump.skip_functions_char)) 285 return -1; 286 287 tok = strchr(name, '>'); 288 if (tok == NULL) 289 return -1; 290 291 *tok = '\0'; 292 ops->target.name = strdup(name); 293 *tok = '>'; 294 295 if (ops->target.name == NULL) 296 return -1; 297 find_target: 298 target.addr = map__objdump_2mem(map, ops->target.addr); 299 300 if (maps__find_ams(ms->maps, &target) == 0 && 301 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 302 ops->target.sym = target.ms.sym; 303 304 return 0; 305 306 indirect_call: 307 tok = strchr(endptr, '*'); 308 if (tok != NULL) { 309 endptr++; 310 311 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx). 312 * Do not parse such instruction. */ 313 if (strstr(endptr, "(%r") == NULL) 314 ops->target.addr = strtoull(endptr, NULL, 16); 315 } 316 goto find_target; 317 } 318 319 static int call__scnprintf(struct ins *ins, char *bf, size_t size, 320 struct ins_operands *ops, int max_ins_name) 321 { 322 if (ops->target.sym) 323 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 324 325 if (ops->target.addr == 0) 326 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 327 328 if (ops->target.name) 329 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name); 330 331 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr); 332 } 333 334 static struct ins_ops call_ops = { 335 .parse = call__parse, 336 .scnprintf = call__scnprintf, 337 }; 338 339 bool ins__is_call(const struct ins *ins) 340 { 341 return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops; 342 } 343 344 /* 345 * Prevents from matching commas in the comment section, e.g.: 346 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast 347 * 348 * and skip comma as part of function arguments, e.g.: 349 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc> 350 */ 351 static inline const char *validate_comma(const char *c, struct ins_operands *ops) 352 { 353 if (ops->jump.raw_comment && c > ops->jump.raw_comment) 354 return NULL; 355 356 if (ops->jump.raw_func_start && c > ops->jump.raw_func_start) 357 return NULL; 358 359 return c; 360 } 361 362 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 363 struct disasm_line *dl __maybe_unused) 364 { 365 struct map *map = ms->map; 366 struct symbol *sym = ms->sym; 367 struct addr_map_symbol target = { 368 .ms = { .map = map, }, 369 }; 370 const char *c = strchr(ops->raw, ','); 371 u64 start, end; 372 373 ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char); 374 ops->jump.raw_func_start = strchr(ops->raw, '<'); 375 376 c = validate_comma(c, ops); 377 378 /* 379 * Examples of lines to parse for the _cpp_lex_token@@Base 380 * function: 381 * 382 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92> 383 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72> 384 * 385 * The first is a jump to an offset inside the same function, 386 * the second is to another function, i.e. that 0xa72 is an 387 * offset in the cpp_named_operator2name@@base function. 388 */ 389 /* 390 * skip over possible up to 2 operands to get to address, e.g.: 391 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0> 392 */ 393 if (c++ != NULL) { 394 ops->target.addr = strtoull(c, NULL, 16); 395 if (!ops->target.addr) { 396 c = strchr(c, ','); 397 c = validate_comma(c, ops); 398 if (c++ != NULL) 399 ops->target.addr = strtoull(c, NULL, 16); 400 } 401 } else { 402 ops->target.addr = strtoull(ops->raw, NULL, 16); 403 } 404 405 target.addr = map__objdump_2mem(map, ops->target.addr); 406 start = map__unmap_ip(map, sym->start); 407 end = map__unmap_ip(map, sym->end); 408 409 ops->target.outside = target.addr < start || target.addr > end; 410 411 /* 412 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program): 413 414 cpp_named_operator2name@@Base+0xa72 415 416 * Point to a place that is after the cpp_named_operator2name 417 * boundaries, i.e. in the ELF symbol table for cc1 418 * cpp_named_operator2name is marked as being 32-bytes long, but it in 419 * fact is much larger than that, so we seem to need a symbols__find() 420 * routine that looks for >= current->start and < next_symbol->start, 421 * possibly just for C++ objects? 422 * 423 * For now lets just make some progress by marking jumps to outside the 424 * current function as call like. 425 * 426 * Actual navigation will come next, with further understanding of how 427 * the symbol searching and disassembly should be done. 428 */ 429 if (maps__find_ams(ms->maps, &target) == 0 && 430 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr) 431 ops->target.sym = target.ms.sym; 432 433 if (!ops->target.outside) { 434 ops->target.offset = target.addr - start; 435 ops->target.offset_avail = true; 436 } else { 437 ops->target.offset_avail = false; 438 } 439 440 return 0; 441 } 442 443 static int jump__scnprintf(struct ins *ins, char *bf, size_t size, 444 struct ins_operands *ops, int max_ins_name) 445 { 446 const char *c; 447 448 if (!ops->target.addr || ops->target.offset < 0) 449 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 450 451 if (ops->target.outside && ops->target.sym != NULL) 452 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name); 453 454 c = strchr(ops->raw, ','); 455 c = validate_comma(c, ops); 456 457 if (c != NULL) { 458 const char *c2 = strchr(c + 1, ','); 459 460 c2 = validate_comma(c2, ops); 461 /* check for 3-op insn */ 462 if (c2 != NULL) 463 c = c2; 464 c++; 465 466 /* mirror arch objdump's space-after-comma style */ 467 if (*c == ' ') 468 c++; 469 } 470 471 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name, 472 ins->name, c ? c - ops->raw : 0, ops->raw, 473 ops->target.offset); 474 } 475 476 static void jump__delete(struct ins_operands *ops __maybe_unused) 477 { 478 /* 479 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the 480 * raw string, don't free them. 481 */ 482 } 483 484 static struct ins_ops jump_ops = { 485 .free = jump__delete, 486 .parse = jump__parse, 487 .scnprintf = jump__scnprintf, 488 }; 489 490 bool ins__is_jump(const struct ins *ins) 491 { 492 return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops; 493 } 494 495 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep) 496 { 497 char *endptr, *name, *t; 498 499 if (strstr(raw, "(%rip)") == NULL) 500 return 0; 501 502 *addrp = strtoull(comment, &endptr, 16); 503 if (endptr == comment) 504 return 0; 505 name = strchr(endptr, '<'); 506 if (name == NULL) 507 return -1; 508 509 name++; 510 511 t = strchr(name, '>'); 512 if (t == NULL) 513 return 0; 514 515 *t = '\0'; 516 *namep = strdup(name); 517 *t = '>'; 518 519 return 0; 520 } 521 522 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms, 523 struct disasm_line *dl __maybe_unused) 524 { 525 ops->locked.ops = zalloc(sizeof(*ops->locked.ops)); 526 if (ops->locked.ops == NULL) 527 return 0; 528 529 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0) 530 goto out_free_ops; 531 532 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name, 0); 533 534 if (ops->locked.ins.ops == NULL) 535 goto out_free_ops; 536 537 if (ops->locked.ins.ops->parse && 538 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms, NULL) < 0) 539 goto out_free_ops; 540 541 return 0; 542 543 out_free_ops: 544 zfree(&ops->locked.ops); 545 return 0; 546 } 547 548 static int lock__scnprintf(struct ins *ins, char *bf, size_t size, 549 struct ins_operands *ops, int max_ins_name) 550 { 551 int printed; 552 553 if (ops->locked.ins.ops == NULL) 554 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name); 555 556 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name); 557 return printed + ins__scnprintf(&ops->locked.ins, bf + printed, 558 size - printed, ops->locked.ops, max_ins_name); 559 } 560 561 static void lock__delete(struct ins_operands *ops) 562 { 563 struct ins *ins = &ops->locked.ins; 564 565 if (ins->ops && ins->ops->free) 566 ins->ops->free(ops->locked.ops); 567 else 568 ins_ops__delete(ops->locked.ops); 569 570 zfree(&ops->locked.ops); 571 zfree(&ops->locked.ins.name); 572 zfree(&ops->target.raw); 573 zfree(&ops->target.name); 574 } 575 576 static struct ins_ops lock_ops = { 577 .free = lock__delete, 578 .parse = lock__parse, 579 .scnprintf = lock__scnprintf, 580 }; 581 582 /* 583 * Check if the operand has more than one registers like x86 SIB addressing: 584 * 0x1234(%rax, %rbx, 8) 585 * 586 * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check 587 * the input string after 'memory_ref_char' if exists. 588 */ 589 static bool check_multi_regs(struct arch *arch, const char *op) 590 { 591 int count = 0; 592 593 if (arch->objdump.register_char == 0) 594 return false; 595 596 if (arch->objdump.memory_ref_char) { 597 op = strchr(op, arch->objdump.memory_ref_char); 598 if (op == NULL) 599 return false; 600 } 601 602 while ((op = strchr(op, arch->objdump.register_char)) != NULL) { 603 count++; 604 op++; 605 } 606 607 return count > 1; 608 } 609 610 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused, 611 struct disasm_line *dl __maybe_unused) 612 { 613 char *s = strchr(ops->raw, ','), *target, *comment, prev; 614 615 if (s == NULL) 616 return -1; 617 618 *s = '\0'; 619 620 /* 621 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1) 622 * then it needs to have the closing parenthesis. 623 */ 624 if (strchr(ops->raw, '(')) { 625 *s = ','; 626 s = strchr(ops->raw, ')'); 627 if (s == NULL || s[1] != ',') 628 return -1; 629 *++s = '\0'; 630 } 631 632 ops->source.raw = strdup(ops->raw); 633 *s = ','; 634 635 if (ops->source.raw == NULL) 636 return -1; 637 638 ops->source.multi_regs = check_multi_regs(arch, ops->source.raw); 639 640 target = skip_spaces(++s); 641 comment = strchr(s, arch->objdump.comment_char); 642 643 if (comment != NULL) 644 s = comment - 1; 645 else 646 s = strchr(s, '\0') - 1; 647 648 while (s > target && isspace(s[0])) 649 --s; 650 s++; 651 prev = *s; 652 *s = '\0'; 653 654 ops->target.raw = strdup(target); 655 *s = prev; 656 657 if (ops->target.raw == NULL) 658 goto out_free_source; 659 660 ops->target.multi_regs = check_multi_regs(arch, ops->target.raw); 661 662 if (comment == NULL) 663 return 0; 664 665 comment = skip_spaces(comment); 666 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name); 667 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 668 669 return 0; 670 671 out_free_source: 672 zfree(&ops->source.raw); 673 return -1; 674 } 675 676 static int mov__scnprintf(struct ins *ins, char *bf, size_t size, 677 struct ins_operands *ops, int max_ins_name) 678 { 679 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name, 680 ops->source.name ?: ops->source.raw, 681 ops->target.name ?: ops->target.raw); 682 } 683 684 static struct ins_ops mov_ops = { 685 .parse = mov__parse, 686 .scnprintf = mov__scnprintf, 687 }; 688 689 #define PPC_22_30(R) (((R) >> 1) & 0x1ff) 690 #define MINUS_EXT_XO_FORM 234 691 #define SUB_EXT_XO_FORM 232 692 #define ADD_ZERO_EXT_XO_FORM 202 693 #define SUB_ZERO_EXT_XO_FORM 200 694 695 static int arithmetic__scnprintf(struct ins *ins, char *bf, size_t size, 696 struct ins_operands *ops, int max_ins_name) 697 { 698 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 699 ops->raw); 700 } 701 702 /* 703 * Sets the fields: multi_regs and "mem_ref". 704 * "mem_ref" is set for ops->source which is later used to 705 * fill the objdump->memory_ref-char field. This ops is currently 706 * used by powerpc and since binary instruction code is used to 707 * extract opcode, regs and offset, no other parsing is needed here. 708 * 709 * Dont set multi regs for 4 cases since it has only one operand 710 * for source: 711 * - Add to Minus One Extended XO-form ( Ex: addme, addmeo ) 712 * - Subtract From Minus One Extended XO-form ( Ex: subfme ) 713 * - Add to Zero Extended XO-form ( Ex: addze, addzeo ) 714 * - Subtract From Zero Extended XO-form ( Ex: subfze ) 715 */ 716 static int arithmetic__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, 717 struct map_symbol *ms __maybe_unused, struct disasm_line *dl) 718 { 719 int opcode = PPC_OP(dl->raw.raw_insn); 720 721 ops->source.mem_ref = false; 722 if (opcode == 31) { 723 if ((opcode != MINUS_EXT_XO_FORM) && (opcode != SUB_EXT_XO_FORM) \ 724 && (opcode != ADD_ZERO_EXT_XO_FORM) && (opcode != SUB_ZERO_EXT_XO_FORM)) 725 ops->source.multi_regs = true; 726 } 727 728 ops->target.mem_ref = false; 729 ops->target.multi_regs = false; 730 731 return 0; 732 } 733 734 static struct ins_ops arithmetic_ops = { 735 .parse = arithmetic__parse, 736 .scnprintf = arithmetic__scnprintf, 737 }; 738 739 static int load_store__scnprintf(struct ins *ins, char *bf, size_t size, 740 struct ins_operands *ops, int max_ins_name) 741 { 742 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 743 ops->raw); 744 } 745 746 /* 747 * Sets the fields: multi_regs and "mem_ref". 748 * "mem_ref" is set for ops->source which is later used to 749 * fill the objdump->memory_ref-char field. This ops is currently 750 * used by powerpc and since binary instruction code is used to 751 * extract opcode, regs and offset, no other parsing is needed here 752 */ 753 static int load_store__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, 754 struct map_symbol *ms __maybe_unused, struct disasm_line *dl __maybe_unused) 755 { 756 ops->source.mem_ref = true; 757 ops->source.multi_regs = false; 758 /* opcode 31 is of X form */ 759 if (PPC_OP(dl->raw.raw_insn) == 31) 760 ops->source.multi_regs = true; 761 762 ops->target.mem_ref = false; 763 ops->target.multi_regs = false; 764 765 return 0; 766 } 767 768 static struct ins_ops load_store_ops = { 769 .parse = load_store__parse, 770 .scnprintf = load_store__scnprintf, 771 }; 772 773 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused, 774 struct disasm_line *dl __maybe_unused) 775 { 776 char *target, *comment, *s, prev; 777 778 target = s = ops->raw; 779 780 while (s[0] != '\0' && !isspace(s[0])) 781 ++s; 782 prev = *s; 783 *s = '\0'; 784 785 ops->target.raw = strdup(target); 786 *s = prev; 787 788 if (ops->target.raw == NULL) 789 return -1; 790 791 comment = strchr(s, arch->objdump.comment_char); 792 if (comment == NULL) 793 return 0; 794 795 comment = skip_spaces(comment); 796 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name); 797 798 return 0; 799 } 800 801 static int dec__scnprintf(struct ins *ins, char *bf, size_t size, 802 struct ins_operands *ops, int max_ins_name) 803 { 804 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, 805 ops->target.name ?: ops->target.raw); 806 } 807 808 static struct ins_ops dec_ops = { 809 .parse = dec__parse, 810 .scnprintf = dec__scnprintf, 811 }; 812 813 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size, 814 struct ins_operands *ops __maybe_unused, int max_ins_name) 815 { 816 return scnprintf(bf, size, "%-*s", max_ins_name, "nop"); 817 } 818 819 static struct ins_ops nop_ops = { 820 .scnprintf = nop__scnprintf, 821 }; 822 823 static struct ins_ops ret_ops = { 824 .scnprintf = ins__raw_scnprintf, 825 }; 826 827 bool ins__is_nop(const struct ins *ins) 828 { 829 return ins->ops == &nop_ops; 830 } 831 832 bool ins__is_ret(const struct ins *ins) 833 { 834 return ins->ops == &ret_ops; 835 } 836 837 bool ins__is_lock(const struct ins *ins) 838 { 839 return ins->ops == &lock_ops; 840 } 841 842 static int ins__key_cmp(const void *name, const void *insp) 843 { 844 const struct ins *ins = insp; 845 846 return strcmp(name, ins->name); 847 } 848 849 static int ins__cmp(const void *a, const void *b) 850 { 851 const struct ins *ia = a; 852 const struct ins *ib = b; 853 854 return strcmp(ia->name, ib->name); 855 } 856 857 static void ins__sort(struct arch *arch) 858 { 859 const int nmemb = arch->nr_instructions; 860 861 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp); 862 } 863 864 static struct ins_ops *__ins__find(struct arch *arch, const char *name, struct disasm_line *dl) 865 { 866 struct ins *ins; 867 const int nmemb = arch->nr_instructions; 868 869 if (arch__is(arch, "powerpc")) { 870 /* 871 * For powerpc, identify the instruction ops 872 * from the opcode using raw_insn. 873 */ 874 struct ins_ops *ops; 875 876 ops = check_ppc_insn(dl); 877 if (ops) 878 return ops; 879 } 880 881 if (!arch->sorted_instructions) { 882 ins__sort(arch); 883 arch->sorted_instructions = true; 884 } 885 886 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 887 if (ins) 888 return ins->ops; 889 890 if (arch->insn_suffix) { 891 char tmp[32]; 892 char suffix; 893 size_t len = strlen(name); 894 895 if (len == 0 || len >= sizeof(tmp)) 896 return NULL; 897 898 suffix = name[len - 1]; 899 if (strchr(arch->insn_suffix, suffix) == NULL) 900 return NULL; 901 902 strcpy(tmp, name); 903 tmp[len - 1] = '\0'; /* remove the suffix and check again */ 904 905 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp); 906 } 907 return ins ? ins->ops : NULL; 908 } 909 910 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl) 911 { 912 struct ins_ops *ops = __ins__find(arch, name, dl); 913 914 if (!ops && arch->associate_instruction_ops) 915 ops = arch->associate_instruction_ops(arch, name); 916 917 return ops; 918 } 919 920 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms) 921 { 922 dl->ins.ops = ins__find(arch, dl->ins.name, dl); 923 924 if (!dl->ins.ops) 925 return; 926 927 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms, dl) < 0) 928 dl->ins.ops = NULL; 929 } 930 931 static int disasm_line__parse(char *line, const char **namep, char **rawp) 932 { 933 char tmp, *name = skip_spaces(line); 934 935 if (name[0] == '\0') 936 return -1; 937 938 *rawp = name + 1; 939 940 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0])) 941 ++*rawp; 942 943 tmp = (*rawp)[0]; 944 (*rawp)[0] = '\0'; 945 *namep = strdup(name); 946 947 if (*namep == NULL) 948 goto out; 949 950 (*rawp)[0] = tmp; 951 *rawp = strim(*rawp); 952 953 return 0; 954 955 out: 956 return -1; 957 } 958 959 /* 960 * Parses the result captured from symbol__disassemble_* 961 * Example, line read from DSO file in powerpc: 962 * line: 38 01 81 e8 963 * opcode: fetched from arch specific get_opcode_insn 964 * rawp_insn: e8810138 965 * 966 * rawp_insn is used later to extract the reg/offset fields 967 */ 968 #define PPC_OP(op) (((op) >> 26) & 0x3F) 969 #define RAW_BYTES 11 970 971 static int disasm_line__parse_powerpc(struct disasm_line *dl) 972 { 973 char *line = dl->al.line; 974 const char **namep = &dl->ins.name; 975 char **rawp = &dl->ops.raw; 976 char *tmp_raw_insn, *name_raw_insn = skip_spaces(line); 977 char *name = skip_spaces(name_raw_insn + RAW_BYTES); 978 int objdump = 0; 979 980 if (strlen(line) > RAW_BYTES) 981 objdump = 1; 982 983 if (name_raw_insn[0] == '\0') 984 return -1; 985 986 if (objdump) { 987 disasm_line__parse(name, namep, rawp); 988 } else 989 *namep = ""; 990 991 tmp_raw_insn = strndup(name_raw_insn, 11); 992 if (tmp_raw_insn == NULL) 993 return -1; 994 995 remove_spaces(tmp_raw_insn); 996 997 sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn); 998 if (objdump) 999 dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn); 1000 1001 return 0; 1002 } 1003 1004 static void annotation_line__init(struct annotation_line *al, 1005 struct annotate_args *args, 1006 int nr) 1007 { 1008 al->offset = args->offset; 1009 al->line = strdup(args->line); 1010 al->line_nr = args->line_nr; 1011 al->fileloc = args->fileloc; 1012 al->data_nr = nr; 1013 } 1014 1015 static void annotation_line__exit(struct annotation_line *al) 1016 { 1017 zfree_srcline(&al->path); 1018 zfree(&al->line); 1019 zfree(&al->cycles); 1020 zfree(&al->br_cntr); 1021 } 1022 1023 static size_t disasm_line_size(int nr) 1024 { 1025 struct annotation_line *al; 1026 1027 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr)); 1028 } 1029 1030 /* 1031 * Allocating the disasm annotation line data with 1032 * following structure: 1033 * 1034 * ------------------------------------------- 1035 * struct disasm_line | struct annotation_line 1036 * ------------------------------------------- 1037 * 1038 * We have 'struct annotation_line' member as last member 1039 * of 'struct disasm_line' to have an easy access. 1040 */ 1041 struct disasm_line *disasm_line__new(struct annotate_args *args) 1042 { 1043 struct disasm_line *dl = NULL; 1044 struct annotation *notes = symbol__annotation(args->ms.sym); 1045 int nr = notes->src->nr_events; 1046 1047 dl = zalloc(disasm_line_size(nr)); 1048 if (!dl) 1049 return NULL; 1050 1051 annotation_line__init(&dl->al, args, nr); 1052 if (dl->al.line == NULL) 1053 goto out_delete; 1054 1055 if (args->offset != -1) { 1056 if (arch__is(args->arch, "powerpc")) { 1057 if (disasm_line__parse_powerpc(dl) < 0) 1058 goto out_free_line; 1059 } else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1060 goto out_free_line; 1061 1062 disasm_line__init_ins(dl, args->arch, &args->ms); 1063 } 1064 1065 return dl; 1066 1067 out_free_line: 1068 zfree(&dl->al.line); 1069 out_delete: 1070 free(dl); 1071 return NULL; 1072 } 1073 1074 void disasm_line__free(struct disasm_line *dl) 1075 { 1076 if (dl->ins.ops && dl->ins.ops->free) 1077 dl->ins.ops->free(&dl->ops); 1078 else 1079 ins_ops__delete(&dl->ops); 1080 zfree(&dl->ins.name); 1081 annotation_line__exit(&dl->al); 1082 free(dl); 1083 } 1084 1085 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name) 1086 { 1087 if (raw || !dl->ins.ops) 1088 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw); 1089 1090 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name); 1091 } 1092 1093 /* 1094 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1095 * which looks like following 1096 * 1097 * 0000000000415500 <_init>: 1098 * 415500: sub $0x8,%rsp 1099 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1100 * 41550b: test %rax,%rax 1101 * 41550e: je 415515 <_init+0x15> 1102 * 415510: callq 416e70 <__gmon_start__@plt> 1103 * 415515: add $0x8,%rsp 1104 * 415519: retq 1105 * 1106 * it will be parsed and saved into struct disasm_line as 1107 * <offset> <name> <ops.raw> 1108 * 1109 * The offset will be a relative offset from the start of the symbol and -1 1110 * means that it's not a disassembly line so should be treated differently. 1111 * The ops.raw part will be parsed further according to type of the instruction. 1112 */ 1113 static int symbol__parse_objdump_line(struct symbol *sym, 1114 struct annotate_args *args, 1115 char *parsed_line, int *line_nr, char **fileloc) 1116 { 1117 struct map *map = args->ms.map; 1118 struct annotation *notes = symbol__annotation(sym); 1119 struct disasm_line *dl; 1120 char *tmp; 1121 s64 line_ip, offset = -1; 1122 regmatch_t match[2]; 1123 1124 /* /filename:linenr ? Save line number and ignore. */ 1125 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1126 *line_nr = atoi(parsed_line + match[1].rm_so); 1127 free(*fileloc); 1128 *fileloc = strdup(parsed_line); 1129 return 0; 1130 } 1131 1132 /* Process hex address followed by ':'. */ 1133 line_ip = strtoull(parsed_line, &tmp, 16); 1134 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') { 1135 u64 start = map__rip_2objdump(map, sym->start), 1136 end = map__rip_2objdump(map, sym->end); 1137 1138 offset = line_ip - start; 1139 if ((u64)line_ip < start || (u64)line_ip >= end) 1140 offset = -1; 1141 else 1142 parsed_line = tmp + 1; 1143 } 1144 1145 args->offset = offset; 1146 args->line = parsed_line; 1147 args->line_nr = *line_nr; 1148 args->fileloc = *fileloc; 1149 args->ms.sym = sym; 1150 1151 dl = disasm_line__new(args); 1152 (*line_nr)++; 1153 1154 if (dl == NULL) 1155 return -1; 1156 1157 if (!disasm_line__has_local_offset(dl)) { 1158 dl->ops.target.offset = dl->ops.target.addr - 1159 map__rip_2objdump(map, sym->start); 1160 dl->ops.target.offset_avail = true; 1161 } 1162 1163 /* kcore has no symbols, so add the call target symbol */ 1164 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1165 struct addr_map_symbol target = { 1166 .addr = dl->ops.target.addr, 1167 .ms = { .map = map, }, 1168 }; 1169 1170 if (!maps__find_ams(args->ms.maps, &target) && 1171 target.ms.sym->start == target.al_addr) 1172 dl->ops.target.sym = target.ms.sym; 1173 } 1174 1175 annotation_line__add(&dl->al, ¬es->src->source); 1176 return 0; 1177 } 1178 1179 static void delete_last_nop(struct symbol *sym) 1180 { 1181 struct annotation *notes = symbol__annotation(sym); 1182 struct list_head *list = ¬es->src->source; 1183 struct disasm_line *dl; 1184 1185 while (!list_empty(list)) { 1186 dl = list_entry(list->prev, struct disasm_line, al.node); 1187 1188 if (dl->ins.ops) { 1189 if (!ins__is_nop(&dl->ins)) 1190 return; 1191 } else { 1192 if (!strstr(dl->al.line, " nop ") && 1193 !strstr(dl->al.line, " nopl ") && 1194 !strstr(dl->al.line, " nopw ")) 1195 return; 1196 } 1197 1198 list_del_init(&dl->al.node); 1199 disasm_line__free(dl); 1200 } 1201 } 1202 1203 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen) 1204 { 1205 struct dso *dso = map__dso(ms->map); 1206 1207 BUG_ON(buflen == 0); 1208 1209 if (errnum >= 0) { 1210 str_error_r(errnum, buf, buflen); 1211 return 0; 1212 } 1213 1214 switch (errnum) { 1215 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1216 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1217 char *build_id_msg = NULL; 1218 1219 if (dso__has_build_id(dso)) { 1220 build_id__sprintf(dso__bid(dso), bf + 15); 1221 build_id_msg = bf; 1222 } 1223 scnprintf(buf, buflen, 1224 "No vmlinux file%s\nwas found in the path.\n\n" 1225 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1226 "Please use:\n\n" 1227 " perf buildid-cache -vu vmlinux\n\n" 1228 "or:\n\n" 1229 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1230 } 1231 break; 1232 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: 1233 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation"); 1234 break; 1235 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: 1236 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions."); 1237 break; 1238 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING: 1239 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization."); 1240 break; 1241 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE: 1242 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso)); 1243 break; 1244 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF: 1245 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.", 1246 dso__long_name(dso)); 1247 break; 1248 case SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE: 1249 scnprintf(buf, buflen, "Couldn't determine the file %s type.", dso__long_name(dso)); 1250 break; 1251 default: 1252 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1253 break; 1254 } 1255 1256 return 0; 1257 } 1258 1259 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1260 { 1261 char linkname[PATH_MAX]; 1262 char *build_id_filename; 1263 char *build_id_path = NULL; 1264 char *pos; 1265 int len; 1266 1267 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && 1268 !dso__is_kcore(dso)) 1269 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1270 1271 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1272 if (build_id_filename) { 1273 __symbol__join_symfs(filename, filename_size, build_id_filename); 1274 free(build_id_filename); 1275 } else { 1276 if (dso__has_build_id(dso)) 1277 return ENOMEM; 1278 goto fallback; 1279 } 1280 1281 build_id_path = strdup(filename); 1282 if (!build_id_path) 1283 return ENOMEM; 1284 1285 /* 1286 * old style build-id cache has name of XX/XXXXXXX.. while 1287 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1288 * extract the build-id part of dirname in the new style only. 1289 */ 1290 pos = strrchr(build_id_path, '/'); 1291 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1292 dirname(build_id_path); 1293 1294 if (dso__is_kcore(dso)) 1295 goto fallback; 1296 1297 len = readlink(build_id_path, linkname, sizeof(linkname) - 1); 1298 if (len < 0) 1299 goto fallback; 1300 1301 linkname[len] = '\0'; 1302 if (strstr(linkname, DSO__NAME_KALLSYMS) || 1303 access(filename, R_OK)) { 1304 fallback: 1305 /* 1306 * If we don't have build-ids or the build-id file isn't in the 1307 * cache, or is just a kallsyms file, well, lets hope that this 1308 * DSO is the same as when 'perf record' ran. 1309 */ 1310 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/') 1311 snprintf(filename, filename_size, "%s", dso__long_name(dso)); 1312 else 1313 __symbol__join_symfs(filename, filename_size, dso__long_name(dso)); 1314 1315 mutex_lock(dso__lock(dso)); 1316 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) { 1317 char *new_name = dso__filename_with_chroot(dso, filename); 1318 if (new_name) { 1319 strlcpy(filename, new_name, filename_size); 1320 free(new_name); 1321 } 1322 } 1323 mutex_unlock(dso__lock(dso)); 1324 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 1325 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); 1326 } 1327 1328 free(build_id_path); 1329 return 0; 1330 } 1331 1332 #ifdef HAVE_LIBCAPSTONE_SUPPORT 1333 #include <capstone/capstone.h> 1334 1335 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style); 1336 1337 static int open_capstone_handle(struct annotate_args *args, bool is_64bit, 1338 csh *handle) 1339 { 1340 struct annotation_options *opt = args->options; 1341 cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32; 1342 1343 /* TODO: support more architectures */ 1344 if (!arch__is(args->arch, "x86")) 1345 return -1; 1346 1347 if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK) 1348 return -1; 1349 1350 if (!opt->disassembler_style || 1351 !strcmp(opt->disassembler_style, "att")) 1352 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); 1353 1354 /* 1355 * Resolving address operands to symbols is implemented 1356 * on x86 by investigating instruction details. 1357 */ 1358 cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON); 1359 1360 return 0; 1361 } 1362 #endif 1363 1364 #if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT) 1365 struct find_file_offset_data { 1366 u64 ip; 1367 u64 offset; 1368 }; 1369 1370 /* This will be called for each PHDR in an ELF binary */ 1371 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) 1372 { 1373 struct find_file_offset_data *data = arg; 1374 1375 if (start <= data->ip && data->ip < start + len) { 1376 data->offset = pgoff + data->ip - start; 1377 return 1; 1378 } 1379 return 0; 1380 } 1381 1382 static u8 * 1383 read_symbol(const char *filename, struct map *map, struct symbol *sym, 1384 u64 *len, bool *is_64bit) 1385 { 1386 struct dso *dso = map__dso(map); 1387 struct nscookie nsc; 1388 u64 start = map__rip_2objdump(map, sym->start); 1389 u64 end = map__rip_2objdump(map, sym->end); 1390 int fd, count; 1391 u8 *buf = NULL; 1392 struct find_file_offset_data data = { 1393 .ip = start, 1394 }; 1395 1396 *is_64bit = false; 1397 1398 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1399 fd = open(filename, O_RDONLY); 1400 nsinfo__mountns_exit(&nsc); 1401 if (fd < 0) 1402 return NULL; 1403 1404 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, 1405 is_64bit) == 0) 1406 goto err; 1407 1408 *len = end - start; 1409 buf = malloc(*len); 1410 if (buf == NULL) 1411 goto err; 1412 1413 count = pread(fd, buf, *len, data.offset); 1414 close(fd); 1415 fd = -1; 1416 1417 if ((u64)count != *len) 1418 goto err; 1419 1420 return buf; 1421 1422 err: 1423 if (fd >= 0) 1424 close(fd); 1425 free(buf); 1426 return NULL; 1427 } 1428 #endif 1429 1430 #if !defined(HAVE_LIBCAPSTONE_SUPPORT) || !defined(HAVE_LIBLLVM_SUPPORT) 1431 static void symbol__disassembler_missing(const char *disassembler, const char *filename, 1432 struct symbol *sym) 1433 { 1434 pr_debug("The %s disassembler isn't linked in for %s in %s\n", 1435 disassembler, sym->name, filename); 1436 } 1437 #endif 1438 1439 #ifdef HAVE_LIBCAPSTONE_SUPPORT 1440 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, 1441 struct annotate_args *args, u64 addr) 1442 { 1443 int i; 1444 struct map *map = args->ms.map; 1445 struct symbol *sym; 1446 1447 /* TODO: support more architectures */ 1448 if (!arch__is(args->arch, "x86")) 1449 return; 1450 1451 if (insn->detail == NULL) 1452 return; 1453 1454 for (i = 0; i < insn->detail->x86.op_count; i++) { 1455 cs_x86_op *op = &insn->detail->x86.operands[i]; 1456 u64 orig_addr; 1457 1458 if (op->type != X86_OP_MEM) 1459 continue; 1460 1461 /* only print RIP-based global symbols for now */ 1462 if (op->mem.base != X86_REG_RIP) 1463 continue; 1464 1465 /* get the target address */ 1466 orig_addr = addr + insn->size + op->mem.disp; 1467 addr = map__objdump_2mem(map, orig_addr); 1468 1469 if (dso__kernel(map__dso(map))) { 1470 /* 1471 * The kernel maps can be splitted into sections, 1472 * let's find the map first and the search the symbol. 1473 */ 1474 map = maps__find(map__kmaps(map), addr); 1475 if (map == NULL) 1476 continue; 1477 } 1478 1479 /* convert it to map-relative address for search */ 1480 addr = map__map_ip(map, addr); 1481 1482 sym = map__find_symbol(map, addr); 1483 if (sym == NULL) 1484 continue; 1485 1486 if (addr == sym->start) { 1487 scnprintf(buf, len, "\t# %"PRIx64" <%s>", 1488 orig_addr, sym->name); 1489 } else { 1490 scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">", 1491 orig_addr, sym->name, addr - sym->start); 1492 } 1493 break; 1494 } 1495 } 1496 1497 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym, 1498 struct annotate_args *args) 1499 { 1500 struct annotation *notes = symbol__annotation(sym); 1501 struct map *map = args->ms.map; 1502 struct dso *dso = map__dso(map); 1503 struct nscookie nsc; 1504 u64 start = map__rip_2objdump(map, sym->start); 1505 u64 end = map__rip_2objdump(map, sym->end); 1506 u64 len = end - start; 1507 u64 offset; 1508 int i, fd, count; 1509 bool is_64bit = false; 1510 bool needs_cs_close = false; 1511 u8 *buf = NULL; 1512 struct find_file_offset_data data = { 1513 .ip = start, 1514 }; 1515 csh handle; 1516 char disasm_buf[512]; 1517 struct disasm_line *dl; 1518 u32 *line; 1519 bool disassembler_style = false; 1520 1521 if (args->options->objdump_path) 1522 return -1; 1523 1524 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1525 fd = open(filename, O_RDONLY); 1526 nsinfo__mountns_exit(&nsc); 1527 if (fd < 0) 1528 return -1; 1529 1530 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, 1531 &is_64bit) == 0) 1532 goto err; 1533 1534 if (!args->options->disassembler_style || 1535 !strcmp(args->options->disassembler_style, "att")) 1536 disassembler_style = true; 1537 1538 if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0) 1539 goto err; 1540 1541 needs_cs_close = true; 1542 1543 buf = malloc(len); 1544 if (buf == NULL) 1545 goto err; 1546 1547 count = pread(fd, buf, len, data.offset); 1548 close(fd); 1549 fd = -1; 1550 1551 if ((u64)count != len) 1552 goto err; 1553 1554 line = (u32 *)buf; 1555 1556 /* add the function address and name */ 1557 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1558 start, sym->name); 1559 1560 args->offset = -1; 1561 args->line = disasm_buf; 1562 args->line_nr = 0; 1563 args->fileloc = NULL; 1564 args->ms.sym = sym; 1565 1566 dl = disasm_line__new(args); 1567 if (dl == NULL) 1568 goto err; 1569 1570 annotation_line__add(&dl->al, ¬es->src->source); 1571 1572 /* 1573 * TODO: enable disassm for powerpc 1574 * count = cs_disasm(handle, buf, len, start, len, &insn); 1575 * 1576 * For now, only binary code is saved in disassembled line 1577 * to be used in "type" and "typeoff" sort keys. Each raw code 1578 * is 32 bit instruction. So use "len/4" to get the number of 1579 * entries. 1580 */ 1581 count = len/4; 1582 1583 for (i = 0, offset = 0; i < count; i++) { 1584 args->offset = offset; 1585 sprintf(args->line, "%x", line[i]); 1586 1587 dl = disasm_line__new(args); 1588 if (dl == NULL) 1589 break; 1590 1591 annotation_line__add(&dl->al, ¬es->src->source); 1592 1593 offset += 4; 1594 } 1595 1596 /* It failed in the middle */ 1597 if (offset != len) { 1598 struct list_head *list = ¬es->src->source; 1599 1600 /* Discard all lines and fallback to objdump */ 1601 while (!list_empty(list)) { 1602 dl = list_first_entry(list, struct disasm_line, al.node); 1603 1604 list_del_init(&dl->al.node); 1605 disasm_line__free(dl); 1606 } 1607 count = -1; 1608 } 1609 1610 out: 1611 if (needs_cs_close) 1612 cs_close(&handle); 1613 free(buf); 1614 return count < 0 ? count : 0; 1615 1616 err: 1617 if (fd >= 0) 1618 close(fd); 1619 count = -1; 1620 goto out; 1621 } 1622 1623 static int symbol__disassemble_capstone(char *filename, struct symbol *sym, 1624 struct annotate_args *args) 1625 { 1626 struct annotation *notes = symbol__annotation(sym); 1627 struct map *map = args->ms.map; 1628 u64 start = map__rip_2objdump(map, sym->start); 1629 u64 len; 1630 u64 offset; 1631 int i, count, free_count; 1632 bool is_64bit = false; 1633 bool needs_cs_close = false; 1634 u8 *buf = NULL; 1635 csh handle; 1636 cs_insn *insn = NULL; 1637 char disasm_buf[512]; 1638 struct disasm_line *dl; 1639 1640 if (args->options->objdump_path) 1641 return -1; 1642 1643 buf = read_symbol(filename, map, sym, &len, &is_64bit); 1644 if (buf == NULL) 1645 return -1; 1646 1647 /* add the function address and name */ 1648 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1649 start, sym->name); 1650 1651 args->offset = -1; 1652 args->line = disasm_buf; 1653 args->line_nr = 0; 1654 args->fileloc = NULL; 1655 args->ms.sym = sym; 1656 1657 dl = disasm_line__new(args); 1658 if (dl == NULL) 1659 goto err; 1660 1661 annotation_line__add(&dl->al, ¬es->src->source); 1662 1663 if (open_capstone_handle(args, is_64bit, &handle) < 0) 1664 goto err; 1665 1666 needs_cs_close = true; 1667 1668 free_count = count = cs_disasm(handle, buf, len, start, len, &insn); 1669 for (i = 0, offset = 0; i < count; i++) { 1670 int printed; 1671 1672 printed = scnprintf(disasm_buf, sizeof(disasm_buf), 1673 " %-7s %s", 1674 insn[i].mnemonic, insn[i].op_str); 1675 print_capstone_detail(&insn[i], disasm_buf + printed, 1676 sizeof(disasm_buf) - printed, args, 1677 start + offset); 1678 1679 args->offset = offset; 1680 args->line = disasm_buf; 1681 1682 dl = disasm_line__new(args); 1683 if (dl == NULL) 1684 goto err; 1685 1686 annotation_line__add(&dl->al, ¬es->src->source); 1687 1688 offset += insn[i].size; 1689 } 1690 1691 /* It failed in the middle: probably due to unknown instructions */ 1692 if (offset != len) { 1693 struct list_head *list = ¬es->src->source; 1694 1695 /* Discard all lines and fallback to objdump */ 1696 while (!list_empty(list)) { 1697 dl = list_first_entry(list, struct disasm_line, al.node); 1698 1699 list_del_init(&dl->al.node); 1700 disasm_line__free(dl); 1701 } 1702 count = -1; 1703 } 1704 1705 out: 1706 if (needs_cs_close) { 1707 cs_close(&handle); 1708 if (free_count > 0) 1709 cs_free(insn, free_count); 1710 } 1711 free(buf); 1712 return count < 0 ? count : 0; 1713 1714 err: 1715 if (needs_cs_close) { 1716 struct disasm_line *tmp; 1717 1718 /* 1719 * It probably failed in the middle of the above loop. 1720 * Release any resources it might add. 1721 */ 1722 list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) { 1723 list_del(&dl->al.node); 1724 disasm_line__free(dl); 1725 } 1726 } 1727 count = -1; 1728 goto out; 1729 } 1730 #else // HAVE_LIBCAPSTONE_SUPPORT 1731 static int symbol__disassemble_capstone(char *filename, struct symbol *sym, 1732 struct annotate_args *args __maybe_unused) 1733 { 1734 symbol__disassembler_missing("capstone", filename, sym); 1735 return -1; 1736 } 1737 1738 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym, 1739 struct annotate_args *args __maybe_unused) 1740 { 1741 symbol__disassembler_missing("capstone powerpc", filename, sym); 1742 return -1; 1743 } 1744 #endif // HAVE_LIBCAPSTONE_SUPPORT 1745 1746 static int symbol__disassemble_raw(char *filename, struct symbol *sym, 1747 struct annotate_args *args) 1748 { 1749 struct annotation *notes = symbol__annotation(sym); 1750 struct map *map = args->ms.map; 1751 struct dso *dso = map__dso(map); 1752 u64 start = map__rip_2objdump(map, sym->start); 1753 u64 end = map__rip_2objdump(map, sym->end); 1754 u64 len = end - start; 1755 u64 offset; 1756 int i, count; 1757 u8 *buf = NULL; 1758 char disasm_buf[512]; 1759 struct disasm_line *dl; 1760 u32 *line; 1761 1762 /* Return if objdump is specified explicitly */ 1763 if (args->options->objdump_path) 1764 return -1; 1765 1766 pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename); 1767 1768 buf = malloc(len); 1769 if (buf == NULL) 1770 goto err; 1771 1772 count = dso__data_read_offset(dso, NULL, sym->start, buf, len); 1773 1774 line = (u32 *)buf; 1775 1776 if ((u64)count != len) 1777 goto err; 1778 1779 /* add the function address and name */ 1780 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1781 start, sym->name); 1782 1783 args->offset = -1; 1784 args->line = disasm_buf; 1785 args->line_nr = 0; 1786 args->fileloc = NULL; 1787 args->ms.sym = sym; 1788 1789 dl = disasm_line__new(args); 1790 if (dl == NULL) 1791 goto err; 1792 1793 annotation_line__add(&dl->al, ¬es->src->source); 1794 1795 /* Each raw instruction is 4 byte */ 1796 count = len/4; 1797 1798 for (i = 0, offset = 0; i < count; i++) { 1799 args->offset = offset; 1800 sprintf(args->line, "%x", line[i]); 1801 dl = disasm_line__new(args); 1802 if (dl == NULL) 1803 break; 1804 1805 annotation_line__add(&dl->al, ¬es->src->source); 1806 offset += 4; 1807 } 1808 1809 /* It failed in the middle */ 1810 if (offset != len) { 1811 struct list_head *list = ¬es->src->source; 1812 1813 /* Discard all lines and fallback to objdump */ 1814 while (!list_empty(list)) { 1815 dl = list_first_entry(list, struct disasm_line, al.node); 1816 1817 list_del_init(&dl->al.node); 1818 disasm_line__free(dl); 1819 } 1820 count = -1; 1821 } 1822 1823 out: 1824 free(buf); 1825 return count < 0 ? count : 0; 1826 1827 err: 1828 count = -1; 1829 goto out; 1830 } 1831 1832 #ifdef HAVE_LIBLLVM_SUPPORT 1833 #include <llvm-c/Disassembler.h> 1834 #include <llvm-c/Target.h> 1835 #include "util/llvm-c-helpers.h" 1836 1837 struct symbol_lookup_storage { 1838 u64 branch_addr; 1839 u64 pcrel_load_addr; 1840 }; 1841 1842 /* 1843 * Whenever LLVM wants to resolve an address into a symbol, it calls this 1844 * callback. We don't ever actually _return_ anything (in particular, because 1845 * it puts quotation marks around what we return), but we use this as a hint 1846 * that there is a branch or PC-relative address in the expression that we 1847 * should add some textual annotation for after the instruction. The caller 1848 * will use this information to add the actual annotation. 1849 */ 1850 static const char * 1851 symbol_lookup_callback(void *disinfo, uint64_t value, 1852 uint64_t *ref_type, 1853 uint64_t address __maybe_unused, 1854 const char **ref __maybe_unused) 1855 { 1856 struct symbol_lookup_storage *storage = disinfo; 1857 1858 if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch) 1859 storage->branch_addr = value; 1860 else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load) 1861 storage->pcrel_load_addr = value; 1862 *ref_type = LLVMDisassembler_ReferenceType_InOut_None; 1863 return NULL; 1864 } 1865 1866 static int symbol__disassemble_llvm(char *filename, struct symbol *sym, 1867 struct annotate_args *args) 1868 { 1869 struct annotation *notes = symbol__annotation(sym); 1870 struct map *map = args->ms.map; 1871 struct dso *dso = map__dso(map); 1872 u64 start = map__rip_2objdump(map, sym->start); 1873 u8 *buf; 1874 u64 len; 1875 u64 pc; 1876 bool is_64bit; 1877 char triplet[64]; 1878 char disasm_buf[2048]; 1879 size_t disasm_len; 1880 struct disasm_line *dl; 1881 LLVMDisasmContextRef disasm = NULL; 1882 struct symbol_lookup_storage storage; 1883 char *line_storage = NULL; 1884 size_t line_storage_len = 0; 1885 int ret = -1; 1886 1887 if (args->options->objdump_path) 1888 return -1; 1889 1890 LLVMInitializeAllTargetInfos(); 1891 LLVMInitializeAllTargetMCs(); 1892 LLVMInitializeAllDisassemblers(); 1893 1894 buf = read_symbol(filename, map, sym, &len, &is_64bit); 1895 if (buf == NULL) 1896 return -1; 1897 1898 if (arch__is(args->arch, "x86")) { 1899 if (is_64bit) 1900 scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux"); 1901 else 1902 scnprintf(triplet, sizeof(triplet), "i686-pc-linux"); 1903 } else { 1904 scnprintf(triplet, sizeof(triplet), "%s-linux-gnu", 1905 args->arch->name); 1906 } 1907 1908 disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL, 1909 symbol_lookup_callback); 1910 if (disasm == NULL) 1911 goto err; 1912 1913 if (args->options->disassembler_style && 1914 !strcmp(args->options->disassembler_style, "intel")) 1915 LLVMSetDisasmOptions(disasm, 1916 LLVMDisassembler_Option_AsmPrinterVariant); 1917 1918 /* 1919 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM; 1920 * setting AsmPrinterVariant makes a new instruction printer, making it 1921 * forget about the PrintImmHex flag (which is applied before if both 1922 * are given to the same call). 1923 */ 1924 LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); 1925 1926 /* add the function address and name */ 1927 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1928 start, sym->name); 1929 1930 args->offset = -1; 1931 args->line = disasm_buf; 1932 args->line_nr = 0; 1933 args->fileloc = NULL; 1934 args->ms.sym = sym; 1935 1936 dl = disasm_line__new(args); 1937 if (dl == NULL) 1938 goto err; 1939 1940 annotation_line__add(&dl->al, ¬es->src->source); 1941 1942 pc = start; 1943 for (u64 offset = 0; offset < len; ) { 1944 unsigned int ins_len; 1945 1946 storage.branch_addr = 0; 1947 storage.pcrel_load_addr = 0; 1948 1949 ins_len = LLVMDisasmInstruction(disasm, buf + offset, 1950 len - offset, pc, 1951 disasm_buf, sizeof(disasm_buf)); 1952 if (ins_len == 0) 1953 goto err; 1954 disasm_len = strlen(disasm_buf); 1955 1956 if (storage.branch_addr != 0) { 1957 char *name = llvm_name_for_code(dso, filename, 1958 storage.branch_addr); 1959 if (name != NULL) { 1960 disasm_len += scnprintf(disasm_buf + disasm_len, 1961 sizeof(disasm_buf) - 1962 disasm_len, 1963 " <%s>", name); 1964 free(name); 1965 } 1966 } 1967 if (storage.pcrel_load_addr != 0) { 1968 char *name = llvm_name_for_data(dso, filename, 1969 storage.pcrel_load_addr); 1970 disasm_len += scnprintf(disasm_buf + disasm_len, 1971 sizeof(disasm_buf) - disasm_len, 1972 " # %#"PRIx64, 1973 storage.pcrel_load_addr); 1974 if (name) { 1975 disasm_len += scnprintf(disasm_buf + disasm_len, 1976 sizeof(disasm_buf) - 1977 disasm_len, 1978 " <%s>", name); 1979 free(name); 1980 } 1981 } 1982 1983 args->offset = offset; 1984 args->line = expand_tabs(disasm_buf, &line_storage, 1985 &line_storage_len); 1986 args->line_nr = 0; 1987 args->fileloc = NULL; 1988 args->ms.sym = sym; 1989 1990 llvm_addr2line(filename, pc, &args->fileloc, 1991 (unsigned int *)&args->line_nr, false, NULL); 1992 1993 dl = disasm_line__new(args); 1994 if (dl == NULL) 1995 goto err; 1996 1997 annotation_line__add(&dl->al, ¬es->src->source); 1998 1999 free(args->fileloc); 2000 pc += ins_len; 2001 offset += ins_len; 2002 } 2003 2004 ret = 0; 2005 2006 err: 2007 LLVMDisasmDispose(disasm); 2008 free(buf); 2009 free(line_storage); 2010 return ret; 2011 } 2012 #else // HAVE_LIBLLVM_SUPPORT 2013 static int symbol__disassemble_llvm(char *filename, struct symbol *sym, 2014 struct annotate_args *args __maybe_unused) 2015 { 2016 symbol__disassembler_missing("LLVM", filename, sym); 2017 return -1; 2018 } 2019 #endif // HAVE_LIBLLVM_SUPPORT 2020 2021 /* 2022 * Possibly create a new version of line with tabs expanded. Returns the 2023 * existing or new line, storage is updated if a new line is allocated. If 2024 * allocation fails then NULL is returned. 2025 */ 2026 static char *expand_tabs(char *line, char **storage, size_t *storage_len) 2027 { 2028 size_t i, src, dst, len, new_storage_len, num_tabs; 2029 char *new_line; 2030 size_t line_len = strlen(line); 2031 2032 for (num_tabs = 0, i = 0; i < line_len; i++) 2033 if (line[i] == '\t') 2034 num_tabs++; 2035 2036 if (num_tabs == 0) 2037 return line; 2038 2039 /* 2040 * Space for the line and '\0', less the leading and trailing 2041 * spaces. Each tab may introduce 7 additional spaces. 2042 */ 2043 new_storage_len = line_len + 1 + (num_tabs * 7); 2044 2045 new_line = malloc(new_storage_len); 2046 if (new_line == NULL) { 2047 pr_err("Failure allocating memory for tab expansion\n"); 2048 return NULL; 2049 } 2050 2051 /* 2052 * Copy regions starting at src and expand tabs. If there are two 2053 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces 2054 * are inserted. 2055 */ 2056 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) { 2057 if (line[i] == '\t') { 2058 len = i - src; 2059 memcpy(&new_line[dst], &line[src], len); 2060 dst += len; 2061 new_line[dst++] = ' '; 2062 while (dst % 8 != 0) 2063 new_line[dst++] = ' '; 2064 src = i + 1; 2065 num_tabs--; 2066 } 2067 } 2068 2069 /* Expand the last region. */ 2070 len = line_len - src; 2071 memcpy(&new_line[dst], &line[src], len); 2072 dst += len; 2073 new_line[dst] = '\0'; 2074 2075 free(*storage); 2076 *storage = new_line; 2077 *storage_len = new_storage_len; 2078 return new_line; 2079 } 2080 2081 static int symbol__disassemble_objdump(const char *filename, struct symbol *sym, 2082 struct annotate_args *args) 2083 { 2084 struct annotation_options *opts = &annotate_opts; 2085 struct map *map = args->ms.map; 2086 struct dso *dso = map__dso(map); 2087 char *command; 2088 FILE *file; 2089 int lineno = 0; 2090 char *fileloc = NULL; 2091 int nline; 2092 char *line; 2093 size_t line_len; 2094 const char *objdump_argv[] = { 2095 "/bin/sh", 2096 "-c", 2097 NULL, /* Will be the objdump command to run. */ 2098 "--", 2099 NULL, /* Will be the symfs path. */ 2100 NULL, 2101 }; 2102 struct child_process objdump_process; 2103 int err; 2104 2105 err = asprintf(&command, 2106 "%s %s%s --start-address=0x%016" PRIx64 2107 " --stop-address=0x%016" PRIx64 2108 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"", 2109 opts->objdump_path ?: "objdump", 2110 opts->disassembler_style ? "-M " : "", 2111 opts->disassembler_style ?: "", 2112 map__rip_2objdump(map, sym->start), 2113 map__rip_2objdump(map, sym->end), 2114 opts->show_linenr ? "-l" : "", 2115 opts->show_asm_raw ? "" : "--no-show-raw-insn", 2116 opts->annotate_src ? "-S" : "", 2117 opts->prefix ? "--prefix " : "", 2118 opts->prefix ? '"' : ' ', 2119 opts->prefix ?: "", 2120 opts->prefix ? '"' : ' ', 2121 opts->prefix_strip ? "--prefix-strip=" : "", 2122 opts->prefix_strip ?: ""); 2123 2124 if (err < 0) { 2125 pr_err("Failure allocating memory for the command to run\n"); 2126 return err; 2127 } 2128 2129 pr_debug("Executing: %s\n", command); 2130 2131 objdump_argv[2] = command; 2132 objdump_argv[4] = filename; 2133 2134 /* Create a pipe to read from for stdout */ 2135 memset(&objdump_process, 0, sizeof(objdump_process)); 2136 objdump_process.argv = objdump_argv; 2137 objdump_process.out = -1; 2138 objdump_process.err = -1; 2139 objdump_process.no_stderr = 1; 2140 if (start_command(&objdump_process)) { 2141 pr_err("Failure starting to run %s\n", command); 2142 err = -1; 2143 goto out_free_command; 2144 } 2145 2146 file = fdopen(objdump_process.out, "r"); 2147 if (!file) { 2148 pr_err("Failure creating FILE stream for %s\n", command); 2149 /* 2150 * If we were using debug info should retry with 2151 * original binary. 2152 */ 2153 err = -1; 2154 goto out_close_stdout; 2155 } 2156 2157 /* Storage for getline. */ 2158 line = NULL; 2159 line_len = 0; 2160 2161 nline = 0; 2162 while (!feof(file)) { 2163 const char *match; 2164 char *expanded_line; 2165 2166 if (getline(&line, &line_len, file) < 0 || !line) 2167 break; 2168 2169 /* Skip lines containing "filename:" */ 2170 match = strstr(line, filename); 2171 if (match && match[strlen(filename)] == ':') 2172 continue; 2173 2174 expanded_line = strim(line); 2175 expanded_line = expand_tabs(expanded_line, &line, &line_len); 2176 if (!expanded_line) 2177 break; 2178 2179 /* 2180 * The source code line number (lineno) needs to be kept in 2181 * across calls to symbol__parse_objdump_line(), so that it 2182 * can associate it with the instructions till the next one. 2183 * See disasm_line__new() and struct disasm_line::line_nr. 2184 */ 2185 if (symbol__parse_objdump_line(sym, args, expanded_line, 2186 &lineno, &fileloc) < 0) 2187 break; 2188 nline++; 2189 } 2190 free(line); 2191 free(fileloc); 2192 2193 err = finish_command(&objdump_process); 2194 if (err) 2195 pr_err("Error running %s\n", command); 2196 2197 if (nline == 0) { 2198 err = -1; 2199 pr_err("No output from %s\n", command); 2200 } 2201 2202 /* 2203 * kallsyms does not have symbol sizes so there may a nop at the end. 2204 * Remove it. 2205 */ 2206 if (dso__is_kcore(dso)) 2207 delete_last_nop(sym); 2208 2209 fclose(file); 2210 2211 out_close_stdout: 2212 close(objdump_process.out); 2213 2214 out_free_command: 2215 free(command); 2216 return err; 2217 } 2218 2219 int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 2220 { 2221 struct annotation_options *options = args->options; 2222 struct map *map = args->ms.map; 2223 struct dso *dso = map__dso(map); 2224 char symfs_filename[PATH_MAX]; 2225 bool delete_extract = false; 2226 struct kcore_extract kce; 2227 bool decomp = false; 2228 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 2229 2230 if (err) 2231 return err; 2232 2233 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 2234 symfs_filename, sym->name, map__unmap_ip(map, sym->start), 2235 map__unmap_ip(map, sym->end)); 2236 2237 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name); 2238 2239 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) { 2240 return symbol__disassemble_bpf(sym, args); 2241 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) { 2242 return symbol__disassemble_bpf_image(sym, args); 2243 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 2244 return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE; 2245 } else if (dso__is_kcore(dso)) { 2246 kce.addr = map__rip_2objdump(map, sym->start); 2247 kce.kcore_filename = symfs_filename; 2248 kce.len = sym->end - sym->start; 2249 kce.offs = sym->start; 2250 2251 if (!kcore_extract__create(&kce)) { 2252 delete_extract = true; 2253 strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename)); 2254 } 2255 } else if (dso__needs_decompress(dso)) { 2256 char tmp[KMOD_DECOMP_LEN]; 2257 2258 if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0) 2259 return -1; 2260 2261 decomp = true; 2262 strcpy(symfs_filename, tmp); 2263 } 2264 2265 /* 2266 * For powerpc data type profiling, use the dso__data_read_offset to 2267 * read raw instruction directly and interpret the binary code to 2268 * understand instructions and register fields. For sort keys as type 2269 * and typeoff, disassemble to mnemonic notation is not required in 2270 * case of powerpc. 2271 */ 2272 if (arch__is(args->arch, "powerpc")) { 2273 extern const char *sort_order; 2274 2275 if (sort_order && !strstr(sort_order, "sym")) { 2276 err = symbol__disassemble_raw(symfs_filename, sym, args); 2277 if (err == 0) 2278 goto out_remove_tmp; 2279 2280 err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args); 2281 if (err == 0) 2282 goto out_remove_tmp; 2283 } 2284 } 2285 2286 err = -1; 2287 for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers) && err != 0; i++) { 2288 enum perf_disassembler dis = options->disassemblers[i]; 2289 2290 switch (dis) { 2291 case PERF_DISASM_LLVM: 2292 err = symbol__disassemble_llvm(symfs_filename, sym, args); 2293 break; 2294 case PERF_DISASM_CAPSTONE: 2295 err = symbol__disassemble_capstone(symfs_filename, sym, args); 2296 break; 2297 case PERF_DISASM_OBJDUMP: 2298 err = symbol__disassemble_objdump(symfs_filename, sym, args); 2299 break; 2300 case PERF_DISASM_UNKNOWN: /* End of disassemblers. */ 2301 default: 2302 goto out_remove_tmp; 2303 } 2304 if (err == 0) 2305 pr_debug("Disassembled with %s\n", perf_disassembler__strs[dis]); 2306 } 2307 out_remove_tmp: 2308 if (decomp) 2309 unlink(symfs_filename); 2310 2311 if (delete_extract) 2312 kcore_extract__delete(&kce); 2313 2314 return err; 2315 } 2316