PageRenderTime 67ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/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
  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, &line, &line_len);
  1665. if (!expanded_line)
  1666. break;
  1667. /*
  1668. * The source code line number (lineno) needs to be kept in
  1669. * across calls to symbol__parse_objdump_line(), so that it
  1670. * can associate it with the instructions till the next one.
  1671. * See disasm_line__new() and struct disasm_line::line_nr.
  1672. */
  1673. if (symbol__parse_objdump_line(sym, args, expanded_line,
  1674. &lineno) < 0)
  1675. break;
  1676. nline++;
  1677. }
  1678. free(line);
  1679. err = finish_command(&objdump_process);
  1680. if (err)
  1681. pr_err("Error running %s\n", command);
  1682. if (nline == 0) {
  1683. err = -1;
  1684. pr_err("No output from %s\n", command);
  1685. }
  1686. /*
  1687. * kallsyms does not have symbol sizes so there may a nop at the end.
  1688. * Remove it.
  1689. */
  1690. if (dso__is_kcore(dso))
  1691. delete_last_nop(sym);
  1692. fclose(file);
  1693. out_close_stdout:
  1694. close(objdump_process.out);
  1695. out_free_command:
  1696. free(command);
  1697. out_remove_tmp:
  1698. if (decomp)
  1699. unlink(symfs_filename);
  1700. if (delete_extract)
  1701. kcore_extract__delete(&kce);
  1702. return err;
  1703. }
  1704. static void calc_percent(struct sym_hist *sym_hist,
  1705. struct hists *hists,
  1706. struct annotation_data *data,
  1707. s64 offset, s64 end)
  1708. {
  1709. unsigned int hits = 0;
  1710. u64 period = 0;
  1711. while (offset < end) {
  1712. hits += sym_hist->addr[offset].nr_samples;
  1713. period += sym_hist->addr[offset].period;
  1714. ++offset;
  1715. }
  1716. if (sym_hist->nr_samples) {
  1717. data->he.period = period;
  1718. data->he.nr_samples = hits;
  1719. data->percent[PERCENT_HITS_LOCAL] = 100.0 * hits / sym_hist->nr_samples;
  1720. }
  1721. if (hists->stats.nr_non_filtered_samples)
  1722. data->percent[PERCENT_HITS_GLOBAL] = 100.0 * hits / hists->stats.nr_non_filtered_samples;
  1723. if (sym_hist->period)
  1724. data->percent[PERCENT_PERIOD_LOCAL] = 100.0 * period / sym_hist->period;
  1725. if (hists->stats.total_period)
  1726. data->percent[PERCENT_PERIOD_GLOBAL] = 100.0 * period / hists->stats.total_period;
  1727. }
  1728. static void annotation__calc_percent(struct annotation *notes,
  1729. struct evsel *leader, s64 len)
  1730. {
  1731. struct annotation_line *al, *next;
  1732. struct evsel *evsel;
  1733. list_for_each_entry(al, &notes->src->source, node) {
  1734. s64 end;
  1735. int i = 0;
  1736. if (al->offset == -1)
  1737. continue;
  1738. next = annotation_line__next(al, &notes->src->source);
  1739. end = next ? next->offset : len;
  1740. for_each_group_evsel(evsel, leader) {
  1741. struct hists *hists = evsel__hists(evsel);
  1742. struct annotation_data *data;
  1743. struct sym_hist *sym_hist;
  1744. BUG_ON(i >= al->data_nr);
  1745. sym_hist = annotation__histogram(notes, evsel->idx);
  1746. data = &al->data[i++];
  1747. calc_percent(sym_hist, hists, data, al->offset, end);
  1748. }
  1749. }
  1750. }
  1751. void symbol__calc_percent(struct symbol *sym, struct evsel *evsel)
  1752. {
  1753. struct annotation *notes = symbol__annotation(sym);
  1754. annotation__calc_percent(notes, evsel, symbol__size(sym));
  1755. }
  1756. int symbol__annotate(struct map_symbol *ms, struct evsel *evsel,
  1757. struct annotation_options *options, struct arch **parch)
  1758. {
  1759. struct symbol *sym = ms->sym;
  1760. struct annotation *notes = symbol__annotation(sym);
  1761. struct annotate_args args = {
  1762. .evsel = evsel,
  1763. .options = options,
  1764. };
  1765. struct perf_env *env = perf_evsel__env(evsel);
  1766. const char *arch_name = perf_env__arch(env);
  1767. struct arch *arch;
  1768. int err;
  1769. if (!arch_name)
  1770. return errno;
  1771. args.arch = arch = arch__find(arch_name);
  1772. if (arch == NULL)
  1773. return ENOTSUP;
  1774. if (parch)
  1775. *parch = arch;
  1776. if (arch->init) {
  1777. err = arch->init(arch, env ? env->cpuid : NULL);
  1778. if (err) {
  1779. pr_err("%s: failed to initialize %s arch priv area\n", __func__, arch->name);
  1780. return err;
  1781. }
  1782. }
  1783. args.ms = *ms;
  1784. notes->start = map__rip_2objdump(ms->map, sym->start);
  1785. return symbol__disassemble(sym, &args);
  1786. }
  1787. static void insert_source_line(struct rb_root *root, struct annotation_line *al,
  1788. struct annotation_options *opts)
  1789. {
  1790. struct annotation_line *iter;
  1791. struct rb_node **p = &root->rb_node;
  1792. struct rb_node *parent = NULL;
  1793. int i, ret;
  1794. while (*p != NULL) {
  1795. parent = *p;
  1796. iter = rb_entry(parent, struct annotation_line, rb_node);
  1797. ret = strcmp(iter->path, al->path);
  1798. if (ret == 0) {
  1799. for (i = 0; i < al->data_nr; i++) {
  1800. iter->data[i].percent_sum += annotation_data__percent(&al->data[i],
  1801. opts->percent_type);
  1802. }
  1803. return;
  1804. }
  1805. if (ret < 0)
  1806. p = &(*p)->rb_left;
  1807. else
  1808. p = &(*p)->rb_right;
  1809. }
  1810. for (i = 0; i < al->data_nr; i++) {
  1811. al->data[i].percent_sum = annotation_data__percent(&al->data[i],
  1812. opts->percent_type);
  1813. }
  1814. rb_link_node(&al->rb_node, parent, p);
  1815. rb_insert_color(&al->rb_node, root);
  1816. }
  1817. static int cmp_source_line(struct annotation_line *a, struct annotation_line *b)
  1818. {
  1819. int i;
  1820. for (i = 0; i < a->data_nr; i++) {
  1821. if (a->data[i].percent_sum == b->data[i].percent_sum)
  1822. continue;
  1823. return a->data[i].percent_sum > b->data[i].percent_sum;
  1824. }
  1825. return 0;
  1826. }
  1827. static void __resort_source_line(struct rb_root *root, struct annotation_line *al)
  1828. {
  1829. struct annotation_line *iter;
  1830. struct rb_node **p = &root->rb_node;
  1831. struct rb_node *parent = NULL;
  1832. while (*p != NULL) {
  1833. parent = *p;
  1834. iter = rb_entry(parent, struct annotation_line, rb_node);
  1835. if (cmp_source_line(al, iter))
  1836. p = &(*p)->rb_left;
  1837. else
  1838. p = &(*p)->rb_right;
  1839. }
  1840. rb_link_node(&al->rb_node, parent, p);
  1841. rb_insert_color(&al->rb_node, root);
  1842. }
  1843. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1844. {
  1845. struct annotation_line *al;
  1846. struct rb_node *node;
  1847. node = rb_first(src_root);
  1848. while (node) {
  1849. struct rb_node *next;
  1850. al = rb_entry(node, struct annotation_line, rb_node);
  1851. next = rb_next(node);
  1852. rb_erase(node, src_root);
  1853. __resort_source_line(dest_root, al);
  1854. node = next;
  1855. }
  1856. }
  1857. static void print_summary(struct rb_root *root, const char *filename)
  1858. {
  1859. struct annotation_line *al;
  1860. struct rb_node *node;
  1861. printf("\nSorted summary for file %s\n", filename);
  1862. printf("----------------------------------------------\n\n");
  1863. if (RB_EMPTY_ROOT(root)) {
  1864. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1865. return;
  1866. }
  1867. node = rb_first(root);
  1868. while (node) {
  1869. double percent, percent_max = 0.0;
  1870. const char *color;
  1871. char *path;
  1872. int i;
  1873. al = rb_entry(node, struct annotation_line, rb_node);
  1874. for (i = 0; i < al->data_nr; i++) {
  1875. percent = al->data[i].percent_sum;
  1876. color = get_percent_color(percent);
  1877. color_fprintf(stdout, color, " %7.2f", percent);
  1878. if (percent > percent_max)
  1879. percent_max = percent;
  1880. }
  1881. path = al->path;
  1882. color = get_percent_color(percent_max);
  1883. color_fprintf(stdout, color, " %s\n", path);
  1884. node = rb_next(node);
  1885. }
  1886. }
  1887. static void symbol__annotate_hits(struct symbol *sym, struct evsel *evsel)
  1888. {
  1889. struct annotation *notes = symbol__annotation(sym);
  1890. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1891. u64 len = symbol__size(sym), offset;
  1892. for (offset = 0; offset < len; ++offset)
  1893. if (h->addr[offset].nr_samples != 0)
  1894. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1895. sym->start + offset, h->addr[offset].nr_samples);
  1896. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->nr_samples", h->nr_samples);
  1897. }
  1898. static int annotated_source__addr_fmt_width(struct list_head *lines, u64 start)
  1899. {
  1900. char bf[32];
  1901. struct annotation_line *line;
  1902. list_for_each_entry_reverse(line, lines, node) {
  1903. if (line->offset != -1)
  1904. return scnprintf(bf, sizeof(bf), "%" PRIx64, start + line->offset);
  1905. }
  1906. return 0;
  1907. }
  1908. int symbol__annotate_printf(struct map_symbol *ms, struct evsel *evsel,
  1909. struct annotation_options *opts)
  1910. {
  1911. struct map *map = ms->map;
  1912. struct symbol *sym = ms->sym;
  1913. struct dso *dso = map->dso;
  1914. char *filename;
  1915. const char *d_filename;
  1916. const char *evsel_name = perf_evsel__name(evsel);
  1917. struct annotation *notes = symbol__annotation(sym);
  1918. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1919. struct annotation_line *pos, *queue = NULL;
  1920. u64 start = map__rip_2objdump(map, sym->start);
  1921. int printed = 2, queue_len = 0, addr_fmt_width;
  1922. int more = 0;
  1923. bool context = opts->context;
  1924. u64 len;
  1925. int width = symbol_conf.show_total_period ? 12 : 8;
  1926. int graph_dotted_len;
  1927. char buf[512];
  1928. filename = strdup(dso->long_name);
  1929. if (!filename)
  1930. return -ENOMEM;
  1931. if (opts->full_path)
  1932. d_filename = filename;
  1933. else
  1934. d_filename = basename(filename);
  1935. len = symbol__size(sym);
  1936. if (perf_evsel__is_group_event(evsel)) {
  1937. width *= evsel->core.nr_members;
  1938. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  1939. evsel_name = buf;
  1940. }
  1941. graph_dotted_len = printf(" %-*.*s| Source code & Disassembly of %s for %s (%" PRIu64 " samples, "
  1942. "percent: %s)\n",
  1943. width, width, symbol_conf.show_total_period ? "Period" :
  1944. symbol_conf.show_nr_samples ? "Samples" : "Percent",
  1945. d_filename, evsel_name, h->nr_samples,
  1946. percent_type_str(opts->percent_type));
  1947. printf("%-*.*s----\n",
  1948. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1949. if (verbose > 0)
  1950. symbol__annotate_hits(sym, evsel);
  1951. addr_fmt_width = annotated_source__addr_fmt_width(&notes->src->source, start);
  1952. list_for_each_entry(pos, &notes->src->source, node) {
  1953. int err;
  1954. if (context && queue == NULL) {
  1955. queue = pos;
  1956. queue_len = 0;
  1957. }
  1958. err = annotation_line__print(pos, sym, start, evsel, len,
  1959. opts->min_pcnt, printed, opts->max_lines,
  1960. queue, addr_fmt_width, opts->percent_type);
  1961. switch (err) {
  1962. case 0:
  1963. ++printed;
  1964. if (context) {
  1965. printed += queue_len;
  1966. queue = NULL;
  1967. queue_len = 0;
  1968. }
  1969. break;
  1970. case 1:
  1971. /* filtered by max_lines */
  1972. ++more;
  1973. break;
  1974. case -1:
  1975. default:
  1976. /*
  1977. * Filtered by min_pcnt or non IP lines when
  1978. * context != 0
  1979. */
  1980. if (!context)
  1981. break;
  1982. if (queue_len == context)
  1983. queue = list_entry(queue->node.next, typeof(*queue), node);
  1984. else
  1985. ++queue_len;
  1986. break;
  1987. }
  1988. }
  1989. free(filename);
  1990. return more;
  1991. }
  1992. static void FILE__set_percent_color(void *fp __maybe_unused,
  1993. double percent __maybe_unused,
  1994. bool current __maybe_unused)
  1995. {
  1996. }
  1997. static int FILE__set_jumps_percent_color(void *fp __maybe_unused,
  1998. int nr __maybe_unused, bool current __maybe_unused)
  1999. {
  2000. return 0;
  2001. }
  2002. static int FILE__set_color(void *fp __maybe_unused, int color __maybe_unused)
  2003. {
  2004. return 0;
  2005. }
  2006. static void FILE__printf(void *fp, const char *fmt, ...)
  2007. {
  2008. va_list args;
  2009. va_start(args, fmt);
  2010. vfprintf(fp, fmt, args);
  2011. va_end(args);
  2012. }
  2013. static void FILE__write_graph(void *fp, int graph)
  2014. {
  2015. const char *s;
  2016. switch (graph) {
  2017. case DARROW_CHAR: s = "↓"; break;
  2018. case UARROW_CHAR: s = "↑"; break;
  2019. case LARROW_CHAR: s = "←"; break;
  2020. case RARROW_CHAR: s = "→"; break;
  2021. default: s = "?"; break;
  2022. }
  2023. fputs(s, fp);
  2024. }
  2025. static int symbol__annotate_fprintf2(struct symbol *sym, FILE *fp,
  2026. struct annotation_options *opts)
  2027. {
  2028. struct annotation *notes = symbol__annotation(sym);
  2029. struct annotation_write_ops wops = {
  2030. .first_line = true,
  2031. .obj = fp,
  2032. .set_color = FILE__set_color,
  2033. .set_percent_color = FILE__set_percent_color,
  2034. .set_jumps_percent_color = FILE__set_jumps_percent_color,
  2035. .printf = FILE__printf,
  2036. .write_graph = FILE__write_graph,
  2037. };
  2038. struct annotation_line *al;
  2039. list_for_each_entry(al, &notes->src->source, node) {
  2040. if (annotation_line__filter(al, notes))
  2041. continue;
  2042. annotation_line__write(al, notes, &wops, opts);
  2043. fputc('\n', fp);
  2044. wops.first_line = false;
  2045. }
  2046. return 0;
  2047. }
  2048. int map_symbol__annotation_dump(struct map_symbol *ms, struct evsel *evsel,
  2049. struct annotation_options *opts)
  2050. {
  2051. const char *ev_name = perf_evsel__name(evsel);
  2052. char buf[1024];
  2053. char *filename;
  2054. int err = -1;
  2055. FILE *fp;
  2056. if (asprintf(&filename, "%s.annotation", ms->sym->name) < 0)
  2057. return -1;
  2058. fp = fopen(filename, "w");
  2059. if (fp == NULL)
  2060. goto out_free_filename;
  2061. if (perf_evsel__is_group_event(evsel)) {
  2062. perf_evsel__group_desc(evsel, buf, sizeof(buf));
  2063. ev_name = buf;
  2064. }
  2065. fprintf(fp, "%s() %s\nEvent: %s\n\n",
  2066. ms->sym->name, ms->map->dso->long_name, ev_name);
  2067. symbol__annotate_fprintf2(ms->sym, fp, opts);
  2068. fclose(fp);
  2069. err = 0;
  2070. out_free_filename:
  2071. free(filename);
  2072. return err;
  2073. }
  2074. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  2075. {
  2076. struct annotation *notes = symbol__annotation(sym);
  2077. struct sym_hist *h = annotation__histogram(notes, evidx);
  2078. memset(h, 0, notes->src->sizeof_sym_hist);
  2079. }
  2080. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  2081. {
  2082. struct annotation *notes = symbol__annotation(sym);
  2083. struct sym_hist *h = annotation__histogram(notes, evidx);
  2084. int len = symbol__size(sym), offset;
  2085. h->nr_samples = 0;
  2086. for (offset = 0; offset < len; ++offset) {
  2087. h->addr[offset].nr_samples = h->addr[offset].nr_samples * 7 / 8;
  2088. h->nr_samples += h->addr[offset].nr_samples;
  2089. }
  2090. }
  2091. void annotated_source__purge(struct annotated_source *as)
  2092. {
  2093. struct annotation_line *al, *n;
  2094. list_for_each_entry_safe(al, n, &as->source, node) {
  2095. list_del_init(&al->node);
  2096. disasm_line__free(disasm_line(al));
  2097. }
  2098. }
  2099. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  2100. {
  2101. size_t printed;
  2102. if (dl->al.offset == -1)
  2103. return fprintf(fp, "%s\n", dl->al.line);
  2104. printed = fprintf(fp, "%#" PRIx64 " %s", dl->al.offset, dl->ins.name);
  2105. if (dl->ops.raw[0] != '\0') {
  2106. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  2107. dl->ops.raw);
  2108. }
  2109. return printed + fprintf(fp, "\n");
  2110. }
  2111. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  2112. {
  2113. struct disasm_line *pos;
  2114. size_t printed = 0;
  2115. list_for_each_entry(pos, head, al.node)
  2116. printed += disasm_line__fprintf(pos, fp);
  2117. return printed;
  2118. }
  2119. bool disasm_line__is_valid_local_jump(struct disasm_line *dl, struct symbol *sym)
  2120. {
  2121. if (!dl || !dl->ins.ops || !ins__is_jump(&dl->ins) ||
  2122. !disasm_line__has_local_offset(dl) || dl->ops.target.offset < 0 ||
  2123. dl->ops.target.offset >= (s64)symbol__size(sym))
  2124. return false;
  2125. return true;
  2126. }
  2127. void annotation__mark_jump_targets(struct annotation *notes, struct symbol *sym)
  2128. {
  2129. u64 offset, size = symbol__size(sym);
  2130. /* PLT symbols contain external offsets */
  2131. if (strstr(sym->name, "@plt"))
  2132. return;
  2133. for (offset = 0; offset < size; ++offset) {
  2134. struct annotation_line *al = notes->offsets[offset];
  2135. struct disasm_line *dl;
  2136. dl = disasm_line(al);
  2137. if (!disasm_line__is_valid_local_jump(dl, sym))
  2138. continue;
  2139. al = notes->offsets[dl->ops.target.offset];
  2140. /*
  2141. * FIXME: Oops, no jump target? Buggy disassembler? Or do we
  2142. * have to adjust to the previous offset?
  2143. */
  2144. if (al == NULL)
  2145. continue;
  2146. if (++al->jump_sources > notes->max_jump_sources)
  2147. notes->max_jump_sources = al->jump_sources;
  2148. }
  2149. }
  2150. void annotation__set_offsets(struct annotation *notes, s64 size)
  2151. {
  2152. struct annotation_line *al;
  2153. notes->max_line_len = 0;
  2154. notes->nr_entries = 0;
  2155. notes->nr_asm_entries = 0;
  2156. list_for_each_entry(al, &notes->src->source, node) {
  2157. size_t line_len = strlen(al->line);
  2158. if (notes->max_line_len < line_len)
  2159. notes->max_line_len = line_len;
  2160. al->idx = notes->nr_entries++;
  2161. if (al->offset != -1) {
  2162. al->idx_asm = notes->nr_asm_entries++;
  2163. /*
  2164. * FIXME: short term bandaid to cope with assembly
  2165. * routines that comes with labels in the same column
  2166. * as the address in objdump, sigh.
  2167. *
  2168. * E.g. copy_user_generic_unrolled
  2169. */
  2170. if (al->offset < size)
  2171. notes->offsets[al->offset] = al;
  2172. } else
  2173. al->idx_asm = -1;
  2174. }
  2175. }
  2176. static inline int width_jumps(int n)
  2177. {
  2178. if (n >= 100)
  2179. return 5;
  2180. if (n / 10)
  2181. return 2;
  2182. return 1;
  2183. }
  2184. static int annotation__max_ins_name(struct annotation *notes)
  2185. {
  2186. int max_name = 0, len;
  2187. struct annotation_line *al;
  2188. list_for_each_entry(al, &notes->src->source, node) {
  2189. if (al->offset == -1)
  2190. continue;
  2191. len = strlen(disasm_line(al)->ins.name);
  2192. if (max_name < len)
  2193. max_name = len;
  2194. }
  2195. return max_name;
  2196. }
  2197. void annotation__init_column_widths(struct annotation *notes, struct symbol *sym)
  2198. {
  2199. notes->widths.addr = notes->widths.target =
  2200. notes->widths.min_addr = hex_width(symbol__size(sym));
  2201. notes->widths.max_addr = hex_width(sym->end);
  2202. notes->widths.jumps = width_jumps(notes->max_jump_sources);
  2203. notes->widths.max_ins_name = annotation__max_ins_name(notes);
  2204. }
  2205. void annotation__update_column_widths(struct annotation *notes)
  2206. {
  2207. if (notes->options->use_offset)
  2208. notes->widths.target = notes->widths.min_addr;
  2209. else
  2210. notes->widths.target = notes->widths.max_addr;
  2211. notes->widths.addr = notes->widths.target;
  2212. if (notes->options->show_nr_jumps)
  2213. notes->widths.addr += notes->widths.jumps + 1;
  2214. }
  2215. static void annotation__calc_lines(struct annotation *notes, struct map *map,
  2216. struct rb_root *root,
  2217. struct annotation_options *opts)
  2218. {
  2219. struct annotation_line *al;
  2220. struct rb_root tmp_root = RB_ROOT;
  2221. list_for_each_entry(al, &notes->src->source, node) {
  2222. double percent_max = 0.0;
  2223. int i;
  2224. for (i = 0; i < al->data_nr; i++) {
  2225. double percent;
  2226. percent = annotation_data__percent(&al->data[i],
  2227. opts->percent_type);
  2228. if (percent > percent_max)
  2229. percent_max = percent;
  2230. }
  2231. if (percent_max <= 0.5)
  2232. continue;
  2233. al->path = get_srcline(map->dso, notes->start + al->offset, NULL,
  2234. false, true, notes->start + al->offset);
  2235. insert_source_line(&tmp_root, al, opts);
  2236. }
  2237. resort_source_line(root, &tmp_root);
  2238. }
  2239. static void symbol__calc_lines(struct map_symbol *ms, struct rb_root *root,
  2240. struct annotation_options *opts)
  2241. {
  2242. struct annotation *notes = symbol__annotation(ms->sym);
  2243. annotation__calc_lines(notes, ms->map, root, opts);
  2244. }
  2245. int symbol__tty_annotate2(struct map_symbol *ms, struct evsel *evsel,
  2246. struct annotation_options *opts)
  2247. {
  2248. struct dso *dso = ms->map->dso;
  2249. struct symbol *sym = ms->sym;
  2250. struct rb_root source_line = RB_ROOT;
  2251. struct hists *hists = evsel__hists(evsel);
  2252. char buf[1024];
  2253. if (symbol__annotate2(ms, evsel, opts, NULL) < 0)
  2254. return -1;
  2255. if (opts->print_lines) {
  2256. srcline_full_filename = opts->full_path;
  2257. symbol__calc_lines(ms, &source_line, opts);
  2258. print_summary(&source_line, dso->long_name);
  2259. }
  2260. hists__scnprintf_title(hists, buf, sizeof(buf));
  2261. fprintf(stdout, "%s, [percent: %s]\n%s() %s\n",
  2262. buf, percent_type_str(opts->percent_type), sym->name, dso->long_name);
  2263. symbol__annotate_fprintf2(sym, stdout, opts);
  2264. annotated_source__purge(symbol__annotation(sym)->src);
  2265. return 0;
  2266. }
  2267. int symbol__tty_annotate(struct map_symbol *ms, struct evsel *evsel,
  2268. struct annotation_options *opts)
  2269. {
  2270. struct dso *dso = ms->map->dso;
  2271. struct symbol *sym = ms->sym;
  2272. struct rb_root source_line = RB_ROOT;
  2273. if (symbol__annotate(ms, evsel, opts, NULL) < 0)
  2274. return -1;
  2275. symbol__calc_percent(sym, evsel);
  2276. if (opts->print_lines) {
  2277. srcline_full_filename = opts->full_path;
  2278. symbol__calc_lines(ms, &source_line, opts);
  2279. print_summary(&source_line, dso->long_name);
  2280. }
  2281. symbol__annotate_printf(ms, evsel, opts);
  2282. annotated_source__purge(symbol__annotation(sym)->src);
  2283. return 0;
  2284. }
  2285. bool ui__has_annotation(void)
  2286. {
  2287. return use_browser == 1 && perf_hpp_list.sym;
  2288. }
  2289. static double annotation_line__max_percent(struct annotation_line *al,
  2290. struct annotation *notes,
  2291. unsigned int percent_type)
  2292. {
  2293. double percent_max = 0.0;
  2294. int i;
  2295. for (i = 0; i < notes->nr_events; i++) {
  2296. double percent;
  2297. percent = annotation_data__percent(&al->data[i],
  2298. percent_type);
  2299. if (percent > percent_max)
  2300. percent_max = percent;
  2301. }
  2302. return percent_max;
  2303. }
  2304. static void disasm_line__write(struct disasm_line *dl, struct annotation *notes,
  2305. void *obj, char *bf, size_t size,
  2306. void (*obj__printf)(void *obj, const char *fmt, ...),
  2307. void (*obj__write_graph)(void *obj, int graph))
  2308. {
  2309. if (dl->ins.ops && dl->ins.ops->scnprintf) {
  2310. if (ins__is_jump(&dl->ins)) {
  2311. bool fwd;
  2312. if (dl->ops.target.outside)
  2313. goto call_like;
  2314. fwd = dl->ops.target.offset > dl->al.offset;
  2315. obj__write_graph(obj, fwd ? DARROW_CHAR : UARROW_CHAR);
  2316. obj__printf(obj, " ");
  2317. } else if (ins__is_call(&dl->ins)) {
  2318. call_like:
  2319. obj__write_graph(obj, RARROW_CHAR);
  2320. obj__printf(obj, " ");
  2321. } else if (ins__is_ret(&dl->ins)) {
  2322. obj__write_graph(obj, LARROW_CHAR);
  2323. obj__printf(obj, " ");
  2324. } else {
  2325. obj__printf(obj, " ");
  2326. }
  2327. } else {
  2328. obj__printf(obj, " ");
  2329. }
  2330. disasm_line__scnprintf(dl, bf, size, !notes->options->use_offset, notes->widths.max_ins_name);
  2331. }
  2332. static void ipc_coverage_string(char *bf, int size, struct annotation *notes)
  2333. {
  2334. double ipc = 0.0, coverage = 0.0;
  2335. if (notes->hit_cycles)
  2336. ipc = notes->hit_insn / ((double)notes->hit_cycles);
  2337. if (notes->total_insn) {
  2338. coverage = notes->cover_insn * 100.0 /
  2339. ((double)notes->total_insn);
  2340. }
  2341. scnprintf(bf, size, "(Average IPC: %.2f, IPC Coverage: %.1f%%)",
  2342. ipc, coverage);
  2343. }
  2344. static void __annotation_line__write(struct annotation_line *al, struct annotation *notes,
  2345. bool first_line, bool current_entry, bool change_color, int width,
  2346. void *obj, unsigned int percent_type,
  2347. int (*obj__set_color)(void *obj, int color),
  2348. void (*obj__set_percent_color)(void *obj, double percent, bool current),
  2349. int (*obj__set_jumps_percent_color)(void *obj, int nr, bool current),
  2350. void (*obj__printf)(void *obj, const char *fmt, ...),
  2351. void (*obj__write_graph)(void *obj, int graph))
  2352. {
  2353. double percent_max = annotation_line__max_percent(al, notes, percent_type);
  2354. int pcnt_width = annotation__pcnt_width(notes),
  2355. cycles_width = annotation__cycles_width(notes);
  2356. bool show_title = false;
  2357. char bf[256];
  2358. int printed;
  2359. if (first_line && (al->offset == -1 || percent_max == 0.0)) {
  2360. if (notes->have_cycles) {
  2361. if (al->ipc == 0.0 && al->cycles == 0)
  2362. show_title = true;
  2363. } else
  2364. show_title = true;
  2365. }
  2366. if (al->offset != -1 && percent_max != 0.0) {
  2367. int i;
  2368. for (i = 0; i < notes->nr_events; i++) {
  2369. double percent;
  2370. percent = annotation_data__percent(&al->data[i], percent_type);
  2371. obj__set_percent_color(obj, percent, current_entry);
  2372. if (symbol_conf.show_total_period) {
  2373. obj__printf(obj, "%11" PRIu64 " ", al->data[i].he.period);
  2374. } else if (symbol_conf.show_nr_samples) {
  2375. obj__printf(obj, "%6" PRIu64 " ",
  2376. al->data[i].he.nr_samples);
  2377. } else {
  2378. obj__printf(obj, "%6.2f ", percent);
  2379. }
  2380. }
  2381. } else {
  2382. obj__set_percent_color(obj, 0, current_entry);
  2383. if (!show_title)
  2384. obj__printf(obj, "%-*s", pcnt_width, " ");
  2385. else {
  2386. obj__printf(obj, "%-*s", pcnt_width,
  2387. symbol_conf.show_total_period ? "Period" :
  2388. symbol_conf.show_nr_samples ? "Samples" : "Percent");
  2389. }
  2390. }
  2391. if (notes->have_cycles) {
  2392. if (al->ipc)
  2393. obj__printf(obj, "%*.2f ", ANNOTATION__IPC_WIDTH - 1, al->ipc);
  2394. else if (!show_title)
  2395. obj__printf(obj, "%*s", ANNOTATION__IPC_WIDTH, " ");
  2396. else
  2397. obj__printf(obj, "%*s ", ANNOTATION__IPC_WIDTH - 1, "IPC");
  2398. if (!notes->options->show_minmax_cycle) {
  2399. if (al->cycles)
  2400. obj__printf(obj, "%*" PRIu64 " ",
  2401. ANNOTATION__CYCLES_WIDTH - 1, al->cycles);
  2402. else if (!show_title)
  2403. obj__printf(obj, "%*s",
  2404. ANNOTATION__CYCLES_WIDTH, " ");
  2405. else
  2406. obj__printf(obj, "%*s ",
  2407. ANNOTATION__CYCLES_WIDTH - 1,
  2408. "Cycle");
  2409. } else {
  2410. if (al->cycles) {
  2411. char str[32];
  2412. scnprintf(str, sizeof(str),
  2413. "%" PRIu64 "(%" PRIu64 "/%" PRIu64 ")",
  2414. al->cycles, al->cycles_min,
  2415. al->cycles_max);
  2416. obj__printf(obj, "%*s ",
  2417. ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
  2418. str);
  2419. } else if (!show_title)
  2420. obj__printf(obj, "%*s",
  2421. ANNOTATION__MINMAX_CYCLES_WIDTH,
  2422. " ");
  2423. else
  2424. obj__printf(obj, "%*s ",
  2425. ANNOTATION__MINMAX_CYCLES_WIDTH - 1,
  2426. "Cycle(min/max)");
  2427. }
  2428. if (show_title && !*al->line) {
  2429. ipc_coverage_string(bf, sizeof(bf), notes);
  2430. obj__printf(obj, "%*s", ANNOTATION__AVG_IPC_WIDTH, bf);
  2431. }
  2432. }
  2433. obj__printf(obj, " ");
  2434. if (!*al->line)
  2435. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width, " ");
  2436. else if (al->offset == -1) {
  2437. if (al->line_nr && notes->options->show_linenr)
  2438. printed = scnprintf(bf, sizeof(bf), "%-*d ", notes->widths.addr + 1, al->line_nr);
  2439. else
  2440. printed = scnprintf(bf, sizeof(bf), "%-*s ", notes->widths.addr, " ");
  2441. obj__printf(obj, bf);
  2442. obj__printf(obj, "%-*s", width - printed - pcnt_width - cycles_width + 1, al->line);
  2443. } else {
  2444. u64 addr = al->offset;
  2445. int color = -1;
  2446. if (!notes->options->use_offset)
  2447. addr += notes->start;
  2448. if (!notes->options->use_offset) {
  2449. printed = scnprintf(bf, sizeof(bf), "%" PRIx64 ": ", addr);
  2450. } else {
  2451. if (al->jump_sources &&
  2452. notes->options->offset_level >= ANNOTATION__OFFSET_JUMP_TARGETS) {
  2453. if (notes->options->show_nr_jumps) {
  2454. int prev;
  2455. printed = scnprintf(bf, sizeof(bf), "%*d ",
  2456. notes->widths.jumps,
  2457. al->jump_sources);
  2458. prev = obj__set_jumps_percent_color(obj, al->jump_sources,
  2459. current_entry);
  2460. obj__printf(obj, bf);
  2461. obj__set_color(obj, prev);
  2462. }
  2463. print_addr:
  2464. printed = scnprintf(bf, sizeof(bf), "%*" PRIx64 ": ",
  2465. notes->widths.target, addr);
  2466. } else if (ins__is_call(&disasm_line(al)->ins) &&
  2467. notes->options->offset_level >= ANNOTATION__OFFSET_CALL) {
  2468. goto print_addr;
  2469. } else if (notes->options->offset_level == ANNOTATION__MAX_OFFSET_LEVEL) {
  2470. goto print_addr;
  2471. } else {
  2472. printed = scnprintf(bf, sizeof(bf), "%-*s ",
  2473. notes->widths.addr, " ");
  2474. }
  2475. }
  2476. if (change_color)
  2477. color = obj__set_color(obj, HE_COLORSET_ADDR);
  2478. obj__printf(obj, bf);
  2479. if (change_color)
  2480. obj__set_color(obj, color);
  2481. disasm_line__write(disasm_line(al), notes, obj, bf, sizeof(bf), obj__printf, obj__write_graph);
  2482. obj__printf(obj, "%-*s", width - pcnt_width - cycles_width - 3 - printed, bf);
  2483. }
  2484. }
  2485. void annotation_line__write(struct annotation_line *al, struct annotation *notes,
  2486. struct annotation_write_ops *wops,
  2487. struct annotation_options *opts)
  2488. {
  2489. __annotation_line__write(al, notes, wops->first_line, wops->current_entry,
  2490. wops->change_color, wops->width, wops->obj,
  2491. opts->percent_type,
  2492. wops->set_color, wops->set_percent_color,
  2493. wops->set_jumps_percent_color, wops->printf,
  2494. wops->write_graph);
  2495. }
  2496. int symbol__annotate2(struct map_symbol *ms, struct evsel *evsel,
  2497. struct annotation_options *options, struct arch **parch)
  2498. {
  2499. struct symbol *sym = ms->sym;
  2500. struct annotation *notes = symbol__annotation(sym);
  2501. size_t size = symbol__size(sym);
  2502. int nr_pcnt = 1, err;
  2503. notes->offsets = zalloc(size * sizeof(struct annotation_line *));
  2504. if (notes->offsets == NULL)
  2505. return ENOMEM;
  2506. if (perf_evsel__is_group_event(evsel))
  2507. nr_pcnt = evsel->core.nr_members;
  2508. err = symbol__annotate(ms, evsel, options, parch);
  2509. if (err)
  2510. goto out_free_offsets;
  2511. notes->options = options;
  2512. symbol__calc_percent(sym, evsel);
  2513. annotation__set_offsets(notes, size);
  2514. annotation__mark_jump_targets(notes, sym);
  2515. annotation__compute_ipc(notes, size);
  2516. annotation__init_column_widths(notes, sym);
  2517. notes->nr_events = nr_pcnt;
  2518. annotation__update_column_widths(notes);
  2519. sym->annotate2 = true;
  2520. return 0;
  2521. out_free_offsets:
  2522. zfree(&notes->offsets);
  2523. return err;
  2524. }
  2525. static int annotation__config(const char *var, const char *value, void *data)
  2526. {
  2527. struct annotation_options *opt = data;
  2528. if (!strstarts(var, "annotate."))
  2529. return 0;
  2530. if (!strcmp(var, "annotate.offset_level")) {
  2531. perf_config_u8(&opt->offset_level, "offset_level", value);
  2532. if (opt->offset_level > ANNOTATION__MAX_OFFSET_LEVEL)
  2533. opt->offset_level = ANNOTATION__MAX_OFFSET_LEVEL;
  2534. else if (opt->offset_level < ANNOTATION__MIN_OFFSET_LEVEL)
  2535. opt->offset_level = ANNOTATION__MIN_OFFSET_LEVEL;
  2536. } else if (!strcmp(var, "annotate.hide_src_code")) {
  2537. opt->hide_src_code = perf_config_bool("hide_src_code", value);
  2538. } else if (!strcmp(var, "annotate.jump_arrows")) {
  2539. opt->jump_arrows = perf_config_bool("jump_arrows", value);
  2540. } else if (!strcmp(var, "annotate.show_linenr")) {
  2541. opt->show_linenr = perf_config_bool("show_linenr", value);
  2542. } else if (!strcmp(var, "annotate.show_nr_jumps")) {
  2543. opt->show_nr_jumps = perf_config_bool("show_nr_jumps", value);
  2544. } else if (!strcmp(var, "annotate.show_nr_samples")) {
  2545. symbol_conf.show_nr_samples = perf_config_bool("show_nr_samples",
  2546. value);
  2547. } else if (!strcmp(var, "annotate.show_total_period")) {
  2548. symbol_conf.show_total_period = perf_config_bool("show_total_period",
  2549. value);
  2550. } else if (!strcmp(var, "annotate.use_offset")) {
  2551. opt->use_offset = perf_config_bool("use_offset", value);
  2552. } else {
  2553. pr_debug("%s variable unknown, ignoring...", var);
  2554. }
  2555. return 0;
  2556. }
  2557. void annotation_config__init(struct annotation_options *opt)
  2558. {
  2559. perf_config(annotation__config, opt);
  2560. }
  2561. static unsigned int parse_percent_type(char *str1, char *str2)
  2562. {
  2563. unsigned int type = (unsigned int) -1;
  2564. if (!strcmp("period", str1)) {
  2565. if (!strcmp("local", str2))
  2566. type = PERCENT_PERIOD_LOCAL;
  2567. else if (!strcmp("global", str2))
  2568. type = PERCENT_PERIOD_GLOBAL;
  2569. }
  2570. if (!strcmp("hits", str1)) {
  2571. if (!strcmp("local", str2))
  2572. type = PERCENT_HITS_LOCAL;
  2573. else if (!strcmp("global", str2))
  2574. type = PERCENT_HITS_GLOBAL;
  2575. }
  2576. return type;
  2577. }
  2578. int annotate_parse_percent_type(const struct option *opt, const char *_str,
  2579. int unset __maybe_unused)
  2580. {
  2581. struct annotation_options *opts = opt->value;
  2582. unsigned int type;
  2583. char *str1, *str2;
  2584. int err = -1;
  2585. str1 = strdup(_str);
  2586. if (!str1)
  2587. return -ENOMEM;
  2588. str2 = strchr(str1, '-');
  2589. if (!str2)
  2590. goto out;
  2591. *str2++ = 0;
  2592. type = parse_percent_type(str1, str2);
  2593. if (type == (unsigned int) -1)
  2594. type = parse_percent_type(str2, str1);
  2595. if (type != (unsigned int) -1) {
  2596. opts->percent_type = type;
  2597. err = 0;
  2598. }
  2599. out:
  2600. free(str1);
  2601. return err;
  2602. }
  2603. int annotate_check_args(struct annotation_options *args)
  2604. {
  2605. if (args->prefix_strip && !args->prefix) {
  2606. pr_err("--prefix-strip requires --prefix\n");
  2607. return -1;
  2608. }
  2609. return 0;
  2610. }