annotate.c 20 KB

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