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"
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 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 */
82 * Functions that get excluded by default:
84 static char *default_excluded_list
[] =
86 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
88 "<locore>", "<hicore>",
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)
98 static struct option long_options
[] =
100 {"line", no_argument
, 0, 'l'},
101 {"no-static", no_argument
, 0, 'a'},
102 {"ignore-non-functions", no_argument
, 0, 'D'},
106 {"annotated-source", optional_argument
, 0, 'A'},
107 {"no-annotated-source", optional_argument
, 0, 'J'},
108 {"flat-profile", optional_argument
, 0, 'p'},
109 {"no-flat-profile", optional_argument
, 0, 'P'},
110 {"graph", optional_argument
, 0, 'q'},
111 {"no-graph", optional_argument
, 0, 'Q'},
112 {"exec-counts", optional_argument
, 0, 'C'},
113 {"no-exec-counts", optional_argument
, 0, 'Z'},
114 {"function-ordering", no_argument
, 0, 'r'},
115 {"file-ordering", required_argument
, 0, 'R'},
116 {"file-info", no_argument
, 0, 'i'},
117 {"sum", no_argument
, 0, 's'},
119 /* various options to affect output: */
121 {"all-lines", no_argument
, 0, 'x'},
122 {"demangle", optional_argument
, 0, OPTION_DEMANGLE
},
123 {"no-demangle", no_argument
, 0, OPTION_NO_DEMANGLE
},
124 {"directory-path", required_argument
, 0, 'I'},
125 {"display-unused-functions", no_argument
, 0, 'z'},
126 {"min-count", required_argument
, 0, 'm'},
127 {"print-path", no_argument
, 0, 'L'},
128 {"separate-files", no_argument
, 0, 'y'},
129 {"static-call-graph", no_argument
, 0, 'c'},
130 {"table-length", required_argument
, 0, 't'},
131 {"time", required_argument
, 0, 'n'},
132 {"no-time", required_argument
, 0, 'N'},
133 {"width", required_argument
, 0, 'w'},
135 * These are for backwards-compatibility only. Their functionality
136 * is provided by the output style options already:
138 {"", required_argument
, 0, 'e'},
139 {"", required_argument
, 0, 'E'},
140 {"", required_argument
, 0, 'f'},
141 {"", required_argument
, 0, 'F'},
142 {"", required_argument
, 0, 'k'},
146 {"brief", no_argument
, 0, 'b'},
147 {"debug", optional_argument
, 0, 'd'},
148 {"help", no_argument
, 0, 'h'},
149 {"file-format", required_argument
, 0, 'O'},
150 {"traditional", no_argument
, 0, 'T'},
151 {"version", no_argument
, 0, 'v'},
152 {0, no_argument
, 0, 0}
157 usage (FILE *stream
, int status
)
159 fprintf (stream
, _("\
160 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
161 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
162 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
163 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
164 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
165 [--function-ordering] [--file-ordering]\n\
166 [--directory-path=dirs] [--display-unused-functions]\n\
167 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
168 [--no-static] [--print-path] [--separate-files]\n\
169 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
170 [--version] [--width=n] [--ignore-non-functions]\n\
171 [--demangle[=STYLE]] [--no-demangle] [@FILE]\n\
172 [image-file] [profile-file...]\n"),
174 if (REPORT_BUGS_TO
[0] && status
== 0)
175 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
181 main (int argc
, char **argv
)
185 int ch
, user_specified
= 0;
187 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
188 setlocale (LC_MESSAGES
, "");
190 #if defined (HAVE_SETLOCALE)
191 setlocale (LC_CTYPE
, "");
194 bindtextdomain (PACKAGE
, LOCALEDIR
);
195 textdomain (PACKAGE
);
199 xmalloc_set_program_name (whoami
);
201 expandargv (&argc
, &argv
);
203 while ((ch
= getopt_long (argc
, argv
,
204 "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:st:Tvw:xyzZ::",
211 ignore_static_funcs
= TRUE
;
216 sym_id_add (optarg
, INCL_ANNO
);
218 output_style
|= STYLE_ANNOTATED_SOURCE
;
219 user_specified
|= STYLE_ANNOTATED_SOURCE
;
222 print_descriptions
= FALSE
;
225 output_style
|= STYLE_CALL_GRAPH
;
226 user_specified
|= STYLE_CALL_GRAPH
;
229 ignore_direct_calls
= TRUE
;
234 sym_id_add (optarg
, INCL_EXEC
);
236 output_style
|= STYLE_EXEC_COUNTS
;
237 user_specified
|= STYLE_EXEC_COUNTS
;
242 debug_level
|= atoi (optarg
);
243 debug_level
|= ANYDEBUG
;
249 DBG (ANYDEBUG
, printf ("[main] debug-level=0x%x\n", debug_level
));
251 printf (_("%s: debugging not supported; -d ignored\n"), whoami
);
255 ignore_non_functions
= TRUE
;
258 sym_id_add (optarg
, EXCL_TIME
);
260 sym_id_add (optarg
, EXCL_GRAPH
);
263 sym_id_add (optarg
, INCL_TIME
);
265 sym_id_add (optarg
, INCL_GRAPH
);
268 sym_id_add (optarg
, EXCL_FLAT
);
271 sym_id_add (optarg
, INCL_FLAT
);
276 output_style
|= STYLE_GMON_INFO
;
277 user_specified
|= STYLE_GMON_INFO
;
280 search_list_append (&src_search_list
, optarg
);
285 sym_id_add (optarg
, EXCL_ANNO
);
286 output_style
|= STYLE_ANNOTATED_SOURCE
;
290 output_style
&= ~STYLE_ANNOTATED_SOURCE
;
292 user_specified
|= STYLE_ANNOTATED_SOURCE
;
295 sym_id_add (optarg
, EXCL_ARCS
);
298 line_granularity
= TRUE
;
304 bb_min_calls
= (unsigned long) strtoul (optarg
, (char **) NULL
, 10);
307 sym_id_add (optarg
, INCL_TIME
);
310 sym_id_add (optarg
, EXCL_TIME
);
316 file_format
= FF_AUTO
;
319 file_format
= FF_MAGIC
;
322 file_format
= FF_BSD
;
325 file_format
= FF_BSD44
;
328 file_format
= FF_PROF
;
331 fprintf (stderr
, _("%s: unknown file format %s\n"),
339 sym_id_add (optarg
, INCL_FLAT
);
341 output_style
|= STYLE_FLAT_PROFILE
;
342 user_specified
|= STYLE_FLAT_PROFILE
;
347 sym_id_add (optarg
, EXCL_FLAT
);
348 output_style
|= STYLE_FLAT_PROFILE
;
352 output_style
&= ~STYLE_FLAT_PROFILE
;
354 user_specified
|= STYLE_FLAT_PROFILE
;
359 if (strchr (optarg
, '/'))
361 sym_id_add (optarg
, INCL_ARCS
);
365 sym_id_add (optarg
, INCL_GRAPH
);
368 output_style
|= STYLE_CALL_GRAPH
;
369 user_specified
|= STYLE_CALL_GRAPH
;
372 output_style
|= STYLE_FUNCTION_ORDER
;
373 user_specified
|= STYLE_FUNCTION_ORDER
;
376 output_style
|= STYLE_FILE_ORDER
;
377 user_specified
|= STYLE_FILE_ORDER
;
378 function_mapping_file
= optarg
;
383 if (strchr (optarg
, '/'))
385 sym_id_add (optarg
, EXCL_ARCS
);
389 sym_id_add (optarg
, EXCL_GRAPH
);
391 output_style
|= STYLE_CALL_GRAPH
;
395 output_style
&= ~STYLE_CALL_GRAPH
;
397 user_specified
|= STYLE_CALL_GRAPH
;
400 output_style
|= STYLE_SUMMARY_FILE
;
401 user_specified
|= STYLE_SUMMARY_FILE
;
404 bb_table_length
= atoi (optarg
);
405 if (bb_table_length
< 0)
411 bsd_style_output
= TRUE
;
414 /* This output is intended to follow the GNU standards document. */
415 printf (_("GNU gprof %s\n"), BFD_VERSION_STRING
);
416 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
418 This program is free software. This program has absolutely no warranty.\n"));
421 output_width
= atoi (optarg
);
422 if (output_width
< 1)
428 bb_annotate_all_lines
= TRUE
;
431 create_annotation_files
= TRUE
;
434 ignore_zeros
= FALSE
;
439 sym_id_add (optarg
, EXCL_EXEC
);
440 output_style
|= STYLE_EXEC_COUNTS
;
444 output_style
&= ~STYLE_EXEC_COUNTS
;
446 user_specified
|= STYLE_ANNOTATED_SOURCE
;
448 case OPTION_DEMANGLE
:
452 enum demangling_styles style
;
454 style
= cplus_demangle_name_to_style (optarg
);
455 if (style
== unknown_demangling
)
458 _("%s: unknown demangling style `%s'\n"),
463 cplus_demangle_set_style (style
);
466 case OPTION_NO_DEMANGLE
:
474 /* Don't allow both ordering options, they modify the arc data in-place. */
475 if ((user_specified
& STYLE_FUNCTION_ORDER
)
476 && (user_specified
& STYLE_FILE_ORDER
))
479 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
484 /* --sum implies --line, otherwise we'd lose basic block counts in
486 if (output_style
& STYLE_SUMMARY_FILE
)
487 line_granularity
= 1;
489 /* append value of GPROF_PATH to source search list if set: */
490 str
= (char *) getenv ("GPROF_PATH");
492 search_list_append (&src_search_list
, str
);
495 a_out_name
= argv
[optind
++];
498 gmon_name
= argv
[optind
++];
500 /* Turn off default functions. */
501 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
503 sym_id_add (*sp
, EXCL_TIME
);
504 sym_id_add (*sp
, EXCL_GRAPH
);
505 sym_id_add (*sp
, EXCL_FLAT
);
508 /* Read symbol table from core file. */
509 core_init (a_out_name
);
511 /* If we should ignore direct function calls, we need to load to
512 core's text-space. */
513 if (ignore_direct_calls
)
514 core_get_text_space (core_bfd
);
516 /* Create symbols from core image. */
517 if (line_granularity
)
518 core_create_line_syms ();
520 core_create_function_syms ();
522 /* Translate sym specs into syms. */
525 if (file_format
== FF_PROF
)
528 _("%s: sorry, file format `prof' is not yet supported\n"),
534 /* Get information about gmon.out file(s). */
537 gmon_out_read (gmon_name
);
539 gmon_name
= argv
[optind
];
541 while (optind
++ < argc
);
544 /* If user did not specify output style, try to guess something
546 if (output_style
== 0)
548 if (gmon_input
& (INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
))
550 if (gmon_input
& INPUT_HISTOGRAM
)
551 output_style
|= STYLE_FLAT_PROFILE
;
552 if (gmon_input
& INPUT_CALL_GRAPH
)
553 output_style
|= STYLE_CALL_GRAPH
;
556 output_style
= STYLE_EXEC_COUNTS
;
558 output_style
&= ~user_specified
;
561 /* Dump a gmon.sum file if requested (before any other
563 if (output_style
& STYLE_SUMMARY_FILE
)
565 gmon_out_write (GMONSUM
);
568 if (gmon_input
& INPUT_HISTOGRAM
)
570 hist_assign_samples ();
573 if (gmon_input
& INPUT_CALL_GRAPH
)
578 /* Do some simple sanity checks. */
579 if ((output_style
& STYLE_FLAT_PROFILE
)
580 && !(gmon_input
& INPUT_HISTOGRAM
))
582 fprintf (stderr
, _("%s: gmon.out file is missing histogram\n"), whoami
);
586 if ((output_style
& STYLE_CALL_GRAPH
) && !(gmon_input
& INPUT_CALL_GRAPH
))
589 _("%s: gmon.out file is missing call-graph data\n"), whoami
);
593 /* Output whatever user whishes to see. */
594 if (cg
&& (output_style
& STYLE_CALL_GRAPH
) && bsd_style_output
)
596 /* Print the dynamic profile. */
600 if (output_style
& STYLE_FLAT_PROFILE
)
602 /* Print the flat profile. */
606 if (cg
&& (output_style
& STYLE_CALL_GRAPH
))
608 if (!bsd_style_output
)
610 /* Print the dynamic profile. */
616 if (output_style
& STYLE_EXEC_COUNTS
)
617 print_exec_counts ();
619 if (output_style
& STYLE_ANNOTATED_SOURCE
)
620 print_annotated_source ();
622 if (output_style
& STYLE_FUNCTION_ORDER
)
623 cg_print_function_ordering ();
625 if (output_style
& STYLE_FILE_ORDER
)
626 cg_print_file_ordering ();