builtin-annotate.c 11 KB

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