builtin-report.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  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_session *session,
  175. union perf_event *event)
  176. {
  177. struct report *rep = container_of(session->tool, struct report, tool);
  178. if (event->feat.feat_id < HEADER_LAST_FEATURE)
  179. return perf_event__process_feature(session, event);
  180. if (event->feat.feat_id != HEADER_LAST_FEATURE) {
  181. pr_err("failed: wrong feature ID: %" PRIu64 "\n",
  182. event->feat.feat_id);
  183. return -1;
  184. }
  185. /*
  186. * (feat_id = HEADER_LAST_FEATURE) is the end marker which
  187. * means all features are received, now 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. .event_update = perf_event__process_event_update,
  816. .feature = process_feature_event,
  817. .ordered_events = true,
  818. .ordering_requires_timestamps = true,
  819. },
  820. .max_stack = PERF_MAX_STACK_DEPTH,
  821. .pretty_printing_style = "normal",
  822. .socket_filter = -1,
  823. .annotation_opts = annotation__default_options,
  824. };
  825. const struct option options[] = {
  826. OPT_STRING('i', "input", &input_name, "file",
  827. "input file name"),
  828. OPT_INCR('v', "verbose", &verbose,
  829. "be more verbose (show symbol address, etc)"),
  830. OPT_BOOLEAN('q', "quiet", &quiet, "Do not show any message"),
  831. OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
  832. "dump raw trace in ASCII"),
  833. OPT_BOOLEAN(0, "stats", &report.stats_mode, "Display event stats"),
  834. OPT_BOOLEAN(0, "tasks", &report.tasks_mode, "Display recorded tasks"),
  835. OPT_BOOLEAN(0, "mmaps", &report.mmaps_mode, "Display recorded tasks memory maps"),
  836. OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
  837. "file", "vmlinux pathname"),
  838. OPT_BOOLEAN(0, "ignore-vmlinux", &symbol_conf.ignore_vmlinux,
  839. "don't load vmlinux even if found"),
  840. OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
  841. "file", "kallsyms pathname"),
  842. OPT_BOOLEAN('f', "force", &symbol_conf.force, "don't complain, do it"),
  843. OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
  844. "load module symbols - WARNING: use only with -k and LIVE kernel"),
  845. OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
  846. "Show a column with the number of samples"),
  847. OPT_BOOLEAN('T', "threads", &report.show_threads,
  848. "Show per-thread event counters"),
  849. OPT_STRING(0, "pretty", &report.pretty_printing_style, "key",
  850. "pretty printing style key: normal raw"),
  851. OPT_BOOLEAN(0, "tui", &report.use_tui, "Use the TUI interface"),
  852. OPT_BOOLEAN(0, "gtk", &report.use_gtk, "Use the GTK2 interface"),
  853. OPT_BOOLEAN(0, "stdio", &report.use_stdio,
  854. "Use the stdio interface"),
  855. OPT_BOOLEAN(0, "header", &report.header, "Show data header."),
  856. OPT_BOOLEAN(0, "header-only", &report.header_only,
  857. "Show only data header."),
  858. OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
  859. "sort by key(s): pid, comm, dso, symbol, parent, cpu, srcline, ..."
  860. " Please refer the man page for the complete list."),
  861. OPT_STRING('F', "fields", &field_order, "key[,keys...]",
  862. "output field(s): overhead, period, sample plus all of sort keys"),
  863. OPT_BOOLEAN(0, "show-cpu-utilization", &symbol_conf.show_cpu_utilization,
  864. "Show sample percentage for different cpu modes"),
  865. OPT_BOOLEAN_FLAG(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
  866. "Show sample percentage for different cpu modes", PARSE_OPT_HIDDEN),
  867. OPT_STRING('p', "parent", &parent_pattern, "regex",
  868. "regex filter to identify parent, see: '--sort parent'"),
  869. OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
  870. "Only display entries with parent-match"),
  871. OPT_CALLBACK_DEFAULT('g', "call-graph", &callchain_param,
  872. "print_type,threshold[,print_limit],order,sort_key[,branch],value",
  873. report_callchain_help, &report_parse_callchain_opt,
  874. callchain_default_opt),
  875. OPT_BOOLEAN(0, "children", &symbol_conf.cumulate_callchain,
  876. "Accumulate callchains of children and show total overhead as well"),
  877. OPT_INTEGER(0, "max-stack", &report.max_stack,
  878. "Set the maximum stack depth when parsing the callchain, "
  879. "anything beyond the specified depth will be ignored. "
  880. "Default: kernel.perf_event_max_stack or " __stringify(PERF_MAX_STACK_DEPTH)),
  881. OPT_BOOLEAN('G', "inverted", &report.inverted_callchain,
  882. "alias for inverted call graph"),
  883. OPT_CALLBACK(0, "ignore-callees", NULL, "regex",
  884. "ignore callees of these functions in call graphs",
  885. report_parse_ignore_callees_opt),
  886. OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
  887. "only consider symbols in these dsos"),
  888. OPT_STRING('c', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
  889. "only consider symbols in these comms"),
  890. OPT_STRING(0, "pid", &symbol_conf.pid_list_str, "pid[,pid...]",
  891. "only consider symbols in these pids"),
  892. OPT_STRING(0, "tid", &symbol_conf.tid_list_str, "tid[,tid...]",
  893. "only consider symbols in these tids"),
  894. OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
  895. "only consider these symbols"),
  896. OPT_STRING(0, "symbol-filter", &report.symbol_filter_str, "filter",
  897. "only show symbols that (partially) match with this filter"),
  898. OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
  899. "width[,width...]",
  900. "don't try to adjust column width, use these fixed values"),
  901. OPT_STRING_NOEMPTY('t', "field-separator", &symbol_conf.field_sep, "separator",
  902. "separator for columns, no spaces will be added between "
  903. "columns '.' is reserved."),
  904. OPT_BOOLEAN('U', "hide-unresolved", &symbol_conf.hide_unresolved,
  905. "Only display entries resolved to a symbol"),
  906. OPT_CALLBACK(0, "symfs", NULL, "directory",
  907. "Look for files with symbols relative to this directory",
  908. symbol__config_symfs),
  909. OPT_STRING('C', "cpu", &report.cpu_list, "cpu",
  910. "list of cpus to profile"),
  911. OPT_BOOLEAN('I', "show-info", &report.show_full_info,
  912. "Display extended information about perf.data file"),
  913. OPT_BOOLEAN(0, "source", &report.annotation_opts.annotate_src,
  914. "Interleave source code with assembly code (default)"),
  915. OPT_BOOLEAN(0, "asm-raw", &report.annotation_opts.show_asm_raw,
  916. "Display raw encoding of assembly instructions (default)"),
  917. OPT_STRING('M', "disassembler-style", &report.annotation_opts.disassembler_style, "disassembler style",
  918. "Specify disassembler style (e.g. -M intel for intel syntax)"),
  919. OPT_BOOLEAN(0, "show-total-period", &symbol_conf.show_total_period,
  920. "Show a column with the sum of periods"),
  921. OPT_BOOLEAN_SET(0, "group", &symbol_conf.event_group, &report.group_set,
  922. "Show event group information together"),
  923. OPT_CALLBACK_NOOPT('b', "branch-stack", &branch_mode, "",
  924. "use branch records for per branch histogram filling",
  925. parse_branch_mode),
  926. OPT_BOOLEAN(0, "branch-history", &branch_call_mode,
  927. "add last branch records to call history"),
  928. OPT_STRING(0, "objdump", &report.annotation_opts.objdump_path, "path",
  929. "objdump binary to use for disassembly and annotations"),
  930. OPT_BOOLEAN(0, "demangle", &symbol_conf.demangle,
  931. "Disable symbol demangling"),
  932. OPT_BOOLEAN(0, "demangle-kernel", &symbol_conf.demangle_kernel,
  933. "Enable kernel symbol demangling"),
  934. OPT_BOOLEAN(0, "mem-mode", &report.mem_mode, "mem access profile"),
  935. OPT_CALLBACK(0, "percent-limit", &report, "percent",
  936. "Don't show entries under that percent", parse_percent_limit),
  937. OPT_CALLBACK(0, "percentage", NULL, "relative|absolute",
  938. "how to display percentage of filtered entries", parse_filter_percentage),
  939. OPT_CALLBACK_OPTARG(0, "itrace", &itrace_synth_opts, NULL, "opts",
  940. "Instruction Tracing options\n" ITRACE_HELP,
  941. itrace_parse_synth_opts),
  942. OPT_BOOLEAN(0, "full-source-path", &srcline_full_filename,
  943. "Show full source file name path for source lines"),
  944. OPT_BOOLEAN(0, "show-ref-call-graph", &symbol_conf.show_ref_callgraph,
  945. "Show callgraph from reference event"),
  946. OPT_INTEGER(0, "socket-filter", &report.socket_filter,
  947. "only show processor socket that match with this filter"),
  948. OPT_BOOLEAN(0, "raw-trace", &symbol_conf.raw_trace,
  949. "Show raw trace event output (do not use print fmt or plugins)"),
  950. OPT_BOOLEAN(0, "hierarchy", &symbol_conf.report_hierarchy,
  951. "Show entries in a hierarchy"),
  952. OPT_CALLBACK_DEFAULT(0, "stdio-color", NULL, "mode",
  953. "'always' (default), 'never' or 'auto' only applicable to --stdio mode",
  954. stdio__config_color, "always"),
  955. OPT_STRING(0, "time", &report.time_str, "str",
  956. "Time span of interest (start,stop)"),
  957. OPT_BOOLEAN(0, "inline", &symbol_conf.inline_name,
  958. "Show inline function"),
  959. OPT_CALLBACK(0, "percent-type", &report.annotation_opts, "local-period",
  960. "Set percent type local/global-period/hits",
  961. annotate_parse_percent_type),
  962. OPT_END()
  963. };
  964. struct perf_data data = {
  965. .mode = PERF_DATA_MODE_READ,
  966. };
  967. int ret = hists__init();
  968. if (ret < 0)
  969. return ret;
  970. ret = perf_config(report__config, &report);
  971. if (ret)
  972. return ret;
  973. argc = parse_options(argc, argv, options, report_usage, 0);
  974. if (argc) {
  975. /*
  976. * Special case: if there's an argument left then assume that
  977. * it's a symbol filter:
  978. */
  979. if (argc > 1)
  980. usage_with_options(report_usage, options);
  981. report.symbol_filter_str = argv[0];
  982. }
  983. if (report.mmaps_mode)
  984. report.tasks_mode = true;
  985. if (quiet)
  986. perf_quiet_option();
  987. if (symbol_conf.vmlinux_name &&
  988. access(symbol_conf.vmlinux_name, R_OK)) {
  989. pr_err("Invalid file: %s\n", symbol_conf.vmlinux_name);
  990. return -EINVAL;
  991. }
  992. if (symbol_conf.kallsyms_name &&
  993. access(symbol_conf.kallsyms_name, R_OK)) {
  994. pr_err("Invalid file: %s\n", symbol_conf.kallsyms_name);
  995. return -EINVAL;
  996. }
  997. if (report.inverted_callchain)
  998. callchain_param.order = ORDER_CALLER;
  999. if (symbol_conf.cumulate_callchain && !callchain_param.order_set)
  1000. callchain_param.order = ORDER_CALLER;
  1001. if (itrace_synth_opts.callchain &&
  1002. (int)itrace_synth_opts.callchain_sz > report.max_stack)
  1003. report.max_stack = itrace_synth_opts.callchain_sz;
  1004. if (!input_name || !strlen(input_name)) {
  1005. if (!fstat(STDIN_FILENO, &st) && S_ISFIFO(st.st_mode))
  1006. input_name = "-";
  1007. else
  1008. input_name = "perf.data";
  1009. }
  1010. data.file.path = input_name;
  1011. data.force = symbol_conf.force;
  1012. repeat:
  1013. session = perf_session__new(&data, false, &report.tool);
  1014. if (session == NULL)
  1015. return -1;
  1016. if (report.queue_size) {
  1017. ordered_events__set_alloc_size(&session->ordered_events,
  1018. report.queue_size);
  1019. }
  1020. session->itrace_synth_opts = &itrace_synth_opts;
  1021. report.session = session;
  1022. has_br_stack = perf_header__has_feat(&session->header,
  1023. HEADER_BRANCH_STACK);
  1024. setup_forced_leader(&report, session->evlist);
  1025. if (itrace_synth_opts.last_branch)
  1026. has_br_stack = true;
  1027. if (has_br_stack && branch_call_mode)
  1028. symbol_conf.show_branchflag_count = true;
  1029. memset(&report.brtype_stat, 0, sizeof(struct branch_type_stat));
  1030. /*
  1031. * Branch mode is a tristate:
  1032. * -1 means default, so decide based on the file having branch data.
  1033. * 0/1 means the user chose a mode.
  1034. */
  1035. if (((branch_mode == -1 && has_br_stack) || branch_mode == 1) &&
  1036. !branch_call_mode) {
  1037. sort__mode = SORT_MODE__BRANCH;
  1038. symbol_conf.cumulate_callchain = false;
  1039. }
  1040. if (branch_call_mode) {
  1041. callchain_param.key = CCKEY_ADDRESS;
  1042. callchain_param.branch_callstack = 1;
  1043. symbol_conf.use_callchain = true;
  1044. callchain_register_param(&callchain_param);
  1045. if (sort_order == NULL)
  1046. sort_order = "srcline,symbol,dso";
  1047. }
  1048. if (report.mem_mode) {
  1049. if (sort__mode == SORT_MODE__BRANCH) {
  1050. pr_err("branch and mem mode incompatible\n");
  1051. goto error;
  1052. }
  1053. sort__mode = SORT_MODE__MEMORY;
  1054. symbol_conf.cumulate_callchain = false;
  1055. }
  1056. if (symbol_conf.report_hierarchy) {
  1057. /* disable incompatible options */
  1058. symbol_conf.cumulate_callchain = false;
  1059. if (field_order) {
  1060. pr_err("Error: --hierarchy and --fields options cannot be used together\n");
  1061. parse_options_usage(report_usage, options, "F", 1);
  1062. parse_options_usage(NULL, options, "hierarchy", 0);
  1063. goto error;
  1064. }
  1065. perf_hpp_list.need_collapse = true;
  1066. }
  1067. if (report.use_stdio)
  1068. use_browser = 0;
  1069. else if (report.use_tui)
  1070. use_browser = 1;
  1071. else if (report.use_gtk)
  1072. use_browser = 2;
  1073. /* Force tty output for header output and per-thread stat. */
  1074. if (report.header || report.header_only || report.show_threads)
  1075. use_browser = 0;
  1076. if (report.header || report.header_only)
  1077. report.tool.show_feat_hdr = SHOW_FEAT_HEADER;
  1078. if (report.show_full_info)
  1079. report.tool.show_feat_hdr = SHOW_FEAT_HEADER_FULL_INFO;
  1080. if (report.stats_mode || report.tasks_mode)
  1081. use_browser = 0;
  1082. if (report.stats_mode && report.tasks_mode) {
  1083. pr_err("Error: --tasks and --mmaps can't be used together with --stats\n");
  1084. goto error;
  1085. }
  1086. if (strcmp(input_name, "-") != 0)
  1087. setup_browser(true);
  1088. else
  1089. use_browser = 0;
  1090. if (setup_sorting(session->evlist) < 0) {
  1091. if (sort_order)
  1092. parse_options_usage(report_usage, options, "s", 1);
  1093. if (field_order)
  1094. parse_options_usage(sort_order ? NULL : report_usage,
  1095. options, "F", 1);
  1096. goto error;
  1097. }
  1098. if ((report.header || report.header_only) && !quiet) {
  1099. perf_session__fprintf_info(session, stdout,
  1100. report.show_full_info);
  1101. if (report.header_only) {
  1102. ret = 0;
  1103. goto error;
  1104. }
  1105. } else if (use_browser == 0 && !quiet &&
  1106. !report.stats_mode && !report.tasks_mode) {
  1107. fputs("# To display the perf.data header info, please use --header/--header-only options.\n#\n",
  1108. stdout);
  1109. }
  1110. /*
  1111. * Only in the TUI browser we are doing integrated annotation,
  1112. * so don't allocate extra space that won't be used in the stdio
  1113. * implementation.
  1114. */
  1115. if (ui__has_annotation()) {
  1116. ret = symbol__annotation_init();
  1117. if (ret < 0)
  1118. goto error;
  1119. /*
  1120. * For searching by name on the "Browse map details".
  1121. * providing it only in verbose mode not to bloat too
  1122. * much struct symbol.
  1123. */
  1124. if (verbose > 0) {
  1125. /*
  1126. * XXX: Need to provide a less kludgy way to ask for
  1127. * more space per symbol, the u32 is for the index on
  1128. * the ui browser.
  1129. * See symbol__browser_index.
  1130. */
  1131. symbol_conf.priv_size += sizeof(u32);
  1132. symbol_conf.sort_by_name = true;
  1133. }
  1134. annotation_config__init();
  1135. }
  1136. if (symbol__init(&session->header.env) < 0)
  1137. goto error;
  1138. report.ptime_range = perf_time__range_alloc(report.time_str,
  1139. &report.range_size);
  1140. if (!report.ptime_range) {
  1141. ret = -ENOMEM;
  1142. goto error;
  1143. }
  1144. if (perf_time__parse_str(report.ptime_range, report.time_str) != 0) {
  1145. if (session->evlist->first_sample_time == 0 &&
  1146. session->evlist->last_sample_time == 0) {
  1147. pr_err("HINT: no first/last sample time found in perf data.\n"
  1148. "Please use latest perf binary to execute 'perf record'\n"
  1149. "(if '--buildid-all' is enabled, please set '--timestamp-boundary').\n");
  1150. ret = -EINVAL;
  1151. goto error;
  1152. }
  1153. report.range_num = perf_time__percent_parse_str(
  1154. report.ptime_range, report.range_size,
  1155. report.time_str,
  1156. session->evlist->first_sample_time,
  1157. session->evlist->last_sample_time);
  1158. if (report.range_num < 0) {
  1159. pr_err("Invalid time string\n");
  1160. ret = -EINVAL;
  1161. goto error;
  1162. }
  1163. } else {
  1164. report.range_num = 1;
  1165. }
  1166. if (session->tevent.pevent &&
  1167. tep_set_function_resolver(session->tevent.pevent,
  1168. machine__resolve_kernel_addr,
  1169. &session->machines.host) < 0) {
  1170. pr_err("%s: failed to set libtraceevent function resolver\n",
  1171. __func__);
  1172. return -1;
  1173. }
  1174. sort__setup_elide(stdout);
  1175. ret = __cmd_report(&report);
  1176. if (ret == K_SWITCH_INPUT_DATA) {
  1177. perf_session__delete(session);
  1178. goto repeat;
  1179. } else
  1180. ret = 0;
  1181. error:
  1182. zfree(&report.ptime_range);
  1183. perf_session__delete(session);
  1184. return ret;
  1185. }