builtin-annotate.c 16 KB

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