gprof.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. bfd_boolean bsd_style_output = FALSE;
  59. bfd_boolean demangle = TRUE;
  60. bfd_boolean ignore_direct_calls = FALSE;
  61. bfd_boolean ignore_static_funcs = FALSE;
  62. bfd_boolean ignore_zeros = TRUE;
  63. bfd_boolean line_granularity = FALSE;
  64. bfd_boolean print_descriptions = TRUE;
  65. bfd_boolean print_path = FALSE;
  66. bfd_boolean ignore_non_functions = FALSE;
  67. bfd_boolean inline_file_names = FALSE;
  68. File_Format file_format = FF_AUTO;
  69. bfd_boolean 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 [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqSQZ][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. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  169. setlocale (LC_MESSAGES, "");
  170. #endif
  171. #if defined (HAVE_SETLOCALE)
  172. setlocale (LC_CTYPE, "");
  173. #endif
  174. #ifdef ENABLE_NLS
  175. bindtextdomain (PACKAGE, LOCALEDIR);
  176. textdomain (PACKAGE);
  177. #endif
  178. whoami = argv[0];
  179. xmalloc_set_program_name (whoami);
  180. expandargv (&argc, &argv);
  181. while ((ch = getopt_long (argc, argv,
  182. "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:sS:t:Tvw:xyzZ::",
  183. long_options, 0))
  184. != EOF)
  185. {
  186. switch (ch)
  187. {
  188. case 'a':
  189. ignore_static_funcs = TRUE;
  190. break;
  191. case 'A':
  192. if (optarg)
  193. {
  194. sym_id_add (optarg, INCL_ANNO);
  195. }
  196. output_style |= STYLE_ANNOTATED_SOURCE;
  197. user_specified |= STYLE_ANNOTATED_SOURCE;
  198. break;
  199. case 'b':
  200. print_descriptions = FALSE;
  201. break;
  202. case 'B':
  203. output_style |= STYLE_CALL_GRAPH;
  204. user_specified |= STYLE_CALL_GRAPH;
  205. break;
  206. case 'c':
  207. ignore_direct_calls = TRUE;
  208. break;
  209. case 'C':
  210. if (optarg)
  211. {
  212. sym_id_add (optarg, INCL_EXEC);
  213. }
  214. output_style |= STYLE_EXEC_COUNTS;
  215. user_specified |= STYLE_EXEC_COUNTS;
  216. break;
  217. case 'd':
  218. if (optarg)
  219. {
  220. debug_level |= atoi (optarg);
  221. debug_level |= ANYDEBUG;
  222. }
  223. else
  224. {
  225. debug_level = ~0;
  226. }
  227. DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
  228. #ifndef DEBUG
  229. printf (_("%s: debugging not supported; -d ignored\n"), whoami);
  230. #endif /* DEBUG */
  231. break;
  232. case 'D':
  233. ignore_non_functions = TRUE;
  234. break;
  235. case 'E':
  236. sym_id_add (optarg, EXCL_TIME);
  237. case 'e':
  238. sym_id_add (optarg, EXCL_GRAPH);
  239. break;
  240. case 'F':
  241. sym_id_add (optarg, INCL_TIME);
  242. case 'f':
  243. sym_id_add (optarg, INCL_GRAPH);
  244. break;
  245. case 'g':
  246. sym_id_add (optarg, EXCL_FLAT);
  247. break;
  248. case 'G':
  249. sym_id_add (optarg, INCL_FLAT);
  250. break;
  251. case 'h':
  252. usage (stdout, 0);
  253. case 'i':
  254. output_style |= STYLE_GMON_INFO;
  255. user_specified |= STYLE_GMON_INFO;
  256. break;
  257. case 'I':
  258. search_list_append (&src_search_list, optarg);
  259. break;
  260. case 'J':
  261. if (optarg)
  262. {
  263. sym_id_add (optarg, EXCL_ANNO);
  264. output_style |= STYLE_ANNOTATED_SOURCE;
  265. }
  266. else
  267. {
  268. output_style &= ~STYLE_ANNOTATED_SOURCE;
  269. }
  270. user_specified |= STYLE_ANNOTATED_SOURCE;
  271. break;
  272. case 'k':
  273. sym_id_add (optarg, EXCL_ARCS);
  274. break;
  275. case 'l':
  276. line_granularity = TRUE;
  277. break;
  278. case 'L':
  279. print_path = TRUE;
  280. break;
  281. case 'm':
  282. bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
  283. break;
  284. case 'n':
  285. sym_id_add (optarg, INCL_TIME);
  286. break;
  287. case 'N':
  288. sym_id_add (optarg, EXCL_TIME);
  289. break;
  290. case 'O':
  291. switch (optarg[0])
  292. {
  293. case 'a':
  294. file_format = FF_AUTO;
  295. break;
  296. case 'm':
  297. file_format = FF_MAGIC;
  298. break;
  299. case 'b':
  300. file_format = FF_BSD;
  301. break;
  302. case '4':
  303. file_format = FF_BSD44;
  304. break;
  305. case 'p':
  306. file_format = FF_PROF;
  307. break;
  308. default:
  309. fprintf (stderr, _("%s: unknown file format %s\n"),
  310. optarg, whoami);
  311. done (1);
  312. }
  313. break;
  314. case 'p':
  315. if (optarg)
  316. {
  317. sym_id_add (optarg, INCL_FLAT);
  318. }
  319. output_style |= STYLE_FLAT_PROFILE;
  320. user_specified |= STYLE_FLAT_PROFILE;
  321. break;
  322. case 'P':
  323. if (optarg)
  324. {
  325. sym_id_add (optarg, EXCL_FLAT);
  326. output_style |= STYLE_FLAT_PROFILE;
  327. }
  328. else
  329. {
  330. output_style &= ~STYLE_FLAT_PROFILE;
  331. }
  332. user_specified |= STYLE_FLAT_PROFILE;
  333. break;
  334. case 'q':
  335. if (optarg)
  336. {
  337. if (strchr (optarg, '/'))
  338. {
  339. sym_id_add (optarg, INCL_ARCS);
  340. }
  341. else
  342. {
  343. sym_id_add (optarg, INCL_GRAPH);
  344. }
  345. }
  346. output_style |= STYLE_CALL_GRAPH;
  347. user_specified |= STYLE_CALL_GRAPH;
  348. break;
  349. case 'r':
  350. output_style |= STYLE_FUNCTION_ORDER;
  351. user_specified |= STYLE_FUNCTION_ORDER;
  352. break;
  353. case 'R':
  354. output_style |= STYLE_FILE_ORDER;
  355. user_specified |= STYLE_FILE_ORDER;
  356. function_mapping_file = optarg;
  357. break;
  358. case 'Q':
  359. if (optarg)
  360. {
  361. if (strchr (optarg, '/'))
  362. {
  363. sym_id_add (optarg, EXCL_ARCS);
  364. }
  365. else
  366. {
  367. sym_id_add (optarg, EXCL_GRAPH);
  368. }
  369. output_style |= STYLE_CALL_GRAPH;
  370. }
  371. else
  372. {
  373. output_style &= ~STYLE_CALL_GRAPH;
  374. }
  375. user_specified |= STYLE_CALL_GRAPH;
  376. break;
  377. case 's':
  378. output_style |= STYLE_SUMMARY_FILE;
  379. user_specified |= STYLE_SUMMARY_FILE;
  380. break;
  381. case 'S':
  382. external_symbol_table = optarg;
  383. DBG (AOUTDEBUG, printf ("external-symbol-table: %s\n", optarg));
  384. break;
  385. case 't':
  386. bb_table_length = atoi (optarg);
  387. if (bb_table_length < 0)
  388. {
  389. bb_table_length = 0;
  390. }
  391. break;
  392. case 'T':
  393. bsd_style_output = TRUE;
  394. break;
  395. case 'v':
  396. /* This output is intended to follow the GNU standards document. */
  397. printf (_("GNU gprof %s\n"), BFD_VERSION_STRING);
  398. printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
  399. printf (_("\
  400. This program is free software. This program has absolutely no warranty.\n"));
  401. done (0);
  402. case 'w':
  403. output_width = atoi (optarg);
  404. if (output_width < 1)
  405. {
  406. output_width = 1;
  407. }
  408. break;
  409. case 'x':
  410. bb_annotate_all_lines = TRUE;
  411. break;
  412. case 'y':
  413. create_annotation_files = TRUE;
  414. break;
  415. case 'z':
  416. ignore_zeros = FALSE;
  417. break;
  418. case 'Z':
  419. if (optarg)
  420. {
  421. sym_id_add (optarg, EXCL_EXEC);
  422. output_style |= STYLE_EXEC_COUNTS;
  423. }
  424. else
  425. {
  426. output_style &= ~STYLE_EXEC_COUNTS;
  427. }
  428. user_specified |= STYLE_ANNOTATED_SOURCE;
  429. break;
  430. case OPTION_DEMANGLE:
  431. demangle = TRUE;
  432. if (optarg != NULL)
  433. {
  434. enum demangling_styles style;
  435. style = cplus_demangle_name_to_style (optarg);
  436. if (style == unknown_demangling)
  437. {
  438. fprintf (stderr,
  439. _("%s: unknown demangling style `%s'\n"),
  440. whoami, optarg);
  441. xexit (1);
  442. }
  443. cplus_demangle_set_style (style);
  444. }
  445. break;
  446. case OPTION_NO_DEMANGLE:
  447. demangle = FALSE;
  448. break;
  449. case OPTION_INLINE_FILE_NAMES:
  450. inline_file_names = TRUE;
  451. break;
  452. default:
  453. usage (stderr, 1);
  454. }
  455. }
  456. /* Don't allow both ordering options, they modify the arc data in-place. */
  457. if ((user_specified & STYLE_FUNCTION_ORDER)
  458. && (user_specified & STYLE_FILE_ORDER))
  459. {
  460. fprintf (stderr,_("\
  461. %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
  462. whoami);
  463. done (1);
  464. }
  465. /* --sum implies --line, otherwise we'd lose basic block counts in
  466. gmon.sum */
  467. if (output_style & STYLE_SUMMARY_FILE)
  468. line_granularity = 1;
  469. /* append value of GPROF_PATH to source search list if set: */
  470. str = (char *) getenv ("GPROF_PATH");
  471. if (str)
  472. search_list_append (&src_search_list, str);
  473. if (optind < argc)
  474. a_out_name = argv[optind++];
  475. if (optind < argc)
  476. gmon_name = argv[optind++];
  477. /* Turn off default functions. */
  478. for (sp = &default_excluded_list[0]; *sp; sp++)
  479. {
  480. sym_id_add (*sp, EXCL_TIME);
  481. sym_id_add (*sp, EXCL_GRAPH);
  482. sym_id_add (*sp, EXCL_FLAT);
  483. }
  484. /* Read symbol table from core file. */
  485. core_init (a_out_name);
  486. /* If we should ignore direct function calls, we need to load to
  487. core's text-space. */
  488. if (ignore_direct_calls)
  489. core_get_text_space (core_bfd);
  490. /* Create symbols from core image. */
  491. if (external_symbol_table)
  492. core_create_syms_from (external_symbol_table);
  493. else if (line_granularity)
  494. core_create_line_syms ();
  495. else
  496. core_create_function_syms ();
  497. /* Translate sym specs into syms. */
  498. sym_id_parse ();
  499. if (file_format == FF_PROF)
  500. {
  501. fprintf (stderr,
  502. _("%s: sorry, file format `prof' is not yet supported\n"),
  503. whoami);
  504. done (1);
  505. }
  506. else
  507. {
  508. /* Get information about gmon.out file(s). */
  509. do
  510. {
  511. gmon_out_read (gmon_name);
  512. if (optind < argc)
  513. gmon_name = argv[optind];
  514. }
  515. while (optind++ < argc);
  516. }
  517. /* If user did not specify output style, try to guess something
  518. reasonable. */
  519. if (output_style == 0)
  520. {
  521. if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
  522. {
  523. if (gmon_input & INPUT_HISTOGRAM)
  524. output_style |= STYLE_FLAT_PROFILE;
  525. if (gmon_input & INPUT_CALL_GRAPH)
  526. output_style |= STYLE_CALL_GRAPH;
  527. }
  528. else
  529. output_style = STYLE_EXEC_COUNTS;
  530. output_style &= ~user_specified;
  531. }
  532. /* Dump a gmon.sum file if requested (before any other
  533. processing!) */
  534. if (output_style & STYLE_SUMMARY_FILE)
  535. {
  536. gmon_out_write (GMONSUM);
  537. }
  538. if (gmon_input & INPUT_HISTOGRAM)
  539. {
  540. hist_assign_samples ();
  541. }
  542. if (gmon_input & INPUT_CALL_GRAPH)
  543. {
  544. cg = cg_assemble ();
  545. }
  546. /* Do some simple sanity checks. */
  547. if ((output_style & STYLE_FLAT_PROFILE)
  548. && !(gmon_input & INPUT_HISTOGRAM))
  549. {
  550. fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
  551. done (1);
  552. }
  553. if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
  554. {
  555. fprintf (stderr,
  556. _("%s: gmon.out file is missing call-graph data\n"), whoami);
  557. done (1);
  558. }
  559. /* Output whatever user whishes to see. */
  560. if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
  561. {
  562. /* Print the dynamic profile. */
  563. cg_print (cg);
  564. }
  565. if (output_style & STYLE_FLAT_PROFILE)
  566. {
  567. /* Print the flat profile. */
  568. hist_print ();
  569. }
  570. if (cg && (output_style & STYLE_CALL_GRAPH))
  571. {
  572. if (!bsd_style_output)
  573. {
  574. /* Print the dynamic profile. */
  575. cg_print (cg);
  576. }
  577. cg_print_index ();
  578. }
  579. if (output_style & STYLE_EXEC_COUNTS)
  580. print_exec_counts ();
  581. if (output_style & STYLE_ANNOTATED_SOURCE)
  582. print_annotated_source ();
  583. if (output_style & STYLE_FUNCTION_ORDER)
  584. cg_print_function_ordering ();
  585. if (output_style & STYLE_FILE_ORDER)
  586. cg_print_file_ordering ();
  587. return 0;
  588. }
  589. void
  590. done (int status)
  591. {
  592. exit (status);
  593. }