builtin-annotate.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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/util.h"
  11. #include "util/color.h"
  12. #include <linux/list.h>
  13. #include "util/cache.h"
  14. #include <linux/rbtree.h>
  15. #include "util/symbol.h"
  16. #include "perf.h"
  17. #include "util/debug.h"
  18. #include "util/evlist.h"
  19. #include "util/evsel.h"
  20. #include "util/annotate.h"
  21. #include "util/event.h"
  22. #include "util/parse-options.h"
  23. #include "util/parse-events.h"
  24. #include "util/thread.h"
  25. #include "util/sort.h"
  26. #include "util/hist.h"
  27. #include "util/session.h"
  28. static char const *input_name = "perf.data";
  29. static bool force, use_tui, use_stdio;
  30. static bool full_paths;
  31. static bool print_line;
  32. static const char *sym_hist_filter;
  33. static int perf_evlist__add_sample(struct perf_evlist *evlist,
  34. struct perf_sample *sample,
  35. struct addr_location *al)
  36. {
  37. struct perf_evsel *evsel;
  38. struct hist_entry *he;
  39. int ret;
  40. if (sym_hist_filter != NULL &&
  41. (al->sym == NULL || strcmp(sym_hist_filter, al->sym->name) != 0)) {
  42. /* We're only interested in a symbol named sym_hist_filter */
  43. if (al->sym != NULL) {
  44. rb_erase(&al->sym->rb_node,
  45. &al->map->dso->symbols[al->map->type]);
  46. symbol__delete(al->sym);
  47. }
  48. return 0;
  49. }
  50. evsel = perf_evlist__id2evsel(evlist, sample->id);
  51. if (evsel == NULL) {
  52. /*
  53. * FIXME: Propagate this back, but at least we're in a builtin,
  54. * where exit() is allowed. ;-)
  55. */
  56. ui__warning("Invalid %s file, contains samples with id not in "
  57. "its header!\n", input_name);
  58. exit_browser(0);
  59. exit(1);
  60. }
  61. he = __hists__add_entry(&evsel->hists, al, NULL, 1);
  62. if (he == NULL)
  63. return -ENOMEM;
  64. ret = 0;
  65. if (he->ms.sym != NULL) {
  66. struct annotation *notes = symbol__annotation(he->ms.sym);
  67. if (notes->src == NULL &&
  68. symbol__alloc_hist(he->ms.sym, evlist->nr_entries) < 0)
  69. return -ENOMEM;
  70. ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
  71. }
  72. evsel->hists.stats.total_period += sample->period;
  73. hists__inc_nr_events(&evsel->hists, PERF_RECORD_SAMPLE);
  74. return ret;
  75. }
  76. static int process_sample_event(union perf_event *event,
  77. struct perf_sample *sample,
  78. struct perf_session *session)
  79. {
  80. struct addr_location al;
  81. if (perf_event__preprocess_sample(event, session, &al, sample,
  82. symbol__annotate_init) < 0) {
  83. pr_warning("problem processing %d event, skipping it.\n",
  84. event->header.type);
  85. return -1;
  86. }
  87. if (!al.filtered && perf_evlist__add_sample(session->evlist, sample, &al)) {
  88. pr_warning("problem incrementing symbol count, "
  89. "skipping event\n");
  90. return -1;
  91. }
  92. return 0;
  93. }
  94. static int hist_entry__tty_annotate(struct hist_entry *he, int evidx)
  95. {
  96. return symbol__tty_annotate(he->ms.sym, he->ms.map, evidx,
  97. print_line, full_paths, 0, 0);
  98. }
  99. static void hists__find_annotations(struct hists *self, int evidx)
  100. {
  101. struct rb_node *nd = rb_first(&self->entries), *next;
  102. int key = KEY_RIGHT;
  103. while (nd) {
  104. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  105. struct annotation *notes;
  106. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  107. goto find_next;
  108. notes = symbol__annotation(he->ms.sym);
  109. if (notes->src == NULL) {
  110. find_next:
  111. if (key == KEY_LEFT)
  112. nd = rb_prev(nd);
  113. else
  114. nd = rb_next(nd);
  115. continue;
  116. }
  117. if (use_browser > 0) {
  118. key = hist_entry__tui_annotate(he, evidx);
  119. switch (key) {
  120. case KEY_RIGHT:
  121. next = rb_next(nd);
  122. break;
  123. case KEY_LEFT:
  124. next = rb_prev(nd);
  125. break;
  126. default:
  127. return;
  128. }
  129. if (next != NULL)
  130. nd = next;
  131. } else {
  132. hist_entry__tty_annotate(he, evidx);
  133. nd = rb_next(nd);
  134. /*
  135. * Since we have a hist_entry per IP for the same
  136. * symbol, free he->ms.sym->src to signal we already
  137. * processed this symbol.
  138. */
  139. free(notes->src);
  140. notes->src = NULL;
  141. }
  142. }
  143. }
  144. static struct perf_event_ops event_ops = {
  145. .sample = process_sample_event,
  146. .mmap = perf_event__process_mmap,
  147. .comm = perf_event__process_comm,
  148. .fork = perf_event__process_task,
  149. .ordered_samples = true,
  150. .ordering_requires_timestamps = true,
  151. };
  152. static int __cmd_annotate(void)
  153. {
  154. int ret;
  155. struct perf_session *session;
  156. struct perf_evsel *pos;
  157. u64 total_nr_samples;
  158. session = perf_session__new(input_name, O_RDONLY, force, false, &event_ops);
  159. if (session == NULL)
  160. return -ENOMEM;
  161. ret = perf_session__process_events(session, &event_ops);
  162. if (ret)
  163. goto out_delete;
  164. if (dump_trace) {
  165. perf_session__fprintf_nr_events(session, stdout);
  166. goto out_delete;
  167. }
  168. if (verbose > 3)
  169. perf_session__fprintf(session, stdout);
  170. if (verbose > 2)
  171. perf_session__fprintf_dsos(session, stdout);
  172. total_nr_samples = 0;
  173. list_for_each_entry(pos, &session->evlist->entries, node) {
  174. struct hists *hists = &pos->hists;
  175. u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  176. if (nr_samples > 0) {
  177. total_nr_samples += nr_samples;
  178. hists__collapse_resort(hists);
  179. hists__output_resort(hists);
  180. hists__find_annotations(hists, pos->idx);
  181. }
  182. }
  183. if (total_nr_samples == 0) {
  184. ui__warning("The %s file has no samples!\n", input_name);
  185. goto out_delete;
  186. }
  187. out_delete:
  188. /*
  189. * Speed up the exit process, for large files this can
  190. * take quite a while.
  191. *
  192. * XXX Enable this when using valgrind or if we ever
  193. * librarize this command.
  194. *
  195. * Also experiment with obstacks to see how much speed
  196. * up we'll get here.
  197. *
  198. * perf_session__delete(session);
  199. */
  200. return ret;
  201. }
  202. static const char * const annotate_usage[] = {
  203. "perf annotate [<options>] <command>",
  204. NULL
  205. };
  206. static const struct option options[] = {
  207. OPT_STRING('i', "input", &input_name, "file",
  208. "input file name"),
  209. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  210. "only consider symbols in these dsos"),
  211. OPT_STRING('s', "symbol", &sym_hist_filter, "symbol",
  212. "symbol to annotate"),
  213. OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
  214. OPT_INCR('v', "verbose", &verbose,
  215. "be more verbose (show symbol address, etc)"),
  216. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  217. "dump raw trace in ASCII"),
  218. OPT_BOOLEAN(0, "tui", &use_tui, "Use the TUI interface"),
  219. OPT_BOOLEAN(0, "stdio", &use_stdio, "Use the stdio interface"),
  220. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  221. "file", "vmlinux pathname"),
  222. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  223. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  224. OPT_BOOLEAN('l', "print-line", &print_line,
  225. "print matching source lines (may be slow)"),
  226. OPT_BOOLEAN('P', "full-paths", &full_paths,
  227. "Don't shorten the displayed pathnames"),
  228. OPT_END()
  229. };
  230. int cmd_annotate(int argc, const char **argv, const char *prefix __used)
  231. {
  232. argc = parse_options(argc, argv, options, annotate_usage, 0);
  233. if (use_stdio)
  234. use_browser = 0;
  235. else if (use_tui)
  236. use_browser = 1;
  237. setup_browser(true);
  238. symbol_conf.priv_size = sizeof(struct annotation);
  239. symbol_conf.try_vmlinux_path = true;
  240. if (symbol__init() < 0)
  241. return -1;
  242. setup_sorting(annotate_usage, options);
  243. if (argc) {
  244. /*
  245. * Special case: if there's an argument left then assume tha
  246. * it's a symbol filter:
  247. */
  248. if (argc > 1)
  249. usage_with_options(annotate_usage, options);
  250. sym_hist_filter = argv[0];
  251. }
  252. if (field_sep && *field_sep == '.') {
  253. pr_err("'.' is the only non valid --field-separator argument\n");
  254. return -1;
  255. }
  256. return __cmd_annotate();
  257. }