builtin-annotate.c 10 KB

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