perf record: Mark MAP_HUGETLB when synthesizing mmap events
[linux-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
0102ef3e 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
bb3eb566 97 if (machine__resolve(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
7009cc34
ACM
207 if (ann->cpu_list) {
208 ret = perf_session__cpu_bitmap(session, ann->cpu_list,
209 ann->cpu_bitmap);
5d67be97 210 if (ret)
fa10f316 211 goto out;
5d67be97
AB
212 }
213
68e94f4e 214 if (!objdump_path) {
eebd0bfc 215 ret = perf_env__lookup_objdump(&session->header.env);
68e94f4e 216 if (ret)
fa10f316 217 goto out;
68e94f4e
IT
218 }
219
b7b61cbe 220 ret = perf_session__process_events(session);
bab81b62 221 if (ret)
fa10f316 222 goto out;
8035e428 223
62daacb5 224 if (dump_trace) {
c8446b9b 225 perf_session__fprintf_nr_events(session, stdout);
2a1731fb 226 perf_evlist__fprintf_nr_events(session->evlist, stdout);
fa10f316 227 goto out;
62daacb5 228 }
8035e428 229
da21d1b5 230 if (verbose > 3)
b3165f41 231 perf_session__fprintf(session, stdout);
8035e428 232
da21d1b5 233 if (verbose > 2)
cbf69680 234 perf_session__fprintf_dsos(session, stdout);
8035e428 235
e248de33 236 total_nr_samples = 0;
e5cadb93 237 evlist__for_each_entry(session->evlist, pos) {
4ea062ed 238 struct hists *hists = evsel__hists(pos);
e248de33
ACM
239 u32 nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
240
241 if (nr_samples > 0) {
242 total_nr_samples += nr_samples;
c1fb5651 243 hists__collapse_resort(hists, NULL);
f9db0d0f
KL
244 /* Don't sort callchain */
245 perf_evsel__reset_sample_bit(pos, CALLCHAIN);
452ce03b 246 perf_evsel__output_resort(pos, NULL);
b1dd4432
NK
247
248 if (symbol_conf.event_group &&
249 !perf_evsel__is_group_leader(pos))
250 continue;
251
db8fd07a 252 hists__find_annotations(hists, pos, ann);
e248de33
ACM
253 }
254 }
8035e428 255
e248de33 256 if (total_nr_samples == 0) {
fa10f316
NK
257 ui__error("The %s file has no samples!\n", session->file->path);
258 goto out;
e248de33 259 }
7a60ba94 260
fc67297b
NK
261 if (use_browser == 2) {
262 void (*show_annotations)(void);
263
264 show_annotations = dlsym(perf_gtk_handle,
265 "perf_gtk__show_annotations");
266 if (show_annotations == NULL) {
267 ui__error("GTK browser not found!\n");
fa10f316 268 goto out;
fc67297b
NK
269 }
270 show_annotations();
271 }
7a60ba94 272
fa10f316 273out:
bab81b62 274 return ret;
8035e428
IM
275}
276
277static const char * const annotate_usage[] = {
99345254 278 "perf annotate [<options>]",
8035e428
IM
279 NULL
280};
281
1d037ca1 282int cmd_annotate(int argc, const char **argv, const char *prefix __maybe_unused)
d20deb64
ACM
283{
284 struct perf_annotate annotate = {
45694aa7 285 .tool = {
d20deb64
ACM
286 .sample = process_sample_event,
287 .mmap = perf_event__process_mmap,
5c5e854b 288 .mmap2 = perf_event__process_mmap2,
d20deb64 289 .comm = perf_event__process_comm,
ec4622f5 290 .exit = perf_event__process_exit,
f62d3f0f 291 .fork = perf_event__process_fork,
0a8cb85c 292 .ordered_events = true,
d20deb64
ACM
293 .ordering_requires_timestamps = true,
294 },
d20deb64 295 };
fa10f316 296 struct perf_data_file file = {
fa10f316
NK
297 .mode = PERF_DATA_MODE_READ,
298 };
d20deb64 299 const struct option options[] = {
70cb4e96 300 OPT_STRING('i', "input", &input_name, "file",
8035e428 301 "input file name"),
ac73c5a9
ACM
302 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
303 "only consider symbols in these dsos"),
7009cc34 304 OPT_STRING('s', "symbol", &annotate.sym_hist_filter, "symbol",
0b73da3f 305 "symbol to annotate"),
fa10f316 306 OPT_BOOLEAN('f', "force", &file.force, "don't complain, do it"),
c0555642 307 OPT_INCR('v', "verbose", &verbose,
8035e428
IM
308 "be more verbose (show symbol address, etc)"),
309 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
310 "dump raw trace in ASCII"),
2b676bf0 311 OPT_BOOLEAN(0, "gtk", &annotate.use_gtk, "Use the GTK interface"),
7009cc34
ACM
312 OPT_BOOLEAN(0, "tui", &annotate.use_tui, "Use the TUI interface"),
313 OPT_BOOLEAN(0, "stdio", &annotate.use_stdio, "Use the stdio interface"),
b32d133a
ACM
314 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
315 "file", "vmlinux pathname"),
316 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 317 "load module symbols - WARNING: use only with -k and LIVE kernel"),
7009cc34 318 OPT_BOOLEAN('l', "print-line", &annotate.print_line,
301406b9 319 "print matching source lines (may be slow)"),
7009cc34 320 OPT_BOOLEAN('P', "full-paths", &annotate.full_paths,
42976487 321 "Don't shorten the displayed pathnames"),
18c9e5c5
NK
322 OPT_BOOLEAN(0, "skip-missing", &annotate.skip_missing,
323 "Skip symbols that cannot be annotated"),
c8e66720 324 OPT_STRING('C', "cpu", &annotate.cpu_list, "cpu", "list of cpus to profile"),
a7066709
HK
325 OPT_CALLBACK(0, "symfs", NULL, "directory",
326 "Look for files with symbols relative to this directory",
327 symbol__config_symfs),
64c6f0c7 328 OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
3e6a2a7f 329 "Interleave source code with assembly code (default)"),
64c6f0c7 330 OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
3e6a2a7f 331 "Display raw encoding of assembly instructions (default)"),
f69b64f7
AK
332 OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
333 "Specify disassembler style (e.g. -M intel for intel syntax)"),
7a4ec938
MB
334 OPT_STRING(0, "objdump", &objdump_path, "path",
335 "objdump binary to use for disassembly and annotations"),
b1dd4432
NK
336 OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
337 "Show event group information together"),
0c4a5bce
ML
338 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
339 "Show a column with the sum of periods"),
53fe4ba1
ACM
340 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
341 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
342 stdio__config_color, "always"),
8035e428 343 OPT_END()
d20deb64 344 };
a635fc51
ACM
345 int ret = hists__init();
346
347 if (ret < 0)
348 return ret;
8035e428 349
655000e7 350 argc = parse_options(argc, argv, options, annotate_usage, 0);
50e19ef9
NK
351 if (argc) {
352 /*
353 * Special case: if there's an argument left then assume that
354 * it's a symbol filter:
355 */
356 if (argc > 1)
357 usage_with_options(annotate_usage, options);
358
359 annotate.sym_hist_filter = argv[0];
360 }
655000e7 361
44848cdb
ML
362 file.path = input_name;
363
fa10f316
NK
364 annotate.session = perf_session__new(&file, false, &annotate.tool);
365 if (annotate.session == NULL)
52e02834 366 return -1;
fa10f316 367
b01141f4
ACM
368 ret = symbol__annotation_init();
369 if (ret < 0)
370 goto out_delete;
371
75be6cf4
ACM
372 symbol_conf.try_vmlinux_path = true;
373
0a7e6d1b 374 ret = symbol__init(&annotate.session->header.env);
fa10f316
NK
375 if (ret < 0)
376 goto out_delete;
8035e428 377
40184c46 378 if (setup_sorting(NULL) < 0)
55309985 379 usage_with_options(annotate_usage, options);
8035e428 380
3df668e7
NK
381 if (annotate.use_stdio)
382 use_browser = 0;
383 else if (annotate.use_tui)
384 use_browser = 1;
385 else if (annotate.use_gtk)
386 use_browser = 2;
387
388 setup_browser(true);
389
fa10f316
NK
390 ret = __cmd_annotate(&annotate);
391
392out_delete:
393 /*
394 * Speed up the exit process, for large files this can
395 * take quite a while.
396 *
397 * XXX Enable this when using valgrind or if we ever
398 * librarize this command.
399 *
400 * Also experiment with obstacks to see how much speed
401 * up we'll get here.
402 *
403 * perf_session__delete(session);
404 */
405 return ret;
8035e428 406}