PageRenderTime 32ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/perf/util/annotate.c

https://github.com/mturquette/linux
C | 1687 lines | 1304 code | 305 blank | 78 comment | 298 complexity | e3bf5274fac6b02c7a3a71fd0868eff4 MD5 | raw file
  1. /*
  2. * Copyright (C) 2011, Red Hat Inc, Arnaldo Carvalho de Melo <acme@redhat.com>
  3. *
  4. * Parts came from builtin-annotate.c, see those files for further
  5. * copyright notes.
  6. *
  7. * Released under the GPL v2. (and only v2, not any later version)
  8. */
  9. #include "util.h"
  10. #include "ui/ui.h"
  11. #include "sort.h"
  12. #include "build-id.h"
  13. #include "color.h"
  14. #include "cache.h"
  15. #include "symbol.h"
  16. #include "debug.h"
  17. #include "annotate.h"
  18. #include "evsel.h"
  19. #include <regex.h>
  20. #include <pthread.h>
  21. #include <linux/bitops.h>
  22. const char *disassembler_style;
  23. const char *objdump_path;
  24. static regex_t file_lineno;
  25. static struct ins *ins__find(const char *name);
  26. static int disasm_line__parse(char *line, char **namep, char **rawp);
  27. static void ins__delete(struct ins_operands *ops)
  28. {
  29. if (ops == NULL)
  30. return;
  31. zfree(&ops->source.raw);
  32. zfree(&ops->source.name);
  33. zfree(&ops->target.raw);
  34. zfree(&ops->target.name);
  35. }
  36. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  37. struct ins_operands *ops)
  38. {
  39. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  40. }
  41. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  42. struct ins_operands *ops)
  43. {
  44. if (ins->ops->scnprintf)
  45. return ins->ops->scnprintf(ins, bf, size, ops);
  46. return ins__raw_scnprintf(ins, bf, size, ops);
  47. }
  48. static int call__parse(struct ins_operands *ops)
  49. {
  50. char *endptr, *tok, *name;
  51. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  52. name = strchr(endptr, '<');
  53. if (name == NULL)
  54. goto indirect_call;
  55. name++;
  56. #ifdef __arm__
  57. if (strchr(name, '+'))
  58. return -1;
  59. #endif
  60. tok = strchr(name, '>');
  61. if (tok == NULL)
  62. return -1;
  63. *tok = '\0';
  64. ops->target.name = strdup(name);
  65. *tok = '>';
  66. return ops->target.name == NULL ? -1 : 0;
  67. indirect_call:
  68. tok = strchr(endptr, '(');
  69. if (tok != NULL) {
  70. ops->target.addr = 0;
  71. return 0;
  72. }
  73. tok = strchr(endptr, '*');
  74. if (tok == NULL)
  75. return -1;
  76. ops->target.addr = strtoull(tok + 1, NULL, 16);
  77. return 0;
  78. }
  79. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  80. struct ins_operands *ops)
  81. {
  82. if (ops->target.name)
  83. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  84. if (ops->target.addr == 0)
  85. return ins__raw_scnprintf(ins, bf, size, ops);
  86. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  87. }
  88. static struct ins_ops call_ops = {
  89. .parse = call__parse,
  90. .scnprintf = call__scnprintf,
  91. };
  92. bool ins__is_call(const struct ins *ins)
  93. {
  94. return ins->ops == &call_ops;
  95. }
  96. static int jump__parse(struct ins_operands *ops)
  97. {
  98. const char *s = strchr(ops->raw, '+');
  99. ops->target.addr = strtoull(ops->raw, NULL, 16);
  100. if (s++ != NULL)
  101. ops->target.offset = strtoull(s, NULL, 16);
  102. else
  103. ops->target.offset = UINT64_MAX;
  104. return 0;
  105. }
  106. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  107. struct ins_operands *ops)
  108. {
  109. return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
  110. }
  111. static struct ins_ops jump_ops = {
  112. .parse = jump__parse,
  113. .scnprintf = jump__scnprintf,
  114. };
  115. bool ins__is_jump(const struct ins *ins)
  116. {
  117. return ins->ops == &jump_ops;
  118. }
  119. static int comment__symbol(char *raw, char *comment, u64 *addrp, char **namep)
  120. {
  121. char *endptr, *name, *t;
  122. if (strstr(raw, "(%rip)") == NULL)
  123. return 0;
  124. *addrp = strtoull(comment, &endptr, 16);
  125. name = strchr(endptr, '<');
  126. if (name == NULL)
  127. return -1;
  128. name++;
  129. t = strchr(name, '>');
  130. if (t == NULL)
  131. return 0;
  132. *t = '\0';
  133. *namep = strdup(name);
  134. *t = '>';
  135. return 0;
  136. }
  137. static int lock__parse(struct ins_operands *ops)
  138. {
  139. char *name;
  140. ops->locked.ops = zalloc(sizeof(*ops->locked.ops));
  141. if (ops->locked.ops == NULL)
  142. return 0;
  143. if (disasm_line__parse(ops->raw, &name, &ops->locked.ops->raw) < 0)
  144. goto out_free_ops;
  145. ops->locked.ins = ins__find(name);
  146. free(name);
  147. if (ops->locked.ins == NULL)
  148. goto out_free_ops;
  149. if (!ops->locked.ins->ops)
  150. return 0;
  151. if (ops->locked.ins->ops->parse &&
  152. ops->locked.ins->ops->parse(ops->locked.ops) < 0)
  153. goto out_free_ops;
  154. return 0;
  155. out_free_ops:
  156. zfree(&ops->locked.ops);
  157. return 0;
  158. }
  159. static int lock__scnprintf(struct ins *ins, char *bf, size_t size,
  160. struct ins_operands *ops)
  161. {
  162. int printed;
  163. if (ops->locked.ins == NULL)
  164. return ins__raw_scnprintf(ins, bf, size, ops);
  165. printed = scnprintf(bf, size, "%-6.6s ", ins->name);
  166. return printed + ins__scnprintf(ops->locked.ins, bf + printed,
  167. size - printed, ops->locked.ops);
  168. }
  169. static void lock__delete(struct ins_operands *ops)
  170. {
  171. struct ins *ins = ops->locked.ins;
  172. if (ins && ins->ops->free)
  173. ins->ops->free(ops->locked.ops);
  174. else
  175. ins__delete(ops->locked.ops);
  176. zfree(&ops->locked.ops);
  177. zfree(&ops->target.raw);
  178. zfree(&ops->target.name);
  179. }
  180. static struct ins_ops lock_ops = {
  181. .free = lock__delete,
  182. .parse = lock__parse,
  183. .scnprintf = lock__scnprintf,
  184. };
  185. static int mov__parse(struct ins_operands *ops)
  186. {
  187. char *s = strchr(ops->raw, ','), *target, *comment, prev;
  188. if (s == NULL)
  189. return -1;
  190. *s = '\0';
  191. ops->source.raw = strdup(ops->raw);
  192. *s = ',';
  193. if (ops->source.raw == NULL)
  194. return -1;
  195. target = ++s;
  196. #ifdef __arm__
  197. comment = strchr(s, ';');
  198. #else
  199. comment = strchr(s, '#');
  200. #endif
  201. if (comment != NULL)
  202. s = comment - 1;
  203. else
  204. s = strchr(s, '\0') - 1;
  205. while (s > target && isspace(s[0]))
  206. --s;
  207. s++;
  208. prev = *s;
  209. *s = '\0';
  210. ops->target.raw = strdup(target);
  211. *s = prev;
  212. if (ops->target.raw == NULL)
  213. goto out_free_source;
  214. if (comment == NULL)
  215. return 0;
  216. while (comment[0] != '\0' && isspace(comment[0]))
  217. ++comment;
  218. comment__symbol(ops->source.raw, comment, &ops->source.addr, &ops->source.name);
  219. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  220. return 0;
  221. out_free_source:
  222. zfree(&ops->source.raw);
  223. return -1;
  224. }
  225. static int mov__scnprintf(struct ins *ins, char *bf, size_t size,
  226. struct ins_operands *ops)
  227. {
  228. return scnprintf(bf, size, "%-6.6s %s,%s", ins->name,
  229. ops->source.name ?: ops->source.raw,
  230. ops->target.name ?: ops->target.raw);
  231. }
  232. static struct ins_ops mov_ops = {
  233. .parse = mov__parse,
  234. .scnprintf = mov__scnprintf,
  235. };
  236. static int dec__parse(struct ins_operands *ops)
  237. {
  238. char *target, *comment, *s, prev;
  239. target = s = ops->raw;
  240. while (s[0] != '\0' && !isspace(s[0]))
  241. ++s;
  242. prev = *s;
  243. *s = '\0';
  244. ops->target.raw = strdup(target);
  245. *s = prev;
  246. if (ops->target.raw == NULL)
  247. return -1;
  248. comment = strchr(s, '#');
  249. if (comment == NULL)
  250. return 0;
  251. while (comment[0] != '\0' && isspace(comment[0]))
  252. ++comment;
  253. comment__symbol(ops->target.raw, comment, &ops->target.addr, &ops->target.name);
  254. return 0;
  255. }
  256. static int dec__scnprintf(struct ins *ins, char *bf, size_t size,
  257. struct ins_operands *ops)
  258. {
  259. return scnprintf(bf, size, "%-6.6s %s", ins->name,
  260. ops->target.name ?: ops->target.raw);
  261. }
  262. static struct ins_ops dec_ops = {
  263. .parse = dec__parse,
  264. .scnprintf = dec__scnprintf,
  265. };
  266. static int nop__scnprintf(struct ins *ins __maybe_unused, char *bf, size_t size,
  267. struct ins_operands *ops __maybe_unused)
  268. {
  269. return scnprintf(bf, size, "%-6.6s", "nop");
  270. }
  271. static struct ins_ops nop_ops = {
  272. .scnprintf = nop__scnprintf,
  273. };
  274. static struct ins instructions[] = {
  275. { .name = "add", .ops = &mov_ops, },
  276. { .name = "addl", .ops = &mov_ops, },
  277. { .name = "addq", .ops = &mov_ops, },
  278. { .name = "addw", .ops = &mov_ops, },
  279. { .name = "and", .ops = &mov_ops, },
  280. #ifdef __arm__
  281. { .name = "b", .ops = &jump_ops, }, // might also be a call
  282. { .name = "bcc", .ops = &jump_ops, },
  283. { .name = "bcs", .ops = &jump_ops, },
  284. { .name = "beq", .ops = &jump_ops, },
  285. { .name = "bge", .ops = &jump_ops, },
  286. { .name = "bgt", .ops = &jump_ops, },
  287. { .name = "bhi", .ops = &jump_ops, },
  288. { .name = "bl", .ops = &call_ops, },
  289. { .name = "bls", .ops = &jump_ops, },
  290. { .name = "blt", .ops = &jump_ops, },
  291. { .name = "blx", .ops = &call_ops, },
  292. { .name = "bne", .ops = &jump_ops, },
  293. #endif
  294. { .name = "bts", .ops = &mov_ops, },
  295. { .name = "call", .ops = &call_ops, },
  296. { .name = "callq", .ops = &call_ops, },
  297. { .name = "cmp", .ops = &mov_ops, },
  298. { .name = "cmpb", .ops = &mov_ops, },
  299. { .name = "cmpl", .ops = &mov_ops, },
  300. { .name = "cmpq", .ops = &mov_ops, },
  301. { .name = "cmpw", .ops = &mov_ops, },
  302. { .name = "cmpxch", .ops = &mov_ops, },
  303. { .name = "dec", .ops = &dec_ops, },
  304. { .name = "decl", .ops = &dec_ops, },
  305. { .name = "imul", .ops = &mov_ops, },
  306. { .name = "inc", .ops = &dec_ops, },
  307. { .name = "incl", .ops = &dec_ops, },
  308. { .name = "ja", .ops = &jump_ops, },
  309. { .name = "jae", .ops = &jump_ops, },
  310. { .name = "jb", .ops = &jump_ops, },
  311. { .name = "jbe", .ops = &jump_ops, },
  312. { .name = "jc", .ops = &jump_ops, },
  313. { .name = "jcxz", .ops = &jump_ops, },
  314. { .name = "je", .ops = &jump_ops, },
  315. { .name = "jecxz", .ops = &jump_ops, },
  316. { .name = "jg", .ops = &jump_ops, },
  317. { .name = "jge", .ops = &jump_ops, },
  318. { .name = "jl", .ops = &jump_ops, },
  319. { .name = "jle", .ops = &jump_ops, },
  320. { .name = "jmp", .ops = &jump_ops, },
  321. { .name = "jmpq", .ops = &jump_ops, },
  322. { .name = "jna", .ops = &jump_ops, },
  323. { .name = "jnae", .ops = &jump_ops, },
  324. { .name = "jnb", .ops = &jump_ops, },
  325. { .name = "jnbe", .ops = &jump_ops, },
  326. { .name = "jnc", .ops = &jump_ops, },
  327. { .name = "jne", .ops = &jump_ops, },
  328. { .name = "jng", .ops = &jump_ops, },
  329. { .name = "jnge", .ops = &jump_ops, },
  330. { .name = "jnl", .ops = &jump_ops, },
  331. { .name = "jnle", .ops = &jump_ops, },
  332. { .name = "jno", .ops = &jump_ops, },
  333. { .name = "jnp", .ops = &jump_ops, },
  334. { .name = "jns", .ops = &jump_ops, },
  335. { .name = "jnz", .ops = &jump_ops, },
  336. { .name = "jo", .ops = &jump_ops, },
  337. { .name = "jp", .ops = &jump_ops, },
  338. { .name = "jpe", .ops = &jump_ops, },
  339. { .name = "jpo", .ops = &jump_ops, },
  340. { .name = "jrcxz", .ops = &jump_ops, },
  341. { .name = "js", .ops = &jump_ops, },
  342. { .name = "jz", .ops = &jump_ops, },
  343. { .name = "lea", .ops = &mov_ops, },
  344. { .name = "lock", .ops = &lock_ops, },
  345. { .name = "mov", .ops = &mov_ops, },
  346. { .name = "movb", .ops = &mov_ops, },
  347. { .name = "movdqa",.ops = &mov_ops, },
  348. { .name = "movl", .ops = &mov_ops, },
  349. { .name = "movq", .ops = &mov_ops, },
  350. { .name = "movslq", .ops = &mov_ops, },
  351. { .name = "movzbl", .ops = &mov_ops, },
  352. { .name = "movzwl", .ops = &mov_ops, },
  353. { .name = "nop", .ops = &nop_ops, },
  354. { .name = "nopl", .ops = &nop_ops, },
  355. { .name = "nopw", .ops = &nop_ops, },
  356. { .name = "or", .ops = &mov_ops, },
  357. { .name = "orl", .ops = &mov_ops, },
  358. { .name = "test", .ops = &mov_ops, },
  359. { .name = "testb", .ops = &mov_ops, },
  360. { .name = "testl", .ops = &mov_ops, },
  361. { .name = "xadd", .ops = &mov_ops, },
  362. { .name = "xbeginl", .ops = &jump_ops, },
  363. { .name = "xbeginq", .ops = &jump_ops, },
  364. };
  365. static int ins__key_cmp(const void *name, const void *insp)
  366. {
  367. const struct ins *ins = insp;
  368. return strcmp(name, ins->name);
  369. }
  370. static int ins__cmp(const void *a, const void *b)
  371. {
  372. const struct ins *ia = a;
  373. const struct ins *ib = b;
  374. return strcmp(ia->name, ib->name);
  375. }
  376. static void ins__sort(void)
  377. {
  378. const int nmemb = ARRAY_SIZE(instructions);
  379. qsort(instructions, nmemb, sizeof(struct ins), ins__cmp);
  380. }
  381. static struct ins *ins__find(const char *name)
  382. {
  383. const int nmemb = ARRAY_SIZE(instructions);
  384. static bool sorted;
  385. if (!sorted) {
  386. ins__sort();
  387. sorted = true;
  388. }
  389. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__key_cmp);
  390. }
  391. int symbol__annotate_init(struct map *map __maybe_unused, struct symbol *sym)
  392. {
  393. struct annotation *notes = symbol__annotation(sym);
  394. pthread_mutex_init(&notes->lock, NULL);
  395. return 0;
  396. }
  397. int symbol__alloc_hist(struct symbol *sym)
  398. {
  399. struct annotation *notes = symbol__annotation(sym);
  400. const size_t size = symbol__size(sym);
  401. size_t sizeof_sym_hist;
  402. /* Check for overflow when calculating sizeof_sym_hist */
  403. if (size > (SIZE_MAX - sizeof(struct sym_hist)) / sizeof(u64))
  404. return -1;
  405. sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  406. /* Check for overflow in zalloc argument */
  407. if (sizeof_sym_hist > (SIZE_MAX - sizeof(*notes->src))
  408. / symbol_conf.nr_events)
  409. return -1;
  410. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  411. if (notes->src == NULL)
  412. return -1;
  413. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  414. notes->src->nr_histograms = symbol_conf.nr_events;
  415. INIT_LIST_HEAD(&notes->src->source);
  416. return 0;
  417. }
  418. /* The cycles histogram is lazily allocated. */
  419. static int symbol__alloc_hist_cycles(struct symbol *sym)
  420. {
  421. struct annotation *notes = symbol__annotation(sym);
  422. const size_t size = symbol__size(sym);
  423. notes->src->cycles_hist = calloc(size, sizeof(struct cyc_hist));
  424. if (notes->src->cycles_hist == NULL)
  425. return -1;
  426. return 0;
  427. }
  428. void symbol__annotate_zero_histograms(struct symbol *sym)
  429. {
  430. struct annotation *notes = symbol__annotation(sym);
  431. pthread_mutex_lock(&notes->lock);
  432. if (notes->src != NULL) {
  433. memset(notes->src->histograms, 0,
  434. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  435. if (notes->src->cycles_hist)
  436. memset(notes->src->cycles_hist, 0,
  437. symbol__size(sym) * sizeof(struct cyc_hist));
  438. }
  439. pthread_mutex_unlock(&notes->lock);
  440. }
  441. static int __symbol__account_cycles(struct annotation *notes,
  442. u64 start,
  443. unsigned offset, unsigned cycles,
  444. unsigned have_start)
  445. {
  446. struct cyc_hist *ch;
  447. ch = notes->src->cycles_hist;
  448. /*
  449. * For now we can only account one basic block per
  450. * final jump. But multiple could be overlapping.
  451. * Always account the longest one. So when
  452. * a shorter one has been already seen throw it away.
  453. *
  454. * We separately always account the full cycles.
  455. */
  456. ch[offset].num_aggr++;
  457. ch[offset].cycles_aggr += cycles;
  458. if (!have_start && ch[offset].have_start)
  459. return 0;
  460. if (ch[offset].num) {
  461. if (have_start && (!ch[offset].have_start ||
  462. ch[offset].start > start)) {
  463. ch[offset].have_start = 0;
  464. ch[offset].cycles = 0;
  465. ch[offset].num = 0;
  466. if (ch[offset].reset < 0xffff)
  467. ch[offset].reset++;
  468. } else if (have_start &&
  469. ch[offset].start < start)
  470. return 0;
  471. }
  472. ch[offset].have_start = have_start;
  473. ch[offset].start = start;
  474. ch[offset].cycles += cycles;
  475. ch[offset].num++;
  476. return 0;
  477. }
  478. static int __symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  479. struct annotation *notes, int evidx, u64 addr)
  480. {
  481. unsigned offset;
  482. struct sym_hist *h;
  483. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  484. if (addr < sym->start || addr >= sym->end) {
  485. pr_debug("%s(%d): ERANGE! sym->name=%s, start=%#" PRIx64 ", addr=%#" PRIx64 ", end=%#" PRIx64 "\n",
  486. __func__, __LINE__, sym->name, sym->start, addr, sym->end);
  487. return -ERANGE;
  488. }
  489. offset = addr - sym->start;
  490. h = annotation__histogram(notes, evidx);
  491. h->sum++;
  492. h->addr[offset]++;
  493. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  494. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  495. addr, addr - sym->start, evidx, h->addr[offset]);
  496. return 0;
  497. }
  498. static struct annotation *symbol__get_annotation(struct symbol *sym, bool cycles)
  499. {
  500. struct annotation *notes = symbol__annotation(sym);
  501. if (notes->src == NULL) {
  502. if (symbol__alloc_hist(sym) < 0)
  503. return NULL;
  504. }
  505. if (!notes->src->cycles_hist && cycles) {
  506. if (symbol__alloc_hist_cycles(sym) < 0)
  507. return NULL;
  508. }
  509. return notes;
  510. }
  511. static int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  512. int evidx, u64 addr)
  513. {
  514. struct annotation *notes;
  515. if (sym == NULL)
  516. return 0;
  517. notes = symbol__get_annotation(sym, false);
  518. if (notes == NULL)
  519. return -ENOMEM;
  520. return __symbol__inc_addr_samples(sym, map, notes, evidx, addr);
  521. }
  522. static int symbol__account_cycles(u64 addr, u64 start,
  523. struct symbol *sym, unsigned cycles)
  524. {
  525. struct annotation *notes;
  526. unsigned offset;
  527. if (sym == NULL)
  528. return 0;
  529. notes = symbol__get_annotation(sym, true);
  530. if (notes == NULL)
  531. return -ENOMEM;
  532. if (addr < sym->start || addr >= sym->end)
  533. return -ERANGE;
  534. if (start) {
  535. if (start < sym->start || start >= sym->end)
  536. return -ERANGE;
  537. if (start >= addr)
  538. start = 0;
  539. }
  540. offset = addr - sym->start;
  541. return __symbol__account_cycles(notes,
  542. start ? start - sym->start : 0,
  543. offset, cycles,
  544. !!start);
  545. }
  546. int addr_map_symbol__account_cycles(struct addr_map_symbol *ams,
  547. struct addr_map_symbol *start,
  548. unsigned cycles)
  549. {
  550. u64 saddr = 0;
  551. int err;
  552. if (!cycles)
  553. return 0;
  554. /*
  555. * Only set start when IPC can be computed. We can only
  556. * compute it when the basic block is completely in a single
  557. * function.
  558. * Special case the case when the jump is elsewhere, but
  559. * it starts on the function start.
  560. */
  561. if (start &&
  562. (start->sym == ams->sym ||
  563. (ams->sym &&
  564. start->addr == ams->sym->start + ams->map->start)))
  565. saddr = start->al_addr;
  566. if (saddr == 0)
  567. pr_debug2("BB with bad start: addr %"PRIx64" start %"PRIx64" sym %"PRIx64" saddr %"PRIx64"\n",
  568. ams->addr,
  569. start ? start->addr : 0,
  570. ams->sym ? ams->sym->start + ams->map->start : 0,
  571. saddr);
  572. err = symbol__account_cycles(ams->al_addr, saddr, ams->sym, cycles);
  573. if (err)
  574. pr_debug2("account_cycles failed %d\n", err);
  575. return err;
  576. }
  577. int addr_map_symbol__inc_samples(struct addr_map_symbol *ams, int evidx)
  578. {
  579. return symbol__inc_addr_samples(ams->sym, ams->map, evidx, ams->al_addr);
  580. }
  581. int hist_entry__inc_addr_samples(struct hist_entry *he, int evidx, u64 ip)
  582. {
  583. return symbol__inc_addr_samples(he->ms.sym, he->ms.map, evidx, ip);
  584. }
  585. static void disasm_line__init_ins(struct disasm_line *dl)
  586. {
  587. dl->ins = ins__find(dl->name);
  588. if (dl->ins == NULL)
  589. return;
  590. if (!dl->ins->ops)
  591. return;
  592. if (dl->ins->ops->parse && dl->ins->ops->parse(&dl->ops) < 0)
  593. dl->ins = NULL;
  594. }
  595. static int disasm_line__parse(char *line, char **namep, char **rawp)
  596. {
  597. char *name = line, tmp;
  598. while (isspace(name[0]))
  599. ++name;
  600. if (name[0] == '\0')
  601. return -1;
  602. *rawp = name + 1;
  603. while ((*rawp)[0] != '\0' && !isspace((*rawp)[0]))
  604. ++*rawp;
  605. tmp = (*rawp)[0];
  606. (*rawp)[0] = '\0';
  607. *namep = strdup(name);
  608. if (*namep == NULL)
  609. goto out_free_name;
  610. (*rawp)[0] = tmp;
  611. if ((*rawp)[0] != '\0') {
  612. (*rawp)++;
  613. while (isspace((*rawp)[0]))
  614. ++(*rawp);
  615. }
  616. return 0;
  617. out_free_name:
  618. zfree(namep);
  619. return -1;
  620. }
  621. static struct disasm_line *disasm_line__new(s64 offset, char *line,
  622. size_t privsize, int line_nr)
  623. {
  624. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  625. if (dl != NULL) {
  626. dl->offset = offset;
  627. dl->line = strdup(line);
  628. dl->line_nr = line_nr;
  629. if (dl->line == NULL)
  630. goto out_delete;
  631. if (offset != -1) {
  632. if (disasm_line__parse(dl->line, &dl->name, &dl->ops.raw) < 0)
  633. goto out_free_line;
  634. disasm_line__init_ins(dl);
  635. }
  636. }
  637. return dl;
  638. out_free_line:
  639. zfree(&dl->line);
  640. out_delete:
  641. free(dl);
  642. return NULL;
  643. }
  644. void disasm_line__free(struct disasm_line *dl)
  645. {
  646. zfree(&dl->line);
  647. zfree(&dl->name);
  648. if (dl->ins && dl->ins->ops->free)
  649. dl->ins->ops->free(&dl->ops);
  650. else
  651. ins__delete(&dl->ops);
  652. free(dl);
  653. }
  654. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  655. {
  656. if (raw || !dl->ins)
  657. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  658. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  659. }
  660. static void disasm__add(struct list_head *head, struct disasm_line *line)
  661. {
  662. list_add_tail(&line->node, head);
  663. }
  664. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  665. {
  666. list_for_each_entry_continue(pos, head, node)
  667. if (pos->offset >= 0)
  668. return pos;
  669. return NULL;
  670. }
  671. double disasm__calc_percent(struct annotation *notes, int evidx, s64 offset,
  672. s64 end, const char **path, u64 *nr_samples)
  673. {
  674. struct source_line *src_line = notes->src->lines;
  675. double percent = 0.0;
  676. *nr_samples = 0;
  677. if (src_line) {
  678. size_t sizeof_src_line = sizeof(*src_line) +
  679. sizeof(src_line->samples) * (src_line->nr_pcnt - 1);
  680. while (offset < end) {
  681. src_line = (void *)notes->src->lines +
  682. (sizeof_src_line * offset);
  683. if (*path == NULL)
  684. *path = src_line->path;
  685. percent += src_line->samples[evidx].percent;
  686. *nr_samples += src_line->samples[evidx].nr;
  687. offset++;
  688. }
  689. } else {
  690. struct sym_hist *h = annotation__histogram(notes, evidx);
  691. unsigned int hits = 0;
  692. while (offset < end)
  693. hits += h->addr[offset++];
  694. if (h->sum) {
  695. *nr_samples = hits;
  696. percent = 100.0 * hits / h->sum;
  697. }
  698. }
  699. return percent;
  700. }
  701. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  702. struct perf_evsel *evsel, u64 len, int min_pcnt, int printed,
  703. int max_lines, struct disasm_line *queue)
  704. {
  705. static const char *prev_line;
  706. static const char *prev_color;
  707. if (dl->offset != -1) {
  708. const char *path = NULL;
  709. u64 nr_samples;
  710. double percent, max_percent = 0.0;
  711. double *ppercents = &percent;
  712. u64 *psamples = &nr_samples;
  713. int i, nr_percent = 1;
  714. const char *color;
  715. struct annotation *notes = symbol__annotation(sym);
  716. s64 offset = dl->offset;
  717. const u64 addr = start + offset;
  718. struct disasm_line *next;
  719. next = disasm__get_next_ip_line(&notes->src->source, dl);
  720. if (perf_evsel__is_group_event(evsel)) {
  721. nr_percent = evsel->nr_members;
  722. ppercents = calloc(nr_percent, sizeof(double));
  723. psamples = calloc(nr_percent, sizeof(u64));
  724. if (ppercents == NULL || psamples == NULL) {
  725. return -1;
  726. }
  727. }
  728. for (i = 0; i < nr_percent; i++) {
  729. percent = disasm__calc_percent(notes,
  730. notes->src->lines ? i : evsel->idx + i,
  731. offset,
  732. next ? next->offset : (s64) len,
  733. &path, &nr_samples);
  734. ppercents[i] = percent;
  735. psamples[i] = nr_samples;
  736. if (percent > max_percent)
  737. max_percent = percent;
  738. }
  739. if (max_percent < min_pcnt)
  740. return -1;
  741. if (max_lines && printed >= max_lines)
  742. return 1;
  743. if (queue != NULL) {
  744. list_for_each_entry_from(queue, &notes->src->source, node) {
  745. if (queue == dl)
  746. break;
  747. disasm_line__print(queue, sym, start, evsel, len,
  748. 0, 0, 1, NULL);
  749. }
  750. }
  751. color = get_percent_color(max_percent);
  752. /*
  753. * Also color the filename and line if needed, with
  754. * the same color than the percentage. Don't print it
  755. * twice for close colored addr with the same filename:line
  756. */
  757. if (path) {
  758. if (!prev_line || strcmp(prev_line, path)
  759. || color != prev_color) {
  760. color_fprintf(stdout, color, " %s", path);
  761. prev_line = path;
  762. prev_color = color;
  763. }
  764. }
  765. for (i = 0; i < nr_percent; i++) {
  766. percent = ppercents[i];
  767. nr_samples = psamples[i];
  768. color = get_percent_color(percent);
  769. if (symbol_conf.show_total_period)
  770. color_fprintf(stdout, color, " %7" PRIu64,
  771. nr_samples);
  772. else
  773. color_fprintf(stdout, color, " %7.2f", percent);
  774. }
  775. printf(" : ");
  776. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  777. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  778. if (ppercents != &percent)
  779. free(ppercents);
  780. if (psamples != &nr_samples)
  781. free(psamples);
  782. } else if (max_lines && printed >= max_lines)
  783. return 1;
  784. else {
  785. int width = 8;
  786. if (queue)
  787. return -1;
  788. if (perf_evsel__is_group_event(evsel))
  789. width *= evsel->nr_members;
  790. if (!*dl->line)
  791. printf(" %*s:\n", width, " ");
  792. else
  793. printf(" %*s: %s\n", width, " ", dl->line);
  794. }
  795. return 0;
  796. }
  797. /*
  798. * symbol__parse_objdump_line() parses objdump output (with -d --no-show-raw)
  799. * which looks like following
  800. *
  801. * 0000000000415500 <_init>:
  802. * 415500: sub $0x8,%rsp
  803. * 415504: mov 0x2f5ad5(%rip),%rax # 70afe0 <_DYNAMIC+0x2f8>
  804. * 41550b: test %rax,%rax
  805. * 41550e: je 415515 <_init+0x15>
  806. * 415510: callq 416e70 <__gmon_start__@plt>
  807. * 415515: add $0x8,%rsp
  808. * 415519: retq
  809. *
  810. * it will be parsed and saved into struct disasm_line as
  811. * <offset> <name> <ops.raw>
  812. *
  813. * The offset will be a relative offset from the start of the symbol and -1
  814. * means that it's not a disassembly line so should be treated differently.
  815. * The ops.raw part will be parsed further according to type of the instruction.
  816. */
  817. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  818. FILE *file, size_t privsize,
  819. int *line_nr)
  820. {
  821. struct annotation *notes = symbol__annotation(sym);
  822. struct disasm_line *dl;
  823. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  824. size_t line_len;
  825. s64 line_ip, offset = -1;
  826. regmatch_t match[2];
  827. if (getline(&line, &line_len, file) < 0)
  828. return -1;
  829. if (!line)
  830. return -1;
  831. while (line_len != 0 && isspace(line[line_len - 1]))
  832. line[--line_len] = '\0';
  833. c = strchr(line, '\n');
  834. if (c)
  835. *c = 0;
  836. line_ip = -1;
  837. parsed_line = line;
  838. /* /filename:linenr ? Save line number and ignore. */
  839. if (regexec(&file_lineno, line, 2, match, 0) == 0) {
  840. *line_nr = atoi(line + match[1].rm_so);
  841. return 0;
  842. }
  843. /*
  844. * Strip leading spaces:
  845. */
  846. tmp = line;
  847. while (*tmp) {
  848. if (*tmp != ' ')
  849. break;
  850. tmp++;
  851. }
  852. if (*tmp) {
  853. /*
  854. * Parse hexa addresses followed by ':'
  855. */
  856. line_ip = strtoull(tmp, &tmp2, 16);
  857. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  858. line_ip = -1;
  859. }
  860. if (line_ip != -1) {
  861. u64 start = map__rip_2objdump(map, sym->start),
  862. end = map__rip_2objdump(map, sym->end);
  863. offset = line_ip - start;
  864. if ((u64)line_ip < start || (u64)line_ip >= end)
  865. offset = -1;
  866. else
  867. parsed_line = tmp2 + 1;
  868. }
  869. dl = disasm_line__new(offset, parsed_line, privsize, *line_nr);
  870. free(line);
  871. (*line_nr)++;
  872. if (dl == NULL)
  873. return -1;
  874. if (dl->ops.target.offset == UINT64_MAX)
  875. dl->ops.target.offset = dl->ops.target.addr -
  876. map__rip_2objdump(map, sym->start);
  877. /* kcore has no symbols, so add the call target name */
  878. if (dl->ins && ins__is_call(dl->ins) && !dl->ops.target.name) {
  879. struct addr_map_symbol target = {
  880. .map = map,
  881. .addr = dl->ops.target.addr,
  882. };
  883. if (!map_groups__find_ams(&target, NULL) &&
  884. target.sym->start == target.al_addr)
  885. dl->ops.target.name = strdup(target.sym->name);
  886. }
  887. disasm__add(&notes->src->source, dl);
  888. return 0;
  889. }
  890. static __attribute__((constructor)) void symbol__init_regexpr(void)
  891. {
  892. regcomp(&file_lineno, "^/[^:]+:([0-9]+)", REG_EXTENDED);
  893. }
  894. static void delete_last_nop(struct symbol *sym)
  895. {
  896. struct annotation *notes = symbol__annotation(sym);
  897. struct list_head *list = &notes->src->source;
  898. struct disasm_line *dl;
  899. while (!list_empty(list)) {
  900. dl = list_entry(list->prev, struct disasm_line, node);
  901. if (dl->ins && dl->ins->ops) {
  902. if (dl->ins->ops != &nop_ops)
  903. return;
  904. } else {
  905. if (!strstr(dl->line, " nop ") &&
  906. !strstr(dl->line, " nopl ") &&
  907. !strstr(dl->line, " nopw "))
  908. return;
  909. }
  910. list_del(&dl->node);
  911. disasm_line__free(dl);
  912. }
  913. }
  914. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  915. {
  916. struct dso *dso = map->dso;
  917. char *filename = dso__build_id_filename(dso, NULL, 0);
  918. bool free_filename = true;
  919. char command[PATH_MAX * 2];
  920. FILE *file;
  921. int err = 0;
  922. char symfs_filename[PATH_MAX];
  923. struct kcore_extract kce;
  924. bool delete_extract = false;
  925. int lineno = 0;
  926. int nline;
  927. if (filename)
  928. symbol__join_symfs(symfs_filename, filename);
  929. if (filename == NULL) {
  930. if (dso->has_build_id) {
  931. pr_err("Can't annotate %s: not enough memory\n",
  932. sym->name);
  933. return -ENOMEM;
  934. }
  935. goto fallback;
  936. } else if (dso__is_kcore(dso)) {
  937. goto fallback;
  938. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  939. strstr(command, DSO__NAME_KALLSYMS) ||
  940. access(symfs_filename, R_OK)) {
  941. free(filename);
  942. fallback:
  943. /*
  944. * If we don't have build-ids or the build-id file isn't in the
  945. * cache, or is just a kallsyms file, well, lets hope that this
  946. * DSO is the same as when 'perf record' ran.
  947. */
  948. filename = (char *)dso->long_name;
  949. symbol__join_symfs(symfs_filename, filename);
  950. free_filename = false;
  951. }
  952. if (dso->symtab_type == DSO_BINARY_TYPE__KALLSYMS &&
  953. !dso__is_kcore(dso)) {
  954. char bf[SBUILD_ID_SIZE + 15] = " with build id ";
  955. char *build_id_msg = NULL;
  956. if (dso->annotate_warned)
  957. goto out_free_filename;
  958. if (dso->has_build_id) {
  959. build_id__sprintf(dso->build_id,
  960. sizeof(dso->build_id), bf + 15);
  961. build_id_msg = bf;
  962. }
  963. err = -ENOENT;
  964. dso->annotate_warned = 1;
  965. pr_err("Can't annotate %s:\n\n"
  966. "No vmlinux file%s\nwas found in the path.\n\n"
  967. "Note that annotation using /proc/kcore requires CAP_SYS_RAWIO capability.\n\n"
  968. "Please use:\n\n"
  969. " perf buildid-cache -vu vmlinux\n\n"
  970. "or:\n\n"
  971. " --vmlinux vmlinux\n",
  972. sym->name, build_id_msg ?: "");
  973. goto out_free_filename;
  974. }
  975. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  976. filename, sym->name, map->unmap_ip(map, sym->start),
  977. map->unmap_ip(map, sym->end));
  978. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  979. dso, dso->long_name, sym, sym->name);
  980. if (dso__is_kcore(dso)) {
  981. kce.kcore_filename = symfs_filename;
  982. kce.addr = map__rip_2objdump(map, sym->start);
  983. kce.offs = sym->start;
  984. kce.len = sym->end - sym->start;
  985. if (!kcore_extract__create(&kce)) {
  986. delete_extract = true;
  987. strlcpy(symfs_filename, kce.extract_filename,
  988. sizeof(symfs_filename));
  989. if (free_filename) {
  990. free(filename);
  991. free_filename = false;
  992. }
  993. filename = symfs_filename;
  994. }
  995. } else if (dso__needs_decompress(dso)) {
  996. char tmp[PATH_MAX];
  997. struct kmod_path m;
  998. int fd;
  999. bool ret;
  1000. if (kmod_path__parse_ext(&m, symfs_filename))
  1001. goto out_free_filename;
  1002. snprintf(tmp, PATH_MAX, "/tmp/perf-kmod-XXXXXX");
  1003. fd = mkstemp(tmp);
  1004. if (fd < 0) {
  1005. free(m.ext);
  1006. goto out_free_filename;
  1007. }
  1008. ret = decompress_to_file(m.ext, symfs_filename, fd);
  1009. if (ret)
  1010. pr_err("Cannot decompress %s %s\n", m.ext, symfs_filename);
  1011. free(m.ext);
  1012. close(fd);
  1013. if (!ret)
  1014. goto out_free_filename;
  1015. strcpy(symfs_filename, tmp);
  1016. }
  1017. snprintf(command, sizeof(command),
  1018. "%s %s%s --start-address=0x%016" PRIx64
  1019. " --stop-address=0x%016" PRIx64
  1020. " -l -d %s %s -C %s 2>/dev/null|grep -v %s|expand",
  1021. objdump_path ? objdump_path : "objdump",
  1022. disassembler_style ? "-M " : "",
  1023. disassembler_style ? disassembler_style : "",
  1024. map__rip_2objdump(map, sym->start),
  1025. map__rip_2objdump(map, sym->end),
  1026. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  1027. symbol_conf.annotate_src ? "-S" : "",
  1028. symfs_filename, filename);
  1029. pr_debug("Executing: %s\n", command);
  1030. file = popen(command, "r");
  1031. if (!file) {
  1032. pr_err("Failure running %s\n", command);
  1033. /*
  1034. * If we were using debug info should retry with
  1035. * original binary.
  1036. */
  1037. goto out_remove_tmp;
  1038. }
  1039. nline = 0;
  1040. while (!feof(file)) {
  1041. if (symbol__parse_objdump_line(sym, map, file, privsize,
  1042. &lineno) < 0)
  1043. break;
  1044. nline++;
  1045. }
  1046. if (nline == 0)
  1047. pr_err("No output from %s\n", command);
  1048. /*
  1049. * kallsyms does not have symbol sizes so there may a nop at the end.
  1050. * Remove it.
  1051. */
  1052. if (dso__is_kcore(dso))
  1053. delete_last_nop(sym);
  1054. pclose(file);
  1055. out_remove_tmp:
  1056. if (dso__needs_decompress(dso))
  1057. unlink(symfs_filename);
  1058. out_free_filename:
  1059. if (delete_extract)
  1060. kcore_extract__delete(&kce);
  1061. if (free_filename)
  1062. free(filename);
  1063. return err;
  1064. }
  1065. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  1066. {
  1067. struct source_line *iter;
  1068. struct rb_node **p = &root->rb_node;
  1069. struct rb_node *parent = NULL;
  1070. int i, ret;
  1071. while (*p != NULL) {
  1072. parent = *p;
  1073. iter = rb_entry(parent, struct source_line, node);
  1074. ret = strcmp(iter->path, src_line->path);
  1075. if (ret == 0) {
  1076. for (i = 0; i < src_line->nr_pcnt; i++)
  1077. iter->samples[i].percent_sum += src_line->samples[i].percent;
  1078. return;
  1079. }
  1080. if (ret < 0)
  1081. p = &(*p)->rb_left;
  1082. else
  1083. p = &(*p)->rb_right;
  1084. }
  1085. for (i = 0; i < src_line->nr_pcnt; i++)
  1086. src_line->samples[i].percent_sum = src_line->samples[i].percent;
  1087. rb_link_node(&src_line->node, parent, p);
  1088. rb_insert_color(&src_line->node, root);
  1089. }
  1090. static int cmp_source_line(struct source_line *a, struct source_line *b)
  1091. {
  1092. int i;
  1093. for (i = 0; i < a->nr_pcnt; i++) {
  1094. if (a->samples[i].percent_sum == b->samples[i].percent_sum)
  1095. continue;
  1096. return a->samples[i].percent_sum > b->samples[i].percent_sum;
  1097. }
  1098. return 0;
  1099. }
  1100. static void __resort_source_line(struct rb_root *root, struct source_line *src_line)
  1101. {
  1102. struct source_line *iter;
  1103. struct rb_node **p = &root->rb_node;
  1104. struct rb_node *parent = NULL;
  1105. while (*p != NULL) {
  1106. parent = *p;
  1107. iter = rb_entry(parent, struct source_line, node);
  1108. if (cmp_source_line(src_line, iter))
  1109. p = &(*p)->rb_left;
  1110. else
  1111. p = &(*p)->rb_right;
  1112. }
  1113. rb_link_node(&src_line->node, parent, p);
  1114. rb_insert_color(&src_line->node, root);
  1115. }
  1116. static void resort_source_line(struct rb_root *dest_root, struct rb_root *src_root)
  1117. {
  1118. struct source_line *src_line;
  1119. struct rb_node *node;
  1120. node = rb_first(src_root);
  1121. while (node) {
  1122. struct rb_node *next;
  1123. src_line = rb_entry(node, struct source_line, node);
  1124. next = rb_next(node);
  1125. rb_erase(node, src_root);
  1126. __resort_source_line(dest_root, src_line);
  1127. node = next;
  1128. }
  1129. }
  1130. static void symbol__free_source_line(struct symbol *sym, int len)
  1131. {
  1132. struct annotation *notes = symbol__annotation(sym);
  1133. struct source_line *src_line = notes->src->lines;
  1134. size_t sizeof_src_line;
  1135. int i;
  1136. sizeof_src_line = sizeof(*src_line) +
  1137. (sizeof(src_line->samples) * (src_line->nr_pcnt - 1));
  1138. for (i = 0; i < len; i++) {
  1139. free_srcline(src_line->path);
  1140. src_line = (void *)src_line + sizeof_src_line;
  1141. }
  1142. zfree(&notes->src->lines);
  1143. }
  1144. /* Get the filename:line for the colored entries */
  1145. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  1146. struct perf_evsel *evsel,
  1147. struct rb_root *root, int len)
  1148. {
  1149. u64 start;
  1150. int i, k;
  1151. int evidx = evsel->idx;
  1152. struct source_line *src_line;
  1153. struct annotation *notes = symbol__annotation(sym);
  1154. struct sym_hist *h = annotation__histogram(notes, evidx);
  1155. struct rb_root tmp_root = RB_ROOT;
  1156. int nr_pcnt = 1;
  1157. u64 h_sum = h->sum;
  1158. size_t sizeof_src_line = sizeof(struct source_line);
  1159. if (perf_evsel__is_group_event(evsel)) {
  1160. for (i = 1; i < evsel->nr_members; i++) {
  1161. h = annotation__histogram(notes, evidx + i);
  1162. h_sum += h->sum;
  1163. }
  1164. nr_pcnt = evsel->nr_members;
  1165. sizeof_src_line += (nr_pcnt - 1) * sizeof(src_line->samples);
  1166. }
  1167. if (!h_sum)
  1168. return 0;
  1169. src_line = notes->src->lines = calloc(len, sizeof_src_line);
  1170. if (!notes->src->lines)
  1171. return -1;
  1172. start = map__rip_2objdump(map, sym->start);
  1173. for (i = 0; i < len; i++) {
  1174. u64 offset;
  1175. double percent_max = 0.0;
  1176. src_line->nr_pcnt = nr_pcnt;
  1177. for (k = 0; k < nr_pcnt; k++) {
  1178. h = annotation__histogram(notes, evidx + k);
  1179. src_line->samples[k].percent = 100.0 * h->addr[i] / h->sum;
  1180. if (src_line->samples[k].percent > percent_max)
  1181. percent_max = src_line->samples[k].percent;
  1182. }
  1183. if (percent_max <= 0.5)
  1184. goto next;
  1185. offset = start + i;
  1186. src_line->path = get_srcline(map->dso, offset, NULL, false);
  1187. insert_source_line(&tmp_root, src_line);
  1188. next:
  1189. src_line = (void *)src_line + sizeof_src_line;
  1190. }
  1191. resort_source_line(root, &tmp_root);
  1192. return 0;
  1193. }
  1194. static void print_summary(struct rb_root *root, const char *filename)
  1195. {
  1196. struct source_line *src_line;
  1197. struct rb_node *node;
  1198. printf("\nSorted summary for file %s\n", filename);
  1199. printf("----------------------------------------------\n\n");
  1200. if (RB_EMPTY_ROOT(root)) {
  1201. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  1202. return;
  1203. }
  1204. node = rb_first(root);
  1205. while (node) {
  1206. double percent, percent_max = 0.0;
  1207. const char *color;
  1208. char *path;
  1209. int i;
  1210. src_line = rb_entry(node, struct source_line, node);
  1211. for (i = 0; i < src_line->nr_pcnt; i++) {
  1212. percent = src_line->samples[i].percent_sum;
  1213. color = get_percent_color(percent);
  1214. color_fprintf(stdout, color, " %7.2f", percent);
  1215. if (percent > percent_max)
  1216. percent_max = percent;
  1217. }
  1218. path = src_line->path;
  1219. color = get_percent_color(percent_max);
  1220. color_fprintf(stdout, color, " %s\n", path);
  1221. node = rb_next(node);
  1222. }
  1223. }
  1224. static void symbol__annotate_hits(struct symbol *sym, struct perf_evsel *evsel)
  1225. {
  1226. struct annotation *notes = symbol__annotation(sym);
  1227. struct sym_hist *h = annotation__histogram(notes, evsel->idx);
  1228. u64 len = symbol__size(sym), offset;
  1229. for (offset = 0; offset < len; ++offset)
  1230. if (h->addr[offset] != 0)
  1231. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  1232. sym->start + offset, h->addr[offset]);
  1233. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  1234. }
  1235. int symbol__annotate_printf(struct symbol *sym, struct map *map,
  1236. struct perf_evsel *evsel, bool full_paths,
  1237. int min_pcnt, int max_lines, int context)
  1238. {
  1239. struct dso *dso = map->dso;
  1240. char *filename;
  1241. const char *d_filename;
  1242. const char *evsel_name = perf_evsel__name(evsel);
  1243. struct annotation *notes = symbol__annotation(sym);
  1244. struct disasm_line *pos, *queue = NULL;
  1245. u64 start = map__rip_2objdump(map, sym->start);
  1246. int printed = 2, queue_len = 0;
  1247. int more = 0;
  1248. u64 len;
  1249. int width = 8;
  1250. int namelen, evsel_name_len, graph_dotted_len;
  1251. filename = strdup(dso->long_name);
  1252. if (!filename)
  1253. return -ENOMEM;
  1254. if (full_paths)
  1255. d_filename = filename;
  1256. else
  1257. d_filename = basename(filename);
  1258. len = symbol__size(sym);
  1259. namelen = strlen(d_filename);
  1260. evsel_name_len = strlen(evsel_name);
  1261. if (perf_evsel__is_group_event(evsel))
  1262. width *= evsel->nr_members;
  1263. printf(" %-*.*s| Source code & Disassembly of %s for %s\n",
  1264. width, width, "Percent", d_filename, evsel_name);
  1265. graph_dotted_len = width + namelen + evsel_name_len;
  1266. printf("-%-*.*s-----------------------------------------\n",
  1267. graph_dotted_len, graph_dotted_len, graph_dotted_line);
  1268. if (verbose)
  1269. symbol__annotate_hits(sym, evsel);
  1270. list_for_each_entry(pos, &notes->src->source, node) {
  1271. if (context && queue == NULL) {
  1272. queue = pos;
  1273. queue_len = 0;
  1274. }
  1275. switch (disasm_line__print(pos, sym, start, evsel, len,
  1276. min_pcnt, printed, max_lines,
  1277. queue)) {
  1278. case 0:
  1279. ++printed;
  1280. if (context) {
  1281. printed += queue_len;
  1282. queue = NULL;
  1283. queue_len = 0;
  1284. }
  1285. break;
  1286. case 1:
  1287. /* filtered by max_lines */
  1288. ++more;
  1289. break;
  1290. case -1:
  1291. default:
  1292. /*
  1293. * Filtered by min_pcnt or non IP lines when
  1294. * context != 0
  1295. */
  1296. if (!context)
  1297. break;
  1298. if (queue_len == context)
  1299. queue = list_entry(queue->node.next, typeof(*queue), node);
  1300. else
  1301. ++queue_len;
  1302. break;
  1303. }
  1304. }
  1305. free(filename);
  1306. return more;
  1307. }
  1308. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  1309. {
  1310. struct annotation *notes = symbol__annotation(sym);
  1311. struct sym_hist *h = annotation__histogram(notes, evidx);
  1312. memset(h, 0, notes->src->sizeof_sym_hist);
  1313. }
  1314. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  1315. {
  1316. struct annotation *notes = symbol__annotation(sym);
  1317. struct sym_hist *h = annotation__histogram(notes, evidx);
  1318. int len = symbol__size(sym), offset;
  1319. h->sum = 0;
  1320. for (offset = 0; offset < len; ++offset) {
  1321. h->addr[offset] = h->addr[offset] * 7 / 8;
  1322. h->sum += h->addr[offset];
  1323. }
  1324. }
  1325. void disasm__purge(struct list_head *head)
  1326. {
  1327. struct disasm_line *pos, *n;
  1328. list_for_each_entry_safe(pos, n, head, node) {
  1329. list_del(&pos->node);
  1330. disasm_line__free(pos);
  1331. }
  1332. }
  1333. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  1334. {
  1335. size_t printed;
  1336. if (dl->offset == -1)
  1337. return fprintf(fp, "%s\n", dl->line);
  1338. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  1339. if (dl->ops.raw[0] != '\0') {
  1340. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  1341. dl->ops.raw);
  1342. }
  1343. return printed + fprintf(fp, "\n");
  1344. }
  1345. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  1346. {
  1347. struct disasm_line *pos;
  1348. size_t printed = 0;
  1349. list_for_each_entry(pos, head, node)
  1350. printed += disasm_line__fprintf(pos, fp);
  1351. return printed;
  1352. }
  1353. int symbol__tty_annotate(struct symbol *sym, struct map *map,
  1354. struct perf_evsel *evsel, bool print_lines,
  1355. bool full_paths, int min_pcnt, int max_lines)
  1356. {
  1357. struct dso *dso = map->dso;
  1358. struct rb_root source_line = RB_ROOT;
  1359. u64 len;
  1360. if (symbol__annotate(sym, map, 0) < 0)
  1361. return -1;
  1362. len = symbol__size(sym);
  1363. if (print_lines) {
  1364. srcline_full_filename = full_paths;
  1365. symbol__get_source_line(sym, map, evsel, &source_line, len);
  1366. print_summary(&source_line, dso->long_name);
  1367. }
  1368. symbol__annotate_printf(sym, map, evsel, full_paths,
  1369. min_pcnt, max_lines, 0);
  1370. if (print_lines)
  1371. symbol__free_source_line(sym, len);
  1372. disasm__purge(&symbol__annotation(sym)->src->source);
  1373. return 0;
  1374. }
  1375. int hist_entry__annotate(struct hist_entry *he, size_t privsize)
  1376. {
  1377. return symbol__annotate(he->ms.sym, he->ms.map, privsize);
  1378. }
  1379. bool ui__has_annotation(void)
  1380. {
  1381. return use_browser == 1 && perf_hpp_list.sym;
  1382. }