builtin-annotate.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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_gtk;
  39. bool full_paths;
  40. bool print_line;
  41. bool skip_missing;
  42. bool has_br_stack;
  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->idx);
  143. if (err)
  144. goto out;
  145. err = addr_map_symbol__inc_samples(&bi->to, sample, evsel->idx);
  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 int perf_evsel__add_sample(struct perf_evsel *evsel,
  174. struct perf_sample *sample,
  175. struct addr_location *al,
  176. struct perf_annotate *ann,
  177. struct machine *machine)
  178. {
  179. struct hists *hists = evsel__hists(evsel);
  180. struct hist_entry *he;
  181. int ret;
  182. if ((!ann->has_br_stack || !ui__has_annotation()) &&
  183. ann->sym_hist_filter != NULL &&
  184. (al->sym == NULL ||
  185. strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
  186. /* We're only interested in a symbol named sym_hist_filter */
  187. /*
  188. * FIXME: why isn't this done in the symbol_filter when loading
  189. * the DSO?
  190. */
  191. if (al->sym != NULL) {
  192. rb_erase(&al->sym->rb_node,
  193. &al->map->dso->symbols[al->map->type]);
  194. symbol__delete(al->sym);
  195. dso__reset_find_symbol_cache(al->map->dso);
  196. }
  197. return 0;
  198. }
  199. /*
  200. * XXX filtered samples can still have branch entires pointing into our
  201. * symbol and are missed.
  202. */
  203. process_branch_stack(sample->branch_stack, al, sample);
  204. if (ann->has_br_stack && ui__has_annotation())
  205. return process_branch_callback(evsel, sample, al, ann, machine);
  206. he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
  207. if (he == NULL)
  208. return -ENOMEM;
  209. ret = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
  210. hists__inc_nr_samples(hists, true);
  211. return ret;
  212. }
  213. static int process_sample_event(struct perf_tool *tool,
  214. union perf_event *event,
  215. struct perf_sample *sample,
  216. struct perf_evsel *evsel,
  217. struct machine *machine)
  218. {
  219. struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
  220. struct addr_location al;
  221. int ret = 0;
  222. if (machine__resolve(machine, &al, sample) < 0) {
  223. pr_warning("problem processing %d event, skipping it.\n",
  224. event->header.type);
  225. return -1;
  226. }
  227. if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
  228. goto out_put;
  229. if (!al.filtered &&
  230. perf_evsel__add_sample(evsel, sample, &al, ann, machine)) {
  231. pr_warning("problem incrementing symbol count, "
  232. "skipping event\n");
  233. ret = -1;
  234. }
  235. out_put:
  236. addr_location__put(&al);
  237. return ret;
  238. }
  239. static int hist_entry__tty_annotate(struct hist_entry *he,
  240. struct perf_evsel *evsel,
  241. struct perf_annotate *ann)
  242. {
  243. return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel,
  244. ann->print_line, ann->full_paths, 0, 0);
  245. }
  246. static void hists__find_annotations(struct hists *hists,
  247. struct perf_evsel *evsel,
  248. struct perf_annotate *ann)
  249. {
  250. struct rb_node *nd = rb_first(&hists->entries), *next;
  251. int key = K_RIGHT;
  252. while (nd) {
  253. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  254. struct annotation *notes;
  255. if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
  256. goto find_next;
  257. if (ann->sym_hist_filter &&
  258. (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
  259. goto find_next;
  260. notes = symbol__annotation(he->ms.sym);
  261. if (notes->src == NULL) {
  262. find_next:
  263. if (key == K_LEFT)
  264. nd = rb_prev(nd);
  265. else
  266. nd = rb_next(nd);
  267. continue;
  268. }
  269. if (use_browser == 2) {
  270. int ret;
  271. int (*annotate)(struct hist_entry *he,
  272. struct perf_evsel *evsel,
  273. struct hist_browser_timer *hbt);
  274. annotate = dlsym(perf_gtk_handle,
  275. "hist_entry__gtk_annotate");
  276. if (annotate == NULL) {
  277. ui__error("GTK browser not found!\n");
  278. return;
  279. }
  280. ret = annotate(he, evsel, NULL);
  281. if (!ret || !ann->skip_missing)
  282. return;
  283. /* skip missing symbols */
  284. nd = rb_next(nd);
  285. } else if (use_browser == 1) {
  286. key = hist_entry__tui_annotate(he, evsel, NULL);
  287. switch (key) {
  288. case -1:
  289. if (!ann->skip_missing)
  290. return;
  291. /* fall through */
  292. case K_RIGHT:
  293. next = rb_next(nd);
  294. break;
  295. case K_LEFT:
  296. next = rb_prev(nd);
  297. break;
  298. default:
  299. return;
  300. }
  301. if (next != NULL)
  302. nd = next;
  303. } else {
  304. hist_entry__tty_annotate(he, evsel, ann);
  305. nd = rb_next(nd);
  306. /*
  307. * Since we have a hist_entry per IP for the same
  308. * symbol, free he->ms.sym->src to signal we already
  309. * processed this symbol.
  310. */
  311. zfree(&notes->src->cycles_hist);
  312. zfree(&notes->src);
  313. }
  314. }
  315. }
  316. static int __cmd_annotate(struct perf_annotate *ann)
  317. {
  318. int ret;
  319. struct perf_session *session = ann->session;
  320. struct perf_evsel *pos;
  321. u64 total_nr_samples;
  322. if (ann->cpu_list) {
  323. ret = perf_session__cpu_bitmap(session, ann->cpu_list,
  324. ann->cpu_bitmap);
  325. if (ret)
  326. goto out;
  327. }
  328. if (!objdump_path) {
  329. ret = perf_env__lookup_objdump(&session->header.env);
  330. if (ret)
  331. goto out;
  332. }
  333. ret = perf_session__process_events(session);
  334. if (ret)
  335. goto out;
  336. if (dump_trace) {
  337. perf_session__fprintf_nr_events(session, stdout);
  338. perf_evlist__fprintf_nr_events(session->evlist, stdout);
  339. goto out;
  340. }
  341. if (verbose > 3)
  342. perf_session__fprintf(session, stdout);
  343. if (verbose > 2)
  344. perf_session__fprintf_dsos(session, stdout);
  345. total_nr_samples = 0;
  346. evlist__for_each_entry(session->evlist, pos) {
  347. struct hists *hists = evsel__hists(pos);
  348. u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
  349. if (nr_samples > 0) {
  350. total_nr_samples += nr_samples;
  351. hists__collapse_resort(hists, NULL);
  352. /* Don't sort callchain */
  353. perf_evsel__reset_sample_bit(pos, CALLCHAIN);
  354. perf_evsel__output_resort(pos, NULL);
  355. if (symbol_conf.event_group &&
  356. !perf_evsel__is_group_leader(pos))
  357. continue;
  358. hists__find_annotations(hists, pos, ann);
  359. }
  360. }
  361. if (total_nr_samples == 0) {
  362. ui__error("The %s file has no samples!\n", session->data->file.path);
  363. goto out;
  364. }
  365. if (use_browser == 2) {
  366. void (*show_annotations)(void);
  367. show_annotations = dlsym(perf_gtk_handle,
  368. "perf_gtk__show_annotations");
  369. if (show_annotations == NULL) {
  370. ui__error("GTK browser not found!\n");
  371. goto out;
  372. }
  373. show_annotations();
  374. }
  375. out:
  376. return ret;
  377. }
  378. static const char * const annotate_usage[] = {
  379. "perf annotate [<options>]",
  380. NULL
  381. };
  382. int cmd_annotate(int argc, const char **argv)
  383. {
  384. struct perf_annotate annotate = {
  385. .tool = {
  386. .sample = process_sample_event,
  387. .mmap = perf_event__process_mmap,
  388. .mmap2 = perf_event__process_mmap2,
  389. .comm = perf_event__process_comm,
  390. .exit = perf_event__process_exit,
  391. .fork = perf_event__process_fork,
  392. .namespaces = perf_event__process_namespaces,
  393. .attr = perf_event__process_attr,
  394. .build_id = perf_event__process_build_id,
  395. .tracing_data = perf_event__process_tracing_data,
  396. .feature = perf_event__process_feature,
  397. .ordered_events = true,
  398. .ordering_requires_timestamps = true,
  399. },
  400. };
  401. struct perf_data data = {
  402. .mode = PERF_DATA_MODE_READ,
  403. };
  404. struct option options[] = {
  405. OPT_STRING('i', "input", &input_name, "file",
  406. "input file name"),
  407. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  408. "only consider symbols in these dsos"),
  409. OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
  410. "symbol to annotate"),
  411. OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
  412. OPT_INCR('v', "verbose", &verbose,
  413. "be more verbose (show symbol address, etc)"),
  414. OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
  415. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  416. "dump raw trace in ASCII"),
  417. OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
  418. OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
  419. OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
  420. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  421. "file", "vmlinux pathname"),
  422. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  423. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  424. OPT_BOOLEAN('l', "print-line", &annotate.print_line,
  425. "print matching source lines (may be slow)"),
  426. OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
  427. "Don't shorten the displayed pathnames"),
  428. OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
  429. "Skip symbols that cannot be annotated"),
  430. OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
  431. OPT_CALLBACK(0, "symfs", NULL, "directory",
  432. "Look for files with symbols relative to this directory",
  433. symbol__config_symfs),
  434. OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
  435. "Interleave source code with assembly code (default)"),
  436. OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
  437. "Display raw encoding of assembly instructions (default)"),
  438. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  439. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  440. OPT_STRING(0, "objdump", &objdump_path, "path",
  441. "objdump binary to use for disassembly and annotations"),
  442. OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
  443. "Show event group information together"),
  444. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  445. "Show a column with the sum of periods"),
  446. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  447. "Show a column with the number of samples"),
  448. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  449. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  450. stdio__config_color, "always"),
  451. OPT_END()
  452. };
  453. int ret;
  454. set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
  455. set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
  456. ret = hists__init();
  457. if (ret < 0)
  458. return ret;
  459. argc = parse_options(argc, argv, options, annotate_usage, 0);
  460. if (argc) {
  461. /*
  462. * Special case: if there's an argument left then assume that
  463. * it's a symbol filter:
  464. */
  465. if (argc > 1)
  466. usage_with_options(annotate_usage, options);
  467. annotate.sym_hist_filter = argv[0];
  468. }
  469. if (symbol_conf.show_nr_samples && annotate.use_gtk) {
  470. pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
  471. return ret;
  472. }
  473. if (quiet)
  474. perf_quiet_option();
  475. data.file.path = input_name;
  476. annotate.session = perf_session__new(&data, false, &annotate.tool);
  477. if (annotate.session == NULL)
  478. return -1;
  479. annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
  480. HEADER_BRANCH_STACK);
  481. ret = symbol__annotation_init();
  482. if (ret < 0)
  483. goto out_delete;
  484. symbol_conf.try_vmlinux_path = true;
  485. ret = symbol__init(&annotate.session->header.env);
  486. if (ret < 0)
  487. goto out_delete;
  488. if (annotate.use_stdio)
  489. use_browser = 0;
  490. else if (annotate.use_tui)
  491. use_browser = 1;
  492. else if (annotate.use_gtk)
  493. use_browser = 2;
  494. setup_browser(true);
  495. if (use_browser == 1 && annotate.has_br_stack) {
  496. sort__mode = SORT_MODE__BRANCH;
  497. if (setup_sorting(annotate.session->evlist) < 0)
  498. usage_with_options(annotate_usage, options);
  499. } else {
  500. if (setup_sorting(NULL) < 0)
  501. usage_with_options(annotate_usage, options);
  502. }
  503. ret = __cmd_annotate(&annotate);
  504. out_delete:
  505. /*
  506. * Speed up the exit process, for large files this can
  507. * take quite a while.
  508. *
  509. * XXX Enable this when using valgrind or if we ever
  510. * librarize this command.
  511. *
  512. * Also experiment with obstacks to see how much speed
  513. * up we'll get here.
  514. *
  515. * perf_session__delete(session);
  516. */
  517. return ret;
  518. }