xref: /linux/tools/perf/builtin-report.c (revision 6a98990010d9d512fef0943cff80d222c90287a6)
1 // SPDX-License-Identifier: GPL-2.0
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  */
9 #include "builtin.h"
10 
11 #include "util/config.h"
12 
13 #include "util/annotate.h"
14 #include "util/color.h"
15 #include "util/dso.h"
16 #include <linux/list.h>
17 #include <linux/rbtree.h>
18 #include <linux/err.h>
19 #include <linux/zalloc.h>
20 #include "util/map.h"
21 #include "util/symbol.h"
22 #include "util/map_symbol.h"
23 #include "util/mem-events.h"
24 #include "util/branch.h"
25 #include "util/callchain.h"
26 #include "util/values.h"
27 
28 #include "perf.h"
29 #include "util/debug.h"
30 #include "util/event.h"
31 #include "util/evlist.h"
32 #include "util/evsel.h"
33 #include "util/evswitch.h"
34 #include "util/header.h"
35 #include "util/mem-info.h"
36 #include "util/session.h"
37 #include "util/srcline.h"
38 #include "util/tool.h"
39 
40 #include <subcmd/parse-options.h>
41 #include <subcmd/exec-cmd.h>
42 #include "util/parse-events.h"
43 
44 #include "util/thread.h"
45 #include "util/sort.h"
46 #include "util/hist.h"
47 #include "util/data.h"
48 #include "arch/common.h"
49 #include "util/time-utils.h"
50 #include "util/auxtrace.h"
51 #include "util/units.h"
52 #include "util/unwind.h"
53 #include "util/util.h" // perf_tip()
54 #include "ui/keysyms.h"
55 #include "ui/ui.h"
56 #include "ui/progress.h"
57 #include "util/block-info.h"
58 
59 #include <dlfcn.h>
60 #include <errno.h>
61 #include <inttypes.h>
62 #include <regex.h>
63 #include <linux/ctype.h>
64 #include <signal.h>
65 #include <linux/bitmap.h>
66 #include <linux/list_sort.h>
67 #include <linux/string.h>
68 #include <linux/stringify.h>
69 #include <linux/time64.h>
70 #include <sys/types.h>
71 #include <sys/stat.h>
72 #include <unistd.h>
73 #include <linux/mman.h>
74 
75 #ifdef HAVE_LIBTRACEEVENT
76 #include <event-parse.h>
77 #endif
78 
79 struct report {
80 	struct perf_tool	tool;
81 	struct perf_session	*session;
82 	struct evswitch		evswitch;
83 #ifdef HAVE_SLANG_SUPPORT
84 	bool			use_tui;
85 #endif
86 #ifdef HAVE_GTK4_SUPPORT
87 	bool			use_gtk;
88 #endif
89 	bool			use_stdio;
90 	bool			progress;
91 	bool			progress_set;
92 	bool			show_full_info;
93 	bool			show_threads;
94 	bool			inverted_callchain;
95 	bool			mem_mode;
96 	bool			stats_mode;
97 	bool			tasks_mode;
98 	bool			mmaps_mode;
99 	bool			header;
100 	bool			header_only;
101 	bool			nonany_branch_mode;
102 	bool			group_set;
103 	bool			stitch_lbr;
104 	bool			disable_order;
105 	bool			skip_empty;
106 	bool			data_type;
107 	int			max_stack;
108 	struct perf_read_values	show_threads_values;
109 	const char		*pretty_printing_style;
110 	const char		*cpu_list;
111 	const char		*symbol_filter_str;
112 	const char		*time_str;
113 	struct perf_time_interval *ptime_range;
114 	int			range_size;
115 	int			range_num;
116 	float			min_percent;
117 	u64			nr_entries;
118 	u64			queue_size;
119 	u64			total_cycles;
120 	u64			total_samples;
121 	u64			singlethreaded_samples;
122 	int			socket_filter;
123 	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
124 	struct branch_type_stat	brtype_stat;
125 	bool			symbol_ipc;
126 	bool			total_cycles_mode;
127 	struct block_report	*block_reports;
128 	int			nr_block_reports;
129 };
130 
131 static int report__config(const char *var, const char *value, void *cb)
132 {
133 	struct report *rep = cb;
134 
135 	if (!strcmp(var, "report.group")) {
136 		symbol_conf.event_group = perf_config_bool(var, value);
137 		return 0;
138 	}
139 	if (!strcmp(var, "report.percent-limit")) {
140 		double pcnt = strtof(value, NULL);
141 
142 		rep->min_percent = pcnt;
143 		callchain_param.min_percent = pcnt;
144 		return 0;
145 	}
146 	if (!strcmp(var, "report.children")) {
147 		symbol_conf.cumulate_callchain = perf_config_bool(var, value);
148 		return 0;
149 	}
150 	if (!strcmp(var, "report.queue-size"))
151 		return perf_config_u64(&rep->queue_size, var, value);
152 
153 	if (!strcmp(var, "report.sort_order")) {
154 		default_sort_order = strdup(value);
155 		if (!default_sort_order) {
156 			pr_err("Not enough memory for report.sort_order\n");
157 			return -1;
158 		}
159 		return 0;
160 	}
161 
162 	if (!strcmp(var, "report.skip-empty")) {
163 		rep->skip_empty = perf_config_bool(var, value);
164 		return 0;
165 	}
166 
167 	pr_debug("%s variable unknown, ignoring...", var);
168 	return 0;
169 }
170 
171 static int hist_iter__report_callback(struct hist_entry_iter *iter,
172 				      struct addr_location *al, bool single,
173 				      void *arg)
174 {
175 	int err = 0;
176 	struct report *rep = arg;
177 	struct hist_entry *he = iter->he;
178 	struct perf_sample *sample = iter->sample;
179 	struct mem_info *mi;
180 	struct branch_info *bi;
181 
182 	if (!ui__has_annotation() && !rep->symbol_ipc)
183 		return 0;
184 
185 	if (sort__mode == SORT_MODE__BRANCH) {
186 		bi = he->branch_info;
187 		err = addr_map_symbol__inc_samples(&bi->from, sample);
188 		if (err)
189 			goto out;
190 
191 		err = addr_map_symbol__inc_samples(&bi->to, sample);
192 
193 	} else if (rep->mem_mode) {
194 		mi = he->mem_info;
195 		err = addr_map_symbol__inc_samples(mem_info__daddr(mi), sample);
196 		if (err)
197 			goto out;
198 
199 		err = hist_entry__inc_addr_samples(he, sample, al->addr);
200 
201 	} else if (symbol_conf.cumulate_callchain) {
202 		if (single)
203 			err = hist_entry__inc_addr_samples(he, sample, al->addr);
204 	} else {
205 		err = hist_entry__inc_addr_samples(he, sample, al->addr);
206 	}
207 
208 out:
209 	return err;
210 }
211 
212 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
213 				      struct addr_location *al __maybe_unused,
214 				      bool single __maybe_unused,
215 				      void *arg)
216 {
217 	struct hist_entry *he = iter->he;
218 	struct report *rep = arg;
219 	struct branch_info *bi = he->branch_info;
220 	struct perf_sample *sample = iter->sample;
221 	int err;
222 
223 	branch_type_count(&rep->brtype_stat, &bi->flags,
224 			  bi->from.addr, bi->to.addr);
225 
226 	if (!ui__has_annotation() && !rep->symbol_ipc)
227 		return 0;
228 
229 	err = addr_map_symbol__inc_samples(&bi->from, sample);
230 	if (err)
231 		goto out;
232 
233 	err = addr_map_symbol__inc_samples(&bi->to, sample);
234 
235 out:
236 	return err;
237 }
238 
239 static void setup_forced_leader(struct report *report,
240 				struct evlist *evlist)
241 {
242 	if (report->group_set)
243 		evlist__force_leader(evlist);
244 }
245 
246 static int process_feature_event(const struct perf_tool *tool,
247 				 struct perf_session *session,
248 				 union perf_event *event)
249 {
250 	struct report *rep = container_of(tool, struct report, tool);
251 	int ret = perf_event__process_feature(tool, session, event);
252 
253 	if (ret == 0 && event->header.size == sizeof(struct perf_record_header_feature) &&
254 	    (int)event->feat.feat_id >= session->header.last_feat) {
255 		/*
256 		 * (feat_id = HEADER_LAST_FEATURE) is the end marker which means
257 		 * all features are received.
258 		 */
259 		if (rep->header_only)
260 			session_done = 1;
261 
262 		setup_forced_leader(rep, session->evlist);
263 	}
264 	return ret;
265 }
266 
267 static int process_sample_event(const struct perf_tool *tool,
268 				union perf_event *event,
269 				struct perf_sample *sample,
270 				struct machine *machine)
271 {
272 	struct report *rep = container_of(tool, struct report, tool);
273 	struct addr_location al;
274 	struct hist_entry_iter iter = {
275 		.sample 		= sample,
276 		.hide_unresolved 	= symbol_conf.hide_unresolved,
277 		.add_entry_cb 		= hist_iter__report_callback,
278 	};
279 	int ret = 0;
280 
281 	if (perf_time__ranges_skip_sample(rep->ptime_range, rep->range_num,
282 					  sample->time)) {
283 		return 0;
284 	}
285 
286 	if (evswitch__discard(&rep->evswitch, sample->evsel))
287 		return 0;
288 
289 	addr_location__init(&al);
290 	if (machine__resolve(machine, &al, sample) < 0) {
291 		pr_debug("problem processing %s (%u) event at offset %#" PRIx64 ", skipping it.\n",
292 			 perf_event__name(event->header.type), event->header.type,
293 			 sample->file_offset);
294 		ret = -1;
295 		goto out_put;
296 	}
297 
298 	if (rep->stitch_lbr)
299 		thread__set_lbr_stitch_enable(al.thread, true);
300 
301 	if (symbol_conf.hide_unresolved && al.sym == NULL)
302 		goto out_put;
303 
304 	if (rep->cpu_list && (sample->cpu >= MAX_NR_CPUS ||
305 			     !test_bit(sample->cpu, rep->cpu_bitmap)))
306 		goto out_put;
307 
308 	if (sort__mode == SORT_MODE__BRANCH) {
309 		/*
310 		 * A non-synthesized event might not have a branch stack if
311 		 * branch stacks have been synthesized (using itrace options).
312 		 */
313 		if (!sample->branch_stack)
314 			goto out_put;
315 
316 		iter.add_entry_cb = hist_iter__branch_callback;
317 		iter.ops = &hist_iter_branch;
318 	} else if (rep->mem_mode) {
319 		iter.ops = &hist_iter_mem;
320 	} else if (symbol_conf.cumulate_callchain) {
321 		iter.ops = &hist_iter_cumulative;
322 	} else {
323 		iter.ops = &hist_iter_normal;
324 	}
325 
326 	if (al.map != NULL)
327 		dso__set_hit(map__dso(al.map));
328 
329 	if (ui__has_annotation() || rep->symbol_ipc || rep->total_cycles_mode) {
330 		hist__account_cycles(sample->branch_stack, &al, sample,
331 				     rep->nonany_branch_mode,
332 				     &rep->total_cycles);
333 	}
334 
335 	rep->total_samples++;
336 	if (al.parallelism == 1)
337 		rep->singlethreaded_samples++;
338 
339 	ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
340 	if (ret < 0)
341 		pr_debug("problem adding hist entry at offset %#" PRIx64 ", skipping event\n",
342 			 sample->file_offset);
343 out_put:
344 	addr_location__exit(&al);
345 	return ret;
346 }
347 
348 static int process_read_event(const struct perf_tool *tool,
349 			      union perf_event *event,
350 			      struct perf_sample *sample __maybe_unused,
351 			      struct machine *machine __maybe_unused)
352 {
353 	struct report *rep = container_of(tool, struct report, tool);
354 
355 	if (rep->show_threads) {
356 		int err = perf_read_values_add_value(&rep->show_threads_values,
357 					   event->read.pid, event->read.tid,
358 					   sample->evsel,
359 					   event->read.value);
360 
361 		if (err)
362 			return err;
363 	}
364 
365 	return 0;
366 }
367 
368 /* For pipe mode, sample_type is not currently set */
369 static int report__setup_sample_type(struct report *rep)
370 {
371 	struct perf_session *session = rep->session;
372 	u64 sample_type = evlist__combined_sample_type(session->evlist);
373 	bool is_pipe = perf_data__is_pipe(session->data);
374 	struct evsel *evsel;
375 
376 	if (session->itrace_synth_opts->callchain ||
377 	    session->itrace_synth_opts->add_callchain ||
378 	    (!is_pipe &&
379 	     perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
380 	     !session->itrace_synth_opts->set))
381 		sample_type |= PERF_SAMPLE_CALLCHAIN;
382 
383 	if (session->itrace_synth_opts->last_branch ||
384 	    session->itrace_synth_opts->add_last_branch)
385 		sample_type |= PERF_SAMPLE_BRANCH_STACK;
386 
387 	if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
388 		if (perf_hpp_list.parent) {
389 			ui__error("Selected --sort parent, but no "
390 				    "callchain data. Did you call "
391 				    "'perf record' without -g?\n");
392 			return -EINVAL;
393 		}
394 		if (symbol_conf.use_callchain &&
395 			!symbol_conf.show_branchflag_count) {
396 			ui__error("Selected -g or --branch-history.\n"
397 				  "But no callchain or branch data.\n"
398 				  "Did you call 'perf record' without -g or -b?\n");
399 			return -1;
400 		}
401 	} else if (!callchain_param.enabled &&
402 		   callchain_param.mode != CHAIN_NONE &&
403 		   !symbol_conf.use_callchain) {
404 			symbol_conf.use_callchain = true;
405 			if (callchain_register_param(&callchain_param) < 0) {
406 				ui__error("Can't register callchain params.\n");
407 				return -EINVAL;
408 			}
409 	}
410 
411 	if (symbol_conf.cumulate_callchain) {
412 		/* Silently ignore if callchain is missing */
413 		if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
414 			symbol_conf.cumulate_callchain = false;
415 			perf_hpp__cancel_cumulate(session->evlist);
416 		}
417 	}
418 
419 	if (sort__mode == SORT_MODE__BRANCH) {
420 		if (!is_pipe &&
421 		    !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
422 			ui__error("Selected -b but no branch data. "
423 				  "Did you call perf record without -b?\n");
424 			return -1;
425 		}
426 	}
427 
428 	if (sort__mode == SORT_MODE__MEMORY) {
429 		/*
430 		 * FIXUP: prior to kernel 5.18, Arm SPE missed to set
431 		 * PERF_SAMPLE_DATA_SRC bit in sample type.  For backward
432 		 * compatibility, set the bit if it's an old perf data file.
433 		 */
434 		evlist__for_each_entry(session->evlist, evsel) {
435 			if (strstr(evsel__name(evsel), "arm_spe") &&
436 				!(sample_type & PERF_SAMPLE_DATA_SRC)) {
437 				evsel->core.attr.sample_type |= PERF_SAMPLE_DATA_SRC;
438 				sample_type |= PERF_SAMPLE_DATA_SRC;
439 			}
440 		}
441 
442 		if (!is_pipe && !(sample_type & PERF_SAMPLE_DATA_SRC)) {
443 			ui__error("Selected --mem-mode but no mem data. "
444 				  "Did you call perf record without -d?\n");
445 			return -1;
446 		}
447 	}
448 
449 	callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
450 
451 	if (rep->stitch_lbr && (callchain_param.record_mode != CALLCHAIN_LBR)) {
452 		ui__warning("Can't find LBR callchain. Switch off --stitch-lbr.\n"
453 			    "Please apply --call-graph lbr when recording.\n");
454 		rep->stitch_lbr = false;
455 	}
456 
457 	/* ??? handle more cases than just ANY? */
458 	if (!(evlist__combined_branch_type(session->evlist) & PERF_SAMPLE_BRANCH_ANY))
459 		rep->nonany_branch_mode = true;
460 
461 #if !defined(HAVE_LIBUNWIND_SUPPORT) && !defined(HAVE_LIBDW_SUPPORT)
462 	if (dwarf_callchain_users) {
463 		ui__warning("Please install libunwind or libdw "
464 			    "development packages during the perf build.\n");
465 	}
466 #endif
467 
468 	return 0;
469 }
470 
471 static void sig_handler(int sig __maybe_unused)
472 {
473 	session_done = 1;
474 }
475 
476 static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
477 					      const char *evname, FILE *fp)
478 {
479 	size_t ret;
480 	char unit;
481 	unsigned long nr_samples = hists->stats.nr_samples;
482 	u64 nr_events = hists->stats.total_period;
483 	struct evsel *evsel = hists_to_evsel(hists);
484 	char buf[512];
485 	size_t size = sizeof(buf);
486 	int socked_id = hists->socket_filter;
487 
488 	if (quiet)
489 		return 0;
490 
491 	if (symbol_conf.filter_relative) {
492 		nr_samples = hists->stats.nr_non_filtered_samples;
493 		nr_events = hists->stats.total_non_filtered_period;
494 	}
495 
496 	if (evsel__is_group_event(evsel)) {
497 		struct evsel *pos;
498 
499 		evsel__group_desc(evsel, buf, size);
500 		evname = buf;
501 
502 		for_each_group_member(pos, evsel) {
503 			const struct hists *pos_hists = evsel__hists(pos);
504 
505 			if (symbol_conf.filter_relative) {
506 				nr_samples += pos_hists->stats.nr_non_filtered_samples;
507 				nr_events += pos_hists->stats.total_non_filtered_period;
508 			} else {
509 				nr_samples += pos_hists->stats.nr_samples;
510 				nr_events += pos_hists->stats.total_period;
511 			}
512 		}
513 	}
514 
515 	nr_samples = convert_unit(nr_samples, &unit);
516 	ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
517 	if (evname != NULL) {
518 		ret += fprintf(fp, " of event%s '%s'",
519 			       evsel->core.nr_members > 1 ? "s" : "", evname);
520 	}
521 
522 	if (rep->time_str)
523 		ret += fprintf(fp, " (time slices: %s)", rep->time_str);
524 
525 	if (symbol_conf.show_ref_callgraph && evname && strstr(evname, "call-graph=no")) {
526 		ret += fprintf(fp, ", show reference callgraph");
527 	}
528 
529 	if (rep->mem_mode) {
530 		ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
531 		if (sort_order || !field_order) {
532 			ret += fprintf(fp, "\n# Sort order   : %s",
533 				       sort_order ? : default_mem_sort_order);
534 		}
535 	} else
536 		ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
537 
538 	if (socked_id > -1)
539 		ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
540 
541 	return ret + fprintf(fp, "\n#\n");
542 }
543 
544 static int evlist__tui_block_hists_browse(struct evlist *evlist, struct report *rep)
545 {
546 	struct evsel *pos;
547 	int i = 0, ret;
548 
549 	evlist__for_each_entry(evlist, pos) {
550 		ret = report__browse_block_hists(&rep->block_reports[i++].hist,
551 						 rep->min_percent, pos,
552 						 perf_session__env(rep->session));
553 		if (ret != 0)
554 			return ret;
555 	}
556 
557 	return 0;
558 }
559 
560 static int evlist__tty_browse_hists(struct evlist *evlist, struct report *rep, const char *help)
561 {
562 	struct evsel *pos;
563 	int i = 0;
564 
565 	if (!quiet) {
566 		fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
567 			evlist__stats(evlist)->total_lost_samples);
568 	}
569 
570 	evlist__for_each_entry(evlist, pos) {
571 		struct hists *hists = evsel__hists(pos);
572 		const char *evname = evsel__name(pos);
573 
574 		i++;
575 		if (symbol_conf.event_group && !evsel__is_group_leader(pos))
576 			continue;
577 
578 		if (rep->skip_empty && !hists->stats.nr_samples)
579 			continue;
580 
581 		hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
582 
583 		if (rep->total_cycles_mode) {
584 			char *buf;
585 
586 			if (!annotation_br_cntr_abbr_list(&buf, pos, true)) {
587 				fprintf(stdout, "%s", buf);
588 				fprintf(stdout, "#\n");
589 				free(buf);
590 			}
591 			report__browse_block_hists(&rep->block_reports[i - 1].hist,
592 						   rep->min_percent, pos, NULL);
593 			continue;
594 		}
595 
596 		hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
597 			       !(symbol_conf.use_callchain ||
598 			         symbol_conf.show_branchflag_count));
599 		fprintf(stdout, "\n\n");
600 	}
601 
602 	if (!quiet)
603 		fprintf(stdout, "#\n# (%s)\n#\n", help);
604 
605 	if (rep->show_threads) {
606 		bool style = !strcmp(rep->pretty_printing_style, "raw");
607 		perf_read_values_display(stdout, &rep->show_threads_values,
608 					 style);
609 		perf_read_values_destroy(&rep->show_threads_values);
610 	}
611 
612 	if (sort__mode == SORT_MODE__BRANCH)
613 		branch_type_stat_display(stdout, &rep->brtype_stat);
614 
615 	return 0;
616 }
617 
618 static void report__warn_kptr_restrict(const struct report *rep)
619 {
620 	struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
621 	struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
622 
623 	if (evlist__exclude_kernel(rep->session->evlist))
624 		return;
625 
626 	if (kernel_map == NULL ||
627 	    (dso__hit(map__dso(kernel_map)) &&
628 	     (kernel_kmap->ref_reloc_sym == NULL ||
629 	      kernel_kmap->ref_reloc_sym->addr == 0))) {
630 		const char *desc =
631 		    "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
632 		    "can't be resolved.";
633 
634 		if (kernel_map && map__has_symbols(kernel_map)) {
635 			desc = "If some relocation was applied (e.g. "
636 			       "kexec) symbols may be misresolved.";
637 		}
638 
639 		ui__warning(
640 "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
641 "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
642 "Samples in kernel modules can't be resolved as well.\n\n",
643 		desc);
644 	}
645 }
646 
647 static int report__gtk_browse_hists(struct report *rep, const char *help)
648 {
649 	int (*hist_browser)(struct evlist *evlist, const char *help,
650 			    struct hist_browser_timer *timer, float min_pcnt);
651 
652 	hist_browser = dlsym(perf_gtk_handle, "evlist__gtk_browse_hists");
653 
654 	if (hist_browser == NULL) {
655 		ui__error("GTK browser not found!\n");
656 		return -1;
657 	}
658 
659 	return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
660 }
661 
662 static int report__browse_hists(struct report *rep)
663 {
664 	int ret;
665 	struct perf_session *session = rep->session;
666 	struct evlist *evlist = session->evlist;
667 	char *help = NULL, *path = NULL;
668 
669 	path = system_path(TIPDIR);
670 	if (perf_tip(&help, path) || help == NULL) {
671 		/* fallback for people who don't install perf ;-) */
672 		free(path);
673 		path = system_path(DOCDIR);
674 		if (perf_tip(&help, path) || help == NULL)
675 			help = strdup("Cannot load tips.txt file, please install perf!");
676 	}
677 	free(path);
678 
679 	switch (use_browser) {
680 	case 1:
681 		if (rep->total_cycles_mode) {
682 			ret = evlist__tui_block_hists_browse(evlist, rep);
683 			break;
684 		}
685 
686 		ret = evlist__tui_browse_hists(evlist, help, NULL, rep->min_percent,
687 					       perf_session__env(session), true);
688 		/*
689 		 * Usually "ret" is the last pressed key, and we only
690 		 * care if the key notifies us to switch data file.
691 		 */
692 		if (ret != K_SWITCH_INPUT_DATA && ret != K_RELOAD)
693 			ret = 0;
694 		break;
695 	case 2:
696 		ret = report__gtk_browse_hists(rep, help);
697 		break;
698 	default:
699 		ret = evlist__tty_browse_hists(evlist, rep, help);
700 		break;
701 	}
702 	free(help);
703 	return ret;
704 }
705 
706 static int report__collapse_hists(struct report *rep)
707 {
708 	struct perf_session *session = rep->session;
709 	struct evlist *evlist = session->evlist;
710 	struct ui_progress prog;
711 	struct evsel *pos;
712 	int ret = 0;
713 
714 	/*
715 	 * The pipe data needs to setup hierarchy hpp formats now, because it
716 	 * cannot know about evsels in the data before reading the data.  The
717 	 * normal file data saves the event (attribute) info in the header
718 	 * section, but pipe does not have the luxury.
719 	 */
720 	if (perf_data__is_pipe(session->data)) {
721 		if (perf_hpp__setup_hists_formats(&perf_hpp_list, evlist) < 0) {
722 			ui__error("Failed to setup hierarchy output formats\n");
723 			return -1;
724 		}
725 	}
726 
727 	ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
728 
729 	evlist__for_each_entry(rep->session->evlist, pos) {
730 		struct hists *hists = evsel__hists(pos);
731 
732 		if (pos->core.idx == 0) {
733 			hists->symbol_filter_str =
734 				rep->symbol_filter_str ?
735 				strdup(rep->symbol_filter_str) : NULL;
736 		}
737 
738 		hists->socket_filter = rep->socket_filter;
739 
740 		ret = hists__collapse_resort(hists, &prog);
741 		if (ret < 0)
742 			break;
743 
744 		/* Non-group events are considered as leader */
745 		if (symbol_conf.event_group && !evsel__is_group_leader(pos)) {
746 			struct hists *leader_hists = evsel__hists(evsel__leader(pos));
747 
748 			hists__match(leader_hists, hists);
749 			hists__link(leader_hists, hists);
750 		}
751 	}
752 
753 	ui_progress__finish();
754 	return ret;
755 }
756 
757 static int hists__resort_cb(struct hist_entry *he, void *arg)
758 {
759 	struct report *rep = arg;
760 	struct symbol *sym = he->ms.sym;
761 
762 	if (rep->symbol_ipc && sym && !symbol__is_annotate2(sym)) {
763 		struct evsel *evsel = hists_to_evsel(he->hists);
764 
765 		symbol__annotate2(&he->ms, evsel, NULL);
766 	}
767 
768 	return 0;
769 }
770 
771 static void report__output_resort(struct report *rep)
772 {
773 	struct ui_progress prog;
774 	struct evsel *pos;
775 
776 	ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
777 
778 	evlist__for_each_entry(rep->session->evlist, pos) {
779 		evsel__output_resort_cb(pos, &prog, hists__resort_cb, rep);
780 	}
781 
782 	ui_progress__finish();
783 }
784 
785 static int count_sample_event(const struct perf_tool *tool __maybe_unused,
786 			      union perf_event *event __maybe_unused,
787 			      struct perf_sample *sample,
788 			      struct machine *machine __maybe_unused)
789 {
790 	struct hists *hists = evsel__hists(sample->evsel);
791 
792 	hists__inc_nr_events(hists);
793 	return 0;
794 }
795 
796 static int count_lost_samples_event(const struct perf_tool *tool,
797 				    union perf_event *event,
798 				    struct perf_sample *sample,
799 				    struct machine *machine __maybe_unused)
800 {
801 	struct report *rep = container_of(tool, struct report, tool);
802 	struct evsel *evsel = sample->evsel;
803 
804 	if (!evsel)
805 		evsel = evlist__id2evsel(rep->session->evlist, sample->id);
806 
807 	if (evsel) {
808 		struct hists *hists = evsel__hists(evsel);
809 		u32 count = event->lost_samples.lost;
810 
811 		if (event->header.misc & PERF_RECORD_MISC_LOST_SAMPLES_BPF)
812 			hists__inc_nr_dropped_samples(hists, count);
813 		else
814 			hists__inc_nr_lost_samples(hists, count);
815 	}
816 	return 0;
817 }
818 
819 static int process_attr(const struct perf_tool *tool __maybe_unused,
820 			union perf_event *event,
821 			struct evlist **pevlist);
822 
823 static void stats_setup(struct report *rep)
824 {
825 	perf_tool__init(&rep->tool, /*ordered_events=*/false);
826 	rep->tool.attr = process_attr;
827 	rep->tool.sample = count_sample_event;
828 	rep->tool.lost_samples = count_lost_samples_event;
829 	rep->tool.event_update = perf_event__process_event_update;
830 	rep->tool.no_warn = true;
831 }
832 
833 static int stats_print(struct report *rep)
834 {
835 	struct perf_session *session = rep->session;
836 
837 	perf_session__fprintf_nr_events(session, stdout);
838 	evlist__fprintf_nr_events(session->evlist, stdout);
839 	return 0;
840 }
841 
842 static void tasks_setup(struct report *rep)
843 {
844 	perf_tool__init(&rep->tool, /*ordered_events=*/true);
845 	if (rep->mmaps_mode) {
846 		rep->tool.mmap = perf_event__process_mmap;
847 		rep->tool.mmap2 = perf_event__process_mmap2;
848 	}
849 	rep->tool.attr = process_attr;
850 	rep->tool.comm = perf_event__process_comm;
851 	rep->tool.exit = perf_event__process_exit;
852 	rep->tool.fork = perf_event__process_fork;
853 	rep->tool.no_warn = true;
854 }
855 
856 struct maps__fprintf_task_args {
857 	int indent;
858 	FILE *fp;
859 	size_t printed;
860 };
861 
862 static int maps__fprintf_task_cb(struct map *map, void *data)
863 {
864 	struct maps__fprintf_task_args *args = data;
865 	const struct dso *dso = map__dso(map);
866 	u32 prot = map__prot(map);
867 	const struct dso_id *dso_id = dso__id_const(dso);
868 	int ret;
869 	char buf[SBUILD_ID_SIZE];
870 
871 	if (dso_id->mmap2_valid)
872 		snprintf(buf, sizeof(buf), "%" PRIu64, dso_id->ino);
873 	else
874 		build_id__snprintf(&dso_id->build_id, buf, sizeof(buf));
875 
876 	ret = fprintf(args->fp,
877 		"%*s  %" PRIx64 "-%" PRIx64 " %c%c%c%c %08" PRIx64 " %s %s\n",
878 		args->indent, "", map__start(map), map__end(map),
879 		prot & PROT_READ ? 'r' : '-',
880 		prot & PROT_WRITE ? 'w' : '-',
881 		prot & PROT_EXEC ? 'x' : '-',
882 		map__flags(map) ? 's' : 'p',
883 		map__pgoff(map),
884 		buf, dso__name(dso));
885 
886 	if (ret < 0)
887 		return ret;
888 
889 	args->printed += ret;
890 	return 0;
891 }
892 
893 static size_t maps__fprintf_task(struct maps *maps, int indent, FILE *fp)
894 {
895 	struct maps__fprintf_task_args args = {
896 		.indent = indent,
897 		.fp = fp,
898 		.printed = 0,
899 	};
900 
901 	maps__for_each_map(maps, maps__fprintf_task_cb, &args);
902 
903 	return args.printed;
904 }
905 
906 static int thread_level(struct machine *machine, const struct thread *thread)
907 {
908 	struct thread *parent_thread;
909 	int res;
910 
911 	if (thread__tid(thread) <= 0)
912 		return 0;
913 
914 	if (thread__ppid(thread) <= 0)
915 		return 1;
916 
917 	parent_thread = machine__find_thread(machine, -1, thread__ppid(thread));
918 	if (!parent_thread) {
919 		pr_err("Missing parent thread of %d\n", thread__tid(thread));
920 		return 0;
921 	}
922 	res = 1 + thread_level(machine, parent_thread);
923 	thread__put(parent_thread);
924 	return res;
925 }
926 
927 static void task__print_level(struct machine *machine, struct thread *thread, FILE *fp)
928 {
929 	int level = thread_level(machine, thread);
930 	int comm_indent = fprintf(fp, "  %8d %8d %8d |%*s",
931 				  thread__pid(thread), thread__tid(thread),
932 				  thread__ppid(thread), level, "");
933 
934 	fprintf(fp, "%s\n", thread__comm_str(thread));
935 
936 	maps__fprintf_task(thread__maps(thread), comm_indent, fp);
937 }
938 
939 /*
940  * Sort two thread list nodes such that they form a tree. The first node is the
941  * root of the tree, its children are ordered numerically after it. If a child
942  * has children itself then they appear immediately after their parent. For
943  * example, the 4 threads in the order they'd appear in the list:
944  * - init with a TID 1 and a parent of 0
945  * - systemd with a TID 3000 and a parent of init/1
946  * - systemd child thread with TID 4000, the parent is 3000
947  * - NetworkManager is a child of init with a TID of 3500.
948  */
949 static int task_list_cmp(void *priv, const struct list_head *la, const struct list_head *lb)
950 {
951 	struct machine *machine = priv;
952 	struct thread_list *task_a = list_entry(la, struct thread_list, list);
953 	struct thread_list *task_b = list_entry(lb, struct thread_list, list);
954 	struct thread *a = task_a->thread;
955 	struct thread *b = task_b->thread;
956 	int level_a, level_b, res;
957 
958 	/* Same thread? */
959 	if (thread__tid(a) == thread__tid(b))
960 		return 0;
961 
962 	/* Compare a and b to root. */
963 	if (thread__tid(a) == 0)
964 		return -1;
965 
966 	if (thread__tid(b) == 0)
967 		return 1;
968 
969 	/* If parents match sort by tid. */
970 	if (thread__ppid(a) == thread__ppid(b))
971 		return thread__tid(a) < thread__tid(b) ? -1 : 1;
972 
973 	/*
974 	 * Find a and b such that if they are a child of each other a and b's
975 	 * tid's match, otherwise a and b have a common parent and distinct
976 	 * tid's to sort by. First make the depths of the threads match.
977 	 */
978 	level_a = thread_level(machine, a);
979 	level_b = thread_level(machine, b);
980 	a = thread__get(a);
981 	b = thread__get(b);
982 	for (int i = level_a; i > level_b; i--) {
983 		struct thread *parent = machine__find_thread(machine, -1, thread__ppid(a));
984 
985 		thread__put(a);
986 		if (!parent) {
987 			pr_err("Missing parent thread of %d\n", thread__tid(a));
988 			thread__put(b);
989 			return -1;
990 		}
991 		a = parent;
992 	}
993 	for (int i = level_b; i > level_a; i--) {
994 		struct thread *parent = machine__find_thread(machine, -1, thread__ppid(b));
995 
996 		thread__put(b);
997 		if (!parent) {
998 			pr_err("Missing parent thread of %d\n", thread__tid(b));
999 			thread__put(a);
1000 			return 1;
1001 		}
1002 		b = parent;
1003 	}
1004 	/* Search up to a common parent. */
1005 	while (thread__ppid(a) != thread__ppid(b)) {
1006 		struct thread *parent;
1007 
1008 		parent = machine__find_thread(machine, -1, thread__ppid(a));
1009 		thread__put(a);
1010 		if (!parent)
1011 			pr_err("Missing parent thread of %d\n", thread__tid(a));
1012 		a = parent;
1013 		parent = machine__find_thread(machine, -1, thread__ppid(b));
1014 		thread__put(b);
1015 		if (!parent)
1016 			pr_err("Missing parent thread of %d\n", thread__tid(b));
1017 		b = parent;
1018 		if (!a || !b) {
1019 			/* Handle missing parent (unexpected) with some sanity. */
1020 			thread__put(a);
1021 			thread__put(b);
1022 			return !a && !b ? 0 : (!a ? -1 : 1);
1023 		}
1024 	}
1025 	if (thread__tid(a) == thread__tid(b)) {
1026 		/* a is a child of b or vice-versa, deeper levels appear later. */
1027 		res = level_a < level_b ? -1 : (level_a > level_b ? 1 : 0);
1028 	} else {
1029 		/* Sort by tid now the parent is the same. */
1030 		res = thread__tid(a) < thread__tid(b) ? -1 : 1;
1031 	}
1032 	thread__put(a);
1033 	thread__put(b);
1034 	return res;
1035 }
1036 
1037 static int tasks_print(struct report *rep, FILE *fp)
1038 {
1039 	struct machine *machine = &rep->session->machines.host;
1040 	LIST_HEAD(tasks);
1041 	int ret;
1042 
1043 	ret = machine__thread_list(machine, &tasks);
1044 	if (!ret) {
1045 		struct thread_list *task;
1046 
1047 		list_sort(machine, &tasks, task_list_cmp);
1048 
1049 		fprintf(fp, "# %8s %8s %8s  %s\n", "pid", "tid", "ppid", "comm");
1050 
1051 		list_for_each_entry(task, &tasks, list)
1052 			task__print_level(machine, task->thread, fp);
1053 	}
1054 	thread_list__delete(&tasks);
1055 	return ret;
1056 }
1057 
1058 static int __cmd_report(struct report *rep)
1059 {
1060 	int ret;
1061 	struct perf_session *session = rep->session;
1062 	struct evsel *pos;
1063 	struct perf_data *data = session->data;
1064 
1065 	signal(SIGINT, sig_handler);
1066 
1067 	if (rep->cpu_list) {
1068 		ret = perf_session__cpu_bitmap(session, rep->cpu_list,
1069 					       rep->cpu_bitmap);
1070 		if (ret) {
1071 			ui__error("failed to set cpu bitmap\n");
1072 			return ret;
1073 		}
1074 		session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
1075 	}
1076 
1077 	if (rep->show_threads) {
1078 		ret = perf_read_values_init(&rep->show_threads_values);
1079 		if (ret)
1080 			return ret;
1081 	}
1082 
1083 	ret = report__setup_sample_type(rep);
1084 	if (ret) {
1085 		/* report__setup_sample_type() already showed error message */
1086 		return ret;
1087 	}
1088 
1089 	if (rep->stats_mode)
1090 		stats_setup(rep);
1091 
1092 	if (rep->tasks_mode)
1093 		tasks_setup(rep);
1094 
1095 	ret = perf_session__process_events(session);
1096 	if (ret) {
1097 		ui__error("failed to process sample\n");
1098 		return ret;
1099 	}
1100 
1101 	/* Don't show Latency column for non-parallel profiles by default. */
1102 	if (!symbol_conf.prefer_latency && rep->total_samples &&
1103 		rep->singlethreaded_samples * 100 / rep->total_samples >= 99)
1104 		perf_hpp__cancel_latency(session->evlist);
1105 
1106 	evlist__check_mem_load_aux(session->evlist);
1107 
1108 	if (rep->stats_mode)
1109 		return stats_print(rep);
1110 
1111 	if (rep->tasks_mode)
1112 		return tasks_print(rep, stdout);
1113 
1114 	report__warn_kptr_restrict(rep);
1115 
1116 	evlist__for_each_entry(session->evlist, pos)
1117 		rep->nr_entries += evsel__hists(pos)->nr_entries;
1118 
1119 	if (symbol_conf.hybrid_merge) {
1120 		struct perf_env *env = perf_session__env(session);
1121 
1122 		if (evlist__can_merge_hybrid(session->evlist, env)) {
1123 			evlist__merge_hybrid(session->evlist, env);
1124 			evlist__merge_hists_hybrid(session->evlist, false);
1125 		} else if (symbol_conf.hybrid_merge_set) {
1126 			/*
1127 			 * Only an explicit --hybrid-merge warns. A
1128 			 * core.hybrid-merge default is silent, as most
1129 			 * machines aren't hybrid and there is nothing the
1130 			 * user needs to do about it.
1131 			 */
1132 			ui__warning("--hybrid-merge: no events to merge across core PMUs\n");
1133 		}
1134 	}
1135 
1136 	if (use_browser == 0) {
1137 		if (verbose > 3)
1138 			perf_session__fprintf(session, stdout);
1139 
1140 		if (verbose > 2)
1141 			perf_session__fprintf_dsos(session, stdout);
1142 
1143 		if (dump_trace) {
1144 			stats_print(rep);
1145 			return 0;
1146 		}
1147 	}
1148 
1149 	ret = report__collapse_hists(rep);
1150 	if (ret) {
1151 		ui__error("failed to process hist entry\n");
1152 		return ret;
1153 	}
1154 
1155 	if (session_done())
1156 		return 0;
1157 
1158 	/*
1159 	 * recalculate number of entries after collapsing since it
1160 	 * might be changed during the collapse phase.
1161 	 */
1162 	rep->nr_entries = 0;
1163 	evlist__for_each_entry(session->evlist, pos)
1164 		rep->nr_entries += evsel__hists(pos)->nr_entries;
1165 
1166 	if (rep->nr_entries == 0) {
1167 		ui__error("The %s data has no samples!\n", data->path);
1168 		return 0;
1169 	}
1170 
1171 	report__output_resort(rep);
1172 
1173 	if (rep->total_cycles_mode) {
1174 		int nr_hpps = 4;
1175 		int block_hpps[PERF_HPP_REPORT__BLOCK_MAX_INDEX] = {
1176 			PERF_HPP_REPORT__BLOCK_TOTAL_CYCLES_PCT,
1177 			PERF_HPP_REPORT__BLOCK_LBR_CYCLES,
1178 			PERF_HPP_REPORT__BLOCK_CYCLES_PCT,
1179 			PERF_HPP_REPORT__BLOCK_AVG_CYCLES,
1180 		};
1181 
1182 		if (evlist__nr_br_cntr(session->evlist) > 0)
1183 			block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_BRANCH_COUNTER;
1184 
1185 		block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_RANGE;
1186 		block_hpps[nr_hpps++] = PERF_HPP_REPORT__BLOCK_DSO;
1187 
1188 		rep->block_reports = block_info__create_report(session->evlist,
1189 							       rep->total_cycles,
1190 							       block_hpps, nr_hpps,
1191 							       &rep->nr_block_reports);
1192 		if (!rep->block_reports)
1193 			return -1;
1194 	}
1195 
1196 	return report__browse_hists(rep);
1197 }
1198 
1199 static int
1200 report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
1201 {
1202 	struct callchain_param *callchain = opt->value;
1203 
1204 	callchain->enabled = !unset;
1205 	/*
1206 	 * --no-call-graph
1207 	 */
1208 	if (unset) {
1209 		symbol_conf.use_callchain = false;
1210 		callchain->mode = CHAIN_NONE;
1211 		return 0;
1212 	}
1213 
1214 	return parse_callchain_report_opt(arg);
1215 }
1216 
1217 static int
1218 parse_time_quantum(const struct option *opt, const char *arg,
1219 		   int unset __maybe_unused)
1220 {
1221 	unsigned long *time_q = opt->value;
1222 	char *end;
1223 
1224 	*time_q = strtoul(arg, &end, 0);
1225 	if (end == arg)
1226 		goto parse_err;
1227 	if (*time_q == 0) {
1228 		pr_err("time quantum cannot be 0");
1229 		return -1;
1230 	}
1231 	end = skip_spaces(end);
1232 	if (*end == 0)
1233 		return 0;
1234 	if (!strcmp(end, "s")) {
1235 		*time_q *= NSEC_PER_SEC;
1236 		return 0;
1237 	}
1238 	if (!strcmp(end, "ms")) {
1239 		*time_q *= NSEC_PER_MSEC;
1240 		return 0;
1241 	}
1242 	if (!strcmp(end, "us")) {
1243 		*time_q *= NSEC_PER_USEC;
1244 		return 0;
1245 	}
1246 	if (!strcmp(end, "ns"))
1247 		return 0;
1248 parse_err:
1249 	pr_err("Cannot parse time quantum `%s'\n", arg);
1250 	return -1;
1251 }
1252 
1253 int
1254 report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
1255 				const char *arg, int unset __maybe_unused)
1256 {
1257 	if (arg) {
1258 		int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
1259 		if (err) {
1260 			char buf[BUFSIZ];
1261 			regerror(err, &ignore_callees_regex, buf, sizeof(buf));
1262 			pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
1263 			return -1;
1264 		}
1265 		have_ignore_callees = 1;
1266 	}
1267 
1268 	return 0;
1269 }
1270 
1271 static int
1272 parse_branch_mode(const struct option *opt,
1273 		  const char *str __maybe_unused, int unset)
1274 {
1275 	int *branch_mode = opt->value;
1276 
1277 	*branch_mode = !unset;
1278 	return 0;
1279 }
1280 
1281 static int
1282 parse_percent_limit(const struct option *opt, const char *str,
1283 		    int unset __maybe_unused)
1284 {
1285 	struct report *rep = opt->value;
1286 	double pcnt = strtof(str, NULL);
1287 
1288 	rep->min_percent = pcnt;
1289 	callchain_param.min_percent = pcnt;
1290 	return 0;
1291 }
1292 
1293 static int
1294 report_parse_addr2line_config(const struct option *opt __maybe_unused,
1295 			      const char *arg, int unset __maybe_unused)
1296 {
1297 	return addr2line_configure("addr2line.style", arg, NULL);
1298 }
1299 
1300 static int process_attr(const struct perf_tool *tool __maybe_unused,
1301 			union perf_event *event,
1302 			struct evlist **pevlist)
1303 {
1304 	struct perf_session *session;
1305 	u64 sample_type;
1306 	int err;
1307 
1308 	err = perf_event__process_attr(tool, event, pevlist);
1309 	if (err)
1310 		return err;
1311 
1312 	/*
1313 	 * Check if we need to enable callchains based
1314 	 * on events sample_type.
1315 	 */
1316 	sample_type = evlist__combined_sample_type(*pevlist);
1317 	session = evlist__session(*pevlist);
1318 	callchain_param_setup(sample_type, perf_session__e_machine(session, /*e_flags=*/NULL));
1319 	return 0;
1320 }
1321 
1322 #define CALLCHAIN_BRANCH_SORT_ORDER	\
1323 	"srcline,symbol,dso,callchain_branch_predicted," \
1324 	"callchain_branch_abort,callchain_branch_cycles"
1325 
1326 int cmd_report(int argc, const char **argv)
1327 {
1328 	struct perf_session *session;
1329 	struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
1330 	struct stat st;
1331 	bool has_br_stack = false;
1332 	int branch_mode = -1;
1333 	int last_key = 0;
1334 	bool branch_call_mode = false;
1335 #define CALLCHAIN_DEFAULT_OPT  "graph,0.5,caller,function,percent"
1336 	static const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
1337 						    CALLCHAIN_REPORT_HELP
1338 						    "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
1339 	char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
1340 	const char * const report_usage[] = {
1341 		"perf report [<options>]",
1342 		NULL
1343 	};
1344 	struct report report = {
1345 		.max_stack		 = PERF_MAX_STACK_DEPTH,
1346 		.pretty_printing_style	 = "normal",
1347 		.socket_filter		 = -1,
1348 		.skip_empty		 = true,
1349 	};
1350 	char *sort_order_help = sort_help("sort by key(s):", SORT_MODE__NORMAL);
1351 	char *field_order_help = sort_help("output field(s):", SORT_MODE__NORMAL);
1352 	const char *disassembler_style = NULL, *objdump_path = NULL, *addr2line_path = NULL;
1353 	const struct option options[] = {
1354 	OPT_STRING('i', "input", &input_name, "file",
1355 		    "input file name"),
1356 	OPT_INCR('v', "verbose", &verbose,
1357 		    "be more verbose (show symbol address, etc)"),
1358 	OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any warnings or messages"),
1359 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
1360 		    "dump raw trace in ASCII"),
1361 	OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
1362 	OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
1363 	OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
1364 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
1365 		   "file", "vmlinux pathname"),
1366 	OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
1367                     "don't load vmlinux even if found"),
1368 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
1369 		   "file", "kallsyms pathname"),
1370 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
1371 	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
1372 		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
1373 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
1374 		    "Show a column with the number of samples"),
1375 	OPT_BOOLEAN('T', "threads", &report.show_threads,
1376 		    "Show per-thread event counters"),
1377 	OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
1378 		   "pretty printing style key: normal raw"),
1379 #ifdef HAVE_SLANG_SUPPORT
1380 	OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
1381 #endif
1382 #ifdef HAVE_GTK4_SUPPORT
1383 	OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK4 interface"),
1384 #endif
1385 	OPT_BOOLEAN(0, "stdio", &report.use_stdio,
1386 		    "Use the stdio interface"),
1387 	OPT_BOOLEAN(0, "weights", &symbol_conf.annotate_weight,
1388 			"Show or hide weight columns in annotation. Default show if non-zero."),
1389 	OPT_BOOLEAN_SET(0, "progress", &report.progress, &report.progress_set,
1390 			"Show progress while processing the perf.data file"),
1391 	OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
1392 	OPT_BOOLEAN(0, "header-only", &report.header_only,
1393 		    "Show only data header."),
1394 	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
1395 		   sort_order_help),
1396 	OPT_STRING('F', "fields", &field_order, "key[,keys...]",
1397 		   field_order_help),
1398 	OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
1399 		    "Show sample percentage for different cpu modes"),
1400 	OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
1401 		    "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
1402 	OPT_STRING('p', "parent", &parent_pattern, "regex",
1403 		   "regex filter to identify parent, see: '--sort parent'"),
1404 	OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
1405 		    "Only display entries with parent-match"),
1406 	OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
1407 			     "print_type,threshold[,print_limit],order,sort_key[,branch],value",
1408 			     report_callchain_help, &report_parse_callchain_opt,
1409 			     callchain_default_opt),
1410 	OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
1411 		    "Accumulate callchains of children and show total overhead as well. "
1412 		    "Enabled by default, use --no-children to disable."),
1413 	OPT_INTEGER(0, "max-stack", &report.max_stack,
1414 		    "Set the maximum stack depth when parsing the callchain, "
1415 		    "anything beyond the specified depth will be ignored. "
1416 		    "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
1417 	OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
1418 		    "alias for inverted call graph"),
1419 	OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
1420 		   "ignore callees of these functions in call graphs",
1421 		   report_parse_ignore_callees_opt),
1422 	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
1423 		   "only consider symbols in these dsos"),
1424 	OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
1425 		   "only consider symbols in these comms"),
1426 	OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
1427 		   "only consider symbols in these pids"),
1428 	OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
1429 		   "only consider symbols in these tids"),
1430 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
1431 		   "only consider these symbols"),
1432 	OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
1433 		   "only show symbols that (partially) match with this filter"),
1434 	OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
1435 		   "width[,width...]",
1436 		   "don't try to adjust column width, use these fixed values"),
1437 	OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
1438 		   "separator for columns, no spaces will be added between "
1439 		   "columns '.' is reserved."),
1440 	OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
1441 		    "Only display entries resolved to a symbol"),
1442 	OPT_CALLBACK(0, "symfs", NULL, "directory[,layout]", SYMFS_HELP,
1443 		     symbol__config_symfs),
1444 	OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
1445 		   "list of cpus to profile"),
1446 	OPT_STRING(0, "parallelism", &symbol_conf.parallelism_list_str, "parallelism",
1447 		   "only consider these parallelism levels (cpu set format)"),
1448 	OPT_BOOLEAN('I', "show-info", &report.show_full_info,
1449 		    "Display extended information about perf.data file"),
1450 	OPT_BOOLEAN(0, "source", &annotate_opts.annotate_src,
1451 		    "Interleave source code with assembly code (default)"),
1452 	OPT_BOOLEAN(0, "asm-raw", &annotate_opts.show_asm_raw,
1453 		    "Display raw encoding of assembly instructions (default)"),
1454 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
1455 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
1456 	OPT_STRING(0, "prefix", &annotate_opts.prefix, "prefix",
1457 		    "Add prefix to source file path names in programs (with --prefix-strip)"),
1458 	OPT_STRING(0, "prefix-strip", &annotate_opts.prefix_strip, "N",
1459 		    "Strip first N entries of source file path name in programs (with --prefix)"),
1460 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
1461 		    "Show a column with the sum of periods"),
1462 	OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
1463 		    "Show event group information together"),
1464 	OPT_INTEGER(0, "group-sort-idx", &symbol_conf.group_sort_idx,
1465 		    "Sort the output by the event at the index n in group. "
1466 		    "If n is invalid, sort by the first event. "
1467 		    "WARNING: should be used on grouped events."),
1468 	OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
1469 		    "use branch records for per branch histogram filling",
1470 		    parse_branch_mode),
1471 	OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
1472 		    "add last branch records to call history"),
1473 	OPT_BOOLEAN_SET(0, "hybrid-merge", &symbol_conf.hybrid_merge,
1474 			&symbol_conf.hybrid_merge_set,
1475 			"merge the same event across hybrid core PMUs"),
1476 	OPT_STRING(0, "objdump", &objdump_path, "path",
1477 		   "objdump binary to use for disassembly and annotations"),
1478 	OPT_STRING(0, "addr2line", &addr2line_path, "path",
1479 		   "addr2line binary to use for line numbers"),
1480 	OPT_CALLBACK(0, "addr2line-style", NULL, "addr2line style",
1481 		     "addr2line styles (libdw,llvm,libbfd,addr2line)",
1482 		     report_parse_addr2line_config),
1483 	OPT_CALLBACK(0, "unwind-style", NULL, "unwind style",
1484 		     "unwind styles (libdw,libunwind)",
1485 		     unwind__option),
1486 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
1487 		    "Symbol demangling. Enabled by default, use --no-demangle to disable."),
1488 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
1489 		    "Enable kernel symbol demangling"),
1490 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
1491 	OPT_INTEGER(0, "samples", &symbol_conf.res_sample,
1492 		    "Number of samples to save per histogram entry for individual browsing"),
1493 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
1494 		     "Don't show entries under that percent", parse_percent_limit),
1495 	OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
1496 		     "how to display percentage of filtered entries", parse_filter_percentage),
1497 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
1498 			    "Instruction Tracing options\n" ITRACE_HELP,
1499 			    itrace_parse_synth_opts),
1500 	OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
1501 			"Show full source file name path for source lines"),
1502 	OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
1503 		    "Show callgraph from reference event"),
1504 	OPT_BOOLEAN(0, "stitch-lbr", &report.stitch_lbr,
1505 		    "Enable LBR callgraph stitching approach"),
1506 	OPT_INTEGER(0, "socket-filter", &report.socket_filter,
1507 		    "only show processor socket that match with this filter"),
1508 	OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
1509 		    "Show raw trace event output (do not use print fmt or plugins)"),
1510 	OPT_BOOLEAN('H', "hierarchy", &symbol_conf.report_hierarchy,
1511 		    "Show entries in a hierarchy"),
1512 	OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
1513 			     "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
1514 			     stdio__config_color, "always"),
1515 	OPT_STRING(0, "time", &report.time_str, "str",
1516 		   "Time span of interest (start,stop)"),
1517 	OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
1518 		    "Show inline function"),
1519 	OPT_CALLBACK(0, "percent-type", &annotate_opts, "local-period",
1520 		     "Set percent type local/global-period/hits",
1521 		     annotate_parse_percent_type),
1522 	OPT_BOOLEAN(0, "ns", &symbol_conf.nanosecs, "Show times in nanosecs"),
1523 	OPT_CALLBACK(0, "time-quantum", &symbol_conf.time_quantum, "time (ms|us|ns|s)",
1524 		     "Set time quantum for time sort key (default 100ms)",
1525 		     parse_time_quantum),
1526 	OPTS_EVSWITCH(&report.evswitch),
1527 	OPT_BOOLEAN(0, "total-cycles", &report.total_cycles_mode,
1528 		    "Sort all blocks by 'Sampled Cycles%'"),
1529 	OPT_BOOLEAN(0, "disable-order", &report.disable_order,
1530 		    "Disable raw trace ordering"),
1531 	OPT_BOOLEAN(0, "skip-empty", &report.skip_empty,
1532 		    "Do not display empty (or dummy) events in the output"),
1533 	OPT_BOOLEAN(0, "latency", &symbol_conf.prefer_latency,
1534 		    "Show latency-centric profile rather than the default\n"
1535 		    "\t\t\t  CPU-consumption-centric profile\n"
1536 		    "\t\t\t  (requires perf record --latency flag)."),
1537 	OPT_END()
1538 	};
1539 	struct perf_data data = {
1540 		.mode  = PERF_DATA_MODE_READ,
1541 	};
1542 	int ret = hists__init();
1543 	char sort_tmp[128];
1544 	bool ordered_events = true;
1545 
1546 	if (ret < 0)
1547 		goto exit;
1548 
1549 	/*
1550 	 * tasks_mode require access to exited threads to list those that are in
1551 	 * the data file. Off-cpu events are synthesized after other events and
1552 	 * reference exited threads.
1553 	 */
1554 	symbol_conf.keep_exited_threads = true;
1555 	symbol_conf.annotate_weight = true;
1556 
1557 	annotation_options__init();
1558 
1559 	ret = perf_config(report__config, &report);
1560 	if (ret)
1561 		goto exit;
1562 
1563 	argc = parse_options(argc, argv, options, report_usage, 0);
1564 	if (argc) {
1565 		/*
1566 		 * Special case: if there's an argument left then assume that
1567 		 * it's a symbol filter:
1568 		 */
1569 		if (argc > 1)
1570 			usage_with_options(report_usage, options);
1571 
1572 		report.symbol_filter_str = argv[0];
1573 	}
1574 
1575 	if (symbol_conf.report_hierarchy && symbol_conf.hybrid_merge) {
1576 		if (symbol_conf.hybrid_merge_set) {
1577 			pr_err("Error: --hierarchy and --hybrid-merge are mutually exclusive.\n");
1578 			ret = -EINVAL;
1579 			goto exit;
1580 		}
1581 		/* A config file default shouldn't fail an explicit option. */
1582 		pr_warning("core.hybrid-merge ignored: --hierarchy cannot display merged hybrid events\n");
1583 		symbol_conf.hybrid_merge = false;
1584 	}
1585 
1586 	if (disassembler_style) {
1587 		annotate_opts.disassembler_style = strdup(disassembler_style);
1588 		if (!annotate_opts.disassembler_style)
1589 			return -ENOMEM;
1590 	}
1591 	if (objdump_path) {
1592 		annotate_opts.objdump_path = strdup(objdump_path);
1593 		if (!annotate_opts.objdump_path)
1594 			return -ENOMEM;
1595 	}
1596 	if (addr2line_path) {
1597 		symbol_conf.addr2line_path = strdup(addr2line_path);
1598 		if (!symbol_conf.addr2line_path)
1599 			return -ENOMEM;
1600 	}
1601 
1602 	if (annotate_check_args() < 0) {
1603 		ret = -EINVAL;
1604 		goto exit;
1605 	}
1606 
1607 	if (report.mmaps_mode)
1608 		report.tasks_mode = true;
1609 
1610 	if (dump_trace && report.disable_order)
1611 		ordered_events = false;
1612 
1613 	if (quiet)
1614 		perf_quiet_option();
1615 
1616 	ret = symbol__validate_sym_arguments();
1617 	if (ret)
1618 		goto exit;
1619 
1620 	if (report.inverted_callchain)
1621 		callchain_param.order = ORDER_CALLER;
1622 	if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
1623 		callchain_param.order = ORDER_CALLER;
1624 
1625 	if ((itrace_synth_opts.callchain || itrace_synth_opts.add_callchain) &&
1626 	    (int)itrace_synth_opts.callchain_sz > report.max_stack)
1627 		report.max_stack = itrace_synth_opts.callchain_sz;
1628 
1629 	if (!input_name || !strlen(input_name)) {
1630 		if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
1631 			input_name = "-";
1632 		else
1633 			input_name = "perf.data";
1634 	}
1635 
1636 repeat:
1637 	data.path  = input_name;
1638 	data.force = symbol_conf.force;
1639 
1640 	symbol_conf.skip_empty = report.skip_empty;
1641 
1642 	perf_tool__init(&report.tool, ordered_events);
1643 	report.tool.sample		 = process_sample_event;
1644 	report.tool.mmap		 = perf_event__process_mmap;
1645 	report.tool.mmap2		 = perf_event__process_mmap2;
1646 	report.tool.comm		 = perf_event__process_comm;
1647 	report.tool.namespaces		 = perf_event__process_namespaces;
1648 	report.tool.cgroup		 = perf_event__process_cgroup;
1649 	report.tool.exit		 = perf_event__process_exit;
1650 	report.tool.fork		 = perf_event__process_fork;
1651 	report.tool.context_switch	 = perf_event__process_switch;
1652 	report.tool.lost		 = perf_event__process_lost;
1653 	report.tool.read		 = process_read_event;
1654 	report.tool.attr		 = process_attr;
1655 #ifdef HAVE_LIBTRACEEVENT
1656 	report.tool.tracing_data	 = perf_event__process_tracing_data;
1657 #endif
1658 	report.tool.build_id		 = perf_event__process_build_id;
1659 	report.tool.id_index		 = perf_event__process_id_index;
1660 	report.tool.auxtrace_info	 = perf_event__process_auxtrace_info;
1661 	report.tool.auxtrace		 = perf_event__process_auxtrace;
1662 	report.tool.event_update	 = perf_event__process_event_update;
1663 	report.tool.feature		 = process_feature_event;
1664 	report.tool.ordering_requires_timestamps = true;
1665 	report.tool.merge_deferred_callchains = !dump_trace;
1666 
1667 	session = perf_session__new(&data, &report.tool);
1668 	if (IS_ERR(session)) {
1669 		ret = PTR_ERR(session);
1670 		goto exit;
1671 	}
1672 
1673 	ret = evswitch__init(&report.evswitch, session->evlist, stderr);
1674 	if (ret)
1675 		goto exit;
1676 
1677 	if (zstd_init(&(session->zstd_data), 0) < 0)
1678 		pr_warning("Decompression initialization failed. Reported data may be incomplete.\n");
1679 
1680 	if (report.queue_size) {
1681 		ordered_events__set_alloc_size(&session->ordered_events,
1682 					       report.queue_size);
1683 	}
1684 
1685 	session->itrace_synth_opts = &itrace_synth_opts;
1686 
1687 	report.session = session;
1688 
1689 	has_br_stack = perf_header__has_feat(&session->header,
1690 					     HEADER_BRANCH_STACK);
1691 	if (evlist__combined_sample_type(session->evlist) & PERF_SAMPLE_STACK_USER)
1692 		has_br_stack = false;
1693 
1694 	setup_forced_leader(&report, session->evlist);
1695 
1696 	if (symbol_conf.group_sort_idx && evlist__nr_groups(session->evlist) == 0) {
1697 		parse_options_usage(NULL, options, "group-sort-idx", 0);
1698 		ret = -EINVAL;
1699 		goto error;
1700 	}
1701 
1702 	if (itrace_synth_opts.last_branch || itrace_synth_opts.add_last_branch)
1703 		has_br_stack = true;
1704 
1705 	if (has_br_stack && branch_call_mode)
1706 		symbol_conf.show_branchflag_count = true;
1707 
1708 	memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
1709 
1710 	/*
1711 	 * Branch mode is a tristate:
1712 	 * -1 means default, so decide based on the file having branch data.
1713 	 * 0/1 means the user chose a mode.
1714 	 */
1715 	if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
1716 	    !branch_call_mode) {
1717 		sort__mode = SORT_MODE__BRANCH;
1718 		symbol_conf.cumulate_callchain = false;
1719 	}
1720 	if (branch_call_mode) {
1721 		callchain_param.key = CCKEY_ADDRESS;
1722 		callchain_param.branch_callstack = true;
1723 		symbol_conf.use_callchain = true;
1724 		callchain_register_param(&callchain_param);
1725 		if (sort_order == NULL)
1726 			sort_order = CALLCHAIN_BRANCH_SORT_ORDER;
1727 	}
1728 
1729 	if (report.mem_mode) {
1730 		if (sort__mode == SORT_MODE__BRANCH) {
1731 			pr_err("branch and mem mode incompatible\n");
1732 			goto error;
1733 		}
1734 		sort__mode = SORT_MODE__MEMORY;
1735 		symbol_conf.cumulate_callchain = false;
1736 	}
1737 
1738 	if (symbol_conf.report_hierarchy) {
1739 		/*
1740 		 * The hist entries in hierarchy are added during the collpase
1741 		 * phase.  Let's enable it even if no sort keys require it.
1742 		 */
1743 		perf_hpp_list.need_collapse = true;
1744 	}
1745 
1746 	if (report.use_stdio)
1747 		use_browser = 0;
1748 #ifdef HAVE_SLANG_SUPPORT
1749 	else if (report.use_tui)
1750 		use_browser = 1;
1751 #endif
1752 #ifdef HAVE_GTK4_SUPPORT
1753 	else if (report.use_gtk)
1754 		use_browser = 2;
1755 #endif
1756 
1757 	/* Force tty output for header output and per-thread stat. */
1758 	if (report.header || report.header_only || report.show_threads)
1759 		use_browser = 0;
1760 	if (report.header || report.header_only)
1761 		report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1762 	if (report.show_full_info)
1763 		report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
1764 	if (report.stats_mode || report.tasks_mode)
1765 		use_browser = 0;
1766 	if (report.stats_mode && report.tasks_mode) {
1767 		pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
1768 		goto error;
1769 	}
1770 
1771 	if (report.total_cycles_mode) {
1772 		if (sort__mode != SORT_MODE__BRANCH)
1773 			report.total_cycles_mode = false;
1774 		else
1775 			sort_order = NULL;
1776 	}
1777 
1778 	if ((sort_order && strstr(sort_order, "type")) ||
1779 	    (field_order && strstr(field_order, "type"))) {
1780 		report.data_type = true;
1781 		annotate_opts.annotate_src = false;
1782 
1783 		/* disable incompatible options */
1784 		symbol_conf.cumulate_callchain = false;
1785 
1786 #ifndef HAVE_LIBDW_SUPPORT
1787 		pr_err("Error: Data type profiling is disabled due to missing DWARF support\n");
1788 		goto error;
1789 #endif
1790 	}
1791 
1792 	if (strcmp(input_name, "-") != 0)
1793 		setup_browser(true);
1794 	else
1795 		use_browser = 0;
1796 
1797 	if (report.progress_set && !report.progress)
1798 		ui_progress__noop_init();
1799 	else if (report.progress && !quiet && use_browser == 0)
1800 		stdio_progress__init();
1801 
1802 	if (report.data_type && use_browser == 1) {
1803 		symbol_conf.annotate_data_member = true;
1804 		symbol_conf.annotate_data_sample = true;
1805 	}
1806 
1807 	symbol_conf.enable_latency = true;
1808 	if (report.disable_order || !perf_session__has_switch_events(session)) {
1809 		if (symbol_conf.parallelism_list_str ||
1810 			symbol_conf.prefer_latency ||
1811 			(sort_order && (strstr(sort_order, "latency") ||
1812 				strstr(sort_order, "parallelism"))) ||
1813 			(field_order && (strstr(field_order, "latency") ||
1814 				strstr(field_order, "parallelism")))) {
1815 			if (report.disable_order)
1816 				ui__error("Use of latency profile or parallelism is incompatible with --disable-order.\n");
1817 			else
1818 				ui__error("Use of latency profile or parallelism requires --latency flag during record.\n");
1819 			return -1;
1820 		}
1821 		/*
1822 		 * If user did not ask for anything related to
1823 		 * latency/parallelism explicitly, just don't show it.
1824 		 */
1825 		symbol_conf.enable_latency = false;
1826 	}
1827 
1828 	if (last_key != K_SWITCH_INPUT_DATA) {
1829 		if (sort_order && strstr(sort_order, "ipc")) {
1830 			parse_options_usage(report_usage, options, "s", 1);
1831 			goto error;
1832 		}
1833 
1834 		if (sort_order && strstr(sort_order, "symbol")) {
1835 			if (sort__mode == SORT_MODE__BRANCH) {
1836 				snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1837 					 sort_order, "ipc_lbr");
1838 				report.symbol_ipc = true;
1839 			} else {
1840 				snprintf(sort_tmp, sizeof(sort_tmp), "%s,%s",
1841 					 sort_order, "ipc_null");
1842 			}
1843 
1844 			sort_order = sort_tmp;
1845 		}
1846 	}
1847 
1848 	if ((last_key != K_SWITCH_INPUT_DATA && last_key != K_RELOAD) &&
1849 	    (setup_sorting(session->evlist, perf_session__env(session)) < 0)) {
1850 		if (sort_order)
1851 			parse_options_usage(report_usage, options, "s", 1);
1852 		if (field_order)
1853 			parse_options_usage(sort_order ? NULL : report_usage,
1854 					    options, "F", 1);
1855 		goto error;
1856 	}
1857 
1858 	if ((report.header || report.header_only) && !quiet) {
1859 		perf_session__fprintf_info(session, stdout,
1860 					   report.show_full_info);
1861 		if (report.header_only) {
1862 			if (data.is_pipe) {
1863 				/*
1864 				 * we need to process first few records
1865 				 * which contains PERF_RECORD_HEADER_FEATURE.
1866 				 */
1867 				perf_session__process_events(session);
1868 			}
1869 			ret = 0;
1870 			goto error;
1871 		}
1872 	} else if (use_browser == 0 && !quiet &&
1873 		   !report.stats_mode && !report.tasks_mode) {
1874 		fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1875 		      stdout);
1876 	}
1877 
1878 	/*
1879 	 * Only in the TUI browser we are doing integrated annotation,
1880 	 * so don't allocate extra space that won't be used in the stdio
1881 	 * implementation.
1882 	 */
1883 	if (ui__has_annotation() || report.symbol_ipc || report.data_type ||
1884 	    report.total_cycles_mode) {
1885 		ret = symbol__annotation_init();
1886 		if (ret < 0)
1887 			goto error;
1888 		/*
1889  		 * For searching by name on the "Browse map details".
1890  		 * providing it only in verbose mode not to bloat too
1891  		 * much struct symbol.
1892  		 */
1893 		if (verbose > 0) {
1894 			/*
1895 			 * XXX: Need to provide a less kludgy way to ask for
1896 			 * more space per symbol, the u32 is for the index on
1897 			 * the ui browser.
1898 			 * See symbol__browser_index.
1899 			 */
1900 			symbol_conf.priv_size += sizeof(u32);
1901 		}
1902 		annotation_config__init();
1903 	}
1904 
1905 	if (symbol__init(perf_session__env(session)) < 0)
1906 		goto error;
1907 
1908 	if (report.time_str) {
1909 		ret = perf_time__parse_for_ranges(report.time_str, session,
1910 						  &report.ptime_range,
1911 						  &report.range_size,
1912 						  &report.range_num);
1913 		if (ret < 0)
1914 			goto error;
1915 
1916 		itrace_synth_opts__set_time_range(&itrace_synth_opts,
1917 						  report.ptime_range,
1918 						  report.range_num);
1919 	}
1920 
1921 #ifdef HAVE_LIBTRACEEVENT
1922 	if (session->tevent.pevent &&
1923 	    tep_set_function_resolver(session->tevent.pevent,
1924 				      machine__resolve_kernel_addr,
1925 				      &session->machines.host) < 0) {
1926 		pr_err("%s: failed to set libtraceevent function resolver\n",
1927 		       __func__);
1928 		return -1;
1929 	}
1930 #endif
1931 	sort__setup_elide(stdout);
1932 
1933 	ret = __cmd_report(&report);
1934 	if (ret == K_SWITCH_INPUT_DATA || ret == K_RELOAD) {
1935 		perf_session__delete(session);
1936 		last_key = K_SWITCH_INPUT_DATA;
1937 		/*
1938 		 * To support switching between data with and without callchains.
1939 		 * report__setup_sample_type() will update it properly.
1940 		 */
1941 		symbol_conf.use_callchain = false;
1942 		goto repeat;
1943 	} else
1944 		ret = 0;
1945 
1946 	if (!use_browser && (verbose > 2 || debug_kmaps))
1947 		perf_session__dump_kmaps(session);
1948 error:
1949 	if (report.ptime_range) {
1950 		itrace_synth_opts__clear_time_range(&itrace_synth_opts);
1951 		zfree(&report.ptime_range);
1952 	}
1953 
1954 	if (report.block_reports) {
1955 		block_info__free_report(report.block_reports,
1956 					report.nr_block_reports);
1957 		report.block_reports = NULL;
1958 	}
1959 
1960 	zstd_fini(&(session->zstd_data));
1961 	perf_session__delete(session);
1962 exit:
1963 	annotation_options__exit();
1964 	free(sort_order_help);
1965 	free(field_order_help);
1966 	return ret;
1967 }
1968