builtin-annotate.c 7.1 KB

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