builtin-annotate.c 9.7 KB

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