annotate.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  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 "build-id.h"
  11. #include "color.h"
  12. #include "cache.h"
  13. #include "symbol.h"
  14. #include "debug.h"
  15. #include "annotate.h"
  16. #include <pthread.h>
  17. const char *disassembler_style;
  18. static int ins__raw_scnprintf(struct ins *ins, char *bf, size_t size,
  19. struct ins_operands *ops)
  20. {
  21. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->raw);
  22. }
  23. int ins__scnprintf(struct ins *ins, char *bf, size_t size,
  24. struct ins_operands *ops)
  25. {
  26. if (ins->ops->scnprintf)
  27. return ins->ops->scnprintf(ins, bf, size, ops);
  28. return ins__raw_scnprintf(ins, bf, size, ops);
  29. }
  30. static int call__parse(struct ins_operands *ops)
  31. {
  32. char *endptr, *tok, *name;
  33. ops->target.addr = strtoull(ops->raw, &endptr, 16);
  34. name = strchr(endptr, '<');
  35. if (name == NULL)
  36. goto indirect_call;
  37. name++;
  38. tok = strchr(name, '>');
  39. if (tok == NULL)
  40. return -1;
  41. *tok = '\0';
  42. ops->target.name = strdup(name);
  43. *tok = '>';
  44. return ops->target.name == NULL ? -1 : 0;
  45. indirect_call:
  46. tok = strchr(endptr, '*');
  47. if (tok == NULL)
  48. return -1;
  49. ops->target.addr = strtoull(tok + 1, NULL, 16);
  50. return 0;
  51. }
  52. static int call__scnprintf(struct ins *ins, char *bf, size_t size,
  53. struct ins_operands *ops)
  54. {
  55. if (ops->target.name)
  56. return scnprintf(bf, size, "%-6.6s %s", ins->name, ops->target.name);
  57. return scnprintf(bf, size, "%-6.6s *%" PRIx64, ins->name, ops->target.addr);
  58. }
  59. static struct ins_ops call_ops = {
  60. .parse = call__parse,
  61. .scnprintf = call__scnprintf,
  62. };
  63. bool ins__is_call(const struct ins *ins)
  64. {
  65. return ins->ops == &call_ops;
  66. }
  67. static int jump__parse(struct ins_operands *ops)
  68. {
  69. const char *s = strchr(ops->raw, '+');
  70. ops->target.addr = strtoll(ops->raw, NULL, 16);
  71. if (s++ != NULL)
  72. ops->target.offset = strtoll(s, NULL, 16);
  73. else
  74. ops->target.offset = UINT64_MAX;
  75. return 0;
  76. }
  77. static int jump__scnprintf(struct ins *ins, char *bf, size_t size,
  78. struct ins_operands *ops)
  79. {
  80. return scnprintf(bf, size, "%-6.6s %" PRIx64, ins->name, ops->target.offset);
  81. }
  82. static struct ins_ops jump_ops = {
  83. .parse = jump__parse,
  84. .scnprintf = jump__scnprintf,
  85. };
  86. bool ins__is_jump(const struct ins *ins)
  87. {
  88. return ins->ops == &jump_ops;
  89. }
  90. /*
  91. * Must be sorted by name!
  92. */
  93. static struct ins instructions[] = {
  94. { .name = "call", .ops = &call_ops, },
  95. { .name = "callq", .ops = &call_ops, },
  96. { .name = "ja", .ops = &jump_ops, },
  97. { .name = "jae", .ops = &jump_ops, },
  98. { .name = "jb", .ops = &jump_ops, },
  99. { .name = "jbe", .ops = &jump_ops, },
  100. { .name = "jc", .ops = &jump_ops, },
  101. { .name = "jcxz", .ops = &jump_ops, },
  102. { .name = "je", .ops = &jump_ops, },
  103. { .name = "jecxz", .ops = &jump_ops, },
  104. { .name = "jg", .ops = &jump_ops, },
  105. { .name = "jge", .ops = &jump_ops, },
  106. { .name = "jl", .ops = &jump_ops, },
  107. { .name = "jle", .ops = &jump_ops, },
  108. { .name = "jmp", .ops = &jump_ops, },
  109. { .name = "jmpq", .ops = &jump_ops, },
  110. { .name = "jna", .ops = &jump_ops, },
  111. { .name = "jnae", .ops = &jump_ops, },
  112. { .name = "jnb", .ops = &jump_ops, },
  113. { .name = "jnbe", .ops = &jump_ops, },
  114. { .name = "jnc", .ops = &jump_ops, },
  115. { .name = "jne", .ops = &jump_ops, },
  116. { .name = "jng", .ops = &jump_ops, },
  117. { .name = "jnge", .ops = &jump_ops, },
  118. { .name = "jnl", .ops = &jump_ops, },
  119. { .name = "jnle", .ops = &jump_ops, },
  120. { .name = "jno", .ops = &jump_ops, },
  121. { .name = "jnp", .ops = &jump_ops, },
  122. { .name = "jns", .ops = &jump_ops, },
  123. { .name = "jnz", .ops = &jump_ops, },
  124. { .name = "jo", .ops = &jump_ops, },
  125. { .name = "jp", .ops = &jump_ops, },
  126. { .name = "jpe", .ops = &jump_ops, },
  127. { .name = "jpo", .ops = &jump_ops, },
  128. { .name = "jrcxz", .ops = &jump_ops, },
  129. { .name = "js", .ops = &jump_ops, },
  130. { .name = "jz", .ops = &jump_ops, },
  131. };
  132. static int ins__cmp(const void *name, const void *insp)
  133. {
  134. const struct ins *ins = insp;
  135. return strcmp(name, ins->name);
  136. }
  137. static struct ins *ins__find(const char *name)
  138. {
  139. const int nmemb = ARRAY_SIZE(instructions);
  140. return bsearch(name, instructions, nmemb, sizeof(struct ins), ins__cmp);
  141. }
  142. int symbol__annotate_init(struct map *map __used, struct symbol *sym)
  143. {
  144. struct annotation *notes = symbol__annotation(sym);
  145. pthread_mutex_init(&notes->lock, NULL);
  146. return 0;
  147. }
  148. int symbol__alloc_hist(struct symbol *sym)
  149. {
  150. struct annotation *notes = symbol__annotation(sym);
  151. const size_t size = symbol__size(sym);
  152. size_t sizeof_sym_hist = (sizeof(struct sym_hist) + size * sizeof(u64));
  153. notes->src = zalloc(sizeof(*notes->src) + symbol_conf.nr_events * sizeof_sym_hist);
  154. if (notes->src == NULL)
  155. return -1;
  156. notes->src->sizeof_sym_hist = sizeof_sym_hist;
  157. notes->src->nr_histograms = symbol_conf.nr_events;
  158. INIT_LIST_HEAD(&notes->src->source);
  159. return 0;
  160. }
  161. void symbol__annotate_zero_histograms(struct symbol *sym)
  162. {
  163. struct annotation *notes = symbol__annotation(sym);
  164. pthread_mutex_lock(&notes->lock);
  165. if (notes->src != NULL)
  166. memset(notes->src->histograms, 0,
  167. notes->src->nr_histograms * notes->src->sizeof_sym_hist);
  168. pthread_mutex_unlock(&notes->lock);
  169. }
  170. int symbol__inc_addr_samples(struct symbol *sym, struct map *map,
  171. int evidx, u64 addr)
  172. {
  173. unsigned offset;
  174. struct annotation *notes;
  175. struct sym_hist *h;
  176. notes = symbol__annotation(sym);
  177. if (notes->src == NULL)
  178. return -ENOMEM;
  179. pr_debug3("%s: addr=%#" PRIx64 "\n", __func__, map->unmap_ip(map, addr));
  180. if (addr < sym->start || addr > sym->end)
  181. return -ERANGE;
  182. offset = addr - sym->start;
  183. h = annotation__histogram(notes, evidx);
  184. h->sum++;
  185. h->addr[offset]++;
  186. pr_debug3("%#" PRIx64 " %s: period++ [addr: %#" PRIx64 ", %#" PRIx64
  187. ", evidx=%d] => %" PRIu64 "\n", sym->start, sym->name,
  188. addr, addr - sym->start, evidx, h->addr[offset]);
  189. return 0;
  190. }
  191. static void disasm_line__init_ins(struct disasm_line *dl)
  192. {
  193. dl->ins = ins__find(dl->name);
  194. if (dl->ins == NULL)
  195. return;
  196. if (!dl->ins->ops)
  197. return;
  198. if (dl->ins->ops->parse)
  199. dl->ins->ops->parse(&dl->ops);
  200. }
  201. static struct disasm_line *disasm_line__new(s64 offset, char *line, size_t privsize)
  202. {
  203. struct disasm_line *dl = zalloc(sizeof(*dl) + privsize);
  204. if (dl != NULL) {
  205. dl->offset = offset;
  206. dl->line = strdup(line);
  207. if (dl->line == NULL)
  208. goto out_delete;
  209. if (offset != -1) {
  210. char *name = dl->line, tmp;
  211. while (isspace(name[0]))
  212. ++name;
  213. if (name[0] == '\0')
  214. goto out_delete;
  215. dl->ops.raw = name + 1;
  216. while (dl->ops.raw[0] != '\0' &&
  217. !isspace(dl->ops.raw[0]))
  218. ++dl->ops.raw;
  219. tmp = dl->ops.raw[0];
  220. dl->ops.raw[0] = '\0';
  221. dl->name = strdup(name);
  222. if (dl->name == NULL)
  223. goto out_free_line;
  224. dl->ops.raw[0] = tmp;
  225. if (dl->ops.raw[0] != '\0') {
  226. dl->ops.raw++;
  227. while (isspace(dl->ops.raw[0]))
  228. ++dl->ops.raw;
  229. }
  230. disasm_line__init_ins(dl);
  231. }
  232. }
  233. return dl;
  234. out_free_line:
  235. free(dl->line);
  236. out_delete:
  237. free(dl);
  238. return NULL;
  239. }
  240. void disasm_line__free(struct disasm_line *dl)
  241. {
  242. free(dl->line);
  243. free(dl->name);
  244. free(dl->ops.target.name);
  245. free(dl);
  246. }
  247. int disasm_line__scnprintf(struct disasm_line *dl, char *bf, size_t size, bool raw)
  248. {
  249. if (raw || !dl->ins)
  250. return scnprintf(bf, size, "%-6.6s %s", dl->name, dl->ops.raw);
  251. return ins__scnprintf(dl->ins, bf, size, &dl->ops);
  252. }
  253. static void disasm__add(struct list_head *head, struct disasm_line *line)
  254. {
  255. list_add_tail(&line->node, head);
  256. }
  257. struct disasm_line *disasm__get_next_ip_line(struct list_head *head, struct disasm_line *pos)
  258. {
  259. list_for_each_entry_continue(pos, head, node)
  260. if (pos->offset >= 0)
  261. return pos;
  262. return NULL;
  263. }
  264. static int disasm_line__print(struct disasm_line *dl, struct symbol *sym, u64 start,
  265. int evidx, u64 len, int min_pcnt, int printed,
  266. int max_lines, struct disasm_line *queue)
  267. {
  268. static const char *prev_line;
  269. static const char *prev_color;
  270. if (dl->offset != -1) {
  271. const char *path = NULL;
  272. unsigned int hits = 0;
  273. double percent = 0.0;
  274. const char *color;
  275. struct annotation *notes = symbol__annotation(sym);
  276. struct source_line *src_line = notes->src->lines;
  277. struct sym_hist *h = annotation__histogram(notes, evidx);
  278. s64 offset = dl->offset;
  279. const u64 addr = start + offset;
  280. struct disasm_line *next;
  281. next = disasm__get_next_ip_line(&notes->src->source, dl);
  282. while (offset < (s64)len &&
  283. (next == NULL || offset < next->offset)) {
  284. if (src_line) {
  285. if (path == NULL)
  286. path = src_line[offset].path;
  287. percent += src_line[offset].percent;
  288. } else
  289. hits += h->addr[offset];
  290. ++offset;
  291. }
  292. if (src_line == NULL && h->sum)
  293. percent = 100.0 * hits / h->sum;
  294. if (percent < min_pcnt)
  295. return -1;
  296. if (max_lines && printed >= max_lines)
  297. return 1;
  298. if (queue != NULL) {
  299. list_for_each_entry_from(queue, &notes->src->source, node) {
  300. if (queue == dl)
  301. break;
  302. disasm_line__print(queue, sym, start, evidx, len,
  303. 0, 0, 1, NULL);
  304. }
  305. }
  306. color = get_percent_color(percent);
  307. /*
  308. * Also color the filename and line if needed, with
  309. * the same color than the percentage. Don't print it
  310. * twice for close colored addr with the same filename:line
  311. */
  312. if (path) {
  313. if (!prev_line || strcmp(prev_line, path)
  314. || color != prev_color) {
  315. color_fprintf(stdout, color, " %s", path);
  316. prev_line = path;
  317. prev_color = color;
  318. }
  319. }
  320. color_fprintf(stdout, color, " %7.2f", percent);
  321. printf(" : ");
  322. color_fprintf(stdout, PERF_COLOR_MAGENTA, " %" PRIx64 ":", addr);
  323. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", dl->line);
  324. } else if (max_lines && printed >= max_lines)
  325. return 1;
  326. else {
  327. if (queue)
  328. return -1;
  329. if (!*dl->line)
  330. printf(" :\n");
  331. else
  332. printf(" : %s\n", dl->line);
  333. }
  334. return 0;
  335. }
  336. static int symbol__parse_objdump_line(struct symbol *sym, struct map *map,
  337. FILE *file, size_t privsize)
  338. {
  339. struct annotation *notes = symbol__annotation(sym);
  340. struct disasm_line *dl;
  341. char *line = NULL, *parsed_line, *tmp, *tmp2, *c;
  342. size_t line_len;
  343. s64 line_ip, offset = -1;
  344. if (getline(&line, &line_len, file) < 0)
  345. return -1;
  346. if (!line)
  347. return -1;
  348. while (line_len != 0 && isspace(line[line_len - 1]))
  349. line[--line_len] = '\0';
  350. c = strchr(line, '\n');
  351. if (c)
  352. *c = 0;
  353. line_ip = -1;
  354. parsed_line = line;
  355. /*
  356. * Strip leading spaces:
  357. */
  358. tmp = line;
  359. while (*tmp) {
  360. if (*tmp != ' ')
  361. break;
  362. tmp++;
  363. }
  364. if (*tmp) {
  365. /*
  366. * Parse hexa addresses followed by ':'
  367. */
  368. line_ip = strtoull(tmp, &tmp2, 16);
  369. if (*tmp2 != ':' || tmp == tmp2 || tmp2[1] == '\0')
  370. line_ip = -1;
  371. }
  372. if (line_ip != -1) {
  373. u64 start = map__rip_2objdump(map, sym->start),
  374. end = map__rip_2objdump(map, sym->end);
  375. offset = line_ip - start;
  376. if (offset < 0 || (u64)line_ip > end)
  377. offset = -1;
  378. else
  379. parsed_line = tmp2 + 1;
  380. }
  381. dl = disasm_line__new(offset, parsed_line, privsize);
  382. free(line);
  383. if (dl == NULL)
  384. return -1;
  385. disasm__add(&notes->src->source, dl);
  386. return 0;
  387. }
  388. int symbol__annotate(struct symbol *sym, struct map *map, size_t privsize)
  389. {
  390. struct dso *dso = map->dso;
  391. char *filename = dso__build_id_filename(dso, NULL, 0);
  392. bool free_filename = true;
  393. char command[PATH_MAX * 2];
  394. FILE *file;
  395. int err = 0;
  396. char symfs_filename[PATH_MAX];
  397. if (filename) {
  398. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  399. symbol_conf.symfs, filename);
  400. }
  401. if (filename == NULL) {
  402. if (dso->has_build_id) {
  403. pr_err("Can't annotate %s: not enough memory\n",
  404. sym->name);
  405. return -ENOMEM;
  406. }
  407. goto fallback;
  408. } else if (readlink(symfs_filename, command, sizeof(command)) < 0 ||
  409. strstr(command, "[kernel.kallsyms]") ||
  410. access(symfs_filename, R_OK)) {
  411. free(filename);
  412. fallback:
  413. /*
  414. * If we don't have build-ids or the build-id file isn't in the
  415. * cache, or is just a kallsyms file, well, lets hope that this
  416. * DSO is the same as when 'perf record' ran.
  417. */
  418. filename = dso->long_name;
  419. snprintf(symfs_filename, sizeof(symfs_filename), "%s%s",
  420. symbol_conf.symfs, filename);
  421. free_filename = false;
  422. }
  423. if (dso->symtab_type == SYMTAB__KALLSYMS) {
  424. char bf[BUILD_ID_SIZE * 2 + 16] = " with build id ";
  425. char *build_id_msg = NULL;
  426. if (dso->annotate_warned)
  427. goto out_free_filename;
  428. if (dso->has_build_id) {
  429. build_id__sprintf(dso->build_id,
  430. sizeof(dso->build_id), bf + 15);
  431. build_id_msg = bf;
  432. }
  433. err = -ENOENT;
  434. dso->annotate_warned = 1;
  435. pr_err("Can't annotate %s:\n\n"
  436. "No vmlinux file%s\nwas found in the path.\n\n"
  437. "Please use:\n\n"
  438. " perf buildid-cache -av vmlinux\n\n"
  439. "or:\n\n"
  440. " --vmlinux vmlinux\n",
  441. sym->name, build_id_msg ?: "");
  442. goto out_free_filename;
  443. }
  444. pr_debug("%s: filename=%s, sym=%s, start=%#" PRIx64 ", end=%#" PRIx64 "\n", __func__,
  445. filename, sym->name, map->unmap_ip(map, sym->start),
  446. map->unmap_ip(map, sym->end));
  447. pr_debug("annotating [%p] %30s : [%p] %30s\n",
  448. dso, dso->long_name, sym, sym->name);
  449. snprintf(command, sizeof(command),
  450. "objdump %s%s --start-address=0x%016" PRIx64
  451. " --stop-address=0x%016" PRIx64
  452. " -d %s %s -C %s|grep -v %s|expand",
  453. disassembler_style ? "-M " : "",
  454. disassembler_style ? disassembler_style : "",
  455. map__rip_2objdump(map, sym->start),
  456. map__rip_2objdump(map, sym->end+1),
  457. symbol_conf.annotate_asm_raw ? "" : "--no-show-raw",
  458. symbol_conf.annotate_src ? "-S" : "",
  459. symfs_filename, filename);
  460. pr_debug("Executing: %s\n", command);
  461. file = popen(command, "r");
  462. if (!file)
  463. goto out_free_filename;
  464. while (!feof(file))
  465. if (symbol__parse_objdump_line(sym, map, file, privsize) < 0)
  466. break;
  467. pclose(file);
  468. out_free_filename:
  469. if (free_filename)
  470. free(filename);
  471. return err;
  472. }
  473. static void insert_source_line(struct rb_root *root, struct source_line *src_line)
  474. {
  475. struct source_line *iter;
  476. struct rb_node **p = &root->rb_node;
  477. struct rb_node *parent = NULL;
  478. while (*p != NULL) {
  479. parent = *p;
  480. iter = rb_entry(parent, struct source_line, node);
  481. if (src_line->percent > iter->percent)
  482. p = &(*p)->rb_left;
  483. else
  484. p = &(*p)->rb_right;
  485. }
  486. rb_link_node(&src_line->node, parent, p);
  487. rb_insert_color(&src_line->node, root);
  488. }
  489. static void symbol__free_source_line(struct symbol *sym, int len)
  490. {
  491. struct annotation *notes = symbol__annotation(sym);
  492. struct source_line *src_line = notes->src->lines;
  493. int i;
  494. for (i = 0; i < len; i++)
  495. free(src_line[i].path);
  496. free(src_line);
  497. notes->src->lines = NULL;
  498. }
  499. /* Get the filename:line for the colored entries */
  500. static int symbol__get_source_line(struct symbol *sym, struct map *map,
  501. int evidx, struct rb_root *root, int len,
  502. const char *filename)
  503. {
  504. u64 start;
  505. int i;
  506. char cmd[PATH_MAX * 2];
  507. struct source_line *src_line;
  508. struct annotation *notes = symbol__annotation(sym);
  509. struct sym_hist *h = annotation__histogram(notes, evidx);
  510. if (!h->sum)
  511. return 0;
  512. src_line = notes->src->lines = calloc(len, sizeof(struct source_line));
  513. if (!notes->src->lines)
  514. return -1;
  515. start = map__rip_2objdump(map, sym->start);
  516. for (i = 0; i < len; i++) {
  517. char *path = NULL;
  518. size_t line_len;
  519. u64 offset;
  520. FILE *fp;
  521. src_line[i].percent = 100.0 * h->addr[i] / h->sum;
  522. if (src_line[i].percent <= 0.5)
  523. continue;
  524. offset = start + i;
  525. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  526. fp = popen(cmd, "r");
  527. if (!fp)
  528. continue;
  529. if (getline(&path, &line_len, fp) < 0 || !line_len)
  530. goto next;
  531. src_line[i].path = malloc(sizeof(char) * line_len + 1);
  532. if (!src_line[i].path)
  533. goto next;
  534. strcpy(src_line[i].path, path);
  535. insert_source_line(root, &src_line[i]);
  536. next:
  537. pclose(fp);
  538. }
  539. return 0;
  540. }
  541. static void print_summary(struct rb_root *root, const char *filename)
  542. {
  543. struct source_line *src_line;
  544. struct rb_node *node;
  545. printf("\nSorted summary for file %s\n", filename);
  546. printf("----------------------------------------------\n\n");
  547. if (RB_EMPTY_ROOT(root)) {
  548. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  549. return;
  550. }
  551. node = rb_first(root);
  552. while (node) {
  553. double percent;
  554. const char *color;
  555. char *path;
  556. src_line = rb_entry(node, struct source_line, node);
  557. percent = src_line->percent;
  558. color = get_percent_color(percent);
  559. path = src_line->path;
  560. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  561. node = rb_next(node);
  562. }
  563. }
  564. static void symbol__annotate_hits(struct symbol *sym, int evidx)
  565. {
  566. struct annotation *notes = symbol__annotation(sym);
  567. struct sym_hist *h = annotation__histogram(notes, evidx);
  568. u64 len = symbol__size(sym), offset;
  569. for (offset = 0; offset < len; ++offset)
  570. if (h->addr[offset] != 0)
  571. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  572. sym->start + offset, h->addr[offset]);
  573. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  574. }
  575. int symbol__annotate_printf(struct symbol *sym, struct map *map, int evidx,
  576. bool full_paths, int min_pcnt, int max_lines,
  577. int context)
  578. {
  579. struct dso *dso = map->dso;
  580. const char *filename = dso->long_name, *d_filename;
  581. struct annotation *notes = symbol__annotation(sym);
  582. struct disasm_line *pos, *queue = NULL;
  583. u64 start = map__rip_2objdump(map, sym->start);
  584. int printed = 2, queue_len = 0;
  585. int more = 0;
  586. u64 len;
  587. if (full_paths)
  588. d_filename = filename;
  589. else
  590. d_filename = basename(filename);
  591. len = symbol__size(sym);
  592. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  593. printf("------------------------------------------------\n");
  594. if (verbose)
  595. symbol__annotate_hits(sym, evidx);
  596. list_for_each_entry(pos, &notes->src->source, node) {
  597. if (context && queue == NULL) {
  598. queue = pos;
  599. queue_len = 0;
  600. }
  601. switch (disasm_line__print(pos, sym, start, evidx, len,
  602. min_pcnt, printed, max_lines,
  603. queue)) {
  604. case 0:
  605. ++printed;
  606. if (context) {
  607. printed += queue_len;
  608. queue = NULL;
  609. queue_len = 0;
  610. }
  611. break;
  612. case 1:
  613. /* filtered by max_lines */
  614. ++more;
  615. break;
  616. case -1:
  617. default:
  618. /*
  619. * Filtered by min_pcnt or non IP lines when
  620. * context != 0
  621. */
  622. if (!context)
  623. break;
  624. if (queue_len == context)
  625. queue = list_entry(queue->node.next, typeof(*queue), node);
  626. else
  627. ++queue_len;
  628. break;
  629. }
  630. }
  631. return more;
  632. }
  633. void symbol__annotate_zero_histogram(struct symbol *sym, int evidx)
  634. {
  635. struct annotation *notes = symbol__annotation(sym);
  636. struct sym_hist *h = annotation__histogram(notes, evidx);
  637. memset(h, 0, notes->src->sizeof_sym_hist);
  638. }
  639. void symbol__annotate_decay_histogram(struct symbol *sym, int evidx)
  640. {
  641. struct annotation *notes = symbol__annotation(sym);
  642. struct sym_hist *h = annotation__histogram(notes, evidx);
  643. int len = symbol__size(sym), offset;
  644. h->sum = 0;
  645. for (offset = 0; offset < len; ++offset) {
  646. h->addr[offset] = h->addr[offset] * 7 / 8;
  647. h->sum += h->addr[offset];
  648. }
  649. }
  650. void disasm__purge(struct list_head *head)
  651. {
  652. struct disasm_line *pos, *n;
  653. list_for_each_entry_safe(pos, n, head, node) {
  654. list_del(&pos->node);
  655. disasm_line__free(pos);
  656. }
  657. }
  658. static size_t disasm_line__fprintf(struct disasm_line *dl, FILE *fp)
  659. {
  660. size_t printed;
  661. if (dl->offset == -1)
  662. return fprintf(fp, "%s\n", dl->line);
  663. printed = fprintf(fp, "%#" PRIx64 " %s", dl->offset, dl->name);
  664. if (dl->ops.raw[0] != '\0') {
  665. printed += fprintf(fp, "%.*s %s\n", 6 - (int)printed, " ",
  666. dl->ops.raw);
  667. }
  668. return printed + fprintf(fp, "\n");
  669. }
  670. size_t disasm__fprintf(struct list_head *head, FILE *fp)
  671. {
  672. struct disasm_line *pos;
  673. size_t printed = 0;
  674. list_for_each_entry(pos, head, node)
  675. printed += disasm_line__fprintf(pos, fp);
  676. return printed;
  677. }
  678. int symbol__tty_annotate(struct symbol *sym, struct map *map, int evidx,
  679. bool print_lines, bool full_paths, int min_pcnt,
  680. int max_lines)
  681. {
  682. struct dso *dso = map->dso;
  683. const char *filename = dso->long_name;
  684. struct rb_root source_line = RB_ROOT;
  685. u64 len;
  686. if (symbol__annotate(sym, map, 0) < 0)
  687. return -1;
  688. len = symbol__size(sym);
  689. if (print_lines) {
  690. symbol__get_source_line(sym, map, evidx, &source_line,
  691. len, filename);
  692. print_summary(&source_line, filename);
  693. }
  694. symbol__annotate_printf(sym, map, evidx, full_paths,
  695. min_pcnt, max_lines, 0);
  696. if (print_lines)
  697. symbol__free_source_line(sym, len);
  698. disasm__purge(&symbol__annotation(sym)->src->source);
  699. return 0;
  700. }