annotate.c 20 KB

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