builtin-annotate.c 9.0 KB

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