builtin-report.c 39 KB

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