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