Merge tag 'regulator-v4.6' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[linux-2.6-block.git] / tools / perf / builtin-annotate.c
CommitLineData
8035e428
IM
1/*
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
78f7defe 10#include "util/util.h"
8035e428 11#include "util/color.h"
5da50258 12#include <linux/list.h>
8035e428 13#include "util/cache.h"
43cbcd8a 14#include <linux/rbtree.h>
8035e428 15#include "util/symbol.h"
8035e428
IM
16
17#include "perf.h"
8f28827a 18#include "util/debug.h"
8035e428 19
e248de33
ACM
20#include "util/evlist.h"
21#include "util/evsel.h"
78f7defe 22#include "util/annotate.h"
62daacb5 23#include "util/event.h"
4b6ab94e 24#include <subcmd/parse-options.h>
8035e428 25#include "util/parse-events.h"
6baa0a5a 26#include "util/thread.h"
dd68ada2 27#include "util/sort.h"
3d1d07ec 28#include "util/hist.h"
94c744b6 29#include "util/session.h"
45694aa7 30#include "util/tool.h"
f5fc1412 31#include "util/data.h"
68e94f4e 32#include "arch/common.h"
8035e428 33
fc67297b 34#include <dlfcn.h>
5d67be97
AB
35#include <linux/bitmap.h>
36
d20deb64 37struct perf_annotate {
45694aa7 38 struct perf_tool tool;
fa10f316
NK
39 struct perf_session *session;
40 bool use_tui, use_stdio, use_gtk;
7009cc34
ACM
41 bool full_paths;
42 bool print_line;
18c9e5c5 43 bool skip_missing;
7009cc34
ACM
44 const char *sym_hist_filter;
45 const char *cpu_list;
46 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
d20deb64 47};
5d67be97 48
d04b35f8 49static int perf_evsel__add_sample(struct perf_evsel *evsel,
fd36f3dd 50 struct perf_sample *sample,
d20deb64
ACM
51 struct addr_location *al,
52 struct perf_annotate *ann)
8035e428 53{
4ea062ed 54 struct hists *hists = evsel__hists(evsel);
628ada0c 55 struct hist_entry *he;
e248de33 56 int ret;
628ada0c 57
7009cc34
ACM
58 if (ann->sym_hist_filter != NULL &&
59 (al->sym == NULL ||
60 strcmp(ann->sym_hist_filter, al->sym->name) != 0)) {
628ada0c 61 /* We're only interested in a symbol named sym_hist_filter */
facf3f06
ACM
62 /*
63 * FIXME: why isn't this done in the symbol_filter when loading
64 * the DSO?
65 */
628ada0c
ACM
66 if (al->sym != NULL) {
67 rb_erase(&al->sym->rb_node,
68 &al->map->dso->symbols[al->map->type]);
69 symbol__delete(al->sym);
c0b4dffb 70 dso__reset_find_symbol_cache(al->map->dso);
628ada0c
ACM
71 }
72 return 0;
73 }
74
fd36f3dd
NK
75 sample->period = 1;
76 sample->weight = 1;
77
78 he = __hists__add_entry(hists, al, NULL, NULL, NULL, sample, true);
9735abf1 79 if (he == NULL)
8035e428 80 return -ENOMEM;
628ada0c 81
00e55218 82 ret = hist_entry__inc_addr_samples(he, evsel->idx, al->addr);
4ea062ed 83 hists__inc_nr_samples(hists, true);
e248de33 84 return ret;
8035e428
IM
85}
86
45694aa7 87static int process_sample_event(struct perf_tool *tool,
d20deb64 88 union perf_event *event,
8115d60c 89 struct perf_sample *sample,
9e69c210 90 struct perf_evsel *evsel,
743eb868 91 struct machine *machine)
8035e428 92{
45694aa7 93 struct perf_annotate *ann = container_of(tool, struct perf_annotate, tool);
1ed091c4 94 struct addr_location al;
b91fc39f 95 int ret = 0;
6baa0a5a 96
e44baa3e 97 if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
29a9f66d
ACM
98 pr_warning("problem processing %d event, skipping it.\n",
99 event->header.type);
8035e428
IM
100 return -1;
101 }
102
7009cc34 103 if (ann->cpu_list && !test_bit(sample->cpu, ann->cpu_bitmap))
b91fc39f 104 goto out_put;
5d67be97 105
d20deb64 106 if (!al.filtered && perf_evsel__add_sample(evsel, sample, &al, ann)) {
29a9f66d
ACM
107 pr_warning("problem incrementing symbol count, "
108 "skipping event\n");
b91fc39f 109 ret = -1;
8035e428 110 }
b91fc39f
ACM
111out_put:
112 addr_location__put(&al);
113 return ret;
8035e428
IM
114}
115
db8fd07a
NK
116static int hist_entry__tty_annotate(struct hist_entry *he,
117 struct perf_evsel *evsel,
d20deb64 118 struct perf_annotate *ann)
0b73da3f 119{
db8fd07a 120 return symbol__tty_annotate(he->ms.sym, he->ms.map, evsel,
7009cc34 121 ann->print_line, ann->full_paths, 0, 0);
0b73da3f
IM
122}
123
c824c433 124static void hists__find_annotations(struct hists *hists,
db8fd07a 125 struct perf_evsel *evsel,
d20deb64 126 struct perf_annotate *ann)
0b73da3f 127{
c824c433 128 struct rb_node *nd = rb_first(&hists->entries), *next;
cf958003 129 int key = K_RIGHT;
0b73da3f 130
46e3e055 131 while (nd) {
ed52ce2e 132 struct hist_entry *he = rb_entry(nd, struct hist_entry, rb_node);
78f7defe 133 struct annotation *notes;
0b73da3f 134
46e3e055
ACM
135 if (he->ms.sym == NULL || he->ms.map->dso->annotate_warned)
136 goto find_next;
0b73da3f 137
78f7defe 138 notes = symbol__annotation(he->ms.sym);
ce6f4fab 139 if (notes->src == NULL) {
46e3e055 140find_next:
cf958003 141 if (key == K_LEFT)
46e3e055
ACM
142 nd = rb_prev(nd);
143 else
144 nd = rb_next(nd);
e4204992 145 continue;
46e3e055 146 }
e4204992 147
2b676bf0 148 if (use_browser == 2) {
18c9e5c5 149 int ret;
fc67297b
NK
150 int (*annotate)(struct hist_entry *he,
151 struct perf_evsel *evsel,
152 struct hist_browser_timer *hbt);
153
154 annotate = dlsym(perf_gtk_handle,
155 "hist_entry__gtk_annotate");
156 if (annotate == NULL) {
157 ui__error("GTK browser not found!\n");
158 return;
159 }
18c9e5c5 160
fc67297b 161 ret = annotate(he, evsel, NULL);
18c9e5c5
NK
162 if (!ret || !ann->skip_missing)
163 return;
164
165 /* skip missing symbols */
166 nd = rb_next(nd);
2b676bf0 167 } else if (use_browser == 1) {
db8fd07a 168 key = hist_entry__tui_annotate(he, evsel, NULL);
46e3e055 169 switch (key) {
18c9e5c5
NK
170 case -1:
171 if (!ann->skip_missing)
172 return;
173 /* fall through */
cf958003 174 case K_RIGHT:
b50e003d 175 next = rb_next(nd);
46e3e055 176 break;
cf958003 177 case K_LEFT:
b50e003d 178 next = rb_prev(nd);
46e3e055 179 break;
b50e003d
ACM
180 default:
181 return;
46e3e055 182 }
b50e003d
ACM
183
184 if (next != NULL)
185 nd = next;
46e3e055 186 } else {
db8fd07a 187 hist_entry__tty_annotate(he, evsel, ann);
46e3e055
ACM
188 nd = rb_next(nd);
189 /*
190 * Since we have a hist_entry per IP for the same
ce6f4fab 191 * symbol, free he->ms.sym->src to signal we already
46e3e055
ACM
192 * processed this symbol.
193 */
d4957633 194 zfree(&notes->src->cycles_hist);
04662523 195 zfree(&notes->src);
46e3e055 196 }
0b73da3f 197 }
0b73da3f
IM
198}
199
d20deb64 200static int __cmd_annotate(struct perf_annotate *ann)
8035e428 201{
bab81b62 202 int ret;
fa10f316 203 struct perf_session *session = ann->session;
e248de33
ACM
204 struct perf_evsel *pos;
205 u64 total_nr_samples;
94c744b6 206
476d35c2
AH
207 machines__set_symbol_filter(&session->machines, symbol__annotate_init);
208
7009cc34
ACM
209 if (ann->cpu_list) {
210 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
211 ann->cpu_bitmap);
5d67be97 212 if (ret)
fa10f316 213 goto out;
5d67be97
AB
214 }
215
68e94f4e 216 if (!objdump_path) {
eebd0bfc 217 ret = perf_env__lookup_objdump(&session->header.env);
68e94f4e 218 if (ret)
fa10f316 219 goto out;
68e94f4e
IT
220 }
221
b7b61cbe 222 ret = perf_session__process_events(session);
bab81b62 223 if (ret)
fa10f316 224 goto out;
8035e428 225
62daacb5 226 if (dump_trace) {
c8446b9b 227 perf_session__fprintf_nr_events(session, stdout);
2a1731fb 228 perf_evlist__fprintf_nr_events(session->evlist, stdout);
fa10f316 229 goto out;
62daacb5 230 }
8035e428 231
da21d1b5 232 if (verbose > 3)
b3165f41 233 perf_session__fprintf(session, stdout);
8035e428 234
da21d1b5 235 if (verbose > 2)
cbf69680 236 perf_session__fprintf_dsos(session, stdout);
8035e428 237
e248de33 238 total_nr_samples = 0;
0050f7aa 239 evlist__for_each(session->evlist, pos) {
4ea062ed 240 struct hists *hists = evsel__hists(pos);
e248de33
ACM
241 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
242
243 if (nr_samples > 0) {
244 total_nr_samples += nr_samples;
c1fb5651 245 hists__collapse_resort(hists, NULL);
f9db0d0f
KL
246 /* Don't sort callchain */
247 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
452ce03b 248 perf_evsel__output_resort(pos, NULL);
b1dd4432
NK
249
250 if (symbol_conf.event_group &&
251 !perf_evsel__is_group_leader(pos))
252 continue;
253
db8fd07a 254 hists__find_annotations(hists, pos, ann);
e248de33
ACM
255 }
256 }
8035e428 257
e248de33 258 if (total_nr_samples == 0) {
fa10f316
NK
259 ui__error("The %s file has no samples!\n", session->file->path);
260 goto out;
e248de33 261 }
7a60ba94 262
fc67297b
NK
263 if (use_browser == 2) {
264 void (*show_annotations)(void);
265
266 show_annotations = dlsym(perf_gtk_handle,
267 "perf_gtk__show_annotations");
268 if (show_annotations == NULL) {
269 ui__error("GTK browser not found!\n");
fa10f316 270 goto out;
fc67297b
NK
271 }
272 show_annotations();
273 }
7a60ba94 274
fa10f316 275out:
bab81b62 276 return ret;
8035e428
IM
277}
278
279static const char * const annotate_usage[] = {
99345254 280 "perf annotate [<options>]",
8035e428
IM
281 NULL
282};
283
1d037ca1 284int cmd_annotate(int argc, const char **argv, const char *prefix __maybe_unused)
d20deb64
ACM
285{
286 struct perf_annotate annotate = {
45694aa7 287 .tool = {
d20deb64
ACM
288 .sample = process_sample_event,
289 .mmap = perf_event__process_mmap,
5c5e854b 290 .mmap2 = perf_event__process_mmap2,
d20deb64 291 .comm = perf_event__process_comm,
ec4622f5 292 .exit = perf_event__process_exit,
f62d3f0f 293 .fork = perf_event__process_fork,
0a8cb85c 294 .ordered_events = true,
d20deb64
ACM
295 .ordering_requires_timestamps = true,
296 },
d20deb64 297 };
fa10f316 298 struct perf_data_file file = {
fa10f316
NK
299 .mode = PERF_DATA_MODE_READ,
300 };
d20deb64 301 const struct option options[] = {
70cb4e96 302 OPT_STRING('i', "input", &input_name, "file",
8035e428 303 "input file name"),
ac73c5a9
ACM
304 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
305 "only consider symbols in these dsos"),
7009cc34 306 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
0b73da3f 307 "symbol to annotate"),
fa10f316 308 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
c0555642 309 OPT_INCR('v', "verbose", &verbose,
8035e428
IM
310 "be more verbose (show symbol address, etc)"),
311 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
312 "dump raw trace in ASCII"),
2b676bf0 313 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
7009cc34
ACM
314 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
315 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
b32d133a
ACM
316 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
317 "file", "vmlinux pathname"),
318 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 319 "load module symbols - WARNING: use only with -k and LIVE kernel"),
7009cc34 320 OPT_BOOLEAN('l', "print-line", &annotate.print_line,
301406b9 321 "print matching source lines (may be slow)"),
7009cc34 322 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
42976487 323 "Don't shorten the displayed pathnames"),
18c9e5c5
NK
324 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
325 "Skip symbols that cannot be annotated"),
c8e66720 326 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
e71a0598
SE
327 OPT_STRING(0, "symfs", &symbol_conf.symfs, "directory",
328 "Look for files with symbols relative to this directory"),
64c6f0c7 329 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
3e6a2a7f 330 "Interleave source code with assembly code (default)"),
64c6f0c7 331 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
3e6a2a7f 332 "Display raw encoding of assembly instructions (default)"),
f69b64f7
AK
333 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
334 "Specify disassembler style (e.g. -M intel for intel syntax)"),
7a4ec938
MB
335 OPT_STRING(0, "objdump", &objdump_path, "path",
336 "objdump binary to use for disassembly and annotations"),
b1dd4432
NK
337 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
338 "Show event group information together"),
0c4a5bce
ML
339 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
340 "Show a column with the sum of periods"),
8035e428 341 OPT_END()
d20deb64 342 };
a635fc51
ACM
343 int ret = hists__init();
344
345 if (ret < 0)
346 return ret;
8035e428 347
655000e7 348 argc = parse_options(argc, argv, options, annotate_usage, 0);
50e19ef9
NK
349 if (argc) {
350 /*
351 * Special case: if there's an argument left then assume that
352 * it's a symbol filter:
353 */
354 if (argc > 1)
355 usage_with_options(annotate_usage, options);
356
357 annotate.sym_hist_filter = argv[0];
358 }
655000e7 359
44848cdb
ML
360 file.path = input_name;
361
fa10f316
NK
362 annotate.session = perf_session__new(&file, false, &annotate.tool);
363 if (annotate.session == NULL)
52e02834 364 return -1;
fa10f316 365
78f7defe 366 symbol_conf.priv_size = sizeof(struct annotation);
75be6cf4
ACM
367 symbol_conf.try_vmlinux_path = true;
368
0a7e6d1b 369 ret = symbol__init(&annotate.session->header.env);
fa10f316
NK
370 if (ret < 0)
371 goto out_delete;
8035e428 372
40184c46 373 if (setup_sorting(NULL) < 0)
55309985 374 usage_with_options(annotate_usage, options);
8035e428 375
3df668e7
NK
376 if (annotate.use_stdio)
377 use_browser = 0;
378 else if (annotate.use_tui)
379 use_browser = 1;
380 else if (annotate.use_gtk)
381 use_browser = 2;
382
383 setup_browser(true);
384
fa10f316
NK
385 ret = __cmd_annotate(&annotate);
386
387out_delete:
388 /*
389 * Speed up the exit process, for large files this can
390 * take quite a while.
391 *
392 * XXX Enable this when using valgrind or if we ever
393 * librarize this command.
394 *
395 * Also experiment with obstacks to see how much speed
396 * up we'll get here.
397 *
398 * perf_session__delete(session);
399 */
400 return ret;
8035e428 401}