annotate.c 21 KB

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