~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/tools/perf/util/disasm.c

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 "disasm_bpf.h"
 19 #include "dso.h"
 20 #include "env.h"
 21 #include "evsel.h"
 22 #include "map.h"
 23 #include "maps.h"
 24 #include "namespaces.h"
 25 #include "srcline.h"
 26 #include "symbol.h"
 27 #include "util.h"
 28 
 29 static regex_t   file_lineno;
 30 
 31 /* These can be referred from the arch-dependent code */
 32 static struct ins_ops call_ops;
 33 static struct ins_ops dec_ops;
 34 static struct ins_ops jump_ops;
 35 static struct ins_ops mov_ops;
 36 static struct ins_ops nop_ops;
 37 static struct ins_ops lock_ops;
 38 static struct ins_ops ret_ops;
 39 
 40 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
 41                            struct ins_operands *ops, int max_ins_name);
 42 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
 43                            struct ins_operands *ops, int max_ins_name);
 44 
 45 static void ins__sort(struct arch *arch);
 46 static int disasm_line__parse(char *line, const char **namep, char **rawp);
 47 
 48 static __attribute__((constructor)) void symbol__init_regexpr(void)
 49 {
 50         regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
 51 }
 52 
 53 static int arch__grow_instructions(struct arch *arch)
 54 {
 55         struct ins *new_instructions;
 56         size_t new_nr_allocated;
 57 
 58         if (arch->nr_instructions_allocated == 0 && arch->instructions)
 59                 goto grow_from_non_allocated_table;
 60 
 61         new_nr_allocated = arch->nr_instructions_allocated + 128;
 62         new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
 63         if (new_instructions == NULL)
 64                 return -1;
 65 
 66 out_update_instructions:
 67         arch->instructions = new_instructions;
 68         arch->nr_instructions_allocated = new_nr_allocated;
 69         return 0;
 70 
 71 grow_from_non_allocated_table:
 72         new_nr_allocated = arch->nr_instructions + 128;
 73         new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
 74         if (new_instructions == NULL)
 75                 return -1;
 76 
 77         memcpy(new_instructions, arch->instructions, arch->nr_instructions);
 78         goto out_update_instructions;
 79 }
 80 
 81 static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
 82 {
 83         struct ins *ins;
 84 
 85         if (arch->nr_instructions == arch->nr_instructions_allocated &&
 86             arch__grow_instructions(arch))
 87                 return -1;
 88 
 89         ins = &arch->instructions[arch->nr_instructions];
 90         ins->name = strdup(name);
 91         if (!ins->name)
 92                 return -1;
 93 
 94         ins->ops  = ops;
 95         arch->nr_instructions++;
 96 
 97         ins__sort(arch);
 98         return 0;
 99 }
