builtin-annotate.c 8.7 KB

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