annotate.c 20 KB

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