100 
101 #include "arch/arc/annotate/instructions.c"
102 #include "arch/arm/annotate/instructions.c"
103 #include "arch/arm64/annotate/instructions.c"
104 #include "arch/csky/annotate/instructions.c"
105 #include "arch/loongarch/annotate/instructions.c"
106 #include "arch/mips/annotate/instructions.c"
107 #include "arch/x86/annotate/instructions.c"
108 #include "arch/powerpc/annotate/instructions.c"
109 #include "arch/riscv64/annotate/instructions.c"
110 #include "arch/s390/annotate/instructions.c"
111 #include "arch/sparc/annotate/instructions.c"
112 
113 static struct arch architectures[] = {
114         {
115                 .name = "arc",
116                 .init = arc__annotate_init,
117         },
118         {
119                 .name = "arm",
120                 .init = arm__annotate_init,
121         },
122         {
123                 .name = "arm64",
124                 .init = arm64__annotate_init,
125         },
126         {
127                 .name = "csky",
128                 .init = csky__annotate_init,
129         },
130         {
131                 .name = "mips",
132                 .init = mips__annotate_init,
133                 .objdump = {
134                         .comment_char = '#',
135                 },
136         },
137         {
138                 .name = "x86",
139                 .init = x86__annotate_init,
140                 .instructions = x86__instructions,
141                 .nr_instructions = ARRAY_SIZE(x86__instructions),
142                 .insn_suffix = "bwlq",
143                 .objdump =  {
144                         .comment_char = '#',
145                         .register_char = '%',
146                         .memory_ref_char = '(',
147                         .imm_char = '$',
148                 },
149         },
150         {
151                 .name = "powerpc",
152                 .init = powerpc__annotate_init,
153         },
154         {
155                 .name = "riscv64",
156                 .init = riscv64__annotate_init,
157         },
158         {
159                 .name = "s390",
160                 .init = s390__annotate_init,
161                 .objdump =  {
162                         .comment_char = '#',
163                 },
164         },
165         {
166                 .name = "sparc",
167                 .init = sparc__annotate_init,
168                 .objdump = {
169                         .comment_char = '#',
170                 },
171         },
172         {
173                 .name = "loongarch",
174                 .init = loongarch__annotate_init,
175                 .objdump = {
176                         .comment_char = '#',
177                 },
178         },
179 };
180 
181 static int arch__key_cmp(const void *name, const void *archp)
182 {
183         const struct arch *arch = archp;
184 
185         return strcmp(name, arch->name);
186 }
187 
188 static int arch__cmp(const void *a, const void *b)
189 {
190         const struct arch *aa = a;
191         const struct arch *ab = b;
192 
193         return strcmp(aa->name, ab->name);
194 }
195 
196 static void arch__sort(void)
197 {
198         const int nmemb = ARRAY_SIZE(architectures);
199 
200         qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
201 }
202 
203 struct arch *arch__find(const char *name)
204 {
205         const int nmemb = ARRAY_SIZE(architectures);
206         static bool sorted;
207 
208         if (!sorted) {
209                 arch__sort();
210                 sorted = true;
211         }
212 
213         return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
214 }
215 
216 bool arch__is(struct arch *arch, const char *name)
217 {
218         return !strcmp(arch->name, name);
219 }
220 
221 static void ins_ops__delete(struct ins_operands *ops)
222 {
223         if (ops == NULL)
224                 return;
225         zfree(&ops->source.raw);
226         zfree(&ops->source.name);
227         zfree(&ops->target.raw);
228         zfree(&ops->target.name);
229 }
230 
231 static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
232                               struct ins_operands *ops, int max_ins_name)
233 {
234         return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
235 }
236 
237 int ins__scnprintf(struct ins *ins, char *bf, size_t size,
238                    struct ins_operands *ops, int max_ins_name)
239 {
240         if (ins->ops->scnprintf)
241                 return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
242 
243         return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
244 }
245 
246 bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
247 {
248         if (!arch || !arch->ins_is_fused)
249                 return false;
250 
251         return arch->ins_is_fused(arch, ins1, ins2);
252 }
253 
254 static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
255 {
256         char *endptr, *tok, *name;
257         struct map *map = ms->map;
258         struct addr_map_symbol target = {
259                 .ms = { .map = map, },
260         };
261 
262         ops->target.addr = strtoull(ops->raw, &endptr, 16);
263 
264         name = strchr(endptr, '<');
265         if (name == NULL)
266                 goto indirect_call;
267 
268         name++;
269 
270         if (arch->objdump.skip_functions_char &&
271             strchr(name, arch->objdump.skip_functions_char))
272                 return -1;
273 
274         tok = strchr(name, '>');
275         if (tok == NULL)
276                 return -1;
277 
278         *tok = '\0';
279         ops->target.name = strdup(name);
280         *tok = '>';
281 
282         if (ops->target.name == NULL)
283                 return -1;
284 find_target:
285         target.addr = map__objdump_2mem(map, ops->target.addr);
286 
287         if (maps__find_ams(ms->maps, &target) == 0 &&
288             map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
289                 ops->target.sym = target.ms.sym;
290 
291         return 0;
292 
293 indirect_call:
294         tok = strchr(endptr, '*');
295         if (tok != NULL) {
296                 endptr++;
297 
298                 /* Indirect call can use a non-rip register and offset: callq  *0x8(%rbx).
299                  * Do not parse such instruction.  */
300                 if (strstr(endptr, "(%r") == NULL)
301                         ops->target.addr = strtoull(endptr, NULL, 16);
302         }
303         goto find_target;
304 }
305 
306 static int call__scnprintf(struct ins *ins, char *bf, size_t size,
307                            struct ins_operands *ops, int max_ins_name)
308 {
309         if (ops->target.sym)
310                 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
311 
312         if (ops->target.addr == 0)
313                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
314 
315         if (ops->target.name)
316                 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
317 
318         return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
319 }
320 
321 static struct ins_ops call_ops = {
322         .parse     = call__parse,
323         .scnprintf = call__scnprintf,
324 };
325 
326 bool ins__is_call(const struct ins *ins)
327 {
328         return ins->ops == &call_ops || ins->ops == &s390_call_ops || ins->ops == &loongarch_call_ops;
329 }
330 
331 /*
332  * Prevents from matching commas in the comment section, e.g.:
333  * ffff200008446e70:       b.cs    ffff2000084470f4 <generic_exec_single+0x314>  // b.hs, b.nlast
334  *
335  * and skip comma as part of function arguments, e.g.:
336  * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
337  */
338 static inline const char *validate_comma(const char *c, struct ins_operands *ops)
339 {
340         if (ops->jump.raw_comment && c > ops->jump.raw_comment)
341                 return NULL;
342 
343         if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
344                 return NULL;
345 
346         return c;
347 }
348 
349 static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
350 {
351         struct map *map = ms->map;
352         struct symbol *sym = ms->sym;
353         struct addr_map_symbol target = {
354                 .ms = { .map = map, },
355         };
356         const char *c = strchr(ops->raw, ',');
357         u64 start, end;
358 
359         ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
360         ops->jump.raw_func_start = strchr(ops->raw, '<');
361 
362         c = validate_comma(c, ops);
363 
364         /*
365          * Examples of lines to parse for the _cpp_lex_token@@Base
366          * function:
367          *
368          * 1159e6c: jne    115aa32 <_cpp_lex_token@@Base+0xf92>
369          * 1159e8b: jne    c469be <cpp_named_operator2name@@Base+0xa72>
370          *
371          * The first is a jump to an offset inside the same function,
372          * the second is to another function, i.e. that 0xa72 is an
373          * offset in the cpp_named_operator2name@@base function.
374          */
375         /*
376          * skip over possible up to 2 operands to get to address, e.g.:
377          * tbnz  w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
378          */
379         if (c++ != NULL) {
380                 ops->target.addr = strtoull(c, NULL, 16);
381                 if (!ops->target.addr) {
382                         c = strchr(c, ',');
383                         c = validate_comma(c, ops);
384                         if (c++ != NULL)
385                                 ops->target.addr = strtoull(c, NULL, 16);
386                 }
387         } else {
388                 ops->target.addr = strtoull(ops->raw, NULL, 16);
389         }
390 
391         target.addr = map__objdump_2mem(map, ops->target.addr);
392         start = map__unmap_ip(map, sym->start);
393         end = map__unmap_ip(map, sym->end);
394 
395         ops->target.outside = target.addr < start || target.addr > end;
396 
397         /*
398          * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
399 
400                 cpp_named_operator2name@@Base+0xa72
401 
402          * Point to a place that is after the cpp_named_operator2name
403          * boundaries, i.e.  in the ELF symbol table for cc1
404          * cpp_named_operator2name is marked as being 32-bytes long, but it in
405          * fact is much larger than that, so we seem to need a symbols__find()
406          * routine that looks for >= current->start and  < next_symbol->start,
407          * possibly just for C++ objects?
408          *
409          * For now lets just make some progress by marking jumps to outside the
410          * current function as call like.
411          *
412          * Actual navigation will come next, with further understanding of how
413          * the symbol searching and disassembly should be done.
414          */
415         if (maps__find_ams(ms->maps, &target) == 0 &&
416             map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
417                 ops->target.sym = target.ms.sym;
418 
419         if (!ops->target.outside) {
420                 ops->target.offset = target.addr - start;
421                 ops->target.offset_avail = true;
422         } else {
423                 ops->target.offset_avail = false;
424         }
425 
426         return 0;
427 }
428 
429 static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
430                            struct ins_operands *ops, int max_ins_name)
431 {
432         const char *c;
433 
434         if (!ops->target.addr || ops->target.offset < 0)
435                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
436 
437         if (ops->target.outside && ops->target.sym != NULL)
438                 return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
439 
440         c = strchr(ops->raw, ',');
441         c = validate_comma(c, ops);
442 
443         if (c != NULL) {
444                 const char *c2 = strchr(c + 1, ',');
445 
446                 c2 = validate_comma(c2, ops);
447                 /* check for 3-op insn */
448                 if (c2 != NULL)
449                         c = c2;
450                 c++;
451 
452                 /* mirror arch objdump's space-after-comma style */
453                 if (*c == ' ')
454                         c++;
455         }
456 
457         return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
458                          ins->name, c ? c - ops->raw : 0, ops->raw,
459                          ops->target.offset);
460 }
461 
462 static void jump__delete(struct ins_operands *ops __maybe_unused)
463 {
464         /*
465          * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
466          * raw string, don't free them.
467          */
468 }
469 
470 static struct ins_ops jump_ops = {
471         .free      = jump__delete,
472         .parse     = jump__parse,
473         .scnprintf = jump__scnprintf,
474 };
475 
476 bool ins__is_jump(const struct ins *ins)
477 {
478         return ins->ops == &jump_ops || ins->ops == &loongarch_jump_ops;
479 }
480 
481 static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
482 {
483         char *endptr, *name, *t;
484 
485         if (strstr(raw, "(%rip)") == NULL)
486                 return 0;
487 
488         *addrp = strtoull(comment, &endptr, 16);
489         if (endptr == comment)
490                 return 0;
491         name = strchr(endptr, '<');
492         if (name == NULL)
493                 return -1;
494 
495         name++;
496 
497         t = strchr(name, '>');
498         if (t == NULL)
499                 return 0;
500 
501         *t = '\0';
502         *namep = strdup(name);
503         *t = '>';
504 
505         return 0;
506 }
507 
508 static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
509 {
510         ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
511         if (ops->locked.ops == NULL)
512                 return 0;
513 
514         if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
515                 goto out_free_ops;
516 
517         ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
518 
519         if (ops->locked.ins.ops == NULL)
520                 goto out_free_ops;
521 
522         if (ops->locked.ins.ops->parse &&
523             ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
524                 goto out_free_ops;
525 
526         return 0;
527 
528 out_free_ops:
529         zfree(&ops->locked.ops);
530         return 0;
531 }
532 
533 static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
534                            struct ins_operands *ops, int max_ins_name)
535 {
536         int printed;
537 
538         if (ops->locked.ins.ops == NULL)
539                 return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
540 
541         printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
542         return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
543                                         size - printed, ops->locked.ops, max_ins_name);
544 }
545 
546 static void lock__delete(struct ins_operands *ops)
547 {
548         struct ins *ins = &ops->locked.ins;
549 
550         if (ins->ops && ins->ops->free)
551                 ins->ops->free(ops->locked.ops);
552         else
553                 ins_ops__delete(ops->locked.ops);
554 
555         zfree(&ops->locked.ops);
556         zfree(&ops->target.raw);
557         zfree(&ops->target.name);
558 }
559 
560 static struct ins_ops lock_ops = {
561         .free      = lock__delete,
562         .parse     = lock__parse,
563         .scnprintf = lock__scnprintf,
564 };
565 
566 /*
567  * Check if the operand has more than one registers like x86 SIB addressing:
568  *   0x1234(%rax, %rbx, 8)
569  *
570  * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check
571  * the input string after 'memory_ref_char' if exists.
572  */
573 static bool check_multi_regs(struct arch *arch, const char *op)
574 {
575         int count = 0;
576 
577         if (arch->objdump.register_char == 0)
578                 return false;
579 
580         if (arch->objdump.memory_ref_char) {
581                 op = strchr(op, arch->objdump.memory_ref_char);
582                 if (op == NULL)
583                         return false;
584         }
585 
586         while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
587                 count++;
588                 op++;
589         }
590 
591         return count > 1;
592 }
593 
594 static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
595 {
596         char *s = strchr(ops->raw, ','), *target, *comment, prev;
597 
598         if (s == NULL)
599                 return -1;
600 
601         *s = '\0';
602 
603         /*
604          * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
605          * then it needs to have the closing parenthesis.
606          */
607         if (strchr(ops->raw, '(')) {
608                 *s = ',';
609                 s = strchr(ops->raw, ')');
610                 if (s == NULL || s[1] != ',')
611                         return -1;
612                 *++s = '\0';
613         }
614 
615         ops->source.raw = strdup(ops->raw);
616         *s = ',';
617 
618         if (ops->source.raw == NULL)
619                 return -1;
620 
621         ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
622 
623         target = skip_spaces(++s);
624         comment = strchr(s, arch->objdump.comment_char);
625 
626         if (comment != NULL)
627                 s = comment - 1;
628         else
629                 s = strchr(s, '\0') - 1;
630 
631         while (s > target && isspace(s[0]))
632                 --s;
633         s++;
634         prev = *s;
635         *s = '\0';
636 
637         ops->target.raw = strdup(target);
638         *s = prev;
639 
640         if (ops->target.raw == NULL)
641                 goto out_free_source;
642 
643         ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
644 
645         if (comment == NULL)
646                 return 0;
647 
648         comment = skip_spaces(comment);
649         comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
650         comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
651 
652         return 0;
653 
654 out_free_source:
655         zfree(&ops->source.raw);
656         return -1;
657 }
658 
659 static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
660                            struct ins_operands *ops, int max_ins_name)
661 {
662         return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
663                          ops->source.name ?: ops->source.raw,
664                          ops->target.name ?: ops->target.raw);
665 }
666 
667 static struct ins_ops mov_ops = {
668         .parse     = mov__parse,
669         .scnprintf = mov__scnprintf,
670 };
671 
672 static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
673 {
674         char *target, *comment, *s, prev;
675 
676         target = s = ops->raw;
677 
678         while (s[0] != '\0' && !isspace(s[0]))
679                 ++s;
680         prev = *s;
681         *s = '\0';
682 
683         ops->target.raw = strdup(target);
684         *s = prev;
685 
686         if (ops->target.raw == NULL)
687                 return -1;
688 
689         comment = strchr(s, arch->objdump.comment_char);
690         if (comment == NULL)
691                 return 0;
692 
693         comment = skip_spaces(comment);
694         comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
695 
696         return 0;
697 }
698 
699 static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
700                            struct ins_operands *ops, int max_ins_name)
701 {
702         return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
703                          ops->target.name ?: ops->target.raw);
704 }
705 
706 static struct ins_ops dec_ops = {
707         .parse     = dec__parse,
708         .scnprintf = dec__scnprintf,
709 };
710 
711 static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
712                           struct ins_operands *ops __maybe_unused, int max_ins_name)
713 {
714         return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
715 }
716 
717 static struct ins_ops nop_ops = {
718         .scnprintf = nop__scnprintf,
719 };
720 
721 static struct ins_ops ret_ops = {
722         .scnprintf = ins__raw_scnprintf,
723 };
724 
725 bool ins__is_nop(const struct ins *ins)
726 {
727         return ins->ops == &nop_ops;
728 }
729 
730 bool ins__is_ret(const struct ins *ins)
731 {
732         return ins->ops == &ret_ops;
733 }
734 
735 bool ins__is_lock(const struct ins *ins)
736 {
737         return ins->ops == &lock_ops;
738 }
739 
740 static int ins__key_cmp(const void *name, const void *insp)
741 {
742         const struct ins *ins = insp;
743 
744         return strcmp(name, ins->name);
745 }
746 
747 static int ins__cmp(const void *a, const void *b)
748 {
749         const struct ins *ia = a;
750         const struct ins *ib = b;
751 
752         return strcmp(ia->name, ib->name);
753 }
754 
755 static void ins__sort(struct arch *arch)
756 {
757         const int nmemb = arch->nr_instructions;
758 
759         qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
760 }
761 
762 static struct ins_ops *__ins__find(struct arch *arch, const char *name)
763 {
764         struct ins *ins;
765         const int nmemb = arch->nr_instructions;
766 
767         if (!arch->sorted_instructions) {
768                 ins__sort(arch);
769                 arch->sorted_instructions = true;
770         }
771 
772         ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
773         if (ins)
774                 return ins->ops;
775 
776         if (arch->insn_suffix) {
777                 char tmp[32];
778                 char suffix;
779                 size_t len = strlen(name);
780 
781                 if (len == 0 || len >= sizeof(tmp))
782                         return NULL;
783 
784                 suffix = name[len - 1];
785                 if (strchr(arch->insn_suffix, suffix) == NULL)
786                         return NULL;
787 
788                 strcpy(tmp, name);
789                 tmp[len - 1] = '\0'; /* remove the suffix and check again */
790 
791                 ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
792         }
793         return ins ? ins->ops : NULL;
794 }
795 
796 struct ins_ops *ins__find(struct arch *arch, const char *name)
797 {
798         struct ins_ops *ops = __ins__find(arch, name);
799 
800         if (!ops && arch->associate_instruction_ops)
801                 ops = arch->associate_instruction_ops(arch, name);
802 
803         return ops;
804 }
805 
806 static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
807 {
808         dl->ins.ops = ins__find(arch, dl->ins.name);
809 
810         if (!dl->ins.ops)
811                 return;
812 
813         if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
814                 dl->ins.ops = NULL;
815 }
816 
817 static int disasm_line__parse(char *line, const char **namep, char **rawp)
818 {
819         char tmp, *name = skip_spaces(line);
820 
821         if (name[0] == '\0')
822                 return -1;
823 
824         *rawp = name + 1;
825 
826         while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
827                 ++*rawp;
828 
829         tmp = (*rawp)[0];
830         (*rawp)[0] = '\0';
831         *namep = strdup(name);
832 
833         if (*namep == NULL)
834                 goto out;
835 
836         (*rawp)[0] = tmp;
837         *rawp = strim(*rawp);
838 
839         return 0;
840 
841 out:
842         return -1;
843 }
844 
845 static void annotation_line__init(struct annotation_line *al,
846                                   struct annotate_args *args,
847                                   int nr)
848 {
849         al->offset = args->offset;
850         al->line = strdup(args->line);
851         al->line_nr = args->line_nr;
852         al->fileloc = args->fileloc;
853         al->data_nr = nr;
854 }
855 
856 static void annotation_line__exit(struct annotation_line *al)
857 {
858         zfree_srcline(&al->path);
859         zfree(&al->line);
860         zfree(&al->cycles);
861 }
862 
863 static size_t disasm_line_size(int nr)
864 {
865         struct annotation_line *al;
866 
867         return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
868 }
869 
870 /*
871  * Allocating the disasm annotation line data with
872  * following structure:
873  *
874  *    -------------------------------------------
875  *    struct disasm_line | struct annotation_line
876  *    -------------------------------------------
877  *
878  * We have 'struct annotation_line' member as last member
879  * of 'struct disasm_line' to have an easy access.
880  */
881 struct disasm_line *disasm_line__new(struct annotate_args *args)
882 {
883         struct disasm_line *dl = NULL;
884         int nr = 1;
885 
886         if (evsel__is_group_event(args->evsel))
887                 nr = args->evsel->core.nr_members;
888 
889         dl = zalloc(disasm_line_size(nr));
890         if (!dl)
891                 return NULL;
892 
893         annotation_line__init(&dl->al, args, nr);
894         if (dl->al.line == NULL)
895                 goto out_delete;
896 
897         if (args->offset != -1) {
898                 if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
899                         goto out_free_line;
900 
901                 disasm_line__init_ins(dl, args->arch, &args->ms);
902         }
903 
904         return dl;
905 
906 out_free_line:
907         zfree(&dl->al.line);
908 out_delete:
909         free(dl);
910         return NULL;
911 }
912 
913 void disasm_line__free(struct disasm_line *dl)
914 {
915         if (dl->ins.ops && dl->ins.ops->free)
916                 dl->ins.ops->free(&dl->ops);
917         else
918                 ins_ops__delete(&dl->ops);
919         zfree(&dl->ins.name);
920         annotation_line__exit(&dl->al);
921         free(dl);
922 }
923 
924 int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
925 {
926         if (raw || !dl->ins.ops)
927                 return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
928 
929         return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
930 }
931 
932 /*
933  * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
934  * which looks like following
935  *
936  *  0000000000415500 <_init>:
937  *    415500:       sub    $0x8,%rsp
938  *    415504:       mov    0x2f5ad5(%rip),%rax        # 70afe0 <_DYNAMIC+0x2f8>
939  *    41550b:       test   %rax,%rax
940  *    41550e:       je     415515 <_init+0x15>
941  *    415510:       callq  416e70 <__gmon_start__@plt>
942  *    415515:       add    $0x8,%rsp
943  *    415519:       retq
944  *
945  * it will be parsed and saved into struct disasm_line as
946  *  <offset>       <name>  <ops.raw>
947  *
948  * The offset will be a relative offset from the start of the symbol and -1
949  * means that it's not a disassembly line so should be treated differently.
950  * The ops.raw part will be parsed further according to type of the instruction.
951  */
952 static int symbol__parse_objdump_line(struct symbol *sym,
953                                       struct annotate_args *args,
954                                       char *parsed_line, int *line_nr, char **fileloc)
955 {
956         struct map *map = args->ms.map;
957         struct annotation *notes = symbol__annotation(sym);
958         struct disasm_line *dl;
959         char *tmp;
960         s64 line_ip, offset = -1;
961         regmatch_t match[2];
962 
963         /* /filename:linenr ? Save line number and ignore. */
964         if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
965                 *line_nr = atoi(parsed_line + match[1].rm_so);
966                 free(*fileloc);
967                 *fileloc = strdup(parsed_line);
968                 return 0;
969         }
970 
971         /* Process hex address followed by ':'. */
972         line_ip = strtoull(parsed_line, &tmp, 16);
973         if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
974                 u64 start = map__rip_2objdump(map, sym->start),
975                     end = map__rip_2objdump(map, sym->end);
976 
977                 offset = line_ip - start;
978                 if ((u64)line_ip < start || (u64)line_ip >= end)
979                         offset = -1;
980                 else
981                         parsed_line = tmp + 1;
982         }
983 
984         args->offset  = offset;
985         args->line    = parsed_line;
986         args->line_nr = *line_nr;
987         args->fileloc = *fileloc;
988         args->ms.sym  = sym;
989 
990         dl = disasm_line__new(args);
991         (*line_nr)++;
992 
993         if (dl == NULL)
994                 return -1;
995 
996         if (!disasm_line__has_local_offset(dl)) {
997                 dl->ops.target.offset = dl->ops.target.addr -
998                                         map__rip_2objdump(map, sym->start);
999                 dl->ops.target.offset_avail = true;
1000         }
1001 
1002         /* kcore has no symbols, so add the call target symbol */
1003         if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
1004                 struct addr_map_symbol target = {
1005                         .addr = dl->ops.target.addr,
1006                         .ms = { .map = map, },
1007                 };
1008 
1009                 if (!maps__find_ams(args->ms.maps, &target) &&
1010                     target.ms.sym->start == target.al_addr)
1011                         dl->ops.target.sym = target.ms.sym;
1012         }
1013 
1014         annotation_line__add(&dl->al, &notes->src->source);
1015         return 0;
1016 }
1017 
1018 static void delete_last_nop(struct symbol *sym)
1019 {
1020         struct annotation *notes = symbol__annotation(sym);
1021         struct list_head *list = &notes->src->source;
1022         struct disasm_line *dl;
1023 
1024         while (!list_empty(list)) {
1025                 dl = list_entry(list->prev, struct disasm_line, al.node);
1026 
1027                 if (dl->ins.ops) {
1028                         if (!ins__is_nop(&dl->ins))
1029                                 return;
1030                 } else {
1031                         if (!strstr(dl->al.line, " nop ") &&
1032                             !strstr(dl->al.line, " nopl ") &&
1033                             !strstr(dl->al.line, " nopw "))
1034                                 return;
1035                 }
1036 
1037                 list_del_init(&dl->al.node);
1038                 disasm_line__free(dl);
1039         }
1040 }
1041 
1042 int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
1043 {
1044         struct dso *dso = map__dso(ms->map);
1045 
1046         BUG_ON(buflen == 0);
1047 
1048         if (errnum >= 0) {
1049                 str_error_r(errnum, buf, buflen);
1050                 return 0;
1051         }
1052 
1053         switch (errnum) {
1054         case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
1055                 char bf[SBUILD_ID_SIZE + 15] = " with build id ";
1056                 char *build_id_msg = NULL;
1057 
1058                 if (dso__has_build_id(dso)) {
1059                         build_id__sprintf(dso__bid(dso), bf + 15);
1060                         build_id_msg = bf;
1061                 }
1062                 scnprintf(buf, buflen,
1063                           "No vmlinux file%s\nwas found in the path.\n\n"
1064                           "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
1065                           "Please use:\n\n"
1066                           "  perf buildid-cache -vu vmlinux\n\n"
1067                           "or:\n\n"
1068                           "  --vmlinux vmlinux\n", build_id_msg ?: "");
1069         }
1070                 break;
1071         case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
1072                 scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
1073                 break;
1074         case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
1075                 scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
1076                 break;
1077         case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
1078                 scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
1079                 break;
1080         case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
1081                 scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
1082                 break;
1083         case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
1084                 scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
1085                           dso__long_name(dso));
1086                 break;
1087         default:
1088                 scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
1089                 break;
1090         }
1091 
1092         return 0;
1093 }
1094 
1095 static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
1096 {
1097         char linkname[PATH_MAX];
1098         char *build_id_filename;
1099         char *build_id_path = NULL;
1100         char *pos;
1101         int len;
1102 
1103         if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
1104             !dso__is_kcore(dso))
1105                 return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
1106 
1107         build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
1108         if (build_id_filename) {
1109                 __symbol__join_symfs(filename, filename_size, build_id_filename);
1110                 free(build_id_filename);
1111         } else {
1112                 if (dso__has_build_id(dso))
1113                         return ENOMEM;
1114                 goto fallback;
1115         }
1116 
1117         build_id_path = strdup(filename);
1118         if (!build_id_path)
1119                 return ENOMEM;
1120 
1121         /*
1122          * old style build-id cache has name of XX/XXXXXXX.. while
1123          * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
1124          * extract the build-id part of dirname in the new style only.
1125          */
1126         pos = strrchr(build_id_path, '/');
1127         if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
1128                 dirname(build_id_path);
1129 
1130         if (dso__is_kcore(dso))
1131                 goto fallback;
1132 
1133         len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
1134         if (len < 0)
1135                 goto fallback;
1136 
1137         linkname[len] = '\0';
1138         if (strstr(linkname, DSO__NAME_KALLSYMS) ||
1139                 access(filename, R_OK)) {
1140 fallback:
1141                 /*
1142                  * If we don't have build-ids or the build-id file isn't in the
1143                  * cache, or is just a kallsyms file, well, lets hope that this
1144                  * DSO is the same as when 'perf record' ran.
1145                  */
1146                 if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
1147                         snprintf(filename, filename_size, "%s", dso__long_name(dso));
1148                 else
1149                         __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
1150 
1151                 mutex_lock(dso__lock(dso));
1152                 if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
1153                         char *new_name = dso__filename_with_chroot(dso, filename);
1154                         if (new_name) {
1155                                 strlcpy(filename, new_name, filename_size);
1156                                 free(new_name);
1157                         }
1158                 }
1159                 mutex_unlock(dso__lock(dso));
1160         } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1161                 dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
1162         }
1163 
1164         free(build_id_path);
1165         return 0;
1166 }
1167 
1168 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1169 #include <capstone/capstone.h>
1170 
1171 static int open_capstone_handle(struct annotate_args *args, bool is_64bit,
1172                                 csh *handle)
1173 {
1174         struct annotation_options *opt = args->options;
1175         cs_mode mode = is_64bit ? CS_MODE_64 : CS_MODE_32;
1176 
1177         /* TODO: support more architectures */
1178         if (!arch__is(args->arch, "x86"))
1179                 return -1;
1180 
1181         if (cs_open(CS_ARCH_X86, mode, handle) != CS_ERR_OK)
1182                 return -1;
1183 
1184         if (!opt->disassembler_style ||
1185             !strcmp(opt->disassembler_style, "att"))
1186                 cs_option(*handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_ATT);
1187 
1188         /*
1189          * Resolving address operands to symbols is implemented
1190          * on x86 by investigating instruction details.
1191          */
1192         cs_option(*handle, CS_OPT_DETAIL, CS_OPT_ON);
1193 
1194         return 0;
1195 }
1196 
1197 struct find_file_offset_data {
1198         u64 ip;
1199         u64 offset;
1200 };
1201 
1202 /* This will be called for each PHDR in an ELF binary */
1203 static int find_file_offset(u64 start, u64 len, u64 pgoff, void *arg)
1204 {
1205         struct find_file_offset_data *data = arg;
1206 
1207         if (start <= data->ip && data->ip < start + len) {
1208                 data->offset = pgoff + data->ip - start;
1209                 return 1;
1210         }
1211         return 0;
1212 }
1213 
1214 static void print_capstone_detail(cs_insn *insn, char *buf, size_t len,
1215                                   struct annotate_args *args, u64 addr)
1216 {
1217         int i;
1218         struct map *map = args->ms.map;
1219         struct symbol *sym;
1220 
1221         /* TODO: support more architectures */
1222         if (!arch__is(args->arch, "x86"))
1223                 return;
1224 
1225         if (insn->detail == NULL)
1226                 return;
1227 
1228         for (i = 0; i < insn->detail->x86.op_count; i++) {
1229                 cs_x86_op *op = &insn->detail->x86.operands[i];
1230                 u64 orig_addr;
1231 
1232                 if (op->type != X86_OP_MEM)
1233                         continue;
1234 
1235                 /* only print RIP-based global symbols for now */
1236                 if (op->mem.base != X86_REG_RIP)
1237                         continue;
1238 
1239                 /* get the target address */
1240                 orig_addr = addr + insn->size + op->mem.disp;
1241                 addr = map__objdump_2mem(map, orig_addr);
1242 
1243                 if (dso__kernel(map__dso(map))) {
1244                         /*
1245                          * The kernel maps can be splitted into sections,
1246                          * let's find the map first and the search the symbol.
1247                          */
1248                         map = maps__find(map__kmaps(map), addr);
1249                         if (map == NULL)
1250                                 continue;
1251                 }
1252 
1253                 /* convert it to map-relative address for search */
1254                 addr = map__map_ip(map, addr);
1255 
1256                 sym = map__find_symbol(map, addr);
1257                 if (sym == NULL)
1258                         continue;
1259 
1260                 if (addr == sym->start) {
1261                         scnprintf(buf, len, "\t# %"PRIx64" <%s>",
1262                                   orig_addr, sym->name);
1263                 } else {
1264                         scnprintf(buf, len, "\t# %"PRIx64" <%s+%#"PRIx64">",
1265                                   orig_addr, sym->name, addr - sym->start);
1266                 }
1267                 break;
1268         }
1269 }
1270 
1271 static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
1272                                         struct annotate_args *args)
1273 {
1274         struct annotation *notes = symbol__annotation(sym);
1275         struct map *map = args->ms.map;
1276         struct dso *dso = map__dso(map);
1277         struct nscookie nsc;
1278         u64 start = map__rip_2objdump(map, sym->start);
1279         u64 end = map__rip_2objdump(map, sym->end);
1280         u64 len = end - start;
1281         u64 offset;
1282         int i, fd, count;
1283         bool is_64bit = false;
1284         bool needs_cs_close = false;
1285         u8 *buf = NULL;
1286         struct find_file_offset_data data = {
1287                 .ip = start,
1288         };
1289         csh handle;
1290         cs_insn *insn;
1291         char disasm_buf[512];
1292         struct disasm_line *dl;
1293 
1294         if (args->options->objdump_path)
1295                 return -1;
1296 
1297         nsinfo__mountns_enter(dso__nsinfo(dso), &nsc);
1298         fd = open(filename, O_RDONLY);
1299         nsinfo__mountns_exit(&nsc);
1300         if (fd < 0)
1301                 return -1;
1302 
1303         if (file__read_maps(fd, /*exe=*/true, find_file_offset, &data,
1304                             &is_64bit) == 0)
1305                 goto err;
1306 
1307         if (open_capstone_handle(args, is_64bit, &handle) < 0)
1308                 goto err;
1309 
1310         needs_cs_close = true;
1311 
1312         buf = malloc(len);
1313         if (buf == NULL)
1314                 goto err;
1315 
1316         count = pread(fd, buf, len, data.offset);
1317         close(fd);
1318         fd = -1;
1319 
1320         if ((u64)count != len)
1321                 goto err;
1322 
1323         /* add the function address and name */
1324         scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
1325                   start, sym->name);
1326 
1327         args->offset = -1;
1328         args->line = disasm_buf;
1329         args->line_nr = 0;
1330         args->fileloc = NULL;
1331         args->ms.sym = sym;
1332 
1333         dl = disasm_line__new(args);
1334         if (dl == NULL)
1335                 goto err;
1336 
1337         annotation_line__add(&dl->al, &notes->src->source);
1338 
1339         count = cs_disasm(handle, buf, len, start, len, &insn);
1340         for (i = 0, offset = 0; i < count; i++) {
1341                 int printed;
1342 
1343                 printed = scnprintf(disasm_buf, sizeof(disasm_buf),
1344                                     "       %-7s %s",
1345                                     insn[i].mnemonic, insn[i].op_str);
1346                 print_capstone_detail(&insn[i], disasm_buf + printed,
1347                                       sizeof(disasm_buf) - printed, args,
1348                                       start + offset);
1349 
1350                 args->offset = offset;
1351                 args->line = disasm_buf;
1352 
1353                 dl = disasm_line__new(args);
1354                 if (dl == NULL)
1355                         goto err;
1356 
1357                 annotation_line__add(&dl->al, &notes->src->source);
1358 
1359                 offset += insn[i].size;
1360         }
1361 
1362         /* It failed in the middle: probably due to unknown instructions */
1363         if (offset != len) {
1364                 struct list_head *list = &notes->src->source;
1365 
1366                 /* Discard all lines and fallback to objdump */
1367                 while (!list_empty(list)) {
1368                         dl = list_first_entry(list, struct disasm_line, al.node);
1369 
1370                         list_del_init(&dl->al.node);
1371                         disasm_line__free(dl);
1372                 }
1373                 count = -1;
1374         }
1375 
1376 out:
1377         if (needs_cs_close)
1378                 cs_close(&handle);
1379         free(buf);
1380         return count < 0 ? count : 0;
1381 
1382 err:
1383         if (fd >= 0)
1384                 close(fd);
1385         if (needs_cs_close) {
1386                 struct disasm_line *tmp;
1387 
1388                 /*
1389                  * It probably failed in the middle of the above loop.
1390                  * Release any resources it might add.
1391                  */
1392                 list_for_each_entry_safe(dl, tmp, &notes->src->source, al.node) {
1393                         list_del(&dl->al.node);
1394                         free(dl);
1395                 }
1396         }
1397         count = -1;
1398         goto out;
1399 }
1400 #endif
1401 
1402 /*
1403  * Possibly create a new version of line with tabs expanded. Returns the
1404  * existing or new line, storage is updated if a new line is allocated. If
1405  * allocation fails then NULL is returned.
1406  */
1407 static char *expand_tabs(char *line, char **storage, size_t *storage_len)
1408 {
1409         size_t i, src, dst, len, new_storage_len, num_tabs;
1410         char *new_line;
1411         size_t line_len = strlen(line);
1412 
1413         for (num_tabs = 0, i = 0; i < line_len; i++)
1414                 if (line[i] == '\t')
1415                         num_tabs++;
1416 
1417         if (num_tabs == 0)
1418                 return line;
1419 
1420         /*
1421          * Space for the line and '\0', less the leading and trailing
1422          * spaces. Each tab may introduce 7 additional spaces.
1423          */
1424         new_storage_len = line_len + 1 + (num_tabs * 7);
1425 
1426         new_line = malloc(new_storage_len);
1427         if (new_line == NULL) {
1428                 pr_err("Failure allocating memory for tab expansion\n");
1429                 return NULL;
1430         }
1431 
1432         /*
1433          * Copy regions starting at src and expand tabs. If there are two
1434          * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
1435          * are inserted.
1436          */
1437         for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
1438                 if (line[i] == '\t') {
1439                         len = i - src;
1440                         memcpy(&new_line[dst], &line[src], len);
1441                         dst += len;
1442                         new_line[dst++] = ' ';
1443                         while (dst % 8 != 0)
1444                                 new_line[dst++] = ' ';
1445                         src = i + 1;
1446                         num_tabs--;
1447                 }
1448         }
1449 
1450         /* Expand the last region. */
1451         len = line_len - src;
1452         memcpy(&new_line[dst], &line[src], len);
1453         dst += len;
1454         new_line[dst] = '\0';
1455 
1456         free(*storage);
1457         *storage = new_line;
1458         *storage_len = new_storage_len;
1459         return new_line;
1460 }
1461 
1462 int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
1463 {
1464         struct annotation_options *opts = &annotate_opts;
1465         struct map *map = args->ms.map;
1466         struct dso *dso = map__dso(map);
1467         char *command;
1468         FILE *file;
1469         char symfs_filename[PATH_MAX];
1470         struct kcore_extract kce;
1471         bool delete_extract = false;
1472         bool decomp = false;
1473         int lineno = 0;
1474         char *fileloc = NULL;
1475         int nline;
1476         char *line;
1477         size_t line_len;
1478         const char *objdump_argv[] = {
1479                 "/bin/sh",
1480                 "-c",
1481                 NULL, /* Will be the objdump command to run. */
1482                 "--",
1483                 NULL, /* Will be the symfs path. */
1484                 NULL,
1485         };
1486         struct child_process objdump_process;
1487         int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
1488 
1489         if (err)
1490                 return err;
1491 
1492         pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
1493                  symfs_filename, sym->name, map__unmap_ip(map, sym->start),
1494                  map__unmap_ip(map, sym->end));
1495 
1496         pr_debug("annotating [%p] %30s : [%p] %30s\n",
1497                  dso, dso__long_name(dso), sym, sym->name);
1498 
1499         if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO) {
1500                 return symbol__disassemble_bpf(sym, args);
1501         } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE) {
1502                 return symbol__disassemble_bpf_image(sym, args);
1503         } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
1504                 return -1;
1505         } else if (dso__is_kcore(dso)) {
1506                 kce.kcore_filename = symfs_filename;
1507                 kce.addr = map__rip_2objdump(map, sym->start);
1508                 kce.offs = sym->start;
1509                 kce.len = sym->end - sym->start;
1510                 if (!kcore_extract__create(&kce)) {
1511                         delete_extract = true;
1512                         strlcpy(symfs_filename, kce.extract_filename,
1513                                 sizeof(symfs_filename));
1514                 }
1515         } else if (dso__needs_decompress(dso)) {
1516                 char tmp[KMOD_DECOMP_LEN];
1517 
1518                 if (dso__decompress_kmodule_path(dso, symfs_filename,
1519                                                  tmp, sizeof(tmp)) < 0)
1520                         return -1;
1521 
1522                 decomp = true;
1523                 strcpy(symfs_filename, tmp);
1524         }
1525 
1526 #ifdef HAVE_LIBCAPSTONE_SUPPORT
1527         err = symbol__disassemble_capstone(symfs_filename, sym, args);
1528         if (err == 0)
1529                 goto out_remove_tmp;
1530 #endif
1531 
1532         err = asprintf(&command,
1533                  "%s %s%s --start-address=0x%016" PRIx64
1534                  " --stop-address=0x%016" PRIx64
1535                  " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
1536                  opts->objdump_path ?: "objdump",
1537                  opts->disassembler_style ? "-M " : "",
1538                  opts->disassembler_style ?: "",
1539                  map__rip_2objdump(map, sym->start),
1540                  map__rip_2objdump(map, sym->end),
1541                  opts->show_linenr ? "-l" : "",
1542                  opts->show_asm_raw ? "" : "--no-show-raw-insn",
1543                  opts->annotate_src ? "-S" : "",
1544                  opts->prefix ? "--prefix " : "",
1545                  opts->prefix ? '"' : ' ',
1546                  opts->prefix ?: "",
1547                  opts->prefix ? '"' : ' ',
1548                  opts->prefix_strip ? "--prefix-strip=" : "",
1549                  opts->prefix_strip ?: "");
1550 
1551         if (err < 0) {
1552                 pr_err("Failure allocating memory for the command to run\n");
1553                 goto out_remove_tmp;
1554         }
1555 
1556         pr_debug("Executing: %s\n", command);
1557 
1558         objdump_argv[2] = command;
1559         objdump_argv[4] = symfs_filename;
1560 
1561         /* Create a pipe to read from for stdout */
1562         memset(&objdump_process, 0, sizeof(objdump_process));
1563         objdump_process.argv = objdump_argv;
1564         objdump_process.out = -1;
1565         objdump_process.err = -1;
1566         objdump_process.no_stderr = 1;
1567         if (start_command(&objdump_process)) {
1568                 pr_err("Failure starting to run %s\n", command);
1569                 err = -1;
1570                 goto out_free_command;
1571         }
1572 
1573         file = fdopen(objdump_process.out, "r");
1574         if (!file) {
1575                 pr_err("Failure creating FILE stream for %s\n", command);
1576                 /*
1577                  * If we were using debug info should retry with
1578                  * original binary.
1579                  */
1580                 err = -1;
1581                 goto out_close_stdout;
1582         }
1583 
1584         /* Storage for getline. */
1585         line = NULL;
1586         line_len = 0;
1587 
1588         nline = 0;
1589         while (!feof(file)) {
1590                 const char *match;
1591                 char *expanded_line;
1592 
1593                 if (getline(&line, &line_len, file) < 0 || !line)
1594                         break;
1595 
1596                 /* Skip lines containing "filename:" */
1597                 match = strstr(line, symfs_filename);
1598                 if (match && match[strlen(symfs_filename)] == ':')
1599                         continue;
1600 
1601                 expanded_line = strim(line);
1602                 expanded_line = expand_tabs(expanded_line, &line, &line_len);
1603                 if (!expanded_line)
1604                         break;
1605 
1606                 /*
1607                  * The source code line number (lineno) needs to be kept in
1608                  * across calls to symbol__parse_objdump_line(), so that it
1609                  * can associate it with the instructions till the next one.
1610                  * See disasm_line__new() and struct disasm_line::line_nr.
1611                  */
1612                 if (symbol__parse_objdump_line(sym, args, expanded_line,
1613                                                &lineno, &fileloc) < 0)
1614                         break;
1615                 nline++;
1616         }
1617         free(line);
1618         free(fileloc);
1619 
1620         err = finish_command(&objdump_process);
1621         if (err)
1622                 pr_err("Error running %s\n", command);
1623 
1624         if (nline == 0) {
1625                 err = -1;
1626                 pr_err("No output from %s\n", command);
1627         }
1628 
1629         /*
1630          * kallsyms does not have symbol sizes so there may a nop at the end.
1631          * Remove it.
1632          */
1633         if (dso__is_kcore(dso))
1634                 delete_last_nop(sym);
1635 
1636         fclose(file);
1637 
1638 out_close_stdout:
1639         close(objdump_process.out);
1640 
1641 out_free_command:
1642         free(command);
1643 
1644 out_remove_tmp:
1645         if (decomp)
1646                 unlink(symfs_filename);
1647 
1648         if (delete_extract)
1649                 kcore_extract__delete(&kce);
1650 
1651         return err;
1652 }
1653 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php