builtin-annotate.c 9.8 KB

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