builtin-annotate.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  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 process_feature_event(struct perf_tool *tool,
  244. union perf_event *event,
  245. struct perf_session *session)
  246. {
  247. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  248. return perf_event__process_feature(tool, event, session);
  249. return 0;
  250. }
  251. static int hist_entry__tty_annotate(struct hist_entry *he,
  252. struct perf_evsel *evsel,
  253. struct perf_annotate *ann)
  254. {
  255. if (!ann->use_stdio2)
  256. return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel, &ann->opts);
  257. return symbol__tty_annotate2(he->ms.sym, he->ms.map, evsel, &ann->opts);
  258. }
  259. static void hists__find_annotations(struct hists *hists,
  260. struct perf_evsel *evsel,
  261. struct perf_annotate *ann)
  262. {
  263. struct rb_node *nd = rb_first(&hists->entries), *next;
  264. int key = K_RIGHT;
  265. while (nd) {
  266. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  267. struct annotation *notes;
  268. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  269. goto find_next;
  270. if (ann->sym_hist_filter &&
  271. (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
  272. goto find_next;
  273. notes = symbol__annotation(he->ms.sym);
  274. if (notes->src == NULL) {
  275. find_next:
  276. if (key == K_LEFT)
  277. nd = rb_prev(nd);
  278. else
  279. nd = rb_next(nd);
  280. continue;
  281. }
  282. if (use_browser == 2) {
  283. int ret;
  284. int (*annotate)(struct hist_entry *he,
  285. struct perf_evsel *evsel,
  286. struct hist_browser_timer *hbt);
  287. annotate = dlsym(perf_gtk_handle,
  288. "hist_entry__gtk_annotate");
  289. if (annotate == NULL) {
  290. ui__error("GTK browser not found!\n");
  291. return;
  292. }
  293. ret = annotate(he, evsel, NULL);
  294. if (!ret || !ann->skip_missing)
  295. return;
  296. /* skip missing symbols */
  297. nd = rb_next(nd);
  298. } else if (use_browser == 1) {
  299. key = hist_entry__tui_annotate(he, evsel, NULL, &ann->opts);
  300. switch (key) {
  301. case -1:
  302. if (!ann->skip_missing)
  303. return;
  304. /* fall through */
  305. case K_RIGHT:
  306. next = rb_next(nd);
  307. break;
  308. case K_LEFT:
  309. next = rb_prev(nd);
  310. break;
  311. default:
  312. return;
  313. }
  314. if (next != NULL)
  315. nd = next;
  316. } else {
  317. hist_entry__tty_annotate(he, evsel, ann);
  318. nd = rb_next(nd);
  319. /*
  320. * Since we have a hist_entry per IP for the same
  321. * symbol, free he->ms.sym->src to signal we already
  322. * processed this symbol.
  323. */
  324. zfree(&notes->src->cycles_hist);
  325. zfree(&notes->src);
  326. }
  327. }
  328. }
  329. static int __cmd_annotate(struct perf_annotate *ann)
  330. {
  331. int ret;
  332. struct perf_session *session = ann->session;
  333. struct perf_evsel *pos;
  334. u64 total_nr_samples;
  335. if (ann->cpu_list) {
  336. ret = perf_session__cpu_bitmap(session, ann->cpu_list,
  337. ann->cpu_bitmap);
  338. if (ret)
  339. goto out;
  340. }
  341. if (!ann->opts.objdump_path) {
  342. ret = perf_env__lookup_objdump(&session->header.env,
  343. &ann->opts.objdump_path);
  344. if (ret)
  345. goto out;
  346. }
  347. ret = perf_session__process_events(session);
  348. if (ret)
  349. goto out;
  350. if (dump_trace) {
  351. perf_session__fprintf_nr_events(session, stdout);
  352. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  353. goto out;
  354. }
  355. if (verbose > 3)
  356. perf_session__fprintf(session, stdout);
  357. if (verbose > 2)
  358. perf_session__fprintf_dsos(session, stdout);
  359. total_nr_samples = 0;
  360. evlist__for_each_entry(session->evlist, pos) {
  361. struct hists *hists = evsel__hists(pos);
  362. u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  363. if (nr_samples > 0) {
  364. total_nr_samples += nr_samples;
  365. hists__collapse_resort(hists, NULL);
  366. /* Don't sort callchain */
  367. perf_evsel__reset_sample_bit(pos, CALLCHAIN);
  368. perf_evsel__output_resort(pos, NULL);
  369. if (symbol_conf.event_group &&
  370. !perf_evsel__is_group_leader(pos))
  371. continue;
  372. hists__find_annotations(hists, pos, ann);
  373. }
  374. }
  375. if (total_nr_samples == 0) {
  376. ui__error("The %s file has no samples!\n", session->data->file.path);
  377. goto out;
  378. }
  379. if (use_browser == 2) {
  380. void (*show_annotations)(void);
  381. show_annotations = dlsym(perf_gtk_handle,
  382. "perf_gtk__show_annotations");
  383. if (show_annotations == NULL) {
  384. ui__error("GTK browser not found!\n");
  385. goto out;
  386. }
  387. show_annotations();
  388. }
  389. out:
  390. return ret;
  391. }
  392. static const char * const annotate_usage[] = {
  393. "perf annotate [<options>]",
  394. NULL
  395. };
  396. int cmd_annotate(int argc, const char **argv)
  397. {
  398. struct perf_annotate annotate = {
  399. .tool = {
  400. .sample = process_sample_event,
  401. .mmap = perf_event__process_mmap,
  402. .mmap2 = perf_event__process_mmap2,
  403. .comm = perf_event__process_comm,
  404. .exit = perf_event__process_exit,
  405. .fork = perf_event__process_fork,
  406. .namespaces = perf_event__process_namespaces,
  407. .attr = perf_event__process_attr,
  408. .build_id = perf_event__process_build_id,
  409. .tracing_data = perf_event__process_tracing_data,
  410. .feature = process_feature_event,
  411. .ordered_events = true,
  412. .ordering_requires_timestamps = true,
  413. },
  414. .opts = annotation__default_options,
  415. };
  416. struct perf_data data = {
  417. .mode = PERF_DATA_MODE_READ,
  418. };
  419. struct option options[] = {
  420. OPT_STRING('i', "input", &input_name, "file",
  421. "input file name"),
  422. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  423. "only consider symbols in these dsos"),
  424. OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
  425. "symbol to annotate"),
  426. OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
  427. OPT_INCR('v', "verbose", &verbose,
  428. "be more verbose (show symbol address, etc)"),
  429. OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
  430. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  431. "dump raw trace in ASCII"),
  432. OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
  433. OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
  434. OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
  435. OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
  436. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  437. "don't load vmlinux even if found"),
  438. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  439. "file", "vmlinux pathname"),
  440. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  441. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  442. OPT_BOOLEAN('l', "print-line", &annotate.opts.print_lines,
  443. "print matching source lines (may be slow)"),
  444. OPT_BOOLEAN('P', "full-paths", &annotate.opts.full_path,
  445. "Don't shorten the displayed pathnames"),
  446. OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
  447. "Skip symbols that cannot be annotated"),
  448. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
  449. &annotate.group_set,
  450. "Show event group information together"),
  451. OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
  452. OPT_CALLBACK(0, "symfs", NULL, "directory",
  453. "Look for files with symbols relative to this directory",
  454. symbol__config_symfs),
  455. OPT_BOOLEAN(0, "source", &annotate.opts.annotate_src,
  456. "Interleave source code with assembly code (default)"),
  457. OPT_BOOLEAN(0, "asm-raw", &annotate.opts.show_asm_raw,
  458. "Display raw encoding of assembly instructions (default)"),
  459. OPT_STRING('M', "disassembler-style", &annotate.opts.disassembler_style, "disassembler style",
  460. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  461. OPT_STRING(0, "objdump", &annotate.opts.objdump_path, "path",
  462. "objdump binary to use for disassembly and annotations"),
  463. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  464. "Show event group information together"),
  465. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  466. "Show a column with the sum of periods"),
  467. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  468. "Show a column with the number of samples"),
  469. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  470. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  471. stdio__config_color, "always"),
  472. OPT_CALLBACK(0, "percent-type", &annotate.opts, "local-period",
  473. "Set percent type local/global-period/hits",
  474. annotate_parse_percent_type),
  475. OPT_END()
  476. };
  477. int ret;
  478. set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
  479. set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
  480. ret = hists__init();
  481. if (ret < 0)
  482. return ret;
  483. argc = parse_options(argc, argv, options, annotate_usage, 0);
  484. if (argc) {
  485. /*
  486. * Special case: if there's an argument left then assume that
  487. * it's a symbol filter:
  488. */
  489. if (argc > 1)
  490. usage_with_options(annotate_usage, options);
  491. annotate.sym_hist_filter = argv[0];
  492. }
  493. if (symbol_conf.show_nr_samples && annotate.use_gtk) {
  494. pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
  495. return ret;
  496. }
  497. if (quiet)
  498. perf_quiet_option();
  499. data.file.path = input_name;
  500. annotate.session = perf_session__new(&data, false, &annotate.tool);
  501. if (annotate.session == NULL)
  502. return -1;
  503. annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
  504. HEADER_BRANCH_STACK);
  505. if (annotate.group_set)
  506. perf_evlist__force_leader(annotate.session->evlist);
  507. ret = symbol__annotation_init();
  508. if (ret < 0)
  509. goto out_delete;
  510. annotation_config__init();
  511. symbol_conf.try_vmlinux_path = true;
  512. ret = symbol__init(&annotate.session->header.env);
  513. if (ret < 0)
  514. goto out_delete;
  515. if (annotate.use_stdio || annotate.use_stdio2)
  516. use_browser = 0;
  517. else if (annotate.use_tui)
  518. use_browser = 1;
  519. else if (annotate.use_gtk)
  520. use_browser = 2;
  521. setup_browser(true);
  522. if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
  523. sort__mode = SORT_MODE__BRANCH;
  524. if (setup_sorting(annotate.session->evlist) < 0)
  525. usage_with_options(annotate_usage, options);
  526. } else {
  527. if (setup_sorting(NULL) < 0)
  528. usage_with_options(annotate_usage, options);
  529. }
  530. ret = __cmd_annotate(&annotate);
  531. out_delete:
  532. /*
  533. * Speed up the exit process, for large files this can
  534. * take quite a while.
  535. *
  536. * XXX Enable this when using valgrind or if we ever
  537. * librarize this command.
  538. *
  539. * Also experiment with obstacks to see how much speed
  540. * up we'll get here.
  541. *
  542. * perf_session__delete(session);
  543. */
  544. return ret;
  545. }