perf callchain: Stitch LBR call stack
[linux-block.git] / tools / perf / builtin-report.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
bf9e1876
IM
2/*
3 * builtin-report.c
4 *
5 * Builtin report 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 */
16f762a2 9#include "builtin.h"
53cb8bc2 10
41840d21 11#include "util/config.h"
bf9e1876 12
78f7defe 13#include "util/annotate.h"
8fc0321f 14#include "util/color.h"
4a3cec84 15#include "util/dso.h"
5da50258 16#include <linux/list.h>
43cbcd8a 17#include <linux/rbtree.h>
930f8b34 18#include <linux/err.h>
7f7c536f 19#include <linux/zalloc.h>
1101f69a 20#include "util/map.h"
a2928c42 21#include "util/symbol.h"
d3300a3c
ACM
22#include "util/map_symbol.h"
23#include "util/mem-events.h"
24#include "util/branch.h"
f55c5552 25#include "util/callchain.h"
8d513270 26#include "util/values.h"
8fa66bdc 27
53cb8bc2 28#include "perf.h"
8f28827a 29#include "util/debug.h"
e248de33
ACM
30#include "util/evlist.h"
31#include "util/evsel.h"
ef4b1a53 32#include "util/evswitch.h"
7c6a1c65 33#include "util/header.h"
94c744b6 34#include "util/session.h"
97b9d866 35#include "util/srcline.h"
45694aa7 36#include "util/tool.h"
53cb8bc2 37
4b6ab94e 38#include <subcmd/parse-options.h>
34b7b0f9 39#include <subcmd/exec-cmd.h>
53cb8bc2
IM
40#include "util/parse-events.h"
41
6baa0a5a 42#include "util/thread.h"
dd68ada2 43#include "util/sort.h"
3d1d07ec 44#include "util/hist.h"
f5fc1412 45#include "util/data.h"
68e94f4e 46#include "arch/common.h"
46690a80 47#include "util/time-utils.h"
520a2ebc 48#include "util/auxtrace.h"
58db1d6e 49#include "util/units.h"
2d78b189 50#include "util/branch.h"
fb71c86c 51#include "util/util.h" // perf_tip()
8520a98d 52#include "ui/ui.h"
171f7474 53#include "ui/progress.h"
6f7164fa 54#include "util/block-info.h"
520a2ebc 55
fc67297b 56#include <dlfcn.h>
a43783ae 57#include <errno.h>
fd20e811 58#include <inttypes.h>
1eae20c1 59#include <regex.h>
3052ba56 60#include <linux/ctype.h>
9607ad3a 61#include <signal.h>
5d67be97 62#include <linux/bitmap.h>
8520a98d 63#include <linux/string.h>
531d2410 64#include <linux/stringify.h>
2a1292cb 65#include <linux/time64.h>
7a8ef4c4
ACM
66#include <sys/types.h>
67#include <sys/stat.h>
68#include <unistd.h>
6439d7d1 69#include <linux/mman.h>
5d67be97 70
28b21393 71struct report {
45694aa7 72 struct perf_tool tool;
d20deb64 73 struct perf_session *session;
ef4b1a53 74 struct evswitch evswitch;
2059fc7a 75 bool use_tui, use_gtk, use_stdio;
fa372aae
ACM
76 bool show_full_info;
77 bool show_threads;
78 bool inverted_callchain;
f4f7e28d 79 bool mem_mode;
a4a4d0a7 80 bool stats_mode;
930f8b34 81 bool tasks_mode;
6439d7d1 82 bool mmaps_mode;
5cfe2c82
JO
83 bool header;
84 bool header_only;
98df858e 85 bool nonany_branch_mode;
57b5de46 86 bool group_set;
91e95617 87 int max_stack;
fa372aae 88 struct perf_read_values show_threads_values;
1eddd9e4 89 struct annotation_options annotation_opts;
fa372aae 90 const char *pretty_printing_style;
fa372aae 91 const char *cpu_list;
b14ffaca 92 const char *symbol_filter_str;
46690a80 93 const char *time_str;
0a3cc3ae
JY
94 struct perf_time_interval *ptime_range;
95 int range_size;
5b969bc7 96 int range_num;
064f1981 97 float min_percent;
58c311da 98 u64 nr_entries;
94786b67 99 u64 queue_size;
6f7164fa 100 u64 total_cycles;
21394d94 101 int socket_filter;
fa372aae 102 DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
2d78b189 103 struct branch_type_stat brtype_stat;
ec6ae74f 104 bool symbol_ipc;
6f7164fa
JY
105 bool total_cycles_mode;
106 struct block_report *block_reports;
cca0cc76 107 int nr_block_reports;
d20deb64 108};
5d67be97 109
28b21393 110static int report__config(const char *var, const char *value, void *cb)
00c7e1f1 111{
94786b67
JO
112 struct report *rep = cb;
113
00c7e1f1
NK
114 if (!strcmp(var, "report.group")) {
115 symbol_conf.event_group = perf_config_bool(var, value);
116 return 0;
117 }
eec574e6 118 if (!strcmp(var, "report.percent-limit")) {
2665b452
NK
119 double pcnt = strtof(value, NULL);
120
121 rep->min_percent = pcnt;
122 callchain_param.min_percent = pcnt;
eec574e6
NK
123 return 0;
124 }
8d8e645c
NK
125 if (!strcmp(var, "report.children")) {
126 symbol_conf.cumulate_callchain = perf_config_bool(var, value);
127 return 0;
128 }
25ce4bb8
ACM
129 if (!strcmp(var, "report.queue-size"))
130 return perf_config_u64(&rep->queue_size, var, value);
131
fa1f4565
ACM
132 if (!strcmp(var, "report.sort_order")) {
133 default_sort_order = strdup(value);
134 return 0;
135 }
00c7e1f1 136
b8cbb349 137 return 0;
00c7e1f1
NK
138}
139
9d3c02d7
NK
140static int hist_iter__report_callback(struct hist_entry_iter *iter,
141 struct addr_location *al, bool single,
142 void *arg)
143{
144 int err = 0;
145 struct report *rep = arg;
146 struct hist_entry *he = iter->he;
32dcd021 147 struct evsel *evsel = iter->evsel;
bab89f6a 148 struct perf_sample *sample = iter->sample;
9d3c02d7
NK
149 struct mem_info *mi;
150 struct branch_info *bi;
151
ec6ae74f 152 if (!ui__has_annotation() && !rep->symbol_ipc)
9d3c02d7
NK
153 return 0;
154
155 if (sort__mode == SORT_MODE__BRANCH) {
156 bi = he->branch_info;
e345f3bd 157 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
9d3c02d7
NK
158 if (err)
159 goto out;
160
e345f3bd 161 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
9d3c02d7
NK
162
163 } else if (rep->mem_mode) {
164 mi = he->mem_info;
e345f3bd 165 err = addr_map_symbol__inc_samples(&mi->daddr, sample, evsel);
9d3c02d7
NK
166 if (err)
167 goto out;
168
e345f3bd 169 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
9d3c02d7
NK
170
171 } else if (symbol_conf.cumulate_callchain) {
172 if (single)
e345f3bd 173 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
9d3c02d7 174 } else {
e345f3bd 175 err = hist_entry__inc_addr_samples(he, sample, evsel, al->addr);
9d3c02d7
NK
176 }
177
178out:
179 return err;
f4f7e28d
SE
180}
181
2d78b189
JY
182static int hist_iter__branch_callback(struct hist_entry_iter *iter,
183 struct addr_location *al __maybe_unused,
184 bool single __maybe_unused,
185 void *arg)
186{
187 struct hist_entry *he = iter->he;
188 struct report *rep = arg;
c3b10649 189 struct branch_info *bi = he->branch_info;
40c39e30 190 struct perf_sample *sample = iter->sample;
32dcd021 191 struct evsel *evsel = iter->evsel;
40c39e30
JY
192 int err;
193
c3b10649
JY
194 branch_type_count(&rep->brtype_stat, &bi->flags,
195 bi->from.addr, bi->to.addr);
196
ec6ae74f 197 if (!ui__has_annotation() && !rep->symbol_ipc)
40c39e30
JY
198 return 0;
199
e345f3bd 200 err = addr_map_symbol__inc_samples(&bi->from, sample, evsel);
40c39e30
JY
201 if (err)
202 goto out;
203
e345f3bd 204 err = addr_map_symbol__inc_samples(&bi->to, sample, evsel);
40c39e30 205
40c39e30
JY
206out:
207 return err;
2d78b189
JY
208}
209
57b5de46 210static void setup_forced_leader(struct report *report,
63503dba 211 struct evlist *evlist)
57b5de46 212{
a26bb0ba
JY
213 if (report->group_set)
214 perf_evlist__force_leader(evlist);
57b5de46
JO
215}
216
89f1688a
JO
217static int process_feature_event(struct perf_session *session,
218 union perf_event *event)
57b5de46 219{
89f1688a 220 struct report *rep = container_of(session->tool, struct report, tool);
57b5de46
JO
221
222 if (event->feat.feat_id < HEADER_LAST_FEATURE)
89f1688a 223 return perf_event__process_feature(session, event);
57b5de46
JO
224
225 if (event->feat.feat_id != HEADER_LAST_FEATURE) {
1b8896fb 226 pr_err("failed: wrong feature ID: %" PRI_lu64 "\n",
57b5de46
JO
227 event->feat.feat_id);
228 return -1;
229 }
230
231 /*
92ead7ee
RB
232 * (feat_id = HEADER_LAST_FEATURE) is the end marker which
233 * means all features are received, now we can force the
57b5de46
JO
234 * group if needed.
235 */
236 setup_forced_leader(rep, session->evlist);
237 return 0;
238}
239
45694aa7 240static int process_sample_event(struct perf_tool *tool,
d20deb64 241 union perf_event *event,
8115d60c 242 struct perf_sample *sample,
32dcd021 243 struct evsel *evsel,
743eb868 244 struct machine *machine)
75051724 245{
28b21393 246 struct report *rep = container_of(tool, struct report, tool);
1ed091c4 247 struct addr_location al;
69bcb019 248 struct hist_entry_iter iter = {
063bd936
NK
249 .evsel = evsel,
250 .sample = sample,
b49a8fe5 251 .hide_unresolved = symbol_conf.hide_unresolved,
063bd936 252 .add_entry_cb = hist_iter__report_callback,
69bcb019 253 };
b91fc39f 254 int ret = 0;
180f95e2 255
5b969bc7
JY
256 if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num,
257 sample->time)) {
46690a80 258 return 0;
5b969bc7 259 }
46690a80 260
ef4b1a53
ACM
261 if (evswitch__discard(&rep->evswitch, evsel))
262 return 0;
263
bb3eb566 264 if (machine__resolve(machine, &al, sample) < 0) {
a4210141
NK
265 pr_debug("problem processing %d event, skipping it.\n",
266 event->header.type);
75051724
IM
267 return -1;
268 }
e7fb08b1 269
b49a8fe5 270 if (symbol_conf.hide_unresolved && al.sym == NULL)
b91fc39f 271 goto out_put;
7bec7a91 272
fa372aae 273 if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
b91fc39f 274 goto out_put;
5d67be97 275
f86225db
AH
276 if (sort__mode == SORT_MODE__BRANCH) {
277 /*
278 * A non-synthesized event might not have a branch stack if
279 * branch stacks have been synthesized (using itrace options).
280 */
281 if (!sample->branch_stack)
282 goto out_put;
2d78b189
JY
283
284 iter.add_entry_cb = hist_iter__branch_callback;
69bcb019 285 iter.ops = &hist_iter_branch;
f86225db 286 } else if (rep->mem_mode) {
69bcb019 287 iter.ops = &hist_iter_mem;
f86225db 288 } else if (symbol_conf.cumulate_callchain) {
7a13aa28 289 iter.ops = &hist_iter_cumulative;
f86225db 290 } else {
69bcb019 291 iter.ops = &hist_iter_normal;
f86225db 292 }
69bcb019
NK
293
294 if (al.map != NULL)
295 al.map->dso->hit = 1;
296
6f7164fa 297 if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) {
bdd1666b 298 hist__account_cycles(sample->branch_stack, &al, sample,
6f7164fa
JY
299 rep->nonany_branch_mode,
300 &rep->total_cycles);
bdd1666b
JY
301 }
302
063bd936 303 ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
69bcb019
NK
304 if (ret < 0)
305 pr_debug("problem adding hist entry, skipping event\n");
b91fc39f
ACM
306out_put:
307 addr_location__put(&al);
27a0dcb7 308 return ret;
75051724 309}
3502973d 310
45694aa7 311static int process_read_event(struct perf_tool *tool,
d20deb64 312 union perf_event *event,
1d037ca1 313 struct perf_sample *sample __maybe_unused,
32dcd021 314 struct evsel *evsel,
1d037ca1 315 struct machine *machine __maybe_unused)
e9ea2fde 316{
28b21393 317 struct report *rep = container_of(tool, struct report, tool);
743eb868 318
fa372aae 319 if (rep->show_threads) {
fc50e0ba 320 const char *name = perf_evsel__name(evsel);
89973506 321 int err = perf_read_values_add_value(&rep->show_threads_values,
8d513270 322 event->read.pid, event->read.tid,
9933183e 323 evsel->idx,
8d513270
BG
324 name,
325 event->read.value);
89973506
ACM
326
327 if (err)
328 return err;
8d513270
BG
329 }
330
e9ea2fde
PZ
331 return 0;
332}
333
300aa941 334/* For pipe mode, sample_type is not currently set */
28b21393 335static int report__setup_sample_type(struct report *rep)
d80d338d 336{
c824c433
ACM
337 struct perf_session *session = rep->session;
338 u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
8ceb41d7 339 bool is_pipe = perf_data__is_pipe(session->data);
d20deb64 340
d062ac16 341 if (session->itrace_synth_opts->callchain ||
1c5c25b3 342 session->itrace_synth_opts->add_callchain ||
d062ac16
AH
343 (!is_pipe &&
344 perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
345 !session->itrace_synth_opts->set))
346 sample_type |= PERF_SAMPLE_CALLCHAIN;
347
c7eced63
AH
348 if (session->itrace_synth_opts->last_branch)
349 sample_type |= PERF_SAMPLE_BRANCH_STACK;
350
cc9784bd 351 if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
de7e6a7c 352 if (perf_hpp_list.parent) {
3780f488 353 ui__error("Selected --sort parent, but no "
00894ce9
ACM
354 "callchain data. Did you call "
355 "'perf record' without -g?\n");
d549c769 356 return -EINVAL;
91b4eaea 357 }
b49a821e
JY
358 if (symbol_conf.use_callchain &&
359 !symbol_conf.show_branchflag_count) {
360 ui__error("Selected -g or --branch-history.\n"
361 "But no callchain or branch data.\n"
362 "Did you call 'perf record' without -g or -b?\n");
016e92fb 363 return -1;
91b4eaea 364 }
1cc83815 365 } else if (!callchain_param.enabled &&
fa372aae 366 callchain_param.mode != CHAIN_NONE &&
b9a63b9b 367 !symbol_conf.use_callchain) {
d599db3f 368 symbol_conf.use_callchain = true;
16537f13 369 if (callchain_register_param(&callchain_param) < 0) {
3780f488 370 ui__error("Can't register callchain params.\n");
d549c769 371 return -EINVAL;
b1a88349 372 }
f5970550
PZ
373 }
374
793aaaab
NK
375 if (symbol_conf.cumulate_callchain) {
376 /* Silently ignore if callchain is missing */
377 if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
378 symbol_conf.cumulate_callchain = false;
379 perf_hpp__cancel_cumulate();
380 }
381 }
382
55369fc1 383 if (sort__mode == SORT_MODE__BRANCH) {
cc9784bd 384 if (!is_pipe &&
7f3be652 385 !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
3780f488
NK
386 ui__error("Selected -b but no branch data. "
387 "Did you call perf record without -b?\n");
b50311dc
RAV
388 return -1;
389 }
390 }
391
bb30acae
RB
392 if (sort__mode == SORT_MODE__MEMORY) {
393 if (!is_pipe && !(sample_type & PERF_SAMPLE_DATA_SRC)) {
394 ui__error("Selected --mem-mode but no mem data. "
395 "Did you call perf record without -d?\n");
396 return -1;
397 }
398 }
399
0cdccac6
NK
400 if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
401 if ((sample_type & PERF_SAMPLE_REGS_USER) &&
eabad8c6 402 (sample_type & PERF_SAMPLE_STACK_USER)) {
0cdccac6 403 callchain_param.record_mode = CALLCHAIN_DWARF;
eabad8c6
ACM
404 dwarf_callchain_users = true;
405 } else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
aad2b21c 406 callchain_param.record_mode = CALLCHAIN_LBR;
0cdccac6
NK
407 else
408 callchain_param.record_mode = CALLCHAIN_FP;
409 }
98df858e
AK
410
411 /* ??? handle more cases than just ANY? */
412 if (!(perf_evlist__combined_branch_type(session->evlist) &
413 PERF_SAMPLE_BRANCH_ANY))
414 rep->nonany_branch_mode = true;
415
c3314a74 416#if !defined(HAVE_LIBUNWIND_SUPPORT) && !defined(HAVE_DWARF_SUPPORT)
800d3f56 417 if (dwarf_callchain_users) {
c3314a74
JY
418 ui__warning("Please install libunwind or libdw "
419 "development packages during the perf build.\n");
800d3f56
JY
420 }
421#endif
422
016e92fb
FW
423 return 0;
424}
6142f9ec 425
1d037ca1 426static void sig_handler(int sig __maybe_unused)
46656ac7
TZ
427{
428 session_done = 1;
429}
430
28b21393 431static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
c82ee828
ACM
432 const char *evname, FILE *fp)
433{
434 size_t ret;
435 char unit;
c824c433
ACM
436 unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
437 u64 nr_events = hists->stats.total_period;
32dcd021 438 struct evsel *evsel = hists_to_evsel(hists);
717e263f
NK
439 char buf[512];
440 size_t size = sizeof(buf);
84734b06 441 int socked_id = hists->socket_filter;
717e263f 442
27fafab5
NK
443 if (quiet)
444 return 0;
445
f2148330
NK
446 if (symbol_conf.filter_relative) {
447 nr_samples = hists->stats.nr_non_filtered_samples;
448 nr_events = hists->stats.total_non_filtered_period;
449 }
450
759ff497 451 if (perf_evsel__is_group_event(evsel)) {
32dcd021 452 struct evsel *pos;
717e263f
NK
453
454 perf_evsel__group_desc(evsel, buf, size);
455 evname = buf;
456
457 for_each_group_member(pos, evsel) {
4ea062ed
ACM
458 const struct hists *pos_hists = evsel__hists(pos);
459
f2148330 460 if (symbol_conf.filter_relative) {
4ea062ed
ACM
461 nr_samples += pos_hists->stats.nr_non_filtered_samples;
462 nr_events += pos_hists->stats.total_non_filtered_period;
f2148330 463 } else {
4ea062ed
ACM
464 nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
465 nr_events += pos_hists->stats.total_period;
f2148330 466 }
717e263f
NK
467 }
468 }
c82ee828 469
cc686280
AR
470 nr_samples = convert_unit(nr_samples, &unit);
471 ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
8ef278bb
JO
472 if (evname != NULL) {
473 ret += fprintf(fp, " of event%s '%s'",
5643b1a5 474 evsel->core.nr_members > 1 ? "s" : "", evname);
8ef278bb 475 }
cc686280 476
7425664b
JY
477 if (rep->time_str)
478 ret += fprintf(fp, " (time slices: %s)", rep->time_str);
479
9e207ddf
KL
480 if (symbol_conf.show_ref_callgraph &&
481 strstr(evname, "call-graph=no")) {
482 ret += fprintf(fp, ", show reference callgraph");
483 }
484
f4f7e28d
SE
485 if (rep->mem_mode) {
486 ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
228f14f2 487 ret += fprintf(fp, "\n# Sort order : %s", sort_order ? : default_mem_sort_order);
f4f7e28d
SE
488 } else
489 ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
21394d94 490
84734b06
KL
491 if (socked_id > -1)
492 ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
21394d94 493
c82ee828
ACM
494 return ret + fprintf(fp, "\n#\n");
495}
496
7fa46cbf
JY
497static int perf_evlist__tui_block_hists_browse(struct evlist *evlist,
498 struct report *rep)
499{
500 struct evsel *pos;
501 int i = 0, ret;
502
503 evlist__for_each_entry(evlist, pos) {
504 ret = report__browse_block_hists(&rep->block_reports[i++].hist,
848a5e50
JY
505 rep->min_percent, pos,
506 &rep->session->header.env,
507 &rep->annotation_opts);
7fa46cbf
JY
508 if (ret != 0)
509 return ret;
510 }
511
512 return 0;
513}
514
63503dba 515static int perf_evlist__tty_browse_hists(struct evlist *evlist,
28b21393 516 struct report *rep,
7f0030b2 517 const char *help)
d67f088e 518{
32dcd021 519 struct evsel *pos;
6f7164fa 520 int i = 0;
d67f088e 521
27fafab5
NK
522 if (!quiet) {
523 fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
524 evlist->stats.total_lost_samples);
525 }
526
e5cadb93 527 evlist__for_each_entry(evlist, pos) {
4ea062ed 528 struct hists *hists = evsel__hists(pos);
7289f83c 529 const char *evname = perf_evsel__name(pos);
d67f088e 530
fc24d7c2
NK
531 if (symbol_conf.event_group &&
532 !perf_evsel__is_group_leader(pos))
533 continue;
534
28b21393 535 hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
6f7164fa
JY
536
537 if (rep->total_cycles_mode) {
538 report__browse_block_hists(&rep->block_reports[i++].hist,
848a5e50
JY
539 rep->min_percent, pos,
540 NULL, NULL);
6f7164fa
JY
541 continue;
542 }
543
27fafab5 544 hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
e9de7e2f
ACM
545 !(symbol_conf.use_callchain ||
546 symbol_conf.show_branchflag_count));
d67f088e 547 fprintf(stdout, "\n\n");
d67f088e
ACM
548 }
549
8b53dbef 550 if (!quiet)
d67f088e
ACM
551 fprintf(stdout, "#\n# (%s)\n#\n", help);
552
021162cf
NK
553 if (rep->show_threads) {
554 bool style = !strcmp(rep->pretty_printing_style, "raw");
555 perf_read_values_display(stdout, &rep->show_threads_values,
556 style);
557 perf_read_values_destroy(&rep->show_threads_values);
d67f088e
ACM
558 }
559
2d78b189
JY
560 if (sort__mode == SORT_MODE__BRANCH)
561 branch_type_stat_display(stdout, &rep->brtype_stat);
562
d67f088e
ACM
563 return 0;
564}
565
fad2918e
ACM
566static void report__warn_kptr_restrict(const struct report *rep)
567{
a5e813c6 568 struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
f6fcc143 569 struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
fad2918e 570
9c39ed90
ACM
571 if (perf_evlist__exclude_kernel(rep->session->evlist))
572 return;
573
fad2918e
ACM
574 if (kernel_map == NULL ||
575 (kernel_map->dso->hit &&
576 (kernel_kmap->ref_reloc_sym == NULL ||
577 kernel_kmap->ref_reloc_sym->addr == 0))) {
578 const char *desc =
579 "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
580 "can't be resolved.";
581
e94b861a
ACM
582 if (kernel_map && map__has_symbols(kernel_map)) {
583 desc = "If some relocation was applied (e.g. "
584 "kexec) symbols may be misresolved.";
fad2918e
ACM
585 }
586
587 ui__warning(
588"Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
589"Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
590"Samples in kernel modules can't be resolved as well.\n\n",
591 desc);
592 }
593}
594
8362951b
ACM
595static int report__gtk_browse_hists(struct report *rep, const char *help)
596{
63503dba 597 int (*hist_browser)(struct evlist *evlist, const char *help,
8362951b
ACM
598 struct hist_browser_timer *timer, float min_pcnt);
599
600 hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
601
602 if (hist_browser == NULL) {
603 ui__error("GTK browser not found!\n");
604 return -1;
605 }
606
607 return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
608}
609
610static int report__browse_hists(struct report *rep)
611{
612 int ret;
613 struct perf_session *session = rep->session;
63503dba 614 struct evlist *evlist = session->evlist;
34b7b0f9
NK
615 const char *help = perf_tip(system_path(TIPDIR));
616
617 if (help == NULL) {
618 /* fallback for people who don't install perf ;-) */
619 help = perf_tip(DOCDIR);
620 if (help == NULL)
621 help = "Cannot load tips.txt file, please install perf!";
622 }
8362951b
ACM
623
624 switch (use_browser) {
625 case 1:
7fa46cbf
JY
626 if (rep->total_cycles_mode) {
627 ret = perf_evlist__tui_block_hists_browse(evlist, rep);
628 break;
629 }
630
8362951b
ACM
631 ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
632 rep->min_percent,
06cc1a47 633 &session->header.env,
cd0cccba 634 true, &rep->annotation_opts);
8362951b
ACM
635 /*
636 * Usually "ret" is the last pressed key, and we only
637 * care if the key notifies us to switch data file.
638 */
5e3b810a 639 if (ret != K_SWITCH_INPUT_DATA && ret != K_RELOAD)
8362951b
ACM
640 ret = 0;
641 break;
642 case 2:
643 ret = report__gtk_browse_hists(rep, help);
644 break;
645 default:
646 ret = perf_evlist__tty_browse_hists(evlist, rep, help);
647 break;
648 }
649
650 return ret;
651}
652
5b2ea6f2 653static int report__collapse_hists(struct report *rep)
f6d8b057
ACM
654{
655 struct ui_progress prog;
32dcd021 656 struct evsel *pos;
5b2ea6f2 657 int ret = 0;
f6d8b057 658
58c311da 659 ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
f6d8b057 660
e5cadb93 661 evlist__for_each_entry(rep->session->evlist, pos) {
4ea062ed 662 struct hists *hists = evsel__hists(pos);
f6d8b057
ACM
663
664 if (pos->idx == 0)
665 hists->symbol_filter_str = rep->symbol_filter_str;
666
21394d94
KL
667 hists->socket_filter = rep->socket_filter;
668
5b2ea6f2
NK
669 ret = hists__collapse_resort(hists, &prog);
670 if (ret < 0)
671 break;
f6d8b057
ACM
672
673 /* Non-group events are considered as leader */
674 if (symbol_conf.event_group &&
675 !perf_evsel__is_group_leader(pos)) {
4ea062ed 676 struct hists *leader_hists = evsel__hists(pos->leader);
f6d8b057
ACM
677
678 hists__match(leader_hists, hists);
679 hists__link(leader_hists, hists);
680 }
681 }
682
683 ui_progress__finish();
5b2ea6f2 684 return ret;
f6d8b057
ACM
685}
686
dbd2a1d5
JO
687static int hists__resort_cb(struct hist_entry *he, void *arg)
688{
689 struct report *rep = arg;
690 struct symbol *sym = he->ms.sym;
691
692 if (rep->symbol_ipc && sym && !sym->annotate2) {
32dcd021 693 struct evsel *evsel = hists_to_evsel(he->hists);
dbd2a1d5 694
29754894 695 symbol__annotate2(&he->ms, evsel,
dbd2a1d5
JO
696 &annotation__default_options, NULL);
697 }
698
699 return 0;
700}
701
740b97f9
NK
702static void report__output_resort(struct report *rep)
703{
704 struct ui_progress prog;
32dcd021 705 struct evsel *pos;
740b97f9
NK
706
707 ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
708
dbd2a1d5
JO
709 evlist__for_each_entry(rep->session->evlist, pos) {
710 perf_evsel__output_resort_cb(pos, &prog,
711 hists__resort_cb, rep);
712 }
740b97f9
NK
713
714 ui_progress__finish();
715}
716
a4a4d0a7
JO
717static void stats_setup(struct report *rep)
718{
719 memset(&rep->tool, 0, sizeof(rep->tool));
720 rep->tool.no_warn = true;
721}
722
723static int stats_print(struct report *rep)
724{
725 struct perf_session *session = rep->session;
726
727 perf_session__fprintf_nr_events(session, stdout);
728 return 0;
729}
730
930f8b34
JO
731static void tasks_setup(struct report *rep)
732{
733 memset(&rep->tool, 0, sizeof(rep->tool));
8614ada0 734 rep->tool.ordered_events = true;
6439d7d1
ACM
735 if (rep->mmaps_mode) {
736 rep->tool.mmap = perf_event__process_mmap;
737 rep->tool.mmap2 = perf_event__process_mmap2;
738 }
930f8b34
JO
739 rep->tool.comm = perf_event__process_comm;
740 rep->tool.exit = perf_event__process_exit;
741 rep->tool.fork = perf_event__process_fork;
742 rep->tool.no_warn = true;
743}
744
745struct task {
746 struct thread *thread;
747 struct list_head list;
748 struct list_head children;
749};
750
751static struct task *tasks_list(struct task *task, struct machine *machine)
752{
753 struct thread *parent_thread, *thread = task->thread;
754 struct task *parent_task;
755
756 /* Already listed. */
757 if (!list_empty(&task->list))
758 return NULL;
759
760 /* Last one in the chain. */
761 if (thread->ppid == -1)
762 return task;
763
764 parent_thread = machine__find_thread(machine, -1, thread->ppid);
765 if (!parent_thread)
766 return ERR_PTR(-ENOENT);
767
768 parent_task = thread__priv(parent_thread);
769 list_add_tail(&task->list, &parent_task->children);
770 return tasks_list(parent_task, machine);
771}
772
6439d7d1
ACM
773static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
774{
775 size_t printed = 0;
8efc4f05 776 struct map *map;
6439d7d1 777
8efc4f05 778 maps__for_each_entry(maps, map) {
6439d7d1
ACM
779 printed += fprintf(fp, "%*s %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %" PRIu64 " %s\n",
780 indent, "", map->start, map->end,
781 map->prot & PROT_READ ? 'r' : '-',
782 map->prot & PROT_WRITE ? 'w' : '-',
783 map->prot & PROT_EXEC ? 'x' : '-',
784 map->flags & MAP_SHARED ? 's' : 'p',
785 map->pgoff,
0e3149f8 786 map->dso->id.ino, map->dso->name);
6439d7d1
ACM
787 }
788
789 return printed;
790}
791
930f8b34
JO
792static void task__print_level(struct task *task, FILE *fp, int level)
793{
794 struct thread *thread = task->thread;
795 struct task *child;
6439d7d1
ACM
796 int comm_indent = fprintf(fp, " %8d %8d %8d |%*s",
797 thread->pid_, thread->tid, thread->ppid,
798 level, "");
799
800 fprintf(fp, "%s\n", thread__comm_str(thread));
930f8b34 801
fe87797d 802 maps__fprintf_task(thread->maps, comm_indent, fp);
930f8b34
JO
803
804 if (!list_empty(&task->children)) {
805 list_for_each_entry(child, &task->children, list)
806 task__print_level(child, fp, level + 1);
807 }
808}
809
810static int tasks_print(struct report *rep, FILE *fp)
811{
812 struct perf_session *session = rep->session;
813 struct machine *machine = &session->machines.host;
814 struct task *tasks, *task;
815 unsigned int nr = 0, itask = 0, i;
816 struct rb_node *nd;
817 LIST_HEAD(list);
818
819 /*
820 * No locking needed while accessing machine->threads,
821 * because --tasks is single threaded command.
822 */
823
824 /* Count all the threads. */
825 for (i = 0; i < THREADS__TABLE_SIZE; i++)
826 nr += machine->threads[i].nr;
827
828 tasks = malloc(sizeof(*tasks) * nr);
829 if (!tasks)
830 return -ENOMEM;
831
832 for (i = 0; i < THREADS__TABLE_SIZE; i++) {
833 struct threads *threads = &machine->threads[i];
834
f3acb3a8
DB
835 for (nd = rb_first_cached(&threads->entries); nd;
836 nd = rb_next(nd)) {
930f8b34
JO
837 task = tasks + itask++;
838
839 task->thread = rb_entry(nd, struct thread, rb_node);
840 INIT_LIST_HEAD(&task->children);
841 INIT_LIST_HEAD(&task->list);
842 thread__set_priv(task->thread, task);
843 }
844 }
845
846 /*
847 * Iterate every task down to the unprocessed parent
848 * and link all in task children list. Task with no
849 * parent is added into 'list'.
850 */
851 for (itask = 0; itask < nr; itask++) {
852 task = tasks + itask;
853
854 if (!list_empty(&task->list))
855 continue;
856
857 task = tasks_list(task, machine);
858 if (IS_ERR(task)) {
859 pr_err("Error: failed to process tasks\n");
860 free(tasks);
861 return PTR_ERR(task);
862 }
863
864 if (task)
865 list_add_tail(&task->list, &list);
866 }
867
868 fprintf(fp, "# %8s %8s %8s %s\n", "pid", "tid", "ppid", "comm");
869
870 list_for_each_entry(task, &list, list)
871 task__print_level(task, fp, 0);
872
873 free(tasks);
874 return 0;
875}
876
28b21393 877static int __cmd_report(struct report *rep)
016e92fb 878{
f6d8b057 879 int ret;
993ac88d 880 struct perf_session *session = rep->session;
32dcd021 881 struct evsel *pos;
8ceb41d7 882 struct perf_data *data = session->data;
8fa66bdc 883
46656ac7
TZ
884 signal(SIGINT, sig_handler);
885
fa372aae
ACM
886 if (rep->cpu_list) {
887 ret = perf_session__cpu_bitmap(session, rep->cpu_list,
888 rep->cpu_bitmap);
25b1606b
NK
889 if (ret) {
890 ui__error("failed to set cpu bitmap\n");
d4ae0a6f 891 return ret;
25b1606b 892 }
644e0840 893 session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
5d67be97
AB
894 }
895
89973506
ACM
896 if (rep->show_threads) {
897 ret = perf_read_values_init(&rep->show_threads_values);
898 if (ret)
899 return ret;
900 }
f5970550 901
28b21393 902 ret = report__setup_sample_type(rep);
25b1606b
NK
903 if (ret) {
904 /* report__setup_sample_type() already showed error message */
d4ae0a6f 905 return ret;
25b1606b 906 }
d549c769 907
a4a4d0a7
JO
908 if (rep->stats_mode)
909 stats_setup(rep);
910
930f8b34
JO
911 if (rep->tasks_mode)
912 tasks_setup(rep);
913
b7b61cbe 914 ret = perf_session__process_events(session);
25b1606b
NK
915 if (ret) {
916 ui__error("failed to process sample\n");
d4ae0a6f 917 return ret;
25b1606b 918 }
97b07b69 919
a4a4d0a7
JO
920 if (rep->stats_mode)
921 return stats_print(rep);
922
930f8b34
JO
923 if (rep->tasks_mode)
924 return tasks_print(rep, stdout);
925
fad2918e 926 report__warn_kptr_restrict(rep);
ec80fde7 927
e5cadb93 928 evlist__for_each_entry(session->evlist, pos)
590cd344
NK
929 rep->nr_entries += evsel__hists(pos)->nr_entries;
930
150e465a
NK
931 if (use_browser == 0) {
932 if (verbose > 3)
933 perf_session__fprintf(session, stdout);
9ac99545 934
150e465a
NK
935 if (verbose > 2)
936 perf_session__fprintf_dsos(session, stdout);
16f762a2 937
150e465a
NK
938 if (dump_trace) {
939 perf_session__fprintf_nr_events(session, stdout);
2a1731fb 940 perf_evlist__fprintf_nr_events(session->evlist, stdout);
150e465a
NK
941 return 0;
942 }
71ad0f5e
JO
943 }
944
5b2ea6f2
NK
945 ret = report__collapse_hists(rep);
946 if (ret) {
947 ui__error("failed to process hist entry\n");
948 return ret;
949 }
e248de33 950
33e940a2
ACM
951 if (session_done())
952 return 0;
953
740b97f9
NK
954 /*
955 * recalculate number of entries after collapsing since it
956 * might be changed during the collapse phase.
957 */
958 rep->nr_entries = 0;
e5cadb93 959 evlist__for_each_entry(session->evlist, pos)
740b97f9
NK
960 rep->nr_entries += evsel__hists(pos)->nr_entries;
961
58c311da 962 if (rep->nr_entries == 0) {
2d4f2799 963 ui__error("The %s data has no samples!\n", data->path);
d4ae0a6f 964 return 0;
cbbc79a5
EM
965 }
966
740b97f9 967 report__output_resort(rep);
6e1f601a 968
6f7164fa 969 if (rep->total_cycles_mode) {
cca0cc76
JY
970 int block_hpps[6] = {
971 PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT,
972 PERF_HPP_REPORT__BLOCK_LBR_CYCLES,
973 PERF_HPP_REPORT__BLOCK_CYCLES_PCT,
974 PERF_HPP_REPORT__BLOCK_AVG_CYCLES,
975 PERF_HPP_REPORT__BLOCK_RANGE,
976 PERF_HPP_REPORT__BLOCK_DSO,
977 };
978
6f7164fa 979 rep->block_reports = block_info__create_report(session->evlist,
cca0cc76
JY
980 rep->total_cycles,
981 block_hpps, 6,
982 &rep->nr_block_reports);
6f7164fa
JY
983 if (!rep->block_reports)
984 return -1;
985 }
986
8362951b 987 return report__browse_hists(rep);
8fa66bdc
ACM
988}
989
4eb3e478 990static int
cff6bb46 991report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
4eb3e478 992{
1cc83815 993 struct callchain_param *callchain = opt->value;
c20ab37e 994
1cc83815 995 callchain->enabled = !unset;
b9a63b9b
ACM
996 /*
997 * --no-call-graph
998 */
999 if (unset) {
1cc83815
ACM
1000 symbol_conf.use_callchain = false;
1001 callchain->mode = CHAIN_NONE;
b9a63b9b
ACM
1002 return 0;
1003 }
1004
cff6bb46 1005 return parse_callchain_report_opt(arg);
4eb3e478
FW
1006}
1007
2a1292cb
AK
1008static int
1009parse_time_quantum(const struct option *opt, const char *arg,
1010 int unset __maybe_unused)
1011{
1012 unsigned long *time_q = opt->value;
1013 char *end;
1014
1015 *time_q = strtoul(arg, &end, 0);
1016 if (end == arg)
1017 goto parse_err;
1018 if (*time_q == 0) {
1019 pr_err("time quantum cannot be 0");
1020 return -1;
1021 }
526bbbdd 1022 end = skip_spaces(end);
2a1292cb
AK
1023 if (*end == 0)
1024 return 0;
1025 if (!strcmp(end, "s")) {
1026 *time_q *= NSEC_PER_SEC;
1027 return 0;
1028 }
1029 if (!strcmp(end, "ms")) {
1030 *time_q *= NSEC_PER_MSEC;
1031 return 0;
1032 }
1033 if (!strcmp(end, "us")) {
1034 *time_q *= NSEC_PER_USEC;
1035 return 0;
1036 }
1037 if (!strcmp(end, "ns"))
1038 return 0;
1039parse_err:
1040 pr_err("Cannot parse time quantum `%s'\n", arg);
1041 return -1;
1042}
1043
b21484f1
GP
1044int
1045report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
1046 const char *arg, int unset __maybe_unused)
1047{
1048 if (arg) {
1049 int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
1050 if (err) {
1051 char buf[BUFSIZ];
1052 regerror(err, &ignore_callees_regex, buf, sizeof(buf));
1053 pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
1054 return -1;
1055 }
1056 have_ignore_callees = 1;
1057 }
1058
1059 return 0;
1060}
1061
993ac88d 1062static int
7e6a7998 1063parse_branch_mode(const struct option *opt,
1d037ca1 1064 const char *str __maybe_unused, int unset)
993ac88d 1065{
55369fc1
NK
1066 int *branch_mode = opt->value;
1067
1068 *branch_mode = !unset;
993ac88d
SE
1069 return 0;
1070}
1071
064f1981
NK
1072static int
1073parse_percent_limit(const struct option *opt, const char *str,
1074 int unset __maybe_unused)
1075{
28b21393 1076 struct report *rep = opt->value;
2665b452 1077 double pcnt = strtof(str, NULL);
064f1981 1078
2665b452
NK
1079 rep->min_percent = pcnt;
1080 callchain_param.min_percent = pcnt;
064f1981
NK
1081 return 0;
1082}
1083
b0ad8ea6 1084int cmd_report(int argc, const char **argv)
d20deb64 1085{
993ac88d 1086 struct perf_session *session;
520a2ebc 1087 struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
efad1415 1088 struct stat st;
993ac88d 1089 bool has_br_stack = false;
55369fc1 1090 int branch_mode = -1;
0feba17b 1091 int last_key = 0;
fa94c36c 1092 bool branch_call_mode = false;
9d0199cd 1093#define CALLCHAIN_DEFAULT_OPT "graph,0.5,caller,function,percent"
49b8e2be
RV
1094 static const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
1095 CALLCHAIN_REPORT_HELP
1096 "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
76a26549 1097 char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
d20deb64 1098 const char * const report_usage[] = {
fb2baceb 1099 "perf report [<options>]",
d20deb64
ACM
1100 NULL
1101 };
28b21393 1102 struct report report = {
45694aa7 1103 .tool = {
d20deb64
ACM
1104 .sample = process_sample_event,
1105 .mmap = perf_event__process_mmap,
5c5e854b 1106 .mmap2 = perf_event__process_mmap2,
d20deb64 1107 .comm = perf_event__process_comm,
f3b3614a 1108 .namespaces = perf_event__process_namespaces,
ba78c1c5 1109 .cgroup = perf_event__process_cgroup,
f62d3f0f
ACM
1110 .exit = perf_event__process_exit,
1111 .fork = perf_event__process_fork,
d20deb64
ACM
1112 .lost = perf_event__process_lost,
1113 .read = process_read_event,
1114 .attr = perf_event__process_attr,
d20deb64
ACM
1115 .tracing_data = perf_event__process_tracing_data,
1116 .build_id = perf_event__process_build_id,
520a2ebc
AH
1117 .id_index = perf_event__process_id_index,
1118 .auxtrace_info = perf_event__process_auxtrace_info,
1119 .auxtrace = perf_event__process_auxtrace,
4ab8455f 1120 .event_update = perf_event__process_event_update,
57b5de46 1121 .feature = process_feature_event,
0a8cb85c 1122 .ordered_events = true,
d20deb64
ACM
1123 .ordering_requires_timestamps = true,
1124 },
fe176085 1125 .max_stack = PERF_MAX_STACK_DEPTH,
d20deb64 1126 .pretty_printing_style = "normal",
21394d94 1127 .socket_filter = -1,
1eddd9e4 1128 .annotation_opts = annotation__default_options,
d20deb64
ACM
1129 };
1130 const struct option options[] = {
70cb4e96 1131 OPT_STRING('i', "input", &input_name, "file",
53cb8bc2 1132 "input file name"),
c0555642 1133 OPT_INCR('v', "verbose", &verbose,
815e777f 1134 "be more verbose (show symbol address, etc)"),
27fafab5 1135 OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
97b07b69
IM
1136 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1137 "dump raw trace in ASCII"),
a4a4d0a7 1138 OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
930f8b34 1139 OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
6439d7d1 1140 OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
b32d133a
ACM
1141 OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1142 "file", "vmlinux pathname"),
91340c51
ACM
1143 OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1144 "don't load vmlinux even if found"),
b226a5a7
DA
1145 OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1146 "file", "kallsyms pathname"),
2059fc7a 1147 OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
b32d133a 1148 OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
42976487 1149 "load module symbols - WARNING: use only with -k and LIVE kernel"),
d599db3f 1150 OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
e3d7e183 1151 "Show a column with the number of samples"),
fa372aae 1152 OPT_BOOLEAN('T', "threads", &report.show_threads,
8d513270 1153 "Show per-thread event counters"),
fa372aae 1154 OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
9f866697 1155 "pretty printing style key: normal raw"),
fa372aae 1156 OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
c31a9457 1157 OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
fa372aae
ACM
1158 OPT_BOOLEAN(0, "stdio", &report.use_stdio,
1159 "Use the stdio interface"),
5cfe2c82
JO
1160 OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
1161 OPT_BOOLEAN(0, "header-only", &report.header_only,
1162 "Show only data header."),
63299f05 1163 OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
702fb9b4 1164 sort_help("sort by key(s):")),
a7d945bc 1165 OPT_STRING('F', "fields", &field_order, "key[,keys...]",
702fb9b4 1166 sort_help("output field(s): overhead period sample ")),
b272a59d 1167 OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
a1645ce1 1168 "Show sample percentage for different cpu modes"),
b272a59d
NK
1169 OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
1170 "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
b25bcf2f
IM
1171 OPT_STRING('p', "parent", &parent_pattern, "regex",
1172 "regex filter to identify parent, see: '--sort parent'"),
d599db3f 1173 OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
b8e6d829 1174 "Only display entries with parent-match"),
1cc83815 1175 OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
f2af0086 1176 "print_type,threshold[,print_limit],order,sort_key[,branch],value",
21cf6284
NK
1177 report_callchain_help, &report_parse_callchain_opt,
1178 callchain_default_opt),
793aaaab 1179 OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
aa9d1f83
AK
1180 "Accumulate callchains of children and show total overhead as well. "
1181 "Enabled by default, use --no-children to disable."),
91e95617
WL
1182 OPT_INTEGER(0, "max-stack", &report.max_stack,
1183 "Set the maximum stack depth when parsing the callchain, "
1184 "anything beyond the specified depth will be ignored. "
4cb93446 1185 "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
fa372aae
ACM
1186 OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
1187 "alias for inverted call graph"),
b21484f1
GP
1188 OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1189 "ignore callees of these functions in call graphs",
1190 report_parse_ignore_callees_opt),
655000e7 1191 OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
25903407 1192 "only consider symbols in these dsos"),
c8e66720 1193 OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
cc8b88b1 1194 "only consider symbols in these comms"),
e03eaa40
DA
1195 OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1196 "only consider symbols in these pids"),
1197 OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1198 "only consider symbols in these tids"),
655000e7 1199 OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
7bec7a91 1200 "only consider these symbols"),
b14ffaca
NK
1201 OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
1202 "only show symbols that (partially) match with this filter"),
655000e7 1203 OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
52d422de
ACM
1204 "width[,width...]",
1205 "don't try to adjust column width, use these fixed values"),
0c8c2077 1206 OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
52d422de
ACM
1207 "separator for columns, no spaces will be added between "
1208 "columns '.' is reserved."),
b49a8fe5 1209 OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
71289be7 1210 "Only display entries resolved to a symbol"),
a7066709
HK
1211 OPT_CALLBACK(0, "symfs", NULL, "directory",
1212 "Look for files with symbols relative to this directory",
1213 symbol__config_symfs),
c8e66720 1214 OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
fa372aae
ACM
1215 "list of cpus to profile"),
1216 OPT_BOOLEAN('I', "show-info", &report.show_full_info,
fbe96f29 1217 "Display extended information about perf.data file"),
1eddd9e4 1218 OPT_BOOLEAN(0, "source", &report.annotation_opts.annotate_src,
64c6f0c7 1219 "Interleave source code with assembly code (default)"),
1eddd9e4 1220 OPT_BOOLEAN(0, "asm-raw", &report.annotation_opts.show_asm_raw,
64c6f0c7 1221 "Display raw encoding of assembly instructions (default)"),
a47e843e 1222 OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
f69b64f7 1223 "Specify disassembler style (e.g. -M intel for intel syntax)"),
3b0b16bf
AK
1224 OPT_STRING(0, "prefix", &report.annotation_opts.prefix, "prefix",
1225 "Add prefix to source file path names in programs (with --prefix-strip)"),
1226 OPT_STRING(0, "prefix-strip", &report.annotation_opts.prefix_strip, "N",
1227 "Strip first N entries of source file path name in programs (with --prefix)"),
3f2728bd
ACM
1228 OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1229 "Show a column with the sum of periods"),
57b5de46 1230 OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
01d14f16 1231 "Show event group information together"),
429a5f9d
JY
1232 OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1233 "Sort the output by the event at the index n in group. "
1234 "If n is invalid, sort by the first event. "
1235 "WARNING: should be used on grouped events."),
55369fc1 1236 OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
fa94c36c
AK
1237 "use branch records for per branch histogram filling",
1238 parse_branch_mode),
1239 OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
1240 "add last branch records to call history"),
f178fd2d 1241 OPT_STRING(0, "objdump", &report.annotation_opts.objdump_path, "path",
7a4ec938 1242 "objdump binary to use for disassembly and annotations"),
328ccdac
NK
1243 OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1244 "Disable symbol demangling"),
763122ad
AK
1245 OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1246 "Enable kernel symbol demangling"),
f4f7e28d 1247 OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
4968ac8f
AK
1248 OPT_INTEGER(0, "samples", &symbol_conf.res_sample,
1249 "Number of samples to save per histogram entry for individual browsing"),
064f1981
NK
1250 OPT_CALLBACK(0, "percent-limit", &report, "percent",
1251 "Don't show entries under that percent", parse_percent_limit),
f2148330 1252 OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
33db4568 1253 "how to display percentage of filtered entries", parse_filter_percentage),
520a2ebc 1254 OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
c12e039d 1255 "Instruction Tracing options\n" ITRACE_HELP,
520a2ebc 1256 itrace_parse_synth_opts),
a9710ba0
AK
1257 OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1258 "Show full source file name path for source lines"),
9e207ddf
KL
1259 OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
1260 "Show callgraph from reference event"),
21394d94
KL
1261 OPT_INTEGER(0, "socket-filter", &report.socket_filter,
1262 "only show processor socket that match with this filter"),
053a3989
NK
1263 OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1264 "Show raw trace event output (do not use print fmt or plugins)"),
4251446d
NK
1265 OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
1266 "Show entries in a hierarchy"),
175b968b
ACM
1267 OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
1268 "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
1269 stdio__config_color, "always"),
46690a80
DA
1270 OPT_STRING(0, "time", &report.time_str, "str",
1271 "Time span of interest (start,stop)"),
f3a60646
JY
1272 OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
1273 "Show inline function"),
e6902d1b
JO
1274 OPT_CALLBACK(0, "percent-type", &report.annotation_opts, "local-period",
1275 "Set percent type local/global-period/hits",
1276 annotate_parse_percent_type),
52bab886 1277 OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, "Show times in nanosecs"),
2a1292cb
AK
1278 OPT_CALLBACK(0, "time-quantum", &symbol_conf.time_quantum, "time (ms|us|ns|s)",
1279 "Set time quantum for time sort key (default 100ms)",
1280 parse_time_quantum),
ef4b1a53 1281 OPTS_EVSWITCH(&report.evswitch),
6f7164fa
JY
1282 OPT_BOOLEAN(0, "total-cycles", &report.total_cycles_mode,
1283 "Sort all blocks by 'Sampled Cycles%'"),
53cb8bc2 1284 OPT_END()
d20deb64 1285 };
8ceb41d7 1286 struct perf_data data = {
f5fc1412
JO
1287 .mode = PERF_DATA_MODE_READ,
1288 };
a635fc51 1289 int ret = hists__init();
ec6ae74f 1290 char sort_tmp[128];
a635fc51
ACM
1291
1292 if (ret < 0)
1293 return ret;
53cb8bc2 1294
ecc4c561
ACM
1295 ret = perf_config(report__config, &report);
1296 if (ret)
1297 return ret;
00c7e1f1 1298
655000e7 1299 argc = parse_options(argc, argv, options, report_usage, 0);
b3f38fc2
NK
1300 if (argc) {
1301 /*
1302 * Special case: if there's an argument left then assume that
1303 * it's a symbol filter:
1304 */
1305 if (argc > 1)
1306 usage_with_options(report_usage, options);
1307
1308 report.symbol_filter_str = argv[0];
1309 }
655000e7 1310
3b0b16bf
AK
1311 if (annotate_check_args(&report.annotation_opts) < 0)
1312 return -EINVAL;
1313
6439d7d1
ACM
1314 if (report.mmaps_mode)
1315 report.tasks_mode = true;
1316
27fafab5
NK
1317 if (quiet)
1318 perf_quiet_option();
1319
36c8bb56
LZ
1320 if (symbol_conf.vmlinux_name &&
1321 access(symbol_conf.vmlinux_name, R_OK)) {
1322 pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
1323 return -EINVAL;
1324 }
1325 if (symbol_conf.kallsyms_name &&
1326 access(symbol_conf.kallsyms_name, R_OK)) {
1327 pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
1328 return -EINVAL;
1329 }
1330
fa372aae 1331 if (report.inverted_callchain)
d797fdc5 1332 callchain_param.order = ORDER_CALLER;
792aeafa
NK
1333 if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1334 callchain_param.order = ORDER_CALLER;
d797fdc5 1335
1c5c25b3 1336 if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
188bb5e2
AH
1337 (int)itrace_synth_opts.callchain_sz > report.max_stack)
1338 report.max_stack = itrace_synth_opts.callchain_sz;
1339
70cb4e96 1340 if (!input_name || !strlen(input_name)) {
efad1415 1341 if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
70cb4e96 1342 input_name = "-";
efad1415 1343 else
70cb4e96 1344 input_name = "perf.data";
efad1415 1345 }
ad0de097 1346
2d4f2799
JO
1347 data.path = input_name;
1348 data.force = symbol_conf.force;
f5fc1412 1349
ad0de097 1350repeat:
8ceb41d7 1351 session = perf_session__new(&data, false, &report.tool);
6ef81c55
MI
1352 if (IS_ERR(session))
1353 return PTR_ERR(session);
993ac88d 1354
ef4b1a53
ACM
1355 ret = evswitch__init(&report.evswitch, session->evlist, stderr);
1356 if (ret)
1357 return ret;
1358
cb62c6f1
AB
1359 if (zstd_init(&(session->zstd_data), 0) < 0)
1360 pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
1361
94786b67
JO
1362 if (report.queue_size) {
1363 ordered_events__set_alloc_size(&session->ordered_events,
1364 report.queue_size);
1365 }
1366
520a2ebc
AH
1367 session->itrace_synth_opts = &itrace_synth_opts;
1368
993ac88d
SE
1369 report.session = session;
1370
1371 has_br_stack = perf_header__has_feat(&session->header,
1372 HEADER_BRANCH_STACK);
10ccbc1c
AB
1373 if (perf_evlist__combined_sample_type(session->evlist) & PERF_SAMPLE_STACK_USER)
1374 has_br_stack = false;
efad1415 1375
57b5de46 1376 setup_forced_leader(&report, session->evlist);
ad52b8cb 1377
429a5f9d
JY
1378 if (symbol_conf.group_sort_idx && !session->evlist->nr_groups) {
1379 parse_options_usage(NULL, options, "group-sort-idx", 0);
1380 ret = -EINVAL;
1381 goto error;
1382 }
1383
fb9fab66
AH
1384 if (itrace_synth_opts.last_branch)
1385 has_br_stack = true;
1386
f9a7be7c
JY
1387 if (has_br_stack && branch_call_mode)
1388 symbol_conf.show_branchflag_count = true;
1389
2d78b189
JY
1390 memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
1391
fa94c36c
AK
1392 /*
1393 * Branch mode is a tristate:
1394 * -1 means default, so decide based on the file having branch data.
1395 * 0/1 means the user chose a mode.
1396 */
1397 if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
fefd2d96 1398 !branch_call_mode) {
55369fc1 1399 sort__mode = SORT_MODE__BRANCH;
793aaaab
NK
1400 symbol_conf.cumulate_callchain = false;
1401 }
fa94c36c 1402 if (branch_call_mode) {
09a6a1b0 1403 callchain_param.key = CCKEY_ADDRESS;
fa94c36c
AK
1404 callchain_param.branch_callstack = 1;
1405 symbol_conf.use_callchain = true;
1406 callchain_register_param(&callchain_param);
1407 if (sort_order == NULL)
1408 sort_order = "srcline,symbol,dso";
1409 }
993ac88d 1410
f4f7e28d 1411 if (report.mem_mode) {
55369fc1 1412 if (sort__mode == SORT_MODE__BRANCH) {
a4210141 1413 pr_err("branch and mem mode incompatible\n");
f4f7e28d
SE
1414 goto error;
1415 }
afab87b9 1416 sort__mode = SORT_MODE__MEMORY;
793aaaab 1417 symbol_conf.cumulate_callchain = false;
f4f7e28d 1418 }
a68c2c58 1419
4251446d
NK
1420 if (symbol_conf.report_hierarchy) {
1421 /* disable incompatible options */
4251446d
NK
1422 symbol_conf.cumulate_callchain = false;
1423
1424 if (field_order) {
1425 pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1426 parse_options_usage(report_usage, options, "F", 1);
1427 parse_options_usage(NULL, options, "hierarchy", 0);
1428 goto error;
1429 }
1430
52225036 1431 perf_hpp_list.need_collapse = true;
4251446d
NK
1432 }
1433
712d36db
SS
1434 if (report.use_stdio)
1435 use_browser = 0;
1436 else if (report.use_tui)
1437 use_browser = 1;
1438 else if (report.use_gtk)
1439 use_browser = 2;
1440
b138f42e
NK
1441 /* Force tty output for header output and per-thread stat. */
1442 if (report.header || report.header_only || report.show_threads)
5cfe2c82 1443 use_browser = 0;
114f709e
DCC
1444 if (report.header || report.header_only)
1445 report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1446 if (report.show_full_info)
1447 report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
930f8b34 1448 if (report.stats_mode || report.tasks_mode)
a4a4d0a7 1449 use_browser = 0;
930f8b34 1450 if (report.stats_mode && report.tasks_mode) {
6439d7d1 1451 pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
930f8b34
JO
1452 goto error;
1453 }
5cfe2c82 1454
6f7164fa
JY
1455 if (report.total_cycles_mode) {
1456 if (sort__mode != SORT_MODE__BRANCH)
1457 report.total_cycles_mode = false;
7fa46cbf 1458 else
848a5e50 1459 sort_order = NULL;
6f7164fa
JY
1460 }
1461
4bceffbc
NK
1462 if (strcmp(input_name, "-") != 0)
1463 setup_browser(true);
22af969e 1464 else
4bceffbc 1465 use_browser = 0;
4bceffbc 1466
ec6ae74f
JY
1467 if (sort_order && strstr(sort_order, "ipc")) {
1468 parse_options_usage(report_usage, options, "s", 1);
1469 goto error;
1470 }
1471
1472 if (sort_order && strstr(sort_order, "symbol")) {
1473 if (sort__mode == SORT_MODE__BRANCH) {
1474 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1475 sort_order, "ipc_lbr");
1476 report.symbol_ipc = true;
1477 } else {
1478 snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1479 sort_order, "ipc_null");
1480 }
1481
1482 sort_order = sort_tmp;
1483 }
1484
5e3b810a 1485 if ((last_key != K_SWITCH_INPUT_DATA && last_key != K_RELOAD) &&
0feba17b 1486 (setup_sorting(session->evlist) < 0)) {
9887804d
JO
1487 if (sort_order)
1488 parse_options_usage(report_usage, options, "s", 1);
1489 if (field_order)
1490 parse_options_usage(sort_order ? NULL : report_usage,
1491 options, "F", 1);
1492 goto error;
1493 }
1494
27fafab5 1495 if ((report.header || report.header_only) && !quiet) {
5cfe2c82
JO
1496 perf_session__fprintf_info(session, stdout,
1497 report.show_full_info);
07a716ff
TS
1498 if (report.header_only) {
1499 ret = 0;
1500 goto error;
1501 }
930f8b34
JO
1502 } else if (use_browser == 0 && !quiet &&
1503 !report.stats_mode && !report.tasks_mode) {
5cfe2c82
JO
1504 fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1505 stdout);
1506 }
1507
ef7b93a1 1508 /*
6692c262 1509 * Only in the TUI browser we are doing integrated annotation,
ef7b93a1
ACM
1510 * so don't allocate extra space that won't be used in the stdio
1511 * implementation.
1512 */
6f7164fa
JY
1513 if (ui__has_annotation() || report.symbol_ipc ||
1514 report.total_cycles_mode) {
b01141f4
ACM
1515 ret = symbol__annotation_init();
1516 if (ret < 0)
1517 goto error;
80d50cae
ACM
1518 /*
1519 * For searching by name on the "Browse map details".
1520 * providing it only in verbose mode not to bloat too
1521 * much struct symbol.
1522 */
bb963e16 1523 if (verbose > 0) {
80d50cae
ACM
1524 /*
1525 * XXX: Need to provide a less kludgy way to ask for
1526 * more space per symbol, the u32 is for the index on
1527 * the ui browser.
1528 * See symbol__browser_index.
1529 */
1530 symbol_conf.priv_size += sizeof(u32);
1531 symbol_conf.sort_by_name = true;
1532 }
7384083b 1533 annotation_config__init(&report.annotation_opts);
80d50cae 1534 }
655000e7 1535
0a7e6d1b 1536 if (symbol__init(&session->header.env) < 0)
993ac88d 1537 goto error;
53cb8bc2 1538
284c4e18
JY
1539 if (report.time_str) {
1540 ret = perf_time__parse_for_ranges(report.time_str, session,
1541 &report.ptime_range,
1542 &report.range_size,
1543 &report.range_num);
1544 if (ret < 0)
0a3cc3ae 1545 goto error;
4885c90c
AH
1546
1547 itrace_synth_opts__set_time_range(&itrace_synth_opts,
1548 report.ptime_range,
1549 report.range_num);
46690a80
DA
1550 }
1551
ea85ab24 1552 if (session->tevent.pevent &&
ece2a4f4
TSV
1553 tep_set_function_resolver(session->tevent.pevent,
1554 machine__resolve_kernel_addr,
1555 &session->machines.host) < 0) {
ea85ab24
WY
1556 pr_err("%s: failed to set libtraceevent function resolver\n",
1557 __func__);
1558 return -1;
1559 }
1560
08e71542 1561 sort__setup_elide(stdout);
25903407 1562
993ac88d 1563 ret = __cmd_report(&report);
5e3b810a 1564 if (ret == K_SWITCH_INPUT_DATA || ret == K_RELOAD) {
ad0de097 1565 perf_session__delete(session);
0feba17b 1566 last_key = K_SWITCH_INPUT_DATA;
ad0de097
FT
1567 goto repeat;
1568 } else
1569 ret = 0;
1570
993ac88d 1571error:
4885c90c
AH
1572 if (report.ptime_range) {
1573 itrace_synth_opts__clear_time_range(&itrace_synth_opts);
284c4e18 1574 zfree(&report.ptime_range);
4885c90c 1575 }
6f7164fa 1576
cca0cc76
JY
1577 if (report.block_reports) {
1578 block_info__free_report(report.block_reports,
1579 report.nr_block_reports);
1580 report.block_reports = NULL;
1581 }
6f7164fa 1582
cb62c6f1 1583 zstd_fini(&(session->zstd_data));
993ac88d
SE
1584 perf_session__delete(session);
1585 return ret;
53cb8bc2 1586}