annotate.c 18 KB

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