builtin-annotate.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * builtin-annotate.c
  3. *
  4. * Builtin annotate command: Analyze the perf.data input file,
  5. * look up and read DSOs and symbol information and display
  6. * a histogram of results, along various sorting keys.
  7. */
  8. #include "builtin.h"
  9. #include "util/util.h"
  10. #include "util/color.h"
  11. #include <linux/list.h>
  12. #include "util/cache.h"
  13. #include <linux/rbtree.h>
  14. #include "util/symbol.h"
  15. #include "perf.h"
  16. #include "util/debug.h"
  17. #include "util/event.h"
  18. #include "util/parse-options.h"
  19. #include "util/parse-events.h"
  20. #include "util/thread.h"
  21. #include "util/sort.h"
  22. #include "util/hist.h"
  23. #include "util/session.h"
  24. static char const *input_name = "perf.data";
  25. static bool force, use_tui, use_stdio;
  26. static bool full_paths;
  27. static bool print_line;
  28. static const char *sym_hist_filter;
  29. static int hists__add_entry(struct hists *self, struct addr_location *al)
  30. {
  31. struct hist_entry *he;
  32. if (sym_hist_filter != NULL &&
  33. (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
  34. /* We're only interested in a symbol named sym_hist_filter */
  35. if (al->sym != NULL) {
  36. rb_erase(&al->sym->rb_node,
  37. &al->map->dso->symbols[al->map->type]);
  38. symbol__delete(al->sym);
  39. }
  40. return 0;
  41. }
  42. he = __hists__add_entry(self, al, NULL, 1);
  43. if (he == NULL)
  44. return -ENOMEM;
  45. return hist_entry__inc_addr_samples(he, al->addr);
  46. }
  47. static int process_sample_event(event_t *event, struct perf_sample *sample,
  48. struct perf_session *session)
  49. {
  50. struct addr_location al;
  51. if (event__preprocess_sample(event, session, &al, sample, NULL) < 0) {
  52. pr_warning("problem processing %d event, skipping it.\n",
  53. event->header.type);
  54. return -1;
  55. }
  56. if (!al.filtered && hists__add_entry(&session->hists, &al)) {
  57. pr_warning("problem incrementing symbol count, "
  58. "skipping event\n");
  59. return -1;
  60. }
  61. return 0;
  62. }
  63. static int objdump_line__print(struct objdump_line *self,
  64. struct list_head *head,
  65. struct hist_entry *he, u64 len)
  66. {
  67. struct symbol *sym = he->ms.sym;
  68. static const char *prev_line;
  69. static const char *prev_color;
  70. if (self->offset != -1) {
  71. const char *path = NULL;
  72. unsigned int hits = 0;
  73. double percent = 0.0;
  74. const char *color;
  75. struct sym_priv *priv = symbol__priv(sym);
  76. struct sym_ext *sym_ext = priv->ext;
  77. struct sym_hist *h = priv->hist;
  78. s64 offset = self->offset;
  79. struct objdump_line *next = objdump__get_next_ip_line(head, self);
  80. while (offset < (s64)len &&
  81. (next == NULL || offset < next->offset)) {
  82. if (sym_ext) {
  83. if (path == NULL)
  84. path = sym_ext[offset].path;
  85. percent += sym_ext[offset].percent;
  86. } else
  87. hits += h->ip[offset];
  88. ++offset;
  89. }
  90. if (sym_ext == NULL && h->sum)
  91. percent = 100.0 * hits / h->sum;
  92. color = get_percent_color(percent);
  93. /*
  94. * Also color the filename and line if needed, with
  95. * the same color than the percentage. Don't print it
  96. * twice for close colored ip with the same filename:line
  97. */
  98. if (path) {
  99. if (!prev_line || strcmp(prev_line, path)
  100. || color != prev_color) {
  101. color_fprintf(stdout, color, " %s", path);
  102. prev_line = path;
  103. prev_color = color;
  104. }
  105. }
  106. color_fprintf(stdout, color, " %7.2f", percent);
  107. printf(" : ");
  108. color_fprintf(stdout, PERF_COLOR_BLUE, "%s\n", self->line);
  109. } else {
  110. if (!*self->line)
  111. printf(" :\n");
  112. else
  113. printf(" : %s\n", self->line);
  114. }
  115. return 0;
  116. }
  117. static struct rb_root root_sym_ext;
  118. static void insert_source_line(struct sym_ext *sym_ext)
  119. {
  120. struct sym_ext *iter;
  121. struct rb_node **p = &root_sym_ext.rb_node;
  122. struct rb_node *parent = NULL;
  123. while (*p != NULL) {
  124. parent = *p;
  125. iter = rb_entry(parent, struct sym_ext, node);
  126. if (sym_ext->percent > iter->percent)
  127. p = &(*p)->rb_left;
  128. else
  129. p = &(*p)->rb_right;
  130. }
  131. rb_link_node(&sym_ext->node, parent, p);
  132. rb_insert_color(&sym_ext->node, &root_sym_ext);
  133. }
  134. static void free_source_line(struct hist_entry *he, int len)
  135. {
  136. struct sym_priv *priv = symbol__priv(he->ms.sym);
  137. struct sym_ext *sym_ext = priv->ext;
  138. int i;
  139. if (!sym_ext)
  140. return;
  141. for (i = 0; i < len; i++)
  142. free(sym_ext[i].path);
  143. free(sym_ext);
  144. priv->ext = NULL;
  145. root_sym_ext = RB_ROOT;
  146. }
  147. /* Get the filename:line for the colored entries */
  148. static void
  149. get_source_line(struct hist_entry *he, int len, const char *filename)
  150. {
  151. struct symbol *sym = he->ms.sym;
  152. u64 start;
  153. int i;
  154. char cmd[PATH_MAX * 2];
  155. struct sym_ext *sym_ext;
  156. struct sym_priv *priv = symbol__priv(sym);
  157. struct sym_hist *h = priv->hist;
  158. if (!h->sum)
  159. return;
  160. sym_ext = priv->ext = calloc(len, sizeof(struct sym_ext));
  161. if (!priv->ext)
  162. return;
  163. start = he->ms.map->unmap_ip(he->ms.map, sym->start);
  164. for (i = 0; i < len; i++) {
  165. char *path = NULL;
  166. size_t line_len;
  167. u64 offset;
  168. FILE *fp;
  169. sym_ext[i].percent = 100.0 * h->ip[i] / h->sum;
  170. if (sym_ext[i].percent <= 0.5)
  171. continue;
  172. offset = start + i;
  173. sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
  174. fp = popen(cmd, "r");
  175. if (!fp)
  176. continue;
  177. if (getline(&path, &line_len, fp) < 0 || !line_len)
  178. goto next;
  179. sym_ext[i].path = malloc(sizeof(char) * line_len + 1);
  180. if (!sym_ext[i].path)
  181. goto next;
  182. strcpy(sym_ext[i].path, path);
  183. insert_source_line(&sym_ext[i]);
  184. next:
  185. pclose(fp);
  186. }
  187. }
  188. static void print_summary(const char *filename)
  189. {
  190. struct sym_ext *sym_ext;
  191. struct rb_node *node;
  192. printf("\nSorted summary for file %s\n", filename);
  193. printf("----------------------------------------------\n\n");
  194. if (RB_EMPTY_ROOT(&root_sym_ext)) {
  195. printf(" Nothing higher than %1.1f%%\n", MIN_GREEN);
  196. return;
  197. }
  198. node = rb_first(&root_sym_ext);
  199. while (node) {
  200. double percent;
  201. const char *color;
  202. char *path;
  203. sym_ext = rb_entry(node, struct sym_ext, node);
  204. percent = sym_ext->percent;
  205. color = get_percent_color(percent);
  206. path = sym_ext->path;
  207. color_fprintf(stdout, color, " %7.2f %s", percent, path);
  208. node = rb_next(node);
  209. }
  210. }
  211. static void hist_entry__print_hits(struct hist_entry *self)
  212. {
  213. struct symbol *sym = self->ms.sym;
  214. struct sym_priv *priv = symbol__priv(sym);
  215. struct sym_hist *h = priv->hist;
  216. u64 len = sym->end - sym->start, offset;
  217. for (offset = 0; offset < len; ++offset)
  218. if (h->ip[offset] != 0)
  219. printf("%*" PRIx64 ": %" PRIu64 "\n", BITS_PER_LONG / 2,
  220. sym->start + offset, h->ip[offset]);
  221. printf("%*s: %" PRIu64 "\n", BITS_PER_LONG / 2, "h->sum", h->sum);
  222. }
  223. static int hist_entry__tty_annotate(struct hist_entry *he)
  224. {
  225. struct map *map = he->ms.map;
  226. struct dso *dso = map->dso;
  227. struct symbol *sym = he->ms.sym;
  228. const char *filename = dso->long_name, *d_filename;
  229. u64 len;
  230. LIST_HEAD(head);
  231. struct objdump_line *pos, *n;
  232. if (hist_entry__annotate(he, &head, 0) < 0)
  233. return -1;
  234. if (full_paths)
  235. d_filename = filename;
  236. else
  237. d_filename = basename(filename);
  238. len = sym->end - sym->start;
  239. if (print_line) {
  240. get_source_line(he, len, filename);
  241. print_summary(filename);
  242. }
  243. printf("\n\n------------------------------------------------\n");
  244. printf(" Percent | Source code & Disassembly of %s\n", d_filename);
  245. printf("------------------------------------------------\n");
  246. if (verbose)
  247. hist_entry__print_hits(he);
  248. list_for_each_entry_safe(pos, n, &head, node) {
  249. objdump_line__print(pos, &head, he, len);
  250. list_del(&pos->node);
  251. objdump_line__free(pos);
  252. }
  253. if (print_line)
  254. free_source_line(he, len);
  255. return 0;
  256. }
  257. static void hists__find_annotations(struct hists *self)
  258. {
  259. struct rb_node *nd = rb_first(&self->entries), *next;
  260. int key = KEY_RIGHT;
  261. while (nd) {
  262. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  263. struct sym_priv *priv;
  264. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  265. goto find_next;
  266. priv = symbol__priv(he->ms.sym);
  267. if (priv->hist == NULL) {
  268. find_next:
  269. if (key == KEY_LEFT)
  270. nd = rb_prev(nd);
  271. else
  272. nd = rb_next(nd);
  273. continue;
  274. }
  275. if (use_browser > 0) {
  276. key = hist_entry__tui_annotate(he);
  277. switch (key) {
  278. case KEY_RIGHT:
  279. next = rb_next(nd);
  280. break;
  281. case KEY_LEFT:
  282. next = rb_prev(nd);
  283. break;
  284. default:
  285. return;
  286. }
  287. if (next != NULL)
  288. nd = next;
  289. } else {
  290. hist_entry__tty_annotate(he);
  291. nd = rb_next(nd);
  292. /*
  293. * Since we have a hist_entry per IP for the same
  294. * symbol, free he->ms.sym->hist to signal we already
  295. * processed this symbol.
  296. */
  297. free(priv->hist);
  298. priv->hist = NULL;
  299. }
  300. }
  301. }
  302. static struct perf_event_ops event_ops = {
  303. .sample = process_sample_event,
  304. .mmap = event__process_mmap,
  305. .comm = event__process_comm,
  306. .fork = event__process_task,
  307. .ordered_samples = true,
  308. .ordering_requires_timestamps = true,
  309. };
  310. static int __cmd_annotate(void)
  311. {
  312. int ret;
  313. struct perf_session *session;
  314. session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
  315. if (session == NULL)
  316. return -ENOMEM;
  317. ret = perf_session__process_events(session, &event_ops);
  318. if (ret)
  319. goto out_delete;
  320. if (dump_trace) {
  321. perf_session__fprintf_nr_events(session, stdout);
  322. goto out_delete;
  323. }
  324. if (verbose > 3)
  325. perf_session__fprintf(session, stdout);
  326. if (verbose > 2)
  327. perf_session__fprintf_dsos(session, stdout);
  328. hists__collapse_resort(&session->hists);
  329. hists__output_resort(&session->hists);
  330. hists__find_annotations(&session->hists);
  331. out_delete:
  332. perf_session__delete(session);
  333. return ret;
  334. }
  335. static const char * const annotate_usage[] = {
  336. "perf annotate [<options>] <command>",
  337. NULL
  338. };
  339. static const struct option options[] = {
  340. OPT_STRING('i', "input", &input_name, "file",
  341. "input file name"),
  342. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  343. "only consider symbols in these dsos"),
  344. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  345. "symbol to annotate"),
  346. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  347. OPT_INCR('v', "verbose", &verbose,
  348. "be more verbose (show symbol address, etc)"),
  349. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  350. "dump raw trace in ASCII"),
  351. OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
  352. OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
  353. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  354. "file", "vmlinux pathname"),
  355. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  356. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  357. OPT_BOOLEAN('l', "print-line", &print_line,
  358. "print matching source lines (may be slow)"),
  359. OPT_BOOLEAN('P', "full-paths", &full_paths,
  360. "Don't shorten the displayed pathnames"),
  361. OPT_END()
  362. };
  363. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  364. {
  365. argc = parse_options(argc, argv, options, annotate_usage, 0);
  366. if (use_stdio)
  367. use_browser = 0;
  368. else if (use_tui)
  369. use_browser = 1;
  370. setup_browser();
  371. symbol_conf.priv_size = sizeof(struct sym_priv);
  372. symbol_conf.try_vmlinux_path = true;
  373. if (symbol__init() < 0)
  374. return -1;
  375. setup_sorting(annotate_usage, options);
  376. if (argc) {
  377. /*
  378. * Special case: if there's an argument left then assume tha
  379. * it's a symbol filter:
  380. */
  381. if (argc > 1)
  382. usage_with_options(annotate_usage, options);
  383. sym_hist_filter = argv[0];
  384. }
  385. if (field_sep && *field_sep == '.') {
  386. pr_err("'.' is the only non valid --field-separator argument\n");
  387. return -1;
  388. }
  389. return __cmd_annotate();
  390. }