builtin-annotate.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 void annotate_sym(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;
  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. }
  257. static void hists__find_annotations(struct hists *self)
  258. {
  259. struct rb_node *nd;
  260. for (nd = rb_first(&self->entries); nd; nd = rb_next(nd)) {
  261. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  262. struct sym_priv *priv;
  263. if (he->ms.sym == NULL)
  264. continue;
  265. priv = symbol__priv(he->ms.sym);
  266. if (priv->hist == NULL)
  267. continue;
  268. annotate_sym(he);
  269. /*
  270. * Since we have a hist_entry per IP for the same symbol, free
  271. * he->ms.sym->hist to signal we already processed this symbol.
  272. */
  273. free(priv->hist);
  274. priv->hist = NULL;
  275. }
  276. }
  277. static struct perf_event_ops event_ops = {
  278. .sample = process_sample_event,
  279. .mmap = event__process_mmap,
  280. .comm = event__process_comm,
  281. .fork = event__process_task,
  282. };
  283. static int __cmd_annotate(void)
  284. {
  285. int ret;
  286. struct perf_session *session;
  287. session = perf_session__new(input_name, O_RDONLY, force, false);
  288. if (session == NULL)
  289. return -ENOMEM;
  290. ret = perf_session__process_events(session, &event_ops);
  291. if (ret)
  292. goto out_delete;
  293. if (dump_trace) {
  294. perf_session__fprintf_nr_events(session, stdout);
  295. goto out_delete;
  296. }
  297. if (verbose > 3)
  298. perf_session__fprintf(session, stdout);
  299. if (verbose > 2)
  300. perf_session__fprintf_dsos(session, stdout);
  301. hists__collapse_resort(&session->hists);
  302. hists__output_resort(&session->hists);
  303. hists__find_annotations(&session->hists);
  304. out_delete:
  305. perf_session__delete(session);
  306. return ret;
  307. }
  308. static const char * const annotate_usage[] = {
  309. "perf annotate [<options>] <command>",
  310. NULL
  311. };
  312. static const struct option options[] = {
  313. OPT_STRING('i', "input", &input_name, "file",
  314. "input file name"),
  315. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  316. "only consider symbols in these dsos"),
  317. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  318. "symbol to annotate"),
  319. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  320. OPT_INCR('v', "verbose", &verbose,
  321. "be more verbose (show symbol address, etc)"),
  322. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  323. "dump raw trace in ASCII"),
  324. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  325. "file", "vmlinux pathname"),
  326. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  327. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  328. OPT_BOOLEAN('l', "print-line", &print_line,
  329. "print matching source lines (may be slow)"),
  330. OPT_BOOLEAN('P', "full-paths", &full_paths,
  331. "Don't shorten the displayed pathnames"),
  332. OPT_END()
  333. };
  334. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  335. {
  336. argc = parse_options(argc, argv, options, annotate_usage, 0);
  337. symbol_conf.priv_size = sizeof(struct sym_priv);
  338. symbol_conf.try_vmlinux_path = true;
  339. if (symbol__init() < 0)
  340. return -1;
  341. setup_sorting(annotate_usage, options);
  342. if (argc) {
  343. /*
  344. * Special case: if there's an argument left then assume tha
  345. * it's a symbol filter:
  346. */
  347. if (argc > 1)
  348. usage_with_options(annotate_usage, options);
  349. sym_hist_filter = argv[0];
  350. }
  351. setup_pager();
  352. if (field_sep && *field_sep == '.') {
  353. pr_err("'.' is the only non valid --field-separator argument\n");
  354. return -1;
  355. }
  356. return __cmd_annotate();
  357. }