builtin-annotate.c 13 KB

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