builtin-report.c 40 KB

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