builtin-annotate.c 7.8 KB

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