annotate.c 19 KB

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