builtin-annotate.c 16 KB

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