blob: 7a5dc7e5c577270edaa3a36f7a916d7f3cc5a182 [file] [log] [blame]
Ingo Molnar8035e422009-06-06 15:19:13 +02001/*
2 * builtin-annotate.c
3 *
4 * Builtin annotate command: Analyze the perf.data input file,
5 * look up and read DSOs and symbol information and display
6 * a histogram of results, along various sorting keys.
7 */
8#include "builtin.h"
9
10#include "util/util.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020011#include "util/color.h"
Arnaldo Carvalho de Melo5da50252009-07-01 14:46:08 -030012#include <linux/list.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020013#include "util/cache.h"
Arnaldo Carvalho de Melo43cbcd82009-07-01 12:28:37 -030014#include <linux/rbtree.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020015#include "util/symbol.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020016
17#include "perf.h"
Frederic Weisbecker8f288272009-08-16 22:05:48 +020018#include "util/debug.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020019
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -030020#include "util/evlist.h"
21#include "util/evsel.h"
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -020022#include "util/annotate.h"
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -020023#include "util/event.h"
Josh Poimboeuf4b6ab942015-12-15 09:39:39 -060024#include <subcmd/parse-options.h>
Ingo Molnar8035e422009-06-06 15:19:13 +020025#include "util/parse-events.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020026#include "util/thread.h"
John Kacurdd68ada2009-09-24 18:02:49 +020027#include "util/sort.h"
John Kacur3d1d07e2009-09-28 15:32:55 +020028#include "util/hist.h"
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -020029#include "util/session.h"
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020030#include "util/tool.h"
Jiri Olsaf5fc1412013-10-15 16:27:32 +020031#include "util/data.h"
Irina Tirdea68e94f42012-10-16 02:33:38 +030032#include "arch/common.h"
Peter Zijlstra70fbe052016-09-05 16:08:12 -030033#include "util/block-range.h"
Ingo Molnar8035e422009-06-06 15:19:13 +020034
Namhyung Kimfc672972013-09-13 15:27:43 +090035#include <dlfcn.h>
Arnaldo Carvalho de Meloa43783a2017-04-18 10:46:11 -030036#include <errno.h>
Anton Blanchard5d67be92011-07-04 21:57:50 +100037#include <linux/bitmap.h>
38
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020039struct perf_annotate {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020040 struct perf_tool tool;
Namhyung Kimfa10f312014-08-12 15:40:35 +090041 struct perf_session *session;
42 bool use_tui, use_stdio, use_gtk;
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -020043 bool full_paths;
44 bool print_line;
Namhyung Kim18c9e5c2013-02-07 18:02:14 +090045 bool skip_missing;
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -020046 const char *sym_hist_filter;
47 const char *cpu_list;
48 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020049};
Anton Blanchard5d67be92011-07-04 21:57:50 +100050
Peter Zijlstra70fbe052016-09-05 16:08:12 -030051/*
52 * Given one basic block:
53 *
54 * from to branch_i
55 * * ----> *
56 * |
57 * | block
58 * v
59 * * ----> *
60 * from to branch_i+1
61 *
62 * where the horizontal are the branches and the vertical is the executed
63 * block of instructions.
64 *
65 * We count, for each 'instruction', the number of blocks that covered it as
66 * well as count the ratio each branch is taken.
67 *
68 * We can do this without knowing the actual instruction stream by keeping
69 * track of the address ranges. We break down ranges such that there is no
70 * overlap and iterate from the start until the end.
71 *
72 * @acme: once we parse the objdump output _before_ processing the samples,
73 * we can easily fold the branch.cycles IPC bits in.
74 */
75static void process_basic_block(struct addr_map_symbol *start,
76 struct addr_map_symbol *end,
77 struct branch_flags *flags)
78{
79 struct symbol *sym = start->sym;
80 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
81 struct block_range_iter iter;
82 struct block_range *entry;
83
84 /*
85 * Sanity; NULL isn't executable and the CPU cannot execute backwards
86 */
87 if (!start->addr || start->addr > end->addr)
88 return;
89
90 iter = block_range__create(start->addr, end->addr);
91 if (!block_range_iter__valid(&iter))
92 return;
93
94 /*
95 * First block in range is a branch target.
96 */
97 entry = block_range_iter(&iter);
98 assert(entry->is_target);
99 entry->entry++;
100
101 do {
102 entry = block_range_iter(&iter);
103
104 entry->coverage++;
105 entry->sym = sym;
106
107 if (notes)
108 notes->max_coverage = max(notes->max_coverage, entry->coverage);
109
110 } while (block_range_iter__next(&iter));
111
112 /*
113 * Last block in rage is a branch.
114 */
115 entry = block_range_iter(&iter);
116 assert(entry->is_branch);
117 entry->taken++;
118 if (flags->predicted)
119 entry->pred++;
120}
121
122static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
123 struct perf_sample *sample)
124{
125 struct addr_map_symbol *prev = NULL;
126 struct branch_info *bi;
127 int i;
128
129 if (!bs || !bs->nr)
130 return;
131
132 bi = sample__resolve_bstack(sample, al);
133 if (!bi)
134 return;
135
136 for (i = bs->nr - 1; i >= 0; i--) {
137 /*
138 * XXX filter against symbol
139 */
140 if (prev)
141 process_basic_block(prev, &bi[i].from, &bi[i].flags);
142 prev = &bi[i].to;
143 }
144
145 free(bi);
146}
147
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200148static int perf_evsel__add_sample(struct perf_evsel *evsel,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900149 struct perf_sample *sample,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200150 struct addr_location *al,
151 struct perf_annotate *ann)
Ingo Molnar8035e422009-06-06 15:19:13 +0200152{
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300153 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300154 struct hist_entry *he;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300155 int ret;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300156
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200157 if (ann->sym_hist_filter != NULL &&
158 (al->sym == NULL ||
159 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300160 /* We're only interested in a symbol named sym_hist_filter */
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300161 /*
162 * FIXME: why isn't this done in the symbol_filter when loading
163 * the DSO?
164 */
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300165 if (al->sym != NULL) {
166 rb_erase(&al->sym->rb_node,
167 &al->map->dso->symbols[al->map->type]);
168 symbol__delete(al->sym);
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300169 dso__reset_find_symbol_cache(al->map->dso);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300170 }
171 return 0;
172 }
173
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300174 /*
175 * XXX filtered samples can still have branch entires pointing into our
176 * symbol and are missed.
177 */
178 process_branch_stack(sample->branch_stack, al, sample);
179
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900180 sample->period = 1;
181 sample->weight = 1;
182
Jiri Olsa0102ef32016-06-14 20:19:21 +0200183 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300184 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200185 return -ENOMEM;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300186
Arnaldo Carvalho de Melo00e55212013-12-18 15:46:32 -0300187 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300188 hists__inc_nr_samples(hists, true);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300189 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200190}
191
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200192static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200193 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200194 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300195 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200196 struct machine *machine)
Ingo Molnar8035e422009-06-06 15:19:13 +0200197{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200198 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200199 struct addr_location al;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300200 int ret = 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200201
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300202 if (machine__resolve(machine, &al, sample) < 0) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200203 pr_warning("problem processing %d event, skipping it.\n",
204 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +0200205 return -1;
206 }
207
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200208 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300209 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000210
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200211 if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200212 pr_warning("problem incrementing symbol count, "
213 "skipping event\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300214 ret = -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200215 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300216out_put:
217 addr_location__put(&al);
218 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200219}
220
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900221static int hist_entry__tty_annotate(struct hist_entry *he,
222 struct perf_evsel *evsel,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200223 struct perf_annotate *ann)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200224{
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900225 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel,
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200226 ann->print_line, ann->full_paths, 0, 0);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200227}
228
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300229static void hists__find_annotations(struct hists *hists,
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900230 struct perf_evsel *evsel,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200231 struct perf_annotate *ann)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200232{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300233 struct rb_node *nd = rb_first(&hists->entries), *next;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200234 int key = K_RIGHT;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200235
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300236 while (nd) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200237 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200238 struct annotation *notes;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200239
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300240 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
241 goto find_next;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200242
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200243 notes = symbol__annotation(he->ms.sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200244 if (notes->src == NULL) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300245find_next:
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200246 if (key == K_LEFT)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300247 nd = rb_prev(nd);
248 else
249 nd = rb_next(nd);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200250 continue;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300251 }
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200252
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900253 if (use_browser == 2) {
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900254 int ret;
Namhyung Kimfc672972013-09-13 15:27:43 +0900255 int (*annotate)(struct hist_entry *he,
256 struct perf_evsel *evsel,
257 struct hist_browser_timer *hbt);
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900258
Namhyung Kimfc672972013-09-13 15:27:43 +0900259 annotate = dlsym(perf_gtk_handle,
260 "hist_entry__gtk_annotate");
261 if (annotate == NULL) {
262 ui__error("GTK browser not found!\n");
263 return;
264 }
265
266 ret = annotate(he, evsel, NULL);
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900267 if (!ret || !ann->skip_missing)
268 return;
269
270 /* skip missing symbols */
271 nd = rb_next(nd);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900272 } else if (use_browser == 1) {
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900273 key = hist_entry__tui_annotate(he, evsel, NULL);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300274 switch (key) {
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900275 case -1:
276 if (!ann->skip_missing)
277 return;
278 /* fall through */
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200279 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300280 next = rb_next(nd);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300281 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200282 case K_LEFT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300283 next = rb_prev(nd);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300284 break;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300285 default:
286 return;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300287 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300288
289 if (next != NULL)
290 nd = next;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300291 } else {
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900292 hist_entry__tty_annotate(he, evsel, ann);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300293 nd = rb_next(nd);
294 /*
295 * Since we have a hist_entry per IP for the same
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200296 * symbol, free he->ms.sym->src to signal we already
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300297 * processed this symbol.
298 */
Andi Kleend4957632015-07-18 08:24:48 -0700299 zfree(&notes->src->cycles_hist);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300300 zfree(&notes->src);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300301 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200302 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200303}
304
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200305static int __cmd_annotate(struct perf_annotate *ann)
Ingo Molnar8035e422009-06-06 15:19:13 +0200306{
Li Zefanbab81b62009-12-01 14:04:49 +0800307 int ret;
Namhyung Kimfa10f312014-08-12 15:40:35 +0900308 struct perf_session *session = ann->session;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300309 struct perf_evsel *pos;
310 u64 total_nr_samples;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200311
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200312 if (ann->cpu_list) {
313 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
314 ann->cpu_bitmap);
Anton Blanchard5d67be92011-07-04 21:57:50 +1000315 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900316 goto out;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000317 }
318
Irina Tirdea68e94f42012-10-16 02:33:38 +0300319 if (!objdump_path) {
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300320 ret = perf_env__lookup_objdump(&session->header.env);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300321 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900322 goto out;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300323 }
324
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300325 ret = perf_session__process_events(session);
Li Zefanbab81b62009-12-01 14:04:49 +0800326 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900327 goto out;
Ingo Molnar8035e422009-06-06 15:19:13 +0200328
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200329 if (dump_trace) {
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300330 perf_session__fprintf_nr_events(session, stdout);
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -0300331 perf_evlist__fprintf_nr_events(session->evlist, stdout);
Namhyung Kimfa10f312014-08-12 15:40:35 +0900332 goto out;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200333 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200334
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300335 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200336 perf_session__fprintf(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200337
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300338 if (verbose > 2)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300339 perf_session__fprintf_dsos(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200340
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300341 total_nr_samples = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300342 evlist__for_each_entry(session->evlist, pos) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300343 struct hists *hists = evsel__hists(pos);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300344 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Ingo Molnar8035e422009-06-06 15:19:13 +0200345
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300346 if (nr_samples > 0) {
347 total_nr_samples += nr_samples;
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900348 hists__collapse_resort(hists, NULL);
Kan Liangf9db0d02015-08-11 06:30:48 -0400349 /* Don't sort callchain */
350 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
Jiri Olsa452ce032016-01-18 10:24:00 +0100351 perf_evsel__output_resort(pos, NULL);
Namhyung Kimb1dd4432013-03-05 14:53:25 +0900352
353 if (symbol_conf.event_group &&
354 !perf_evsel__is_group_leader(pos))
355 continue;
356
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900357 hists__find_annotations(hists, pos, ann);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300358 }
359 }
360
361 if (total_nr_samples == 0) {
Namhyung Kimfa10f312014-08-12 15:40:35 +0900362 ui__error("The %s file has no samples!\n", session->file->path);
363 goto out;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300364 }
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900365
Namhyung Kimfc672972013-09-13 15:27:43 +0900366 if (use_browser == 2) {
367 void (*show_annotations)(void);
368
369 show_annotations = dlsym(perf_gtk_handle,
370 "perf_gtk__show_annotations");
371 if (show_annotations == NULL) {
372 ui__error("GTK browser not found!\n");
Namhyung Kimfa10f312014-08-12 15:40:35 +0900373 goto out;
Namhyung Kimfc672972013-09-13 15:27:43 +0900374 }
375 show_annotations();
376 }
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900377
Namhyung Kimfa10f312014-08-12 15:40:35 +0900378out:
Li Zefanbab81b62009-12-01 14:04:49 +0800379 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200380}
381
382static const char * const annotate_usage[] = {
Namhyung Kim99345252012-01-08 02:25:30 +0900383 "perf annotate [<options>]",
Ingo Molnar8035e422009-06-06 15:19:13 +0200384 NULL
385};
386
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300387int cmd_annotate(int argc, const char **argv)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200388{
389 struct perf_annotate annotate = {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200390 .tool = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200391 .sample = process_sample_event,
392 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200393 .mmap2 = perf_event__process_mmap2,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200394 .comm = perf_event__process_comm,
Arnaldo Carvalho de Meloec4622f2012-10-06 15:53:41 -0300395 .exit = perf_event__process_exit,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300396 .fork = perf_event__process_fork,
Hari Bathinif3b36142017-03-08 02:11:43 +0530397 .namespaces = perf_event__process_namespaces,
David Carrillo-Cisneros6ab11f32017-04-10 13:14:29 -0700398 .attr = perf_event__process_attr,
399 .build_id = perf_event__process_build_id,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200400 .ordered_events = true,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200401 .ordering_requires_timestamps = true,
402 },
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200403 };
Namhyung Kimfa10f312014-08-12 15:40:35 +0900404 struct perf_data_file file = {
Namhyung Kimfa10f312014-08-12 15:40:35 +0900405 .mode = PERF_DATA_MODE_READ,
406 };
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200407 const struct option options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +0800408 OPT_STRING('i', "input", &input_name, "file",
Ingo Molnar8035e422009-06-06 15:19:13 +0200409 "input file name"),
Arnaldo Carvalho de Meloac73c5a2010-03-24 16:40:16 -0300410 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
411 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200412 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200413 "symbol to annotate"),
Namhyung Kimfa10f312014-08-12 15:40:35 +0900414 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Ian Munsiec0555642010-04-13 18:37:33 +1000415 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar8035e422009-06-06 15:19:13 +0200416 "be more verbose (show symbol address, etc)"),
Namhyung Kimeddaef82017-02-17 17:17:41 +0900417 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200418 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
419 "dump raw trace in ASCII"),
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900420 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200421 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
422 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200423 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
424 "file", "vmlinux pathname"),
425 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200426 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200427 OPT_BOOLEAN('l', "print-line", &annotate.print_line,
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200428 "print matching source lines (may be slow)"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200429 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
Mike Galbraith42976482009-07-02 08:09:46 +0200430 "Don't shorten the displayed pathnames"),
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900431 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
432 "Skip symbols that cannot be annotated"),
David Ahernc8e66722011-11-13 11:30:08 -0700433 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
He Kuanga7066702016-05-19 11:47:37 +0000434 OPT_CALLBACK(0, "symfs", NULL, "directory",
435 "Look for files with symbols relative to this directory",
436 symbol__config_symfs),
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -0300437 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200438 "Interleave source code with assembly code (default)"),
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -0300439 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200440 "Display raw encoding of assembly instructions (default)"),
Andi Kleenf69b64f2011-09-15 14:31:41 -0700441 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
442 "Specify disassembler style (e.g. -M intel for intel syntax)"),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +0200443 OPT_STRING(0, "objdump", &objdump_path, "path",
444 "objdump binary to use for disassembly and annotations"),
Namhyung Kimb1dd4432013-03-05 14:53:25 +0900445 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
446 "Show event group information together"),
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300447 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
448 "Show a column with the sum of periods"),
Arnaldo Carvalho de Melo53fe4ba2016-07-05 11:08:17 -0300449 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
450 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
451 stdio__config_color, "always"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200452 OPT_END()
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200453 };
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -0300454 int ret = hists__init();
455
456 if (ret < 0)
457 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200458
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200459 argc = parse_options(argc, argv, options, annotate_usage, 0);
Namhyung Kim50e19ef2015-12-10 12:00:53 +0900460 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
468 annotate.sym_hist_filter = argv[0];
469 }
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200470
Namhyung Kimeddaef82017-02-17 17:17:41 +0900471 if (quiet)
472 perf_quiet_option();
473
Martin Liška44848cd2015-05-29 14:06:44 +0200474 file.path = input_name;
475
Namhyung Kimfa10f312014-08-12 15:40:35 +0900476 annotate.session = perf_session__new(&file, false, &annotate.tool);
477 if (annotate.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900478 return -1;
Namhyung Kimfa10f312014-08-12 15:40:35 +0900479
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300480 ret = symbol__annotation_init();
481 if (ret < 0)
482 goto out_delete;
483
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200484 symbol_conf.try_vmlinux_path = true;
485
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900486 ret = symbol__init(&annotate.session->header.env);
Namhyung Kimfa10f312014-08-12 15:40:35 +0900487 if (ret < 0)
488 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200489
Namhyung Kim40184c42015-12-23 02:07:01 +0900490 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +0900491 usage_with_options(annotate_usage, options);
Ingo Molnar8035e422009-06-06 15:19:13 +0200492
Namhyung Kim3df668e72015-12-10 12:00:54 +0900493 if (annotate.use_stdio)
494 use_browser = 0;
495 else if (annotate.use_tui)
496 use_browser = 1;
497 else if (annotate.use_gtk)
498 use_browser = 2;
499
500 setup_browser(true);
501
Namhyung Kimfa10f312014-08-12 15:40:35 +0900502 ret = __cmd_annotate(&annotate);
503
504out_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;
Ingo Molnar8035e422009-06-06 15:19:13 +0200518}