builtin-annotate.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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 "util/block-range.h"
  31. #include <dlfcn.h>
  32. #include <errno.h>
  33. #include <linux/bitmap.h>
  34. struct perf_annotate {
  35. struct perf_tool tool;
  36. struct perf_session *session;
  37. bool use_tui, use_stdio, use_gtk;
  38. bool full_paths;
  39. bool print_line;
  40. bool skip_missing;
  41. const char *sym_hist_filter;
  42. const char *cpu_list;
  43. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  44. };
  45. /*
  46. * Given one basic block:
  47. *
  48. * from to branch_i
  49. * * ----> *
  50. * |
  51. * | block
  52. * v
  53. * * ----> *
  54. * from to branch_i+1
  55. *
  56. * where the horizontal are the branches and the vertical is the executed
  57. * block of instructions.
  58. *
  59. * We count, for each 'instruction', the number of blocks that covered it as
  60. * well as count the ratio each branch is taken.
  61. *
  62. * We can do this without knowing the actual instruction stream by keeping
  63. * track of the address ranges. We break down ranges such that there is no
  64. * overlap and iterate from the start until the end.
  65. *
  66. * @acme: once we parse the objdump output _before_ processing the samples,
  67. * we can easily fold the branch.cycles IPC bits in.
  68. */
  69. static void process_basic_block(struct addr_map_symbol *start,
  70. struct addr_map_symbol *end,
  71. struct branch_flags *flags)
  72. {
  73. struct symbol *sym = start->sym;
  74. struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
  75. struct block_range_iter iter;
  76. struct block_range *entry;
  77. /*
  78. * Sanity; NULL isn't executable and the CPU cannot execute backwards
  79. */
  80. if (!start->addr || start->addr > end->addr)
  81. return;
  82. iter = block_range__create(start->addr, end->addr);
  83. if (!block_range_iter__valid(&iter))
  84. return;
  85. /*
  86. * First block in range is a branch target.
  87. */
  88. entry = block_range_iter(&iter);
  89. assert(entry->is_target);
  90. entry->entry++;
  91. do {
  92. entry = block_range_iter(&iter);
  93. entry->coverage++;
  94. entry->sym = sym;
  95. if (notes)
  96. notes->max_coverage = max(notes->max_coverage, entry->coverage);
  97. } while (block_range_iter__next(&iter));
  98. /*
  99. * Last block in rage is a branch.
  100. */
  101. entry = block_range_iter(&iter);
  102. assert(entry->is_branch);
  103. entry->taken++;
  104. if (flags->predicted)
  105. entry->pred++;
  106. }
  107. static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
  108. struct perf_sample *sample)
  109. {
  110. struct addr_map_symbol *prev = NULL;
  111. struct branch_info *bi;
  112. int i;
  113. if (!bs || !bs->nr)
  114. return;
  115. bi = sample__resolve_bstack(sample, al);
  116. if (!bi)
  117. return;
  118. for (i = bs->nr - 1; i >= 0; i--) {
  119. /*
  120. * XXX filter against symbol
  121. */
  122. if (prev)
  123. process_basic_block(prev, &bi[i].from, &bi[i].flags);
  124. prev = &bi[i].to;
  125. }
  126. free(bi);
  127. }
  128. static int perf_evsel__add_sample(struct perf_evsel *evsel,
  129. struct perf_sample *sample,
  130. struct addr_location *al,
  131. struct perf_annotate *ann)
  132. {
  133. struct hists *hists = evsel__hists(evsel);
  134. struct hist_entry *he;
  135. int ret;
  136. if (ann->sym_hist_filter != NULL &&
  137. (al->sym == NULL ||
  138. strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
  139. /* We're only interested in a symbol named sym_hist_filter */
  140. /*
  141. * FIXME: why isn't this done in the symbol_filter when loading
  142. * the DSO?
  143. */
  144. if (al->sym != NULL) {
  145. rb_erase(&al->sym->rb_node,
  146. &al->map->dso->symbols[al->map->type]);
  147. symbol__delete(al->sym);
  148. dso__reset_find_symbol_cache(al->map->dso);
  149. }
  150. return 0;
  151. }
  152. /*
  153. * XXX filtered samples can still have branch entires pointing into our
  154. * symbol and are missed.
  155. */
  156. process_branch_stack(sample->branch_stack, al, sample);
  157. he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
  158. if (he == NULL)
  159. return -ENOMEM;
  160. ret = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
  161. hists__inc_nr_samples(hists, true);
  162. return ret;
  163. }
  164. static int process_sample_event(struct perf_tool *tool,
  165. union perf_event *event,
  166. struct perf_sample *sample,
  167. struct perf_evsel *evsel,
  168. struct machine *machine)
  169. {
  170. struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
  171. struct addr_location al;
  172. int ret = 0;
  173. if (machine__resolve(machine, &al, sample) < 0) {
  174. pr_warning("problem processing %d event, skipping it.\n",
  175. event->header.type);
  176. return -1;
  177. }
  178. if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
  179. goto out_put;
  180. if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
  181. pr_warning("problem incrementing symbol count, "
  182. "skipping event\n");
  183. ret = -1;
  184. }
  185. out_put:
  186. addr_location__put(&al);
  187. return ret;
  188. }
  189. static int hist_entry__tty_annotate(struct hist_entry *he,
  190. struct perf_evsel *evsel,
  191. struct perf_annotate *ann)
  192. {
  193. return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel,
  194. ann->print_line, ann->full_paths, 0, 0);
  195. }
  196. static void hists__find_annotations(struct hists *hists,
  197. struct perf_evsel *evsel,
  198. struct perf_annotate *ann)
  199. {
  200. struct rb_node *nd = rb_first(&hists->entries), *next;
  201. int key = K_RIGHT;
  202. while (nd) {
  203. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  204. struct annotation *notes;
  205. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  206. goto find_next;
  207. notes = symbol__annotation(he->ms.sym);
  208. if (notes->src == NULL) {
  209. find_next:
  210. if (key == K_LEFT)
  211. nd = rb_prev(nd);
  212. else
  213. nd = rb_next(nd);
  214. continue;
  215. }
  216. if (use_browser == 2) {
  217. int ret;
  218. int (*annotate)(struct hist_entry *he,
  219. struct perf_evsel *evsel,
  220. struct hist_browser_timer *hbt);
  221. annotate = dlsym(perf_gtk_handle,
  222. "hist_entry__gtk_annotate");
  223. if (annotate == NULL) {
  224. ui__error("GTK browser not found!\n");
  225. return;
  226. }
  227. ret = annotate(he, evsel, NULL);
  228. if (!ret || !ann->skip_missing)
  229. return;
  230. /* skip missing symbols */
  231. nd = rb_next(nd);
  232. } else if (use_browser == 1) {
  233. key = hist_entry__tui_annotate(he, evsel, NULL);
  234. switch (key) {
  235. case -1:
  236. if (!ann->skip_missing)
  237. return;
  238. /* fall through */
  239. case K_RIGHT:
  240. next = rb_next(nd);
  241. break;
  242. case K_LEFT:
  243. next = rb_prev(nd);
  244. break;
  245. default:
  246. return;
  247. }
  248. if (next != NULL)
  249. nd = next;
  250. } else {
  251. hist_entry__tty_annotate(he, evsel, ann);
  252. nd = rb_next(nd);
  253. /*
  254. * Since we have a hist_entry per IP for the same
  255. * symbol, free he->ms.sym->src to signal we already
  256. * processed this symbol.
  257. */
  258. zfree(&notes->src->cycles_hist);
  259. zfree(&notes->src);
  260. }
  261. }
  262. }
  263. static int __cmd_annotate(struct perf_annotate *ann)
  264. {
  265. int ret;
  266. struct perf_session *session = ann->session;
  267. struct perf_evsel *pos;
  268. u64 total_nr_samples;
  269. if (ann->cpu_list) {
  270. ret = perf_session__cpu_bitmap(session, ann->cpu_list,
  271. ann->cpu_bitmap);
  272. if (ret)
  273. goto out;
  274. }
  275. if (!objdump_path) {
  276. ret = perf_env__lookup_objdump(&session->header.env);
  277. if (ret)
  278. goto out;
  279. }
  280. ret = perf_session__process_events(session);
  281. if (ret)
  282. goto out;
  283. if (dump_trace) {
  284. perf_session__fprintf_nr_events(session, stdout);
  285. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  286. goto out;
  287. }
  288. if (verbose > 3)
  289. perf_session__fprintf(session, stdout);
  290. if (verbose > 2)
  291. perf_session__fprintf_dsos(session, stdout);
  292. total_nr_samples = 0;
  293. evlist__for_each_entry(session->evlist, pos) {
  294. struct hists *hists = evsel__hists(pos);
  295. u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  296. if (nr_samples > 0) {
  297. total_nr_samples += nr_samples;
  298. hists__collapse_resort(hists, NULL);
  299. /* Don't sort callchain */
  300. perf_evsel__reset_sample_bit(pos, CALLCHAIN);
  301. perf_evsel__output_resort(pos, NULL);
  302. if (symbol_conf.event_group &&
  303. !perf_evsel__is_group_leader(pos))
  304. continue;
  305. hists__find_annotations(hists, pos, ann);
  306. }
  307. }
  308. if (total_nr_samples == 0) {
  309. ui__error("The %s file has no samples!\n", session->file->path);
  310. goto out;
  311. }
  312. if (use_browser == 2) {
  313. void (*show_annotations)(void);
  314. show_annotations = dlsym(perf_gtk_handle,
  315. "perf_gtk__show_annotations");
  316. if (show_annotations == NULL) {
  317. ui__error("GTK browser not found!\n");
  318. goto out;
  319. }
  320. show_annotations();
  321. }
  322. out:
  323. return ret;
  324. }
  325. static const char * const annotate_usage[] = {
  326. "perf annotate [<options>]",
  327. NULL
  328. };
  329. int cmd_annotate(int argc, const char **argv)
  330. {
  331. struct perf_annotate annotate = {
  332. .tool = {
  333. .sample = process_sample_event,
  334. .mmap = perf_event__process_mmap,
  335. .mmap2 = perf_event__process_mmap2,
  336. .comm = perf_event__process_comm,
  337. .exit = perf_event__process_exit,
  338. .fork = perf_event__process_fork,
  339. .namespaces = perf_event__process_namespaces,
  340. .attr = perf_event__process_attr,
  341. .build_id = perf_event__process_build_id,
  342. .tracing_data = perf_event__process_tracing_data,
  343. .feature = perf_event__process_feature,
  344. .ordered_events = true,
  345. .ordering_requires_timestamps = true,
  346. },
  347. };
  348. struct perf_data_file file = {
  349. .mode = PERF_DATA_MODE_READ,
  350. };
  351. struct option options[] = {
  352. OPT_STRING('i', "input", &input_name, "file",
  353. "input file name"),
  354. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  355. "only consider symbols in these dsos"),
  356. OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
  357. "symbol to annotate"),
  358. OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
  359. OPT_INCR('v', "verbose", &verbose,
  360. "be more verbose (show symbol address, etc)"),
  361. OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
  362. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  363. "dump raw trace in ASCII"),
  364. OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
  365. OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
  366. OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
  367. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  368. "file", "vmlinux pathname"),
  369. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  370. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  371. OPT_BOOLEAN('l', "print-line", &annotate.print_line,
  372. "print matching source lines (may be slow)"),
  373. OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
  374. "Don't shorten the displayed pathnames"),
  375. OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
  376. "Skip symbols that cannot be annotated"),
  377. OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
  378. OPT_CALLBACK(0, "symfs", NULL, "directory",
  379. "Look for files with symbols relative to this directory",
  380. symbol__config_symfs),
  381. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  382. "Interleave source code with assembly code (default)"),
  383. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  384. "Display raw encoding of assembly instructions (default)"),
  385. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  386. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  387. OPT_STRING(0, "objdump", &objdump_path, "path",
  388. "objdump binary to use for disassembly and annotations"),
  389. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  390. "Show event group information together"),
  391. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  392. "Show a column with the sum of periods"),
  393. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  394. "Show a column with the number of samples"),
  395. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  396. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  397. stdio__config_color, "always"),
  398. OPT_END()
  399. };
  400. int ret;
  401. set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
  402. set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
  403. ret = hists__init();
  404. if (ret < 0)
  405. return ret;
  406. argc = parse_options(argc, argv, options, annotate_usage, 0);
  407. if (argc) {
  408. /*
  409. * Special case: if there's an argument left then assume that
  410. * it's a symbol filter:
  411. */
  412. if (argc > 1)
  413. usage_with_options(annotate_usage, options);
  414. annotate.sym_hist_filter = argv[0];
  415. }
  416. if (symbol_conf.show_nr_samples && !annotate.use_stdio) {
  417. pr_err("--show-nr-samples is only available in --stdio mode at this time\n");
  418. return ret;
  419. }
  420. if (quiet)
  421. perf_quiet_option();
  422. file.path = input_name;
  423. annotate.session = perf_session__new(&file, false, &annotate.tool);
  424. if (annotate.session == NULL)
  425. return -1;
  426. ret = symbol__annotation_init();
  427. if (ret < 0)
  428. goto out_delete;
  429. symbol_conf.try_vmlinux_path = true;
  430. ret = symbol__init(&annotate.session->header.env);
  431. if (ret < 0)
  432. goto out_delete;
  433. if (setup_sorting(NULL) < 0)
  434. usage_with_options(annotate_usage, options);
  435. if (annotate.use_stdio)
  436. use_browser = 0;
  437. else if (annotate.use_tui)
  438. use_browser = 1;
  439. else if (annotate.use_gtk)
  440. use_browser = 2;
  441. setup_browser(true);
  442. ret = __cmd_annotate(&annotate);
  443. out_delete:
  444. /*
  445. * Speed up the exit process, for large files this can
  446. * take quite a while.
  447. *
  448. * XXX Enable this when using valgrind or if we ever
  449. * librarize this command.
  450. *
  451. * Also experiment with obstacks to see how much speed
  452. * up we'll get here.
  453. *
  454. * perf_session__delete(session);
  455. */
  456. return ret;
  457. }