2 * Copyright (c) 1983, 1993, 1998, 2001, 2002
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
31 #include "libiberty.h"
33 #include "search_list.h"
36 #include "basic_blocks.h"
37 #include "call_graph.h"
48 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
51 const char *function_mapping_file;
52 const char *a_out_name = A_OUTNAME;
56 * Default options values:
60 int output_width = 80;
61 bfd_boolean bsd_style_output = FALSE;
62 bfd_boolean demangle = TRUE;
63 bfd_boolean ignore_direct_calls = FALSE;
64 bfd_boolean ignore_static_funcs = FALSE;
65 bfd_boolean ignore_zeros = TRUE;
66 bfd_boolean line_granularity = FALSE;
67 bfd_boolean print_descriptions = TRUE;
68 bfd_boolean print_path = FALSE;
69 bfd_boolean ignore_non_functions = FALSE;
70 File_Format file_format = FF_AUTO;
72 bfd_boolean first_output = TRUE;
75 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
76 All rights reserved.\n";
78 static char *gmon_name = GMONNAME; /* profile filename */
81 * Functions that get excluded by default:
83 static char *default_excluded_list[] =
85 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
90 /* Codes used for the long options with no short synonyms. 150 isn't
91 special; it's just an arbitrary non-ASCII char value. */
93 #define OPTION_DEMANGLE (150)
94 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
96 static struct option long_options[] =
98 {"line", no_argument, 0, 'l'},
99 {"no-static", no_argument, 0, 'a'},
100 {"ignore-non-functions", no_argument, 0, 'D'},
104 {"annotated-source", optional_argument, 0, 'A'},
105 {"no-annotated-source", optional_argument, 0, 'J'},
106 {"flat-profile", optional_argument, 0, 'p'},
107 {"no-flat-profile", optional_argument, 0, 'P'},
108 {"graph", optional_argument, 0, 'q'},
109 {"no-graph", optional_argument, 0, 'Q'},
110 {"exec-counts", optional_argument, 0, 'C'},
111 {"no-exec-counts", optional_argument, 0, 'Z'},
112 {"function-ordering", no_argument, 0, 'r'},
113 {"file-ordering", required_argument, 0, 'R'},
114 {"file-info", no_argument, 0, 'i'},
115 {"sum", no_argument, 0, 's'},
117 /* various options to affect output: */
119 {"all-lines", no_argument, 0, 'x'},
120 {"demangle", optional_argument, 0, OPTION_DEMANGLE},
121 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
122 {"directory-path", required_argument, 0, 'I'},
123 {"display-unused-functions", no_argument, 0, 'z'},
124 {"min-count", required_argument, 0, 'm'},
125 {"print-path", no_argument, 0, 'L'},
126 {"separate-files", no_argument, 0, 'y'},
127 {"static-call-graph", no_argument, 0, 'c'},
128 {"table-length", required_argument, 0, 't'},
129 {"time", required_argument, 0, 'n'},
130 {"no-time", required_argument, 0, 'N'},
131 {"width", required_argument, 0, 'w'},
133 * These are for backwards-compatibility only. Their functionality
134 * is provided by the output style options already:
136 {"", required_argument, 0, 'e'},
137 {"", required_argument, 0, 'E'},
138 {"", required_argument, 0, 'f'},
139 {"", required_argument, 0, 'F'},
140 {"", required_argument, 0, 'k'},
144 {"brief", no_argument, 0, 'b'},
145 {"debug", optional_argument, 0, 'd'},
146 {"help", no_argument, 0, 'h'},
147 {"file-format", required_argument, 0, 'O'},
148 {"traditional", no_argument, 0, 'T'},
149 {"version", no_argument, 0, 'v'},
150 {0, no_argument, 0, 0}
155 usage (FILE *stream, int status)
157 fprintf (stream, _("\
158 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
159 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
160 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
161 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
162 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
163 [--function-ordering] [--file-ordering]\n\
164 [--directory-path=dirs] [--display-unused-functions]\n\
165 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
166 [--no-static] [--print-path] [--separate-files]\n\
167 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
168 [--version] [--width=n] [--ignore-non-functions]\n\
169 [--demangle[=STYLE]] [--no-demangle] [@FILE]\n\
170 [image-file] [profile-file...]\n"),
172 if (REPORT_BUGS_TO[0] && status == 0)
173 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
179 main (int argc, char **argv)
183 int ch, user_specified = 0;
185 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
186 setlocale (LC_MESSAGES, "");
188 #if defined (HAVE_SETLOCALE)
189 setlocale (LC_CTYPE, "");
192 bindtextdomain (PACKAGE, LOCALEDIR);
193 textdomain (PACKAGE);
197 xmalloc_set_program_name (whoami);
199 expandargv (&argc, &argv);
201 while ((ch = getopt_long (argc, argv,
202 "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:st:Tvw:xyzZ::",
209 ignore_static_funcs = TRUE;
214 sym_id_add (optarg, INCL_ANNO);
216 output_style |= STYLE_ANNOTATED_SOURCE;
217 user_specified |= STYLE_ANNOTATED_SOURCE;
220 print_descriptions = FALSE;
223 output_style |= STYLE_CALL_GRAPH;
224 user_specified |= STYLE_CALL_GRAPH;
227 ignore_direct_calls = TRUE;
232 sym_id_add (optarg, INCL_EXEC);
234 output_style |= STYLE_EXEC_COUNTS;
235 user_specified |= STYLE_EXEC_COUNTS;
240 debug_level |= atoi (optarg);
241 debug_level |= ANYDEBUG;
247 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
249 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
253 ignore_non_functions = TRUE;
256 sym_id_add (optarg, EXCL_TIME);
258 sym_id_add (optarg, EXCL_GRAPH);
261 sym_id_add (optarg, INCL_TIME);
263 sym_id_add (optarg, INCL_GRAPH);
266 sym_id_add (optarg, EXCL_FLAT);
269 sym_id_add (optarg, INCL_FLAT);
274 output_style |= STYLE_GMON_INFO;
275 user_specified |= STYLE_GMON_INFO;
278 search_list_append (&src_search_list, optarg);
283 sym_id_add (optarg, EXCL_ANNO);
284 output_style |= STYLE_ANNOTATED_SOURCE;
288 output_style &= ~STYLE_ANNOTATED_SOURCE;
290 user_specified |= STYLE_ANNOTATED_SOURCE;
293 sym_id_add (optarg, EXCL_ARCS);
296 line_granularity = TRUE;
302 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
305 sym_id_add (optarg, INCL_TIME);
308 sym_id_add (optarg, EXCL_TIME);
314 file_format = FF_AUTO;
317 file_format = FF_MAGIC;
320 file_format = FF_BSD;
323 file_format = FF_BSD44;
326 file_format = FF_PROF;
329 fprintf (stderr, _("%s: unknown file format %s\n"),
337 sym_id_add (optarg, INCL_FLAT);
339 output_style |= STYLE_FLAT_PROFILE;
340 user_specified |= STYLE_FLAT_PROFILE;
345 sym_id_add (optarg, EXCL_FLAT);
346 output_style |= STYLE_FLAT_PROFILE;
350 output_style &= ~STYLE_FLAT_PROFILE;
352 user_specified |= STYLE_FLAT_PROFILE;
357 if (strchr (optarg, '/'))
359 sym_id_add (optarg, INCL_ARCS);
363 sym_id_add (optarg, INCL_GRAPH);
366 output_style |= STYLE_CALL_GRAPH;
367 user_specified |= STYLE_CALL_GRAPH;
370 output_style |= STYLE_FUNCTION_ORDER;
371 user_specified |= STYLE_FUNCTION_ORDER;
374 output_style |= STYLE_FILE_ORDER;
375 user_specified |= STYLE_FILE_ORDER;
376 function_mapping_file = optarg;
381 if (strchr (optarg, '/'))
383 sym_id_add (optarg, EXCL_ARCS);
387 sym_id_add (optarg, EXCL_GRAPH);
389 output_style |= STYLE_CALL_GRAPH;
393 output_style &= ~STYLE_CALL_GRAPH;
395 user_specified |= STYLE_CALL_GRAPH;
398 output_style |= STYLE_SUMMARY_FILE;
399 user_specified |= STYLE_SUMMARY_FILE;
402 bb_table_length = atoi (optarg);
403 if (bb_table_length < 0)
409 bsd_style_output = TRUE;
412 /* This output is intended to follow the GNU standards document. */
413 printf (_("GNU gprof %s\n"), BFD_VERSION_STRING);
414 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
416 This program is free software. This program has absolutely no warranty.\n"));
419 output_width = atoi (optarg);
420 if (output_width < 1)
426 bb_annotate_all_lines = TRUE;
429 create_annotation_files = TRUE;
432 ignore_zeros = FALSE;
437 sym_id_add (optarg, EXCL_EXEC);
438 output_style |= STYLE_EXEC_COUNTS;
442 output_style &= ~STYLE_EXEC_COUNTS;
444 user_specified |= STYLE_ANNOTATED_SOURCE;
446 case OPTION_DEMANGLE:
450 enum demangling_styles style;
452 style = cplus_demangle_name_to_style (optarg);
453 if (style == unknown_demangling)
456 _("%s: unknown demangling style `%s'\n"),
461 cplus_demangle_set_style (style);
464 case OPTION_NO_DEMANGLE:
472 /* Don't allow both ordering options, they modify the arc data in-place. */
473 if ((user_specified & STYLE_FUNCTION_ORDER)
474 && (user_specified & STYLE_FILE_ORDER))
477 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
482 /* --sum implies --line, otherwise we'd lose basic block counts in
484 if (output_style & STYLE_SUMMARY_FILE)
485 line_granularity = 1;
487 /* append value of GPROF_PATH to source search list if set: */
488 str = (char *) getenv ("GPROF_PATH");
490 search_list_append (&src_search_list, str);
493 a_out_name = argv[optind++];
496 gmon_name = argv[optind++];
498 /* Turn off default functions. */
499 for (sp = &default_excluded_list[0]; *sp; sp++)
501 sym_id_add (*sp, EXCL_TIME);
502 sym_id_add (*sp, EXCL_GRAPH);
503 sym_id_add (*sp, EXCL_FLAT);
506 /* Read symbol table from core file. */
507 core_init (a_out_name);
509 /* If we should ignore direct function calls, we need to load to
510 core's text-space. */
511 if (ignore_direct_calls)
512 core_get_text_space (core_bfd);
514 /* Create symbols from core image. */
515 if (line_granularity)
516 core_create_line_syms ();
518 core_create_function_syms ();
520 /* Translate sym specs into syms. */
523 if (file_format == FF_PROF)
526 _("%s: sorry, file format `prof' is not yet supported\n"),
532 /* Get information about gmon.out file(s). */
535 gmon_out_read (gmon_name);
537 gmon_name = argv[optind];
539 while (optind++ < argc);
542 /* If user did not specify output style, try to guess something
544 if (output_style == 0)
546 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
548 if (gmon_input & INPUT_HISTOGRAM)
549 output_style |= STYLE_FLAT_PROFILE;
550 if (gmon_input & INPUT_CALL_GRAPH)
551 output_style |= STYLE_CALL_GRAPH;
554 output_style = STYLE_EXEC_COUNTS;
556 output_style &= ~user_specified;
559 /* Dump a gmon.sum file if requested (before any other
561 if (output_style & STYLE_SUMMARY_FILE)
563 gmon_out_write (GMONSUM);
566 if (gmon_input & INPUT_HISTOGRAM)
568 hist_assign_samples ();
571 if (gmon_input & INPUT_CALL_GRAPH)
576 /* Do some simple sanity checks. */
577 if ((output_style & STYLE_FLAT_PROFILE)
578 && !(gmon_input & INPUT_HISTOGRAM))
580 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
584 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
587 _("%s: gmon.out file is missing call-graph data\n"), whoami);
591 /* Output whatever user whishes to see. */
592 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
594 /* Print the dynamic profile. */
598 if (output_style & STYLE_FLAT_PROFILE)
600 /* Print the flat profile. */
604 if (cg && (output_style & STYLE_CALL_GRAPH))
606 if (!bsd_style_output)
608 /* Print the dynamic profile. */
614 if (output_style & STYLE_EXEC_COUNTS)
615 print_exec_counts ();
617 if (output_style & STYLE_ANNOTATED_SOURCE)
618 print_annotated_source ();
620 if (output_style & STYLE_FUNCTION_ORDER)
621 cg_print_function_ordering ();
623 if (output_style & STYLE_FILE_ORDER)
624 cg_print_file_ordering ();