xref: /openbmc/linux/tools/perf/builtin-report.c (revision ca481398)
1 /*
2  * builtin-report.c
3  *
4  * Builtin report command: Analyze the perf.data input file,
5  * look up and read DSOs and symbol information and display
6  * a histogram of results, along various sorting keys.
7  */
8 #include "builtin.h"
9 
10 #include "util/util.h"
11 #include "util/config.h"
12 
13 #include "util/annotate.h"
14 #include "util/color.h"
15 #include <linux/list.h>
16 #include <linux/rbtree.h>
17 #include "util/symbol.h"
18 #include "util/callchain.h"
19 #include "util/values.h"
20 
21 #include "perf.h"
22 #include "util/debug.h"
23 #include "util/evlist.h"
24 #include "util/evsel.h"
25 #include "util/header.h"
26 #include "util/session.h"
27 #include "util/tool.h"
28 
29 #include <subcmd/parse-options.h>
30 #include <subcmd/exec-cmd.h>
31 #include "util/parse-events.h"
32 
33 #include "util/thread.h"
34 #include "util/sort.h"
35 #include "util/hist.h"
36 #include "util/data.h"
37 #include "arch/common.h"
38 #include "util/time-utils.h"
39 #include "util/auxtrace.h"
40 #include "util/units.h"
41 #include "util/branch.h"
42 
43 #include <dlfcn.h>
44 #include <errno.h>
45 #include <inttypes.h>
46 #include <regex.h>
47 #include <signal.h>
48 #include <linux/bitmap.h>
49 #include <linux/stringify.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <unistd.h>
53 
54 struct report {
55 	struct perf_tool	tool;
56 	struct perf_session	*session;
57 	bool			use_tui, use_gtk, use_stdio;
58 	bool			show_full_info;
59 	bool			show_threads;
60 	bool			inverted_callchain;
61 	bool			mem_mode;
62 	bool			header;
63 	bool			header_only;
64 	bool			nonany_branch_mode;
65 	int			max_stack;
66 	struct perf_read_values	show_threads_values;
67 	const char		*pretty_printing_style;
68 	const char		*cpu_list;
69 	const char		*symbol_filter_str;
70 	const char		*time_str;
71 	struct perf_time_interval ptime;
72 	float			min_percent;
73 	u64			nr_entries;
74 	u64			queue_size;
75 	int			socket_filter;
76 	DECLARE_BITMAP(cpu_bitmap, MAX_NR_CPUS);
77 	struct branch_type_stat	brtype_stat;
78 };
79 
80 static int report__config(const char *var, const char *value, void *cb)
81 {
82 	struct report *rep = cb;
83 
84 	if (!strcmp(var, "report.group")) {
85 		symbol_conf.event_group = perf_config_bool(var, value);
86 		return 0;
87 	}
88 	if (!strcmp(var, "report.percent-limit")) {
89 		double pcnt = strtof(value, NULL);
90 
91 		rep->min_percent = pcnt;
92 		callchain_param.min_percent = pcnt;
93 		return 0;
94 	}
95 	if (!strcmp(var, "report.children")) {
96 		symbol_conf.cumulate_callchain = perf_config_bool(var, value);
97 		return 0;
98 	}
99 	if (!strcmp(var, "report.queue-size"))
100 		return perf_config_u64(&rep->queue_size, var, value);
101 
102 	if (!strcmp(var, "report.sort_order")) {
103 		default_sort_order = strdup(value);
104 		return 0;
105 	}
106 
107 	return 0;
108 }
109 
110 static int hist_iter__report_callback(struct hist_entry_iter *iter,
111 				      struct addr_location *al, bool single,
112 				      void *arg)
113 {
114 	int err = 0;
115 	struct report *rep = arg;
116 	struct hist_entry *he = iter->he;
117 	struct perf_evsel *evsel = iter->evsel;
118 	struct perf_sample *sample = iter->sample;
119 	struct mem_info *mi;
120 	struct branch_info *bi;
121 
122 	if (!ui__has_annotation())
123 		return 0;
124 
125 	hist__account_cycles(sample->branch_stack, al, sample,
126 			     rep->nonany_branch_mode);
127 
128 	if (sort__mode == SORT_MODE__BRANCH) {
129 		bi = he->branch_info;
130 		err = addr_map_symbol__inc_samples(&bi->from, sample, evsel->idx);
131 		if (err)
132 			goto out;
133 
134 		err = addr_map_symbol__inc_samples(&bi->to, sample, evsel->idx);
135 
136 	} else if (rep->mem_mode) {
137 		mi = he->mem_info;
138 		err = addr_map_symbol__inc_samples(&mi->daddr, sample, evsel->idx);
139 		if (err)
140 			goto out;
141 
142 		err = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
143 
144 	} else if (symbol_conf.cumulate_callchain) {
145 		if (single)
146 			err = hist_entry__inc_addr_samples(he, sample, evsel->idx,
147 							   al->addr);
148 	} else {
149 		err = hist_entry__inc_addr_samples(he, sample, evsel->idx, al->addr);
150 	}
151 
152 out:
153 	return err;
154 }
155 
156 static int hist_iter__branch_callback(struct hist_entry_iter *iter,
157 				      struct addr_location *al __maybe_unused,
158 				      bool single __maybe_unused,
159 				      void *arg)
160 {
161 	struct hist_entry *he = iter->he;
162 	struct report *rep = arg;
163 	struct branch_info *bi;
164 
165 	bi = he->branch_info;
166 	branch_type_count(&rep->brtype_stat, &bi->flags,
167 			  bi->from.addr, bi->to.addr);
168 
169 	return 0;
170 }
171 
172 static int process_sample_event(struct perf_tool *tool,
173 				union perf_event *event,
174 				struct perf_sample *sample,
175 				struct perf_evsel *evsel,
176 				struct machine *machine)
177 {
178 	struct report *rep = container_of(tool, struct report, tool);
179 	struct addr_location al;
180 	struct hist_entry_iter iter = {
181 		.evsel 			= evsel,
182 		.sample 		= sample,
183 		.hide_unresolved 	= symbol_conf.hide_unresolved,
184 		.add_entry_cb 		= hist_iter__report_callback,
185 	};
186 	int ret = 0;
187 
188 	if (perf_time__skip_sample(&rep->ptime, sample->time))
189 		return 0;
190 
191 	if (machine__resolve(machine, &al, sample) < 0) {
192 		pr_debug("problem processing %d event, skipping it.\n",
193 			 event->header.type);
194 		return -1;
195 	}
196 
197 	if (symbol_conf.hide_unresolved && al.sym == NULL)
198 		goto out_put;
199 
200 	if (rep->cpu_list && !test_bit(sample->cpu, rep->cpu_bitmap))
201 		goto out_put;
202 
203 	if (sort__mode == SORT_MODE__BRANCH) {
204 		/*
205 		 * A non-synthesized event might not have a branch stack if
206 		 * branch stacks have been synthesized (using itrace options).
207 		 */
208 		if (!sample->branch_stack)
209 			goto out_put;
210 
211 		iter.add_entry_cb = hist_iter__branch_callback;
212 		iter.ops = &hist_iter_branch;
213 	} else if (rep->mem_mode) {
214 		iter.ops = &hist_iter_mem;
215 	} else if (symbol_conf.cumulate_callchain) {
216 		iter.ops = &hist_iter_cumulative;
217 	} else {
218 		iter.ops = &hist_iter_normal;
219 	}
220 
221 	if (al.map != NULL)
222 		al.map->dso->hit = 1;
223 
224 	ret = hist_entry_iter__add(&iter, &al, rep->max_stack, rep);
225 	if (ret < 0)
226 		pr_debug("problem adding hist entry, skipping event\n");
227 out_put:
228 	addr_location__put(&al);
229 	return ret;
230 }
231 
232 static int process_read_event(struct perf_tool *tool,
233 			      union perf_event *event,
234 			      struct perf_sample *sample __maybe_unused,
235 			      struct perf_evsel *evsel,
236 			      struct machine *machine __maybe_unused)
237 {
238 	struct report *rep = container_of(tool, struct report, tool);
239 
240 	if (rep->show_threads) {
241 		const char *name = evsel ? perf_evsel__name(evsel) : "unknown";
242 		int err = perf_read_values_add_value(&rep->show_threads_values,
243 					   event->read.pid, event->read.tid,
244 					   evsel->idx,
245 					   name,
246 					   event->read.value);
247 
248 		if (err)
249 			return err;
250 	}
251 
252 	return 0;
253 }
254 
255 /* For pipe mode, sample_type is not currently set */
256 static int report__setup_sample_type(struct report *rep)
257 {
258 	struct perf_session *session = rep->session;
259 	u64 sample_type = perf_evlist__combined_sample_type(session->evlist);
260 	bool is_pipe = perf_data_file__is_pipe(session->file);
261 
262 	if (session->itrace_synth_opts->callchain ||
263 	    (!is_pipe &&
264 	     perf_header__has_feat(&session->header, HEADER_AUXTRACE) &&
265 	     !session->itrace_synth_opts->set))
266 		sample_type |= PERF_SAMPLE_CALLCHAIN;
267 
268 	if (session->itrace_synth_opts->last_branch)
269 		sample_type |= PERF_SAMPLE_BRANCH_STACK;
270 
271 	if (!is_pipe && !(sample_type & PERF_SAMPLE_CALLCHAIN)) {
272 		if (perf_hpp_list.parent) {
273 			ui__error("Selected --sort parent, but no "
274 				    "callchain data. Did you call "
275 				    "'perf record' without -g?\n");
276 			return -EINVAL;
277 		}
278 		if (symbol_conf.use_callchain &&
279 			!symbol_conf.show_branchflag_count) {
280 			ui__error("Selected -g or --branch-history.\n"
281 				  "But no callchain or branch data.\n"
282 				  "Did you call 'perf record' without -g or -b?\n");
283 			return -1;
284 		}
285 	} else if (!callchain_param.enabled &&
286 		   callchain_param.mode != CHAIN_NONE &&
287 		   !symbol_conf.use_callchain) {
288 			symbol_conf.use_callchain = true;
289 			if (callchain_register_param(&callchain_param) < 0) {
290 				ui__error("Can't register callchain params.\n");
291 				return -EINVAL;
292 			}
293 	}
294 
295 	if (symbol_conf.cumulate_callchain) {
296 		/* Silently ignore if callchain is missing */
297 		if (!(sample_type & PERF_SAMPLE_CALLCHAIN)) {
298 			symbol_conf.cumulate_callchain = false;
299 			perf_hpp__cancel_cumulate();
300 		}
301 	}
302 
303 	if (sort__mode == SORT_MODE__BRANCH) {
304 		if (!is_pipe &&
305 		    !(sample_type & PERF_SAMPLE_BRANCH_STACK)) {
306 			ui__error("Selected -b but no branch data. "
307 				  "Did you call perf record without -b?\n");
308 			return -1;
309 		}
310 	}
311 
312 	if (symbol_conf.use_callchain || symbol_conf.cumulate_callchain) {
313 		if ((sample_type & PERF_SAMPLE_REGS_USER) &&
314 		    (sample_type & PERF_SAMPLE_STACK_USER))
315 			callchain_param.record_mode = CALLCHAIN_DWARF;
316 		else if (sample_type & PERF_SAMPLE_BRANCH_STACK)
317 			callchain_param.record_mode = CALLCHAIN_LBR;
318 		else
319 			callchain_param.record_mode = CALLCHAIN_FP;
320 	}
321 
322 	/* ??? handle more cases than just ANY? */
323 	if (!(perf_evlist__combined_branch_type(session->evlist) &
324 				PERF_SAMPLE_BRANCH_ANY))
325 		rep->nonany_branch_mode = true;
326 
327 	return 0;
328 }
329 
330 static void sig_handler(int sig __maybe_unused)
331 {
332 	session_done = 1;
333 }
334 
335 static size_t hists__fprintf_nr_sample_events(struct hists *hists, struct report *rep,
336 					      const char *evname, FILE *fp)
337 {
338 	size_t ret;
339 	char unit;
340 	unsigned long nr_samples = hists->stats.nr_events[PERF_RECORD_SAMPLE];
341 	u64 nr_events = hists->stats.total_period;
342 	struct perf_evsel *evsel = hists_to_evsel(hists);
343 	char buf[512];
344 	size_t size = sizeof(buf);
345 	int socked_id = hists->socket_filter;
346 
347 	if (quiet)
348 		return 0;
349 
350 	if (symbol_conf.filter_relative) {
351 		nr_samples = hists->stats.nr_non_filtered_samples;
352 		nr_events = hists->stats.total_non_filtered_period;
353 	}
354 
355 	if (perf_evsel__is_group_event(evsel)) {
356 		struct perf_evsel *pos;
357 
358 		perf_evsel__group_desc(evsel, buf, size);
359 		evname = buf;
360 
361 		for_each_group_member(pos, evsel) {
362 			const struct hists *pos_hists = evsel__hists(pos);
363 
364 			if (symbol_conf.filter_relative) {
365 				nr_samples += pos_hists->stats.nr_non_filtered_samples;
366 				nr_events += pos_hists->stats.total_non_filtered_period;
367 			} else {
368 				nr_samples += pos_hists->stats.nr_events[PERF_RECORD_SAMPLE];
369 				nr_events += pos_hists->stats.total_period;
370 			}
371 		}
372 	}
373 
374 	nr_samples = convert_unit(nr_samples, &unit);
375 	ret = fprintf(fp, "# Samples: %lu%c", nr_samples, unit);
376 	if (evname != NULL)
377 		ret += fprintf(fp, " of event '%s'", evname);
378 
379 	if (symbol_conf.show_ref_callgraph &&
380 	    strstr(evname, "call-graph=no")) {
381 		ret += fprintf(fp, ", show reference callgraph");
382 	}
383 
384 	if (rep->mem_mode) {
385 		ret += fprintf(fp, "\n# Total weight : %" PRIu64, nr_events);
386 		ret += fprintf(fp, "\n# Sort order   : %s", sort_order ? : default_mem_sort_order);
387 	} else
388 		ret += fprintf(fp, "\n# Event count (approx.): %" PRIu64, nr_events);
389 
390 	if (socked_id > -1)
391 		ret += fprintf(fp, "\n# Processor Socket: %d", socked_id);
392 
393 	return ret + fprintf(fp, "\n#\n");
394 }
395 
396 static int perf_evlist__tty_browse_hists(struct perf_evlist *evlist,
397 					 struct report *rep,
398 					 const char *help)
399 {
400 	struct perf_evsel *pos;
401 
402 	if (!quiet) {
403 		fprintf(stdout, "#\n# Total Lost Samples: %" PRIu64 "\n#\n",
404 			evlist->stats.total_lost_samples);
405 	}
406 
407 	evlist__for_each_entry(evlist, pos) {
408 		struct hists *hists = evsel__hists(pos);
409 		const char *evname = perf_evsel__name(pos);
410 
411 		if (symbol_conf.event_group &&
412 		    !perf_evsel__is_group_leader(pos))
413 			continue;
414 
415 		hists__fprintf_nr_sample_events(hists, rep, evname, stdout);
416 		hists__fprintf(hists, !quiet, 0, 0, rep->min_percent, stdout,
417 			       symbol_conf.use_callchain ||
418 			       symbol_conf.show_branchflag_count);
419 		fprintf(stdout, "\n\n");
420 	}
421 
422 	if (!quiet)
423 		fprintf(stdout, "#\n# (%s)\n#\n", help);
424 
425 	if (rep->show_threads) {
426 		bool style = !strcmp(rep->pretty_printing_style, "raw");
427 		perf_read_values_display(stdout, &rep->show_threads_values,
428 					 style);
429 		perf_read_values_destroy(&rep->show_threads_values);
430 	}
431 
432 	if (sort__mode == SORT_MODE__BRANCH)
433 		branch_type_stat_display(stdout, &rep->brtype_stat);
434 
435 	return 0;
436 }
437 
438 static void report__warn_kptr_restrict(const struct report *rep)
439 {
440 	struct map *kernel_map = machine__kernel_map(&rep->session->machines.host);
441 	struct kmap *kernel_kmap = kernel_map ? map__kmap(kernel_map) : NULL;
442 
443 	if (kernel_map == NULL ||
444 	    (kernel_map->dso->hit &&
445 	     (kernel_kmap->ref_reloc_sym == NULL ||
446 	      kernel_kmap->ref_reloc_sym->addr == 0))) {
447 		const char *desc =
448 		    "As no suitable kallsyms nor vmlinux was found, kernel samples\n"
449 		    "can't be resolved.";
450 
451 		if (kernel_map) {
452 			const struct dso *kdso = kernel_map->dso;
453 			if (!RB_EMPTY_ROOT(&kdso->symbols[MAP__FUNCTION])) {
454 				desc = "If some relocation was applied (e.g. "
455 				       "kexec) symbols may be misresolved.";
456 			}
457 		}
458 
459 		ui__warning(
460 "Kernel address maps (/proc/{kallsyms,modules}) were restricted.\n\n"
461 "Check /proc/sys/kernel/kptr_restrict before running 'perf record'.\n\n%s\n\n"
462 "Samples in kernel modules can't be resolved as well.\n\n",
463 		desc);
464 	}
465 }
466 
467 static int report__gtk_browse_hists(struct report *rep, const char *help)
468 {
469 	int (*hist_browser)(struct perf_evlist *evlist, const char *help,
470 			    struct hist_browser_timer *timer, float min_pcnt);
471 
472 	hist_browser = dlsym(perf_gtk_handle, "perf_evlist__gtk_browse_hists");
473 
474 	if (hist_browser == NULL) {
475 		ui__error("GTK browser not found!\n");
476 		return -1;
477 	}
478 
479 	return hist_browser(rep->session->evlist, help, NULL, rep->min_percent);
480 }
481 
482 static int report__browse_hists(struct report *rep)
483 {
484 	int ret;
485 	struct perf_session *session = rep->session;
486 	struct perf_evlist *evlist = session->evlist;
487 	const char *help = perf_tip(system_path(TIPDIR));
488 
489 	if (help == NULL) {
490 		/* fallback for people who don't install perf ;-) */
491 		help = perf_tip(DOCDIR);
492 		if (help == NULL)
493 			help = "Cannot load tips.txt file, please install perf!";
494 	}
495 
496 	switch (use_browser) {
497 	case 1:
498 		ret = perf_evlist__tui_browse_hists(evlist, help, NULL,
499 						    rep->min_percent,
500 						    &session->header.env);
501 		/*
502 		 * Usually "ret" is the last pressed key, and we only
503 		 * care if the key notifies us to switch data file.
504 		 */
505 		if (ret != K_SWITCH_INPUT_DATA)
506 			ret = 0;
507 		break;
508 	case 2:
509 		ret = report__gtk_browse_hists(rep, help);
510 		break;
511 	default:
512 		ret = perf_evlist__tty_browse_hists(evlist, rep, help);
513 		break;
514 	}
515 
516 	return ret;
517 }
518 
519 static int report__collapse_hists(struct report *rep)
520 {
521 	struct ui_progress prog;
522 	struct perf_evsel *pos;
523 	int ret = 0;
524 
525 	ui_progress__init(&prog, rep->nr_entries, "Merging related events...");
526 
527 	evlist__for_each_entry(rep->session->evlist, pos) {
528 		struct hists *hists = evsel__hists(pos);
529 
530 		if (pos->idx == 0)
531 			hists->symbol_filter_str = rep->symbol_filter_str;
532 
533 		hists->socket_filter = rep->socket_filter;
534 
535 		ret = hists__collapse_resort(hists, &prog);
536 		if (ret < 0)
537 			break;
538 
539 		/* Non-group events are considered as leader */
540 		if (symbol_conf.event_group &&
541 		    !perf_evsel__is_group_leader(pos)) {
542 			struct hists *leader_hists = evsel__hists(pos->leader);
543 
544 			hists__match(leader_hists, hists);
545 			hists__link(leader_hists, hists);
546 		}
547 	}
548 
549 	ui_progress__finish();
550 	return ret;
551 }
552 
553 static void report__output_resort(struct report *rep)
554 {
555 	struct ui_progress prog;
556 	struct perf_evsel *pos;
557 
558 	ui_progress__init(&prog, rep->nr_entries, "Sorting events for output...");
559 
560 	evlist__for_each_entry(rep->session->evlist, pos)
561 		perf_evsel__output_resort(pos, &prog);
562 
563 	ui_progress__finish();
564 }
565 
566 static int __cmd_report(struct report *rep)
567 {
568 	int ret;
569 	struct perf_session *session = rep->session;
570 	struct perf_evsel *pos;
571 	struct perf_data_file *file = session->file;
572 
573 	signal(SIGINT, sig_handler);
574 
575 	if (rep->cpu_list) {
576 		ret = perf_session__cpu_bitmap(session, rep->cpu_list,
577 					       rep->cpu_bitmap);
578 		if (ret) {
579 			ui__error("failed to set cpu bitmap\n");
580 			return ret;
581 		}
582 		session->itrace_synth_opts->cpu_bitmap = rep->cpu_bitmap;
583 	}
584 
585 	if (rep->show_threads) {
586 		ret = perf_read_values_init(&rep->show_threads_values);
587 		if (ret)
588 			return ret;
589 	}
590 
591 	ret = report__setup_sample_type(rep);
592 	if (ret) {
593 		/* report__setup_sample_type() already showed error message */
594 		return ret;
595 	}
596 
597 	ret = perf_session__process_events(session);
598 	if (ret) {
599 		ui__error("failed to process sample\n");
600 		return ret;
601 	}
602 
603 	report__warn_kptr_restrict(rep);
604 
605 	evlist__for_each_entry(session->evlist, pos)
606 		rep->nr_entries += evsel__hists(pos)->nr_entries;
607 
608 	if (use_browser == 0) {
609 		if (verbose > 3)
610 			perf_session__fprintf(session, stdout);
611 
612 		if (verbose > 2)
613 			perf_session__fprintf_dsos(session, stdout);
614 
615 		if (dump_trace) {
616 			perf_session__fprintf_nr_events(session, stdout);
617 			perf_evlist__fprintf_nr_events(session->evlist, stdout);
618 			return 0;
619 		}
620 	}
621 
622 	ret = report__collapse_hists(rep);
623 	if (ret) {
624 		ui__error("failed to process hist entry\n");
625 		return ret;
626 	}
627 
628 	if (session_done())
629 		return 0;
630 
631 	/*
632 	 * recalculate number of entries after collapsing since it
633 	 * might be changed during the collapse phase.
634 	 */
635 	rep->nr_entries = 0;
636 	evlist__for_each_entry(session->evlist, pos)
637 		rep->nr_entries += evsel__hists(pos)->nr_entries;
638 
639 	if (rep->nr_entries == 0) {
640 		ui__error("The %s file has no samples!\n", file->path);
641 		return 0;
642 	}
643 
644 	report__output_resort(rep);
645 
646 	return report__browse_hists(rep);
647 }
648 
649 static int
650 report_parse_callchain_opt(const struct option *opt, const char *arg, int unset)
651 {
652 	struct callchain_param *callchain = opt->value;
653 
654 	callchain->enabled = !unset;
655 	/*
656 	 * --no-call-graph
657 	 */
658 	if (unset) {
659 		symbol_conf.use_callchain = false;
660 		callchain->mode = CHAIN_NONE;
661 		return 0;
662 	}
663 
664 	return parse_callchain_report_opt(arg);
665 }
666 
667 int
668 report_parse_ignore_callees_opt(const struct option *opt __maybe_unused,
669 				const char *arg, int unset __maybe_unused)
670 {
671 	if (arg) {
672 		int err = regcomp(&ignore_callees_regex, arg, REG_EXTENDED);
673 		if (err) {
674 			char buf[BUFSIZ];
675 			regerror(err, &ignore_callees_regex, buf, sizeof(buf));
676 			pr_err("Invalid --ignore-callees regex: %s\n%s", arg, buf);
677 			return -1;
678 		}
679 		have_ignore_callees = 1;
680 	}
681 
682 	return 0;
683 }
684 
685 static int
686 parse_branch_mode(const struct option *opt,
687 		  const char *str __maybe_unused, int unset)
688 {
689 	int *branch_mode = opt->value;
690 
691 	*branch_mode = !unset;
692 	return 0;
693 }
694 
695 static int
696 parse_percent_limit(const struct option *opt, const char *str,
697 		    int unset __maybe_unused)
698 {
699 	struct report *rep = opt->value;
700 	double pcnt = strtof(str, NULL);
701 
702 	rep->min_percent = pcnt;
703 	callchain_param.min_percent = pcnt;
704 	return 0;
705 }
706 
707 #define CALLCHAIN_DEFAULT_OPT  "graph,0.5,caller,function,percent"
708 
709 const char report_callchain_help[] = "Display call graph (stack chain/backtrace):\n\n"
710 				     CALLCHAIN_REPORT_HELP
711 				     "\n\t\t\t\tDefault: " CALLCHAIN_DEFAULT_OPT;
712 
713 int cmd_report(int argc, const char **argv)
714 {
715 	struct perf_session *session;
716 	struct itrace_synth_opts itrace_synth_opts = { .set = 0, };
717 	struct stat st;
718 	bool has_br_stack = false;
719 	int branch_mode = -1;
720 	bool branch_call_mode = false;
721 	char callchain_default_opt[] = CALLCHAIN_DEFAULT_OPT;
722 	const char * const report_usage[] = {
723 		"perf report [<options>]",
724 		NULL
725 	};
726 	struct report report = {
727 		.tool = {
728 			.sample		 = process_sample_event,
729 			.mmap		 = perf_event__process_mmap,
730 			.mmap2		 = perf_event__process_mmap2,
731 			.comm		 = perf_event__process_comm,
732 			.namespaces	 = perf_event__process_namespaces,
733 			.exit		 = perf_event__process_exit,
734 			.fork		 = perf_event__process_fork,
735 			.lost		 = perf_event__process_lost,
736 			.read		 = process_read_event,
737 			.attr		 = perf_event__process_attr,
738 			.tracing_data	 = perf_event__process_tracing_data,
739 			.build_id	 = perf_event__process_build_id,
740 			.id_index	 = perf_event__process_id_index,
741 			.auxtrace_info	 = perf_event__process_auxtrace_info,
742 			.auxtrace	 = perf_event__process_auxtrace,
743 			.feature	 = perf_event__process_feature,
744 			.ordered_events	 = true,
745 			.ordering_requires_timestamps = true,
746 		},
747 		.max_stack		 = PERF_MAX_STACK_DEPTH,
748 		.pretty_printing_style	 = "normal",
749 		.socket_filter		 = -1,
750 	};
751 	const struct option options[] = {
752 	OPT_STRING('i', "input", &input_name, "file",
753 		    "input file name"),
754 	OPT_INCR('v', "verbose", &verbose,
755 		    "be more verbose (show symbol address, etc)"),
756 	OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
757 	OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
758 		    "dump raw trace in ASCII"),
759 	OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
760 		   "file", "vmlinux pathname"),
761 	OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
762 		   "file", "kallsyms pathname"),
763 	OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
764 	OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
765 		    "load module symbols - WARNING: use only with -k and LIVE kernel"),
766 	OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
767 		    "Show a column with the number of samples"),
768 	OPT_BOOLEAN('T', "threads", &report.show_threads,
769 		    "Show per-thread event counters"),
770 	OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
771 		   "pretty printing style key: normal raw"),
772 	OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
773 	OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
774 	OPT_BOOLEAN(0, "stdio", &report.use_stdio,
775 		    "Use the stdio interface"),
776 	OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
777 	OPT_BOOLEAN(0, "header-only", &report.header_only,
778 		    "Show only data header."),
779 	OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
780 		   "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
781 		   " Please refer the man page for the complete list."),
782 	OPT_STRING('F', "fields", &field_order, "key[,keys...]",
783 		   "output field(s): overhead, period, sample plus all of sort keys"),
784 	OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
785 		    "Show sample percentage for different cpu modes"),
786 	OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
787 		    "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
788 	OPT_STRING('p', "parent", &parent_pattern, "regex",
789 		   "regex filter to identify parent, see: '--sort parent'"),
790 	OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
791 		    "Only display entries with parent-match"),
792 	OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
793 			     "print_type,threshold[,print_limit],order,sort_key[,branch],value",
794 			     report_callchain_help, &report_parse_callchain_opt,
795 			     callchain_default_opt),
796 	OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
797 		    "Accumulate callchains of children and show total overhead as well"),
798 	OPT_INTEGER(0, "max-stack", &report.max_stack,
799 		    "Set the maximum stack depth when parsing the callchain, "
800 		    "anything beyond the specified depth will be ignored. "
801 		    "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
802 	OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
803 		    "alias for inverted call graph"),
804 	OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
805 		   "ignore callees of these functions in call graphs",
806 		   report_parse_ignore_callees_opt),
807 	OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
808 		   "only consider symbols in these dsos"),
809 	OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
810 		   "only consider symbols in these comms"),
811 	OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
812 		   "only consider symbols in these pids"),
813 	OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
814 		   "only consider symbols in these tids"),
815 	OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
816 		   "only consider these symbols"),
817 	OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
818 		   "only show symbols that (partially) match with this filter"),
819 	OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
820 		   "width[,width...]",
821 		   "don't try to adjust column width, use these fixed values"),
822 	OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
823 		   "separator for columns, no spaces will be added between "
824 		   "columns '.' is reserved."),
825 	OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
826 		    "Only display entries resolved to a symbol"),
827 	OPT_CALLBACK(0, "symfs", NULL, "directory",
828 		     "Look for files with symbols relative to this directory",
829 		     symbol__config_symfs),
830 	OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
831 		   "list of cpus to profile"),
832 	OPT_BOOLEAN('I', "show-info", &report.show_full_info,
833 		    "Display extended information about perf.data file"),
834 	OPT_BOOLEAN(0, "source", &symbol_conf.annotate_src,
835 		    "Interleave source code with assembly code (default)"),
836 	OPT_BOOLEAN(0, "asm-raw", &symbol_conf.annotate_asm_raw,
837 		    "Display raw encoding of assembly instructions (default)"),
838 	OPT_STRING('M', "disassembler-style", &disassembler_style, "disassembler style",
839 		   "Specify disassembler style (e.g. -M intel for intel syntax)"),
840 	OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
841 		    "Show a column with the sum of periods"),
842 	OPT_BOOLEAN(0, "group", &symbol_conf.event_group,
843 		    "Show event group information together"),
844 	OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
845 		    "use branch records for per branch histogram filling",
846 		    parse_branch_mode),
847 	OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
848 		    "add last branch records to call history"),
849 	OPT_STRING(0, "objdump", &objdump_path, "path",
850 		   "objdump binary to use for disassembly and annotations"),
851 	OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
852 		    "Disable symbol demangling"),
853 	OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
854 		    "Enable kernel symbol demangling"),
855 	OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
856 	OPT_CALLBACK(0, "percent-limit", &report, "percent",
857 		     "Don't show entries under that percent", parse_percent_limit),
858 	OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
859 		     "how to display percentage of filtered entries", parse_filter_percentage),
860 	OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
861 			    "Instruction Tracing options",
862 			    itrace_parse_synth_opts),
863 	OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
864 			"Show full source file name path for source lines"),
865 	OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
866 		    "Show callgraph from reference event"),
867 	OPT_INTEGER(0, "socket-filter", &report.socket_filter,
868 		    "only show processor socket that match with this filter"),
869 	OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
870 		    "Show raw trace event output (do not use print fmt or plugins)"),
871 	OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
872 		    "Show entries in a hierarchy"),
873 	OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
874 			     "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
875 			     stdio__config_color, "always"),
876 	OPT_STRING(0, "time", &report.time_str, "str",
877 		   "Time span of interest (start,stop)"),
878 	OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
879 		    "Show inline function"),
880 	OPT_END()
881 	};
882 	struct perf_data_file file = {
883 		.mode  = PERF_DATA_MODE_READ,
884 	};
885 	int ret = hists__init();
886 
887 	if (ret < 0)
888 		return ret;
889 
890 	ret = perf_config(report__config, &report);
891 	if (ret)
892 		return ret;
893 
894 	argc = parse_options(argc, argv, options, report_usage, 0);
895 	if (argc) {
896 		/*
897 		 * Special case: if there's an argument left then assume that
898 		 * it's a symbol filter:
899 		 */
900 		if (argc > 1)
901 			usage_with_options(report_usage, options);
902 
903 		report.symbol_filter_str = argv[0];
904 	}
905 
906 	if (quiet)
907 		perf_quiet_option();
908 
909 	if (symbol_conf.vmlinux_name &&
910 	    access(symbol_conf.vmlinux_name, R_OK)) {
911 		pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
912 		return -EINVAL;
913 	}
914 	if (symbol_conf.kallsyms_name &&
915 	    access(symbol_conf.kallsyms_name, R_OK)) {
916 		pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
917 		return -EINVAL;
918 	}
919 
920 	if (report.use_stdio)
921 		use_browser = 0;
922 	else if (report.use_tui)
923 		use_browser = 1;
924 	else if (report.use_gtk)
925 		use_browser = 2;
926 
927 	if (report.inverted_callchain)
928 		callchain_param.order = ORDER_CALLER;
929 	if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
930 		callchain_param.order = ORDER_CALLER;
931 
932 	if (itrace_synth_opts.callchain &&
933 	    (int)itrace_synth_opts.callchain_sz > report.max_stack)
934 		report.max_stack = itrace_synth_opts.callchain_sz;
935 
936 	if (!input_name || !strlen(input_name)) {
937 		if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
938 			input_name = "-";
939 		else
940 			input_name = "perf.data";
941 	}
942 
943 	file.path  = input_name;
944 	file.force = symbol_conf.force;
945 
946 repeat:
947 	session = perf_session__new(&file, false, &report.tool);
948 	if (session == NULL)
949 		return -1;
950 
951 	if (report.queue_size) {
952 		ordered_events__set_alloc_size(&session->ordered_events,
953 					       report.queue_size);
954 	}
955 
956 	session->itrace_synth_opts = &itrace_synth_opts;
957 
958 	report.session = session;
959 
960 	has_br_stack = perf_header__has_feat(&session->header,
961 					     HEADER_BRANCH_STACK);
962 
963 	if (itrace_synth_opts.last_branch)
964 		has_br_stack = true;
965 
966 	if (has_br_stack && branch_call_mode)
967 		symbol_conf.show_branchflag_count = true;
968 
969 	memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
970 
971 	/*
972 	 * Branch mode is a tristate:
973 	 * -1 means default, so decide based on the file having branch data.
974 	 * 0/1 means the user chose a mode.
975 	 */
976 	if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
977 	    !branch_call_mode) {
978 		sort__mode = SORT_MODE__BRANCH;
979 		symbol_conf.cumulate_callchain = false;
980 	}
981 	if (branch_call_mode) {
982 		callchain_param.key = CCKEY_ADDRESS;
983 		callchain_param.branch_callstack = 1;
984 		symbol_conf.use_callchain = true;
985 		callchain_register_param(&callchain_param);
986 		if (sort_order == NULL)
987 			sort_order = "srcline,symbol,dso";
988 	}
989 
990 	if (report.mem_mode) {
991 		if (sort__mode == SORT_MODE__BRANCH) {
992 			pr_err("branch and mem mode incompatible\n");
993 			goto error;
994 		}
995 		sort__mode = SORT_MODE__MEMORY;
996 		symbol_conf.cumulate_callchain = false;
997 	}
998 
999 	if (symbol_conf.report_hierarchy) {
1000 		/* disable incompatible options */
1001 		symbol_conf.cumulate_callchain = false;
1002 
1003 		if (field_order) {
1004 			pr_err("Error: --hierarchy and --fields options cannot be used together\n");
1005 			parse_options_usage(report_usage, options, "F", 1);
1006 			parse_options_usage(NULL, options, "hierarchy", 0);
1007 			goto error;
1008 		}
1009 
1010 		perf_hpp_list.need_collapse = true;
1011 	}
1012 
1013 	/* Force tty output for header output and per-thread stat. */
1014 	if (report.header || report.header_only || report.show_threads)
1015 		use_browser = 0;
1016 	if (report.header || report.header_only)
1017 		report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
1018 	if (report.show_full_info)
1019 		report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
1020 
1021 	if (strcmp(input_name, "-") != 0)
1022 		setup_browser(true);
1023 	else
1024 		use_browser = 0;
1025 
1026 	if (setup_sorting(session->evlist) < 0) {
1027 		if (sort_order)
1028 			parse_options_usage(report_usage, options, "s", 1);
1029 		if (field_order)
1030 			parse_options_usage(sort_order ? NULL : report_usage,
1031 					    options, "F", 1);
1032 		goto error;
1033 	}
1034 
1035 	if ((report.header || report.header_only) && !quiet) {
1036 		perf_session__fprintf_info(session, stdout,
1037 					   report.show_full_info);
1038 		if (report.header_only) {
1039 			ret = 0;
1040 			goto error;
1041 		}
1042 	} else if (use_browser == 0 && !quiet) {
1043 		fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
1044 		      stdout);
1045 	}
1046 
1047 	/*
1048 	 * Only in the TUI browser we are doing integrated annotation,
1049 	 * so don't allocate extra space that won't be used in the stdio
1050 	 * implementation.
1051 	 */
1052 	if (ui__has_annotation()) {
1053 		ret = symbol__annotation_init();
1054 		if (ret < 0)
1055 			goto error;
1056 		/*
1057  		 * For searching by name on the "Browse map details".
1058  		 * providing it only in verbose mode not to bloat too
1059  		 * much struct symbol.
1060  		 */
1061 		if (verbose > 0) {
1062 			/*
1063 			 * XXX: Need to provide a less kludgy way to ask for
1064 			 * more space per symbol, the u32 is for the index on
1065 			 * the ui browser.
1066 			 * See symbol__browser_index.
1067 			 */
1068 			symbol_conf.priv_size += sizeof(u32);
1069 			symbol_conf.sort_by_name = true;
1070 		}
1071 	}
1072 
1073 	if (symbol__init(&session->header.env) < 0)
1074 		goto error;
1075 
1076 	if (perf_time__parse_str(&report.ptime, report.time_str) != 0) {
1077 		pr_err("Invalid time string\n");
1078 		return -EINVAL;
1079 	}
1080 
1081 	sort__setup_elide(stdout);
1082 
1083 	ret = __cmd_report(&report);
1084 	if (ret == K_SWITCH_INPUT_DATA) {
1085 		perf_session__delete(session);
1086 		goto repeat;
1087 	} else
1088 		ret = 0;
1089 
1090 error:
1091 	perf_session__delete(session);
1092 	return ret;
1093 }
1094