builtin-annotate.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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 "perf.h"
  11. #include "util/color.h"
  12. #include <linux/list.h>
  13. #include "util/cache.h"
  14. #include <linux/rbtree.h>
  15. #include <linux/zalloc.h>
  16. #include "util/symbol.h"
  17. #include "util/debug.h"
  18. #include "util/evlist.h"
  19. #include "util/evsel.h"
  20. #include "util/annotate.h"
  21. #include "util/annotate-data.h"
  22. #include "util/event.h"
  23. #include <subcmd/parse-options.h>
  24. #include "util/parse-events.h"
  25. #include "util/sort.h"
  26. #include "util/hist.h"
  27. #include "util/dso.h"
  28. #include "util/machine.h"
  29. #include "util/map.h"
  30. #include "util/session.h"
  31. #include "util/tool.h"
  32. #include "util/data.h"
  33. #include "arch/common.h"
  34. #include "util/block-range.h"
  35. #include "util/map_symbol.h"
  36. #include "util/branch.h"
  37. #include "util/util.h"
  38. #include "ui/progress.h"
  39. #include <dlfcn.h>
  40. #include <errno.h>
  41. #include <linux/bitmap.h>
  42. #include <linux/err.h>
  43. #include <inttypes.h>
  44. struct perf_annotate {
  45. struct perf_tool tool;
  46. struct perf_session *session;
  47. #ifdef HAVE_SLANG_SUPPORT
  48. bool use_tui;
  49. #endif
  50. bool use_stdio, use_stdio2;
  51. #ifdef HAVE_GTK2_SUPPORT
  52. bool use_gtk;
  53. #endif
  54. bool skip_missing;
  55. bool has_br_stack;
  56. bool group_set;
  57. bool data_type;
  58. bool type_stat;
  59. bool insn_stat;
  60. float min_percent;
  61. const char *sym_hist_filter;
  62. const char *cpu_list;
  63. const char *target_data_type;
  64. DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
  65. };
  66. /*
  67. * Given one basic block:
  68. *
  69. * from to branch_i
  70. * * ----> *
  71. * |
  72. * | block
  73. * v
  74. * * ----> *
  75. * from to branch_i+1
  76. *
  77. * where the horizontal are the branches and the vertical is the executed
  78. * block of instructions.
  79. *
  80. * We count, for each 'instruction', the number of blocks that covered it as
  81. * well as count the ratio each branch is taken.
  82. *
  83. * We can do this without knowing the actual instruction stream by keeping
  84. * track of the address ranges. We break down ranges such that there is no
  85. * overlap and iterate from the start until the end.
  86. *
  87. * @acme: once we parse the objdump output _before_ processing the samples,
  88. * we can easily fold the branch.cycles IPC bits in.
  89. */
  90. static void process_basic_block(struct addr_map_symbol *start,
  91. struct addr_map_symbol *end,
  92. struct branch_flags *flags)
  93. {
  94. struct symbol *sym = start->ms.sym;
  95. struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
  96. struct block_range_iter iter;
  97. struct block_range *entry;
  98. struct annotated_branch *branch;
  99. /*
  100. * Sanity; NULL isn't executable and the CPU cannot execute backwards
  101. */
  102. if (!start->addr || start->addr > end->addr)
  103. return;
  104. iter = block_range__create(start->addr, end->addr);
  105. if (!block_range_iter__valid(&iter))
  106. return;
  107. branch = annotation__get_branch(notes);
  108. /*
  109. * First block in range is a branch target.
  110. */
  111. entry = block_range_iter(&iter);
  112. assert(entry->is_target);
  113. entry->entry++;
  114. do {
  115. entry = block_range_iter(&iter);
  116. entry->coverage++;
  117. entry->sym = sym;
  118. if (branch)
  119. branch->max_coverage = max(branch->max_coverage, entry->coverage);
  120. } while (block_range_iter__next(&iter));
  121. /*
  122. * Last block in rage is a branch.
  123. */
  124. entry = block_range_iter(&iter);
  125. assert(entry->is_branch);
  126. entry->taken++;
  127. if (flags->predicted)
  128. entry->pred++;
  129. }
  130. static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
  131. struct perf_sample *sample)
  132. {
  133. struct addr_map_symbol *prev = NULL;
  134. struct branch_info *bi;
  135. int i;
  136. if (!bs || !bs->nr)
  137. return;
  138. bi = sample__resolve_bstack(sample, al);
  139. if (!bi)
  140. return;
  141. for (i = bs->nr - 1; i >= 0; i--) {
  142. /*
  143. * XXX filter against symbol
  144. */
  145. if (prev)
  146. process_basic_block(prev, &bi[i].from, &bi[i].flags);
  147. prev = &bi[i].to;
  148. }
  149. free(bi);
  150. }
  151. static int hist_iter__branch_callback(struct hist_entry_iter *iter,
  152. struct addr_location *al __maybe_unused,
  153. bool single __maybe_unused,
  154. void *arg __maybe_unused)
  155. {
  156. struct hist_entry *he = iter->he;
  157. struct branch_info *bi;
  158. struct perf_sample *sample = iter->sample;
  159. struct evsel *evsel = iter->evsel;
  160. int err;
  161. bi = he->branch_info;
  162. err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
  163. if (err)
  164. goto out;
  165. err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
  166. out:
  167. return err;
  168. }
  169. static int process_branch_callback(struct evsel *evsel,
  170. struct perf_sample *sample,
  171. struct addr_location *al,
  172. struct perf_annotate *ann,
  173. struct machine *machine)
  174. {
  175. struct hist_entry_iter iter = {
  176. .evsel = evsel,
  177. .sample = sample,
  178. .add_entry_cb = hist_iter__branch_callback,
  179. .hide_unresolved = symbol_conf.hide_unresolved,
  180. .ops = &hist_iter_branch,
  181. };
  182. struct addr_location a;
  183. int ret;
  184. addr_location__init(&a);
  185. if (machine__resolve(machine, &a, sample) < 0) {
  186. ret = -1;
  187. goto out;
  188. }
  189. if (a.sym == NULL) {
  190. ret = 0;
  191. goto out;
  192. }
  193. if (a.map != NULL)
  194. dso__set_hit(map__dso(a.map));
  195. hist__account_cycles(sample->branch_stack, al, sample, false,
  196. NULL, evsel);
  197. ret = hist_entry_iter__add(&iter, &a, PERF_MAX_STACK_DEPTH, ann);
  198. out:
  199. addr_location__exit(&a);
  200. return ret;
  201. }
  202. static bool has_annotation(struct perf_annotate *ann)
  203. {
  204. return ui__has_annotation() || ann->use_stdio2;
  205. }
  206. static int evsel__add_sample(struct evsel *evsel, struct perf_sample *sample,
  207. struct addr_location *al, struct perf_annotate *ann,
  208. struct machine *machine)
  209. {
  210. struct hists *hists = evsel__hists(evsel);
  211. struct hist_entry *he;
  212. int ret;
  213. if ((!ann->has_br_stack || !has_annotation(ann)) &&
  214. ann->sym_hist_filter != NULL &&
  215. (al->sym == NULL ||
  216. strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
  217. /* We're only interested in a symbol named sym_hist_filter */
  218. /*
  219. * FIXME: why isn't this done in the symbol_filter when loading
  220. * the DSO?
  221. */
  222. if (al->sym != NULL) {
  223. struct dso *dso = map__dso(al->map);
  224. rb_erase_cached(&al->sym->rb_node, dso__symbols(dso));
  225. symbol__delete(al->sym);
  226. dso__reset_find_symbol_cache(dso);
  227. }
  228. return 0;
  229. }
  230. /*
  231. * XXX filtered samples can still have branch entries pointing into our
  232. * symbol and are missed.
  233. */
  234. process_branch_stack(sample->branch_stack, al, sample);
  235. if (ann->has_br_stack && has_annotation(ann))
  236. return process_branch_callback(evsel, sample, al, ann, machine);
  237. he = hists__add_entry(hists, al, NULL, NULL, NULL, NULL, sample, true);
  238. if (he == NULL)
  239. return -ENOMEM;
  240. ret = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
  241. hists__inc_nr_samples(hists, true);
  242. return ret;
  243. }
  244. static int process_sample_event(const struct perf_tool *tool,
  245. union perf_event *event,
  246. struct perf_sample *sample,
  247. struct evsel *evsel,
  248. struct machine *machine)
  249. {
  250. struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
  251. struct addr_location al;
  252. int ret = 0;
  253. addr_location__init(&al);
  254. if (machine__resolve(machine, &al, sample) < 0) {
  255. pr_warning("problem processing %d event, skipping it.\n",
  256. event->header.type);
  257. ret = -1;
  258. goto out_put;
  259. }
  260. if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
  261. goto out_put;
  262. if (!al.filtered &&
  263. evsel__add_sample(evsel, sample, &al, ann, machine)) {
  264. pr_warning("problem incrementing symbol count, "
  265. "skipping event\n");
  266. ret = -1;
  267. }
  268. out_put:
  269. addr_location__exit(&al);
  270. return ret;
  271. }
  272. static int process_feature_event(const struct perf_tool *tool __maybe_unused,
  273. struct perf_session *session,
  274. union perf_event *event)
  275. {
  276. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  277. return perf_event__process_feature(session, event);
  278. return 0;
  279. }
  280. static int hist_entry__stdio_annotate(struct hist_entry *he,
  281. struct evsel *evsel,
  282. struct perf_annotate *ann)
  283. {
  284. if (ann->use_stdio2)
  285. return hist_entry__tty_annotate2(he, evsel);
  286. return hist_entry__tty_annotate(he, evsel);
  287. }
  288. static void print_annotate_data_stat(struct annotated_data_stat *s)
  289. {
  290. #define PRINT_STAT(fld) if (s->fld) printf("%10d : %s\n", s->fld, #fld)
  291. int bad = s->no_sym +
  292. s->no_insn +
  293. s->no_insn_ops +
  294. s->no_mem_ops +
  295. s->no_reg +
  296. s->no_dbginfo +
  297. s->no_cuinfo +
  298. s->no_var +
  299. s->no_typeinfo +
  300. s->invalid_size +
  301. s->bad_offset;
  302. int ok = s->total - bad;
  303. printf("Annotate data type stats:\n");
  304. printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n",
  305. s->total, ok, 100.0 * ok / (s->total ?: 1), bad, 100.0 * bad / (s->total ?: 1));
  306. printf("-----------------------------------------------------------\n");
  307. PRINT_STAT(no_sym);
  308. PRINT_STAT(no_insn);
  309. PRINT_STAT(no_insn_ops);
  310. PRINT_STAT(no_mem_ops);
  311. PRINT_STAT(no_reg);
  312. PRINT_STAT(no_dbginfo);
  313. PRINT_STAT(no_cuinfo);
  314. PRINT_STAT(no_var);
  315. PRINT_STAT(no_typeinfo);
  316. PRINT_STAT(invalid_size);
  317. PRINT_STAT(bad_offset);
  318. PRINT_STAT(insn_track);
  319. printf("\n");
  320. #undef PRINT_STAT
  321. }
  322. static void print_annotate_item_stat(struct list_head *head, const char *title)
  323. {
  324. struct annotated_item_stat *istat, *pos, *iter;
  325. int total_good, total_bad, total;
  326. int sum1, sum2;
  327. LIST_HEAD(tmp);
  328. /* sort the list by count */
  329. list_splice_init(head, &tmp);
  330. total_good = total_bad = 0;
  331. list_for_each_entry_safe(istat, pos, &tmp, list) {
  332. total_good += istat->good;
  333. total_bad += istat->bad;
  334. sum1 = istat->good + istat->bad;
  335. list_for_each_entry(iter, head, list) {
  336. sum2 = iter->good + iter->bad;
  337. if (sum1 > sum2)
  338. break;
  339. }
  340. list_move_tail(&istat->list, &iter->list);
  341. }
  342. total = total_good + total_bad;
  343. printf("Annotate %s stats\n", title);
  344. printf("total %d, ok %d (%.1f%%), bad %d (%.1f%%)\n\n", total,
  345. total_good, 100.0 * total_good / (total ?: 1),
  346. total_bad, 100.0 * total_bad / (total ?: 1));
  347. printf(" %-20s: %5s %5s\n", "Name/opcode", "Good", "Bad");
  348. printf("-----------------------------------------------------------\n");
  349. list_for_each_entry(istat, head, list)
  350. printf(" %-20s: %5d %5d\n", istat->name, istat->good, istat->bad);
  351. printf("\n");
  352. }
  353. static void hists__find_annotations(struct hists *hists,
  354. struct evsel *evsel,
  355. struct perf_annotate *ann)
  356. {
  357. struct rb_node *nd = rb_first_cached(&hists->entries), *next;
  358. int key = K_RIGHT;
  359. if (ann->type_stat)
  360. print_annotate_data_stat(&ann_data_stat);
  361. if (ann->insn_stat)
  362. print_annotate_item_stat(&ann_insn_stat, "Instruction");
  363. while (nd) {
  364. struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
  365. struct annotation *notes;
  366. if (he->ms.sym == NULL || dso__annotate_warned(map__dso(he->ms.map)))
  367. goto find_next;
  368. if (ann->sym_hist_filter &&
  369. (strcmp(he->ms.sym->name, ann->sym_hist_filter) != 0))
  370. goto find_next;
  371. if (ann->min_percent) {
  372. float percent = 0;
  373. u64 total = hists__total_period(hists);
  374. if (total)
  375. percent = 100.0 * he->stat.period / total;
  376. if (percent < ann->min_percent)
  377. goto find_next;
  378. }
  379. notes = symbol__annotation(he->ms.sym);
  380. if (notes->src == NULL) {
  381. find_next:
  382. if (key == K_LEFT || key == '<')
  383. nd = rb_prev(nd);
  384. else
  385. nd = rb_next(nd);
  386. continue;
  387. }
  388. if (ann->data_type) {
  389. /* skip unknown type */
  390. if (he->mem_type->histograms == NULL)
  391. goto find_next;
  392. if (ann->target_data_type) {
  393. const char *type_name = he->mem_type->self.type_name;
  394. /* skip 'struct ' prefix in the type name */
  395. if (strncmp(ann->target_data_type, "struct ", 7) &&
  396. !strncmp(type_name, "struct ", 7))
  397. type_name += 7;
  398. /* skip 'union ' prefix in the type name */
  399. if (strncmp(ann->target_data_type, "union ", 6) &&
  400. !strncmp(type_name, "union ", 6))
  401. type_name += 6;
  402. if (strcmp(ann->target_data_type, type_name))
  403. goto find_next;
  404. }
  405. if (use_browser == 1)
  406. key = hist_entry__annotate_data_tui(he, evsel, NULL);
  407. else
  408. key = hist_entry__annotate_data_tty(he, evsel);
  409. switch (key) {
  410. case -1:
  411. if (!ann->skip_missing)
  412. return;
  413. /* fall through */
  414. case K_RIGHT:
  415. case '>':
  416. next = rb_next(nd);
  417. break;
  418. case K_LEFT:
  419. case '<':
  420. next = rb_prev(nd);
  421. break;
  422. default:
  423. return;
  424. }
  425. if (use_browser == 0 || next != NULL)
  426. nd = next;
  427. continue;
  428. }
  429. if (use_browser == 2) {
  430. int ret;
  431. int (*annotate)(struct hist_entry *he,
  432. struct evsel *evsel,
  433. struct hist_browser_timer *hbt);
  434. annotate = dlsym(perf_gtk_handle,
  435. "hist_entry__gtk_annotate");
  436. if (annotate == NULL) {
  437. ui__error("GTK browser not found!\n");
  438. return;
  439. }
  440. ret = annotate(he, evsel, NULL);
  441. if (!ret || !ann->skip_missing)
  442. return;
  443. /* skip missing symbols */
  444. nd = rb_next(nd);
  445. } else if (use_browser == 1) {
  446. key = hist_entry__tui_annotate(he, evsel, NULL, NO_ADDR);
  447. switch (key) {
  448. case -1:
  449. if (!ann->skip_missing)
  450. return;
  451. /* fall through */
  452. case K_RIGHT:
  453. case '>':
  454. next = rb_next(nd);
  455. break;
  456. case K_LEFT:
  457. case '<':
  458. next = rb_prev(nd);
  459. break;
  460. default:
  461. return;
  462. }
  463. if (next != NULL)
  464. nd = next;
  465. } else {
  466. hist_entry__stdio_annotate(he, evsel, ann);
  467. nd = rb_next(nd);
  468. }
  469. }
  470. }
  471. static int __cmd_annotate(struct perf_annotate *ann)
  472. {
  473. int ret;
  474. struct perf_session *session = ann->session;
  475. struct evsel *pos;
  476. u64 total_nr_samples;
  477. if (ann->cpu_list) {
  478. ret = perf_session__cpu_bitmap(session, ann->cpu_list,
  479. ann->cpu_bitmap);
  480. if (ret)
  481. goto out;
  482. }
  483. if (!annotate_opts.objdump_path) {
  484. ret = perf_env__lookup_objdump(perf_session__env(session),
  485. &annotate_opts.objdump_path);
  486. if (ret)
  487. goto out;
  488. }
  489. ret = perf_session__process_events(session);
  490. if (ret)
  491. goto out;
  492. if (dump_trace) {
  493. perf_session__fprintf_nr_events(session, stdout);
  494. evlist__fprintf_nr_events(session->evlist, stdout);
  495. goto out;
  496. }
  497. if (verbose > 3)
  498. perf_session__fprintf(session, stdout);
  499. if (verbose > 2)
  500. perf_session__fprintf_dsos(session, stdout);
  501. total_nr_samples = 0;
  502. evlist__for_each_entry(session->evlist, pos) {
  503. struct hists *hists = evsel__hists(pos);
  504. u32 nr_samples = hists->stats.nr_samples;
  505. struct ui_progress prog;
  506. if (nr_samples > 0) {
  507. total_nr_samples += nr_samples;
  508. ui_progress__init(&prog, nr_samples,
  509. "Merging related events...");
  510. hists__collapse_resort(hists, &prog);
  511. ui_progress__finish();
  512. /* Don't sort callchain */
  513. evsel__reset_sample_bit(pos, CALLCHAIN);
  514. ui_progress__init(&prog, nr_samples,
  515. "Sorting events for output...");
  516. evsel__output_resort(pos, &prog);
  517. ui_progress__finish();
  518. /*
  519. * An event group needs to display other events too.
  520. * Let's delay printing until other events are processed.
  521. */
  522. if (symbol_conf.event_group) {
  523. if (!evsel__is_group_leader(pos)) {
  524. struct hists *leader_hists;
  525. leader_hists = evsel__hists(evsel__leader(pos));
  526. hists__match(leader_hists, hists);
  527. hists__link(leader_hists, hists);
  528. }
  529. continue;
  530. }
  531. hists__find_annotations(hists, pos, ann);
  532. }
  533. }
  534. if (total_nr_samples == 0) {
  535. ui__error("The %s data has no samples!\n", session->data->path);
  536. goto out;
  537. }
  538. /* Display group events together */
  539. evlist__for_each_entry(session->evlist, pos) {
  540. struct hists *hists = evsel__hists(pos);
  541. u32 nr_samples = hists->stats.nr_samples;
  542. struct ui_progress prog;
  543. struct evsel *evsel;
  544. if (!symbol_conf.event_group || !evsel__is_group_leader(pos))
  545. continue;
  546. for_each_group_member(evsel, pos)
  547. nr_samples += evsel__hists(evsel)->stats.nr_samples;
  548. if (nr_samples == 0)
  549. continue;
  550. ui_progress__init(&prog, nr_samples,
  551. "Sorting group events for output...");
  552. evsel__output_resort(pos, &prog);
  553. ui_progress__finish();
  554. hists__find_annotations(hists, pos, ann);
  555. }
  556. if (use_browser == 2) {
  557. void (*show_annotations)(void);
  558. show_annotations = dlsym(perf_gtk_handle,
  559. "perf_gtk__show_annotations");
  560. if (show_annotations == NULL) {
  561. ui__error("GTK browser not found!\n");
  562. goto out;
  563. }
  564. show_annotations();
  565. }
  566. out:
  567. return ret;
  568. }
  569. static int parse_percent_limit(const struct option *opt, const char *str,
  570. int unset __maybe_unused)
  571. {
  572. struct perf_annotate *ann = opt->value;
  573. double pcnt = strtof(str, NULL);
  574. ann->min_percent = pcnt;
  575. return 0;
  576. }
  577. static int parse_data_type(const struct option *opt, const char *str, int unset)
  578. {
  579. struct perf_annotate *ann = opt->value;
  580. ann->data_type = !unset;
  581. if (str)
  582. ann->target_data_type = strdup(str);
  583. return 0;
  584. }
  585. static const char * const annotate_usage[] = {
  586. "perf annotate [<options>]",
  587. NULL
  588. };
  589. int cmd_annotate(int argc, const char **argv)
  590. {
  591. struct perf_annotate annotate = {};
  592. struct perf_data data = {
  593. .mode = PERF_DATA_MODE_READ,
  594. };
  595. struct itrace_synth_opts itrace_synth_opts = {
  596. .set = 0,
  597. };
  598. const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
  599. struct option options[] = {
  600. OPT_STRING('i', "input", &input_name, "file",
  601. "input file name"),
  602. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  603. "only consider symbols in these dsos"),
  604. OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
  605. "symbol to annotate"),
  606. OPT_BOOLEAN('f', "force", &data.force, "don't complain, do it"),
  607. OPT_INCR('v', "verbose", &verbose,
  608. "be more verbose (show symbol address, etc)"),
  609. OPT_BOOLEAN('q', "quiet", &quiet, "do now show any warnings or messages"),
  610. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  611. "dump raw trace in ASCII"),
  612. #ifdef HAVE_GTK2_SUPPORT
  613. OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
  614. #endif
  615. #ifdef HAVE_SLANG_SUPPORT
  616. OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
  617. #endif
  618. OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
  619. OPT_BOOLEAN(0, "stdio2", &annotate.use_stdio2, "Use the stdio interface"),
  620. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  621. "don't load vmlinux even if found"),
  622. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  623. "file", "vmlinux pathname"),
  624. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  625. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  626. OPT_BOOLEAN('l', "print-line", &annotate_opts.print_lines,
  627. "print matching source lines (may be slow)"),
  628. OPT_BOOLEAN('P', "full-paths", &annotate_opts.full_path,
  629. "Don't shorten the displayed pathnames"),
  630. OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
  631. "Skip symbols that cannot be annotated"),
  632. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group,
  633. &annotate.group_set,
  634. "Show event group information together"),
  635. OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
  636. OPT_CALLBACK(0, "symfs", NULL, "directory",
  637. "Look for files with symbols relative to this directory",
  638. symbol__config_symfs),
  639. OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
  640. "Interleave source code with assembly code (default)"),
  641. OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
  642. "Display raw encoding of assembly instructions (default)"),
  643. OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
  644. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  645. OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
  646. "Add prefix to source file path names in programs (with --prefix-strip)"),
  647. OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
  648. "Strip first N entries of source file path name in programs (with --prefix)"),
  649. OPT_STRING(0, "objdump", &objdump_path, "path",
  650. "objdump binary to use for disassembly and annotations"),
  651. OPT_STRING(0, "addr2line", &addr2line_path, "path",
  652. "addr2line binary to use for line numbers"),
  653. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  654. "Enable symbol demangling"),
  655. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  656. "Enable kernel symbol demangling"),
  657. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  658. "Show a column with the sum of periods"),
  659. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  660. "Show a column with the number of samples"),
  661. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  662. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  663. stdio__config_color, "always"),
  664. OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
  665. "Set percent type local/global-period/hits",
  666. annotate_parse_percent_type),
  667. OPT_CALLBACK(0, "percent-limit", &annotate, "percent",
  668. "Don't show entries under that percent", parse_percent_limit),
  669. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  670. "Instruction Tracing options\n" ITRACE_HELP,
  671. itrace_parse_synth_opts),
  672. OPT_CALLBACK_OPTARG(0, "data-type", &annotate, NULL, "name",
  673. "Show data type annotate for the memory accesses",
  674. parse_data_type),
  675. OPT_BOOLEAN(0, "type-stat", &annotate.type_stat,
  676. "Show stats for the data type annotation"),
  677. OPT_BOOLEAN(0, "insn-stat", &annotate.insn_stat,
  678. "Show instruction stats for the data type annotation"),
  679. OPT_BOOLEAN(0, "skip-empty", &symbol_conf.skip_empty,
  680. "Do not display empty (or dummy) events in the output"),
  681. OPT_BOOLEAN(0, "code-with-type", &annotate_opts.code_with_type,
  682. "Show data type info in code annotation (memory instructions only)"),
  683. OPT_END()
  684. };
  685. int ret;
  686. set_option_flag(options, 0, "show-total-period", PARSE_OPT_EXCLUSIVE);
  687. set_option_flag(options, 0, "show-nr-samples", PARSE_OPT_EXCLUSIVE);
  688. annotation_options__init();
  689. ret = hists__init();
  690. if (ret < 0)
  691. return ret;
  692. annotation_config__init();
  693. argc = parse_options(argc, argv, options, annotate_usage, 0);
  694. if (argc) {
  695. /*
  696. * Special case: if there's an argument left then assume that
  697. * it's a symbol filter:
  698. */
  699. if (argc > 1)
  700. usage_with_options(annotate_usage, options);
  701. annotate.sym_hist_filter = argv[0];
  702. }
  703. if (disassembler_style) {
  704. annotate_opts.disassembler_style = strdup(disassembler_style);
  705. if (!annotate_opts.disassembler_style)
  706. return -ENOMEM;
  707. }
  708. if (objdump_path) {
  709. annotate_opts.objdump_path = strdup(objdump_path);
  710. if (!annotate_opts.objdump_path)
  711. return -ENOMEM;
  712. }
  713. if (addr2line_path) {
  714. symbol_conf.addr2line_path = strdup(addr2line_path);
  715. if (!symbol_conf.addr2line_path)
  716. return -ENOMEM;
  717. }
  718. if (annotate_check_args() < 0)
  719. return -EINVAL;
  720. #ifdef HAVE_GTK2_SUPPORT
  721. if (symbol_conf.show_nr_samples && annotate.use_gtk) {
  722. pr_err("--show-nr-samples is not available in --gtk mode at this time\n");
  723. return ret;
  724. }
  725. #endif
  726. #ifndef HAVE_LIBDW_SUPPORT
  727. if (annotate.data_type) {
  728. pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
  729. return -ENOTSUP;
  730. }
  731. #endif
  732. ret = symbol__validate_sym_arguments();
  733. if (ret)
  734. return ret;
  735. if (quiet)
  736. perf_quiet_option();
  737. data.path = input_name;
  738. perf_tool__init(&annotate.tool, /*ordered_events=*/true);
  739. annotate.tool.sample = process_sample_event;
  740. annotate.tool.mmap = perf_event__process_mmap;
  741. annotate.tool.mmap2 = perf_event__process_mmap2;
  742. annotate.tool.comm = perf_event__process_comm;
  743. annotate.tool.exit = perf_event__process_exit;
  744. annotate.tool.fork = perf_event__process_fork;
  745. annotate.tool.namespaces = perf_event__process_namespaces;
  746. annotate.tool.attr = perf_event__process_attr;
  747. annotate.tool.build_id = perf_event__process_build_id;
  748. #ifdef HAVE_LIBTRACEEVENT
  749. annotate.tool.tracing_data = perf_event__process_tracing_data;
  750. #endif
  751. annotate.tool.id_index = perf_event__process_id_index;
  752. annotate.tool.auxtrace_info = perf_event__process_auxtrace_info;
  753. annotate.tool.auxtrace = perf_event__process_auxtrace;
  754. annotate.tool.feature = process_feature_event;
  755. annotate.tool.ordering_requires_timestamps = true;
  756. annotate.session = perf_session__new(&data, &annotate.tool);
  757. if (IS_ERR(annotate.session))
  758. return PTR_ERR(annotate.session);
  759. annotate.session->itrace_synth_opts = &itrace_synth_opts;
  760. annotate.has_br_stack = perf_header__has_feat(&annotate.session->header,
  761. HEADER_BRANCH_STACK);
  762. if (annotate.group_set)
  763. evlist__force_leader(annotate.session->evlist);
  764. ret = symbol__annotation_init();
  765. if (ret < 0)
  766. goto out_delete;
  767. symbol_conf.try_vmlinux_path = true;
  768. ret = symbol__init(perf_session__env(annotate.session));
  769. if (ret < 0)
  770. goto out_delete;
  771. if (annotate.use_stdio || annotate.use_stdio2)
  772. use_browser = 0;
  773. #ifdef HAVE_SLANG_SUPPORT
  774. else if (annotate.use_tui)
  775. use_browser = 1;
  776. #endif
  777. #ifdef HAVE_GTK2_SUPPORT
  778. else if (annotate.use_gtk)
  779. use_browser = 2;
  780. #endif
  781. if (annotate.data_type) {
  782. annotate_opts.annotate_src = false;
  783. symbol_conf.annotate_data_member = true;
  784. symbol_conf.annotate_data_sample = true;
  785. } else if (annotate_opts.code_with_type) {
  786. symbol_conf.annotate_data_member = true;
  787. }
  788. setup_browser(true);
  789. /*
  790. * Events of different processes may correspond to the same
  791. * symbol, we do not care about the processes in annotate,
  792. * set sort order to avoid repeated output.
  793. */
  794. if (annotate.data_type)
  795. sort_order = "dso,type";
  796. else
  797. sort_order = "dso,symbol";
  798. /*
  799. * Set SORT_MODE__BRANCH so that annotate displays IPC/Cycle and
  800. * branch counters, if the corresponding branch info is available
  801. * in the perf data in the TUI mode.
  802. */
  803. if ((use_browser == 1 || annotate.use_stdio2) && annotate.has_br_stack) {
  804. sort__mode = SORT_MODE__BRANCH;
  805. if (annotate.session->evlist->nr_br_cntr > 0)
  806. annotate_opts.show_br_cntr = true;
  807. }
  808. if (setup_sorting(/*evlist=*/NULL, perf_session__env(annotate.session)) < 0)
  809. usage_with_options(annotate_usage, options);
  810. ret = __cmd_annotate(&annotate);
  811. out_delete:
  812. /*
  813. * Speed up the exit process by only deleting for debug builds. For
  814. * large files this can save time.
  815. */
  816. #ifndef NDEBUG
  817. perf_session__delete(annotate.session);
  818. #endif
  819. annotation_options__exit();
  820. return ret;
  821. }