]> Git Repo - J-linux.git/blob - tools/perf/util/disasm.c
Merge tag 'apparmor-pr-2024-07-25' of git://git.kernel.org/pub/scm/linux/kernel/git...
[J-linux.git] / tools / perf / util / disasm.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <ctype.h>
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <inttypes.h>
6 #include <libgen.h>
7 #include <regex.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10
11 #include <linux/string.h>
12 #include <subcmd/run-command.h>
13
14 #include "annotate.h"
15 #include "build-id.h"
16 #include "debug.h"
17 #include "disasm.h"
18 #include "dso.h"
19 #include "env.h"
20 #include "evsel.h"
21 #include "map.h"
22 #include "maps.h"
23 #include "namespaces.h"
24 #include "srcline.h"
25 #include "symbol.h"
26 #include "util.h"
27
28 static regex_t   file_lineno;
29
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;
38
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);
43
44 static void ins__sort(struct arch *arch);
45 static int disasm_line__parse(char *line, const char **namep, char **rawp);
46
47 static __attribute__((constructor)) void symbol__init_regexpr(void)
48 {
49         regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
50 }
51
52 static int arch__grow_instructions(struct arch *arch)
53 {
54         struct ins *new_instructions;
55         size_t new_nr_allocated;
56
57         if (arch->nr_instructions_allocated == 0 && arch->instructions)
58                 goto grow_from_non_allocated_table;
59
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)
63                 return -1;
64
65 out_update_instructions:
66         arch->instructions = new_instructions;
67         arch->nr_instructions_allocated = new_nr_allocated;
68         return 0;
69
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)
74                 return -1;
75
76         memcpy(new_instructions, arch->instructions, arch->nr_instructions);
77         goto out_update_instructions;
78 }
79
80 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
81 {
82         struct ins *ins;
83
84         if (arch->nr_instructions == arch->nr_instructions_allocated &&
85             arch__grow_instructions(arch))
86                 return -1;
87
88         ins = &arch->instructions[arch->nr_instructions];
89         ins->name = strdup(name);
90         if (!ins->name)
91                 return -1;
92
93         ins->ops  = ops;
94         arch->nr_instructions++;
95
96         ins__sort(arch);
97         return 0;
98 }
99
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"
111
112 static struct arch architectures[] = {
113         {
114                 .name = "arc",
115                 .init = arc__annotate_init,
116         },
117         {
118                 .name = "arm",
119                 .init = arm__annotate_init,
120         },
121         {
122                 .name = "arm64",
123                 .init = arm64__annotate_init,
124         },
125         {
126                 .name = "csky",
127                 .init = csky__annotate_init,
128         },
129         {
130                 .name = "mips",
131                 .init = mips__annotate_init,
132                 .objdump = {
133                         .comment_char = '#',
134                 },
135         },
136         {
137                 .name = "x86",
138                 .init = x86__annotate_init,
139                 .instructions = x86__instructions,
140                 .nr_instructions = ARRAY_SIZE(x86__instructions),
141                 .insn_suffix = "bwlq",
142                 .objdump =  {
143                         .comment_char = '#',
144                         .register_char = '%',
145                         .memory_ref_char = '(',
146                         .imm_char = '$',
147                 },
148         },
149         {
150                 .name = "powerpc",
151                 .init = powerpc__annotate_init,
152         },
153         {
154                 .name = "riscv64",
155                 .init = riscv64__annotate_init,
156         },
157         {
158                 .name = "s390",
159                 .init = s390__annotate_init,
160                 .objdump =  {
161                         .comment_char = '#',
162                 },
163         },
164         {
165                 .name = "sparc",
166                 .init = sparc__annotate_init,
167                 .objdump = {
168                         .comment_char = '#',
169                 },
170         },
171         {
172                 .name = "loongarch",
173                 .init = loongarch__annotate_init,
174                 .objdump = {
175                         .comment_char = '#',
176                 },
177         },
178 };
179
180 static int arch__key_cmp(const void *name, const void *archp)
181 {
182         const struct arch *arch = archp;
183
184         return strcmp(name, arch->name);
185 }
186
187 static int arch__cmp(const void *a, const void *b)
188 {
189         const struct arch *aa = a;
190         const struct arch *ab = b;
191
192         return strcmp(aa->name, ab->name);
193 }
194
195 static void arch__sort(void)
196 {
197         const int nmemb = ARRAY_SIZE(architectures);
198
199         qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
200 }
201
202 struct arch *arch__find(const char *name)
203 {
204         const int nmemb = ARRAY_SIZE(architectures);
205         static bool sorted;
206
207         if (!sorted) {
208                 arch__sort();
209                 sorted = true;
210         }
211
212         return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
213 }
214
215 bool arch__is(struct arch *arch, const char *name)
216 {
217         return !strcmp(arch->name, name);
218 }
219
220 static void ins_ops__delete(struct ins_operands *ops)
221 {
222         if (ops == NULL)
223                 return;
224         zfree(&ops->source.raw);
225         zfree(&ops->source.name);
226         zfree(&ops->target.raw);
227         zfree(&ops->target.name);
228 }
229
230 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
231                               struct ins_operands *ops, int max_ins_name)
232 {
233         return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
234 }
235
236 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
237                    struct ins_operands *ops, int max_ins_name)
238 {
239         if (ins->ops->scnprintf)
240                 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
241
242         return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
243 }
244
245 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
246 {
247         if (!arch || !arch->ins_is_fused)
248                 return false;
249
250         return arch->ins_is_fused(arch, ins1, ins2);
251 }
252
253 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
254 {
255         char *endptr, *tok, *name;
256         struct map *map = ms->map;
257         struct addr_map_symbol target = {
258                 .ms = { .map = map, },
259         };
260
261         ops->target.addr = strtoull(ops->raw, &endptr, 16);
262
263         name = strchr(endptr, '<');
264         if (name == NULL)
265                 goto indirect_call;
266
267         name++;
268
269         if (arch->objdump.skip_functions_char &&
270             strchr(name, arch->objdump.skip_functions_char))
271                 return -1;
272
273         tok = strchr(name, '>');
274         if (tok == NULL)
275                 return -1;
276
277         *tok = '\0';
278         ops->target.name = strdup(name);
279         *tok = '>';
280
281         if (ops->target.name == NULL)
282                 return -1;
283 find_target:
284         target.addr = map__objdump_2mem(map, ops->target.addr);
285
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;
289
290         return 0;
291
292 indirect_call:
293         tok = strchr(endptr, '*');
294         if (tok != NULL) {
295                 endptr++;
296
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);
301         }
302         goto find_target;
303 }
304
305 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
306                            struct ins_operands *ops, int max_ins_name)
307 {
308         if (ops->target.sym)
309                 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
310
311         if (ops->target.addr == 0)
312                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
313
314         if (ops->target.name)
315                 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
316
317         return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
318 }
319
320 static struct ins_ops call_ops = {
321         .parse     = call__parse,
322         .scnprintf = call__scnprintf,
323 };
324
325 bool ins__is_call(const struct ins *ins)
326 {
327         return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
328 }
329
330 /*
331  * Prevents from matching commas in the comment section, e.g.:
332  * ffff200008446e70:       b.cs    ffff2000084470f4 <generic_exec_single+0x314>  // b.hs, b.nlast
333  *
334  * and skip comma as part of function arguments, e.g.:
335  * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
336  */
337 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
338 {
339         if (ops->jump.raw_comment && c > ops->jump.raw_comment)
340                 return NULL;
341
342         if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
343                 return NULL;
344
345         return c;
346 }
347
348 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
349 {
350         struct map *map = ms->map;
351         struct symbol *sym = ms->sym;
352         struct addr_map_symbol target = {
353                 .ms = { .map = map, },
354         };
355         const char *c = strchr(ops->raw, ',');
356         u64 start, end;
357
358         ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
359         ops->jump.raw_func_start = strchr(ops->raw, '<');
360
361         c = validate_comma(c, ops);
362
363         /*
364          * Examples of lines to parse for the _cpp_lex_token@@Base
365          * function:
366          *
367          * 1159e6c: jne    115aa32 <_cpp_lex_token@@Base+0xf92>
368          * 1159e8b: jne    c469be <cpp_named_operator2name@@Base+0xa72>
369          *
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.
373          */
374         /*
375          * skip over possible up to 2 operands to get to address, e.g.:
376          * tbnz  w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
377          */
378         if (c++ != NULL) {
379                 ops->target.addr = strtoull(c, NULL, 16);
380                 if (!ops->target.addr) {
381                         c = strchr(c, ',');
382                         c = validate_comma(c, ops);
383                         if (c++ != NULL)
384                                 ops->target.addr = strtoull(c, NULL, 16);
385                 }
386         } else {
387                 ops->target.addr = strtoull(ops->raw, NULL, 16);
388         }
389
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);
393
394         ops->target.outside = target.addr < start || target.addr > end;
395
396         /*
397          * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
398
399                 cpp_named_operator2name@@Base+0xa72
400
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?
407          *
408          * For now lets just make some progress by marking jumps to outside the
409          * current function as call like.
410          *
411          * Actual navigation will come next, with further understanding of how
412          * the symbol searching and disassembly should be done.
413          */
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;
417
418         if (!ops->target.outside) {
419                 ops->target.offset = target.addr - start;
420                 ops->target.offset_avail = true;
421         } else {
422                 ops->target.offset_avail = false;
423         }
424
425         return 0;
426 }
427
428 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
429                            struct ins_operands *ops, int max_ins_name)
430 {
431         const char *c;
432
433         if (!ops->target.addr || ops->target.offset < 0)
434                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
435
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);
438
439         c = strchr(ops->raw, ',');
440         c = validate_comma(c, ops);
441
442         if (c != NULL) {
443                 const char *c2 = strchr(c + 1, ',');
444
445                 c2 = validate_comma(c2, ops);
446                 /* check for 3-op insn */
447                 if (c2 != NULL)
448                         c = c2;
449                 c++;
450
451                 /* mirror arch objdump's space-after-comma style */
452                 if (*c == ' ')
453                         c++;
454         }
455
456         return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
457                          ins->name, c ? c - ops->raw : 0, ops->raw,
458                          ops->target.offset);
459 }
460
461 static void jump__delete(struct ins_operands *ops __maybe_unused)
462 {
463         /*
464          * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
465          * raw string, don't free them.
466          */
467 }
468
469 static struct ins_ops jump_ops = {
470         .free      = jump__delete,
471         .parse     = jump__parse,
472         .scnprintf = jump__scnprintf,
473 };
474
475 bool ins__is_jump(const struct ins *ins)
476 {
477         return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
478 }
479
480 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
481 {
482         char *endptr, *name, *t;
483
484         if (strstr(raw, "(%rip)") == NULL)
485                 return 0;
486
487         *addrp = strtoull(comment, &endptr, 16);
488         if (endptr == comment)
489                 return 0;
490         name = strchr(endptr, '<');
491         if (name == NULL)
492                 return -1;
493
494         name++;
495
496         t = strchr(name, '>');
497         if (t == NULL)
498                 return 0;
499
500         *t = '\0';
501         *namep = strdup(name);
502         *t = '>';
503
504         return 0;
505 }
506
507 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
508 {
509         ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
510         if (ops->locked.ops == NULL)
511                 return 0;
512
513         if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
514                 goto out_free_ops;
515
516         ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
517
518         if (ops->locked.ins.ops == NULL)
519                 goto out_free_ops;
520
521         if (ops->locked.ins.ops->parse &&
522             ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
523                 goto out_free_ops;
524
525         return 0;
526
527 out_free_ops:
528         zfree(&ops->locked.ops);
529         return 0;
530 }
531
532 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
533                            struct ins_operands *ops, int max_ins_name)
534 {
535         int printed;
536
537         if (ops->locked.ins.ops == NULL)
538                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
539
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);
543 }
544
545 static void lock__delete(struct ins_operands *ops)
546 {
547         struct ins *ins = &ops->locked.ins;
548
549         if (ins->ops && ins->ops->free)
550                 ins->ops->free(ops->locked.ops);
551         else
552                 ins_ops__delete(ops->locked.ops);
553
554         zfree(&ops->locked.ops);
555         zfree(&ops->target.raw);
556         zfree(&ops->target.name);
557 }
558
559 static struct ins_ops lock_ops = {
560         .free      = lock__delete,
561         .parse     = lock__parse,
562         .scnprintf = lock__scnprintf,
563 };
564
565 /*
566  * Check if the operand has more than one registers like x86 SIB addressing:
567  *   0x1234(%rax, %rbx, 8)
568  *
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.
571  */
572 static bool check_multi_regs(struct arch *arch, const char *op)
573 {
574         int count = 0;
575
576         if (arch->objdump.register_char == 0)
577                 return false;
578
579         if (arch->objdump.memory_ref_char) {
580                 op = strchr(op, arch->objdump.memory_ref_char);
581                 if (op == NULL)
582                         return false;
583         }
584
585         while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
586                 count++;
587                 op++;
588         }
589
590         return count > 1;
591 }
592
593 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
594 {
595         char *s = strchr(ops->raw, ','), *target, *comment, prev;
596
597         if (s == NULL)
598                 return -1;
599
600         *s = '\0';
601
602         /*
603          * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
604          * then it needs to have the closing parenthesis.
605          */
606         if (strchr(ops->raw, '(')) {
607                 *s = ',';
608                 s = strchr(ops->raw, ')');
609                 if (s == NULL || s[1] != ',')
610                         return -1;
611                 *++s = '\0';
612         }
613
614         ops->source.raw = strdup(ops->raw);
615         *s = ',';
616
617         if (ops->source.raw == NULL)
618                 return -1;
619
620         ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
621
622         target = skip_spaces(++s);
623         comment = strchr(s, arch->objdump.comment_char);
624
625         if (comment != NULL)
626                 s = comment - 1;
627         else
628                 s = strchr(s, '\0') - 1;
629
630         while (s > target && isspace(s[0]))
631                 --s;
632         s++;
633         prev = *s;
634         *s = '\0';
635
636         ops->target.raw = strdup(target);
637         *s = prev;
638
639         if (ops->target.raw == NULL)
640                 goto out_free_source;
641
642         ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
643
644         if (comment == NULL)
645                 return 0;
646
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);
650
651         return 0;
652
653 out_free_source:
654         zfree(&ops->source.raw);
655         return -1;
656 }
657
658 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
659                            struct ins_operands *ops, int max_ins_name)
660 {
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);
664 }
665
666 static struct ins_ops mov_ops = {
667         .parse     = mov__parse,
668         .scnprintf = mov__scnprintf,
669 };
670
671 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
672 {
673         char *target, *comment, *s, prev;
674
675         target = s = ops->raw;
676
677         while (s[0] != '\0' && !isspace(s[0]))
678                 ++s;
679         prev = *s;
680         *s = '\0';
681
682         ops->target.raw = strdup(target);
683         *s = prev;
684
685         if (ops->target.raw == NULL)
686                 return -1;
687
688         comment = strchr(s, arch->objdump.comment_char);
689         if (comment == NULL)
690                 return 0;
691
692         comment = skip_spaces(comment);
693         comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
694
695         return 0;
696 }
697
698 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
699                            struct ins_operands *ops, int max_ins_name)
700 {
701         return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
702                          ops->target.name ?: ops->target.raw);
703 }
704
705 static struct ins_ops dec_ops = {
706         .parse     = dec__parse,
707         .scnprintf = dec__scnprintf,
708 };
709
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)
712 {
713         return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
714 }
715
716 static struct ins_ops nop_ops = {
717         .scnprintf = nop__scnprintf,
718 };
719
720 static struct ins_ops ret_ops = {
721         .scnprintf = ins__raw_scnprintf,
722 };
723
724 bool ins__is_nop(const struct ins *ins)
725 {
726         return ins->ops == &nop_ops;
727 }
728
729 bool ins__is_ret(const struct ins *ins)
730 {
731         return ins->ops == &ret_ops;
732 }
733
734 bool ins__is_lock(const struct ins *ins)
735 {
736         return ins->ops == &lock_ops;
737 }
738
739 static int ins__key_cmp(const void *name, const void *insp)
740 {
741         const struct ins *ins = insp;
742
743         return strcmp(name, ins->name);
744 }
745
746 static int ins__cmp(const void *a, const void *b)
747 {
748         const struct ins *ia = a;
749         const struct ins *ib = b;
750
751         return strcmp(ia->name, ib->name);
752 }
753
754 static void ins__sort(struct arch *arch)
755 {
756         const int nmemb = arch->nr_instructions;
757
758         qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
759 }
760
761 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
762 {
763         struct ins *ins;
764         const int nmemb = arch->nr_instructions;
765
766         if (!arch->sorted_instructions) {
767                 ins__sort(arch);
768                 arch->sorted_instructions = true;
769         }
770
771         ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
772         if (ins)
773                 return ins->ops;
774
775         if (arch->insn_suffix) {
776                 char tmp[32];
777                 char suffix;
778                 size_t len = strlen(name);
779
780                 if (len == 0 || len >= sizeof(tmp))
781                         return NULL;
782
783                 suffix = name[len - 1];
784                 if (strchr(arch->insn_suffix, suffix) == NULL)
785                         return NULL;
786
787                 strcpy(tmp, name);
788                 tmp[len - 1] = '\0'; /* remove the suffix and check again */
789
790                 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
791         }
792         return ins ? ins->ops : NULL;
793 }
794
795 struct ins_ops *ins__find(struct arch *arch, const char *name)
796 {
797         struct ins_ops *ops = __ins__find(arch, name);
798
799         if (!ops && arch->associate_instruction_ops)
800                 ops = arch->associate_instruction_ops(arch, name);
801
802         return ops;
803 }
804
805 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
806 {
807         dl->ins.ops = ins__find(arch, dl->ins.name);
808
809         if (!dl->ins.ops)
810                 return;
811
812         if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
813                 dl->ins.ops = NULL;
814 }
815
816 static int disasm_line__parse(char *line, const char **namep, char **rawp)
817 {
818         char tmp, *name = skip_spaces(line);
819
820         if (name[0] == '\0')
821                 return -1;
822
823         *rawp = name + 1;
824
825         while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
826                 ++*rawp;
827
828         tmp = (*rawp)[0];
829         (*rawp)[0] = '\0';
830         *namep = strdup(name);
831
832         if (*namep == NULL)
833                 goto out;
834
835         (*rawp)[0] = tmp;
836         *rawp = strim(*rawp);
837
838         return 0;
839
840 out:
841         return -1;
842 }
843
844 static void annotation_line__init(struct annotation_line *al,
845                                   struct annotate_args *args,
846                                   int nr)
847 {
848         al->offset = args->offset;
849         al->line = strdup(args->line);
850         al->line_nr = args->line_nr;
851         al->fileloc = args->fileloc;
852         al->data_nr = nr;
853 }
854
855 static void annotation_line__exit(struct annotation_line *al)
856 {
857         zfree_srcline(&al->path);
858         zfree(&al->line);
859         zfree(&al->cycles);
860 }
861
862 static size_t disasm_line_size(int nr)
863 {
864         struct annotation_line *al;
865
866         return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
867 }
868
869 /*
870  * Allocating the disasm annotation line data with
871  * following structure:
872  *
873  *    -------------------------------------------
874  *    struct disasm_line | struct annotation_line
875  *    -------------------------------------------
876  *
877  * We have 'struct annotation_line' member as last member
878  * of 'struct disasm_line' to have an easy access.
879  */
880 struct disasm_line *disasm_line__new(struct annotate_args *args)
881 {
882         struct disasm_line *dl = NULL;
883         int nr = 1;
884
885         if (evsel__is_group_event(args->evsel))
886                 nr = args->evsel->core.nr_members;
887
888         dl = zalloc(disasm_line_size(nr));
889         if (!dl)
890                 return NULL;
891
892         annotation_line__init(&dl->al, args, nr);
893         if (dl->al.line == NULL)
894                 goto out_delete;
895
896         if (args->offset != -1) {
897                 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
898                         goto out_free_line;
899
900                 disasm_line__init_ins(dl, args->arch, &args->ms);
901         }
902
903         return dl;
904
905 out_free_line:
906         zfree(&dl->al.line);
907 out_delete:
908         free(dl);
909         return NULL;
910 }
911
912 void disasm_line__free(struct disasm_line *dl)
913 {
914         if (dl->ins.ops && dl->ins.ops->free)
915                 dl->ins.ops->free(&dl->ops);
916         else
917                 ins_ops__delete(&dl->ops);
918         zfree(&dl->ins.name);
919         annotation_line__exit(&dl->al);
920         free(dl);
921 }
922
923 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
924 {
925         if (raw || !dl->ins.ops)
926                 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
927
928         return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
929 }
930
931 /*
932  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
933  * which looks like following
934  *
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
942  *    415519:       retq
943  *
944  * it will be parsed and saved into struct disasm_line as
945  *  <offset>       <name>  <ops.raw>
946  *
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.
950  */
951 static int symbol__parse_objdump_line(struct symbol *sym,
952                                       struct annotate_args *args,
953                                       char *parsed_line, int *line_nr, char **fileloc)
954 {
955         struct map *map = args->ms.map;
956         struct annotation *notes = symbol__annotation(sym);
957         struct disasm_line *dl;
958         char *tmp;
959         s64 line_ip, offset = -1;
960         regmatch_t match[2];
961
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);
965                 free(*fileloc);
966                 *fileloc = strdup(parsed_line);
967                 return 0;
968         }
969
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);
975
976                 offset = line_ip - start;
977                 if ((u64)line_ip < start || (u64)line_ip >= end)
978                         offset = -1;
979                 else
980                         parsed_line = tmp + 1;
981         }
982
983         args->offset  = offset;
984         args->line    = parsed_line;
985         args->line_nr = *line_nr;
986         args->fileloc = *fileloc;
987         args->ms.sym  = sym;
988
989         dl = disasm_line__new(args);
990         (*line_nr)++;
991
992         if (dl == NULL)
993                 return -1;
994
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;
999         }
1000
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, },
1006                 };
1007
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;
1011         }
1012
1013         annotation_line__add(&dl->al, &notes->src->source);
1014         return 0;
1015 }
1016
1017 static void delete_last_nop(struct symbol *sym)
1018 {
1019         struct annotation *notes = symbol__annotation(sym);
1020         struct list_head *list = &notes->src->source;
1021         struct disasm_line *dl;
1022
1023         while (!list_empty(list)) {
1024                 dl = list_entry(list->prev, struct disasm_line, al.node);
1025
1026                 if (dl->ins.ops) {
1027                         if (!ins__is_nop(&dl->ins))
1028                                 return;
1029                 } else {
1030                         if (!strstr(dl->al.line, " nop ") &&
1031                             !strstr(dl->al.line, " nopl ") &&
1032                             !strstr(dl->al.line, " nopw "))
1033                                 return;
1034                 }
1035
1036                 list_del_init(&dl->al.node);
1037                 disasm_line__free(dl);
1038         }
1039 }
1040
1041 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1042 {
1043         struct dso *dso = map__dso(ms->map);
1044
1045         BUG_ON(buflen == 0);
1046
1047         if (errnum >= 0) {
1048                 str_error_r(errnum, buf, buflen);
1049                 return 0;
1050         }
1051
1052         switch (errnum) {
1053         case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1054                 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1055                 char *build_id_msg = NULL;
1056
1057                 if (dso__has_build_id(dso)) {
1058                         build_id__sprintf(dso__bid(dso), bf + 15);
1059                         build_id_msg = bf;
1060                 }
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"
1064                           "Please use:\n\n"
1065                           "  perf buildid-cache -vu vmlinux\n\n"
1066                           "or:\n\n"
1067                           "  --vmlinux vmlinux\n", build_id_msg ?: "");
1068         }
1069                 break;
1070         case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1071                 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1072                 break;
1073         case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1074                 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1075                 break;
1076         case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1077                 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1078                 break;
1079         case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1080                 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
1081                 break;
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));
1085                 break;
1086         default:
1087                 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1088                 break;
1089         }
1090
1091         return 0;
1092 }
1093
1094 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1095 {
1096         char linkname[PATH_MAX];
1097         char *build_id_filename;
1098         char *build_id_path = NULL;
1099         char *pos;
1100         int len;
1101
1102         if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1103             !dso__is_kcore(dso))
1104                 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1105
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);
1110         } else {
1111                 if (dso__has_build_id(dso))
1112                         return ENOMEM;
1113                 goto fallback;
1114         }
1115
1116         build_id_path = strdup(filename);
1117         if (!build_id_path)
1118                 return ENOMEM;
1119
1120         /*
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.
1124          */
1125         pos = strrchr(build_id_path, '/');
1126         if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1127                 dirname(build_id_path);
1128
1129         if (dso__is_kcore(dso))
1130                 goto fallback;
1131
1132         len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1133         if (len < 0)
1134                 goto fallback;
1135
1136         linkname[len] = '\0';
1137         if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1138                 access(filename, R_OK)) {
1139 fallback:
1140                 /*
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.
1144                  */
1145                 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1146                         snprintf(filename, filename_size, "%s", dso__long_name(dso));
1147                 else
1148                         __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1149
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);
1153                         if (new_name) {
1154                                 strlcpy(filename, new_name, filename_size);
1155                                 free(new_name);
1156                         }
1157                 }
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);
1161         }
1162
1163         free(build_id_path);
1164         return 0;
1165 }
1166
1167 #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1168 #define PACKAGE "perf"
1169 #include <bfd.h>
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>
1176
1177 #include "bpf-event.h"
1178 #include "bpf-utils.h"
1179
1180 static int symbol__disassemble_bpf(struct symbol *sym,
1181                                    struct annotate_args *args)
1182 {
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];
1195         size_t buf_size;
1196         int nr_skip = 0;
1197         char *buf;
1198         bfd *bfdf;
1199         int ret;
1200         FILE *s;
1201
1202         if (dso__binary_type(dso) != DSO_BINARY_TYPE__BPF_PROG_INFO)
1203                 return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
1204
1205         pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
1206                   sym->name, sym->start, sym->end - sym->start);
1207
1208         memset(tpath, 0, sizeof(tpath));
1209         perf_exe(tpath, sizeof(tpath));
1210
1211         bfdf = bfd_openr(tpath, NULL);
1212         if (bfdf == NULL)
1213                 abort();
1214
1215         if (!bfd_check_format(bfdf, bfd_object))
1216                 abort();
1217
1218         s = open_memstream(&buf, &buf_size);
1219         if (!s) {
1220                 ret = errno;
1221                 goto out;
1222         }
1223         init_disassemble_info_compat(&info, s,
1224                                      (fprintf_ftype) fprintf,
1225                                      fprintf_styled);
1226         info.arch = bfd_get_arch(bfdf);
1227         info.mach = bfd_get_mach(bfdf);
1228
1229         info_node = perf_env__find_bpf_prog_info(dso__bpf_prog(dso)->env,
1230                                                  dso__bpf_prog(dso)->id);
1231         if (!info_node) {
1232                 ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
1233                 goto out;
1234         }
1235         info_linear = info_node->info_linear;
1236         sub_id = dso__bpf_prog(dso)->sub_id;
1237
1238         info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
1239         info.buffer_length = info_linear->info.jited_prog_len;
1240
1241         if (info_linear->info.nr_line_info)
1242                 prog_linfo = bpf_prog_linfo__new(&info_linear->info);
1243
1244         if (info_linear->info.btf_id) {
1245                 struct btf_node *node;
1246
1247                 node = perf_env__find_btf(dso__bpf_prog(dso)->env,
1248                                           info_linear->info.btf_id);
1249                 if (node)
1250                         btf = btf__new((__u8 *)(node->data),
1251                                        node->data_size);
1252         }
1253
1254         disassemble_init_for_target(&info);
1255
1256 #ifdef DISASM_FOUR_ARGS_SIGNATURE
1257         disassemble = disassembler(info.arch,
1258                                    bfd_big_endian(bfdf),
1259                                    info.mach,
1260                                    bfdf);
1261 #else
1262         disassemble = disassembler(bfdf);
1263 #endif
1264         if (disassemble == NULL)
1265                 abort();
1266
1267         fflush(s);
1268         do {
1269                 const struct bpf_line_info *linfo = NULL;
1270                 struct disasm_line *dl;
1271                 size_t prev_buf_size;
1272                 const char *srcline;
1273                 u64 addr;
1274
1275                 addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
1276                 count = disassemble(pc, &info);
1277
1278                 if (prog_linfo)
1279                         linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
1280                                                                 addr, sub_id,
1281                                                                 nr_skip);
1282
1283                 if (linfo && btf) {
1284                         srcline = btf__name_by_offset(btf, linfo->line_off);
1285                         nr_skip++;
1286                 } else
1287                         srcline = NULL;
1288
1289                 fprintf(s, "\n");
1290                 prev_buf_size = buf_size;
1291                 fflush(s);
1292
1293                 if (!annotate_opts.hide_src_code && srcline) {
1294                         args->offset = -1;
1295                         args->line = strdup(srcline);
1296                         args->line_nr = 0;
1297                         args->fileloc = NULL;
1298                         args->ms.sym  = sym;
1299                         dl = disasm_line__new(args);
1300                         if (dl) {
1301                                 annotation_line__add(&dl->al,
1302                                                      &notes->src->source);
1303                         }
1304                 }
1305
1306                 args->offset = pc;
1307                 args->line = buf + prev_buf_size;
1308                 args->line_nr = 0;
1309                 args->fileloc = NULL;
1310                 args->ms.sym  = sym;
1311                 dl = disasm_line__new(args);
1312                 if (dl)
1313                         annotation_line__add(&dl->al, &notes->src->source);
1314
1315                 pc += count;
1316         } while (count > 0 && pc < len);
1317
1318         ret = 0;
1319 out:
1320         free(prog_linfo);
1321         btf__free(btf);
1322         fclose(s);
1323         bfd_close(bfdf);
1324         return ret;
1325 }
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)
1329 {
1330         return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
1331 }
1332 #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
1333
1334 static int
1335 symbol__disassemble_bpf_image(struct symbol *sym,
1336                               struct annotate_args *args)
1337 {
1338         struct annotation *notes = symbol__annotation(sym);
1339         struct disasm_line *dl;
1340
1341         args->offset = -1;
1342         args->line = strdup("to be implemented");
1343         args->line_nr = 0;
1344         args->fileloc = NULL;
1345         dl = disasm_line__new(args);
1346         if (dl)
1347                 annotation_line__add(&dl->al, &notes->src->source);
1348
1349         zfree(&args->line);
1350         return 0;
1351 }
1352
1353 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1354 #include <capstone/capstone.h>
1355
1356 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1357                                 csh *handle)
1358 {
1359         struct annotation_options *opt = args->options;
1360         cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1361
1362         /* TODO: support more architectures */
1363         if (!arch__is(args->arch, "x86"))
1364                 return -1;
1365
1366         if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1367                 return -1;
1368
1369         if (!opt->disassembler_style ||
1370             !strcmp(opt->disassembler_style, "att"))
1371                 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1372
1373         /*
1374          * Resolving address operands to symbols is implemented
1375          * on x86 by investigating instruction details.
1376          */
1377         cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1378
1379         return 0;
1380 }
1381
1382 struct find_file_offset_data {
1383         u64 ip;
1384         u64 offset;
1385 };
1386
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)
1389 {
1390         struct find_file_offset_data *data = arg;
1391
1392         if (start <= data->ip && data->ip < start + len) {
1393                 data->offset = pgoff + data->ip - start;
1394                 return 1;
1395         }
1396         return 0;
1397 }
1398
1399 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1400                                   struct annotate_args *args, u64 addr)
1401 {
1402         int i;
1403         struct map *map = args->ms.map;
1404         struct symbol *sym;
1405
1406         /* TODO: support more architectures */
1407         if (!arch__is(args->arch, "x86"))
1408                 return;
1409
1410         if (insn->detail == NULL)
1411                 return;
1412
1413         for (i = 0; i < insn->detail->x86.op_count; i++) {
1414                 cs_x86_op *op = &insn->detail->x86.operands[i];
1415                 u64 orig_addr;
1416
1417                 if (op->type != X86_OP_MEM)
1418                         continue;
1419
1420                 /* only print RIP-based global symbols for now */
1421                 if (op->mem.base != X86_REG_RIP)
1422                         continue;
1423
1424                 /* get the target address */
1425                 orig_addr = addr + insn->size + op->mem.disp;
1426                 addr = map__objdump_2mem(map, orig_addr);
1427
1428                 if (dso__kernel(map__dso(map))) {
1429                         /*
1430                          * The kernel maps can be splitted into sections,
1431                          * let's find the map first and the search the symbol.
1432                          */
1433                         map = maps__find(map__kmaps(map), addr);
1434                         if (map == NULL)
1435                                 continue;
1436                 }
1437
1438                 /* convert it to map-relative address for search */
1439                 addr = map__map_ip(map, addr);
1440
1441                 sym = map__find_symbol(map, addr);
1442                 if (sym == NULL)
1443                         continue;
1444
1445                 if (addr == sym->start) {
1446                         scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1447                                   orig_addr, sym->name);
1448                 } else {
1449                         scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1450                                   orig_addr, sym->name, addr - sym->start);
1451                 }
1452                 break;
1453         }
1454 }
1455
1456 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1457                                         struct annotate_args *args)
1458 {
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;
1466         u64 offset;
1467         int i, fd, count;
1468         bool is_64bit = false;
1469         bool needs_cs_close = false;
1470         u8 *buf = NULL;
1471         struct find_file_offset_data data = {
1472                 .ip = start,
1473         };
1474         csh handle;
1475         cs_insn *insn;
1476         char disasm_buf[512];
1477         struct disasm_line *dl;
1478
1479         if (args->options->objdump_path)
1480                 return -1;
1481
1482         nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1483         fd = open(filename, O_RDONLY);
1484         nsinfo__mountns_exit(&nsc);
1485         if (fd < 0)
1486                 return -1;
1487
1488         if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1489                             &is_64bit) == 0)
1490                 goto err;
1491
1492         if (open_capstone_handle(args, is_64bit, &handle) < 0)
1493                 goto err;
1494
1495         needs_cs_close = true;
1496
1497         buf = malloc(len);
1498         if (buf == NULL)
1499                 goto err;
1500
1501         count = pread(fd, buf, len, data.offset);
1502         close(fd);
1503         fd = -1;
1504
1505         if ((u64)count != len)
1506                 goto err;
1507
1508         /* add the function address and name */
1509         scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1510                   start, sym->name);
1511
1512         args->offset = -1;
1513         args->line = disasm_buf;
1514         args->line_nr = 0;
1515         args->fileloc = NULL;
1516         args->ms.sym = sym;
1517
1518         dl = disasm_line__new(args);
1519         if (dl == NULL)
1520                 goto err;
1521
1522         annotation_line__add(&dl->al, &notes->src->source);
1523
1524         count = cs_disasm(handle, buf, len, start, len, &insn);
1525         for (i = 0, offset = 0; i < count; i++) {
1526                 int printed;
1527
1528                 printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1529                                     "       %-7s %s",
1530                                     insn[i].mnemonic, insn[i].op_str);
1531                 print_capstone_detail(&insn[i], disasm_buf + printed,
1532                                       sizeof(disasm_buf) - printed, args,
1533                                       start + offset);
1534
1535                 args->offset = offset;
1536                 args->line = disasm_buf;
1537
1538                 dl = disasm_line__new(args);
1539                 if (dl == NULL)
1540                         goto err;
1541
1542                 annotation_line__add(&dl->al, &notes->src->source);
1543
1544                 offset += insn[i].size;
1545         }
1546
1547         /* It failed in the middle: probably due to unknown instructions */
1548         if (offset != len) {
1549                 struct list_head *list = &notes->src->source;
1550
1551                 /* Discard all lines and fallback to objdump */
1552                 while (!list_empty(list)) {
1553                         dl = list_first_entry(list, struct disasm_line, al.node);
1554
1555                         list_del_init(&dl->al.node);
1556                         disasm_line__free(dl);
1557                 }
1558                 count = -1;
1559         }
1560
1561 out:
1562         if (needs_cs_close)
1563                 cs_close(&handle);
1564         free(buf);
1565         return count < 0 ? count : 0;
1566
1567 err:
1568         if (fd >= 0)
1569                 close(fd);
1570         if (needs_cs_close) {
1571                 struct disasm_line *tmp;
1572
1573                 /*
1574                  * It probably failed in the middle of the above loop.
1575                  * Release any resources it might add.
1576                  */
1577                 list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
1578                         list_del(&dl->al.node);
1579                         free(dl);
1580                 }
1581         }
1582         count = -1;
1583         goto out;
1584 }
1585 #endif
1586
1587 /*
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.
1591  */
1592 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
1593 {
1594         size_t i, src, dst, len, new_storage_len, num_tabs;
1595         char *new_line;
1596         size_t line_len = strlen(line);
1597
1598         for (num_tabs = 0, i = 0; i < line_len; i++)
1599                 if (line[i] == '\t')
1600                         num_tabs++;
1601
1602         if (num_tabs == 0)
1603                 return line;
1604
1605         /*
1606          * Space for the line and '\0', less the leading and trailing
1607          * spaces. Each tab may introduce 7 additional spaces.
1608          */
1609         new_storage_len = line_len + 1 + (num_tabs * 7);
1610
1611         new_line = malloc(new_storage_len);
1612         if (new_line == NULL) {
1613                 pr_err("Failure allocating memory for tab expansion\n");
1614                 return NULL;
1615         }
1616
1617         /*
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
1620          * are inserted.
1621          */
1622         for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
1623                 if (line[i] == '\t') {
1624                         len = i - src;
1625                         memcpy(&new_line[dst], &line[src], len);
1626                         dst += len;
1627                         new_line[dst++] = ' ';
1628                         while (dst % 8 != 0)
1629                                 new_line[dst++] = ' ';
1630                         src = i + 1;
1631                         num_tabs--;
1632                 }
1633         }
1634
1635         /* Expand the last region. */
1636         len = line_len - src;
1637         memcpy(&new_line[dst], &line[src], len);
1638         dst += len;
1639         new_line[dst] = '\0';
1640
1641         free(*storage);
1642         *storage = new_line;
1643         *storage_len = new_storage_len;
1644         return new_line;
1645 }
1646
1647 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1648 {
1649         struct annotation_options *opts = &annotate_opts;
1650         struct map *map = args->ms.map;
1651         struct dso *dso = map__dso(map);
1652         char *command;
1653         FILE *file;
1654         char symfs_filename[PATH_MAX];
1655         struct kcore_extract kce;
1656         bool delete_extract = false;
1657         bool decomp = false;
1658         int lineno = 0;
1659         char *fileloc = NULL;
1660         int nline;
1661         char *line;
1662         size_t line_len;
1663         const char *objdump_argv[] = {
1664                 "/bin/sh",
1665                 "-c",
1666                 NULL, /* Will be the objdump command to run. */
1667                 "--",
1668                 NULL, /* Will be the symfs path. */
1669                 NULL,
1670         };
1671         struct child_process objdump_process;
1672         int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1673
1674         if (err)
1675                 return err;
1676
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));
1680
1681         pr_debug("annotating [%p] %30s : [%p] %30s\n",
1682                  dso, dso__long_name(dso), sym, sym->name);
1683
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) {
1689                 return -1;
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));
1699                 }
1700         } else if (dso__needs_decompress(dso)) {
1701                 char tmp[KMOD_DECOMP_LEN];
1702
1703                 if (dso__decompress_kmodule_path(dso, symfs_filename,
1704                                                  tmp, sizeof(tmp)) < 0)
1705                         return -1;
1706
1707                 decomp = true;
1708                 strcpy(symfs_filename, tmp);
1709         }
1710
1711 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1712         err = symbol__disassemble_capstone(symfs_filename, sym, args);
1713         if (err == 0)
1714                 goto out_remove_tmp;
1715 #endif
1716
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 ? '"' : ' ',
1731                  opts->prefix ?: "",
1732                  opts->prefix ? '"' : ' ',
1733                  opts->prefix_strip ? "--prefix-strip=" : "",
1734                  opts->prefix_strip ?: "");
1735
1736         if (err < 0) {
1737                 pr_err("Failure allocating memory for the command to run\n");
1738                 goto out_remove_tmp;
1739         }
1740
1741         pr_debug("Executing: %s\n", command);
1742
1743         objdump_argv[2] = command;
1744         objdump_argv[4] = symfs_filename;
1745
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);
1754                 err = -1;
1755                 goto out_free_command;
1756         }
1757
1758         file = fdopen(objdump_process.out, "r");
1759         if (!file) {
1760                 pr_err("Failure creating FILE stream for %s\n", command);
1761                 /*
1762                  * If we were using debug info should retry with
1763                  * original binary.
1764                  */
1765                 err = -1;
1766                 goto out_close_stdout;
1767         }
1768
1769         /* Storage for getline. */
1770         line = NULL;
1771         line_len = 0;
1772
1773         nline = 0;
1774         while (!feof(file)) {
1775                 const char *match;
1776                 char *expanded_line;
1777
1778                 if (getline(&line, &line_len, file) < 0 || !line)
1779                         break;
1780
1781                 /* Skip lines containing "filename:" */
1782                 match = strstr(line, symfs_filename);
1783                 if (match && match[strlen(symfs_filename)] == ':')
1784                         continue;
1785
1786                 expanded_line = strim(line);
1787                 expanded_line = expand_tabs(expanded_line, &line, &line_len);
1788                 if (!expanded_line)
1789                         break;
1790
1791                 /*
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.
1796                  */
1797                 if (symbol__parse_objdump_line(sym, args, expanded_line,
1798                                                &lineno, &fileloc) < 0)
1799                         break;
1800                 nline++;
1801         }
1802         free(line);
1803         free(fileloc);
1804
1805         err = finish_command(&objdump_process);
1806         if (err)
1807                 pr_err("Error running %s\n", command);
1808
1809         if (nline == 0) {
1810                 err = -1;
1811                 pr_err("No output from %s\n", command);
1812         }
1813
1814         /*
1815          * kallsyms does not have symbol sizes so there may a nop at the end.
1816          * Remove it.
1817          */
1818         if (dso__is_kcore(dso))
1819                 delete_last_nop(sym);
1820
1821         fclose(file);
1822
1823 out_close_stdout:
1824         close(objdump_process.out);
1825
1826 out_free_command:
1827         free(command);
1828
1829 out_remove_tmp:
1830         if (decomp)
1831                 unlink(symfs_filename);
1832
1833         if (delete_extract)
1834                 kcore_extract__delete(&kce);
1835
1836         return err;
1837 }
This page took 0.133723 seconds and 4 git commands to generate.