PageRenderTime 77ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/perf/util/annotate.c

http://github.com/torvalds/linux
C | 3175 lines | 2421 code | 563 blank | 191 comment | 547 complexity | b9bb8acf632791c4db799b04a17f432c MD5 | raw file
Possible License(s): LGPL-2.0, AGPL-1.0, GPL-2.0

Large files files are truncated, but you can click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  4. *
  5. * Parts came from builtin-annotate.c, see those files for further
  6. * copyright notes.
  7. */
  8. #include <errno.h>
  9. #include <inttypes.h>
  10. #include <libgen.h>
  11. #include <stdlib.h>
  12. #include <bpf/bpf.h>
  13. #include <bpf/btf.h>
  14. #include <bpf/libbpf.h>
  15. #include <linux/btf.h>
  16. #include "util.h" // hex_width()
  17. #include "ui/ui.h"
  18. #include "sort.h"
  19. #include "build-id.h"
  20. #include "color.h"
  21. #include "config.h"
  22. #include "dso.h"
  23. #include "env.h"
  24. #include "map.h"
  25. #include "maps.h"
  26. #include "symbol.h"
  27. #include "srcline.h"
  28. #include "units.h"
  29. #include "debug.h"
  30. #include "annotate.h"
  31. #include "evsel.h"
  32. #include "evlist.h"
  33. #include "bpf-event.h"
  34. #include "block-range.h"
  35. #include "string2.h"
  36. #include "util/event.h"
  37. #include "arch/common.h"
  38. #include <regex.h>
  39. #include <pthread.h>
  40. #include <linux/bitops.h>
  41. #include <linux/kernel.h>
  42. #include <linux/string.h>
  43. #include <bpf/libbpf.h>
  44. #include <subcmd/parse-options.h>
  45. #include <subcmd/run-command.h>
  46. /* FIXME: For the HE_COLORSET */
  47. #include "ui/browser.h"
  48. /*
  49. * FIXME: Using the same values as slang.h,
  50. * but that header may not be available everywhere
  51. */
  52. #define LARROW_CHAR ((unsigned char)',')
  53. #define RARROW_CHAR ((unsigned char)'+')
  54. #define DARROW_CHAR ((unsigned char)'.')
  55. #define UARROW_CHAR ((unsigned char)'-')
  56. #include <linux/ctype.h>
  57. struct annotation_options annotation__default_options = {
  58. .use_offset = true,
  59. .jump_arrows = true,
  60. .annotate_src = true,
  61. .offset_level = ANNOTATION__OFFSET_JUMP_TARGETS,
  62. .percent_type = PERCENT_PERIOD_LOCAL,
  63. };
  64. static regex_t file_lineno;
  65. static struct ins_ops *ins__find(struct arch *arch, const char *name);
  66. static void ins__sort(struct arch *arch);
  67. static int disasm_line__parse(char *line, const char **namep, char **rawp);
  68. struct arch {
  69. const char *name;
  70. struct ins *instructions;
  71. size_t nr_instructions;
  72. size_t nr_instructions_allocated;
  73. struct ins_ops *(*associate_instruction_ops)(struct arch *arch, const char *name);
  74. bool sorted_instructions;
  75. bool initialized;
  76. void *priv;
  77. unsigned int model;
  78. unsigned int family;
  79. int (*init)(struct arch *arch, char *cpuid);
  80. bool (*ins_is_fused)(struct arch *arch, const char *ins1,
  81. const char *ins2);
  82. struct {
  83. char comment_char;
  84. char skip_functions_char;
  85. } objdump;
  86. };
  87. static struct ins_ops call_ops;
  88. static struct ins_ops dec_ops;
  89. static struct ins_ops jump_ops;
  90. static struct ins_ops mov_ops;
  91. static struct ins_ops nop_ops;
  92. static struct ins_ops lock_ops;
  93. static struct ins_ops ret_ops;
  94. static int arch__grow_instructions(struct arch *arch)
  95. {
  96. struct ins *new_instructions;
  97. size_t new_nr_allocated;
  98. if (arch->nr_instructions_allocated == 0 && arch->instructions)
  99. goto grow_from_non_allocated_table;
  100. new_nr_allocated = arch->nr_instructions_allocated + 128;
  101. new_instructions = realloc(arch->instructions, new_nr_allocated * sizeof(struct ins));
  102. if (new_instructions == NULL)
  103. return -1;
  104. out_update_instructions:
  105. arch->instructions = new_instructions;
  106. arch->nr_instructions_allocated = new_nr_allocated;
  107. return 0;
  108. grow_from_non_allocated_table:
  109. new_nr_allocated = arch->nr_instructions + 128;
  110. new_instructions = calloc(new_nr_allocated, sizeof(struct ins));
  111. if (new_instructions == NULL)
  112. return -1;
  113. memcpy(new_instructions, arch->instructions, arch->nr_instructions);
  114. goto out_update_instructions;
  115. }
  116. static int arch__associate_ins_ops(struct arch* arch, const char *name, struct ins_ops *ops)
  117. {
  118. struct ins *ins;
  119. if (arch->nr_instructions == arch->nr_instructions_allocated &&
  120. arch__grow_instructions(arch))
  121. return -1;
  122. ins = &arch->instructions[arch->nr_instructions];
  123. ins->name = strdup(name);
  124. if (!ins->name)
  125. return -1;
  126. ins->ops = ops;
  127. arch->nr_instructions++;
  128. ins__sort(arch);
  129. return 0;
  130. }
  131. #include "arch/arc/annotate/instructions.c"
  132. #include "arch/arm/annotate/instructions.c"
  133. #include "arch/arm64/annotate/instructions.c"
  134. #include "arch/csky/annotate/instructions.c"
  135. #include "arch/x86/annotate/instructions.c"
  136. #include "arch/powerpc/annotate/instructions.c"
  137. #include "arch/s390/annotate/instructions.c"
  138. #include "arch/sparc/annotate/instructions.c"
  139. static struct arch architectures[] = {
  140. {
  141. .name = "arc",
  142. .init = arc__annotate_init,
  143. },
  144. {
  145. .name = "arm",
  146. .init = arm__annotate_init,
  147. },
  148. {
  149. .name = "arm64",
  150. .init = arm64__annotate_init,
  151. },
  152. {
  153. .name = "csky",
  154. .init = csky__annotate_init,
  155. },
  156. {
  157. .name = "x86",
  158. .init = x86__annotate_init,
  159. .instructions = x86__instructions,
  160. .nr_instructions = ARRAY_SIZE(x86__instructions),
  161. .ins_is_fused = x86__ins_is_fused,
  162. .objdump = {
  163. .comment_char = '#',
  164. },
  165. },
  166. {
  167. .name = "powerpc",
  168. .init = powerpc__annotate_init,
  169. },
  170. {
  171. .name = "s390",
  172. .init = s390__annotate_init,
  173. .objdump = {
  174. .comment_char = '#',
  175. },
  176. },
  177. {
  178. .name = "sparc",
  179. .init = sparc__annotate_init,
  180. .objdump = {
  181. .comment_char = '#',
  182. },
  183. },
  184. };
  185. static void ins__delete(struct ins_operands *ops)
  186. {
  187. if (ops == NULL)
  188. return;
  189. zfree(&ops->source.raw);
  190. zfree(&ops->source.name);
  191. zfree(&ops->target.raw);
  192. zfree(&ops->target.name);
  193. }
  194. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  195. struct ins_operands *ops, int max_ins_name)
  196. {
  197. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->raw);
  198. }
  199. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  200. struct ins_operands *ops, int max_ins_name)
  201. {
  202. if (ins->ops->scnprintf)
  203. return ins->ops->scnprintf(ins, bf, size, ops, max_ins_name);
  204. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  205. }
  206. bool ins__is_fused(struct arch *arch, const char *ins1, const char *ins2)
  207. {
  208. if (!arch || !arch->ins_is_fused)
  209. return false;
  210. return arch->ins_is_fused(arch, ins1, ins2);
  211. }
  212. static int call__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  213. {
  214. char *endptr, *tok, *name;
  215. struct map *map = ms->map;
  216. struct addr_map_symbol target = {
  217. .ms = { .map = map, },
  218. };
  219. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  220. name = strchr(endptr, '<');
  221. if (name == NULL)
  222. goto indirect_call;
  223. name++;
  224. if (arch->objdump.skip_functions_char &&
  225. strchr(name, arch->objdump.skip_functions_char))
  226. return -1;
  227. tok = strchr(name, '>');
  228. if (tok == NULL)
  229. return -1;
  230. *tok = '\0';
  231. ops->target.name = strdup(name);
  232. *tok = '>';
  233. if (ops->target.name == NULL)
  234. return -1;
  235. find_target:
  236. target.addr = map__objdump_2mem(map, ops->target.addr);
  237. if (maps__find_ams(ms->maps, &target) == 0 &&
  238. map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) == ops->target.addr)
  239. ops->target.sym = target.ms.sym;
  240. return 0;
  241. indirect_call:
  242. tok = strchr(endptr, '*');
  243. if (tok != NULL) {
  244. endptr++;
  245. /* Indirect call can use a non-rip register and offset: callq *0x8(%rbx).
  246. * Do not parse such instruction. */
  247. if (strstr(endptr, "(%r") == NULL)
  248. ops->target.addr = strtoull(endptr, NULL, 16);
  249. }
  250. goto find_target;
  251. }
  252. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  253. struct ins_operands *ops, int max_ins_name)
  254. {
  255. if (ops->target.sym)
  256. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
  257. if (ops->target.addr == 0)
  258. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  259. if (ops->target.name)
  260. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.name);
  261. return scnprintf(bf, size, "%-*s *%" PRIx64, max_ins_name, ins->name, ops->target.addr);
  262. }
  263. static struct ins_ops call_ops = {
  264. .parse = call__parse,
  265. .scnprintf = call__scnprintf,
  266. };
  267. bool ins__is_call(const struct ins *ins)
  268. {
  269. return ins->ops == &call_ops || ins->ops == &s390_call_ops;
  270. }
  271. /*
  272. * Prevents from matching commas in the comment section, e.g.:
  273. * ffff200008446e70: b.cs ffff2000084470f4 <generic_exec_single+0x314> // b.hs, b.nlast
  274. */
  275. static inline const char *validate_comma(const char *c, struct ins_operands *ops)
  276. {
  277. if (ops->raw_comment && c > ops->raw_comment)
  278. return NULL;
  279. return c;
  280. }
  281. static int jump__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  282. {
  283. struct map *map = ms->map;
  284. struct symbol *sym = ms->sym;
  285. struct addr_map_symbol target = {
  286. .ms = { .map = map, },
  287. };
  288. const char *c = strchr(ops->raw, ',');
  289. u64 start, end;
  290. ops->raw_comment = strchr(ops->raw, arch->objdump.comment_char);
  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. ops->target.addr = strtoull(c, NULL, 16);
  309. if (!ops->target.addr) {
  310. c = strchr(c, ',');
  311. c = validate_comma(c, ops);
  312. if (c++ != NULL)
  313. ops->target.addr = strtoull(c, NULL, 16);
  314. }
  315. } else {
  316. ops->target.addr = strtoull(ops->raw, NULL, 16);
  317. }
  318. target.addr = map__objdump_2mem(map, ops->target.addr);
  319. start = map->unmap_ip(map, sym->start),
  320. end = map->unmap_ip(map, sym->end);
  321. ops->target.outside = target.addr < start || target.addr > end;
  322. /*
  323. * FIXME: things like this in _cpp_lex_token (gcc's cc1 program):
  324. cpp_named_operator2name@@Base+0xa72
  325. * Point to a place that is after the cpp_named_operator2name
  326. * boundaries, i.e. in the ELF symbol table for cc1
  327. * cpp_named_operator2name is marked as being 32-bytes long, but it in
  328. * fact is much larger than that, so we seem to need a symbols__find()
  329. * routine that looks for >= current->start and < next_symbol->start,
  330. * possibly just for C++ objects?
  331. *
  332. * For now lets just make some progress by marking jumps to outside the
  333. * current function as call like.
  334. *
  335. * Actual navigation will come next, with further understanding of how
  336. * the symbol searching and disassembly should be done.
  337. */
  338. if (maps__find_ams(ms->maps, &target) == 0 &&
  339. map__rip_2objdump(target.ms.map, map->map_ip(target.ms.map, target.addr)) == ops->target.addr)
  340. ops->target.sym = target.ms.sym;
  341. if (!ops->target.outside) {
  342. ops->target.offset = target.addr - start;
  343. ops->target.offset_avail = true;
  344. } else {
  345. ops->target.offset_avail = false;
  346. }
  347. return 0;
  348. }
  349. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  350. struct ins_operands *ops, int max_ins_name)
  351. {
  352. const char *c;
  353. if (!ops->target.addr || ops->target.offset < 0)
  354. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  355. if (ops->target.outside && ops->target.sym != NULL)
  356. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name, ops->target.sym->name);
  357. c = strchr(ops->raw, ',');
  358. c = validate_comma(c, ops);
  359. if (c != NULL) {
  360. const char *c2 = strchr(c + 1, ',');
  361. c2 = validate_comma(c2, ops);
  362. /* check for 3-op insn */
  363. if (c2 != NULL)
  364. c = c2;
  365. c++;
  366. /* mirror arch objdump's space-after-comma style */
  367. if (*c == ' ')
  368. c++;
  369. }
  370. return scnprintf(bf, size, "%-*s %.*s%" PRIx64, max_ins_name,
  371. ins->name, c ? c - ops->raw : 0, ops->raw,
  372. ops->target.offset);
  373. }
  374. static struct ins_ops jump_ops = {
  375. .parse = jump__parse,
  376. .scnprintf = jump__scnprintf,
  377. };
  378. bool ins__is_jump(const struct ins *ins)
  379. {
  380. return ins->ops == &jump_ops;
  381. }
  382. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  383. {
  384. char *endptr, *name, *t;
  385. if (strstr(raw, "(%rip)") == NULL)
  386. return 0;
  387. *addrp = strtoull(comment, &endptr, 16);
  388. if (endptr == comment)
  389. return 0;
  390. name = strchr(endptr, '<');
  391. if (name == NULL)
  392. return -1;
  393. name++;
  394. t = strchr(name, '>');
  395. if (t == NULL)
  396. return 0;
  397. *t = '\0';
  398. *namep = strdup(name);
  399. *t = '>';
  400. return 0;
  401. }
  402. static int lock__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms)
  403. {
  404. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  405. if (ops->locked.ops == NULL)
  406. return 0;
  407. if (disasm_line__parse(ops->raw, &ops->locked.ins.name, &ops->locked.ops->raw) < 0)
  408. goto out_free_ops;
  409. ops->locked.ins.ops = ins__find(arch, ops->locked.ins.name);
  410. if (ops->locked.ins.ops == NULL)
  411. goto out_free_ops;
  412. if (ops->locked.ins.ops->parse &&
  413. ops->locked.ins.ops->parse(arch, ops->locked.ops, ms) < 0)
  414. goto out_free_ops;
  415. return 0;
  416. out_free_ops:
  417. zfree(&ops->locked.ops);
  418. return 0;
  419. }
  420. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  421. struct ins_operands *ops, int max_ins_name)
  422. {
  423. int printed;
  424. if (ops->locked.ins.ops == NULL)
  425. return ins__raw_scnprintf(ins, bf, size, ops, max_ins_name);
  426. printed = scnprintf(bf, size, "%-*s ", max_ins_name, ins->name);
  427. return printed + ins__scnprintf(&ops->locked.ins, bf + printed,
  428. size - printed, ops->locked.ops, max_ins_name);
  429. }
  430. static void lock__delete(struct ins_operands *ops)
  431. {
  432. struct ins *ins = &ops->locked.ins;
  433. if (ins->ops && ins->ops->free)
  434. ins->ops->free(ops->locked.ops);
  435. else
  436. ins__delete(ops->locked.ops);
  437. zfree(&ops->locked.ops);
  438. zfree(&ops->target.raw);
  439. zfree(&ops->target.name);
  440. }
  441. static struct ins_ops lock_ops = {
  442. .free = lock__delete,
  443. .parse = lock__parse,
  444. .scnprintf = lock__scnprintf,
  445. };
  446. static int mov__parse(struct arch *arch, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  447. {
  448. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  449. if (s == NULL)
  450. return -1;
  451. *s = '\0';
  452. ops->source.raw = strdup(ops->raw);
  453. *s = ',';
  454. if (ops->source.raw == NULL)
  455. return -1;
  456. target = ++s;
  457. comment = strchr(s, arch->objdump.comment_char);
  458. if (comment != NULL)
  459. s = comment - 1;
  460. else
  461. s = strchr(s, '\0') - 1;
  462. while (s > target && isspace(s[0]))
  463. --s;
  464. s++;
  465. prev = *s;
  466. *s = '\0';
  467. ops->target.raw = strdup(target);
  468. *s = prev;
  469. if (ops->target.raw == NULL)
  470. goto out_free_source;
  471. if (comment == NULL)
  472. return 0;
  473. comment = skip_spaces(comment);
  474. comment__symbol(ops->source.raw, comment + 1, &ops->source.addr, &ops->source.name);
  475. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  476. return 0;
  477. out_free_source:
  478. zfree(&ops->source.raw);
  479. return -1;
  480. }
  481. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  482. struct ins_operands *ops, int max_ins_name)
  483. {
  484. return scnprintf(bf, size, "%-*s %s,%s", max_ins_name, ins->name,
  485. ops->source.name ?: ops->source.raw,
  486. ops->target.name ?: ops->target.raw);
  487. }
  488. static struct ins_ops mov_ops = {
  489. .parse = mov__parse,
  490. .scnprintf = mov__scnprintf,
  491. };
  492. static int dec__parse(struct arch *arch __maybe_unused, struct ins_operands *ops, struct map_symbol *ms __maybe_unused)
  493. {
  494. char *target, *comment, *s, prev;
  495. target = s = ops->raw;
  496. while (s[0] != '\0' && !isspace(s[0]))
  497. ++s;
  498. prev = *s;
  499. *s = '\0';
  500. ops->target.raw = strdup(target);
  501. *s = prev;
  502. if (ops->target.raw == NULL)
  503. return -1;
  504. comment = strchr(s, arch->objdump.comment_char);
  505. if (comment == NULL)
  506. return 0;
  507. comment = skip_spaces(comment);
  508. comment__symbol(ops->target.raw, comment + 1, &ops->target.addr, &ops->target.name);
  509. return 0;
  510. }
  511. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  512. struct ins_operands *ops, int max_ins_name)
  513. {
  514. return scnprintf(bf, size, "%-*s %s", max_ins_name, ins->name,
  515. ops->target.name ?: ops->target.raw);
  516. }
  517. static struct ins_ops dec_ops = {
  518. .parse = dec__parse,
  519. .scnprintf = dec__scnprintf,
  520. };
  521. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  522. struct ins_operands *ops __maybe_unused, int max_ins_name)
  523. {
  524. return scnprintf(bf, size, "%-*s", max_ins_name, "nop");
  525. }
  526. static struct ins_ops nop_ops = {
  527. .scnprintf = nop__scnprintf,
  528. };
  529. static struct ins_ops ret_ops = {
  530. .scnprintf = ins__raw_scnprintf,
  531. };
  532. bool ins__is_ret(const struct ins *ins)
  533. {
  534. return ins->ops == &ret_ops;
  535. }
  536. bool ins__is_lock(const struct ins *ins)
  537. {
  538. return ins->ops == &lock_ops;
  539. }
  540. static int ins__key_cmp(const void *name, const void *insp)
  541. {
  542. const struct ins *ins = insp;
  543. return strcmp(name, ins->name);
  544. }
  545. static int ins__cmp(const void *a, const void *b)
  546. {
  547. const struct ins *ia = a;
  548. const struct ins *ib = b;
  549. return strcmp(ia->name, ib->name);
  550. }
  551. static void ins__sort(struct arch *arch)
  552. {
  553. const int nmemb = arch->nr_instructions;
  554. qsort(arch->instructions, nmemb, sizeof(struct ins), ins__cmp);
  555. }
  556. static struct ins_ops *__ins__find(struct arch *arch, const char *name)
  557. {
  558. struct ins *ins;
  559. const int nmemb = arch->nr_instructions;
  560. if (!arch->sorted_instructions) {
  561. ins__sort(arch);
  562. arch->sorted_instructions = true;
  563. }
  564. ins = bsearch(name, arch->instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  565. return ins ? ins->ops : NULL;
  566. }
  567. static struct ins_ops *ins__find(struct arch *arch, const char *name)
  568. {
  569. struct ins_ops *ops = __ins__find(arch, name);
  570. if (!ops && arch->associate_instruction_ops)
  571. ops = arch->associate_instruction_ops(arch, name);
  572. return ops;
  573. }
  574. static int arch__key_cmp(const void *name, const void *archp)
  575. {
  576. const struct arch *arch = archp;
  577. return strcmp(name, arch->name);
  578. }
  579. static int arch__cmp(const void *a, const void *b)
  580. {
  581. const struct arch *aa = a;
  582. const struct arch *ab = b;
  583. return strcmp(aa->name, ab->name);
  584. }
  585. static void arch__sort(void)
  586. {
  587. const int nmemb = ARRAY_SIZE(architectures);
  588. qsort(architectures, nmemb, sizeof(struct arch), arch__cmp);
  589. }
  590. static struct arch *arch__find(const char *name)
  591. {
  592. const int nmemb = ARRAY_SIZE(architectures);
  593. static bool sorted;
  594. if (!sorted) {
  595. arch__sort();
  596. sorted = true;
  597. }
  598. return bsearch(name, architectures, nmemb, sizeof(struct arch), arch__key_cmp);
  599. }
  600. static struct annotated_source *annotated_source__new(void)
  601. {
  602. struct annotated_source *src = zalloc(sizeof(*src));
  603. if (src != NULL)
  604. INIT_LIST_HEAD(&src->source);
  605. return src;
  606. }
  607. static __maybe_unused void annotated_source__delete(struct annotated_source *src)
  608. {
  609. if (src == NULL)
  610. return;
  611. zfree(&src->histograms);
  612. zfree(&src->cycles_hist);
  613. free(src);
  614. }
  615. static int annotated_source__alloc_histograms(struct annotated_source *src,
  616. size_t size, int nr_hists)
  617. {
  618. size_t sizeof_sym_hist;
  619. /*
  620. * Add buffer of one element for zero length symbol.
  621. * When sample is taken from first instruction of
  622. * zero length symbol, perf still resolves it and
  623. * shows symbol name in perf report and allows to
  624. * annotate it.
  625. */
  626. if (size == 0)
  627. size = 1;
  628. /* Check for overflow when calculating sizeof_sym_hist */
  629. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(struct sym_hist_entry))
  630. return -1;
  631. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(struct sym_hist_entry));
  632. /* Check for overflow in zalloc argument */
  633. if (sizeof_sym_hist > SIZE_MAX / nr_hists)
  634. return -1;
  635. src->sizeof_sym_hist = sizeof_sym_hist;
  636. src->nr_histograms = nr_hists;
  637. src->histograms = calloc(nr_hists, sizeof_sym_hist) ;
  638. return src->histograms ? 0 : -1;
  639. }
  640. /* The cycles histogram is lazily allocated. */
  641. static int symbol__alloc_hist_cycles(struct symbol *sym)
  642. {
  643. struct annotation *notes = symbol__annotation(sym);
  644. const size_t size = symbol__size(sym);
  645. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  646. if (notes->src->cycles_hist == NULL)
  647. return -1;
  648. return 0;
  649. }
  650. void symbol__annotate_zero_histograms(struct symbol *sym)
  651. {
  652. struct annotation *notes = symbol__annotation(sym);
  653. pthread_mutex_lock(&notes->lock);
  654. if (notes->src != NULL) {
  655. memset(notes->src->histograms, 0,
  656. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  657. if (notes->src->cycles_hist)
  658. memset(notes->src->cycles_hist, 0,
  659. symbol__size(sym) * sizeof(struct cyc_hist));
  660. }
  661. pthread_mutex_unlock(&notes->lock);
  662. }
  663. static int __symbol__account_cycles(struct cyc_hist *ch,
  664. u64 start,
  665. unsigned offset, unsigned cycles,
  666. unsigned have_start)
  667. {
  668. /*
  669. * For now we can only account one basic block per
  670. * final jump. But multiple could be overlapping.
  671. * Always account the longest one. So when
  672. * a shorter one has been already seen throw it away.
  673. *
  674. * We separately always account the full cycles.
  675. */
  676. ch[offset].num_aggr++;
  677. ch[offset].cycles_aggr += cycles;
  678. if (cycles > ch[offset].cycles_max)
  679. ch[offset].cycles_max = cycles;
  680. if (ch[offset].cycles_min) {
  681. if (cycles && cycles < ch[offset].cycles_min)
  682. ch[offset].cycles_min = cycles;
  683. } else
  684. ch[offset].cycles_min = cycles;
  685. if (!have_start && ch[offset].have_start)
  686. return 0;
  687. if (ch[offset].num) {
  688. if (have_start && (!ch[offset].have_start ||
  689. ch[offset].start > start)) {
  690. ch[offset].have_start = 0;
  691. ch[offset].cycles = 0;
  692. ch[offset].num = 0;
  693. if (ch[offset].reset < 0xffff)
  694. ch[offset].reset++;
  695. } else if (have_start &&
  696. ch[offset].start < start)
  697. return 0;
  698. }
  699. if (ch[offset].num < NUM_SPARKS)
  700. ch[offset].cycles_spark[ch[offset].num] = cycles;
  701. ch[offset].have_start = have_start;
  702. ch[offset].start = start;
  703. ch[offset].cycles += cycles;
  704. ch[offset].num++;
  705. return 0;
  706. }
  707. static int __symbol__inc_addr_samples(struct map_symbol *ms,
  708. struct annotated_source *src, int evidx, u64 addr,
  709. struct perf_sample *sample)
  710. {
  711. struct symbol *sym = ms->sym;
  712. unsigned offset;
  713. struct sym_hist *h;
  714. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, ms->map->unmap_ip(ms->map, addr));
  715. if ((addr < sym->start || addr >= sym->end) &&
  716. (addr != sym->end || sym->start != sym->end)) {
  717. pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
  718. __func__, __LINE__, sym->name, sym->start, addr, sym->end);
  719. return -ERANGE;
  720. }
  721. offset = addr - sym->start;
  722. h = annotated_source__histogram(src, evidx);
  723. if (h == NULL) {
  724. pr_debug("%s(%d): ENOMEM! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 ", func: %d\n",
  725. __func__, __LINE__, sym->name, sym->start, addr, sym->end, sym->type == STT_FUNC);
  726. return -ENOMEM;
  727. }
  728. h->nr_samples++;
  729. h->addr[offset].nr_samples++;
  730. h->period += sample->period;
  731. h->addr[offset].period += sample->period;
  732. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  733. ", evidx=%d] => nr_samples: %" PRIu64 ", period: %" PRIu64 "\n",
  734. sym->start, sym->name, addr, addr - sym->start, evidx,
  735. h->addr[offset].nr_samples, h->addr[offset].period);
  736. return 0;
  737. }
  738. static struct cyc_hist *symbol__cycles_hist(struct symbol *sym)
  739. {
  740. struct annotation *notes = symbol__annotation(sym);
  741. if (notes->src == NULL) {
  742. notes->src = annotated_source__new();
  743. if (notes->src == NULL)
  744. return NULL;
  745. goto alloc_cycles_hist;
  746. }
  747. if (!notes->src->cycles_hist) {
  748. alloc_cycles_hist:
  749. symbol__alloc_hist_cycles(sym);
  750. }
  751. return notes->src->cycles_hist;
  752. }
  753. struct annotated_source *symbol__hists(struct symbol *sym, int nr_hists)
  754. {
  755. struct annotation *notes = symbol__annotation(sym);
  756. if (notes->src == NULL) {
  757. notes->src = annotated_source__new();
  758. if (notes->src == NULL)
  759. return NULL;
  760. goto alloc_histograms;
  761. }
  762. if (notes->src->histograms == NULL) {
  763. alloc_histograms:
  764. annotated_source__alloc_histograms(notes->src, symbol__size(sym),
  765. nr_hists);
  766. }
  767. return notes->src;
  768. }
  769. static int symbol__inc_addr_samples(struct map_symbol *ms,
  770. struct evsel *evsel, u64 addr,
  771. struct perf_sample *sample)
  772. {
  773. struct symbol *sym = ms->sym;
  774. struct annotated_source *src;
  775. if (sym == NULL)
  776. return 0;
  777. src = symbol__hists(sym, evsel->evlist->core.nr_entries);
  778. return src ? __symbol__inc_addr_samples(ms, src, evsel->idx, addr, sample) : 0;
  779. }
  780. static int symbol__account_cycles(u64 addr, u64 start,
  781. struct symbol *sym, unsigned cycles)
  782. {
  783. struct cyc_hist *cycles_hist;
  784. unsigned offset;
  785. if (sym == NULL)
  786. return 0;
  787. cycles_hist = symbol__cycles_hist(sym);
  788. if (cycles_hist == NULL)
  789. return -ENOMEM;
  790. if (addr < sym->start || addr >= sym->end)
  791. return -ERANGE;
  792. if (start) {
  793. if (start < sym->start || start >= sym->end)
  794. return -ERANGE;
  795. if (start >= addr)
  796. start = 0;
  797. }
  798. offset = addr - sym->start;
  799. return __symbol__account_cycles(cycles_hist,
  800. start ? start - sym->start : 0,
  801. offset, cycles,
  802. !!start);
  803. }
  804. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  805. struct addr_map_symbol *start,
  806. unsigned cycles)
  807. {
  808. u64 saddr = 0;
  809. int err;
  810. if (!cycles)
  811. return 0;
  812. /*
  813. * Only set start when IPC can be computed. We can only
  814. * compute it when the basic block is completely in a single
  815. * function.
  816. * Special case the case when the jump is elsewhere, but
  817. * it starts on the function start.
  818. */
  819. if (start &&
  820. (start->ms.sym == ams->ms.sym ||
  821. (ams->ms.sym &&
  822. start->addr == ams->ms.sym->start + ams->ms.map->start)))
  823. saddr = start->al_addr;
  824. if (saddr == 0)
  825. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  826. ams->addr,
  827. start ? start->addr : 0,
  828. ams->ms.sym ? ams->ms.sym->start + ams->ms.map->start : 0,
  829. saddr);
  830. err = symbol__account_cycles(ams->al_addr, saddr, ams->ms.sym, cycles);
  831. if (err)
  832. pr_debug2("account_cycles failed %d\n", err);
  833. return err;
  834. }
  835. static unsigned annotation__count_insn(struct annotation *notes, u64 start, u64 end)
  836. {
  837. unsigned n_insn = 0;
  838. u64 offset;
  839. for (offset = start; offset <= end; offset++) {
  840. if (notes->offsets[offset])
  841. n_insn++;
  842. }
  843. return n_insn;
  844. }
  845. static void annotation__count_and_fill(struct annotation *notes, u64 start, u64 end, struct cyc_hist *ch)
  846. {
  847. unsigned n_insn;
  848. unsigned int cover_insn = 0;
  849. u64 offset;
  850. n_insn = annotation__count_insn(notes, start, end);
  851. if (n_insn && ch->num && ch->cycles) {
  852. float ipc = n_insn / ((double)ch->cycles / (double)ch->num);
  853. /* Hide data when there are too many overlaps. */
  854. if (ch->reset >= 0x7fff)
  855. return;
  856. for (offset = start; offset <= end; offset++) {
  857. struct annotation_line *al = notes->offsets[offset];
  858. if (al && al->ipc == 0.0) {
  859. al->ipc = ipc;
  860. cover_insn++;
  861. }
  862. }
  863. if (cover_insn) {
  864. notes->hit_cycles += ch->cycles;
  865. notes->hit_insn += n_insn * ch->num;
  866. notes->cover_insn += cover_insn;
  867. }
  868. }
  869. }
  870. void annotation__compute_ipc(struct annotation *notes, size_t size)
  871. {
  872. s64 offset;
  873. if (!notes->src || !notes->src->cycles_hist)
  874. return;
  875. notes->total_insn = annotation__count_insn(notes, 0, size - 1);
  876. notes->hit_cycles = 0;
  877. notes->hit_insn = 0;
  878. notes->cover_insn = 0;
  879. pthread_mutex_lock(&notes->lock);
  880. for (offset = size - 1; offset >= 0; --offset) {
  881. struct cyc_hist *ch;
  882. ch = &notes->src->cycles_hist[offset];
  883. if (ch && ch->cycles) {
  884. struct annotation_line *al;
  885. if (ch->have_start)
  886. annotation__count_and_fill(notes, ch->start, offset, ch);
  887. al = notes->offsets[offset];
  888. if (al && ch->num_aggr) {
  889. al->cycles = ch->cycles_aggr / ch->num_aggr;
  890. al->cycles_max = ch->cycles_max;
  891. al->cycles_min = ch->cycles_min;
  892. }
  893. notes->have_cycles = true;
  894. }
  895. }
  896. pthread_mutex_unlock(&notes->lock);
  897. }
  898. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, struct perf_sample *sample,
  899. struct evsel *evsel)
  900. {
  901. return symbol__inc_addr_samples(&ams->ms, evsel, ams->al_addr, sample);
  902. }
  903. int hist_entry__inc_addr_samples(struct hist_entry *he, struct perf_sample *sample,
  904. struct evsel *evsel, u64 ip)
  905. {
  906. return symbol__inc_addr_samples(&he->ms, evsel, ip, sample);
  907. }
  908. static void disasm_line__init_ins(struct disasm_line *dl, struct arch *arch, struct map_symbol *ms)
  909. {
  910. dl->ins.ops = ins__find(arch, dl->ins.name);
  911. if (!dl->ins.ops)
  912. return;
  913. if (dl->ins.ops->parse && dl->ins.ops->parse(arch, &dl->ops, ms) < 0)
  914. dl->ins.ops = NULL;
  915. }
  916. static int disasm_line__parse(char *line, const char **namep, char **rawp)
  917. {
  918. char tmp, *name = skip_spaces(line);
  919. if (name[0] == '\0')
  920. return -1;
  921. *rawp = name + 1;
  922. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  923. ++*rawp;
  924. tmp = (*rawp)[0];
  925. (*rawp)[0] = '\0';
  926. *namep = strdup(name);
  927. if (*namep == NULL)
  928. goto out;
  929. (*rawp)[0] = tmp;
  930. *rawp = strim(*rawp);
  931. return 0;
  932. out:
  933. return -1;
  934. }
  935. struct annotate_args {
  936. struct arch *arch;
  937. struct map_symbol ms;
  938. struct evsel *evsel;
  939. struct annotation_options *options;
  940. s64 offset;
  941. char *line;
  942. int line_nr;
  943. };
  944. static void annotation_line__init(struct annotation_line *al,
  945. struct annotate_args *args,
  946. int nr)
  947. {
  948. al->offset = args->offset;
  949. al->line = strdup(args->line);
  950. al->line_nr = args->line_nr;
  951. al->data_nr = nr;
  952. }
  953. static void annotation_line__exit(struct annotation_line *al)
  954. {
  955. free_srcline(al->path);
  956. zfree(&al->line);
  957. }
  958. static size_t disasm_line_size(int nr)
  959. {
  960. struct annotation_line *al;
  961. return (sizeof(struct disasm_line) + (sizeof(al->data[0]) * nr));
  962. }
  963. /*
  964. * Allocating the disasm annotation line data with
  965. * following structure:
  966. *
  967. * -------------------------------------------
  968. * struct disasm_line | struct annotation_line
  969. * -------------------------------------------
  970. *
  971. * We have 'struct annotation_line' member as last member
  972. * of 'struct disasm_line' to have an easy access.
  973. */
  974. static struct disasm_line *disasm_line__new(struct annotate_args *args)
  975. {
  976. struct disasm_line *dl = NULL;
  977. int nr = 1;
  978. if (perf_evsel__is_group_event(args->evsel))
  979. nr = args->evsel->core.nr_members;
  980. dl = zalloc(disasm_line_size(nr));
  981. if (!dl)
  982. return NULL;
  983. annotation_line__init(&dl->al, args, nr);
  984. if (dl->al.line == NULL)
  985. goto out_delete;
  986. if (args->offset != -1) {
  987. if (disasm_line__parse(dl->al.line, &dl->ins.name, &dl->ops.raw) < 0)
  988. goto out_free_line;
  989. disasm_line__init_ins(dl, args->arch, &args->ms);
  990. }
  991. return dl;
  992. out_free_line:
  993. zfree(&dl->al.line);
  994. out_delete:
  995. free(dl);
  996. return NULL;
  997. }
  998. void disasm_line__free(struct disasm_line *dl)
  999. {
  1000. if (dl->ins.ops && dl->ins.ops->free)
  1001. dl->ins.ops->free(&dl->ops);
  1002. else
  1003. ins__delete(&dl->ops);
  1004. zfree(&dl->ins.name);
  1005. annotation_line__exit(&dl->al);
  1006. free(dl);
  1007. }
  1008. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw, int max_ins_name)
  1009. {
  1010. if (raw || !dl->ins.ops)
  1011. return scnprintf(bf, size, "%-*s %s", max_ins_name, dl->ins.name, dl->ops.raw);
  1012. return ins__scnprintf(&dl->ins, bf, size, &dl->ops, max_ins_name);
  1013. }
  1014. static void annotation_line__add(struct annotation_line *al, struct list_head *head)
  1015. {
  1016. list_add_tail(&al->node, head);
  1017. }
  1018. struct annotation_line *
  1019. annotation_line__next(struct annotation_line *pos, struct list_head *head)
  1020. {
  1021. list_for_each_entry_continue(pos, head, node)
  1022. if (pos->offset >= 0)
  1023. return pos;
  1024. return NULL;
  1025. }
  1026. static const char *annotate__address_color(struct block_range *br)
  1027. {
  1028. double cov = block_range__coverage(br);
  1029. if (cov >= 0) {
  1030. /* mark red for >75% coverage */
  1031. if (cov > 0.75)
  1032. return PERF_COLOR_RED;
  1033. /* mark dull for <1% coverage */
  1034. if (cov < 0.01)
  1035. return PERF_COLOR_NORMAL;
  1036. }
  1037. return PERF_COLOR_MAGENTA;
  1038. }
  1039. static const char *annotate__asm_color(struct block_range *br)
  1040. {
  1041. double cov = block_range__coverage(br);
  1042. if (cov >= 0) {
  1043. /* mark dull for <1% coverage */
  1044. if (cov < 0.01)
  1045. return PERF_COLOR_NORMAL;
  1046. }
  1047. return PERF_COLOR_BLUE;
  1048. }
  1049. static void annotate__branch_printf(struct block_range *br, u64 addr)
  1050. {
  1051. bool emit_comment = true;
  1052. if (!br)
  1053. return;
  1054. #if 1
  1055. if (br->is_target && br->start == addr) {
  1056. struct block_range *branch = br;
  1057. double p;
  1058. /*
  1059. * Find matching branch to our target.
  1060. */
  1061. while (!branch->is_branch)
  1062. branch = block_range__next(branch);
  1063. p = 100 *(double)br->entry / branch->coverage;
  1064. if (p > 0.1) {
  1065. if (emit_comment) {
  1066. emit_comment = false;
  1067. printf("\t#");
  1068. }
  1069. /*
  1070. * The percentage of coverage joined at this target in relation
  1071. * to the next branch.
  1072. */
  1073. printf(" +%.2f%%", p);
  1074. }
  1075. }
  1076. #endif
  1077. if (br->is_branch && br->end == addr) {
  1078. double p = 100*(double)br->taken / br->coverage;
  1079. if (p > 0.1) {
  1080. if (emit_comment) {
  1081. emit_comment = false;
  1082. printf("\t#");
  1083. }
  1084. /*
  1085. * The percentage of coverage leaving at this branch, and
  1086. * its prediction ratio.
  1087. */
  1088. printf(" -%.2f%% (p:%.2f%%)", p, 100*(double)br->pred / br->taken);
  1089. }
  1090. }
  1091. }
  1092. static int disasm_line__print(struct disasm_line *dl, u64 start, int addr_fmt_width)
  1093. {
  1094. s64 offset = dl->al.offset;
  1095. const u64 addr = start + offset;
  1096. struct block_range *br;
  1097. br = block_range__find(addr);
  1098. color_fprintf(stdout, annotate__address_color(br), " %*" PRIx64 ":", addr_fmt_width, addr);
  1099. color_fprintf(stdout, annotate__asm_color(br), "%s", dl->al.line);
  1100. annotate__branch_printf(br, addr);
  1101. return 0;
  1102. }
  1103. static int
  1104. annotation_line__print(struct annotation_line *al, struct symbol *sym, u64 start,
  1105. struct evsel *evsel, u64 len, int min_pcnt, int printed,
  1106. int max_lines, struct annotation_line *queue, int addr_fmt_width,
  1107. int percent_type)
  1108. {
  1109. struct disasm_line *dl = container_of(al, struct disasm_line, al);
  1110. static const char *prev_line;
  1111. static const char *prev_color;
  1112. if (al->offset != -1) {
  1113. double max_percent = 0.0;
  1114. int i, nr_percent = 1;
  1115. const char *color;
  1116. struct annotation *notes = symbol__annotation(sym);
  1117. for (i = 0; i < al->data_nr; i++) {
  1118. double percent;
  1119. percent = annotation_data__percent(&al->data[i],
  1120. percent_type);
  1121. if (percent > max_percent)
  1122. max_percent = percent;
  1123. }
  1124. if (al->data_nr > nr_percent)
  1125. nr_percent = al->data_nr;
  1126. if (max_percent < min_pcnt)
  1127. return -1;
  1128. if (max_lines && printed >= max_lines)
  1129. return 1;
  1130. if (queue != NULL) {
  1131. list_for_each_entry_from(queue, &notes->src->source, node) {
  1132. if (queue == al)
  1133. break;
  1134. annotation_line__print(queue, sym, start, evsel, len,
  1135. 0, 0, 1, NULL, addr_fmt_width,
  1136. percent_type);
  1137. }
  1138. }
  1139. color = get_percent_color(max_percent);
  1140. /*
  1141. * Also color the filename and line if needed, with
  1142. * the same color than the percentage. Don't print it
  1143. * twice for close colored addr with the same filename:line
  1144. */
  1145. if (al->path) {
  1146. if (!prev_line || strcmp(prev_line, al->path)
  1147. || color != prev_color) {
  1148. color_fprintf(stdout, color, " %s", al->path);
  1149. prev_line = al->path;
  1150. prev_color = color;
  1151. }
  1152. }
  1153. for (i = 0; i < nr_percent; i++) {
  1154. struct annotation_data *data = &al->data[i];
  1155. double percent;
  1156. percent = annotation_data__percent(data, percent_type);
  1157. color = get_percent_color(percent);
  1158. if (symbol_conf.show_total_period)
  1159. color_fprintf(stdout, color, " %11" PRIu64,
  1160. data->he.period);
  1161. else if (symbol_conf.show_nr_samples)
  1162. color_fprintf(stdout, color, " %7" PRIu64,
  1163. data->he.nr_samples);
  1164. else
  1165. color_fprintf(stdout, color, " %7.2f", percent);
  1166. }
  1167. printf(" : ");
  1168. disasm_line__print(dl, start, addr_fmt_width);
  1169. printf("\n");
  1170. } else if (max_lines && printed >= max_lines)
  1171. return 1;
  1172. else {
  1173. int width = symbol_conf.show_total_period ? 12 : 8;
  1174. if (queue)
  1175. return -1;
  1176. if (perf_evsel__is_group_event(evsel))
  1177. width *= evsel->core.nr_members;
  1178. if (!*al->line)
  1179. printf(" %*s:\n", width, " ");
  1180. else
  1181. printf(" %*s: %*s %s\n", width, " ", addr_fmt_width, " ", al->line);
  1182. }
  1183. return 0;
  1184. }
  1185. /*
  1186. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  1187. * which looks like following
  1188. *
  1189. * 0000000000415500 <_init>:
  1190. * 415500: sub $0x8,%rsp
  1191. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  1192. * 41550b: test %rax,%rax
  1193. * 41550e: je 415515 <_init+0x15>
  1194. * 415510: callq 416e70 <__gmon_start__@plt>
  1195. * 415515: add $0x8,%rsp
  1196. * 415519: retq
  1197. *
  1198. * it will be parsed and saved into struct disasm_line as
  1199. * <offset> <name> <ops.raw>
  1200. *
  1201. * The offset will be a relative offset from the start of the symbol and -1
  1202. * means that it's not a disassembly line so should be treated differently.
  1203. * The ops.raw part will be parsed further according to type of the instruction.
  1204. */
  1205. static int symbol__parse_objdump_line(struct symbol *sym,
  1206. struct annotate_args *args,
  1207. char *parsed_line, int *line_nr)
  1208. {
  1209. struct map *map = args->ms.map;
  1210. struct annotation *notes = symbol__annotation(sym);
  1211. struct disasm_line *dl;
  1212. char *tmp;
  1213. s64 line_ip, offset = -1;
  1214. regmatch_t match[2];
  1215. /* /filename:linenr ? Save line number and ignore. */
  1216. if (regexec(&file_lineno, parsed_line, 2, match, 0) == 0) {
  1217. *line_nr = atoi(parsed_line + match[1].rm_so);
  1218. return 0;
  1219. }
  1220. /* Process hex address followed by ':'. */
  1221. line_ip = strtoull(parsed_line, &tmp, 16);
  1222. if (parsed_line != tmp && tmp[0] == ':' && tmp[1] != '\0') {
  1223. u64 start = map__rip_2objdump(map, sym->start),
  1224. end = map__rip_2objdump(map, sym->end);
  1225. offset = line_ip - start;
  1226. if ((u64)line_ip < start || (u64)line_ip >= end)
  1227. offset = -1;
  1228. else
  1229. parsed_line = tmp + 1;
  1230. }
  1231. args->offset = offset;
  1232. args->line = parsed_line;
  1233. args->line_nr = *line_nr;
  1234. args->ms.sym = sym;
  1235. dl = disasm_line__new(args);
  1236. (*line_nr)++;
  1237. if (dl == NULL)
  1238. return -1;
  1239. if (!disasm_line__has_local_offset(dl)) {
  1240. dl->ops.target.offset = dl->ops.target.addr -
  1241. map__rip_2objdump(map, sym->start);
  1242. dl->ops.target.offset_avail = true;
  1243. }
  1244. /* kcore has no symbols, so add the call target symbol */
  1245. if (dl->ins.ops && ins__is_call(&dl->ins) && !dl->ops.target.sym) {
  1246. struct addr_map_symbol target = {
  1247. .addr = dl->ops.target.addr,
  1248. .ms = { .map = map, },
  1249. };
  1250. if (!maps__find_ams(args->ms.maps, &target) &&
  1251. target.ms.sym->start == target.al_addr)
  1252. dl->ops.target.sym = target.ms.sym;
  1253. }
  1254. annotation_line__add(&dl->al, &notes->src->source);
  1255. return 0;
  1256. }
  1257. static __attribute__((constructor)) void symbol__init_regexpr(void)
  1258. {
  1259. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  1260. }
  1261. static void delete_last_nop(struct symbol *sym)
  1262. {
  1263. struct annotation *notes = symbol__annotation(sym);
  1264. struct list_head *list = &notes->src->source;
  1265. struct disasm_line *dl;
  1266. while (!list_empty(list)) {
  1267. dl = list_entry(list->prev, struct disasm_line, al.node);
  1268. if (dl->ins.ops) {
  1269. if (dl->ins.ops != &nop_ops)
  1270. return;
  1271. } else {
  1272. if (!strstr(dl->al.line, " nop ") &&
  1273. !strstr(dl->al.line, " nopl ") &&
  1274. !strstr(dl->al.line, " nopw "))
  1275. return;
  1276. }
  1277. list_del_init(&dl->al.node);
  1278. disasm_line__free(dl);
  1279. }
  1280. }
  1281. int symbol__strerror_disassemble(struct map_symbol *ms, int errnum, char *buf, size_t buflen)
  1282. {
  1283. struct dso *dso = ms->map->dso;
  1284. BUG_ON(buflen == 0);
  1285. if (errnum >= 0) {
  1286. str_error_r(errnum, buf, buflen);
  1287. return 0;
  1288. }
  1289. switch (errnum) {
  1290. case SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX: {
  1291. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  1292. char *build_id_msg = NULL;
  1293. if (dso->has_build_id) {
  1294. build_id__sprintf(dso->build_id,
  1295. sizeof(dso->build_id), bf + 15);
  1296. build_id_msg = bf;
  1297. }
  1298. scnprintf(buf, buflen,
  1299. "No vmlinux file%s\nwas found in the path.\n\n"
  1300. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  1301. "Please use:\n\n"
  1302. " perf buildid-cache -vu vmlinux\n\n"
  1303. "or:\n\n"
  1304. " --vmlinux vmlinux\n", build_id_msg ?: "");
  1305. }
  1306. break;
  1307. case SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF:
  1308. scnprintf(buf, buflen, "Please link with binutils's libopcode to enable BPF annotation");
  1309. break;
  1310. case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_REGEXP:
  1311. scnprintf(buf, buflen, "Problems with arch specific instruction name regular expressions.");
  1312. break;
  1313. case SYMBOL_ANNOTATE_ERRNO__ARCH_INIT_CPUID_PARSING:
  1314. scnprintf(buf, buflen, "Problems while parsing the CPUID in the arch specific initialization.");
  1315. break;
  1316. case SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE:
  1317. scnprintf(buf, buflen, "Invalid BPF file: %s.", dso->long_name);
  1318. break;
  1319. case SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF:
  1320. scnprintf(buf, buflen, "The %s BPF file has no BTF section, compile with -g or use pahole -J.",
  1321. dso->long_name);
  1322. break;
  1323. default:
  1324. scnprintf(buf, buflen, "Internal error: Invalid %d error code\n", errnum);
  1325. break;
  1326. }
  1327. return 0;
  1328. }
  1329. static int dso__disassemble_filename(struct dso *dso, char *filename, size_t filename_size)
  1330. {
  1331. char linkname[PATH_MAX];
  1332. char *build_id_filename;
  1333. char *build_id_path = NULL;
  1334. char *pos;
  1335. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  1336. !dso__is_kcore(dso))
  1337. return SYMBOL_ANNOTATE_ERRNO__NO_VMLINUX;
  1338. build_id_filename = dso__build_id_filename(dso, NULL, 0, false);
  1339. if (build_id_filename) {
  1340. __symbol__join_symfs(filename, filename_size, build_id_filename);
  1341. free(build_id_filename);
  1342. } else {
  1343. if (dso->has_build_id)
  1344. return ENOMEM;
  1345. goto fallback;
  1346. }
  1347. build_id_path = strdup(filename);
  1348. if (!build_id_path)
  1349. return ENOMEM;
  1350. /*
  1351. * old style build-id cache has name of XX/XXXXXXX.. while
  1352. * new style has XX/XXXXXXX../{elf,kallsyms,vdso}.
  1353. * extract the build-id part of dirname in the new style only.
  1354. */
  1355. pos = strrchr(build_id_path, '/');
  1356. if (pos && strlen(pos) < SBUILD_ID_SIZE - 2)
  1357. dirname(build_id_path);
  1358. if (dso__is_kcore(dso) ||
  1359. readlink(build_id_path, linkname, sizeof(linkname)) < 0 ||
  1360. strstr(linkname, DSO__NAME_KALLSYMS) ||
  1361. access(filename, R_OK)) {
  1362. fallback:
  1363. /*
  1364. * If we don't have build-ids or the build-id file isn't in the
  1365. * cache, or is just a kallsyms file, well, lets hope that this
  1366. * DSO is the same as when 'perf record' ran.
  1367. */
  1368. __symbol__join_symfs(filename, filename_size, dso->long_name);
  1369. }
  1370. free(build_id_path);
  1371. return 0;
  1372. }
  1373. #if defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
  1374. #define PACKAGE "perf"
  1375. #include <bfd.h>
  1376. #include <dis-asm.h>
  1377. static int symbol__disassemble_bpf(struct symbol *sym,
  1378. struct annotate_args *args)
  1379. {
  1380. struct annotation *notes = symbol__annotation(sym);
  1381. struct annotation_options *opts = args->options;
  1382. struct bpf_prog_info_linear *info_linear;
  1383. struct bpf_prog_linfo *prog_linfo = NULL;
  1384. struct bpf_prog_info_node *info_node;
  1385. int len = sym->end - sym->start;
  1386. disassembler_ftype disassemble;
  1387. struct map *map = args->ms.map;
  1388. struct disassemble_info info;
  1389. struct dso *dso = map->dso;
  1390. int pc = 0, count, sub_id;
  1391. struct btf *btf = NULL;
  1392. char tpath[PATH_MAX];
  1393. size_t buf_size;
  1394. int nr_skip = 0;
  1395. char *buf;
  1396. bfd *bfdf;
  1397. int ret;
  1398. FILE *s;
  1399. if (dso->binary_type != DSO_BINARY_TYPE__BPF_PROG_INFO)
  1400. return SYMBOL_ANNOTATE_ERRNO__BPF_INVALID_FILE;
  1401. pr_debug("%s: handling sym %s addr %" PRIx64 " len %" PRIx64 "\n", __func__,
  1402. sym->name, sym->start, sym->end - sym->start);
  1403. memset(tpath, 0, sizeof(tpath));
  1404. perf_exe(tpath, sizeof(tpath));
  1405. bfdf = bfd_openr(tpath, NULL);
  1406. assert(bfdf);
  1407. assert(bfd_check_format(bfdf, bfd_object));
  1408. s = open_memstream(&buf, &buf_size);
  1409. if (!s) {
  1410. ret = errno;
  1411. goto out;
  1412. }
  1413. init_disassemble_info(&info, s,
  1414. (fprintf_ftype) fprintf);
  1415. info.arch = bfd_get_arch(bfdf);
  1416. info.mach = bfd_get_mach(bfdf);
  1417. info_node = perf_env__find_bpf_prog_info(dso->bpf_prog.env,
  1418. dso->bpf_prog.id);
  1419. if (!info_node) {
  1420. ret = SYMBOL_ANNOTATE_ERRNO__BPF_MISSING_BTF;
  1421. goto out;
  1422. }
  1423. info_linear = info_node->info_linear;
  1424. sub_id = dso->bpf_prog.sub_id;
  1425. info.buffer = (void *)(uintptr_t)(info_linear->info.jited_prog_insns);
  1426. info.buffer_length = info_linear->info.jited_prog_len;
  1427. if (info_linear->info.nr_line_info)
  1428. prog_linfo = bpf_prog_linfo__new(&info_linear->info);
  1429. if (info_linear->info.btf_id) {
  1430. struct btf_node *node;
  1431. node = perf_env__find_btf(dso->bpf_prog.env,
  1432. info_linear->info.btf_id);
  1433. if (node)
  1434. btf = btf__new((__u8 *)(node->data),
  1435. node->data_size);
  1436. }
  1437. disassemble_init_for_target(&info);
  1438. #ifdef DISASM_FOUR_ARGS_SIGNATURE
  1439. disassemble = disassembler(info.arch,
  1440. bfd_big_endian(bfdf),
  1441. info.mach,
  1442. bfdf);
  1443. #else
  1444. disassemble = disassembler(bfdf);
  1445. #endif
  1446. assert(disassemble);
  1447. fflush(s);
  1448. do {
  1449. const struct bpf_line_info *linfo = NULL;
  1450. struct disasm_line *dl;
  1451. size_t prev_buf_size;
  1452. const char *srcline;
  1453. u64 addr;
  1454. addr = pc + ((u64 *)(uintptr_t)(info_linear->info.jited_ksyms))[sub_id];
  1455. count = disassemble(pc, &info);
  1456. if (prog_linfo)
  1457. linfo = bpf_prog_linfo__lfind_addr_func(prog_linfo,
  1458. addr, sub_id,
  1459. nr_skip);
  1460. if (linfo && btf) {
  1461. srcline = btf__name_by_offset(btf, linfo->line_off);
  1462. nr_skip++;
  1463. } else
  1464. srcline = NULL;
  1465. fprintf(s, "\n");
  1466. prev_buf_size = buf_size;
  1467. fflush(s);
  1468. if (!opts->hide_src_code && srcline) {
  1469. args->offset = -1;
  1470. args->line = strdup(srcline);
  1471. args->line_nr = 0;
  1472. args->ms.sym = sym;
  1473. dl = disasm_line__new(args);
  1474. if (dl) {
  1475. annotation_line__add(&dl->al,
  1476. &notes->src->source);
  1477. }
  1478. }
  1479. args->offset = pc;
  1480. args->line = buf + prev_buf_size;
  1481. args->line_nr = 0;
  1482. args->ms.sym = sym;
  1483. dl = disasm_line__new(args);
  1484. if (dl)
  1485. annotation_line__add(&dl->al, &notes->src->source);
  1486. pc += count;
  1487. } while (count > 0 && pc < len);
  1488. ret = 0;
  1489. out:
  1490. free(prog_linfo);
  1491. free(btf);
  1492. fclose(s);
  1493. bfd_close(bfdf);
  1494. return ret;
  1495. }
  1496. #else // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
  1497. static int symbol__disassemble_bpf(struct symbol *sym __maybe_unused,
  1498. struct annotate_args *args __maybe_unused)
  1499. {
  1500. return SYMBOL_ANNOTATE_ERRNO__NO_LIBOPCODES_FOR_BPF;
  1501. }
  1502. #endif // defined(HAVE_LIBBFD_SUPPORT) && defined(HAVE_LIBBPF_SUPPORT)
  1503. /*
  1504. * Possibly create a new version of line with tabs expanded. Returns the
  1505. * existing or new line, storage is updated if a new line is allocated. If
  1506. * allocation fails then NULL is returned.
  1507. */
  1508. static char *expand_tabs(char *line, char **storage, size_t *storage_len)
  1509. {
  1510. size_t i, src, dst, len, new_storage_len, num_tabs;
  1511. char *new_line;
  1512. size_t line_len = strlen(line);
  1513. for (num_tabs = 0, i = 0; i < line_len; i++)
  1514. if (line[i] == '\t')
  1515. num_tabs++;
  1516. if (num_tabs == 0)
  1517. return line;
  1518. /*
  1519. * Space for the line and '\0', less the leading and trailing
  1520. * spaces. Each tab may introduce 7 additional spaces.
  1521. */
  1522. new_storage_len = line_len + 1 + (num_tabs * 7);
  1523. new_line = malloc(new_storage_len);
  1524. if (new_line == NULL) {
  1525. pr_err("Failure allocating memory for tab expansion\n");
  1526. return NULL;
  1527. }
  1528. /*
  1529. * Copy regions starting at src and expand tabs. If there are two
  1530. * adjacent tabs then 'src == i', the memcpy is of size 0 and the spaces
  1531. * are inserted.
  1532. */
  1533. for (i = 0, src = 0, dst = 0; i < line_len && num_tabs; i++) {
  1534. if (line[i] == '\t') {
  1535. len = i - src;
  1536. memcpy(&new_line[dst], &line[src], len);
  1537. dst += len;
  1538. new_line[dst++] = ' ';
  1539. while (dst % 8 != 0)
  1540. new_line[dst++] = ' ';
  1541. src = i + 1;
  1542. num_tabs--;
  1543. }
  1544. }
  1545. /* Expand the last region. */
  1546. len = line_len - src;
  1547. memcpy(&new_line[dst], &line[src], len);
  1548. dst += len;
  1549. new_line[dst] = '\0';
  1550. free(*storage);
  1551. *storage = new_line;
  1552. *storage_len = new_storage_len;
  1553. return new_line;
  1554. }
  1555. static int symbol__disassemble(struct symbol *sym, struct annotate_args *args)
  1556. {
  1557. struct annotation_options *opts = args->options;
  1558. struct map *map = args->ms.map;
  1559. struct dso *dso = map->dso;
  1560. char *command;
  1561. FILE *file;
  1562. char symfs_filename[PATH_MAX];
  1563. struct kcore_extract kce;
  1564. bool delete_extract = false;
  1565. bool decomp = false;
  1566. int lineno = 0;
  1567. int nline;
  1568. char *line;
  1569. size_t line_len;
  1570. const char *objdump_argv[] = {
  1571. "/bin/sh",
  1572. "-c",
  1573. NULL, /* Will be the objdump command to run. */
  1574. "--",
  1575. NULL, /* Will be the symfs path. */
  1576. NULL,
  1577. };
  1578. struct child_process objdump_process;
  1579. int err = dso__disassemble_filename(dso, symfs_filename, sizeof(symfs_filename));
  1580. if (err)
  1581. return err;
  1582. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  1583. symfs_filename, sym->name, map->unmap_ip(map, sym->start),
  1584. map->unmap_ip(map, sym->end));
  1585. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  1586. dso, dso->long_name, sym, sym->name);
  1587. if (dso->binary_type == DSO_BINARY_TYPE__BPF_PROG_INFO) {
  1588. return symbol__disassemble_bpf(sym, args);
  1589. } else if (dso__is_kcore(dso)) {
  1590. kce.kcore_filename = symfs_filename;
  1591. kce.addr = map__rip_2objdump(map, sym->start);
  1592. kce.offs = sym->start;
  1593. kce.len = sym->end - sym->start;
  1594. if (!kcore_extract__create(&kce)) {
  1595. delete_extract = true;
  1596. strlcpy(symfs_filename, kce.extract_filename,
  1597. sizeof(symfs_filename));
  1598. }
  1599. } else if (dso__needs_decompress(dso)) {
  1600. char tmp[KMOD_DECOMP_LEN];
  1601. if (dso__decompress_kmodule_path(dso, symfs_filename,
  1602. tmp, sizeof(tmp)) < 0)
  1603. return -1;
  1604. decomp = true;
  1605. strcpy(symfs_filename, tmp);
  1606. }
  1607. err = asprintf(&command,
  1608. "%s %s%s --start-address=0x%016" PRIx64
  1609. " --stop-address=0x%016" PRIx64
  1610. " -l -d %s %s %s %c%s%c %s%s -C \"$1\"",
  1611. opts->objdump_path ?: "objdump",
  1612. opts->disassembler_style ? "-M " : "",
  1613. opts->disassembler_style ?: "",
  1614. map__rip_2objdump(map, sym->start),
  1615. map__rip_2objdump(map, sym->end),
  1616. opts->show_asm_raw ? "" : "--no-show-raw-insn",
  1617. opts->annotate_src ? "-S" : "",
  1618. opts->prefix ? "--prefix " : "",
  1619. opts->prefix ? '"' : ' ',
  1620. opts->prefix ?: "",
  1621. opts->prefix ? '"' : ' ',
  1622. opts->prefix_strip ? "--prefix-strip=" : "",
  1623. opts->prefix_strip ?: "");
  1624. if (err < 0) {
  1625. pr_err("Failure allocating memory for the command to run\n");
  1626. goto out_remove_tmp;
  1627. }
  1628. pr_debug("Executing: %s\n", command);
  1629. objdump_argv[2] = command;
  1630. objdump_argv[4] = symfs_filename;
  1631. /* Create a pipe to read from for stdout */
  1632. memset(&objdump_process, 0, sizeof(objdump_process));
  1633. objdump_process.argv = objdump_argv;
  1634. objdump_process.out = -1;
  1635. if (start_command(&objdump_process)) {
  1636. pr_err("Failure starting to run %s\n", command);
  1637. err = -1;
  1638. goto out_free_command;
  1639. }
  1640. file = fdopen(objdump_process.out, "r");
  1641. if (!file) {
  1642. pr_err("Failure creating FILE stream for %s\n", command);
  1643. /*
  1644. * If we were using debug info should retry with
  1645. * original binary.
  1646. */
  1647. err = -1;
  1648. goto out_close_stdout;
  1649. }
  1650. /* Storage for getline. */
  1651. line = NULL;
  1652. line_len = 0;
  1653. nline = 0;
  1654. while (!feof(file)) {
  1655. const char *match;
  1656. char *expanded_line;
  1657. if (getline(&line, &line_len, file) < 0 || !line)
  1658. break;
  1659. /* Skip lines containing "filename:" */
  1660. match = strstr(line, symfs_filename);
  1661. if (match && match[strlen(symfs_filename)] == ':')
  1662. continue;
  1663. expanded_line = strim(line);
  1664. expanded_line = expand_tabs(expanded_line

Large files files are truncated, but you can click here to view the full file