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 static const char * external_symbol_table
;
53 const char * a_out_name
= A_OUTNAME
;
57 * Default options values:
61 int output_width
= 80;
62 bfd_boolean bsd_style_output
= FALSE
;
63 bfd_boolean demangle
= TRUE
;
64 bfd_boolean ignore_direct_calls
= FALSE
;
65 bfd_boolean ignore_static_funcs
= FALSE
;
66 bfd_boolean ignore_zeros
= TRUE
;
67 bfd_boolean line_granularity
= FALSE
;
68 bfd_boolean print_descriptions
= TRUE
;
69 bfd_boolean print_path
= FALSE
;
70 bfd_boolean ignore_non_functions
= FALSE
;
71 bfd_boolean inline_file_names
= FALSE
;
72 File_Format file_format
= FF_AUTO
;
74 bfd_boolean first_output
= TRUE
;
77 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
78 All rights reserved.\n";
80 static char *gmon_name
= GMONNAME
; /* profile filename */
83 * Functions that get excluded by default:
85 static char *default_excluded_list
[] =
87 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
92 /* Codes used for the long options with no short synonyms. 150 isn't
93 special; it's just an arbitrary non-ASCII char value. */
95 #define OPTION_DEMANGLE (150)
96 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
97 #define OPTION_INLINE_FILE_NAMES (OPTION_DEMANGLE + 2)
99 static struct option long_options
[] =
101 {"line", no_argument
, 0, 'l'},
102 {"no-static", no_argument
, 0, 'a'},
103 {"ignore-non-functions", no_argument
, 0, 'D'},
104 {"external-symbol-table", required_argument
, 0, 'S'},
108 {"annotated-source", optional_argument
, 0, 'A'},
109 {"no-annotated-source", optional_argument
, 0, 'J'},
110 {"flat-profile", optional_argument
, 0, 'p'},
111 {"no-flat-profile", optional_argument
, 0, 'P'},
112 {"graph", optional_argument
, 0, 'q'},
113 {"no-graph", optional_argument
, 0, 'Q'},
114 {"exec-counts", optional_argument
, 0, 'C'},
115 {"no-exec-counts", optional_argument
, 0, 'Z'},
116 {"function-ordering", no_argument
, 0, 'r'},
117 {"file-ordering", required_argument
, 0, 'R'},
118 {"file-info", no_argument
, 0, 'i'},
119 {"sum", no_argument
, 0, 's'},
121 /* various options to affect output: */
123 {"all-lines", no_argument
, 0, 'x'},
124 {"demangle", optional_argument
, 0, OPTION_DEMANGLE
},
125 {"no-demangle", no_argument
, 0, OPTION_NO_DEMANGLE
},
126 {"directory-path", required_argument
, 0, 'I'},
127 {"display-unused-functions", no_argument
, 0, 'z'},
128 {"inline-file-names", no_argument
, 0, OPTION_INLINE_FILE_NAMES
},
129 {"min-count", required_argument
, 0, 'm'},
130 {"print-path", no_argument
, 0, 'L'},
131 {"separate-files", no_argument
, 0, 'y'},
132 {"static-call-graph", no_argument
, 0, 'c'},
133 {"table-length", required_argument
, 0, 't'},
134 {"time", required_argument
, 0, 'n'},
135 {"no-time", required_argument
, 0, 'N'},
136 {"width", required_argument
, 0, 'w'},
138 * These are for backwards-compatibility only. Their functionality
139 * is provided by the output style options already:
141 {"", required_argument
, 0, 'e'},
142 {"", required_argument
, 0, 'E'},
143 {"", required_argument
, 0, 'f'},
144 {"", required_argument
, 0, 'F'},
145 {"", required_argument
, 0, 'k'},
149 {"brief", no_argument
, 0, 'b'},
150 {"debug", optional_argument
, 0, 'd'},
151 {"help", no_argument
, 0, 'h'},
152 {"file-format", required_argument
, 0, 'O'},
153 {"traditional", no_argument
, 0, 'T'},
154 {"version", no_argument
, 0, 'v'},
155 {0, no_argument
, 0, 0}
160 usage (FILE *stream
, int status
)
162 fprintf (stream
, _("\
163 Usage: %s [-[abcDhilLrsTvwxyz]] [-[ACeEfFJnNOpPqQRStZ][name]] [-I dirs]\n\
164 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
165 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
166 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
167 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
168 [--function-ordering] [--file-ordering] [--inline-file-names]\n\
169 [--directory-path=dirs] [--display-unused-functions]\n\
170 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
171 [--no-static] [--print-path] [--separate-files]\n\
172 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
173 [--version] [--width=n] [--ignore-non-functions]\n\
174 [--demangle[=STYLE]] [--no-demangle] [--external-symbol-table=name] [@FILE]\n\
175 [image-file] [profile-file...]\n"),
177 if (REPORT_BUGS_TO
[0] && status
== 0)
178 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
184 main (int argc
, char **argv
)
188 int ch
, user_specified
= 0;
190 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
191 setlocale (LC_MESSAGES
, "");
193 #if defined (HAVE_SETLOCALE)
194 setlocale (LC_CTYPE
, "");
197 bindtextdomain (PACKAGE
, LOCALEDIR
);
198 textdomain (PACKAGE
);
202 xmalloc_set_program_name (whoami
);
204 expandargv (&argc
, &argv
);
206 while ((ch
= getopt_long (argc
, argv
,
207 "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:sS:t:Tvw:xyzZ::",
214 ignore_static_funcs
= TRUE
;
219 sym_id_add (optarg
, INCL_ANNO
);
221 output_style
|= STYLE_ANNOTATED_SOURCE
;
222 user_specified
|= STYLE_ANNOTATED_SOURCE
;
225 print_descriptions
= FALSE
;
228 output_style
|= STYLE_CALL_GRAPH
;
229 user_specified
|= STYLE_CALL_GRAPH
;
232 ignore_direct_calls
= TRUE
;
237 sym_id_add (optarg
, INCL_EXEC
);
239 output_style
|= STYLE_EXEC_COUNTS
;
240 user_specified
|= STYLE_EXEC_COUNTS
;
245 debug_level
|= atoi (optarg
);
246 debug_level
|= ANYDEBUG
;
252 DBG (ANYDEBUG
, printf ("[main] debug-level=0x%x\n", debug_level
));
254 printf (_("%s: debugging not supported; -d ignored\n"), whoami
);
258 ignore_non_functions
= TRUE
;
261 sym_id_add (optarg
, EXCL_TIME
);
264 sym_id_add (optarg
, EXCL_GRAPH
);
267 sym_id_add (optarg
, INCL_TIME
);
270 sym_id_add (optarg
, INCL_GRAPH
);
272 /* FIXME: The -g and -G options are not present in the getopt_long
273 invocation above, and they are not documented in gprof.texi.
274 Therefore they appear to be deprecated. Test this theory and
275 delete them if true. */
277 sym_id_add (optarg
, EXCL_FLAT
);
280 sym_id_add (optarg
, INCL_FLAT
);
285 output_style
|= STYLE_GMON_INFO
;
286 user_specified
|= STYLE_GMON_INFO
;
289 search_list_append (&src_search_list
, optarg
);
294 sym_id_add (optarg
, EXCL_ANNO
);
295 output_style
|= STYLE_ANNOTATED_SOURCE
;
299 output_style
&= ~STYLE_ANNOTATED_SOURCE
;
301 user_specified
|= STYLE_ANNOTATED_SOURCE
;
304 sym_id_add (optarg
, EXCL_ARCS
);
307 line_granularity
= TRUE
;
313 bb_min_calls
= (unsigned long) strtoul (optarg
, (char **) NULL
, 10);
316 sym_id_add (optarg
, INCL_TIME
);
319 sym_id_add (optarg
, EXCL_TIME
);
325 file_format
= FF_AUTO
;
328 file_format
= FF_MAGIC
;
331 file_format
= FF_BSD
;
334 file_format
= FF_BSD44
;
337 file_format
= FF_PROF
;
340 fprintf (stderr
, _("%s: unknown file format %s\n"),
348 sym_id_add (optarg
, INCL_FLAT
);
350 output_style
|= STYLE_FLAT_PROFILE
;
351 user_specified
|= STYLE_FLAT_PROFILE
;
356 sym_id_add (optarg
, EXCL_FLAT
);
357 output_style
|= STYLE_FLAT_PROFILE
;
361 output_style
&= ~STYLE_FLAT_PROFILE
;
363 user_specified
|= STYLE_FLAT_PROFILE
;
368 if (strchr (optarg
, '/'))
370 sym_id_add (optarg
, INCL_ARCS
);
374 sym_id_add (optarg
, INCL_GRAPH
);
377 output_style
|= STYLE_CALL_GRAPH
;
378 user_specified
|= STYLE_CALL_GRAPH
;
381 output_style
|= STYLE_FUNCTION_ORDER
;
382 user_specified
|= STYLE_FUNCTION_ORDER
;
385 output_style
|= STYLE_FILE_ORDER
;
386 user_specified
|= STYLE_FILE_ORDER
;
387 function_mapping_file
= optarg
;
392 if (strchr (optarg
, '/'))
394 sym_id_add (optarg
, EXCL_ARCS
);
398 sym_id_add (optarg
, EXCL_GRAPH
);
400 output_style
|= STYLE_CALL_GRAPH
;
404 output_style
&= ~STYLE_CALL_GRAPH
;
406 user_specified
|= STYLE_CALL_GRAPH
;
409 output_style
|= STYLE_SUMMARY_FILE
;
410 user_specified
|= STYLE_SUMMARY_FILE
;
413 external_symbol_table
= optarg
;
414 DBG (AOUTDEBUG
, printf ("external-symbol-table: %s\n", optarg
));
417 bb_table_length
= atoi (optarg
);
418 if (bb_table_length
< 0)
424 bsd_style_output
= TRUE
;
427 /* This output is intended to follow the GNU standards document. */
428 printf (_("GNU gprof %s\n"), BFD_VERSION_STRING
);
429 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
431 This program is free software. This program has absolutely no warranty.\n"));
434 output_width
= atoi (optarg
);
435 if (output_width
< 1)
441 bb_annotate_all_lines
= TRUE
;
444 create_annotation_files
= TRUE
;
447 ignore_zeros
= FALSE
;
452 sym_id_add (optarg
, EXCL_EXEC
);
453 output_style
|= STYLE_EXEC_COUNTS
;
457 output_style
&= ~STYLE_EXEC_COUNTS
;
459 user_specified
|= STYLE_EXEC_COUNTS
;
461 case OPTION_DEMANGLE
:
465 enum demangling_styles style
;
467 style
= cplus_demangle_name_to_style (optarg
);
468 if (style
== unknown_demangling
)
471 _("%s: unknown demangling style `%s'\n"),
476 cplus_demangle_set_style (style
);
479 case OPTION_NO_DEMANGLE
:
482 case OPTION_INLINE_FILE_NAMES
:
483 inline_file_names
= TRUE
;
490 /* Don't allow both ordering options, they modify the arc data in-place. */
491 if ((user_specified
& STYLE_FUNCTION_ORDER
)
492 && (user_specified
& STYLE_FILE_ORDER
))
495 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
500 /* --sum implies --line, otherwise we'd lose basic block counts in
502 if (output_style
& STYLE_SUMMARY_FILE
)
503 line_granularity
= 1;
505 /* append value of GPROF_PATH to source search list if set: */
506 str
= (char *) getenv ("GPROF_PATH");
508 search_list_append (&src_search_list
, str
);
511 a_out_name
= argv
[optind
++];
514 gmon_name
= argv
[optind
++];
516 /* Turn off default functions. */
517 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
519 sym_id_add (*sp
, EXCL_TIME
);
520 sym_id_add (*sp
, EXCL_GRAPH
);
521 sym_id_add (*sp
, EXCL_FLAT
);
524 /* Read symbol table from core file. */
525 core_init (a_out_name
);
527 /* If we should ignore direct function calls, we need to load to
528 core's text-space. */
529 if (ignore_direct_calls
)
530 core_get_text_space (core_bfd
);
532 /* Create symbols from core image. */
533 if (external_symbol_table
)
534 core_create_syms_from (external_symbol_table
);
535 else if (line_granularity
)
536 core_create_line_syms ();
538 core_create_function_syms ();
540 /* Translate sym specs into syms. */
543 if (file_format
== FF_PROF
)
546 _("%s: sorry, file format `prof' is not yet supported\n"),
552 /* Get information about gmon.out file(s). */
555 gmon_out_read (gmon_name
);
557 gmon_name
= argv
[optind
];
559 while (optind
++ < argc
);
562 /* If user did not specify output style, try to guess something
564 if (output_style
== 0)
566 if (gmon_input
& (INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
))
568 if (gmon_input
& INPUT_HISTOGRAM
)
569 output_style
|= STYLE_FLAT_PROFILE
;
570 if (gmon_input
& INPUT_CALL_GRAPH
)
571 output_style
|= STYLE_CALL_GRAPH
;
574 output_style
= STYLE_EXEC_COUNTS
;
576 output_style
&= ~user_specified
;
579 /* Dump a gmon.sum file if requested (before any other
581 if (output_style
& STYLE_SUMMARY_FILE
)
583 gmon_out_write (GMONSUM
);
586 if (gmon_input
& INPUT_HISTOGRAM
)
588 hist_assign_samples ();
591 if (gmon_input
& INPUT_CALL_GRAPH
)
596 /* Do some simple sanity checks. */
597 if ((output_style
& STYLE_FLAT_PROFILE
)
598 && !(gmon_input
& INPUT_HISTOGRAM
))
600 fprintf (stderr
, _("%s: gmon.out file is missing histogram\n"), whoami
);
604 if ((output_style
& STYLE_CALL_GRAPH
) && !(gmon_input
& INPUT_CALL_GRAPH
))
607 _("%s: gmon.out file is missing call-graph data\n"), whoami
);
611 /* Output whatever user whishes to see. */
612 if (cg
&& (output_style
& STYLE_CALL_GRAPH
) && bsd_style_output
)
614 /* Print the dynamic profile. */
618 if (output_style
& STYLE_FLAT_PROFILE
)
620 /* Print the flat profile. */
624 if (cg
&& (output_style
& STYLE_CALL_GRAPH
))
626 if (!bsd_style_output
)
628 /* Print the dynamic profile. */
634 if (output_style
& STYLE_EXEC_COUNTS
)
635 print_exec_counts ();
637 if (output_style
& STYLE_ANNOTATED_SOURCE
)
638 print_annotated_source ();
640 if (output_style
& STYLE_FUNCTION_ORDER
)
641 cg_print_function_ordering ();
643 if (output_style
& STYLE_FILE_ORDER
)
644 cg_print_file_ordering ();