blob: 56a7c8d210b91b55d48a6d01f97f829ed18d3c35 [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>
Anton Blanchard5d67be92011-07-04 21:57:50 +100036#include <linux/bitmap.h>
37
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020038struct perf_annotate {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -020039 struct perf_tool tool;
Namhyung Kimfa10f312014-08-12 15:40:35 +090040 struct perf_session *session;
41 bool use_tui, use_stdio, use_gtk;
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -020042 bool full_paths;
43 bool print_line;
Namhyung Kim18c9e5c2013-02-07 18:02:14 +090044 bool skip_missing;
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -020045 const char *sym_hist_filter;
46 const char *cpu_list;
47 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -020048};
Anton Blanchard5d67be92011-07-04 21:57:50 +100049
Peter Zijlstra70fbe052016-09-05 16:08:12 -030050/*
51 * Given one basic block:
52 *
53 * from to branch_i
54 * * ----> *
55 * |
56 * | block
57 * v
58 * * ----> *
59 * from to branch_i+1
60 *
61 * where the horizontal are the branches and the vertical is the executed
62 * block of instructions.
63 *
64 * We count, for each 'instruction', the number of blocks that covered it as
65 * well as count the ratio each branch is taken.
66 *
67 * We can do this without knowing the actual instruction stream by keeping
68 * track of the address ranges. We break down ranges such that there is no
69 * overlap and iterate from the start until the end.
70 *
71 * @acme: once we parse the objdump output _before_ processing the samples,
72 * we can easily fold the branch.cycles IPC bits in.
73 */
74static void process_basic_block(struct addr_map_symbol *start,
75 struct addr_map_symbol *end,
76 struct branch_flags *flags)
77{
78 struct symbol *sym = start->sym;
79 struct annotation *notes = sym ? symbol__annotation(sym) : NULL;
80 struct block_range_iter iter;
81 struct block_range *entry;
82
83 /*
84 * Sanity; NULL isn't executable and the CPU cannot execute backwards
85 */
86 if (!start->addr || start->addr > end->addr)
87 return;
88
89 iter = block_range__create(start->addr, end->addr);
90 if (!block_range_iter__valid(&iter))
91 return;
92
93 /*
94 * First block in range is a branch target.
95 */
96 entry = block_range_iter(&iter);
97 assert(entry->is_target);
98 entry->entry++;
99
100 do {
101 entry = block_range_iter(&iter);
102
103 entry->coverage++;
104 entry->sym = sym;
105
106 if (notes)
107 notes->max_coverage = max(notes->max_coverage, entry->coverage);
108
109 } while (block_range_iter__next(&iter));
110
111 /*
112 * Last block in rage is a branch.
113 */
114 entry = block_range_iter(&iter);
115 assert(entry->is_branch);
116 entry->taken++;
117 if (flags->predicted)
118 entry->pred++;
119}
120
121static void process_branch_stack(struct branch_stack *bs, struct addr_location *al,
122 struct perf_sample *sample)
123{
124 struct addr_map_symbol *prev = NULL;
125 struct branch_info *bi;
126 int i;
127
128 if (!bs || !bs->nr)
129 return;
130
131 bi = sample__resolve_bstack(sample, al);
132 if (!bi)
133 return;
134
135 for (i = bs->nr - 1; i >= 0; i--) {
136 /*
137 * XXX filter against symbol
138 */
139 if (prev)
140 process_basic_block(prev, &bi[i].from, &bi[i].flags);
141 prev = &bi[i].to;
142 }
143
144 free(bi);
145}
146
Arnaldo Carvalho de Melod04b35f2011-11-11 22:17:32 -0200147static int perf_evsel__add_sample(struct perf_evsel *evsel,
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900148 struct perf_sample *sample,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200149 struct addr_location *al,
150 struct perf_annotate *ann)
Ingo Molnar8035e422009-06-06 15:19:13 +0200151{
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300152 struct hists *hists = evsel__hists(evsel);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300153 struct hist_entry *he;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300154 int ret;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300155
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200156 if (ann->sym_hist_filter != NULL &&
157 (al->sym == NULL ||
158 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300159 /* We're only interested in a symbol named sym_hist_filter */
Arnaldo Carvalho de Melofacf3f02015-05-25 15:30:09 -0300160 /*
161 * FIXME: why isn't this done in the symbol_filter when loading
162 * the DSO?
163 */
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300164 if (al->sym != NULL) {
165 rb_erase(&al->sym->rb_node,
166 &al->map->dso->symbols[al->map->type]);
167 symbol__delete(al->sym);
Arnaldo Carvalho de Meloc0b4dff2015-08-24 13:33:14 -0300168 dso__reset_find_symbol_cache(al->map->dso);
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300169 }
170 return 0;
171 }
172
Peter Zijlstra70fbe052016-09-05 16:08:12 -0300173 /*
174 * XXX filtered samples can still have branch entires pointing into our
175 * symbol and are missed.
176 */
177 process_branch_stack(sample->branch_stack, al, sample);
178
Namhyung Kimfd36f3d2015-12-23 02:06:58 +0900179 sample->period = 1;
180 sample->weight = 1;
181
Jiri Olsa0102ef32016-06-14 20:19:21 +0200182 he = hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
Arnaldo Carvalho de Melo9735abf2009-10-03 10:42:45 -0300183 if (he == NULL)
Ingo Molnar8035e422009-06-06 15:19:13 +0200184 return -ENOMEM;
Arnaldo Carvalho de Melo628ada02010-02-25 12:57:40 -0300185
Arnaldo Carvalho de Melo00e55212013-12-18 15:46:32 -0300186 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300187 hists__inc_nr_samples(hists, true);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300188 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200189}
190
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200191static int process_sample_event(struct perf_tool *tool,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200192 union perf_event *event,
Arnaldo Carvalho de Melo8115d602011-01-29 14:01:45 -0200193 struct perf_sample *sample,
Arnaldo Carvalho de Melo9e69c212011-03-15 15:44:01 -0300194 struct perf_evsel *evsel,
Arnaldo Carvalho de Melo743eb862011-11-28 07:56:39 -0200195 struct machine *machine)
Ingo Molnar8035e422009-06-06 15:19:13 +0200196{
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200197 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
Arnaldo Carvalho de Melo1ed091c2009-11-27 16:29:23 -0200198 struct addr_location al;
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300199 int ret = 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200200
Arnaldo Carvalho de Melobb3eb562016-03-22 18:39:09 -0300201 if (machine__resolve(machine, &al, sample) < 0) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200202 pr_warning("problem processing %d event, skipping it.\n",
203 event->header.type);
Ingo Molnar8035e422009-06-06 15:19:13 +0200204 return -1;
205 }
206
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200207 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300208 goto out_put;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000209
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200210 if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
Arnaldo Carvalho de Melo29a9f662010-02-03 16:52:06 -0200211 pr_warning("problem incrementing symbol count, "
212 "skipping event\n");
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300213 ret = -1;
Ingo Molnar8035e422009-06-06 15:19:13 +0200214 }
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -0300215out_put:
216 addr_location__put(&al);
217 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200218}
219
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900220static int hist_entry__tty_annotate(struct hist_entry *he,
221 struct perf_evsel *evsel,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200222 struct perf_annotate *ann)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200223{
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900224 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel,
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200225 ann->print_line, ann->full_paths, 0, 0);
Ingo Molnar0b73da32009-06-06 15:48:52 +0200226}
227
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300228static void hists__find_annotations(struct hists *hists,
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900229 struct perf_evsel *evsel,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200230 struct perf_annotate *ann)
Ingo Molnar0b73da32009-06-06 15:48:52 +0200231{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300232 struct rb_node *nd = rb_first(&hists->entries), *next;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200233 int key = K_RIGHT;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200234
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300235 while (nd) {
Arnaldo Carvalho de Meloed52ce22009-10-19 17:17:57 -0200236 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200237 struct annotation *notes;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200238
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300239 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
240 goto find_next;
Ingo Molnar0b73da32009-06-06 15:48:52 +0200241
Arnaldo Carvalho de Melo78f7def2011-02-04 09:45:46 -0200242 notes = symbol__annotation(he->ms.sym);
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200243 if (notes->src == NULL) {
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300244find_next:
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200245 if (key == K_LEFT)
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300246 nd = rb_prev(nd);
247 else
248 nd = rb_next(nd);
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200249 continue;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300250 }
Arnaldo Carvalho de Meloe4204992009-10-20 14:25:40 -0200251
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900252 if (use_browser == 2) {
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900253 int ret;
Namhyung Kimfc672972013-09-13 15:27:43 +0900254 int (*annotate)(struct hist_entry *he,
255 struct perf_evsel *evsel,
256 struct hist_browser_timer *hbt);
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900257
Namhyung Kimfc672972013-09-13 15:27:43 +0900258 annotate = dlsym(perf_gtk_handle,
259 "hist_entry__gtk_annotate");
260 if (annotate == NULL) {
261 ui__error("GTK browser not found!\n");
262 return;
263 }
264
265 ret = annotate(he, evsel, NULL);
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900266 if (!ret || !ann->skip_missing)
267 return;
268
269 /* skip missing symbols */
270 nd = rb_next(nd);
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900271 } else if (use_browser == 1) {
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900272 key = hist_entry__tui_annotate(he, evsel, NULL);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300273 switch (key) {
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900274 case -1:
275 if (!ann->skip_missing)
276 return;
277 /* fall through */
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200278 case K_RIGHT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300279 next = rb_next(nd);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300280 break;
Arnaldo Carvalho de Melocf958002011-10-20 16:59:15 -0200281 case K_LEFT:
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300282 next = rb_prev(nd);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300283 break;
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300284 default:
285 return;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300286 }
Arnaldo Carvalho de Melob50e0032010-08-11 10:07:43 -0300287
288 if (next != NULL)
289 nd = next;
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300290 } else {
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900291 hist_entry__tty_annotate(he, evsel, ann);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300292 nd = rb_next(nd);
293 /*
294 * Since we have a hist_entry per IP for the same
Arnaldo Carvalho de Meloce6f4fa2011-02-08 13:27:39 -0200295 * symbol, free he->ms.sym->src to signal we already
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300296 * processed this symbol.
297 */
Andi Kleend4957632015-07-18 08:24:48 -0700298 zfree(&notes->src->cycles_hist);
Arnaldo Carvalho de Melo04662522013-12-26 17:41:15 -0300299 zfree(&notes->src);
Arnaldo Carvalho de Melo46e3e052010-05-22 11:25:40 -0300300 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200301 }
Ingo Molnar0b73da32009-06-06 15:48:52 +0200302}
303
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200304static int __cmd_annotate(struct perf_annotate *ann)
Ingo Molnar8035e422009-06-06 15:19:13 +0200305{
Li Zefanbab81b62009-12-01 14:04:49 +0800306 int ret;
Namhyung Kimfa10f312014-08-12 15:40:35 +0900307 struct perf_session *session = ann->session;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300308 struct perf_evsel *pos;
309 u64 total_nr_samples;
Arnaldo Carvalho de Melo94c744b2009-12-11 21:24:02 -0200310
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200311 if (ann->cpu_list) {
312 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
313 ann->cpu_bitmap);
Anton Blanchard5d67be92011-07-04 21:57:50 +1000314 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900315 goto out;
Anton Blanchard5d67be92011-07-04 21:57:50 +1000316 }
317
Irina Tirdea68e94f42012-10-16 02:33:38 +0300318 if (!objdump_path) {
Arnaldo Carvalho de Meloeebd0bf2015-09-08 15:52:20 -0300319 ret = perf_env__lookup_objdump(&session->header.env);
Irina Tirdea68e94f42012-10-16 02:33:38 +0300320 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900321 goto out;
Irina Tirdea68e94f42012-10-16 02:33:38 +0300322 }
323
Arnaldo Carvalho de Melob7b61cb2015-03-03 11:58:45 -0300324 ret = perf_session__process_events(session);
Li Zefanbab81b62009-12-01 14:04:49 +0800325 if (ret)
Namhyung Kimfa10f312014-08-12 15:40:35 +0900326 goto out;
Ingo Molnar8035e422009-06-06 15:19:13 +0200327
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200328 if (dump_trace) {
Arnaldo Carvalho de Meloc8446b92010-05-14 10:36:42 -0300329 perf_session__fprintf_nr_events(session, stdout);
Arnaldo Carvalho de Melo2a1731f2014-10-10 15:49:21 -0300330 perf_evlist__fprintf_nr_events(session->evlist, stdout);
Namhyung Kimfa10f312014-08-12 15:40:35 +0900331 goto out;
Arnaldo Carvalho de Melo62daacb2009-11-27 16:29:22 -0200332 }
Ingo Molnar8035e422009-06-06 15:19:13 +0200333
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300334 if (verbose > 3)
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -0200335 perf_session__fprintf(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200336
Arnaldo Carvalho de Meloda21d1b2009-10-07 10:49:00 -0300337 if (verbose > 2)
Arnaldo Carvalho de Melocbf69682010-04-27 21:22:44 -0300338 perf_session__fprintf_dsos(session, stdout);
Ingo Molnar8035e422009-06-06 15:19:13 +0200339
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300340 total_nr_samples = 0;
Arnaldo Carvalho de Meloe5cadb92016-06-23 11:26:15 -0300341 evlist__for_each_entry(session->evlist, pos) {
Arnaldo Carvalho de Melo4ea062ed2014-10-09 13:13:41 -0300342 struct hists *hists = evsel__hists(pos);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300343 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
Ingo Molnar8035e422009-06-06 15:19:13 +0200344
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300345 if (nr_samples > 0) {
346 total_nr_samples += nr_samples;
Namhyung Kimc1fb5652013-10-11 14:15:38 +0900347 hists__collapse_resort(hists, NULL);
Kan Liangf9db0d02015-08-11 06:30:48 -0400348 /* Don't sort callchain */
349 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
Jiri Olsa452ce032016-01-18 10:24:00 +0100350 perf_evsel__output_resort(pos, NULL);
Namhyung Kimb1dd4432013-03-05 14:53:25 +0900351
352 if (symbol_conf.event_group &&
353 !perf_evsel__is_group_leader(pos))
354 continue;
355
Namhyung Kimdb8fd072013-03-05 14:53:21 +0900356 hists__find_annotations(hists, pos, ann);
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300357 }
358 }
359
360 if (total_nr_samples == 0) {
Namhyung Kimfa10f312014-08-12 15:40:35 +0900361 ui__error("The %s file has no samples!\n", session->file->path);
362 goto out;
Arnaldo Carvalho de Meloe248de32011-03-05 21:40:06 -0300363 }
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900364
Namhyung Kimfc672972013-09-13 15:27:43 +0900365 if (use_browser == 2) {
366 void (*show_annotations)(void);
367
368 show_annotations = dlsym(perf_gtk_handle,
369 "perf_gtk__show_annotations");
370 if (show_annotations == NULL) {
371 ui__error("GTK browser not found!\n");
Namhyung Kimfa10f312014-08-12 15:40:35 +0900372 goto out;
Namhyung Kimfc672972013-09-13 15:27:43 +0900373 }
374 show_annotations();
375 }
Namhyung Kim7a60ba92013-02-07 18:02:09 +0900376
Namhyung Kimfa10f312014-08-12 15:40:35 +0900377out:
Li Zefanbab81b62009-12-01 14:04:49 +0800378 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200379}
380
381static const char * const annotate_usage[] = {
Namhyung Kim99345252012-01-08 02:25:30 +0900382 "perf annotate [<options>]",
Ingo Molnar8035e422009-06-06 15:19:13 +0200383 NULL
384};
385
Arnaldo Carvalho de Melob0ad8ea2017-03-27 11:47:20 -0300386int cmd_annotate(int argc, const char **argv)
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200387{
388 struct perf_annotate annotate = {
Arnaldo Carvalho de Melo45694aa2011-11-28 08:30:20 -0200389 .tool = {
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200390 .sample = process_sample_event,
391 .mmap = perf_event__process_mmap,
Stephane Eranian5c5e8542013-08-21 12:10:25 +0200392 .mmap2 = perf_event__process_mmap2,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200393 .comm = perf_event__process_comm,
Arnaldo Carvalho de Meloec4622f2012-10-06 15:53:41 -0300394 .exit = perf_event__process_exit,
Arnaldo Carvalho de Melof62d3f02012-10-06 15:44:59 -0300395 .fork = perf_event__process_fork,
Hari Bathinif3b36142017-03-08 02:11:43 +0530396 .namespaces = perf_event__process_namespaces,
Jiri Olsa0a8cb852014-07-06 14:18:21 +0200397 .ordered_events = true,
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200398 .ordering_requires_timestamps = true,
399 },
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200400 };
Namhyung Kimfa10f312014-08-12 15:40:35 +0900401 struct perf_data_file file = {
Namhyung Kimfa10f312014-08-12 15:40:35 +0900402 .mode = PERF_DATA_MODE_READ,
403 };
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200404 const struct option options[] = {
Feng Tang70cb4e92012-10-30 11:56:02 +0800405 OPT_STRING('i', "input", &input_name, "file",
Ingo Molnar8035e422009-06-06 15:19:13 +0200406 "input file name"),
Arnaldo Carvalho de Meloac73c5a2010-03-24 16:40:16 -0300407 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
408 "only consider symbols in these dsos"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200409 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
Ingo Molnar0b73da32009-06-06 15:48:52 +0200410 "symbol to annotate"),
Namhyung Kimfa10f312014-08-12 15:40:35 +0900411 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
Ian Munsiec0555642010-04-13 18:37:33 +1000412 OPT_INCR('v', "verbose", &verbose,
Ingo Molnar8035e422009-06-06 15:19:13 +0200413 "be more verbose (show symbol address, etc)"),
Namhyung Kimeddaef82017-02-17 17:17:41 +0900414 OPT_BOOLEAN('q', "quiet", &quiet, "do now show any message"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200415 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
416 "dump raw trace in ASCII"),
Namhyung Kim2b676bf2013-02-07 18:02:08 +0900417 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200418 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
419 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
Arnaldo Carvalho de Melob32d1332009-11-24 12:05:15 -0200420 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
421 "file", "vmlinux pathname"),
422 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
Mike Galbraith42976482009-07-02 08:09:46 +0200423 "load module symbols - WARNING: use only with -k and LIVE kernel"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200424 OPT_BOOLEAN('l', "print-line", &annotate.print_line,
Frederic Weisbecker301406b2009-06-13 00:11:21 +0200425 "print matching source lines (may be slow)"),
Arnaldo Carvalho de Melo7009cc32011-11-17 12:33:21 -0200426 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
Mike Galbraith42976482009-07-02 08:09:46 +0200427 "Don't shorten the displayed pathnames"),
Namhyung Kim18c9e5c2013-02-07 18:02:14 +0900428 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
429 "Skip symbols that cannot be annotated"),
David Ahernc8e66722011-11-13 11:30:08 -0700430 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
He Kuanga7066702016-05-19 11:47:37 +0000431 OPT_CALLBACK(0, "symfs", NULL, "directory",
432 "Look for files with symbols relative to this directory",
433 symbol__config_symfs),
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -0300434 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200435 "Interleave source code with assembly code (default)"),
Arnaldo Carvalho de Melo64c6f0c2011-10-06 12:48:31 -0300436 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
Stephane Eranian3e6a2a72011-05-17 17:32:07 +0200437 "Display raw encoding of assembly instructions (default)"),
Andi Kleenf69b64f2011-09-15 14:31:41 -0700438 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
439 "Specify disassembler style (e.g. -M intel for intel syntax)"),
Maciek Borzecki7a4ec932012-09-04 12:32:30 +0200440 OPT_STRING(0, "objdump", &objdump_path, "path",
441 "objdump binary to use for disassembly and annotations"),
Namhyung Kimb1dd4432013-03-05 14:53:25 +0900442 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
443 "Show event group information together"),
Martin Liška0c4a5bc2015-06-19 16:10:43 -0300444 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
445 "Show a column with the sum of periods"),
Arnaldo Carvalho de Melo53fe4ba2016-07-05 11:08:17 -0300446 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
447 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
448 stdio__config_color, "always"),
Ingo Molnar8035e422009-06-06 15:19:13 +0200449 OPT_END()
Arnaldo Carvalho de Melod20deb62011-11-25 08:19:45 -0200450 };
Arnaldo Carvalho de Meloa635fc52014-10-09 16:16:00 -0300451 int ret = hists__init();
452
453 if (ret < 0)
454 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200455
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200456 argc = parse_options(argc, argv, options, annotate_usage, 0);
Namhyung Kim50e19ef2015-12-10 12:00:53 +0900457 if (argc) {
458 /*
459 * Special case: if there's an argument left then assume that
460 * it's a symbol filter:
461 */
462 if (argc > 1)
463 usage_with_options(annotate_usage, options);
464
465 annotate.sym_hist_filter = argv[0];
466 }
Arnaldo Carvalho de Melo655000e2009-12-15 20:04:40 -0200467
Namhyung Kimeddaef82017-02-17 17:17:41 +0900468 if (quiet)
469 perf_quiet_option();
470
Martin Liška44848cd2015-05-29 14:06:44 +0200471 file.path = input_name;
472
Namhyung Kimfa10f312014-08-12 15:40:35 +0900473 annotate.session = perf_session__new(&file, false, &annotate.tool);
474 if (annotate.session == NULL)
Taeung Song52e028342014-09-24 10:33:37 +0900475 return -1;
Namhyung Kimfa10f312014-08-12 15:40:35 +0900476
Arnaldo Carvalho de Melob01141f2016-08-25 16:09:21 -0300477 ret = symbol__annotation_init();
478 if (ret < 0)
479 goto out_delete;
480
Arnaldo Carvalho de Melo75be6cf2009-12-15 20:04:39 -0200481 symbol_conf.try_vmlinux_path = true;
482
Namhyung Kim0a7e6d12014-08-12 15:40:45 +0900483 ret = symbol__init(&annotate.session->header.env);
Namhyung Kimfa10f312014-08-12 15:40:35 +0900484 if (ret < 0)
485 goto out_delete;
Ingo Molnar8035e422009-06-06 15:19:13 +0200486
Namhyung Kim40184c42015-12-23 02:07:01 +0900487 if (setup_sorting(NULL) < 0)
Namhyung Kim55309982013-02-06 14:57:16 +0900488 usage_with_options(annotate_usage, options);
Ingo Molnar8035e422009-06-06 15:19:13 +0200489
Namhyung Kim3df668e72015-12-10 12:00:54 +0900490 if (annotate.use_stdio)
491 use_browser = 0;
492 else if (annotate.use_tui)
493 use_browser = 1;
494 else if (annotate.use_gtk)
495 use_browser = 2;
496
497 setup_browser(true);
498
Namhyung Kimfa10f312014-08-12 15:40:35 +0900499 ret = __cmd_annotate(&annotate);
500
501out_delete:
502 /*
503 * Speed up the exit process, for large files this can
504 * take quite a while.
505 *
506 * XXX Enable this when using valgrind or if we ever
507 * librarize this command.
508 *
509 * Also experiment with obstacks to see how much speed
510 * up we'll get here.
511 *
512 * perf_session__delete(session);
513 */
514 return ret;
Ingo Molnar8035e422009-06-06 15:19:13 +0200515}