1 // SPDX-License-Identifier: GPL-2.0-only
11 #include <linux/string.h>
12 #include <subcmd/run-command.h>
23 #include "namespaces.h"
28 static regex_t file_lineno;
30 /* These can be referred from the arch-dependent code */
31 static struct ins_ops call_ops;
32 static struct ins_ops dec_ops;
33 static struct ins_ops jump_ops;
34 static struct ins_ops mov_ops;
35 static struct ins_ops nop_ops;
36 static struct ins_ops lock_ops;
37 static struct ins_ops ret_ops;
39 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
40 struct ins_operands *ops, int max_ins_name);
41 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
42 struct ins_operands *ops, int max_ins_name);
44 static void ins__sort(struct arch *arch);
45 static int disasm_line__parse(char *line, const char **namep, char **rawp);
47 static __attribute__((constructor)) void symbol__init_regexpr(void)
49 regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
52 static int arch__grow_instructions(struct arch *arch)
54 struct ins *new_instructions;
55 size_t new_nr_allocated;
57 if (arch->nr_instructions_allocated == 0 && arch->instructions)
58 goto grow_from_non_allocated_table;
60 new_nr_allocated = arch->nr_instructions_allocated + 128;
61 new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
62 if (new_instructions == NULL)
65 out_update_instructions:
66 arch->instructions = new_instructions;
67 arch->nr_instructions_allocated = new_nr_allocated;
70 grow_from_non_allocated_table:
71 new_nr_allocated = arch->nr_instructions + 128;
72 new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
73 if (new_instructions == NULL)
76 memcpy(new_instructions, arch->instructions, arch->nr_instructions);
77 goto out_update_instructions;
80 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
84 if (arch->nr_instructions == arch->nr_instructions_allocated &&
85 arch__grow_instructions(arch))
88 ins = &arch->instructions[arch->nr_instructions];
89 ins->name = strdup(name);
94 arch->nr_instructions++;
100 #include "arch/arc/annotate/instructions.c"
101 #include "arch/arm/annotate/instructions.c"
102 #include "arch/arm64/annotate/instructions.c"
103 #include "arch/csky/annotate/instructions.c"
104 #include "arch/loongarch/annotate/instructions.c"
105 #include "arch/mips/annotate/instructions.c"
106 #include "arch/x86/annotate/instructions.c"
107 #include "arch/powerpc/annotate/instructions.c"
108 #include "arch/riscv64/annotate/instructions.c"
109 #include "arch/s390/annotate/instructions.c"
110 #include "arch/sparc/annotate/instructions.c"
112 static struct arch architectures[] = {
115 .init = arc__annotate_init,
119 .init = arm__annotate_init,
123 .init = arm64__annotate_init,
127 .init = csky__annotate_init,
131 .init = mips__annotate_init,
138 .init = x86__annotate_init,
139 .instructions = x86__instructions,
140 .nr_instructions = ARRAY_SIZE(x86__instructions),
141 .insn_suffix = "bwlq",
144 .register_char = '%',
145 .memory_ref_char = '(',
151 .init = powerpc__annotate_init,
155 .init = riscv64__annotate_init,
159 .init = s390__annotate_init,
166 .init = sparc__annotate_init,
173 .init = loongarch__annotate_init,
180 static int arch__key_cmp(const void *name, const void *archp)
182 const struct arch *arch = archp;
184 return strcmp(name, arch->name);
187 static int arch__cmp(const void *a, const void *b)
189 const struct arch *aa = a;
190 const struct arch *ab = b;
192 return strcmp(aa->name, ab->name);
195 static void arch__sort(void)
197 const int nmemb = ARRAY_SIZE(architectures);
199 qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
202 struct arch *arch__find(const char *name)
204 const int nmemb = ARRAY_SIZE(architectures);
212 return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
215 bool arch__is(struct arch *arch, const char *name)
217 return !strcmp(arch->name, name);
220 static void ins_ops__delete(struct ins_operands *ops)
224 zfree(&ops->source.raw);
225 zfree(&ops->source.name);
226 zfree(&ops->target.raw);
227 zfree(&ops->target.name);
230 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
231 struct ins_operands *ops, int max_ins_name)
233 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
236 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
237 struct ins_operands *ops, int max_ins_name)
239 if (ins->ops->scnprintf)
240 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
242 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
245 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
247 if (!arch || !arch->ins_is_fused)
250 return arch->ins_is_fused(arch, ins1, ins2);
253 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
255 char *endptr, *tok, *name;
256 struct map *map = ms->map;
257 struct addr_map_symbol target = {
258 .ms = { .map = map, },
261 ops->target.addr = strtoull(ops->raw, &endptr, 16);
263 name = strchr(endptr, '<');
269 if (arch->objdump.skip_functions_char &&
270 strchr(name, arch->objdump.skip_functions_char))
273 tok = strchr(name, '>');
278 ops->target.name = strdup(name);
281 if (ops->target.name == NULL)
284 target.addr = map__objdump_2mem(map, ops->target.addr);
286 if (maps__find_ams(ms->maps, &target) == 0 &&
287 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
288 ops->target.sym = target.ms.sym;
293 tok = strchr(endptr, '*');
297 /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
298 * Do not parse such instruction. */
299 if (strstr(endptr, "(%r") == NULL)
300 ops->target.addr = strtoull(endptr, NULL, 16);
305 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
306 struct ins_operands *ops, int max_ins_name)
309 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
311 if (ops->target.addr == 0)
312 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
314 if (ops->target.name)
315 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
317 return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
320 static struct ins_ops call_ops = {
321 .parse = call__parse,
322 .scnprintf = call__scnprintf,
325 bool ins__is_call(const struct ins *ins)
327 return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
331 * Prevents from matching commas in the comment section, e.g.:
332 * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
334 * and skip comma as part of function arguments, e.g.:
335 * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
337 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
339 if (ops->jump.raw_comment && c > ops->jump.raw_comment)
342 if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
348 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
350 struct map *map = ms->map;
351 struct symbol *sym = ms->sym;
352 struct addr_map_symbol target = {
353 .ms = { .map = map, },
355 const char *c = strchr(ops->raw, ',');
358 ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
359 ops->jump.raw_func_start = strchr(ops->raw, '<');
361 c = validate_comma(c, ops);
364 * Examples of lines to parse for the _cpp_lex_token@@Base
367 * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
368 * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
370 * The first is a jump to an offset inside the same function,
371 * the second is to another function, i.e. that 0xa72 is an
372 * offset in the cpp_named_operator2name@@base function.
375 * skip over possible up to 2 operands to get to address, e.g.:
376 * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
379 ops->target.addr = strtoull(c, NULL, 16);
380 if (!ops->target.addr) {
382 c = validate_comma(c, ops);
384 ops->target.addr = strtoull(c, NULL, 16);
387 ops->target.addr = strtoull(ops->raw, NULL, 16);
390 target.addr = map__objdump_2mem(map, ops->target.addr);
391 start = map__unmap_ip(map, sym->start);
392 end = map__unmap_ip(map, sym->end);
394 ops->target.outside = target.addr < start || target.addr > end;
397 * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
399 cpp_named_operator2name@@Base+0xa72
401 * Point to a place that is after the cpp_named_operator2name
402 * boundaries, i.e. in the ELF symbol table for cc1
403 * cpp_named_operator2name is marked as being 32-bytes long, but it in
404 * fact is much larger than that, so we seem to need a symbols__find()
405 * routine that looks for >= current->start and < next_symbol->start,
406 * possibly just for C++ objects?
408 * For now lets just make some progress by marking jumps to outside the
409 * current function as call like.
411 * Actual navigation will come next, with further understanding of how
412 * the symbol searching and disassembly should be done.
414 if (maps__find_ams(ms->maps, &target) == 0 &&
415 map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
416 ops->target.sym = target.ms.sym;
418 if (!ops->target.outside) {
419 ops->target.offset = target.addr - start;
420 ops->target.offset_avail = true;
422 ops->target.offset_avail = false;
428 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
429 struct ins_operands *ops, int max_ins_name)
433 if (!ops->target.addr || ops->target.offset < 0)
434 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
436 if (ops->target.outside && ops->target.sym != NULL)
437 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
439 c = strchr(ops->raw, ',');
440 c = validate_comma(c, ops);
443 const char *c2 = strchr(c + 1, ',');
445 c2 = validate_comma(c2, ops);
446 /* check for 3-op insn */
451 /* mirror arch objdump's space-after-comma style */
456 return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
457 ins->name, c ? c - ops->raw : 0, ops->raw,
461 static void jump__delete(struct ins_operands *ops __maybe_unused)
464 * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
465 * raw string, don't free them.
469 static struct ins_ops jump_ops = {
470 .free = jump__delete,
471 .parse = jump__parse,
472 .scnprintf = jump__scnprintf,
475 bool ins__is_jump(const struct ins *ins)
477 return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
480 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
482 char *endptr, *name, *t;
484 if (strstr(raw, "(%rip)") == NULL)
487 *addrp = strtoull(comment, &endptr, 16);
488 if (endptr == comment)
490 name = strchr(endptr, '<');
496 t = strchr(name, '>');
501 *namep = strdup(name);
507 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
509 ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
510 if (ops->locked.ops == NULL)
513 if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
516 ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
518 if (ops->locked.ins.ops == NULL)
521 if (ops->locked.ins.ops->parse &&
522 ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
528 zfree(&ops->locked.ops);
532 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
533 struct ins_operands *ops, int max_ins_name)
537 if (ops->locked.ins.ops == NULL)
538 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
540 printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
541 return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
542 size - printed, ops->locked.ops, max_ins_name);
545 static void lock__delete(struct ins_operands *ops)
547 struct ins *ins = &ops->locked.ins;
549 if (ins->ops && ins->ops->free)
550 ins->ops->free(ops->locked.ops);
552 ins_ops__delete(ops->locked.ops);
554 zfree(&ops->locked.ops);
555 zfree(&ops->target.raw);
556 zfree(&ops->target.name);
559 static struct ins_ops lock_ops = {
560 .free = lock__delete,
561 .parse = lock__parse,
562 .scnprintf = lock__scnprintf,
566 * Check if the operand has more than one registers like x86 SIB addressing:
567 * 0x1234(%rax, %rbx, 8)
569 * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check
570 * the input string after 'memory_ref_char' if exists.
572 static bool check_multi_regs(struct arch *arch, const char *op)
576 if (arch->objdump.register_char == 0)
579 if (arch->objdump.memory_ref_char) {
580 op = strchr(op, arch->objdump.memory_ref_char);
585 while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
593 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
595 char *s = strchr(ops->raw, ','), *target, *comment, prev;
603 * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
604 * then it needs to have the closing parenthesis.
606 if (strchr(ops->raw, '(')) {
608 s = strchr(ops->raw, ')');
609 if (s == NULL || s[1] != ',')
614 ops->source.raw = strdup(ops->raw);
617 if (ops->source.raw == NULL)
620 ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
622 target = skip_spaces(++s);
623 comment = strchr(s, arch->objdump.comment_char);
628 s = strchr(s, '\0') - 1;
630 while (s > target && isspace(s[0]))
636 ops->target.raw = strdup(target);
639 if (ops->target.raw == NULL)
640 goto out_free_source;
642 ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
647 comment = skip_spaces(comment);
648 comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
649 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
654 zfree(&ops->source.raw);
658 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
659 struct ins_operands *ops, int max_ins_name)
661 return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
662 ops->source.name ?: ops->source.raw,
663 ops->target.name ?: ops->target.raw);
666 static struct ins_ops mov_ops = {
668 .scnprintf = mov__scnprintf,
671 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
673 char *target, *comment, *s, prev;
675 target = s = ops->raw;
677 while (s[0] != '\0' && !isspace(s[0]))
682 ops->target.raw = strdup(target);
685 if (ops->target.raw == NULL)
688 comment = strchr(s, arch->objdump.comment_char);
692 comment = skip_spaces(comment);
693 comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
698 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
699 struct ins_operands *ops, int max_ins_name)
701 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
702 ops->target.name ?: ops->target.raw);
705 static struct ins_ops dec_ops = {
707 .scnprintf = dec__scnprintf,
710 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
711 struct ins_operands *ops __maybe_unused, int max_ins_name)
713 return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
716 static struct ins_ops nop_ops = {
717 .scnprintf = nop__scnprintf,
720 static struct ins_ops ret_ops = {
721 .scnprintf = ins__raw_scnprintf,
724 bool ins__is_nop(const struct ins *ins)
726 return ins->ops == &nop_ops;
729 bool ins__is_ret(const struct ins *ins)
731 return ins->ops == &ret_ops;
734 bool ins__is_lock(const struct ins *ins)
736 return ins->ops == &lock_ops;
739 static int ins__key_cmp(const void *name, const void *insp)
741 const struct ins *ins = insp;
743 return strcmp(name, ins->name);
746 static int ins__cmp(const void *a, const void *b)
748 const struct ins *ia = a;
749 const struct ins *ib = b;
751 return strcmp(ia->name, ib->name);
754 static void ins__sort(struct arch *arch)
756 const int nmemb = arch->nr_instructions;
758 qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
761 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
764 const int nmemb = arch->nr_instructions;
766 if (!arch->sorted_instructions) {
768 arch->sorted_instructions = true;
771 ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
775 if (arch->insn_suffix) {
778 size_t len = strlen(name);
780 if (len == 0 || len >= sizeof(tmp))
783 suffix = name[len - 1];
784 if (strchr(arch->insn_suffix, suffix) == NULL)
788 tmp[len - 1] = '\0'; /* remove the suffix and check again */
790 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
792 return ins ? ins->ops : NULL;
795 struct ins_ops *ins__find(struct arch *arch, const char *name)
797 struct ins_ops *ops = __ins__find(arch, name);
799 if (!ops && arch->associate_instruction_ops)
800 ops = arch->associate_instruction_ops(arch, name);
805 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
807 dl->ins.ops = ins__find(arch, dl->ins.name);
812 if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
816 static int disasm_line__parse(char *line, const char **namep, char **rawp)
818 char tmp, *name = skip_spaces(line);
825 while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
830 *namep = strdup(name);
836 *rawp = strim(*rawp);
844 static void annotation_line__init(struct annotation_line *al,
845 struct annotate_args *args,
848 al->offset = args->offset;
849 al->line = strdup(args->line);
850 al->line_nr = args->line_nr;
851 al->fileloc = args->fileloc;
855 static void annotation_line__exit(struct annotation_line *al)
857 zfree_srcline(&al->path);
862 static size_t disasm_line_size(int nr)
864 struct annotation_line *al;
866 return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
870 * Allocating the disasm annotation line data with
871 * following structure:
873 * -------------------------------------------
874 * struct disasm_line | struct annotation_line
875 * -------------------------------------------
877 * We have 'struct annotation_line' member as last member
878 * of 'struct disasm_line' to have an easy access.
880 struct disasm_line *disasm_line__new(struct annotate_args *args)
882 struct disasm_line *dl = NULL;
885 if (evsel__is_group_event(args->evsel))
886 nr = args->evsel->core.nr_members;
888 dl = zalloc(disasm_line_size(nr));
892 annotation_line__init(&dl->al, args, nr);
893 if (dl->al.line == NULL)
896 if (args->offset != -1) {
897 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
900 disasm_line__init_ins(dl, args->arch, &args->ms);
912 void disasm_line__free(struct disasm_line *dl)
914 if (dl->ins.ops && dl->ins.ops->free)
915 dl->ins.ops->free(&dl->ops);
917 ins_ops__delete(&dl->ops);
918 zfree(&dl->ins.name);
919 annotation_line__exit(&dl->al);
923 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
925 if (raw || !dl->ins.ops)
926 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
928 return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
932 * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
933 * which looks like following
935 * 0000000000415500 <_init>:
936 * 415500: sub $0x8,%rsp
937 * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
938 * 41550b: test %rax,%rax
939 * 41550e: je 415515 <_init+0x15>
940 * 415510: callq 416e70 <__gmon_start__@plt>
941 * 415515: add $0x8,%rsp
944 * it will be parsed and saved into struct disasm_line as
945 * <offset> <name> <ops.raw>
947 * The offset will be a relative offset from the start of the symbol and -1
948 * means that it's not a disassembly line so should be treated differently.
949 * The ops.raw part will be parsed further according to type of the instruction.
951 static int symbol__parse_objdump_line(struct symbol *sym,
952 struct annotate_args *args,
953 char *parsed_line, int *line_nr, char **fileloc)
955 struct map *map = args->ms.map;
956 struct annotation *notes = symbol__annotation(sym);
957 struct disasm_line *dl;
959 s64 line_ip, offset = -1;
962 /* /filename:linenr ? Save line number and ignore. */
963 if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
964 *line_nr = atoi(parsed_line + match[1].rm_so);
966 *fileloc = strdup(parsed_line);
970 /* Process hex address followed by ':'. */
971 line_ip = strtoull(parsed_line, &tmp, 16);
972 if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
973 u64 start = map__rip_2objdump(map, sym->start),
974 end = map__rip_2objdump(map, sym->end);
976 offset = line_ip - start;
977 if ((u64)line_ip < start || (u64)line_ip >= end)
980 parsed_line = tmp + 1;
983 args->offset = offset;
984 args->line = parsed_line;
985 args->line_nr = *line_nr;
986 args->fileloc = *fileloc;
989 dl = disasm_line__new(args);
995 if (!disasm_line__has_local_offset(dl)) {
996 dl->ops.target.offset = dl->ops.target.addr -
997 map__rip_2objdump(map, sym->start);
998 dl->ops.target.offset_avail = true;
1001 /* kcore has no symbols, so add the call target symbol */
1002 if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1003 struct addr_map_symbol target = {
1004 .addr = dl->ops.target.addr,
1005 .ms = { .map = map, },
1008 if (!maps__find_ams(args->ms.maps, &target) &&
1009 target.ms.sym->start == target.al_addr)
1010 dl->ops.target.sym = target.ms.sym;
1013 annotation_line__add(&dl->al, ¬es->src->source);
1017 static void delete_last_nop(struct symbol *sym)
1019 struct annotation *notes = symbol__annotation(sym);
1020 struct list_head *list = ¬es->src->source;
1021 struct disasm_line *dl;
1023 while (!list_empty(list)) {
1024 dl = list_entry(list->prev, struct disasm_line, al.node);
1027 if (!ins__is_nop(&dl->ins))
1030 if (!strstr(dl->al.line, " nop ") &&
1031 !strstr(dl->al.line, " nopl ") &&
1032 !strstr(dl->al.line, " nopw "))
1036 list_del_init(&dl->al.node);
1037 disasm_line__free(dl);
1041 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1043 struct dso *dso = map__dso(ms->map);
1045 BUG_ON(buflen == 0);
1048 str_error_r(errnum, buf, buflen);
1053 case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1054 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1055 char *build_id_msg = NULL;
1057 if (dso__has_build_id(dso)) {
1058 build_id__sprintf(dso__bid(dso), bf + 15);
1061 scnprintf(buf, buflen,
1062 "No vmlinux file%s\nwas found in the path.\n\n"
1063 "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1065 " perf buildid-cache -vu vmlinux\n\n"
1067 " --vmlinux vmlinux\n", build_id_msg ?: "");
1070 case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1071 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1073 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1074 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1076 case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1077 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1079 case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1080 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
1082 case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1083 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1084 dso__long_name(dso));
1087 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1094 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1096 char linkname[PATH_MAX];
1097 char *build_id_filename;
1098 char *build_id_path = NULL;
1102 if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1103 !dso__is_kcore(dso))
1104 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1106 build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1107 if (build_id_filename) {
1108 __symbol__join_symfs(filename, filename_size, build_id_filename);
1109 free(build_id_filename);
1111 if (dso__has_build_id(dso))
1116 build_id_path = strdup(filename);
1121 * old style build-id cache has name of XX/XXXXXXX.. while
1122 * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1123 * extract the build-id part of dirname in the new style only.
1125 pos = strrchr(build_id_path, '/');
1126 if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1127 dirname(build_id_path);
1129 if (dso__is_kcore(dso))
1132 len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1136 linkname[len] = '\0';
1137 if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1138 access(filename, R_OK)) {
1141 * If we don't have build-ids or the build-id file isn't in the
1142 * cache, or is just a kallsyms file, well, lets hope that this
1143 * DSO is the same as when 'perf record' ran.
1145 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1146 snprintf(filename, filename_size, "%s", dso__long_name(dso));
1148 __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1150 mutex_lock(dso__lock(dso));
1151 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
1152 char *new_name = dso__filename_with_chroot(dso, filename);
1154 strlcpy(filename, new_name, filename_size);
1158 mutex_unlock(dso__lock(dso));
1159 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1160 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
1163 free(build_id_path);
1167 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1168 #define PACKAGE "perf"
1170 #include <dis-asm.h>
1171 #include <bpf/bpf.h>
1172 #include <bpf/btf.h>
1173 #include <bpf/libbpf.h>
1174 #include <linux/btf.h>
1175 #include <tools/dis-asm-compat.h>
1177 #include "bpf-event.h"
1178 #include "bpf-utils.h"
1180 static int symbol__disassemble_bpf(struct symbol *sym,
1181 struct annotate_args *args)
1183 struct annotation *notes = symbol__annotation(sym);
1184 struct bpf_prog_linfo *prog_linfo = NULL;
1185 struct bpf_prog_info_node *info_node;
1186 int len = sym->end - sym->start;
1187 disassembler_ftype disassemble;
1188 struct map *map = args->ms.map;
1189 struct perf_bpil *info_linear;
1190 struct disassemble_info info;
1191 struct dso *dso = map__dso(map);
1192 int pc = 0, count, sub_id;
1193 struct btf *btf = NULL;
1194 char tpath[PATH_MAX];
1202 if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO)
1203 return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
1205 pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1206 sym->name, sym->start, sym->end - sym->start);
1208 memset(tpath, 0, sizeof(tpath));
1209 perf_exe(tpath, sizeof(tpath));
1211 bfdf = bfd_openr(tpath, NULL);
1215 if (!bfd_check_format(bfdf, bfd_object))
1218 s = open_memstream(&buf, &buf_size);
1223 init_disassemble_info_compat(&info, s,
1224 (fprintf_ftype) fprintf,
1226 info.arch = bfd_get_arch(bfdf);
1227 info.mach = bfd_get_mach(bfdf);
1229 info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
1230 dso__bpf_prog(dso)->id);
1232 ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
1235 info_linear = info_node->info_linear;
1236 sub_id = dso__bpf_prog(dso)->sub_id;
1238 info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1239 info.buffer_length = info_linear->info.jited_prog_len;
1241 if (info_linear->info.nr_line_info)
1242 prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1244 if (info_linear->info.btf_id) {
1245 struct btf_node *node;
1247 node = perf_env__find_btf(dso__bpf_prog(dso)->env,
1248 info_linear->info.btf_id);
1250 btf = btf__new((__u8 *)(node->data),
1254 disassemble_init_for_target(&info);
1256 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1257 disassemble = disassembler(info.arch,
1258 bfd_big_endian(bfdf),
1262 disassemble = disassembler(bfdf);
1264 if (disassemble == NULL)
1269 const struct bpf_line_info *linfo = NULL;
1270 struct disasm_line *dl;
1271 size_t prev_buf_size;
1272 const char *srcline;
1275 addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1276 count = disassemble(pc, &info);
1279 linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1284 srcline = btf__name_by_offset(btf, linfo->line_off);
1290 prev_buf_size = buf_size;
1293 if (!annotate_opts.hide_src_code && srcline) {
1295 args->line = strdup(srcline);
1297 args->fileloc = NULL;
1299 dl = disasm_line__new(args);
1301 annotation_line__add(&dl->al,
1302 ¬es->src->source);
1307 args->line = buf + prev_buf_size;
1309 args->fileloc = NULL;
1311 dl = disasm_line__new(args);
1313 annotation_line__add(&dl->al, ¬es->src->source);
1316 } while (count > 0 && pc < len);
1326 #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1327 static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
1328 struct annotate_args *args __maybe_unused)
1330 return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
1332 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1335 symbol__disassemble_bpf_image(struct symbol *sym,
1336 struct annotate_args *args)
1338 struct annotation *notes = symbol__annotation(sym);
1339 struct disasm_line *dl;
1342 args->line = strdup("to be implemented");
1344 args->fileloc = NULL;
1345 dl = disasm_line__new(args);
1347 annotation_line__add(&dl->al, ¬es->src->source);
1353 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1354 #include <capstone/capstone.h>
1356 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1359 struct annotation_options *opt = args->options;
1360 cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1362 /* TODO: support more architectures */
1363 if (!arch__is(args->arch, "x86"))
1366 if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1369 if (!opt->disassembler_style ||
1370 !strcmp(opt->disassembler_style, "att"))
1371 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1374 * Resolving address operands to symbols is implemented
1375 * on x86 by investigating instruction details.
1377 cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1382 struct find_file_offset_data {
1387 /* This will be called for each PHDR in an ELF binary */
1388 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1390 struct find_file_offset_data *data = arg;
1392 if (start <= data->ip && data->ip < start + len) {
1393 data->offset = pgoff + data->ip - start;
1399 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1400 struct annotate_args *args, u64 addr)
1403 struct map *map = args->ms.map;
1406 /* TODO: support more architectures */
1407 if (!arch__is(args->arch, "x86"))
1410 if (insn->detail == NULL)
1413 for (i = 0; i < insn->detail->x86.op_count; i++) {
1414 cs_x86_op *op = &insn->detail->x86.operands[i];
1417 if (op->type != X86_OP_MEM)
1420 /* only print RIP-based global symbols for now */
1421 if (op->mem.base != X86_REG_RIP)
1424 /* get the target address */
1425 orig_addr = addr + insn->size + op->mem.disp;
1426 addr = map__objdump_2mem(map, orig_addr);
1428 if (dso__kernel(map__dso(map))) {
1430 * The kernel maps can be splitted into sections,
1431 * let's find the map first and the search the symbol.
1433 map = maps__find(map__kmaps(map), addr);
1438 /* convert it to map-relative address for search */
1439 addr = map__map_ip(map, addr);
1441 sym = map__find_symbol(map, addr);
1445 if (addr == sym->start) {
1446 scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1447 orig_addr, sym->name);
1449 scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1450 orig_addr, sym->name, addr - sym->start);
1456 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1457 struct annotate_args *args)
1459 struct annotation *notes = symbol__annotation(sym);
1460 struct map *map = args->ms.map;
1461 struct dso *dso = map__dso(map);
1462 struct nscookie nsc;
1463 u64 start = map__rip_2objdump(map, sym->start);
1464 u64 end = map__rip_2objdump(map, sym->end);
1465 u64 len = end - start;
1468 bool is_64bit = false;
1469 bool needs_cs_close = false;
1471 struct find_file_offset_data data = {
1476 char disasm_buf[512];
1477 struct disasm_line *dl;
1479 if (args->options->objdump_path)
1482 nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1483 fd = open(filename, O_RDONLY);
1484 nsinfo__mountns_exit(&nsc);
1488 if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1492 if (open_capstone_handle(args, is_64bit, &handle) < 0)
1495 needs_cs_close = true;
1501 count = pread(fd, buf, len, data.offset);
1505 if ((u64)count != len)
1508 /* add the function address and name */
1509 scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1513 args->line = disasm_buf;
1515 args->fileloc = NULL;
1518 dl = disasm_line__new(args);
1522 annotation_line__add(&dl->al, ¬es->src->source);
1524 count = cs_disasm(handle, buf, len, start, len, &insn);
1525 for (i = 0, offset = 0; i < count; i++) {
1528 printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1530 insn[i].mnemonic, insn[i].op_str);
1531 print_capstone_detail(&insn[i], disasm_buf + printed,
1532 sizeof(disasm_buf) - printed, args,
1535 args->offset = offset;
1536 args->line = disasm_buf;
1538 dl = disasm_line__new(args);
1542 annotation_line__add(&dl->al, ¬es->src->source);
1544 offset += insn[i].size;
1547 /* It failed in the middle: probably due to unknown instructions */
1548 if (offset != len) {
1549 struct list_head *list = ¬es->src->source;
1551 /* Discard all lines and fallback to objdump */
1552 while (!list_empty(list)) {
1553 dl = list_first_entry(list, struct disasm_line, al.node);
1555 list_del_init(&dl->al.node);
1556 disasm_line__free(dl);
1565 return count < 0 ? count : 0;
1570 if (needs_cs_close) {
1571 struct disasm_line *tmp;
1574 * It probably failed in the middle of the above loop.
1575 * Release any resources it might add.
1577 list_for_each_entry_safe(dl, tmp, ¬es->src->source, al.node) {
1578 list_del(&dl->al.node);
1588 * Possibly create a new version of line with tabs expanded. Returns the
1589 * existing or new line, storage is updated if a new line is allocated. If
1590 * allocation fails then NULL is returned.
1592 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
1594 size_t i, src, dst, len, new_storage_len, num_tabs;
1596 size_t line_len = strlen(line);
1598 for (num_tabs = 0, i = 0; i < line_len; i++)
1599 if (line[i] == '\t')
1606 * Space for the line and '\0', less the leading and trailing
1607 * spaces. Each tab may introduce 7 additional spaces.
1609 new_storage_len = line_len + 1 + (num_tabs * 7);
1611 new_line = malloc(new_storage_len);
1612 if (new_line == NULL) {
1613 pr_err("Failure allocating memory for tab expansion\n");
1618 * Copy regions starting at src and expand tabs. If there are two
1619 * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
1622 for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
1623 if (line[i] == '\t') {
1625 memcpy(&new_line[dst], &line[src], len);
1627 new_line[dst++] = ' ';
1628 while (dst % 8 != 0)
1629 new_line[dst++] = ' ';
1635 /* Expand the last region. */
1636 len = line_len - src;
1637 memcpy(&new_line[dst], &line[src], len);
1639 new_line[dst] = '\0';
1642 *storage = new_line;
1643 *storage_len = new_storage_len;
1647 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1649 struct annotation_options *opts = &annotate_opts;
1650 struct map *map = args->ms.map;
1651 struct dso *dso = map__dso(map);
1654 char symfs_filename[PATH_MAX];
1655 struct kcore_extract kce;
1656 bool delete_extract = false;
1657 bool decomp = false;
1659 char *fileloc = NULL;
1663 const char *objdump_argv[] = {
1666 NULL, /* Will be the objdump command to run. */
1668 NULL, /* Will be the symfs path. */
1671 struct child_process objdump_process;
1672 int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1677 pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1678 symfs_filename, sym->name, map__unmap_ip(map, sym->start),
1679 map__unmap_ip(map, sym->end));
1681 pr_debug("annotating [%p] %30s : [%p] %30s\n",
1682 dso, dso__long_name(dso), sym, sym->name);
1684 if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
1685 return symbol__disassemble_bpf(sym, args);
1686 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
1687 return symbol__disassemble_bpf_image(sym, args);
1688 } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1690 } else if (dso__is_kcore(dso)) {
1691 kce.kcore_filename = symfs_filename;
1692 kce.addr = map__rip_2objdump(map, sym->start);
1693 kce.offs = sym->start;
1694 kce.len = sym->end - sym->start;
1695 if (!kcore_extract__create(&kce)) {
1696 delete_extract = true;
1697 strlcpy(symfs_filename, kce.extract_filename,
1698 sizeof(symfs_filename));
1700 } else if (dso__needs_decompress(dso)) {
1701 char tmp[KMOD_DECOMP_LEN];
1703 if (dso__decompress_kmodule_path(dso, symfs_filename,
1704 tmp, sizeof(tmp)) < 0)
1708 strcpy(symfs_filename, tmp);
1711 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1712 err = symbol__disassemble_capstone(symfs_filename, sym, args);
1714 goto out_remove_tmp;
1717 err = asprintf(&command,
1718 "%s %s%s --start-address=0x%016" PRIx64
1719 " --stop-address=0x%016" PRIx64
1720 " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
1721 opts->objdump_path ?: "objdump",
1722 opts->disassembler_style ? "-M " : "",
1723 opts->disassembler_style ?: "",
1724 map__rip_2objdump(map, sym->start),
1725 map__rip_2objdump(map, sym->end),
1726 opts->show_linenr ? "-l" : "",
1727 opts->show_asm_raw ? "" : "--no-show-raw-insn",
1728 opts->annotate_src ? "-S" : "",
1729 opts->prefix ? "--prefix " : "",
1730 opts->prefix ? '"' : ' ',
1732 opts->prefix ? '"' : ' ',
1733 opts->prefix_strip ? "--prefix-strip=" : "",
1734 opts->prefix_strip ?: "");
1737 pr_err("Failure allocating memory for the command to run\n");
1738 goto out_remove_tmp;
1741 pr_debug("Executing: %s\n", command);
1743 objdump_argv[2] = command;
1744 objdump_argv[4] = symfs_filename;
1746 /* Create a pipe to read from for stdout */
1747 memset(&objdump_process, 0, sizeof(objdump_process));
1748 objdump_process.argv = objdump_argv;
1749 objdump_process.out = -1;
1750 objdump_process.err = -1;
1751 objdump_process.no_stderr = 1;
1752 if (start_command(&objdump_process)) {
1753 pr_err("Failure starting to run %s\n", command);
1755 goto out_free_command;
1758 file = fdopen(objdump_process.out, "r");
1760 pr_err("Failure creating FILE stream for %s\n", command);
1762 * If we were using debug info should retry with
1766 goto out_close_stdout;
1769 /* Storage for getline. */
1774 while (!feof(file)) {
1776 char *expanded_line;
1778 if (getline(&line, &line_len, file) < 0 || !line)
1781 /* Skip lines containing "filename:" */
1782 match = strstr(line, symfs_filename);
1783 if (match && match[strlen(symfs_filename)] == ':')
1786 expanded_line = strim(line);
1787 expanded_line = expand_tabs(expanded_line, &line, &line_len);
1792 * The source code line number (lineno) needs to be kept in
1793 * across calls to symbol__parse_objdump_line(), so that it
1794 * can associate it with the instructions till the next one.
1795 * See disasm_line__new() and struct disasm_line::line_nr.
1797 if (symbol__parse_objdump_line(sym, args, expanded_line,
1798 &lineno, &fileloc) < 0)
1805 err = finish_command(&objdump_process);
1807 pr_err("Error running %s\n", command);
1811 pr_err("No output from %s\n", command);
1815 * kallsyms does not have symbol sizes so there may a nop at the end.
1818 if (dso__is_kcore(dso))
1819 delete_last_nop(sym);
1824 close(objdump_process.out);
1831 unlink(symfs_filename);
1834 kcore_extract__delete(&kce);