builtin-annotate.c 9.7 KB

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