disasm.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #include <ctype.h>
  3. #include <elf.h>
  4. #ifndef EF_CSKY_ABIMASK
  5. #define EF_CSKY_ABIMASK 0XF0000000
  6. #endif
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <inttypes.h>
  10. #include <libgen.h>
  11. #include <regex.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <linux/string.h>
  15. #include <subcmd/run-command.h>
  16. #include "annotate.h"
  17. #include "annotate-data.h"
  18. #include "build-id.h"
  19. #include "capstone.h"
  20. #include "debug.h"
  21. #include "disasm.h"
  22. #include "dso.h"
  23. #include "dwarf-regs.h"
  24. #include "env.h"
  25. #include "evsel.h"
  26. #include "libbfd.h"
  27. #include "llvm.h"
  28. #include "map.h"
  29. #include "maps.h"
  30. #include "namespaces.h"
  31. #include "srcline.h"
  32. #include "symbol.h"
  33. #include "thread.h"
  34. #include "util.h"
  35. static regex_t file_lineno;
  36. /* These can be referred from the arch-dependent code */
  37. const struct ins_ops call_ops;
  38. const struct ins_ops dec_ops;
  39. const struct ins_ops jump_ops;
  40. const struct ins_ops mov_ops;
  41. const struct ins_ops nop_ops;
  42. const struct ins_ops lock_ops;
  43. const struct ins_ops ret_ops;
  44. const struct ins_ops load_store_ops;
  45. const struct ins_ops arithmetic_ops;
  46. static void ins__sort(struct arch *arch);
  47. static int disasm_line__parse(char *line, const char **namep, char **rawp);
  48. static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annotate_args *args);
  49. static __attribute__((constructor)) void symbol__init_regexpr(void)
  50. {
  51. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  52. }
  53. static int arch__grow_instructions(struct arch *arch)
  54. {
  55. struct ins *new_instructions;
  56. size_t new_nr_allocated;
  57. if (arch->nr_instructions_allocated == 0 && arch->instructions)
  58. goto grow_from_non_allocated_table;
  59. new_nr_allocated = arch->nr_instructions_allocated + 128;
  60. new_instructions = realloc((void *)arch->instructions,
  61. new_nr_allocated * sizeof(struct ins));
  62. if (new_instructions == NULL)
  63. return -1;
  64. out_update_instructions:
  65. arch->instructions = new_instructions;
  66. arch->nr_instructions_allocated = new_nr_allocated;
  67. return 0;
  68. grow_from_non_allocated_table:
  69. new_nr_allocated = arch->nr_instructions + 128;
  70. new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
  71. if (new_instructions == NULL)
  72. return -1;
  73. memcpy(new_instructions, arch->instructions, arch->nr_instructions * sizeof(struct ins));
  74. goto out_update_instructions;
  75. }
  76. int arch__associate_ins_ops(struct arch *arch, const char *name, const struct ins_ops *ops)
  77. {
  78. struct ins *ins;
  79. if (arch->nr_instructions == arch->nr_instructions_allocated &&
  80. arch__grow_instructions(arch))
  81. return -1;
  82. ins = (struct ins *)&arch->instructions[arch->nr_instructions];
  83. ins->name = strdup(name);
  84. if (!ins->name)
  85. return -1;
  86. ins->ops = ops;
  87. arch->nr_instructions++;
  88. ins__sort(arch);
  89. return 0;
  90. }
  91. static int e_machine_and_eflags__cmp(const struct e_machine_and_e_flags *val1,
  92. const struct e_machine_and_e_flags *val2)
  93. {
  94. if (val1->e_machine == val2->e_machine) {
  95. if (val1->e_machine != EM_CSKY)
  96. return 0;
  97. if ((val1->e_flags & EF_CSKY_ABIMASK) < (val2->e_flags & EF_CSKY_ABIMASK))
  98. return -1;
  99. return (val1->e_flags & EF_CSKY_ABIMASK) > (val2->e_flags & EF_CSKY_ABIMASK);
  100. }
  101. return val1->e_machine < val2->e_machine ? -1 : 1;
  102. }
  103. static int arch__key_cmp(const void *key, const void *archp)
  104. {
  105. const struct arch *const *arch = archp;
  106. return e_machine_and_eflags__cmp(key, &(*arch)->id);
  107. }
  108. static int arch__cmp(const void *a, const void *b)
  109. {
  110. const struct arch *const *aa = a;
  111. const struct arch *const *ab = b;
  112. return e_machine_and_eflags__cmp(&(*aa)->id, &(*ab)->id);
  113. }
  114. const struct arch *arch__find(uint16_t e_machine, uint32_t e_flags, const char *cpuid)
  115. {
  116. static const struct arch *(*const arch_new_fn[])(const struct e_machine_and_e_flags *id,
  117. const char *cpuid) = {
  118. [EM_386] = arch__new_x86,
  119. [EM_ARC] = arch__new_arc,
  120. [EM_ARM] = arch__new_arm,
  121. [EM_AARCH64] = arch__new_arm64,
  122. [EM_CSKY] = arch__new_csky,
  123. [EM_LOONGARCH] = arch__new_loongarch,
  124. [EM_MIPS] = arch__new_mips,
  125. [EM_PPC64] = arch__new_powerpc,
  126. [EM_PPC] = arch__new_powerpc,
  127. [EM_RISCV] = arch__new_riscv64,
  128. [EM_S390] = arch__new_s390,
  129. [EM_SPARC] = arch__new_sparc,
  130. [EM_SPARCV9] = arch__new_sparc,
  131. [EM_X86_64] = arch__new_x86,
  132. };
  133. static const struct arch **archs;
  134. static size_t num_archs;
  135. struct e_machine_and_e_flags key = {
  136. .e_machine = e_machine,
  137. .e_flags = e_flags,
  138. };
  139. const struct arch *result = NULL, **tmp;
  140. if (num_archs > 0) {
  141. tmp = bsearch(&key, archs, num_archs, sizeof(*archs), arch__key_cmp);
  142. if (tmp)
  143. result = *tmp;
  144. }
  145. if (result)
  146. return result;
  147. if (e_machine >= ARRAY_SIZE(arch_new_fn) || arch_new_fn[e_machine] == NULL) {
  148. errno = ENOTSUP;
  149. return NULL;
  150. }
  151. tmp = reallocarray(archs, num_archs + 1, sizeof(*archs));
  152. if (!tmp)
  153. return NULL;
  154. result = arch_new_fn[e_machine](&key, cpuid);
  155. if (!result) {
  156. pr_err("%s: failed to initialize %s (%u) arch priv area\n",
  157. __func__, result->name, e_machine);
  158. free(tmp);
  159. return NULL;
  160. }
  161. archs = tmp;
  162. archs[num_archs++] = result;
  163. qsort(archs, num_archs, sizeof(*archs), arch__cmp);
  164. return result;
  165. }
  166. bool arch__is_x86(const struct arch *arch)
  167. {
  168. return arch->id.e_machine == EM_386 || arch->id.e_machine == EM_X86_64;
  169. }
  170. bool arch__is_powerpc(const struct arch *arch)
  171. {
  172. return arch->id.e_machine == EM_PPC || arch->id.e_machine == EM_PPC64;
  173. }
  174. static void ins_ops__delete(struct ins_operands *ops)
  175. {
  176. if (ops == NULL)
  177. return;
  178. zfree(&ops->source.raw);
  179. zfree(&ops->source.name);
  180. zfree(&ops->target.raw);
  181. zfree(&ops->target.name);
  182. }
  183. int ins__raw_scnprintf(const struct ins *ins, char *bf, size_t size,
  184. struct ins_operands *ops, int max_ins_name)
  185. {
  186. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
  187. }
  188. int ins__scnprintf(const struct ins *ins, char *bf, size_t size,
  189. struct ins_operands *ops, int max_ins_name)
  190. {
  191. if (ins->ops->scnprintf)
  192. return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
  193. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  194. }
  195. bool ins__is_fused(const struct arch *arch, const char *ins1, const char *ins2)
  196. {
  197. if (!arch || !arch->ins_is_fused)
  198. return false;
  199. return arch->ins_is_fused(arch, ins1, ins2);
  200. }
  201. static int call__parse(const struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
  202. struct disasm_line *dl __maybe_unused)
  203. {
  204. char *endptr, *tok, *name;
  205. struct map *map = ms->map;
  206. struct addr_map_symbol target;
  207. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  208. name = strchr(endptr, '<');
  209. if (name == NULL)
  210. goto indirect_call;
  211. name++;
  212. if (arch->objdump.skip_functions_char &&
  213. strchr(name, arch->objdump.skip_functions_char))
  214. return -1;
  215. tok = strchr(name, '>');
  216. if (tok == NULL)
  217. return -1;
  218. *tok = '\0';
  219. ops->target.name = strdup(name);
  220. *tok = '>';
  221. if (ops->target.name == NULL)
  222. return -1;
  223. find_target:
  224. target = (struct addr_map_symbol) {
  225. .ms = { .map = map__get(map), },
  226. .addr = map__objdump_2mem(map, ops->target.addr),
  227. };
  228. if (maps__find_ams(thread__maps(ms->thread), &target) == 0 &&
  229. map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
  230. ops->target.sym = target.ms.sym;
  231. addr_map_symbol__exit(&target);
  232. return 0;
  233. indirect_call:
  234. tok = strchr(endptr, '*');
  235. if (tok != NULL) {
  236. endptr++;
  237. /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
  238. * Do not parse such instruction. */
  239. if (strstr(endptr, "(%r") == NULL)
  240. ops->target.addr = strtoull(endptr, NULL, 16);
  241. }
  242. goto find_target;
  243. }
  244. int call__scnprintf(const struct ins *ins, char *bf, size_t size,
  245. struct ins_operands *ops, int max_ins_name)
  246. {
  247. if (ops->target.sym)
  248. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
  249. if (ops->target.addr == 0)
  250. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  251. if (ops->target.name)
  252. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
  253. return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
  254. }
  255. const struct ins_ops call_ops = {
  256. .parse = call__parse,
  257. .scnprintf = call__scnprintf,
  258. .is_call = true,
  259. };
  260. bool ins__is_call(const struct ins *ins)
  261. {
  262. return ins->ops && ins->ops->is_call;
  263. }
  264. /*
  265. * Prevents from matching commas in the comment section, e.g.:
  266. * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
  267. *
  268. * and skip comma as part of function arguments, e.g.:
  269. * 1d8b4ac <linemap_lookup(line_maps const*, unsigned int)+0xcc>
  270. */
  271. static inline const char *validate_comma(const char *c, struct ins_operands *ops)
  272. {
  273. if (ops->jump.raw_comment && c > ops->jump.raw_comment)
  274. return NULL;
  275. if (ops->jump.raw_func_start && c > ops->jump.raw_func_start)
  276. return NULL;
  277. return c;
  278. }
  279. static int jump__parse(const struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
  280. struct disasm_line *dl __maybe_unused)
  281. {
  282. struct map *map = ms->map;
  283. struct symbol *sym = ms->sym;
  284. struct addr_map_symbol target = {
  285. .ms = { .map = map__get(map), },
  286. };
  287. const char *c = strchr(ops->raw, ',');
  288. u64 start, end;
  289. ops->jump.raw_comment = strchr(ops->raw, arch->objdump.comment_char);
  290. ops->jump.raw_func_start = strchr(ops->raw, '<');
  291. c = validate_comma(c, ops);
  292. /*
  293. * Examples of lines to parse for the _cpp_lex_token@@Base
  294. * function:
  295. *
  296. * 1159e6c: jne 115aa32 <_cpp_lex_token@@Base+0xf92>
  297. * 1159e8b: jne c469be <cpp_named_operator2name@@Base+0xa72>
  298. *
  299. * The first is a jump to an offset inside the same function,
  300. * the second is to another function, i.e. that 0xa72 is an
  301. * offset in the cpp_named_operator2name@@base function.
  302. */
  303. /*
  304. * skip over possible up to 2 operands to get to address, e.g.:
  305. * tbnz w0, #26, ffff0000083cd190 <security_file_permission+0xd0>
  306. */
  307. if (c != NULL) {
  308. c++;
  309. ops->target.addr = strtoull(c, NULL, 16);
  310. if (!ops->target.addr) {
  311. c = strchr(c, ',');
  312. c = validate_comma(c, ops);
  313. if (c != NULL) {
  314. c++;
  315. ops->target.addr = strtoull(c, NULL, 16);
  316. }
  317. }
  318. } else {
  319. ops->target.addr = strtoull(ops->raw, NULL, 16);
  320. }
  321. target.addr = map__objdump_2mem(map, ops->target.addr);
  322. start = map__unmap_ip(map, sym->start);
  323. end = map__unmap_ip(map, sym->end);
  324. ops->target.outside = target.addr < start || target.addr >= end;
  325. /*
  326. * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
  327. cpp_named_operator2name@@Base+0xa72
  328. * Point to a place that is after the cpp_named_operator2name
  329. * boundaries, i.e. in the ELF symbol table for cc1
  330. * cpp_named_operator2name is marked as being 32-bytes long, but it in
  331. * fact is much larger than that, so we seem to need a symbols__find()
  332. * routine that looks for >= current->start and < next_symbol->start,
  333. * possibly just for C++ objects?
  334. *
  335. * For now lets just make some progress by marking jumps to outside the
  336. * current function as call like.
  337. *
  338. * Actual navigation will come next, with further understanding of how
  339. * the symbol searching and disassembly should be done.
  340. */
  341. if (maps__find_ams(thread__maps(ms->thread), &target) == 0 &&
  342. map__rip_2objdump(target.ms.map, map__map_ip(target.ms.map, target.addr)) == ops->target.addr)
  343. ops->target.sym = target.ms.sym;
  344. if (!ops->target.outside) {
  345. ops->target.offset = target.addr - start;
  346. ops->target.offset_avail = true;
  347. } else {
  348. ops->target.offset_avail = false;
  349. }
  350. addr_map_symbol__exit(&target);
  351. return 0;
  352. }
  353. int jump__scnprintf(const struct ins *ins, char *bf, size_t size,
  354. struct ins_operands *ops, int max_ins_name)
  355. {
  356. const char *c;
  357. if (!ops->target.addr || ops->target.offset < 0)
  358. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  359. if (ops->target.outside && ops->target.sym != NULL)
  360. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
  361. c = strchr(ops->raw, ',');
  362. c = validate_comma(c, ops);
  363. if (c != NULL) {
  364. const char *c2 = strchr(c + 1, ',');
  365. c2 = validate_comma(c2, ops);
  366. /* check for 3-op insn */
  367. if (c2 != NULL)
  368. c = c2;
  369. c++;
  370. /* mirror arch objdump's space-after-comma style */
  371. if (*c == ' ')
  372. c++;
  373. }
  374. return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
  375. ins->name, c ? c - ops->raw : 0, ops->raw,
  376. ops->target.offset);
  377. }
  378. static void jump__delete(struct ins_operands *ops __maybe_unused)
  379. {
  380. /*
  381. * The ops->jump.raw_comment and ops->jump.raw_func_start belong to the
  382. * raw string, don't free them.
  383. */
  384. }
  385. const struct ins_ops jump_ops = {
  386. .free = jump__delete,
  387. .parse = jump__parse,
  388. .scnprintf = jump__scnprintf,
  389. .is_jump = true,
  390. };
  391. bool ins__is_jump(const struct ins *ins)
  392. {
  393. return ins->ops && ins->ops->is_jump;
  394. }
  395. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  396. {
  397. char *endptr, *name, *t;
  398. if (strstr(raw, "(%rip)") == NULL)
  399. return 0;
  400. *addrp = strtoull(comment, &endptr, 16);
  401. if (endptr == comment)
  402. return 0;
  403. name = strchr(endptr, '<');
  404. if (name == NULL)
  405. return -1;
  406. name++;
  407. t = strchr(name, '>');
  408. if (t == NULL)
  409. return 0;
  410. *t = '\0';
  411. *namep = strdup(name);
  412. *t = '>';
  413. return 0;
  414. }
  415. static int lock__parse(const struct arch *arch, struct ins_operands *ops, struct map_symbol *ms,
  416. struct disasm_line *dl __maybe_unused)
  417. {
  418. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  419. if (ops->locked.ops == NULL)
  420. return 0;
  421. if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
  422. goto out_free_ops;
  423. ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name, 0);
  424. if (ops->locked.ins.ops == NULL)
  425. goto out_free_ops;
  426. if (ops->locked.ins.ops->parse &&
  427. ops->locked.ins.ops->parse(arch, ops->locked.ops, ms, NULL) < 0)
  428. goto out_free_ops;
  429. return 0;
  430. out_free_ops:
  431. zfree(&ops->locked.ops);
  432. return 0;
  433. }
  434. static int lock__scnprintf(const struct ins *ins, char *bf, size_t size,
  435. struct ins_operands *ops, int max_ins_name)
  436. {
  437. int printed;
  438. if (ops->locked.ins.ops == NULL)
  439. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  440. printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
  441. return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
  442. size - printed, ops->locked.ops, max_ins_name);
  443. }
  444. static void lock__delete(struct ins_operands *ops)
  445. {
  446. struct ins *ins = &ops->locked.ins;
  447. if (ins->ops && ins->ops->free)
  448. ins->ops->free(ops->locked.ops);
  449. else
  450. ins_ops__delete(ops->locked.ops);
  451. zfree(&ops->locked.ops);
  452. zfree(&ops->locked.ins.name);
  453. zfree(&ops->target.raw);
  454. zfree(&ops->target.name);
  455. }
  456. const struct ins_ops lock_ops = {
  457. .free = lock__delete,
  458. .parse = lock__parse,
  459. .scnprintf = lock__scnprintf,
  460. };
  461. /*
  462. * Check if the operand has more than one registers like x86 SIB addressing:
  463. * 0x1234(%rax, %rbx, 8)
  464. *
  465. * But it doesn't care segment selectors like %gs:0x5678(%rcx), so just check
  466. * the input string after 'memory_ref_char' if exists.
  467. */
  468. static bool check_multi_regs(const struct arch *arch, const char *op)
  469. {
  470. int count = 0;
  471. if (arch->objdump.register_char == 0)
  472. return false;
  473. if (arch->objdump.memory_ref_char) {
  474. op = strchr(op, arch->objdump.memory_ref_char);
  475. if (op == NULL)
  476. return false;
  477. }
  478. while ((op = strchr(op, arch->objdump.register_char)) != NULL) {
  479. count++;
  480. op++;
  481. }
  482. return count > 1;
  483. }
  484. static int mov__parse(const struct arch *arch, struct ins_operands *ops,
  485. struct map_symbol *ms __maybe_unused,
  486. struct disasm_line *dl __maybe_unused)
  487. {
  488. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  489. if (s == NULL)
  490. return -1;
  491. *s = '\0';
  492. /*
  493. * x86 SIB addressing has something like 0x8(%rax, %rcx, 1)
  494. * then it needs to have the closing parenthesis.
  495. */
  496. if (strchr(ops->raw, '(')) {
  497. *s = ',';
  498. s = strchr(ops->raw, ')');
  499. if (s == NULL || s[1] != ',')
  500. return -1;
  501. *++s = '\0';
  502. }
  503. ops->source.raw = strdup(ops->raw);
  504. *s = ',';
  505. if (ops->source.raw == NULL)
  506. return -1;
  507. ops->source.multi_regs = check_multi_regs(arch, ops->source.raw);
  508. target = skip_spaces(++s);
  509. comment = strchr(s, arch->objdump.comment_char);
  510. if (comment != NULL)
  511. s = comment - 1;
  512. else
  513. s = strchr(s, '\0') - 1;
  514. while (s > target && isspace(s[0]))
  515. --s;
  516. s++;
  517. prev = *s;
  518. *s = '\0';
  519. ops->target.raw = strdup(target);
  520. *s = prev;
  521. if (ops->target.raw == NULL)
  522. goto out_free_source;
  523. ops->target.multi_regs = check_multi_regs(arch, ops->target.raw);
  524. if (comment == NULL)
  525. return 0;
  526. comment = skip_spaces(comment);
  527. comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
  528. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  529. return 0;
  530. out_free_source:
  531. zfree(&ops->source.raw);
  532. return -1;
  533. }
  534. int mov__scnprintf(const struct ins *ins, char *bf, size_t size,
  535. struct ins_operands *ops, int max_ins_name)
  536. {
  537. return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
  538. ops->source.name ?: ops->source.raw,
  539. ops->target.name ?: ops->target.raw);
  540. }
  541. const struct ins_ops mov_ops = {
  542. .parse = mov__parse,
  543. .scnprintf = mov__scnprintf,
  544. };
  545. static int dec__parse(const struct arch *arch __maybe_unused, struct ins_operands *ops,
  546. struct map_symbol *ms __maybe_unused,
  547. struct disasm_line *dl __maybe_unused)
  548. {
  549. char *target, *comment, *s, prev;
  550. target = s = ops->raw;
  551. while (s[0] != '\0' && !isspace(s[0]))
  552. ++s;
  553. prev = *s;
  554. *s = '\0';
  555. ops->target.raw = strdup(target);
  556. *s = prev;
  557. if (ops->target.raw == NULL)
  558. return -1;
  559. comment = strchr(s, arch->objdump.comment_char);
  560. if (comment == NULL)
  561. return 0;
  562. comment = skip_spaces(comment);
  563. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  564. return 0;
  565. }
  566. static int dec__scnprintf(const struct ins *ins, char *bf, size_t size,
  567. struct ins_operands *ops, int max_ins_name)
  568. {
  569. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
  570. ops->target.name ?: ops->target.raw);
  571. }
  572. const struct ins_ops dec_ops = {
  573. .parse = dec__parse,
  574. .scnprintf = dec__scnprintf,
  575. };
  576. static int nop__scnprintf(const struct ins *ins __maybe_unused, char *bf, size_t size,
  577. struct ins_operands *ops __maybe_unused, int max_ins_name)
  578. {
  579. return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
  580. }
  581. const struct ins_ops nop_ops = {
  582. .scnprintf = nop__scnprintf,
  583. };
  584. const struct ins_ops ret_ops = {
  585. .scnprintf = ins__raw_scnprintf,
  586. };
  587. static bool ins__is_nop(const struct ins *ins)
  588. {
  589. return ins->ops == &nop_ops;
  590. }
  591. bool ins__is_ret(const struct ins *ins)
  592. {
  593. return ins->ops == &ret_ops;
  594. }
  595. bool ins__is_lock(const struct ins *ins)
  596. {
  597. return ins->ops == &lock_ops;
  598. }
  599. static int ins__key_cmp(const void *name, const void *insp)
  600. {
  601. const struct ins *ins = insp;
  602. return strcmp(name, ins->name);
  603. }
  604. static int ins__cmp(const void *a, const void *b)
  605. {
  606. const struct ins *ia = a;
  607. const struct ins *ib = b;
  608. return strcmp(ia->name, ib->name);
  609. }
  610. static void ins__sort(struct arch *arch)
  611. {
  612. const int nmemb = arch->nr_instructions;
  613. qsort((void *)arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
  614. }
  615. static const struct ins_ops *__ins__find(const struct arch *arch, const char *name,
  616. struct disasm_line *dl)
  617. {
  618. const struct ins *ins;
  619. const int nmemb = arch->nr_instructions;
  620. if (arch__is_powerpc(arch)) {
  621. /*
  622. * For powerpc, identify the instruction ops
  623. * from the opcode using raw_insn.
  624. */
  625. const struct ins_ops *ops;
  626. ops = check_ppc_insn(dl);
  627. if (ops)
  628. return ops;
  629. }
  630. if (!arch->sorted_instructions) {
  631. ins__sort((struct arch *)arch);
  632. ((struct arch *)arch)->sorted_instructions = true;
  633. }
  634. ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  635. if (ins)
  636. return ins->ops;
  637. if (arch->insn_suffix) {
  638. char tmp[32];
  639. char suffix;
  640. size_t len = strlen(name);
  641. if (len == 0 || len >= sizeof(tmp))
  642. return NULL;
  643. suffix = name[len - 1];
  644. if (strchr(arch->insn_suffix, suffix) == NULL)
  645. return NULL;
  646. strcpy(tmp, name);
  647. tmp[len - 1] = '\0'; /* remove the suffix and check again */
  648. ins = bsearch(tmp, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  649. }
  650. return ins ? ins->ops : NULL;
  651. }
  652. const struct ins_ops *ins__find(const struct arch *arch, const char *name, struct disasm_line *dl)
  653. {
  654. const struct ins_ops *ops = __ins__find(arch, name, dl);
  655. if (!ops && arch->associate_instruction_ops)
  656. ops = arch->associate_instruction_ops((struct arch *)arch, name);
  657. return ops;
  658. }
  659. static void disasm_line__init_ins(struct disasm_line *dl, const struct arch *arch,
  660. struct map_symbol *ms)
  661. {
  662. dl->ins.ops = ins__find(arch, dl->ins.name, dl);
  663. if (!dl->ins.ops)
  664. return;
  665. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms, dl) < 0)
  666. dl->ins.ops = NULL;
  667. }
  668. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  669. {
  670. char tmp, *name = skip_spaces(line);
  671. if (name[0] == '\0')
  672. return -1;
  673. *rawp = name + 1;
  674. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  675. ++*rawp;
  676. tmp = (*rawp)[0];
  677. (*rawp)[0] = '\0';
  678. *namep = strdup(name);
  679. if (*namep == NULL)
  680. goto out;
  681. (*rawp)[0] = tmp;
  682. *rawp = strim(*rawp);
  683. return 0;
  684. out:
  685. return -1;
  686. }
  687. /*
  688. * Parses the result captured from symbol__disassemble_*
  689. * Example, line read from DSO file in powerpc:
  690. * line: 38 01 81 e8
  691. * opcode: fetched from arch specific get_opcode_insn
  692. * rawp_insn: e8810138
  693. *
  694. * rawp_insn is used later to extract the reg/offset fields
  695. */
  696. #define PPC_OP(op) (((op) >> 26) & 0x3F)
  697. #define RAW_BYTES 11
  698. static int disasm_line__parse_powerpc(struct disasm_line *dl, struct annotate_args *args)
  699. {
  700. char *line = dl->al.line;
  701. const char **namep = &dl->ins.name;
  702. char **rawp = &dl->ops.raw;
  703. char *tmp_raw_insn, *name_raw_insn = skip_spaces(line);
  704. char *name = skip_spaces(name_raw_insn + RAW_BYTES);
  705. int disasm = 0;
  706. int ret = 0;
  707. if (args->options->disassembler_used)
  708. disasm = 1;
  709. if (name_raw_insn[0] == '\0')
  710. return -1;
  711. if (disasm)
  712. ret = disasm_line__parse(name, namep, rawp);
  713. else
  714. *namep = "";
  715. tmp_raw_insn = strndup(name_raw_insn, 11);
  716. if (tmp_raw_insn == NULL)
  717. return -1;
  718. remove_spaces(tmp_raw_insn);
  719. sscanf(tmp_raw_insn, "%x", &dl->raw.raw_insn);
  720. if (disasm)
  721. dl->raw.raw_insn = be32_to_cpu(dl->raw.raw_insn);
  722. return ret;
  723. }
  724. static void annotation_line__init(struct annotation_line *al,
  725. struct annotate_args *args,
  726. int nr)
  727. {
  728. al->offset = args->offset;
  729. al->line = strdup(args->line);
  730. al->line_nr = args->line_nr;
  731. al->fileloc = args->fileloc;
  732. al->data_nr = nr;
  733. }
  734. static void annotation_line__exit(struct annotation_line *al)
  735. {
  736. zfree_srcline(&al->path);
  737. zfree(&al->line);
  738. zfree(&al->cycles);
  739. zfree(&al->br_cntr);
  740. }
  741. static size_t disasm_line_size(int nr)
  742. {
  743. struct annotation_line *al;
  744. return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
  745. }
  746. /*
  747. * Allocating the disasm annotation line data with
  748. * following structure:
  749. *
  750. * -------------------------------------------
  751. * struct disasm_line | struct annotation_line
  752. * -------------------------------------------
  753. *
  754. * We have 'struct annotation_line' member as last member
  755. * of 'struct disasm_line' to have an easy access.
  756. */
  757. struct disasm_line *disasm_line__new(struct annotate_args *args)
  758. {
  759. struct disasm_line *dl = NULL;
  760. struct annotation *notes = symbol__annotation(args->ms->sym);
  761. int nr = notes->src->nr_events;
  762. dl = zalloc(disasm_line_size(nr));
  763. if (!dl)
  764. return NULL;
  765. annotation_line__init(&dl->al, args, nr);
  766. if (dl->al.line == NULL)
  767. goto out_delete;
  768. if (args->offset != -1) {
  769. if (arch__is_powerpc(args->arch)) {
  770. if (disasm_line__parse_powerpc(dl, args) < 0)
  771. goto out_free_line;
  772. } else if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  773. goto out_free_line;
  774. disasm_line__init_ins(dl, args->arch, args->ms);
  775. }
  776. return dl;
  777. out_free_line:
  778. zfree(&dl->al.line);
  779. out_delete:
  780. free(dl);
  781. return NULL;
  782. }
  783. void disasm_line__free(struct disasm_line *dl)
  784. {
  785. if (dl->ins.ops && dl->ins.ops->free)
  786. dl->ins.ops->free(&dl->ops);
  787. else
  788. ins_ops__delete(&dl->ops);
  789. zfree(&dl->ins.name);
  790. annotation_line__exit(&dl->al);
  791. free(dl);
  792. }
  793. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
  794. {
  795. if (raw || !dl->ins.ops)
  796. return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
  797. return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
  798. }
  799. /*
  800. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  801. * which looks like following
  802. *
  803. * 0000000000415500 <_init>:
  804. * 415500: sub $0x8,%rsp
  805. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  806. * 41550b: test %rax,%rax
  807. * 41550e: je 415515 <_init+0x15>
  808. * 415510: callq 416e70 <__gmon_start__@plt>
  809. * 415515: add $0x8,%rsp
  810. * 415519: retq
  811. *
  812. * it will be parsed and saved into struct disasm_line as
  813. * <offset> <name> <ops.raw>
  814. *
  815. * The offset will be a relative offset from the start of the symbol and -1
  816. * means that it's not a disassembly line so should be treated differently.
  817. * The ops.raw part will be parsed further according to type of the instruction.
  818. */
  819. static int symbol__parse_objdump_line(struct symbol *sym,
  820. struct annotate_args *args,
  821. char *parsed_line, int *line_nr, char **fileloc)
  822. {
  823. struct map *map = args->ms->map;
  824. struct annotation *notes = symbol__annotation(sym);
  825. struct disasm_line *dl;
  826. char *tmp;
  827. s64 line_ip, offset = -1;
  828. regmatch_t match[2];
  829. /* /filename:linenr ? Save line number and ignore. */
  830. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  831. *line_nr = atoi(parsed_line + match[1].rm_so);
  832. free(*fileloc);
  833. *fileloc = strdup(parsed_line);
  834. return 0;
  835. }
  836. /* Process hex address followed by ':'. */
  837. line_ip = strtoull(parsed_line, &tmp, 16);
  838. if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
  839. u64 start = map__rip_2objdump(map, sym->start),
  840. end = map__rip_2objdump(map, sym->end);
  841. offset = line_ip - start;
  842. if ((u64)line_ip < start || (u64)line_ip >= end)
  843. offset = -1;
  844. else
  845. parsed_line = tmp + 1;
  846. }
  847. args->offset = offset;
  848. args->line = parsed_line;
  849. args->line_nr = *line_nr;
  850. args->fileloc = *fileloc;
  851. args->ms->sym = sym;
  852. dl = disasm_line__new(args);
  853. (*line_nr)++;
  854. if (dl == NULL)
  855. return -1;
  856. if (!disasm_line__has_local_offset(dl)) {
  857. dl->ops.target.offset = dl->ops.target.addr -
  858. map__rip_2objdump(map, sym->start);
  859. dl->ops.target.offset_avail = true;
  860. }
  861. /* kcore has no symbols, so add the call target symbol */
  862. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
  863. struct addr_map_symbol target = {
  864. .addr = dl->ops.target.addr,
  865. .ms = { .map = map__get(map), },
  866. };
  867. if (!maps__find_ams(thread__maps(args->ms->thread), &target) &&
  868. target.ms.sym->start == target.al_addr)
  869. dl->ops.target.sym = target.ms.sym;
  870. addr_map_symbol__exit(&target);
  871. }
  872. annotation_line__add(&dl->al, &notes->src->source);
  873. return 0;
  874. }
  875. static void delete_last_nop(struct symbol *sym)
  876. {
  877. struct annotation *notes = symbol__annotation(sym);
  878. struct list_head *list = &notes->src->source;
  879. struct disasm_line *dl;
  880. while (!list_empty(list)) {
  881. dl = list_entry(list->prev, struct disasm_line, al.node);
  882. if (dl->ins.ops) {
  883. if (!ins__is_nop(&dl->ins))
  884. return;
  885. } else {
  886. if (!strstr(dl->al.line, " nop ") &&
  887. !strstr(dl->al.line, " nopl ") &&
  888. !strstr(dl->al.line, " nopw "))
  889. return;
  890. }
  891. list_del_init(&dl->al.node);
  892. disasm_line__free(dl);
  893. }
  894. }
  895. int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
  896. {
  897. struct dso *dso = map__dso(ms->map);
  898. BUG_ON(buflen == 0);
  899. if (errnum >= 0) {
  900. str_error_r(errnum, buf, buflen);
  901. return 0;
  902. }
  903. switch (errnum) {
  904. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  905. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  906. char *build_id_msg = NULL;
  907. if (dso__has_build_id(dso)) {
  908. build_id__snprintf(dso__bid(dso), bf + 15, sizeof(bf) - 15);
  909. build_id_msg = bf;
  910. }
  911. scnprintf(buf, buflen,
  912. "No vmlinux file%s\nwas found in the path.\n\n"
  913. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  914. "Please use:\n\n"
  915. " perf buildid-cache -vu vmlinux\n\n"
  916. "or:\n\n"
  917. " --vmlinux vmlinux\n", build_id_msg ?: "");
  918. }
  919. break;
  920. case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
  921. scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
  922. break;
  923. case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
  924. scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
  925. break;
  926. case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
  927. scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
  928. break;
  929. case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
  930. scnprintf(buf, buflen, "Invalid BPF file: %s.", dso__long_name(dso));
  931. break;
  932. case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
  933. scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
  934. dso__long_name(dso));
  935. break;
  936. case SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE:
  937. scnprintf(buf, buflen, "Couldn't determine the file %s type.", dso__long_name(dso));
  938. break;
  939. default:
  940. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  941. break;
  942. }
  943. return 0;
  944. }
  945. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  946. {
  947. char linkname[PATH_MAX];
  948. char *build_id_filename;
  949. char *build_id_path = NULL;
  950. char *pos;
  951. int len;
  952. if (dso__symtab_type(dso) == DSO_BINARY_TYPE__KALLSYMS &&
  953. !dso__is_kcore(dso))
  954. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  955. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  956. if (build_id_filename) {
  957. __symbol__join_symfs(filename, filename_size, build_id_filename);
  958. free(build_id_filename);
  959. } else {
  960. if (dso__has_build_id(dso))
  961. return ENOMEM;
  962. goto fallback;
  963. }
  964. build_id_path = strdup(filename);
  965. if (!build_id_path)
  966. return ENOMEM;
  967. /*
  968. * old style build-id cache has name of XX/XXXXXXX.. while
  969. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  970. * extract the build-id part of dirname in the new style only.
  971. */
  972. pos = strrchr(build_id_path, '/');
  973. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  974. dirname(build_id_path);
  975. if (dso__is_kcore(dso))
  976. goto fallback;
  977. len = readlink(build_id_path, linkname, sizeof(linkname) - 1);
  978. if (len < 0)
  979. goto fallback;
  980. linkname[len] = '\0';
  981. if (strstr(linkname, DSO__NAME_KALLSYMS) ||
  982. access(filename, R_OK)) {
  983. fallback:
  984. /*
  985. * If we don't have build-ids or the build-id file isn't in the
  986. * cache, or is just a kallsyms file, well, lets hope that this
  987. * DSO is the same as when 'perf record' ran.
  988. */
  989. if (dso__kernel(dso) && dso__long_name(dso)[0] == '/')
  990. snprintf(filename, filename_size, "%s", dso__long_name(dso));
  991. else
  992. __symbol__join_symfs(filename, filename_size, dso__long_name(dso));
  993. mutex_lock(dso__lock(dso));
  994. if (access(filename, R_OK) && errno == ENOENT && dso__nsinfo(dso)) {
  995. char *new_name = dso__filename_with_chroot(dso, filename);
  996. if (new_name) {
  997. strlcpy(filename, new_name, filename_size);
  998. free(new_name);
  999. }
  1000. }
  1001. mutex_unlock(dso__lock(dso));
  1002. } else if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
  1003. dso__set_binary_type(dso, DSO_BINARY_TYPE__BUILD_ID_CACHE);
  1004. }
  1005. free(build_id_path);
  1006. return 0;
  1007. }
  1008. static int symbol__disassemble_raw(char *filename, struct symbol *sym,
  1009. struct annotate_args *args)
  1010. {
  1011. struct annotation *notes = symbol__annotation(sym);
  1012. struct map *map = args->ms->map;
  1013. struct dso *dso = map__dso(map);
  1014. u64 start = map__rip_2objdump(map, sym->start);
  1015. u64 end = map__rip_2objdump(map, sym->end);
  1016. u64 len = end - start;
  1017. u64 offset;
  1018. int i, count;
  1019. u8 *buf = NULL;
  1020. char disasm_buf[512];
  1021. struct disasm_line *dl;
  1022. u32 *line;
  1023. /* Return if objdump is specified explicitly */
  1024. if (args->options->objdump_path)
  1025. return -1;
  1026. pr_debug("Reading raw instruction from : %s using dso__data_read_offset\n", filename);
  1027. buf = malloc(len);
  1028. if (buf == NULL)
  1029. goto err;
  1030. count = dso__data_read_offset(dso, NULL, sym->start, buf, len);
  1031. line = (u32 *)buf;
  1032. if ((u64)count != len)
  1033. goto err;
  1034. /* add the function address and name */
  1035. scnprintf(disasm_buf, sizeof(disasm_buf), "%#"PRIx64" <%s>:",
  1036. start, sym->name);
  1037. args->offset = -1;
  1038. args->line = disasm_buf;
  1039. args->line_nr = 0;
  1040. args->fileloc = NULL;
  1041. args->ms->sym = sym;
  1042. dl = disasm_line__new(args);
  1043. if (dl == NULL)
  1044. goto err;
  1045. annotation_line__add(&dl->al, &notes->src->source);
  1046. /* Each raw instruction is 4 byte */
  1047. count = len/4;
  1048. for (i = 0, offset = 0; i < count; i++) {
  1049. args->offset = offset;
  1050. sprintf(args->line, "%x", line[i]);
  1051. dl = disasm_line__new(args);
  1052. if (dl == NULL)
  1053. break;
  1054. annotation_line__add(&dl->al, &notes->src->source);
  1055. offset += 4;
  1056. }
  1057. /* It failed in the middle */
  1058. if (offset != len) {
  1059. struct list_head *list = &notes->src->source;
  1060. /* Discard all lines and fallback to objdump */
  1061. while (!list_empty(list)) {
  1062. dl = list_first_entry(list, struct disasm_line, al.node);
  1063. list_del_init(&dl->al.node);
  1064. disasm_line__free(dl);
  1065. }
  1066. count = -1;
  1067. }
  1068. out:
  1069. free(buf);
  1070. return count < 0 ? count : 0;
  1071. err:
  1072. count = -1;
  1073. goto out;
  1074. }
  1075. /*
  1076. * Possibly create a new version of line with tabs expanded. Returns the
  1077. * existing or new line, storage is updated if a new line is allocated. If
  1078. * allocation fails then NULL is returned.
  1079. */
  1080. char *expand_tabs(char *line, char **storage, size_t *storage_len)
  1081. {
  1082. size_t i, src, dst, len, new_storage_len, num_tabs;
  1083. char *new_line;
  1084. size_t line_len = strlen(line);
  1085. for (num_tabs = 0, i = 0; i < line_len; i++)
  1086. if (line[i] == '\t')
  1087. num_tabs++;
  1088. if (num_tabs == 0)
  1089. return line;
  1090. /*
  1091. * Space for the line and '\0', less the leading and trailing
  1092. * spaces. Each tab may introduce 7 additional spaces.
  1093. */
  1094. new_storage_len = line_len + 1 + (num_tabs * 7);
  1095. new_line = malloc(new_storage_len);
  1096. if (new_line == NULL) {
  1097. pr_err("Failure allocating memory for tab expansion\n");
  1098. return NULL;
  1099. }
  1100. /*
  1101. * Copy regions starting at src and expand tabs. If there are two
  1102. * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
  1103. * are inserted.
  1104. */
  1105. for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
  1106. if (line[i] == '\t') {
  1107. len = i - src;
  1108. memcpy(&new_line[dst], &line[src], len);
  1109. dst += len;
  1110. new_line[dst++] = ' ';
  1111. while (dst % 8 != 0)
  1112. new_line[dst++] = ' ';
  1113. src = i + 1;
  1114. num_tabs--;
  1115. }
  1116. }
  1117. /* Expand the last region. */
  1118. len = line_len - src;
  1119. memcpy(&new_line[dst], &line[src], len);
  1120. dst += len;
  1121. new_line[dst] = '\0';
  1122. free(*storage);
  1123. *storage = new_line;
  1124. *storage_len = new_storage_len;
  1125. return new_line;
  1126. }
  1127. static int symbol__disassemble_bpf_image(struct symbol *sym, struct annotate_args *args)
  1128. {
  1129. struct annotation *notes = symbol__annotation(sym);
  1130. struct disasm_line *dl;
  1131. args->offset = -1;
  1132. args->line = strdup("to be implemented");
  1133. args->line_nr = 0;
  1134. args->fileloc = NULL;
  1135. dl = disasm_line__new(args);
  1136. if (dl)
  1137. annotation_line__add(&dl->al, &notes->src->source);
  1138. zfree(&args->line);
  1139. return 0;
  1140. }
  1141. static int symbol__disassemble_objdump(const char *filename, struct symbol *sym,
  1142. struct annotate_args *args)
  1143. {
  1144. struct annotation_options *opts = &annotate_opts;
  1145. struct map *map = args->ms->map;
  1146. struct dso *dso = map__dso(map);
  1147. char *command;
  1148. FILE *file;
  1149. int lineno = 0;
  1150. char *fileloc = NULL;
  1151. int nline;
  1152. char *line;
  1153. size_t line_len;
  1154. const char *objdump_argv[] = {
  1155. "/bin/sh",
  1156. "-c",
  1157. NULL, /* Will be the objdump command to run. */
  1158. "--",
  1159. NULL, /* Will be the symfs path. */
  1160. NULL,
  1161. };
  1162. struct child_process objdump_process;
  1163. int err;
  1164. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_PROG_INFO)
  1165. return symbol__disassemble_bpf_libbfd(sym, args);
  1166. if (dso__binary_type(dso) == DSO_BINARY_TYPE__BPF_IMAGE)
  1167. return symbol__disassemble_bpf_image(sym, args);
  1168. err = asprintf(&command,
  1169. "%s %s%s --start-address=0x%016" PRIx64
  1170. " --stop-address=0x%016" PRIx64
  1171. " %s -d %s %s %s %c%s%c %s%s -C \"$1\"",
  1172. opts->objdump_path ?: "objdump",
  1173. opts->disassembler_style ? "-M " : "",
  1174. opts->disassembler_style ?: "",
  1175. map__rip_2objdump(map, sym->start),
  1176. map__rip_2objdump(map, sym->end),
  1177. opts->show_linenr ? "-l" : "",
  1178. opts->show_asm_raw ? "" : "--no-show-raw-insn",
  1179. opts->annotate_src ? "-S" : "",
  1180. opts->prefix ? "--prefix " : "",
  1181. opts->prefix ? '"' : ' ',
  1182. opts->prefix ?: "",
  1183. opts->prefix ? '"' : ' ',
  1184. opts->prefix_strip ? "--prefix-strip=" : "",
  1185. opts->prefix_strip ?: "");
  1186. if (err < 0) {
  1187. pr_err("Failure allocating memory for the command to run\n");
  1188. return err;
  1189. }
  1190. pr_debug("Executing: %s\n", command);
  1191. objdump_argv[2] = command;
  1192. objdump_argv[4] = filename;
  1193. /* Create a pipe to read from for stdout */
  1194. memset(&objdump_process, 0, sizeof(objdump_process));
  1195. objdump_process.argv = objdump_argv;
  1196. objdump_process.out = -1;
  1197. objdump_process.err = -1;
  1198. objdump_process.no_stderr = 1;
  1199. if (start_command(&objdump_process)) {
  1200. pr_err("Failure starting to run %s\n", command);
  1201. err = -1;
  1202. goto out_free_command;
  1203. }
  1204. file = fdopen(objdump_process.out, "r");
  1205. if (!file) {
  1206. pr_err("Failure creating FILE stream for %s\n", command);
  1207. /*
  1208. * If we were using debug info should retry with
  1209. * original binary.
  1210. */
  1211. err = -1;
  1212. goto out_close_stdout;
  1213. }
  1214. /* Storage for getline. */
  1215. line = NULL;
  1216. line_len = 0;
  1217. nline = 0;
  1218. while (!feof(file)) {
  1219. const char *match;
  1220. char *expanded_line;
  1221. if (getline(&line, &line_len, file) < 0 || !line)
  1222. break;
  1223. /* Skip lines containing "filename:" */
  1224. match = strstr(line, filename);
  1225. if (match && match[strlen(filename)] == ':')
  1226. continue;
  1227. expanded_line = strim(line);
  1228. expanded_line = expand_tabs(expanded_line, &line, &line_len);
  1229. if (!expanded_line)
  1230. break;
  1231. /*
  1232. * The source code line number (lineno) needs to be kept in
  1233. * across calls to symbol__parse_objdump_line(), so that it
  1234. * can associate it with the instructions till the next one.
  1235. * See disasm_line__new() and struct disasm_line::line_nr.
  1236. */
  1237. if (symbol__parse_objdump_line(sym, args, expanded_line,
  1238. &lineno, &fileloc) < 0)
  1239. break;
  1240. nline++;
  1241. }
  1242. free(line);
  1243. free(fileloc);
  1244. err = finish_command(&objdump_process);
  1245. if (err)
  1246. pr_err("Error running %s\n", command);
  1247. if (nline == 0) {
  1248. err = -1;
  1249. pr_err("No output from %s\n", command);
  1250. }
  1251. /*
  1252. * kallsyms does not have symbol sizes so there may a nop at the end.
  1253. * Remove it.
  1254. */
  1255. if (dso__is_kcore(dso))
  1256. delete_last_nop(sym);
  1257. fclose(file);
  1258. out_close_stdout:
  1259. close(objdump_process.out);
  1260. out_free_command:
  1261. free(command);
  1262. return err;
  1263. }
  1264. int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
  1265. {
  1266. struct annotation_options *options = args->options;
  1267. struct map *map = args->ms->map;
  1268. struct dso *dso = map__dso(map);
  1269. char symfs_filename[PATH_MAX];
  1270. bool delete_extract = false;
  1271. struct kcore_extract kce;
  1272. bool decomp = false;
  1273. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1274. if (err)
  1275. return err;
  1276. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1277. symfs_filename, sym->name, map__unmap_ip(map, sym->start),
  1278. map__unmap_ip(map, sym->end));
  1279. pr_debug("annotating [%p] %30s : [%p] %30s\n", dso, dso__long_name(dso), sym, sym->name);
  1280. if (dso__binary_type(dso) == DSO_BINARY_TYPE__NOT_FOUND) {
  1281. return SYMBOL_ANNOTATE_ERRNO__COULDNT_DETERMINE_FILE_TYPE;
  1282. } else if (dso__is_kcore(dso)) {
  1283. kce.addr = map__rip_2objdump(map, sym->start);
  1284. kce.kcore_filename = symfs_filename;
  1285. kce.len = sym->end - sym->start;
  1286. kce.offs = sym->start;
  1287. if (!kcore_extract__create(&kce)) {
  1288. delete_extract = true;
  1289. strlcpy(symfs_filename, kce.extract_filename, sizeof(symfs_filename));
  1290. }
  1291. } else if (dso__needs_decompress(dso)) {
  1292. char tmp[KMOD_DECOMP_LEN];
  1293. if (dso__decompress_kmodule_path(dso, symfs_filename, tmp, sizeof(tmp)) < 0)
  1294. return -1;
  1295. decomp = true;
  1296. strcpy(symfs_filename, tmp);
  1297. }
  1298. /*
  1299. * For powerpc data type profiling, use the dso__data_read_offset to
  1300. * read raw instruction directly and interpret the binary code to
  1301. * understand instructions and register fields. For sort keys as type
  1302. * and typeoff, disassemble to mnemonic notation is not required in
  1303. * case of powerpc.
  1304. */
  1305. if (arch__is_powerpc(args->arch)) {
  1306. extern const char *sort_order;
  1307. if (sort_order && !strstr(sort_order, "sym")) {
  1308. err = symbol__disassemble_raw(symfs_filename, sym, args);
  1309. if (err == 0)
  1310. goto out_remove_tmp;
  1311. err = symbol__disassemble_capstone_powerpc(symfs_filename, sym, args);
  1312. if (err == 0)
  1313. goto out_remove_tmp;
  1314. }
  1315. }
  1316. /* FIXME: LLVM and CAPSTONE should support source code */
  1317. if (options->annotate_src && !options->hide_src_code) {
  1318. err = symbol__disassemble_objdump(symfs_filename, sym, args);
  1319. if (err == 0)
  1320. goto out_remove_tmp;
  1321. }
  1322. err = -1;
  1323. for (u8 i = 0; i < ARRAY_SIZE(options->disassemblers) && err != 0; i++) {
  1324. enum perf_disassembler dis = options->disassemblers[i];
  1325. switch (dis) {
  1326. case PERF_DISASM_LLVM:
  1327. args->options->disassembler_used = PERF_DISASM_LLVM;
  1328. err = symbol__disassemble_llvm(symfs_filename, sym, args);
  1329. break;
  1330. case PERF_DISASM_CAPSTONE:
  1331. args->options->disassembler_used = PERF_DISASM_CAPSTONE;
  1332. err = symbol__disassemble_capstone(symfs_filename, sym, args);
  1333. break;
  1334. case PERF_DISASM_OBJDUMP:
  1335. args->options->disassembler_used = PERF_DISASM_OBJDUMP;
  1336. err = symbol__disassemble_objdump(symfs_filename, sym, args);
  1337. break;
  1338. case PERF_DISASM_UNKNOWN: /* End of disassemblers. */
  1339. default:
  1340. args->options->disassembler_used = PERF_DISASM_UNKNOWN;
  1341. goto out_remove_tmp;
  1342. }
  1343. if (err == 0)
  1344. pr_debug("Disassembled with %s\n", perf_disassembler__strs[dis]);
  1345. }
  1346. out_remove_tmp:
  1347. if (decomp)
  1348. unlink(symfs_filename);
  1349. if (delete_extract)
  1350. kcore_extract__delete(&kce);
  1351. return err;
  1352. }