builtin-annotate.c 8.1 KB

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