builtin-annotate.c 7.1 KB

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