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, struct annotate_args *args); 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, struct annotate_args *args) 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 disasm = 0; 979 int ret = 0; 980 981 if (args->options->disassembler_used) 982 disasm = 1; 983 984 if (name_raw_insn[0] == '\0') 985 return -1; 986 987 if (disasm) 988 ret = disasm_line__parse(name, namep, rawp); 989 else 990 *namep = ""; 991 992 tmp_raw_insn = strndup(name_raw_insn, 11); 993 if (tmp_raw_insn == NULL) 994 return -1; 995 996 remove_spaces(tmp_raw_insn); 997 998 sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn); 999 if (disasm) 1000 dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn); 1001 1002 return ret; 1003 } 1004 1005 static void annotation_line__init(struct annotation_line *al, 1006 struct annotate_args *args, 1007 int nr) 1008 { 1009 al->offset = args->offset; 1010 al->line = strdup(args->line); 1011 al->line_nr = args->line_nr; 1012 al->fileloc = args->fileloc; 1013 al->data_nr = nr; 1014 } 1015 1016 static void annotation_line__exit(struct annotation_line *al) 1017 { 1018 zfree_srcline(&al->path); 1019 zfree(&al->line); 1020 zfree(&al->cycles); 1021 zfree(&al->br_cntr); 1022 } 1023 1024 static size_t disasm_line_size(int nr) 1025 { 1026 struct annotation_line *al; 1027 1028 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr)); 1029 } 1030 1031 /* 1032 * Allocating the disasm annotation line data with 1033 * following structure: 1034 * 1035 * ------------------------------------------- 1036 * struct disasm_line | struct annotation_line 1037 * ------------------------------------------- 1038 * 1039 * We have 'struct annotation_line' member as last member 1040 * of 'struct disasm_line' to have an easy access. 1041 */ 1042 struct disasm_line *disasm_line__new(struct annotate_args *args) 1043 { 1044 struct disasm_line *dl = NULL; 1045 struct annotation *notes = symbol__annotation(args->ms.sym); 1046 int nr = notes->src->nr_events; 1047 1048 dl = zalloc(disasm_line_size(nr)); 1049 if (!dl) 1050 return NULL; 1051 1052 annotation_line__init(&dl->al, args, nr); 1053 if (dl->al.line == NULL) 1054 goto out_delete; 1055 1056 if (args->offset != -1) { 1057 if (arch__is(args->arch, "powerpc")) { 1058 if (disasm_line__parse_powerpc(dl, args) < 0) 1059 goto out_free_line; 1060 } else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0) 1061 goto out_free_line; 1062 1063 disasm_line__init_ins(dl, args->arch, &args->ms); 1064 } 1065 1066 return dl; 1067 1068 out_free_line: 1069 zfree(&dl->al.line); 1070 out_delete: 1071 free(dl); 1072 return NULL; 1073 } 1074 1075 void disasm_line__free(struct disasm_line *dl) 1076 { 1077 if (dl->ins.ops && dl->ins.ops->free) 1078 dl->ins.ops->free(&dl->ops); 1079 else 1080 ins_ops__delete(&dl->ops); 1081 zfree(&dl->ins.name); 1082 annotation_line__exit(&dl->al); 1083 free(dl); 1084 } 1085 1086 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name) 1087 { 1088 if (raw || !dl->ins.ops) 1089 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw); 1090 1091 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name); 1092 } 1093 1094 /* 1095 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw) 1096 * which looks like following 1097 * 1098 * 0000000000415500 <_init>: 1099 * 415500: sub $0x8,%rsp 1100 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8> 1101 * 41550b: test %rax,%rax 1102 * 41550e: je 415515 <_init+0x15> 1103 * 415510: callq 416e70 <__gmon_start__@plt> 1104 * 415515: add $0x8,%rsp 1105 * 415519: retq 1106 * 1107 * it will be parsed and saved into struct disasm_line as 1108 * <offset> <name> <ops.raw> 1109 * 1110 * The offset will be a relative offset from the start of the symbol and -1 1111 * means that it's not a disassembly line so should be treated differently. 1112 * The ops.raw part will be parsed further according to type of the instruction. 1113 */ 1114 static int symbol__parse_objdump_line(struct symbol *sym, 1115 struct annotate_args *args, 1116 char *parsed_line, int *line_nr, char **fileloc) 1117 { 1118 struct map *map = args->ms.map; 1119 struct annotation *notes = symbol__annotation(sym); 1120 struct disasm_line *dl; 1121 char *tmp; 1122 s64 line_ip, offset = -1; 1123 regmatch_t match[2]; 1124 1125 /* /filename:linenr ? Save line number and ignore. */ 1126 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) { 1127 *line_nr = atoi(parsed_line + match[1].rm_so); 1128 free(*fileloc); 1129 *fileloc = strdup(parsed_line); 1130 return 0; 1131 } 1132 1133 /* Process hex address followed by ':'. */ 1134 line_ip = strtoull(parsed_line, &tmp, 16); 1135 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') { 1136 u64 start = map__rip_2objdump(map, sym->start), 1137 end = map__rip_2objdump(map, sym->end); 1138 1139 offset = line_ip - start; 1140 if ((u64)line_ip < start || (u64)line_ip >= end) 1141 offset = -1; 1142 else 1143 parsed_line = tmp + 1; 1144 } 1145 1146 args->offset = offset; 1147 args->line = parsed_line; 1148 args->line_nr = *line_nr; 1149 args->fileloc = *fileloc; 1150 args->ms.sym = sym; 1151 1152 dl = disasm_line__new(args); 1153 (*line_nr)++; 1154 1155 if (dl == NULL) 1156 return -1; 1157 1158 if (!disasm_line__has_local_offset(dl)) { 1159 dl->ops.target.offset = dl->ops.target.addr - 1160 map__rip_2objdump(map, sym->start); 1161 dl->ops.target.offset_avail = true; 1162 } 1163 1164 /* kcore has no symbols, so add the call target symbol */ 1165 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) { 1166 struct addr_map_symbol target = { 1167 .addr = dl->ops.target.addr, 1168 .ms = { .map = map, }, 1169 }; 1170 1171 if (!maps__find_ams(args->ms.maps, &target) && 1172 target.ms.sym->start == target.al_addr) 1173 dl->ops.target.sym = target.ms.sym; 1174 } 1175 1176 annotation_line__add(&dl->al, ¬es->src->source); 1177 return 0; 1178 } 1179 1180 static void delete_last_nop(struct symbol *sym) 1181 { 1182 struct annotation *notes = symbol__annotation(sym); 1183 struct list_head *list = ¬es->src->source; 1184 struct disasm_line *dl; 1185 1186 while (!list_empty(list)) { 1187 dl = list_entry(list->prev, struct disasm_line, al.node); 1188 1189 if (dl->ins.ops) { 1190 if (!ins__is_nop(&dl->ins)) 1191 return; 1192 } else { 1193 if (!strstr(dl->al.line, " nop ") && 1194 !strstr(dl->al.line, " nopl ") && 1195 !strstr(dl->al.line, " nopw ")) 1196 return; 1197 } 1198 1199 list_del_init(&dl->al.node); 1200 disasm_line__free(dl); 1201 } 1202 } 1203 1204 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen) 1205 { 1206 struct dso *dso = map__dso(ms->map); 1207 1208 BUG_ON(buflen == 0); 1209 1210 if (errnum >= 0) { 1211 str_error_r(errnum, buf, buflen); 1212 return 0; 1213 } 1214 1215 switch (errnum) { 1216 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: { 1217 char bf[SBUILD_ID_SIZE + 15] = " with build id "; 1218 char *build_id_msg = NULL; 1219 1220 if (dso__has_build_id(dso)) { 1221 build_id__sprintf(dso__bid(dso), bf + 15); 1222 build_id_msg = bf; 1223 } 1224 scnprintf(buf, buflen, 1225 "No vmlinux file%s\nwas found in the path.\n\n" 1226 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n" 1227 "Please use:\n\n" 1228 " perf buildid-cache -vu vmlinux\n\n" 1229 "or:\n\n" 1230 " --vmlinux vmlinux\n", build_id_msg ?: ""); 1231 } 1232 break; 1233 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF: 1234 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation"); 1235 break; 1236 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP: 1237 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions."); 1238 break; 1239 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING: 1240 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization."); 1241 break; 1242 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE: 1243 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso)); 1244 break; 1245 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF: 1246 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.", 1247 dso__long_name(dso)); 1248 break; 1249 case SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE: 1250 scnprintf(buf, buflen, "Couldn't determine the file %s type.", dso__long_name(dso)); 1251 break; 1252 default: 1253 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum); 1254 break; 1255 } 1256 1257 return 0; 1258 } 1259 1260 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size) 1261 { 1262 char linkname[PATH_MAX]; 1263 char *build_id_filename; 1264 char *build_id_path = NULL; 1265 char *pos; 1266 int len; 1267 1268 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS && 1269 !dso__is_kcore(dso)) 1270 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX; 1271 1272 build_id_filename = dso__build_id_filename(dso, NULL, 0, false); 1273 if (build_id_filename) { 1274 __symbol__join_symfs(filename, filename_size, build_id_filename); 1275 free(build_id_filename); 1276 } else { 1277 if (dso__has_build_id(dso)) 1278 return ENOMEM; 1279 goto fallback; 1280 } 1281 1282 build_id_path = strdup(filename); 1283 if (!build_id_path) 1284 return ENOMEM; 1285 1286 /* 1287 * old style build-id cache has name of XX/XXXXXXX.. while 1288 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}. 1289 * extract the build-id part of dirname in the new style only. 1290 */ 1291 pos = strrchr(build_id_path, '/'); 1292 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2) 1293 dirname(build_id_path); 1294 1295 if (dso__is_kcore(dso)) 1296 goto fallback; 1297 1298 len = readlink(build_id_path, linkname, sizeof(linkname) - 1); 1299 if (len < 0) 1300 goto fallback; 1301 1302 linkname[len] = '\0'; 1303 if (strstr(linkname, DSO__NAME_KALLSYMS) || 1304 access(filename, R_OK)) { 1305 fallback: 1306 /* 1307 * If we don't have build-ids or the build-id file isn't in the 1308 * cache, or is just a kallsyms file, well, lets hope that this 1309 * DSO is the same as when 'perf record' ran. 1310 */ 1311 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/') 1312 snprintf(filename, filename_size, "%s", dso__long_name(dso)); 1313 else 1314 __symbol__join_symfs(filename, filename_size, dso__long_name(dso)); 1315 1316 mutex_lock(dso__lock(dso)); 1317 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) { 1318 char *new_name = dso__filename_with_chroot(dso, filename); 1319 if (new_name) { 1320 strlcpy(filename, new_name, filename_size); 1321 free(new_name); 1322 } 1323 } 1324 mutex_unlock(dso__lock(dso)); 1325 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 1326 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE); 1327 } 1328 1329 free(build_id_path); 1330 return 0; 1331 } 1332 1333 #ifdef HAVE_LIBCAPSTONE_SUPPORT 1334 #include <capstone/capstone.h> 1335 1336 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style); 1337 1338 static int open_capstone_handle(struct annotate_args *args, bool is_64bit, 1339 csh *handle) 1340 { 1341 struct annotation_options *opt = args->options; 1342 cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32; 1343 1344 /* TODO: support more architectures */ 1345 if (!arch__is(args->arch, "x86")) 1346 return -1; 1347 1348 if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK) 1349 return -1; 1350 1351 if (!opt->disassembler_style || 1352 !strcmp(opt->disassembler_style, "att")) 1353 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT); 1354 1355 /* 1356 * Resolving address operands to symbols is implemented 1357 * on x86 by investigating instruction details. 1358 */ 1359 cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON); 1360 1361 return 0; 1362 } 1363 #endif 1364 1365 #if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT) 1366 struct find_file_offset_data { 1367 u64 ip; 1368 u64 offset; 1369 }; 1370 1371 /* This will be called for each PHDR in an ELF binary */ 1372 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg) 1373 { 1374 struct find_file_offset_data *data = arg; 1375 1376 if (start <= data->ip && data->ip < start + len) { 1377 data->offset = pgoff + data->ip - start; 1378 return 1; 1379 } 1380 return 0; 1381 } 1382 1383 static u8 * 1384 read_symbol(const char *filename, struct map *map, struct symbol *sym, 1385 u64 *len, bool *is_64bit) 1386 { 1387 struct dso *dso = map__dso(map); 1388 struct nscookie nsc; 1389 u64 start = map__rip_2objdump(map, sym->start); 1390 u64 end = map__rip_2objdump(map, sym->end); 1391 int fd, count; 1392 u8 *buf = NULL; 1393 struct find_file_offset_data data = { 1394 .ip = start, 1395 }; 1396 1397 *is_64bit = false; 1398 1399 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1400 fd = open(filename, O_RDONLY); 1401 nsinfo__mountns_exit(&nsc); 1402 if (fd < 0) 1403 return NULL; 1404 1405 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, 1406 is_64bit) == 0) 1407 goto err; 1408 1409 *len = end - start; 1410 buf = malloc(*len); 1411 if (buf == NULL) 1412 goto err; 1413 1414 count = pread(fd, buf, *len, data.offset); 1415 close(fd); 1416 fd = -1; 1417 1418 if ((u64)count != *len) 1419 goto err; 1420 1421 return buf; 1422 1423 err: 1424 if (fd >= 0) 1425 close(fd); 1426 free(buf); 1427 return NULL; 1428 } 1429 #endif 1430 1431 #if !defined(HAVE_LIBCAPSTONE_SUPPORT) || !defined(HAVE_LIBLLVM_SUPPORT) 1432 static void symbol__disassembler_missing(const char *disassembler, const char *filename, 1433 struct symbol *sym) 1434 { 1435 pr_debug("The %s disassembler isn't linked in for %s in %s\n", 1436 disassembler, sym->name, filename); 1437 } 1438 #endif 1439 1440 #ifdef HAVE_LIBCAPSTONE_SUPPORT 1441 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len, 1442 struct annotate_args *args, u64 addr) 1443 { 1444 int i; 1445 struct map *map = args->ms.map; 1446 struct symbol *sym; 1447 1448 /* TODO: support more architectures */ 1449 if (!arch__is(args->arch, "x86")) 1450 return; 1451 1452 if (insn->detail == NULL) 1453 return; 1454 1455 for (i = 0; i < insn->detail->x86.op_count; i++) { 1456 cs_x86_op *op = &insn->detail->x86.operands[i]; 1457 u64 orig_addr; 1458 1459 if (op->type != X86_OP_MEM) 1460 continue; 1461 1462 /* only print RIP-based global symbols for now */ 1463 if (op->mem.base != X86_REG_RIP) 1464 continue; 1465 1466 /* get the target address */ 1467 orig_addr = addr + insn->size + op->mem.disp; 1468 addr = map__objdump_2mem(map, orig_addr); 1469 1470 if (dso__kernel(map__dso(map))) { 1471 /* 1472 * The kernel maps can be splitted into sections, 1473 * let's find the map first and the search the symbol. 1474 */ 1475 map = maps__find(map__kmaps(map), addr); 1476 if (map == NULL) 1477 continue; 1478 } 1479 1480 /* convert it to map-relative address for search */ 1481 addr = map__map_ip(map, addr); 1482 1483 sym = map__find_symbol(map, addr); 1484 if (sym == NULL) 1485 continue; 1486 1487 if (addr == sym->start) { 1488 scnprintf(buf, len, "\t# %"PRIx64" <%s>", 1489 orig_addr, sym->name); 1490 } else { 1491 scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">", 1492 orig_addr, sym->name, addr - sym->start); 1493 } 1494 break; 1495 } 1496 } 1497 1498 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym, 1499 struct annotate_args *args) 1500 { 1501 struct annotation *notes = symbol__annotation(sym); 1502 struct map *map = args->ms.map; 1503 struct dso *dso = map__dso(map); 1504 struct nscookie nsc; 1505 u64 start = map__rip_2objdump(map, sym->start); 1506 u64 end = map__rip_2objdump(map, sym->end); 1507 u64 len = end - start; 1508 u64 offset; 1509 int i, fd, count; 1510 bool is_64bit = false; 1511 bool needs_cs_close = false; 1512 u8 *buf = NULL; 1513 struct find_file_offset_data data = { 1514 .ip = start, 1515 }; 1516 csh handle; 1517 char disasm_buf[512]; 1518 struct disasm_line *dl; 1519 u32 *line; 1520 bool disassembler_style = false; 1521 1522 if (args->options->objdump_path) 1523 return -1; 1524 1525 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc); 1526 fd = open(filename, O_RDONLY); 1527 nsinfo__mountns_exit(&nsc); 1528 if (fd < 0) 1529 return -1; 1530 1531 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data, 1532 &is_64bit) == 0) 1533 goto err; 1534 1535 if (!args->options->disassembler_style || 1536 !strcmp(args->options->disassembler_style, "att")) 1537 disassembler_style = true; 1538 1539 if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0) 1540 goto err; 1541 1542 needs_cs_close = true; 1543 1544 buf = malloc(len); 1545 if (buf == NULL) 1546 goto err; 1547 1548 count = pread(fd, buf, len, data.offset); 1549 close(fd); 1550 fd = -1; 1551 1552 if ((u64)count != len) 1553 goto err; 1554 1555 line = (u32 *)buf; 1556 1557 /* add the function address and name */ 1558 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1559 start, sym->name); 1560 1561 args->offset = -1; 1562 args->line = disasm_buf; 1563 args->line_nr = 0; 1564 args->fileloc = NULL; 1565 args->ms.sym = sym; 1566 1567 dl = disasm_line__new(args); 1568 if (dl == NULL) 1569 goto err; 1570 1571 annotation_line__add(&dl->al, ¬es->src->source); 1572 1573 /* 1574 * TODO: enable disassm for powerpc 1575 * count = cs_disasm(handle, buf, len, start, len, &insn); 1576 * 1577 * For now, only binary code is saved in disassembled line 1578 * to be used in "type" and "typeoff" sort keys. Each raw code 1579 * is 32 bit instruction. So use "len/4" to get the number of 1580 * entries. 1581 */ 1582 count = len/4; 1583 1584 for (i = 0, offset = 0; i < count; i++) { 1585 args->offset = offset; 1586 sprintf(args->line, "%x", line[i]); 1587 1588 dl = disasm_line__new(args); 1589 if (dl == NULL) 1590 break; 1591 1592 annotation_line__add(&dl->al, ¬es->src->source); 1593 1594 offset += 4; 1595 } 1596 1597 /* It failed in the middle */ 1598 if (offset != len) { 1599 struct list_head *list = ¬es->src->source; 1600 1601 /* Discard all lines and fallback to objdump */ 1602 while (!list_empty(list)) { 1603 dl = list_first_entry(list, struct disasm_line, al.node); 1604 1605 list_del_init(&dl->al.node); 1606 disasm_line__free(dl); 1607 } 1608 count = -1; 1609 } 1610 1611 out: 1612 if (needs_cs_close) 1613 cs_close(&handle); 1614 free(buf); 1615 return count < 0 ? count : 0; 1616 1617 err: 1618 if (fd >= 0) 1619 close(fd); 1620 count = -1; 1621 goto out; 1622 } 1623 1624 static int symbol__disassemble_capstone(char *filename, struct symbol *sym, 1625 struct annotate_args *args) 1626 { 1627 struct annotation *notes = symbol__annotation(sym); 1628 struct map *map = args->ms.map; 1629 u64 start = map__rip_2objdump(map, sym->start); 1630 u64 len; 1631 u64 offset; 1632 int i, count, free_count; 1633 bool is_64bit = false; 1634 bool needs_cs_close = false; 1635 u8 *buf = NULL; 1636 csh handle; 1637 cs_insn *insn = NULL; 1638 char disasm_buf[512]; 1639 struct disasm_line *dl; 1640 1641 if (args->options->objdump_path) 1642 return -1; 1643 1644 buf = read_symbol(filename, map, sym, &len, &is_64bit); 1645 if (buf == NULL) 1646 return -1; 1647 1648 /* add the function address and name */ 1649 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1650 start, sym->name); 1651 1652 args->offset = -1; 1653 args->line = disasm_buf; 1654 args->line_nr = 0; 1655 args->fileloc = NULL; 1656 args->ms.sym = sym; 1657 1658 dl = disasm_line__new(args); 1659 if (dl == NULL) 1660 goto err; 1661 1662 annotation_line__add(&dl->al, ¬es->src->source); 1663 1664 if (open_capstone_handle(args, is_64bit, &handle) < 0) 1665 goto err; 1666 1667 needs_cs_close = true; 1668 1669 free_count = count = cs_disasm(handle, buf, len, start, len, &insn); 1670 for (i = 0, offset = 0; i < count; i++) { 1671 int printed; 1672 1673 printed = scnprintf(disasm_buf, sizeof(disasm_buf), 1674 " %-7s %s", 1675 insn[i].mnemonic, insn[i].op_str); 1676 print_capstone_detail(&insn[i], disasm_buf + printed, 1677 sizeof(disasm_buf) - printed, args, 1678 start + offset); 1679 1680 args->offset = offset; 1681 args->line = disasm_buf; 1682 1683 dl = disasm_line__new(args); 1684 if (dl == NULL) 1685 goto err; 1686 1687 annotation_line__add(&dl->al, ¬es->src->source); 1688 1689 offset += insn[i].size; 1690 } 1691 1692 /* It failed in the middle: probably due to unknown instructions */ 1693 if (offset != len) { 1694 struct list_head *list = ¬es->src->source; 1695 1696 /* Discard all lines and fallback to objdump */ 1697 while (!list_empty(list)) { 1698 dl = list_first_entry(list, struct disasm_line, al.node); 1699 1700 list_del_init(&dl->al.node); 1701 disasm_line__free(dl); 1702 } 1703 count = -1; 1704 } 1705 1706 out: 1707 if (needs_cs_close) { 1708 cs_close(&handle); 1709 if (free_count > 0) 1710 cs_free(insn, free_count); 1711 } 1712 free(buf); 1713 return count < 0 ? count : 0; 1714 1715 err: 1716 if (needs_cs_close) { 1717 struct disasm_line *tmp; 1718 1719 /* 1720 * It probably failed in the middle of the above loop. 1721 * Release any resources it might add. 1722 */ 1723 list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) { 1724 list_del(&dl->al.node); 1725 disasm_line__free(dl); 1726 } 1727 } 1728 count = -1; 1729 goto out; 1730 } 1731 #else // HAVE_LIBCAPSTONE_SUPPORT 1732 static int symbol__disassemble_capstone(char *filename, struct symbol *sym, 1733 struct annotate_args *args __maybe_unused) 1734 { 1735 symbol__disassembler_missing("capstone", filename, sym); 1736 return -1; 1737 } 1738 1739 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym, 1740 struct annotate_args *args __maybe_unused) 1741 { 1742 symbol__disassembler_missing("capstone powerpc", filename, sym); 1743 return -1; 1744 } 1745 #endif // HAVE_LIBCAPSTONE_SUPPORT 1746 1747 static int symbol__disassemble_raw(char *filename, struct symbol *sym, 1748 struct annotate_args *args) 1749 { 1750 struct annotation *notes = symbol__annotation(sym); 1751 struct map *map = args->ms.map; 1752 struct dso *dso = map__dso(map); 1753 u64 start = map__rip_2objdump(map, sym->start); 1754 u64 end = map__rip_2objdump(map, sym->end); 1755 u64 len = end - start; 1756 u64 offset; 1757 int i, count; 1758 u8 *buf = NULL; 1759 char disasm_buf[512]; 1760 struct disasm_line *dl; 1761 u32 *line; 1762 1763 /* Return if objdump is specified explicitly */ 1764 if (args->options->objdump_path) 1765 return -1; 1766 1767 pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename); 1768 1769 buf = malloc(len); 1770 if (buf == NULL) 1771 goto err; 1772 1773 count = dso__data_read_offset(dso, NULL, sym->start, buf, len); 1774 1775 line = (u32 *)buf; 1776 1777 if ((u64)count != len) 1778 goto err; 1779 1780 /* add the function address and name */ 1781 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1782 start, sym->name); 1783 1784 args->offset = -1; 1785 args->line = disasm_buf; 1786 args->line_nr = 0; 1787 args->fileloc = NULL; 1788 args->ms.sym = sym; 1789 1790 dl = disasm_line__new(args); 1791 if (dl == NULL) 1792 goto err; 1793 1794 annotation_line__add(&dl->al, ¬es->src->source); 1795 1796 /* Each raw instruction is 4 byte */ 1797 count = len/4; 1798 1799 for (i = 0, offset = 0; i < count; i++) { 1800 args->offset = offset; 1801 sprintf(args->line, "%x", line[i]); 1802 dl = disasm_line__new(args); 1803 if (dl == NULL) 1804 break; 1805 1806 annotation_line__add(&dl->al, ¬es->src->source); 1807 offset += 4; 1808 } 1809 1810 /* It failed in the middle */ 1811 if (offset != len) { 1812 struct list_head *list = ¬es->src->source; 1813 1814 /* Discard all lines and fallback to objdump */ 1815 while (!list_empty(list)) { 1816 dl = list_first_entry(list, struct disasm_line, al.node); 1817 1818 list_del_init(&dl->al.node); 1819 disasm_line__free(dl); 1820 } 1821 count = -1; 1822 } 1823 1824 out: 1825 free(buf); 1826 return count < 0 ? count : 0; 1827 1828 err: 1829 count = -1; 1830 goto out; 1831 } 1832 1833 #ifdef HAVE_LIBLLVM_SUPPORT 1834 #include <llvm-c/Disassembler.h> 1835 #include <llvm-c/Target.h> 1836 #include "util/llvm-c-helpers.h" 1837 1838 struct symbol_lookup_storage { 1839 u64 branch_addr; 1840 u64 pcrel_load_addr; 1841 }; 1842 1843 /* 1844 * Whenever LLVM wants to resolve an address into a symbol, it calls this 1845 * callback. We don't ever actually _return_ anything (in particular, because 1846 * it puts quotation marks around what we return), but we use this as a hint 1847 * that there is a branch or PC-relative address in the expression that we 1848 * should add some textual annotation for after the instruction. The caller 1849 * will use this information to add the actual annotation. 1850 */ 1851 static const char * 1852 symbol_lookup_callback(void *disinfo, uint64_t value, 1853 uint64_t *ref_type, 1854 uint64_t address __maybe_unused, 1855 const char **ref __maybe_unused) 1856 { 1857 struct symbol_lookup_storage *storage = disinfo; 1858 1859 if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch) 1860 storage->branch_addr = value; 1861 else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load) 1862 storage->pcrel_load_addr = value; 1863 *ref_type = LLVMDisassembler_ReferenceType_InOut_None; 1864 return NULL; 1865 } 1866 1867 static int symbol__disassemble_llvm(char *filename, struct symbol *sym, 1868 struct annotate_args *args) 1869 { 1870 struct annotation *notes = symbol__annotation(sym); 1871 struct map *map = args->ms.map; 1872 struct dso *dso = map__dso(map); 1873 u64 start = map__rip_2objdump(map, sym->start); 1874 u8 *buf; 1875 u64 len; 1876 u64 pc; 1877 bool is_64bit; 1878 char triplet[64]; 1879 char disasm_buf[2048]; 1880 size_t disasm_len; 1881 struct disasm_line *dl; 1882 LLVMDisasmContextRef disasm = NULL; 1883 struct symbol_lookup_storage storage; 1884 char *line_storage = NULL; 1885 size_t line_storage_len = 0; 1886 int ret = -1; 1887 1888 if (args->options->objdump_path) 1889 return -1; 1890 1891 LLVMInitializeAllTargetInfos(); 1892 LLVMInitializeAllTargetMCs(); 1893 LLVMInitializeAllDisassemblers(); 1894 1895 buf = read_symbol(filename, map, sym, &len, &is_64bit); 1896 if (buf == NULL) 1897 return -1; 1898 1899 if (arch__is(args->arch, "x86")) { 1900 if (is_64bit) 1901 scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux"); 1902 else 1903 scnprintf(triplet, sizeof(triplet), "i686-pc-linux"); 1904 } else { 1905 scnprintf(triplet, sizeof(triplet), "%s-linux-gnu", 1906 args->arch->name); 1907 } 1908 1909 disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL, 1910 symbol_lookup_callback); 1911 if (disasm == NULL) 1912 goto err; 1913 1914 if (args->options->disassembler_style && 1915 !strcmp(args->options->disassembler_style, "intel")) 1916 LLVMSetDisasmOptions(disasm, 1917 LLVMDisassembler_Option_AsmPrinterVariant); 1918 1919 /* 1920 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM; 1921 * setting AsmPrinterVariant makes a new instruction printer, making it 1922 * forget about the PrintImmHex flag (which is applied before if both 1923 * are given to the same call). 1924 */ 1925 LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex); 1926 1927 /* add the function address and name */ 1928 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:", 1929 start, sym->name); 1930 1931 args->offset = -1; 1932 args->line = disasm_buf; 1933 args->line_nr = 0; 1934 args->fileloc = NULL; 1935 args->ms.sym = sym; 1936 1937 dl = disasm_line__new(args); 1938 if (dl == NULL) 1939 goto err; 1940 1941 annotation_line__add(&dl->al, ¬es->src->source); 1942 1943 pc = start; 1944 for (u64 offset = 0; offset < len; ) { 1945 unsigned int ins_len; 1946 1947 storage.branch_addr = 0; 1948 storage.pcrel_load_addr = 0; 1949 1950 ins_len = LLVMDisasmInstruction(disasm, buf + offset, 1951 len - offset, pc, 1952 disasm_buf, sizeof(disasm_buf)); 1953 if (ins_len == 0) 1954 goto err; 1955 disasm_len = strlen(disasm_buf); 1956 1957 if (storage.branch_addr != 0) { 1958 char *name = llvm_name_for_code(dso, filename, 1959 storage.branch_addr); 1960 if (name != NULL) { 1961 disasm_len += scnprintf(disasm_buf + disasm_len, 1962 sizeof(disasm_buf) - 1963 disasm_len, 1964 " <%s>", name); 1965 free(name); 1966 } 1967 } 1968 if (storage.pcrel_load_addr != 0) { 1969 char *name = llvm_name_for_data(dso, filename, 1970 storage.pcrel_load_addr); 1971 disasm_len += scnprintf(disasm_buf + disasm_len, 1972 sizeof(disasm_buf) - disasm_len, 1973 " # %#"PRIx64, 1974 storage.pcrel_load_addr); 1975 if (name) { 1976 disasm_len += scnprintf(disasm_buf + disasm_len, 1977 sizeof(disasm_buf) - 1978 disasm_len, 1979 " <%s>", name); 1980 free(name); 1981 } 1982 } 1983 1984 args->offset = offset; 1985 args->line = expand_tabs(disasm_buf, &line_storage, 1986 &line_storage_len); 1987 args->line_nr = 0; 1988 args->fileloc = NULL; 1989 args->ms.sym = sym; 1990 1991 llvm_addr2line(filename, pc, &args->fileloc, 1992 (unsigned int *)&args->line_nr, false, NULL); 1993 1994 dl = disasm_line__new(args); 1995 if (dl == NULL) 1996 goto err; 1997 1998 annotation_line__add(&dl->al, ¬es->src->source); 1999 2000 free(args->fileloc); 2001 pc += ins_len; 2002 offset += ins_len; 2003 } 2004 2005 ret = 0; 2006 2007 err: 2008 LLVMDisasmDispose(disasm); 2009 free(buf); 2010 free(line_storage); 2011 return ret; 2012 } 2013 #else // HAVE_LIBLLVM_SUPPORT 2014 static int symbol__disassemble_llvm(char *filename, struct symbol *sym, 2015 struct annotate_args *args __maybe_unused) 2016 { 2017 symbol__disassembler_missing("LLVM", filename, sym); 2018 return -1; 2019 } 2020 #endif // HAVE_LIBLLVM_SUPPORT 2021 2022 /* 2023 * Possibly create a new version of line with tabs expanded. Returns the 2024 * existing or new line, storage is updated if a new line is allocated. If 2025 * allocation fails then NULL is returned. 2026 */ 2027 static char *expand_tabs(char *line, char **storage, size_t *storage_len) 2028 { 2029 size_t i, src, dst, len, new_storage_len, num_tabs; 2030 char *new_line; 2031 size_t line_len = strlen(line); 2032 2033 for (num_tabs = 0, i = 0; i < line_len; i++) 2034 if (line[i] == '\t') 2035 num_tabs++; 2036 2037 if (num_tabs == 0) 2038 return line; 2039 2040 /* 2041 * Space for the line and '\0', less the leading and trailing 2042 * spaces. Each tab may introduce 7 additional spaces. 2043 */ 2044 new_storage_len = line_len + 1 + (num_tabs * 7); 2045 2046 new_line = malloc(new_storage_len); 2047 if (new_line == NULL) { 2048 pr_err("Failure allocating memory for tab expansion\n"); 2049 return NULL; 2050 } 2051 2052 /* 2053 * Copy regions starting at src and expand tabs. If there are two 2054 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces 2055 * are inserted. 2056 */ 2057 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) { 2058 if (line[i] == '\t') { 2059 len = i - src; 2060 memcpy(&new_line[dst], &line[src], len); 2061 dst += len; 2062 new_line[dst++] = ' '; 2063 while (dst % 8 != 0) 2064 new_line[dst++] = ' '; 2065 src = i + 1; 2066 num_tabs--; 2067 } 2068 } 2069 2070 /* Expand the last region. */ 2071 len = line_len - src; 2072 memcpy(&new_line[dst], &line[src], len); 2073 dst += len; 2074 new_line[dst] = '\0'; 2075 2076 free(*storage); 2077 *storage = new_line; 2078 *storage_len = new_storage_len; 2079 return new_line; 2080 } 2081 2082 static int symbol__disassemble_objdump(const char *filename, struct symbol *sym, 2083 struct annotate_args *args) 2084 { 2085 struct annotation_options *opts = &annotate_opts; 2086 struct map *map = args->ms.map; 2087 struct dso *dso = map__dso(map); 2088 char *command; 2089 FILE *file; 2090 int lineno = 0; 2091 char *fileloc = NULL; 2092 int nline; 2093 char *line; 2094 size_t line_len; 2095 const char *objdump_argv[] = { 2096 "/bin/sh", 2097 "-c", 2098 NULL, /* Will be the objdump command to run. */ 2099 "--", 2100 NULL, /* Will be the symfs path. */ 2101 NULL, 2102 }; 2103 struct child_process objdump_process; 2104 int err; 2105 2106 err = asprintf(&command, 2107 "%s %s%s --start-address=0x%016" PRIx64 2108 " --stop-address=0x%016" PRIx64 2109 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"", 2110 opts->objdump_path ?: "objdump", 2111 opts->disassembler_style ? "-M " : "", 2112 opts->disassembler_style ?: "", 2113 map__rip_2objdump(map, sym->start), 2114 map__rip_2objdump(map, sym->end), 2115 opts->show_linenr ? "-l" : "", 2116 opts->show_asm_raw ? "" : "--no-show-raw-insn", 2117 opts->annotate_src ? "-S" : "", 2118 opts->prefix ? "--prefix " : "", 2119 opts->prefix ? '"' : ' ', 2120 opts->prefix ?: "", 2121 opts->prefix ? '"' : ' ', 2122 opts->prefix_strip ? "--prefix-strip=" : "", 2123 opts->prefix_strip ?: ""); 2124 2125 if (err < 0) { 2126 pr_err("Failure allocating memory for the command to run\n"); 2127 return err; 2128 } 2129 2130 pr_debug("Executing: %s\n", command); 2131 2132 objdump_argv[2] = command; 2133 objdump_argv[4] = filename; 2134 2135 /* Create a pipe to read from for stdout */ 2136 memset(&objdump_process, 0, sizeof(objdump_process)); 2137 objdump_process.argv = objdump_argv; 2138 objdump_process.out = -1; 2139 objdump_process.err = -1; 2140 objdump_process.no_stderr = 1; 2141 if (start_command(&objdump_process)) { 2142 pr_err("Failure starting to run %s\n", command); 2143 err = -1; 2144 goto out_free_command; 2145 } 2146 2147 file = fdopen(objdump_process.out, "r"); 2148 if (!file) { 2149 pr_err("Failure creating FILE stream for %s\n", command); 2150 /* 2151 * If we were using debug info should retry with 2152 * original binary. 2153 */ 2154 err = -1; 2155 goto out_close_stdout; 2156 } 2157 2158 /* Storage for getline. */ 2159 line = NULL; 2160 line_len = 0; 2161 2162 nline = 0; 2163 while (!feof(file)) { 2164 const char *match; 2165 char *expanded_line; 2166 2167 if (getline(&line, &line_len, file) < 0 || !line) 2168 break; 2169 2170 /* Skip lines containing "filename:" */ 2171 match = strstr(line, filename); 2172 if (match && match[strlen(filename)] == ':') 2173 continue; 2174 2175 expanded_line = strim(line); 2176 expanded_line = expand_tabs(expanded_line, &line, &line_len); 2177 if (!expanded_line) 2178 break; 2179 2180 /* 2181 * The source code line number (lineno) needs to be kept in 2182 * across calls to symbol__parse_objdump_line(), so that it 2183 * can associate it with the instructions till the next one. 2184 * See disasm_line__new() and struct disasm_line::line_nr. 2185 */ 2186 if (symbol__parse_objdump_line(sym, args, expanded_line, 2187 &lineno, &fileloc) < 0) 2188 break; 2189 nline++; 2190 } 2191 free(line); 2192 free(fileloc); 2193 2194 err = finish_command(&objdump_process); 2195 if (err) 2196 pr_err("Error running %s\n", command); 2197 2198 if (nline == 0) { 2199 err = -1; 2200 pr_err("No output from %s\n", command); 2201 } 2202 2203 /* 2204 * kallsyms does not have symbol sizes so there may a nop at the end. 2205 * Remove it. 2206 */ 2207 if (dso__is_kcore(dso)) 2208 delete_last_nop(sym); 2209 2210 fclose(file); 2211 2212 out_close_stdout: 2213 close(objdump_process.out); 2214 2215 out_free_command: 2216 free(command); 2217 return err; 2218 } 2219 2220 int symbol__disassemble(struct symbol *sym, struct annotate_args *args) 2221 { 2222 struct annotation_options *options = args->options; 2223 struct map *map = args->ms.map; 2224 struct dso *dso = map__dso(map); 2225 char symfs_filename[PATH_MAX]; 2226 bool delete_extract = false; 2227 struct kcore_extract kce; 2228 bool decomp = false; 2229 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename)); 2230 2231 if (err) 2232 return err; 2233 2234 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__, 2235 symfs_filename, sym->name, map__unmap_ip(map, sym->start), 2236 map__unmap_ip(map, sym->end)); 2237 2238 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name); 2239 2240 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) { 2241 return symbol__disassemble_bpf(sym, args); 2242 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) { 2243 return symbol__disassemble_bpf_image(sym, args); 2244 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) { 2245 return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE; 2246 } else if (dso__is_kcore(dso)) { 2247 kce.addr = map__rip_2objdump(map, sym->start); 2248 kce.kcore_filename = symfs_filename; 2249 kce.len = sym->end - sym->start; 2250 kce.offs = sym->start; 2251 2252 if (!kcore_extract__create(&kce)) { 2253 delete_extract = true; 2254 strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename)); 2255 } 2256 } else if (dso__needs_decompress(dso)) { 2257 char tmp[KMOD_DECOMP_LEN]; 2258 2259 if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0) 2260 return -1; 2261 2262 decomp = true; 2263 strcpy(symfs_filename, tmp); 2264 } 2265 2266 /* 2267 * For powerpc data type profiling, use the dso__data_read_offset to 2268 * read raw instruction directly and interpret the binary code to 2269 * understand instructions and register fields. For sort keys as type 2270 * and typeoff, disassemble to mnemonic notation is not required in 2271 * case of powerpc. 2272 */ 2273 if (arch__is(args->arch, "powerpc")) { 2274 extern const char *sort_order; 2275 2276 if (sort_order && !strstr(sort_order, "sym")) { 2277 err = symbol__disassemble_raw(symfs_filename, sym, args); 2278 if (err == 0) 2279 goto out_remove_tmp; 2280 2281 err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args); 2282 if (err == 0) 2283 goto out_remove_tmp; 2284 } 2285 } 2286 2287 err = -1; 2288 for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers) && err != 0; i++) { 2289 enum perf_disassembler dis = options->disassemblers[i]; 2290 2291 switch (dis) { 2292 case PERF_DISASM_LLVM: 2293 args->options->disassembler_used = PERF_DISASM_LLVM; 2294 err = symbol__disassemble_llvm(symfs_filename, sym, args); 2295 break; 2296 case PERF_DISASM_CAPSTONE: 2297 args->options->disassembler_used = PERF_DISASM_CAPSTONE; 2298 err = symbol__disassemble_capstone(symfs_filename, sym, args); 2299 break; 2300 case PERF_DISASM_OBJDUMP: 2301 args->options->disassembler_used = PERF_DISASM_OBJDUMP; 2302 err = symbol__disassemble_objdump(symfs_filename, sym, args); 2303 break; 2304 case PERF_DISASM_UNKNOWN: /* End of disassemblers. */ 2305 default: 2306 args->options->disassembler_used = PERF_DISASM_UNKNOWN; 2307 goto out_remove_tmp; 2308 } 2309 if (err == 0) 2310 pr_debug("Disassembled with %s\n", perf_disassembler__strs[dis]); 2311 } 2312 out_remove_tmp: 2313 if (decomp) 2314 unlink(symfs_filename); 2315 2316 if (delete_extract) 2317 kcore_extract__delete(&kce); 2318 2319 return err; 2320 } 2321