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));
1249 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1256 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1258 char linkname[PATH_MAX];
1259 char *build_id_filename;
1260 char *build_id_path = NULL;
1264 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1265 !dso__is_kcore(dso))
1266 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1268 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1269 if (build_id_filename) {
1270 __symbol__join_symfs(filename, filename_size, build_id_filename);
1271 free(build_id_filename);
1273 if (dso__has_build_id(dso))
1278 build_id_path = strdup(filename);
1283 * old style build-id cache has name of XX/XXXXXXX.. while
1284 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1285 * extract the build-id part of dirname in the new style only.
1287 pos = strrchr(build_id_path, '/');
1288 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1289 dirname(build_id_path);
1291 if (dso__is_kcore(dso))
1294 len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1298 linkname[len] = '\0';
1299 if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1300 access(filename, R_OK)) {
1303 * If we don't have build-ids or the build-id file isn't in the
1304 * cache, or is just a kallsyms file, well, lets hope that this
1305 * DSO is the same as when 'perf record' ran.
1307 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1308 snprintf(filename, filename_size, "%s", dso__long_name(dso));
1310 __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1312 mutex_lock(dso__lock(dso));
1313 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
1314 char *new_name = dso__filename_with_chroot(dso, filename);
1316 strlcpy(filename, new_name, filename_size);
1320 mutex_unlock(dso__lock(dso));
1321 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1322 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
1325 free(build_id_path);
1329 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1330 #include <capstone/capstone.h>
1332 int capstone_init(struct machine *machine, csh *cs_handle, bool is64, bool disassembler_style);
1334 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1337 struct annotation_options *opt = args->options;
1338 cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1340 /* TODO: support more architectures */
1341 if (!arch__is(args->arch, "x86"))
1344 if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1347 if (!opt->disassembler_style ||
1348 !strcmp(opt->disassembler_style, "att"))
1349 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1352 * Resolving address operands to symbols is implemented
1353 * on x86 by investigating instruction details.
1355 cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1361 #if defined(HAVE_LIBCAPSTONE_SUPPORT) || defined(HAVE_LIBLLVM_SUPPORT)
1362 struct find_file_offset_data {
1367 /* This will be called for each PHDR in an ELF binary */
1368 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1370 struct find_file_offset_data *data = arg;
1372 if (start <= data->ip && data->ip < start + len) {
1373 data->offset = pgoff + data->ip - start;
1380 read_symbol(const char *filename, struct map *map, struct symbol *sym,
1381 u64 *len, bool *is_64bit)
1383 struct dso *dso = map__dso(map);
1384 struct nscookie nsc;
1385 u64 start = map__rip_2objdump(map, sym->start);
1386 u64 end = map__rip_2objdump(map, sym->end);
1389 struct find_file_offset_data data = {
1395 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1396 fd = open(filename, O_RDONLY);
1397 nsinfo__mountns_exit(&nsc);
1401 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1410 count = pread(fd, buf, *len, data.offset);
1414 if ((u64)count != *len)
1427 #if !defined(HAVE_LIBCAPSTONE_SUPPORT) || !defined(HAVE_LIBLLVM_SUPPORT)
1428 static void symbol__disassembler_missing(const char *disassembler, const char *filename,
1431 pr_debug("The %s disassembler isn't linked in for %s in %s\n",
1432 disassembler, sym->name, filename);
1436 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1437 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1438 struct annotate_args *args, u64 addr)
1441 struct map *map = args->ms.map;
1444 /* TODO: support more architectures */
1445 if (!arch__is(args->arch, "x86"))
1448 if (insn->detail == NULL)
1451 for (i = 0; i < insn->detail->x86.op_count; i++) {
1452 cs_x86_op *op = &insn->detail->x86.operands[i];
1455 if (op->type != X86_OP_MEM)
1458 /* only print RIP-based global symbols for now */
1459 if (op->mem.base != X86_REG_RIP)
1462 /* get the target address */
1463 orig_addr = addr + insn->size + op->mem.disp;
1464 addr = map__objdump_2mem(map, orig_addr);
1466 if (dso__kernel(map__dso(map))) {
1468 * The kernel maps can be splitted into sections,
1469 * let's find the map first and the search the symbol.
1471 map = maps__find(map__kmaps(map), addr);
1476 /* convert it to map-relative address for search */
1477 addr = map__map_ip(map, addr);
1479 sym = map__find_symbol(map, addr);
1483 if (addr == sym->start) {
1484 scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1485 orig_addr, sym->name);
1487 scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1488 orig_addr, sym->name, addr - sym->start);
1494 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
1495 struct annotate_args *args)
1497 struct annotation *notes = symbol__annotation(sym);
1498 struct map *map = args->ms.map;
1499 struct dso *dso = map__dso(map);
1500 struct nscookie nsc;
1501 u64 start = map__rip_2objdump(map, sym->start);
1502 u64 end = map__rip_2objdump(map, sym->end);
1503 u64 len = end - start;
1506 bool is_64bit = false;
1507 bool needs_cs_close = false;
1509 struct find_file_offset_data data = {
1513 char disasm_buf[512];
1514 struct disasm_line *dl;
1516 bool disassembler_style = false;
1518 if (args->options->objdump_path)
1521 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1522 fd = open(filename, O_RDONLY);
1523 nsinfo__mountns_exit(&nsc);
1527 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1531 if (!args->options->disassembler_style ||
1532 !strcmp(args->options->disassembler_style, "att"))
1533 disassembler_style = true;
1535 if (capstone_init(maps__machine(args->ms.maps), &handle, is_64bit, disassembler_style) < 0)
1538 needs_cs_close = true;
1544 count = pread(fd, buf, len, data.offset);
1548 if ((u64)count != len)
1553 /* add the function address and name */
1554 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1558 args->line = disasm_buf;
1560 args->fileloc = NULL;
1563 dl = disasm_line__new(args);
1567 annotation_line__add(&dl->al, ¬es->src->source);
1570 * TODO: enable disassm for powerpc
1571 * count = cs_disasm(handle, buf, len, start, len, &insn);
1573 * For now, only binary code is saved in disassembled line
1574 * to be used in "type" and "typeoff" sort keys. Each raw code
1575 * is 32 bit instruction. So use "len/4" to get the number of
1580 for (i = 0, offset = 0; i < count; i++) {
1581 args->offset = offset;
1582 sprintf(args->line, "%x", line[i]);
1584 dl = disasm_line__new(args);
1588 annotation_line__add(&dl->al, ¬es->src->source);
1593 /* It failed in the middle */
1594 if (offset != len) {
1595 struct list_head *list = ¬es->src->source;
1597 /* Discard all lines and fallback to objdump */
1598 while (!list_empty(list)) {
1599 dl = list_first_entry(list, struct disasm_line, al.node);
1601 list_del_init(&dl->al.node);
1602 disasm_line__free(dl);
1611 return count < 0 ? count : 0;
1620 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1621 struct annotate_args *args)
1623 struct annotation *notes = symbol__annotation(sym);
1624 struct map *map = args->ms.map;
1625 u64 start = map__rip_2objdump(map, sym->start);
1628 int i, count, free_count;
1629 bool is_64bit = false;
1630 bool needs_cs_close = false;
1633 cs_insn *insn = NULL;
1634 char disasm_buf[512];
1635 struct disasm_line *dl;
1637 if (args->options->objdump_path)
1640 buf = read_symbol(filename, map, sym, &len, &is_64bit);
1644 /* add the function address and name */
1645 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1649 args->line = disasm_buf;
1651 args->fileloc = NULL;
1654 dl = disasm_line__new(args);
1658 annotation_line__add(&dl->al, ¬es->src->source);
1660 if (open_capstone_handle(args, is_64bit, &handle) < 0)
1663 needs_cs_close = true;
1665 free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
1666 for (i = 0, offset = 0; i < count; i++) {
1669 printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1671 insn[i].mnemonic, insn[i].op_str);
1672 print_capstone_detail(&insn[i], disasm_buf + printed,
1673 sizeof(disasm_buf) - printed, args,
1676 args->offset = offset;
1677 args->line = disasm_buf;
1679 dl = disasm_line__new(args);
1683 annotation_line__add(&dl->al, ¬es->src->source);
1685 offset += insn[i].size;
1688 /* It failed in the middle: probably due to unknown instructions */
1689 if (offset != len) {
1690 struct list_head *list = ¬es->src->source;
1692 /* Discard all lines and fallback to objdump */
1693 while (!list_empty(list)) {
1694 dl = list_first_entry(list, struct disasm_line, al.node);
1696 list_del_init(&dl->al.node);
1697 disasm_line__free(dl);
1703 if (needs_cs_close) {
1706 cs_free(insn, free_count);
1709 return count < 0 ? count : 0;
1712 if (needs_cs_close) {
1713 struct disasm_line *tmp;
1716 * It probably failed in the middle of the above loop.
1717 * Release any resources it might add.
1719 list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) {
1720 list_del(&dl->al.node);
1721 disasm_line__free(dl);
1727 #else // HAVE_LIBCAPSTONE_SUPPORT
1728 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1729 struct annotate_args *args __maybe_unused)
1731 symbol__disassembler_missing("capstone", filename, sym);
1735 static int symbol__disassemble_capstone_powerpc(char *filename, struct symbol *sym,
1736 struct annotate_args *args __maybe_unused)
1738 symbol__disassembler_missing("capstone powerpc", filename, sym);
1741 #endif // HAVE_LIBCAPSTONE_SUPPORT
1743 static int symbol__disassemble_raw(char *filename, struct symbol *sym,
1744 struct annotate_args *args)
1746 struct annotation *notes = symbol__annotation(sym);
1747 struct map *map = args->ms.map;
1748 struct dso *dso = map__dso(map);
1749 u64 start = map__rip_2objdump(map, sym->start);
1750 u64 end = map__rip_2objdump(map, sym->end);
1751 u64 len = end - start;
1755 char disasm_buf[512];
1756 struct disasm_line *dl;
1759 /* Return if objdump is specified explicitly */
1760 if (args->options->objdump_path)
1763 pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename);
1769 count = dso__data_read_offset(dso, NULL, sym->start, buf, len);
1773 if ((u64)count != len)
1776 /* add the function address and name */
1777 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1781 args->line = disasm_buf;
1783 args->fileloc = NULL;
1786 dl = disasm_line__new(args);
1790 annotation_line__add(&dl->al, ¬es->src->source);
1792 /* Each raw instruction is 4 byte */
1795 for (i = 0, offset = 0; i < count; i++) {
1796 args->offset = offset;
1797 sprintf(args->line, "%x", line[i]);
1798 dl = disasm_line__new(args);
1802 annotation_line__add(&dl->al, ¬es->src->source);
1806 /* It failed in the middle */
1807 if (offset != len) {
1808 struct list_head *list = ¬es->src->source;
1810 /* Discard all lines and fallback to objdump */
1811 while (!list_empty(list)) {
1812 dl = list_first_entry(list, struct disasm_line, al.node);
1814 list_del_init(&dl->al.node);
1815 disasm_line__free(dl);
1822 return count < 0 ? count : 0;
1829 #ifdef HAVE_LIBLLVM_SUPPORT
1830 #include <llvm-c/Disassembler.h>
1831 #include <llvm-c/Target.h>
1832 #include "util/llvm-c-helpers.h"
1834 struct symbol_lookup_storage {
1836 u64 pcrel_load_addr;
1840 * Whenever LLVM wants to resolve an address into a symbol, it calls this
1841 * callback. We don't ever actually _return_ anything (in particular, because
1842 * it puts quotation marks around what we return), but we use this as a hint
1843 * that there is a branch or PC-relative address in the expression that we
1844 * should add some textual annotation for after the instruction. The caller
1845 * will use this information to add the actual annotation.
1848 symbol_lookup_callback(void *disinfo, uint64_t value,
1850 uint64_t address __maybe_unused,
1851 const char **ref __maybe_unused)
1853 struct symbol_lookup_storage *storage = disinfo;
1855 if (*ref_type == LLVMDisassembler_ReferenceType_In_Branch)
1856 storage->branch_addr = value;
1857 else if (*ref_type == LLVMDisassembler_ReferenceType_In_PCrel_Load)
1858 storage->pcrel_load_addr = value;
1859 *ref_type = LLVMDisassembler_ReferenceType_InOut_None;
1863 static int symbol__disassemble_llvm(char *filename, struct symbol *sym,
1864 struct annotate_args *args)
1866 struct annotation *notes = symbol__annotation(sym);
1867 struct map *map = args->ms.map;
1868 struct dso *dso = map__dso(map);
1869 u64 start = map__rip_2objdump(map, sym->start);
1875 char disasm_buf[2048];
1877 struct disasm_line *dl;
1878 LLVMDisasmContextRef disasm = NULL;
1879 struct symbol_lookup_storage storage;
1880 char *line_storage = NULL;
1881 size_t line_storage_len = 0;
1884 if (args->options->objdump_path)
1887 LLVMInitializeAllTargetInfos();
1888 LLVMInitializeAllTargetMCs();
1889 LLVMInitializeAllDisassemblers();
1891 buf = read_symbol(filename, map, sym, &len, &is_64bit);
1895 if (arch__is(args->arch, "x86")) {
1897 scnprintf(triplet, sizeof(triplet), "x86_64-pc-linux");
1899 scnprintf(triplet, sizeof(triplet), "i686-pc-linux");
1901 scnprintf(triplet, sizeof(triplet), "%s-linux-gnu",
1905 disasm = LLVMCreateDisasm(triplet, &storage, 0, NULL,
1906 symbol_lookup_callback);
1910 if (args->options->disassembler_style &&
1911 !strcmp(args->options->disassembler_style, "intel"))
1912 LLVMSetDisasmOptions(disasm,
1913 LLVMDisassembler_Option_AsmPrinterVariant);
1916 * This needs to be set after AsmPrinterVariant, due to a bug in LLVM;
1917 * setting AsmPrinterVariant makes a new instruction printer, making it
1918 * forget about the PrintImmHex flag (which is applied before if both
1919 * are given to the same call).
1921 LLVMSetDisasmOptions(disasm, LLVMDisassembler_Option_PrintImmHex);
1923 /* add the function address and name */
1924 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1928 args->line = disasm_buf;
1930 args->fileloc = NULL;
1933 dl = disasm_line__new(args);
1937 annotation_line__add(&dl->al, ¬es->src->source);
1940 for (u64 offset = 0; offset < len; ) {
1941 unsigned int ins_len;
1943 storage.branch_addr = 0;
1944 storage.pcrel_load_addr = 0;
1946 ins_len = LLVMDisasmInstruction(disasm, buf + offset,
1948 disasm_buf, sizeof(disasm_buf));
1951 disasm_len = strlen(disasm_buf);
1953 if (storage.branch_addr != 0) {
1954 char *name = llvm_name_for_code(dso, filename,
1955 storage.branch_addr);
1957 disasm_len += scnprintf(disasm_buf + disasm_len,
1958 sizeof(disasm_buf) -
1964 if (storage.pcrel_load_addr != 0) {
1965 char *name = llvm_name_for_data(dso, filename,
1966 storage.pcrel_load_addr);
1967 disasm_len += scnprintf(disasm_buf + disasm_len,
1968 sizeof(disasm_buf) - disasm_len,
1970 storage.pcrel_load_addr);
1972 disasm_len += scnprintf(disasm_buf + disasm_len,
1973 sizeof(disasm_buf) -
1980 args->offset = offset;
1981 args->line = expand_tabs(disasm_buf, &line_storage,
1984 args->fileloc = NULL;
1987 llvm_addr2line(filename, pc, &args->fileloc,
1988 (unsigned int *)&args->line_nr, false, NULL);
1990 dl = disasm_line__new(args);
1994 annotation_line__add(&dl->al, ¬es->src->source);
1996 free(args->fileloc);
2004 LLVMDisasmDispose(disasm);
2009 #else // HAVE_LIBLLVM_SUPPORT
2010 static int symbol__disassemble_llvm(char *filename, struct symbol *sym,
2011 struct annotate_args *args __maybe_unused)
2013 symbol__disassembler_missing("LLVM", filename, sym);
2016 #endif // HAVE_LIBLLVM_SUPPORT
2019 * Possibly create a new version of line with tabs expanded. Returns the
2020 * existing or new line, storage is updated if a new line is allocated. If
2021 * allocation fails then NULL is returned.
2023 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
2025 size_t i, src, dst, len, new_storage_len, num_tabs;
2027 size_t line_len = strlen(line);
2029 for (num_tabs = 0, i = 0; i < line_len; i++)
2030 if (line[i] == '\t')
2037 * Space for the line and '\0', less the leading and trailing
2038 * spaces. Each tab may introduce 7 additional spaces.
2040 new_storage_len = line_len + 1 + (num_tabs * 7);
2042 new_line = malloc(new_storage_len);
2043 if (new_line == NULL) {
2044 pr_err("Failure allocating memory for tab expansion\n");
2049 * Copy regions starting at src and expand tabs. If there are two
2050 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
2053 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
2054 if (line[i] == '\t') {
2056 memcpy(&new_line[dst], &line[src], len);
2058 new_line[dst++] = ' ';
2059 while (dst % 8 != 0)
2060 new_line[dst++] = ' ';
2066 /* Expand the last region. */
2067 len = line_len - src;
2068 memcpy(&new_line[dst], &line[src], len);
2070 new_line[dst] = '\0';
2073 *storage = new_line;
2074 *storage_len = new_storage_len;
2078 static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
2079 struct annotate_args *args)
2081 struct annotation_options *opts = &annotate_opts;
2082 struct map *map = args->ms.map;
2083 struct dso *dso = map__dso(map);
2087 char *fileloc = NULL;
2091 const char *objdump_argv[] = {
2094 NULL, /* Will be the objdump command to run. */
2096 NULL, /* Will be the symfs path. */
2099 struct child_process objdump_process;
2102 err = asprintf(&command,
2103 "%s %s%s --start-address=0x%016" PRIx64
2104 " --stop-address=0x%016" PRIx64
2105 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
2106 opts->objdump_path ?: "objdump",
2107 opts->disassembler_style ? "-M " : "",
2108 opts->disassembler_style ?: "",
2109 map__rip_2objdump(map, sym->start),
2110 map__rip_2objdump(map, sym->end),
2111 opts->show_linenr ? "-l" : "",
2112 opts->show_asm_raw ? "" : "--no-show-raw-insn",
2113 opts->annotate_src ? "-S" : "",
2114 opts->prefix ? "--prefix " : "",
2115 opts->prefix ? '"' : ' ',
2117 opts->prefix ? '"' : ' ',
2118 opts->prefix_strip ? "--prefix-strip=" : "",
2119 opts->prefix_strip ?: "");
2122 pr_err("Failure allocating memory for the command to run\n");
2126 pr_debug("Executing: %s\n", command);
2128 objdump_argv[2] = command;
2129 objdump_argv[4] = filename;
2131 /* Create a pipe to read from for stdout */
2132 memset(&objdump_process, 0, sizeof(objdump_process));
2133 objdump_process.argv = objdump_argv;
2134 objdump_process.out = -1;
2135 objdump_process.err = -1;
2136 objdump_process.no_stderr = 1;
2137 if (start_command(&objdump_process)) {
2138 pr_err("Failure starting to run %s\n", command);
2140 goto out_free_command;
2143 file = fdopen(objdump_process.out, "r");
2145 pr_err("Failure creating FILE stream for %s\n", command);
2147 * If we were using debug info should retry with
2151 goto out_close_stdout;
2154 /* Storage for getline. */
2159 while (!feof(file)) {
2161 char *expanded_line;
2163 if (getline(&line, &line_len, file) < 0 || !line)
2166 /* Skip lines containing "filename:" */
2167 match = strstr(line, filename);
2168 if (match && match[strlen(filename)] == ':')
2171 expanded_line = strim(line);
2172 expanded_line = expand_tabs(expanded_line, &line, &line_len);
2177 * The source code line number (lineno) needs to be kept in
2178 * across calls to symbol__parse_objdump_line(), so that it
2179 * can associate it with the instructions till the next one.
2180 * See disasm_line__new() and struct disasm_line::line_nr.
2182 if (symbol__parse_objdump_line(sym, args, expanded_line,
2183 &lineno, &fileloc) < 0)
2190 err = finish_command(&objdump_process);
2192 pr_err("Error running %s\n", command);
2196 pr_err("No output from %s\n", command);
2200 * kallsyms does not have symbol sizes so there may a nop at the end.
2203 if (dso__is_kcore(dso))
2204 delete_last_nop(sym);
2209 close(objdump_process.out);
2216 static int annotation_options__init_disassemblers(struct annotation_options *options)
2220 if (options->disassemblers_str == NULL) {
2221 const char *default_disassemblers_str =
2222 #ifdef HAVE_LIBLLVM_SUPPORT
2225 #ifdef HAVE_LIBCAPSTONE_SUPPORT
2230 options->disassemblers_str = strdup(default_disassemblers_str);
2231 if (!options->disassemblers_str)
2235 disassembler = strdup(options->disassemblers_str);
2236 if (disassembler == NULL)
2240 char *comma = strchr(disassembler, ',');
2245 options->disassemblers[options->nr_disassemblers++] = strim(disassembler);
2250 disassembler = comma + 1;
2252 if (options->nr_disassemblers >= MAX_DISASSEMBLERS) {
2253 pr_debug("annotate.disassemblers can have at most %d entries, ignoring \"%s\"\n",
2254 MAX_DISASSEMBLERS, disassembler);
2262 pr_err("Not enough memory for annotate.disassemblers\n");
2266 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
2268 struct annotation_options *options = args->options;
2269 struct map *map = args->ms.map;
2270 struct dso *dso = map__dso(map);
2271 char symfs_filename[PATH_MAX];
2272 bool delete_extract = false;
2273 struct kcore_extract kce;
2274 const char *disassembler;
2275 bool decomp = false;
2276 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
2281 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
2282 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
2283 map__unmap_ip(map, sym->end));
2285 pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name);
2287 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
2288 return symbol__disassemble_bpf(sym, args);
2289 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
2290 return symbol__disassemble_bpf_image(sym, args);
2291 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
2293 } else if (dso__is_kcore(dso)) {
2294 kce.addr = map__rip_2objdump(map, sym->start);
2295 kce.kcore_filename = symfs_filename;
2296 kce.len = sym->end - sym->start;
2297 kce.offs = sym->start;
2299 if (!kcore_extract__create(&kce)) {
2300 delete_extract = true;
2301 strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename));
2303 } else if (dso__needs_decompress(dso)) {
2304 char tmp[KMOD_DECOMP_LEN];
2306 if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0)
2310 strcpy(symfs_filename, tmp);
2314 * For powerpc data type profiling, use the dso__data_read_offset to
2315 * read raw instruction directly and interpret the binary code to
2316 * understand instructions and register fields. For sort keys as type
2317 * and typeoff, disassemble to mnemonic notation is not required in
2320 if (arch__is(args->arch, "powerpc")) {
2321 extern const char *sort_order;
2323 if (sort_order && !strstr(sort_order, "sym")) {
2324 err = symbol__disassemble_raw(symfs_filename, sym, args);
2326 goto out_remove_tmp;
2328 err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
2330 goto out_remove_tmp;
2334 err = annotation_options__init_disassemblers(options);
2336 goto out_remove_tmp;
2340 for (int i = 0; i < options->nr_disassemblers && err != 0; ++i) {
2341 disassembler = options->disassemblers[i];
2343 if (!strcmp(disassembler, "llvm"))
2344 err = symbol__disassemble_llvm(symfs_filename, sym, args);
2345 else if (!strcmp(disassembler, "capstone"))
2346 err = symbol__disassemble_capstone(symfs_filename, sym, args);
2347 else if (!strcmp(disassembler, "objdump"))
2348 err = symbol__disassemble_objdump(symfs_filename, sym, args);
2350 pr_debug("Unknown disassembler %s, skipping...\n", disassembler);
2354 pr_debug("Disassembled with %s\nannotate.disassemblers=%s\n",
2355 disassembler, options->disassemblers_str);
2359 unlink(symfs_filename);
2362 kcore_extract__delete(&kce);