1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <linux/string.h>
12 #include <subcmd/run-command.h>
15 #include "annotate-data.h"
19 #include "disasm_bpf.h"
21 #include "dwarf-regs.h"
26 #include "namespaces.h"
31 static regex_t file_lineno;
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;
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);
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);
54 static __attribute__((constructor)) void symbol__init_regexpr(void)
56 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
59 static int arch__grow_instructions(struct arch *arch)
61 struct ins *new_instructions;
62 size_t new_nr_allocated;
64 if (arch->nr_instructions_allocated == 0 && arch->instructions)
65 goto grow_from_non_allocated_table;
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)
72 out_update_instructions:
73 arch->instructions = new_instructions;
74 arch->nr_instructions_allocated = new_nr_allocated;
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)
83 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
84 goto out_update_instructions;
87 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
91 if (arch->nr_instructions == arch->nr_instructions_allocated &&
92 arch__grow_instructions(arch))
95 ins = &arch->instructions[arch->nr_instructions];
96 ins->name = strdup(name);
101 arch->nr_instructions++;
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"
119 static struct arch architectures[] = {
122 .init = arc__annotate_init,
126 .init = arm__annotate_init,
130 .init = arm64__annotate_init,
134 .init = csky__annotate_init,
138 .init = mips__annotate_init,
145 .init = x86__annotate_init,
146 .instructions = x86__instructions,
147 .nr_instructions = ARRAY_SIZE(x86__instructions),
148 .insn_suffix = "bwlq",
151 .register_char = '%',
152 .memory_ref_char = '(',
155 #ifdef HAVE_LIBDW_SUPPORT
156 .update_insn_state = update_insn_state_x86,
161 .init = powerpc__annotate_init,
162 #ifdef HAVE_LIBDW_SUPPORT
163 .update_insn_state = update_insn_state_powerpc,
168 .init = riscv64__annotate_init,
172 .init = s390__annotate_init,
179 .init = sparc__annotate_init,
186 .init = loongarch__annotate_init,
193 static int arch__key_cmp(const void *name, const void *archp)
195 const struct arch *arch = archp;
197 return strcmp(name, arch->name);
200 static int arch__cmp(const void *a, const void *b)
202 const struct arch *aa = a;
203 const struct arch *ab = b;
205 return strcmp(aa->name, ab->name);
208 static void arch__sort(void)
210 const int nmemb = ARRAY_SIZE(architectures);
212 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
215 struct arch *arch__find(const char *name)
217 const int nmemb = ARRAY_SIZE(architectures);
225 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
228 bool arch__is(struct arch *arch, const char *name)
230 return !strcmp(arch->name, name);
233 static void ins_ops__delete(struct ins_operands *ops)
237 zfree(&ops->source.raw);
238 zfree(&ops->source.name);
239 zfree(&ops->target.raw);
240 zfree(&ops->target.name);
243 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
244 struct ins_operands *ops, int max_ins_name)
246 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
249 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
250 struct ins_operands *ops, int max_ins_name)
252 if (ins->ops->scnprintf)
253 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
255 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
258 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
260 if (!arch || !arch->ins_is_fused)
263 return arch->ins_is_fused(arch, ins1, ins2);
266 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
267 struct disasm_line *dl __maybe_unused)
269 char *endptr, *tok, *name;
270 struct map *map = ms->map;
271 struct addr_map_symbol target = {
272 .ms = { .map = map, },
275 ops->target.addr = strtoull(ops->raw, &endptr, 16);
277 name = strchr(endptr, '<');
283 if (arch->objdump.skip_functions_char &&
284 strchr(name, arch->objdump.skip_functions_char))
287 tok = strchr(name, '>');
292 ops->target.name = strdup(name);
295 if (ops->target.name == NULL)
298 target.addr = map__objdump_2mem(map, ops->target.addr);
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;
307 tok = strchr(endptr, '*');
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);
319 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
320 struct ins_operands *ops, int max_ins_name)
323 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
325 if (ops->target.addr == 0)
326 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
328 if (ops->target.name)
329 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
331 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
334 static struct ins_ops call_ops = {
335 .parse = call__parse,
336 .scnprintf = call__scnprintf,
339 bool ins__is_call(const struct ins *ins)
341 return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
345 * Prevents from matching commas in the comment section, e.g.:
346 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
348 * and skip comma as part of function arguments, e.g.:
349 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
351 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
353 if (ops->jump.raw_comment && c > ops->jump.raw_comment)
356 if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
362 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
363 struct disasm_line *dl __maybe_unused)
365 struct map *map = ms->map;
366 struct symbol *sym = ms->sym;
367 struct addr_map_symbol target = {
368 .ms = { .map = map, },
370 const char *c = strchr(ops->raw, ',');
373 ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
374 ops->jump.raw_func_start = strchr(ops->raw, '<');
376 c = validate_comma(c, ops);
379 * Examples of lines to parse for the _cpp_lex_token@@Base
382 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
383 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
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.
390 * skip over possible up to 2 operands to get to address, e.g.:
391 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
394 ops->target.addr = strtoull(c, NULL, 16);
395 if (!ops->target.addr) {
397 c = validate_comma(c, ops);
399 ops->target.addr = strtoull(c, NULL, 16);
402 ops->target.addr = strtoull(ops->raw, NULL, 16);
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);
409 ops->target.outside = target.addr < start || target.addr > end;
412 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
414 cpp_named_operator2name@@Base+0xa72
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?
423 * For now lets just make some progress by marking jumps to outside the
424 * current function as call like.
426 * Actual navigation will come next, with further understanding of how
427 * the symbol searching and disassembly should be done.
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;
433 if (!ops->target.outside) {
434 ops->target.offset = target.addr - start;
435 ops->target.offset_avail = true;
437 ops->target.offset_avail = false;
443 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
444 struct ins_operands *ops, int max_ins_name)
448 if (!ops->target.addr || ops->target.offset < 0)
449 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
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);
454 c = strchr(ops->raw, ',');
455 c = validate_comma(c, ops);
458 const char *c2 = strchr(c + 1, ',');
460 c2 = validate_comma(c2, ops);
461 /* check for 3-op insn */
466 /* mirror arch objdump's space-after-comma style */
471 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
472 ins->name, c ? c - ops->raw : 0, ops->raw,
476 static void jump__delete(struct ins_operands *ops __maybe_unused)
479 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
480 * raw string, don't free them.
484 static struct ins_ops jump_ops = {
485 .free = jump__delete,
486 .parse = jump__parse,
487 .scnprintf = jump__scnprintf,
490 bool ins__is_jump(const struct ins *ins)
492 return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
495 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
497 char *endptr, *name, *t;
499 if (strstr(raw, "(%rip)") == NULL)
502 *addrp = strtoull(comment, &endptr, 16);
503 if (endptr == comment)
505 name = strchr(endptr, '<');
511 t = strchr(name, '>');
516 *namep = strdup(name);
522 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
523 struct disasm_line *dl __maybe_unused)
525 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
526 if (ops->locked.ops == NULL)
529 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
532 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name, 0);
534 if (ops->locked.ins.ops == NULL)
537 if (ops->locked.ins.ops->parse &&
538 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms, NULL) < 0)
544 zfree(&ops->locked.ops);
548 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
549 struct ins_operands *ops, int max_ins_name)
553 if (ops->locked.ins.ops == NULL)
554 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
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);
561 static void lock__delete(struct ins_operands *ops)
563 struct ins *ins = &ops->locked.ins;
565 if (ins->ops && ins->ops->free)
566 ins->ops->free(ops->locked.ops);
568 ins_ops__delete(ops->locked.ops);
570 zfree(&ops->locked.ops);
571 zfree(&ops->locked.ins.name);
572 zfree(&ops->target.raw);
573 zfree(&ops->target.name);
576 static struct ins_ops lock_ops = {
577 .free = lock__delete,
578 .parse = lock__parse,
579 .scnprintf = lock__scnprintf,
583 * Check if the operand has more than one registers like x86 SIB addressing:
584 * 0x1234(%rax, %rbx, 8)
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.
589 static bool check_multi_regs(struct arch *arch, const char *op)
593 if (arch->objdump.register_char == 0)
596 if (arch->objdump.memory_ref_char) {
597 op = strchr(op, arch->objdump.memory_ref_char);
602 while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
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)
613 char *s = strchr(ops->raw, ','), *target, *comment, prev;
621 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
622 * then it needs to have the closing parenthesis.
624 if (strchr(ops->raw, '(')) {
626 s = strchr(ops->raw, ')');
627 if (s == NULL || s[1] != ',')
632 ops->source.raw = strdup(ops->raw);
635 if (ops->source.raw == NULL)
638 ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
640 target = skip_spaces(++s);
641 comment = strchr(s, arch->objdump.comment_char);
646 s = strchr(s, '\0') - 1;
648 while (s > target && isspace(s[0]))
654 ops->target.raw = strdup(target);
657 if (ops->target.raw == NULL)
658 goto out_free_source;
660 ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
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);
672 zfree(&ops->source.raw);
676 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
677 struct ins_operands *ops, int max_ins_name)
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);
684 static struct ins_ops mov_ops = {
686 .scnprintf = mov__scnprintf,
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
695 static int arithmetic__scnprintf(struct ins *ins, char *bf, size_t size,
696 struct ins_operands *ops, int max_ins_name)
698 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
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.
709 * Dont set multi regs for 4 cases since it has only one operand
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 )
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)
719 int opcode = PPC_OP(dl->raw.raw_insn);
721 ops->source.mem_ref = false;
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;
728 ops->target.mem_ref = false;
729 ops->target.multi_regs = false;
734 static struct ins_ops arithmetic_ops = {
735 .parse = arithmetic__parse,
736 .scnprintf = arithmetic__scnprintf,
739 static int load_store__scnprintf(struct ins *ins, char *bf, size_t size,
740 struct ins_operands *ops, int max_ins_name)
742 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
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
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)
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;
762 ops->target.mem_ref = false;
763 ops->target.multi_regs = false;
768 static struct ins_ops load_store_ops = {
769 .parse = load_store__parse,
770 .scnprintf = load_store__scnprintf,
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)
776 char *target, *comment, *s, prev;
778 target = s = ops->raw;
780 while (s[0] != '\0' && !isspace(s[0]))
785 ops->target.raw = strdup(target);
788 if (ops->target.raw == NULL)
791 comment = strchr(s, arch->objdump.comment_char);
795 comment = skip_spaces(comment);
796 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
801 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
802 struct ins_operands *ops, int max_ins_name)
804 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
805 ops->target.name ?: ops->target.raw);
808 static struct ins_ops dec_ops = {
810 .scnprintf = dec__scnprintf,
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)
816 return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
819 static struct ins_ops nop_ops = {
820 .scnprintf = nop__scnprintf,
823 static struct ins_ops ret_ops = {
824 .scnprintf = ins__raw_scnprintf,
827 bool ins__is_nop(const struct ins *ins)
829 return ins->ops == &nop_ops;
832 bool ins__is_ret(const struct ins *ins)
834 return ins->ops == &ret_ops;
837 bool ins__is_lock(const struct ins *ins)
839 return ins->ops == &lock_ops;
842 static int ins__key_cmp(const void *name, const void *insp)
844 const struct ins *ins = insp;
846 return strcmp(name, ins->name);
849 static int ins__cmp(const void *a, const void *b)
851 const struct ins *ia = a;
852 const struct ins *ib = b;
854 return strcmp(ia->name, ib->name);
857 static void ins__sort(struct arch *arch)
859 const int nmemb = arch->nr_instructions;
861 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
864 static struct ins_ops *__ins__find(struct arch *arch, const char *name, struct disasm_line *dl)
867 const int nmemb = arch->nr_instructions;
869 if (arch__is(arch, "powerpc")) {
871 * For powerpc, identify the instruction ops
872 * from the opcode using raw_insn.
876 ops = check_ppc_insn(dl);
881 if (!arch->sorted_instructions) {
883 arch->sorted_instructions = true;
886 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
890 if (arch->insn_suffix) {
893 size_t len = strlen(name);
895 if (len == 0 || len >= sizeof(tmp))
898 suffix = name[len - 1];
899 if (strchr(arch->insn_suffix, suffix) == NULL)
903 tmp[len - 1] = '\0'; /* remove the suffix and check again */
905 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
907 return ins ? ins->ops : NULL;
910 struct ins_ops *ins__find(struct arch *arch, const char *name, struct disasm_line *dl)
912 struct ins_ops *ops = __ins__find(arch, name, dl);
914 if (!ops && arch->associate_instruction_ops)
915 ops = arch->associate_instruction_ops(arch, name);
920 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
922 dl->ins.ops = ins__find(arch, dl->ins.name, dl);
927 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms, dl) < 0)
931 static int disasm_line__parse(char *line, const char **namep, char **rawp)
933 char tmp, *name = skip_spaces(line);
940 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
945 *namep = strdup(name);
951 *rawp = strim(*rawp);
960 * Parses the result captured from symbol__disassemble_*
961 * Example, line read from DSO file in powerpc:
963 * opcode: fetched from arch specific get_opcode_insn
964 * rawp_insn: e8810138
966 * rawp_insn is used later to extract the reg/offset fields
968 #define PPC_OP(op) (((op) >> 26) & 0x3F)
971 static int disasm_line__parse_powerpc(struct disasm_line *dl)
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);
980 if (strlen(line) > RAW_BYTES)
983 if (name_raw_insn[0] == '\0')
987 disasm_line__parse(name, namep, rawp);
991 tmp_raw_insn = strndup(name_raw_insn, 11);
992 if (tmp_raw_insn == NULL)
995 remove_spaces(tmp_raw_insn);
997 sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn);
999 dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn);
1004 static void annotation_line__init(struct annotation_line *al,
1005 struct annotate_args *args,
1008 al->offset = args->offset;
1009 al->line = strdup(args->line);
1010 al->line_nr = args->line_nr;
1011 al->fileloc = args->fileloc;
1015 static void annotation_line__exit(struct annotation_line *al)
1017 zfree_srcline(&al->path);
1020 zfree(&al->br_cntr);
1023 static size_t disasm_line_size(int nr)
1025 struct annotation_line *al;
1027 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
1031 * Allocating the disasm annotation line data with
1032 * following structure:
1034 * -------------------------------------------
1035 * struct disasm_line | struct annotation_line
1036 * -------------------------------------------
1038 * We have 'struct annotation_line' member as last member
1039 * of 'struct disasm_line' to have an easy access.
1041 struct disasm_line *disasm_line__new(struct annotate_args *args)
1043 struct disasm_line *dl = NULL;
1044 struct annotation *notes = symbol__annotation(args->ms.sym);
1045 int nr = notes->src->nr_events;
1047 dl = zalloc(disasm_line_size(nr));
1051 annotation_line__init(&dl->al, args, nr);
1052 if (dl->al.line == NULL)
1055 if (args->offset != -1) {
1056 if (arch__is(args->arch, "powerpc")) {
1057 if (disasm_line__parse_powerpc(dl) < 0)
1059 } else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
1062 disasm_line__init_ins(dl, args->arch, &args->ms);
1068 zfree(&dl->al.line);
1074 void disasm_line__free(struct disasm_line *dl)
1076 if (dl->ins.ops && dl->ins.ops->free)
1077 dl->ins.ops->free(&dl->ops);
1079 ins_ops__delete(&dl->ops);
1080 zfree(&dl->ins.name);
1081 annotation_line__exit(&dl->al);
1085 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
1087 if (raw || !dl->ins.ops)
1088 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
1090 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
1094 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
1095 * which looks like following
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
1106 * it will be parsed and saved into struct disasm_line as
1107 * <offset> <name> <ops.raw>
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.
1113 static int symbol__parse_objdump_line(struct symbol *sym,
1114 struct annotate_args *args,
1115 char *parsed_line, int *line_nr, char **fileloc)
1117 struct map *map = args->ms.map;
1118 struct annotation *notes = symbol__annotation(sym);
1119 struct disasm_line *dl;
1121 s64 line_ip, offset = -1;
1122 regmatch_t match[2];
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);
1128 *fileloc = strdup(parsed_line);
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);
1138 offset = line_ip - start;
1139 if ((u64)line_ip < start || (u64)line_ip >= end)
1142 parsed_line = tmp + 1;
1145 args->offset = offset;
1146 args->line = parsed_line;
1147 args->line_nr = *line_nr;
1148 args->fileloc = *fileloc;
1151 dl = disasm_line__new(args);
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;
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, },
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;
1175 annotation_line__add(&dl->al, ¬es->src->source);
1179 static void delete_last_nop(struct symbol *sym)
1181 struct annotation *notes = symbol__annotation(sym);
1182 struct list_head *list = ¬es->src->source;
1183 struct disasm_line *dl;
1185 while (!list_empty(list)) {
1186 dl = list_entry(list->prev, struct disasm_line, al.node);
1189 if (!ins__is_nop(&dl->ins))
1192 if (!strstr(dl->al.line, " nop ") &&
1193 !strstr(dl->al.line, " nopl ") &&
1194 !strstr(dl->al.line, " nopw "))
1198 list_del_init(&dl->al.node);
1199 disasm_line__free(dl);
1203 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1205 struct dso *dso = map__dso(ms->map);
1207 BUG_ON(buflen == 0);
1210 str_error_r(errnum, buf, buflen);
1215 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1216 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1217 char *build_id_msg = NULL;
1219 if (dso__has_build_id(dso)) {
1220 build_id__sprintf(dso__bid(dso), bf + 15);
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"
1227 " perf buildid-cache -vu vmlinux\n\n"
1229 " --vmlinux vmlinux\n", build_id_msg ?: "");
1232 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1233 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1235 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1236 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1238 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1239 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1241 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1242 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
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));
1248 case SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE:
1249 scnprintf(buf, buflen, "Couldn't determine the file %s type.", dso__long_name(dso));
1252 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1259 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1261 char linkname[PATH_MAX];
1262 char *build_id_filename;
1263 char *build_id_path = NULL;
1267 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1268 !dso__is_kcore(dso))
1269 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
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);
1276 if (dso__has_build_id(dso))
1281 build_id_path = strdup(filename);
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.
1290 pos = strrchr(build_id_path, '/');
1291 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1292 dirname(build_id_path);
1294 if (dso__is_kcore(dso))
1297 len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1301 linkname[len] = '\0';
1302 if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1303 access(filename, R_OK)) {
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.
1310 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1311 snprintf(filename, filename_size, "%s", dso__long_name(dso));
1313 __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
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);
1319 strlcpy(filename, new_name, filename_size);
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);
1328 free(build_id_path);
1332 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1333 #include <capstone/capstone.h>
1335 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style);
1337 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1340 struct annotation_options *opt = args->options;
1341 cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1343 /* TODO: support more architectures */
1344 if (!arch__is(args->arch, "x86"))
1347 if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1350 if (!opt->disassembler_style ||
1351 !strcmp(opt->disassembler_style, "att"))
1352 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1355 * Resolving address operands to symbols is implemented
1356 * on x86 by investigating instruction details.
1358 cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1364 #if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT)
1365 struct find_file_offset_data {
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)
1373 struct find_file_offset_data *data = arg;
1375 if (start <= data->ip && data->ip < start + len) {
1376 data->offset = pgoff + data->ip - start;
1383 read_symbol(const char *filename, struct map *map, struct symbol *sym,
1384 u64 *len, bool *is_64bit)
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);
1392 struct find_file_offset_data data = {
1398 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1399 fd = open(filename, O_RDONLY);
1400 nsinfo__mountns_exit(&nsc);
1404 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1413 count = pread(fd, buf, *len, data.offset);
1417 if ((u64)count != *len)
1430 #if !defined(HAVE_LIBCAPSTONE_SUPPORT) || !defined(HAVE_LIBLLVM_SUPPORT)
1431 static void symbol__disassembler_missing(const char *disassembler, const char *filename,
1434 pr_debug("The %s disassembler isn't linked in for %s in %s\n",
1435 disassembler, sym->name, filename);
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)
1444 struct map *map = args->ms.map;
1447 /* TODO: support more architectures */
1448 if (!arch__is(args->arch, "x86"))
1451 if (insn->detail == NULL)
1454 for (i = 0; i < insn->detail->x86.op_count; i++) {
1455 cs_x86_op *op = &insn->detail->x86.operands[i];
1458 if (op->type != X86_OP_MEM)
1461 /* only print RIP-based global symbols for now */
1462 if (op->mem.base != X86_REG_RIP)
1465 /* get the target address */
1466 orig_addr = addr + insn->size + op->mem.disp;
1467 addr = map__objdump_2mem(map, orig_addr);
1469 if (dso__kernel(map__dso(map))) {
1471 * The kernel maps can be splitted into sections,
1472 * let's find the map first and the search the symbol.
1474 map = maps__find(map__kmaps(map), addr);
1479 /* convert it to map-relative address for search */
1480 addr = map__map_ip(map, addr);
1482 sym = map__find_symbol(map, addr);
1486 if (addr == sym->start) {
1487 scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1488 orig_addr, sym->name);
1490 scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1491 orig_addr, sym->name, addr - sym->start);
1497 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
1498 struct annotate_args *args)
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;
1509 bool is_64bit = false;
1510 bool needs_cs_close = false;
1512 struct find_file_offset_data data = {
1516 char disasm_buf[512];
1517 struct disasm_line *dl;
1519 bool disassembler_style = false;
1521 if (args->options->objdump_path)
1524 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1525 fd = open(filename, O_RDONLY);
1526 nsinfo__mountns_exit(&nsc);
1530 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1534 if (!args->options->disassembler_style ||
1535 !strcmp(args->options->disassembler_style, "att"))
1536 disassembler_style = true;
1538 if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
1541 needs_cs_close = true;
1547 count = pread(fd, buf, len, data.offset);
1551 if ((u64)count != len)
1556 /* add the function address and name */
1557 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1561 args->line = disasm_buf;
1563 args->fileloc = NULL;
1566 dl = disasm_line__new(args);
1570 annotation_line__add(&dl->al, ¬es->src->source);
1573 * TODO: enable disassm for powerpc
1574 * count = cs_disasm(handle, buf, len, start, len, &insn);
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
1583 for (i = 0, offset = 0; i < count; i++) {
1584 args->offset = offset;
1585 sprintf(args->line, "%x", line[i]);
1587 dl = disasm_line__new(args);
1591 annotation_line__add(&dl->al, ¬es->src->source);
1596 /* It failed in the middle */
1597 if (offset != len) {
1598 struct list_head *list = ¬es->src->source;
1600 /* Discard all lines and fallback to objdump */
1601 while (!list_empty(list)) {
1602 dl = list_first_entry(list, struct disasm_line, al.node);
1604 list_del_init(&dl->al.node);
1605 disasm_line__free(dl);
1614 return count < 0 ? count : 0;
1623 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1624 struct annotate_args *args)
1626 struct annotation *notes = symbol__annotation(sym);
1627 struct map *map = args->ms.map;
1628 u64 start = map__rip_2objdump(map, sym->start);
1631 int i, count, free_count;
1632 bool is_64bit = false;
1633 bool needs_cs_close = false;
1636 cs_insn *insn = NULL;
1637 char disasm_buf[512];
1638 struct disasm_line *dl;
1640 if (args->options->objdump_path)
1643 buf = read_symbol(filename, map, sym, &len, &is_64bit);
1647 /* add the function address and name */
1648 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1652 args->line = disasm_buf;
1654 args->fileloc = NULL;
1657 dl = disasm_line__new(args);
1661 annotation_line__add(&dl->al, ¬es->src->source);
1663 if (open_capstone_handle(args, is_64bit, &handle) < 0)
1666 needs_cs_close = true;
1668 free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
1669 for (i = 0, offset = 0; i < count; i++) {
1672 printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1674 insn[i].mnemonic, insn[i].op_str);
1675 print_capstone_detail(&insn[i], disasm_buf + printed,
1676 sizeof(disasm_buf) - printed, args,
1679 args->offset = offset;
1680 args->line = disasm_buf;
1682 dl = disasm_line__new(args);
1686 annotation_line__add(&dl->al, ¬es->src->source);
1688 offset += insn[i].size;
1691 /* It failed in the middle: probably due to unknown instructions */
1692 if (offset != len) {
1693 struct list_head *list = ¬es->src->source;
1695 /* Discard all lines and fallback to objdump */
1696 while (!list_empty(list)) {
1697 dl = list_first_entry(list, struct disasm_line, al.node);
1699 list_del_init(&dl->al.node);
1700 disasm_line__free(dl);
1706 if (needs_cs_close) {
1709 cs_free(insn, free_count);
1712 return count < 0 ? count : 0;
1715 if (needs_cs_close) {
1716 struct disasm_line *tmp;
1719 * It probably failed in the middle of the above loop.
1720 * Release any resources it might add.
1722 list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) {
1723 list_del(&dl->al.node);
1724 disasm_line__free(dl);
1730 #else // HAVE_LIBCAPSTONE_SUPPORT
1731 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1732 struct annotate_args *args __maybe_unused)
1734 symbol__disassembler_missing("capstone", filename, sym);
1738 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
1739 struct annotate_args *args __maybe_unused)
1741 symbol__disassembler_missing("capstone powerpc", filename, sym);
1744 #endif // HAVE_LIBCAPSTONE_SUPPORT
1746 static int symbol__disassemble_raw(char *filename, struct symbol *sym,
1747 struct annotate_args *args)
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;
1758 char disasm_buf[512];
1759 struct disasm_line *dl;
1762 /* Return if objdump is specified explicitly */
1763 if (args->options->objdump_path)
1766 pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename);
1772 count = dso__data_read_offset(dso, NULL, sym->start, buf, len);
1776 if ((u64)count != len)
1779 /* add the function address and name */
1780 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1784 args->line = disasm_buf;
1786 args->fileloc = NULL;
1789 dl = disasm_line__new(args);
1793 annotation_line__add(&dl->al, ¬es->src->source);
1795 /* Each raw instruction is 4 byte */
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);
1805 annotation_line__add(&dl->al, ¬es->src->source);
1809 /* It failed in the middle */
1810 if (offset != len) {
1811 struct list_head *list = ¬es->src->source;
1813 /* Discard all lines and fallback to objdump */
1814 while (!list_empty(list)) {
1815 dl = list_first_entry(list, struct disasm_line, al.node);
1817 list_del_init(&dl->al.node);
1818 disasm_line__free(dl);
1825 return count < 0 ? count : 0;
1832 #ifdef HAVE_LIBLLVM_SUPPORT
1833 #include <llvm-c/Disassembler.h>
1834 #include <llvm-c/Target.h>
1835 #include "util/llvm-c-helpers.h"
1837 struct symbol_lookup_storage {
1839 u64 pcrel_load_addr;
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.
1851 symbol_lookup_callback(void *disinfo, uint64_t value,
1853 uint64_t address __maybe_unused,
1854 const char **ref __maybe_unused)
1856 struct symbol_lookup_storage *storage = disinfo;
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;
1866 static int symbol__disassemble_llvm(char *filename, struct symbol *sym,
1867 struct annotate_args *args)
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);
1878 char disasm_buf[2048];
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;
1887 if (args->options->objdump_path)
1890 LLVMInitializeAllTargetInfos();
1891 LLVMInitializeAllTargetMCs();
1892 LLVMInitializeAllDisassemblers();
1894 buf = read_symbol(filename, map, sym, &len, &is_64bit);
1898 if (arch__is(args->arch, "x86")) {
1900 scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux");
1902 scnprintf(triplet, sizeof(triplet), "i686-pc-linux");
1904 scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
1908 disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL,
1909 symbol_lookup_callback);
1913 if (args->options->disassembler_style &&
1914 !strcmp(args->options->disassembler_style, "intel"))
1915 LLVMSetDisasmOptions(disasm,
1916 LLVMDisassembler_Option_AsmPrinterVariant);
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).
1924 LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);
1926 /* add the function address and name */
1927 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1931 args->line = disasm_buf;
1933 args->fileloc = NULL;
1936 dl = disasm_line__new(args);
1940 annotation_line__add(&dl->al, ¬es->src->source);
1943 for (u64 offset = 0; offset < len; ) {
1944 unsigned int ins_len;
1946 storage.branch_addr = 0;
1947 storage.pcrel_load_addr = 0;
1949 ins_len = LLVMDisasmInstruction(disasm, buf + offset,
1951 disasm_buf, sizeof(disasm_buf));
1954 disasm_len = strlen(disasm_buf);
1956 if (storage.branch_addr != 0) {
1957 char *name = llvm_name_for_code(dso, filename,
1958 storage.branch_addr);
1960 disasm_len += scnprintf(disasm_buf + disasm_len,
1961 sizeof(disasm_buf) -
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,
1973 storage.pcrel_load_addr);
1975 disasm_len += scnprintf(disasm_buf + disasm_len,
1976 sizeof(disasm_buf) -
1983 args->offset = offset;
1984 args->line = expand_tabs(disasm_buf, &line_storage,
1987 args->fileloc = NULL;
1990 llvm_addr2line(filename, pc, &args->fileloc,
1991 (unsigned int *)&args->line_nr, false, NULL);
1993 dl = disasm_line__new(args);
1997 annotation_line__add(&dl->al, ¬es->src->source);
1999 free(args->fileloc);
2007 LLVMDisasmDispose(disasm);
2012 #else // HAVE_LIBLLVM_SUPPORT
2013 static int symbol__disassemble_llvm(char *filename, struct symbol *sym,
2014 struct annotate_args *args __maybe_unused)
2016 symbol__disassembler_missing("LLVM", filename, sym);
2019 #endif // HAVE_LIBLLVM_SUPPORT
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.
2026 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
2028 size_t i, src, dst, len, new_storage_len, num_tabs;
2030 size_t line_len = strlen(line);
2032 for (num_tabs = 0, i = 0; i < line_len; i++)
2033 if (line[i] == '\t')
2040 * Space for the line and '\0', less the leading and trailing
2041 * spaces. Each tab may introduce 7 additional spaces.
2043 new_storage_len = line_len + 1 + (num_tabs * 7);
2045 new_line = malloc(new_storage_len);
2046 if (new_line == NULL) {
2047 pr_err("Failure allocating memory for tab expansion\n");
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
2056 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
2057 if (line[i] == '\t') {
2059 memcpy(&new_line[dst], &line[src], len);
2061 new_line[dst++] = ' ';
2062 while (dst % 8 != 0)
2063 new_line[dst++] = ' ';
2069 /* Expand the last region. */
2070 len = line_len - src;
2071 memcpy(&new_line[dst], &line[src], len);
2073 new_line[dst] = '\0';
2076 *storage = new_line;
2077 *storage_len = new_storage_len;
2081 static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
2082 struct annotate_args *args)
2084 struct annotation_options *opts = &annotate_opts;
2085 struct map *map = args->ms.map;
2086 struct dso *dso = map__dso(map);
2090 char *fileloc = NULL;
2094 const char *objdump_argv[] = {
2097 NULL, /* Will be the objdump command to run. */
2099 NULL, /* Will be the symfs path. */
2102 struct child_process objdump_process;
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 ? '"' : ' ',
2120 opts->prefix ? '"' : ' ',
2121 opts->prefix_strip ? "--prefix-strip=" : "",
2122 opts->prefix_strip ?: "");
2125 pr_err("Failure allocating memory for the command to run\n");
2129 pr_debug("Executing: %s\n", command);
2131 objdump_argv[2] = command;
2132 objdump_argv[4] = filename;
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);
2143 goto out_free_command;
2146 file = fdopen(objdump_process.out, "r");
2148 pr_err("Failure creating FILE stream for %s\n", command);
2150 * If we were using debug info should retry with
2154 goto out_close_stdout;
2157 /* Storage for getline. */
2162 while (!feof(file)) {
2164 char *expanded_line;
2166 if (getline(&line, &line_len, file) < 0 || !line)
2169 /* Skip lines containing "filename:" */
2170 match = strstr(line, filename);
2171 if (match && match[strlen(filename)] == ':')
2174 expanded_line = strim(line);
2175 expanded_line = expand_tabs(expanded_line, &line, &line_len);
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.
2185 if (symbol__parse_objdump_line(sym, args, expanded_line,
2186 &lineno, &fileloc) < 0)
2193 err = finish_command(&objdump_process);
2195 pr_err("Error running %s\n", command);
2199 pr_err("No output from %s\n", command);
2203 * kallsyms does not have symbol sizes so there may a nop at the end.
2206 if (dso__is_kcore(dso))
2207 delete_last_nop(sym);
2212 close(objdump_process.out);
2219 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
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));
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));
2237 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name);
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;
2251 if (!kcore_extract__create(&kce)) {
2252 delete_extract = true;
2253 strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename));
2255 } else if (dso__needs_decompress(dso)) {
2256 char tmp[KMOD_DECOMP_LEN];
2258 if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0)
2262 strcpy(symfs_filename, tmp);
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
2272 if (arch__is(args->arch, "powerpc")) {
2273 extern const char *sort_order;
2275 if (sort_order && !strstr(sort_order, "sym")) {
2276 err = symbol__disassemble_raw(symfs_filename, sym, args);
2278 goto out_remove_tmp;
2280 err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
2282 goto out_remove_tmp;
2287 for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers) && err != 0; i++) {
2288 enum perf_disassembler dis = options->disassemblers[i];
2291 case PERF_DISASM_LLVM:
2292 err = symbol__disassemble_llvm(symfs_filename, sym, args);
2294 case PERF_DISASM_CAPSTONE:
2295 err = symbol__disassemble_capstone(symfs_filename, sym, args);
2297 case PERF_DISASM_OBJDUMP:
2298 err = symbol__disassemble_objdump(symfs_filename, sym, args);
2300 case PERF_DISASM_UNKNOWN: /* End of disassemblers. */
2302 goto out_remove_tmp;
2305 pr_debug("Disassembled with %s\n", perf_disassembler__strs[dis]);
2309 unlink(symfs_filename);
2312 kcore_extract__delete(&kce);