gprof.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Copyright (c) 1983, 1993, 1998, 2001, 2002
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "gprof.h"
  30. #include "libiberty.h"
  31. #include "bfdver.h"
  32. #include "search_list.h"
  33. #include "source.h"
  34. #include "symtab.h"
  35. #include "basic_blocks.h"
  36. #include "call_graph.h"
  37. #include "cg_arcs.h"
  38. #include "cg_print.h"
  39. #include "corefile.h"
  40. #include "gmon_io.h"
  41. #include "hertz.h"
  42. #include "hist.h"
  43. #include "sym_ids.h"
  44. #include "demangle.h"
  45. #include "getopt.h"
  46. static void usage (FILE *, int) ATTRIBUTE_NORETURN;
  47. const char * whoami;
  48. const char * function_mapping_file;
  49. static const char * external_symbol_table;
  50. const char * a_out_name = A_OUTNAME;
  51. long hz = HZ_WRONG;
  52. /*
  53. * Default options values:
  54. */
  55. int debug_level = 0;
  56. int output_style = 0;
  57. int output_width = 80;
  58. bool bsd_style_output = false;
  59. bool demangle = true;
  60. bool ignore_direct_calls = false;
  61. bool ignore_static_funcs = false;
  62. bool ignore_zeros = true;
  63. bool line_granularity = false;
  64. bool print_descriptions = true;
  65. bool print_path = false;
  66. bool ignore_non_functions = false;
  67. bool inline_file_names = false;
  68. File_Format file_format = FF_AUTO;
  69. bool first_output = true;
  70. char copyright[] =
  71. "@(#) Copyright (c) 1983 Regents of the University of California.\n\
  72. All rights reserved.\n";
  73. static char *gmon_name = GMONNAME; /* profile filename */
  74. /*
  75. * Functions that get excluded by default:
  76. */
  77. static char *default_excluded_list[] =
  78. {
  79. "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
  80. "__mcleanup",
  81. 0
  82. };
  83. /* Codes used for the long options with no short synonyms. 150 isn't
  84. special; it's just an arbitrary non-ASCII char value. */
  85. #define OPTION_DEMANGLE (150)
  86. #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
  87. #define OPTION_INLINE_FILE_NAMES (OPTION_DEMANGLE + 2)
  88. static struct option long_options[] =
  89. {
  90. {"line", no_argument, 0, 'l'},
  91. {"no-static", no_argument, 0, 'a'},
  92. {"ignore-non-functions", no_argument, 0, 'D'},
  93. {"external-symbol-table", required_argument, 0, 'S'},
  94. /* output styles: */
  95. {"annotated-source", optional_argument, 0, 'A'},
  96. {"no-annotated-source", optional_argument, 0, 'J'},
  97. {"flat-profile", optional_argument, 0, 'p'},
  98. {"no-flat-profile", optional_argument, 0, 'P'},
  99. {"graph", optional_argument, 0, 'q'},
  100. {"no-graph", optional_argument, 0, 'Q'},
  101. {"exec-counts", optional_argument, 0, 'C'},
  102. {"no-exec-counts", optional_argument, 0, 'Z'},
  103. {"function-ordering", no_argument, 0, 'r'},
  104. {"file-ordering", required_argument, 0, 'R'},
  105. {"file-info", no_argument, 0, 'i'},
  106. {"sum", no_argument, 0, 's'},
  107. /* various options to affect output: */
  108. {"all-lines", no_argument, 0, 'x'},
  109. {"demangle", optional_argument, 0, OPTION_DEMANGLE},
  110. {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
  111. {"directory-path", required_argument, 0, 'I'},
  112. {"display-unused-functions", no_argument, 0, 'z'},
  113. {"inline-file-names", no_argument, 0, OPTION_INLINE_FILE_NAMES},
  114. {"min-count", required_argument, 0, 'm'},
  115. {"print-path", no_argument, 0, 'L'},
  116. {"separate-files", no_argument, 0, 'y'},
  117. {"static-call-graph", no_argument, 0, 'c'},
  118. {"table-length", required_argument, 0, 't'},
  119. {"time", required_argument, 0, 'n'},
  120. {"no-time", required_argument, 0, 'N'},
  121. {"width", required_argument, 0, 'w'},
  122. /*
  123. * These are for backwards-compatibility only. Their functionality
  124. * is provided by the output style options already:
  125. */
  126. {"", required_argument, 0, 'e'},
  127. {"", required_argument, 0, 'E'},
  128. {"", required_argument, 0, 'f'},
  129. {"", required_argument, 0, 'F'},
  130. {"", required_argument, 0, 'k'},
  131. /* miscellaneous: */
  132. {"brief", no_argument, 0, 'b'},
  133. {"debug", optional_argument, 0, 'd'},
  134. {"help", no_argument, 0, 'h'},
  135. {"file-format", required_argument, 0, 'O'},
  136. {"traditional", no_argument, 0, 'T'},
  137. {"version", no_argument, 0, 'v'},
  138. {0, no_argument, 0, 0}
  139. };
  140. static void
  141. usage (FILE *stream, int status)
  142. {
  143. fprintf (stream, _("\
  144. Usage: %s [-[abcDhilLrsTvwxyz]] [-[ACeEfFJnNOpPqQRStZ][name]] [-I dirs]\n\
  145. [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
  146. [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
  147. [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
  148. [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
  149. [--function-ordering] [--file-ordering] [--inline-file-names]\n\
  150. [--directory-path=dirs] [--display-unused-functions]\n\
  151. [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
  152. [--no-static] [--print-path] [--separate-files]\n\
  153. [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
  154. [--version] [--width=n] [--ignore-non-functions]\n\
  155. [--demangle[=STYLE]] [--no-demangle] [--external-symbol-table=name] [@FILE]\n\
  156. [image-file] [profile-file...]\n"),
  157. whoami);
  158. if (REPORT_BUGS_TO[0] && status == 0)
  159. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  160. done (status);
  161. }
  162. int
  163. main (int argc, char **argv)
  164. {
  165. char **sp, *str;
  166. Sym **cg = 0;
  167. int ch, user_specified = 0;
  168. #ifdef HAVE_LC_MESSAGES
  169. setlocale (LC_MESSAGES, "");
  170. #endif
  171. setlocale (LC_CTYPE, "");
  172. #ifdef ENABLE_NLS
  173. bindtextdomain (PACKAGE, LOCALEDIR);
  174. textdomain (PACKAGE);
  175. #endif
  176. whoami = argv[0];
  177. xmalloc_set_program_name (whoami);
  178. expandargv (&argc, &argv);
  179. while ((ch = getopt_long (argc, argv,
  180. "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:sS:t:Tvw:xyzZ::",
  181. long_options, 0))
  182. != EOF)
  183. {
  184. switch (ch)
  185. {
  186. case 'a':
  187. ignore_static_funcs = true;
  188. break;
  189. case 'A':
  190. if (optarg)
  191. {
  192. sym_id_add (optarg, INCL_ANNO);
  193. }
  194. output_style |= STYLE_ANNOTATED_SOURCE;
  195. user_specified |= STYLE_ANNOTATED_SOURCE;
  196. break;
  197. case 'b':
  198. print_descriptions = false;
  199. break;
  200. case 'B':
  201. output_style |= STYLE_CALL_GRAPH;
  202. user_specified |= STYLE_CALL_GRAPH;
  203. break;
  204. case 'c':
  205. ignore_direct_calls = true;
  206. break;
  207. case 'C':
  208. if (optarg)
  209. {
  210. sym_id_add (optarg, INCL_EXEC);
  211. }
  212. output_style |= STYLE_EXEC_COUNTS;
  213. user_specified |= STYLE_EXEC_COUNTS;
  214. break;
  215. case 'd':
  216. if (optarg)
  217. {
  218. debug_level |= atoi (optarg);
  219. debug_level |= ANYDEBUG;
  220. }
  221. else
  222. {
  223. debug_level = ~0;
  224. }
  225. DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
  226. #ifndef DEBUG
  227. printf (_("%s: debugging not supported; -d ignored\n"), whoami);
  228. #endif /* DEBUG */
  229. break;
  230. case 'D':
  231. ignore_non_functions = true;
  232. break;
  233. case 'E':
  234. sym_id_add (optarg, EXCL_TIME);
  235. /* Fall through. */
  236. case 'e':
  237. sym_id_add (optarg, EXCL_GRAPH);
  238. break;
  239. case 'F':
  240. sym_id_add (optarg, INCL_TIME);
  241. /* Fall through. */
  242. case 'f':
  243. sym_id_add (optarg, INCL_GRAPH);
  244. break;
  245. /* FIXME: The -g and -G options are not present in the getopt_long
  246. invocation above, and they are not documented in gprof.texi.
  247. Therefore they appear to be deprecated. Test this theory and
  248. delete them if true. */
  249. case 'g':
  250. sym_id_add (optarg, EXCL_FLAT);
  251. break;
  252. case 'G':
  253. sym_id_add (optarg, INCL_FLAT);
  254. break;
  255. case 'h':
  256. usage (stdout, 0);
  257. case 'i':
  258. output_style |= STYLE_GMON_INFO;
  259. user_specified |= STYLE_GMON_INFO;
  260. break;
  261. case 'I':
  262. search_list_append (&src_search_list, optarg);
  263. break;
  264. case 'J':
  265. if (optarg)
  266. {
  267. sym_id_add (optarg, EXCL_ANNO);
  268. output_style |= STYLE_ANNOTATED_SOURCE;
  269. }
  270. else
  271. {
  272. output_style &= ~STYLE_ANNOTATED_SOURCE;
  273. }
  274. user_specified |= STYLE_ANNOTATED_SOURCE;
  275. break;
  276. case 'k':
  277. sym_id_add (optarg, EXCL_ARCS);
  278. break;
  279. case 'l':
  280. line_granularity = true;
  281. break;
  282. case 'L':
  283. print_path = true;
  284. break;
  285. case 'm':
  286. bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
  287. break;
  288. case 'n':
  289. sym_id_add (optarg, INCL_TIME);
  290. break;
  291. case 'N':
  292. sym_id_add (optarg, EXCL_TIME);
  293. break;
  294. case 'O':
  295. switch (optarg[0])
  296. {
  297. case 'a':
  298. file_format = FF_AUTO;
  299. break;
  300. case 'm':
  301. file_format = FF_MAGIC;
  302. break;
  303. case 'b':
  304. file_format = FF_BSD;
  305. break;
  306. case '4':
  307. file_format = FF_BSD44;
  308. break;
  309. case 'p':
  310. file_format = FF_PROF;
  311. break;
  312. default:
  313. fprintf (stderr, _("%s: unknown file format %s\n"),
  314. optarg, whoami);
  315. done (1);
  316. }
  317. break;
  318. case 'p':
  319. if (optarg)
  320. {
  321. sym_id_add (optarg, INCL_FLAT);
  322. }
  323. output_style |= STYLE_FLAT_PROFILE;
  324. user_specified |= STYLE_FLAT_PROFILE;
  325. break;
  326. case 'P':
  327. if (optarg)
  328. {
  329. sym_id_add (optarg, EXCL_FLAT);
  330. output_style |= STYLE_FLAT_PROFILE;
  331. }
  332. else
  333. {
  334. output_style &= ~STYLE_FLAT_PROFILE;
  335. }
  336. user_specified |= STYLE_FLAT_PROFILE;
  337. break;
  338. case 'q':
  339. if (optarg)
  340. {
  341. if (strchr (optarg, '/'))
  342. {
  343. sym_id_add (optarg, INCL_ARCS);
  344. }
  345. else
  346. {
  347. sym_id_add (optarg, INCL_GRAPH);
  348. }
  349. }
  350. output_style |= STYLE_CALL_GRAPH;
  351. user_specified |= STYLE_CALL_GRAPH;
  352. break;
  353. case 'r':
  354. output_style |= STYLE_FUNCTION_ORDER;
  355. user_specified |= STYLE_FUNCTION_ORDER;
  356. break;
  357. case 'R':
  358. output_style |= STYLE_FILE_ORDER;
  359. user_specified |= STYLE_FILE_ORDER;
  360. function_mapping_file = optarg;
  361. break;
  362. case 'Q':
  363. if (optarg)
  364. {
  365. if (strchr (optarg, '/'))
  366. {
  367. sym_id_add (optarg, EXCL_ARCS);
  368. }
  369. else
  370. {
  371. sym_id_add (optarg, EXCL_GRAPH);
  372. }
  373. output_style |= STYLE_CALL_GRAPH;
  374. }
  375. else
  376. {
  377. output_style &= ~STYLE_CALL_GRAPH;
  378. }
  379. user_specified |= STYLE_CALL_GRAPH;
  380. break;
  381. case 's':
  382. output_style |= STYLE_SUMMARY_FILE;
  383. user_specified |= STYLE_SUMMARY_FILE;
  384. break;
  385. case 'S':
  386. external_symbol_table = optarg;
  387. DBG (AOUTDEBUG, printf ("external-symbol-table: %s\n", optarg));
  388. break;
  389. case 't':
  390. bb_table_length = atoi (optarg);
  391. if (bb_table_length < 0)
  392. {
  393. bb_table_length = 0;
  394. }
  395. break;
  396. case 'T':
  397. bsd_style_output = true;
  398. break;
  399. case 'v':
  400. /* This output is intended to follow the GNU standards document. */
  401. printf (_("GNU gprof %s\n"), BFD_VERSION_STRING);
  402. printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
  403. printf (_("\
  404. This program is free software. This program has absolutely no warranty.\n"));
  405. done (0);
  406. case 'w':
  407. output_width = atoi (optarg);
  408. if (output_width < 1)
  409. {
  410. output_width = 1;
  411. }
  412. break;
  413. case 'x':
  414. bb_annotate_all_lines = true;
  415. break;
  416. case 'y':
  417. create_annotation_files = true;
  418. break;
  419. case 'z':
  420. ignore_zeros = false;
  421. break;
  422. case 'Z':
  423. if (optarg)
  424. {
  425. sym_id_add (optarg, EXCL_EXEC);
  426. output_style |= STYLE_EXEC_COUNTS;
  427. }
  428. else
  429. {
  430. output_style &= ~STYLE_EXEC_COUNTS;
  431. }
  432. user_specified |= STYLE_EXEC_COUNTS;
  433. break;
  434. case OPTION_DEMANGLE:
  435. demangle = true;
  436. if (optarg != NULL)
  437. {
  438. enum demangling_styles style;
  439. style = cplus_demangle_name_to_style (optarg);
  440. if (style == unknown_demangling)
  441. {
  442. fprintf (stderr,
  443. _("%s: unknown demangling style `%s'\n"),
  444. whoami, optarg);
  445. xexit (1);
  446. }
  447. cplus_demangle_set_style (style);
  448. }
  449. break;
  450. case OPTION_NO_DEMANGLE:
  451. demangle = false;
  452. break;
  453. case OPTION_INLINE_FILE_NAMES:
  454. inline_file_names = true;
  455. break;
  456. default:
  457. usage (stderr, 1);
  458. }
  459. }
  460. /* Don't allow both ordering options, they modify the arc data in-place. */
  461. if ((user_specified & STYLE_FUNCTION_ORDER)
  462. && (user_specified & STYLE_FILE_ORDER))
  463. {
  464. fprintf (stderr,_("\
  465. %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
  466. whoami);
  467. done (1);
  468. }
  469. /* --sum implies --line, otherwise we'd lose basic block counts in
  470. gmon.sum */
  471. if (output_style & STYLE_SUMMARY_FILE)
  472. line_granularity = 1;
  473. /* append value of GPROF_PATH to source search list if set: */
  474. str = (char *) getenv ("GPROF_PATH");
  475. if (str)
  476. search_list_append (&src_search_list, str);
  477. if (optind < argc)
  478. a_out_name = argv[optind++];
  479. if (optind < argc)
  480. gmon_name = argv[optind++];
  481. /* Turn off default functions. */
  482. for (sp = &default_excluded_list[0]; *sp; sp++)
  483. {
  484. sym_id_add (*sp, EXCL_TIME);
  485. sym_id_add (*sp, EXCL_GRAPH);
  486. sym_id_add (*sp, EXCL_FLAT);
  487. }
  488. /* Read symbol table from core file. */
  489. core_init (a_out_name);
  490. /* If we should ignore direct function calls, we need to load to
  491. core's text-space. */
  492. if (ignore_direct_calls)
  493. core_get_text_space (core_bfd);
  494. /* Create symbols from core image. */
  495. if (external_symbol_table)
  496. core_create_syms_from (external_symbol_table);
  497. else if (line_granularity)
  498. core_create_line_syms ();
  499. else
  500. core_create_function_syms ();
  501. /* Translate sym specs into syms. */
  502. sym_id_parse ();
  503. if (file_format == FF_PROF)
  504. {
  505. fprintf (stderr,
  506. _("%s: sorry, file format `prof' is not yet supported\n"),
  507. whoami);
  508. done (1);
  509. }
  510. else
  511. {
  512. /* Get information about gmon.out file(s). */
  513. do
  514. {
  515. gmon_out_read (gmon_name);
  516. if (optind < argc)
  517. gmon_name = argv[optind];
  518. }
  519. while (optind++ < argc);
  520. }
  521. /* If user did not specify output style, try to guess something
  522. reasonable. */
  523. if (output_style == 0)
  524. {
  525. if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
  526. {
  527. if (gmon_input & INPUT_HISTOGRAM)
  528. output_style |= STYLE_FLAT_PROFILE;
  529. if (gmon_input & INPUT_CALL_GRAPH)
  530. output_style |= STYLE_CALL_GRAPH;
  531. }
  532. else
  533. output_style = STYLE_EXEC_COUNTS;
  534. output_style &= ~user_specified;
  535. }
  536. /* Dump a gmon.sum file if requested (before any other
  537. processing!) */
  538. if (output_style & STYLE_SUMMARY_FILE)
  539. {
  540. gmon_out_write (GMONSUM);
  541. }
  542. if (gmon_input & INPUT_HISTOGRAM)
  543. {
  544. hist_assign_samples ();
  545. }
  546. if (gmon_input & INPUT_CALL_GRAPH)
  547. {
  548. cg = cg_assemble ();
  549. }
  550. /* Do some simple sanity checks. */
  551. if ((output_style & STYLE_FLAT_PROFILE)
  552. && !(gmon_input & INPUT_HISTOGRAM))
  553. {
  554. fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
  555. done (1);
  556. }
  557. if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
  558. {
  559. fprintf (stderr,
  560. _("%s: gmon.out file is missing call-graph data\n"), whoami);
  561. done (1);
  562. }
  563. /* Output whatever user whishes to see. */
  564. if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
  565. {
  566. /* Print the dynamic profile. */
  567. cg_print (cg);
  568. }
  569. if (output_style & STYLE_FLAT_PROFILE)
  570. {
  571. /* Print the flat profile. */
  572. hist_print ();
  573. }
  574. if (cg && (output_style & STYLE_CALL_GRAPH))
  575. {
  576. if (!bsd_style_output)
  577. {
  578. /* Print the dynamic profile. */
  579. cg_print (cg);
  580. }
  581. cg_print_index ();
  582. }
  583. if (output_style & STYLE_EXEC_COUNTS)
  584. print_exec_counts ();
  585. if (output_style & STYLE_ANNOTATED_SOURCE)
  586. print_annotated_source ();
  587. if (output_style & STYLE_FUNCTION_ORDER)
  588. cg_print_function_ordering ();
  589. if (output_style & STYLE_FILE_ORDER)
  590. cg_print_file_ordering ();
  591. return 0;
  592. }
  593. void
  594. done (int status)
  595. {
  596. exit (status);
  597. }