builtin-annotate.c 25 KB

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