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
30 #include "libiberty.h"
32 #include "search_list.h"
35 #include "basic_blocks.h"
36 #include "call_graph.h"
47 static void usage
PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN
;
48 int main
PARAMS ((int, char **));
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 discard_underscores
= 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 File_Format file_format
= FF_AUTO
;
73 bfd_boolean first_output
= TRUE
;
76 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
77 All rights reserved.\n";
79 static char *gmon_name
= GMONNAME
; /* profile filename */
84 * Functions that get excluded by default:
86 static char *default_excluded_list
[] =
88 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
90 "<locore>", "<hicore>",
94 /* Codes used for the long options with no short synonyms. 150 isn't
95 special; it's just an arbitrary non-ASCII char value. */
97 #define OPTION_DEMANGLE (150)
98 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
100 static struct option long_options
[] =
102 {"line", no_argument
, 0, 'l'},
103 {"no-static", no_argument
, 0, 'a'},
104 {"ignore-non-functions", no_argument
, 0, 'D'},
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 {"min-count", required_argument
, 0, 'm'},
129 {"print-path", no_argument
, 0, 'L'},
130 {"separate-files", no_argument
, 0, 'y'},
131 {"static-call-graph", no_argument
, 0, 'c'},
132 {"table-length", required_argument
, 0, 't'},
133 {"time", required_argument
, 0, 'n'},
134 {"no-time", required_argument
, 0, 'N'},
135 {"width", required_argument
, 0, 'w'},
137 * These are for backwards-compatibility only. Their functionality
138 * is provided by the output style options already:
140 {"", required_argument
, 0, 'e'},
141 {"", required_argument
, 0, 'E'},
142 {"", required_argument
, 0, 'f'},
143 {"", required_argument
, 0, 'F'},
144 {"", required_argument
, 0, 'k'},
148 {"brief", no_argument
, 0, 'b'},
149 {"debug", optional_argument
, 0, 'd'},
150 {"help", no_argument
, 0, 'h'},
151 {"file-format", required_argument
, 0, 'O'},
152 {"traditional", no_argument
, 0, 'T'},
153 {"version", no_argument
, 0, 'v'},
154 {0, no_argument
, 0, 0}
159 usage (stream
, status
)
163 fprintf (stream
, _("\
164 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
165 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
166 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
167 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
168 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
169 [--function-ordering] [--file-ordering]\n\
170 [--directory-path=dirs] [--display-unused-functions]\n\
171 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
172 [--no-static] [--print-path] [--separate-files]\n\
173 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
174 [--version] [--width=n] [--ignore-non-functions]\n\
175 [--demangle[=STYLE]] [--no-demangle]\n\
176 [image-file] [profile-file...]\n"),
179 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
191 int ch
, user_specified
= 0;
193 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
194 setlocale (LC_MESSAGES
, "");
196 #if defined (HAVE_SETLOCALE)
197 setlocale (LC_CTYPE
, "");
199 bindtextdomain (PACKAGE
, LOCALEDIR
);
200 textdomain (PACKAGE
);
203 xmalloc_set_program_name (whoami
);
205 while ((ch
= getopt_long (argc
, argv
,
206 "aA::bBcCd::De:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
213 ignore_static_funcs
= TRUE
;
218 sym_id_add (optarg
, INCL_ANNO
);
220 output_style
|= STYLE_ANNOTATED_SOURCE
;
221 user_specified
|= STYLE_ANNOTATED_SOURCE
;
224 print_descriptions
= FALSE
;
227 output_style
|= STYLE_CALL_GRAPH
;
228 user_specified
|= STYLE_CALL_GRAPH
;
231 ignore_direct_calls
= TRUE
;
236 sym_id_add (optarg
, INCL_EXEC
);
238 output_style
|= STYLE_EXEC_COUNTS
;
239 user_specified
|= STYLE_EXEC_COUNTS
;
244 debug_level
|= atoi (optarg
);
245 debug_level
|= ANYDEBUG
;
251 DBG (ANYDEBUG
, printf ("[main] debug-level=0x%x\n", debug_level
));
253 printf (_("%s: debugging not supported; -d ignored\n"), whoami
);
257 ignore_non_functions
= TRUE
;
260 sym_id_add (optarg
, EXCL_TIME
);
262 sym_id_add (optarg
, EXCL_GRAPH
);
265 sym_id_add (optarg
, INCL_TIME
);
267 sym_id_add (optarg
, INCL_GRAPH
);
270 sym_id_add (optarg
, EXCL_FLAT
);
273 sym_id_add (optarg
, INCL_FLAT
);
278 output_style
|= STYLE_GMON_INFO
;
279 user_specified
|= STYLE_GMON_INFO
;
282 search_list_append (&src_search_list
, optarg
);
287 sym_id_add (optarg
, EXCL_ANNO
);
288 output_style
|= STYLE_ANNOTATED_SOURCE
;
292 output_style
&= ~STYLE_ANNOTATED_SOURCE
;
294 user_specified
|= STYLE_ANNOTATED_SOURCE
;
297 sym_id_add (optarg
, EXCL_ARCS
);
300 line_granularity
= TRUE
;
306 bb_min_calls
= (unsigned long) strtoul (optarg
, (char **) NULL
, 10);
309 sym_id_add (optarg
, INCL_TIME
);
312 sym_id_add (optarg
, EXCL_TIME
);
318 file_format
= FF_AUTO
;
321 file_format
= FF_MAGIC
;
324 file_format
= FF_BSD
;
327 file_format
= FF_BSD44
;
330 file_format
= FF_PROF
;
333 fprintf (stderr
, _("%s: unknown file format %s\n"),
341 sym_id_add (optarg
, INCL_FLAT
);
343 output_style
|= STYLE_FLAT_PROFILE
;
344 user_specified
|= STYLE_FLAT_PROFILE
;
349 sym_id_add (optarg
, EXCL_FLAT
);
350 output_style
|= STYLE_FLAT_PROFILE
;
354 output_style
&= ~STYLE_FLAT_PROFILE
;
356 user_specified
|= STYLE_FLAT_PROFILE
;
361 if (strchr (optarg
, '/'))
363 sym_id_add (optarg
, INCL_ARCS
);
367 sym_id_add (optarg
, INCL_GRAPH
);
370 output_style
|= STYLE_CALL_GRAPH
;
371 user_specified
|= STYLE_CALL_GRAPH
;
374 output_style
|= STYLE_FUNCTION_ORDER
;
375 user_specified
|= STYLE_FUNCTION_ORDER
;
378 output_style
|= STYLE_FILE_ORDER
;
379 user_specified
|= STYLE_FILE_ORDER
;
380 function_mapping_file
= optarg
;
385 if (strchr (optarg
, '/'))
387 sym_id_add (optarg
, EXCL_ARCS
);
391 sym_id_add (optarg
, EXCL_GRAPH
);
393 output_style
|= STYLE_CALL_GRAPH
;
397 output_style
&= ~STYLE_CALL_GRAPH
;
399 user_specified
|= STYLE_CALL_GRAPH
;
402 output_style
|= STYLE_SUMMARY_FILE
;
403 user_specified
|= STYLE_SUMMARY_FILE
;
406 bb_table_length
= atoi (optarg
);
407 if (bb_table_length
< 0)
413 bsd_style_output
= TRUE
;
416 /* This output is intended to follow the GNU standards document. */
417 printf (_("GNU gprof %s\n"), VERSION
);
418 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
420 This program is free software. This program has absolutely no warranty.\n"));
423 output_width
= atoi (optarg
);
424 if (output_width
< 1)
430 bb_annotate_all_lines
= TRUE
;
433 create_annotation_files
= TRUE
;
436 ignore_zeros
= FALSE
;
441 sym_id_add (optarg
, EXCL_EXEC
);
442 output_style
|= STYLE_EXEC_COUNTS
;
446 output_style
&= ~STYLE_EXEC_COUNTS
;
448 user_specified
|= STYLE_ANNOTATED_SOURCE
;
450 case OPTION_DEMANGLE
:
454 enum demangling_styles style
;
456 style
= cplus_demangle_name_to_style (optarg
);
457 if (style
== unknown_demangling
)
460 _("%s: unknown demangling style `%s'\n"),
465 cplus_demangle_set_style (style
);
468 case OPTION_NO_DEMANGLE
:
476 /* Don't allow both ordering options, they modify the arc data in-place. */
477 if ((user_specified
& STYLE_FUNCTION_ORDER
)
478 && (user_specified
& STYLE_FILE_ORDER
))
481 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
486 /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
487 if (output_style
& STYLE_SUMMARY_FILE
)
489 line_granularity
= 1;
492 /* append value of GPROF_PATH to source search list if set: */
493 str
= (char *) getenv ("GPROF_PATH");
496 search_list_append (&src_search_list
, str
);
501 a_out_name
= argv
[optind
++];
505 gmon_name
= argv
[optind
++];
509 * Turn off default functions:
511 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
513 sym_id_add (*sp
, EXCL_TIME
);
514 sym_id_add (*sp
, EXCL_GRAPH
);
515 sym_id_add (*sp
, EXCL_FLAT
);
519 * For line-by-line profiling, also want to keep those
520 * functions off the flat profile:
522 if (line_granularity
)
524 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
526 sym_id_add (*sp
, EXCL_FLAT
);
531 * Read symbol table from core file:
533 core_init (a_out_name
);
536 * If we should ignore direct function calls, we need to load
537 * to core's text-space:
539 if (ignore_direct_calls
)
541 core_get_text_space (core_bfd
);
545 * Create symbols from core image:
547 if (line_granularity
)
549 core_create_line_syms (core_bfd
);
553 core_create_function_syms (core_bfd
);
557 * Translate sym specs into syms:
561 if (file_format
== FF_PROF
)
563 #ifdef PROF_SUPPORT_IMPLEMENTED
565 * Get information about mon.out file(s):
569 mon_out_read (gmon_name
);
572 gmon_name
= argv
[optind
];
575 while (optind
++ < argc
);
578 _("%s: sorry, file format `prof' is not yet supported\n"),
586 * Get information about gmon.out file(s):
590 gmon_out_read (gmon_name
);
593 gmon_name
= argv
[optind
];
596 while (optind
++ < argc
);
600 * If user did not specify output style, try to guess something
603 if (output_style
== 0)
605 if (gmon_input
& (INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
))
607 output_style
= STYLE_FLAT_PROFILE
| STYLE_CALL_GRAPH
;
611 output_style
= STYLE_EXEC_COUNTS
;
613 output_style
&= ~user_specified
;
617 * Dump a gmon.sum file if requested (before any other processing!):
619 if (output_style
& STYLE_SUMMARY_FILE
)
621 gmon_out_write (GMONSUM
);
624 if (gmon_input
& INPUT_HISTOGRAM
)
626 hist_assign_samples ();
629 if (gmon_input
& INPUT_CALL_GRAPH
)
634 /* do some simple sanity checks: */
636 if ((output_style
& STYLE_FLAT_PROFILE
)
637 && !(gmon_input
& INPUT_HISTOGRAM
))
639 fprintf (stderr
, _("%s: gmon.out file is missing histogram\n"), whoami
);
643 if ((output_style
& STYLE_CALL_GRAPH
) && !(gmon_input
& INPUT_CALL_GRAPH
))
646 _("%s: gmon.out file is missing call-graph data\n"), whoami
);
650 /* output whatever user whishes to see: */
652 if (cg
&& (output_style
& STYLE_CALL_GRAPH
) && bsd_style_output
)
654 cg_print (cg
); /* print the dynamic profile */
657 if (output_style
& STYLE_FLAT_PROFILE
)
659 hist_print (); /* print the flat profile */
662 if (cg
&& (output_style
& STYLE_CALL_GRAPH
))
664 if (!bsd_style_output
)
666 cg_print (cg
); /* print the dynamic profile */
671 if (output_style
& STYLE_EXEC_COUNTS
)
673 print_exec_counts ();
676 if (output_style
& STYLE_ANNOTATED_SOURCE
)
678 print_annotated_source ();
680 if (output_style
& STYLE_FUNCTION_ORDER
)
682 cg_print_function_ordering ();
684 if (output_style
& STYLE_FILE_ORDER
)
686 cg_print_file_ordering ();