2 * Copyright (c) 1983, 1998, 2001 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 #include "libiberty.h"
22 #include "basic_blocks.h"
23 #include "call_graph.h"
35 const char *function_mapping_file
;
36 const char *a_out_name
= A_OUTNAME
;
40 * Default options values:
44 int output_width
= 80;
45 bool bsd_style_output
= FALSE
;
47 bool discard_underscores
= TRUE
;
48 bool ignore_direct_calls
= FALSE
;
49 bool ignore_static_funcs
= FALSE
;
50 bool ignore_zeros
= TRUE
;
51 bool line_granularity
= FALSE
;
52 bool print_descriptions
= TRUE
;
53 bool print_path
= FALSE
;
54 bool ignore_non_functions
= FALSE
;
55 File_Format file_format
= FF_AUTO
;
57 bool first_output
= TRUE
;
60 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
61 All rights reserved.\n";
63 static char *gmon_name
= GMONNAME
; /* profile filename */
68 * Functions that get excluded by default:
70 static char *default_excluded_list
[] =
72 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
74 "<locore>", "<hicore>",
78 /* Codes used for the long options with no short synonyms. 150 isn't
79 special; it's just an arbitrary non-ASCII char value. */
81 #define OPTION_DEMANGLE (150)
82 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
84 static struct option long_options
[] =
86 {"line", no_argument
, 0, 'l'},
87 {"no-static", no_argument
, 0, 'a'},
88 {"ignore-non-functions", no_argument
, 0, 'D'},
92 {"annotated-source", optional_argument
, 0, 'A'},
93 {"no-annotated-source", optional_argument
, 0, 'J'},
94 {"flat-profile", optional_argument
, 0, 'p'},
95 {"no-flat-profile", optional_argument
, 0, 'P'},
96 {"graph", optional_argument
, 0, 'q'},
97 {"no-graph", optional_argument
, 0, 'Q'},
98 {"exec-counts", optional_argument
, 0, 'C'},
99 {"no-exec-counts", optional_argument
, 0, 'Z'},
100 {"function-ordering", no_argument
, 0, 'r'},
101 {"file-ordering", required_argument
, 0, 'R'},
102 {"file-info", no_argument
, 0, 'i'},
103 {"sum", no_argument
, 0, 's'},
105 /* various options to affect output: */
107 {"all-lines", no_argument
, 0, 'x'},
108 {"demangle", optional_argument
, 0, OPTION_DEMANGLE
},
109 {"no-demangle", no_argument
, 0, OPTION_NO_DEMANGLE
},
110 {"directory-path", required_argument
, 0, 'I'},
111 {"display-unused-functions", no_argument
, 0, 'z'},
112 {"min-count", required_argument
, 0, 'm'},
113 {"print-path", no_argument
, 0, 'L'},
114 {"separate-files", no_argument
, 0, 'y'},
115 {"static-call-graph", no_argument
, 0, 'c'},
116 {"table-length", required_argument
, 0, 't'},
117 {"time", required_argument
, 0, 'n'},
118 {"no-time", required_argument
, 0, 'N'},
119 {"width", required_argument
, 0, 'w'},
121 * These are for backwards-compatibility only. Their functionality
122 * is provided by the output style options already:
124 {"", required_argument
, 0, 'e'},
125 {"", required_argument
, 0, 'E'},
126 {"", required_argument
, 0, 'f'},
127 {"", required_argument
, 0, 'F'},
128 {"", required_argument
, 0, 'k'},
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}
143 DEFUN (usage
, (stream
, status
), FILE * stream AND
int status
)
145 fprintf (stream
, _("\
146 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
147 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
148 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
149 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
150 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
151 [--function-ordering] [--file-ordering]\n\
152 [--directory-path=dirs] [--display-unused-functions]\n\
153 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
154 [--no-static] [--print-path] [--separate-files]\n\
155 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
156 [--version] [--width=n] [--ignore-non-functions]\n\
157 [--demangle[=STYLE]] [--no-demangle]\n\
158 [image-file] [profile-file...]\n"),
161 fprintf (stream
, _("Report bugs to %s\n"), REPORT_BUGS_TO
);
167 DEFUN (main
, (argc
, argv
), int argc AND
char **argv
)
171 int ch
, user_specified
= 0;
173 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
174 setlocale (LC_MESSAGES
, "");
176 bindtextdomain (PACKAGE
, LOCALEDIR
);
177 textdomain (PACKAGE
);
180 xmalloc_set_program_name (whoami
);
182 while ((ch
= getopt_long (argc
, argv
,
183 "aA::bBcCd::De:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
190 ignore_static_funcs
= TRUE
;
195 sym_id_add (optarg
, INCL_ANNO
);
197 output_style
|= STYLE_ANNOTATED_SOURCE
;
198 user_specified
|= STYLE_ANNOTATED_SOURCE
;
201 print_descriptions
= FALSE
;
204 output_style
|= STYLE_CALL_GRAPH
;
205 user_specified
|= STYLE_CALL_GRAPH
;
208 ignore_direct_calls
= TRUE
;
213 sym_id_add (optarg
, INCL_EXEC
);
215 output_style
|= STYLE_EXEC_COUNTS
;
216 user_specified
|= STYLE_EXEC_COUNTS
;
221 debug_level
|= atoi (optarg
);
222 debug_level
|= ANYDEBUG
;
228 DBG (ANYDEBUG
, printf ("[main] debug-level=0x%x\n", debug_level
));
230 printf (_("%s: debugging not supported; -d ignored\n"), whoami
);
234 ignore_non_functions
= TRUE
;
237 sym_id_add (optarg
, EXCL_TIME
);
239 sym_id_add (optarg
, EXCL_GRAPH
);
242 sym_id_add (optarg
, INCL_TIME
);
244 sym_id_add (optarg
, INCL_GRAPH
);
247 sym_id_add (optarg
, EXCL_FLAT
);
250 sym_id_add (optarg
, INCL_FLAT
);
255 output_style
|= STYLE_GMON_INFO
;
256 user_specified
|= STYLE_GMON_INFO
;
259 search_list_append (&src_search_list
, optarg
);
264 sym_id_add (optarg
, EXCL_ANNO
);
265 output_style
|= STYLE_ANNOTATED_SOURCE
;
269 output_style
&= ~STYLE_ANNOTATED_SOURCE
;
271 user_specified
|= STYLE_ANNOTATED_SOURCE
;
274 sym_id_add (optarg
, EXCL_ARCS
);
277 line_granularity
= TRUE
;
283 bb_min_calls
= (unsigned long) strtoul (optarg
, (char **) NULL
, 10);
286 sym_id_add (optarg
, INCL_TIME
);
289 sym_id_add (optarg
, EXCL_TIME
);
295 file_format
= FF_AUTO
;
298 file_format
= FF_MAGIC
;
301 file_format
= FF_BSD
;
304 file_format
= FF_BSD44
;
307 file_format
= FF_PROF
;
310 fprintf (stderr
, _("%s: unknown file format %s\n"),
318 sym_id_add (optarg
, INCL_FLAT
);
320 output_style
|= STYLE_FLAT_PROFILE
;
321 user_specified
|= STYLE_FLAT_PROFILE
;
326 sym_id_add (optarg
, EXCL_FLAT
);
327 output_style
|= STYLE_FLAT_PROFILE
;
331 output_style
&= ~STYLE_FLAT_PROFILE
;
333 user_specified
|= STYLE_FLAT_PROFILE
;
338 if (strchr (optarg
, '/'))
340 sym_id_add (optarg
, INCL_ARCS
);
344 sym_id_add (optarg
, INCL_GRAPH
);
347 output_style
|= STYLE_CALL_GRAPH
;
348 user_specified
|= STYLE_CALL_GRAPH
;
351 output_style
|= STYLE_FUNCTION_ORDER
;
352 user_specified
|= STYLE_FUNCTION_ORDER
;
355 output_style
|= STYLE_FILE_ORDER
;
356 user_specified
|= STYLE_FILE_ORDER
;
357 function_mapping_file
= optarg
;
362 if (strchr (optarg
, '/'))
364 sym_id_add (optarg
, EXCL_ARCS
);
368 sym_id_add (optarg
, EXCL_GRAPH
);
370 output_style
|= STYLE_CALL_GRAPH
;
374 output_style
&= ~STYLE_CALL_GRAPH
;
376 user_specified
|= STYLE_CALL_GRAPH
;
379 output_style
|= STYLE_SUMMARY_FILE
;
380 user_specified
|= STYLE_SUMMARY_FILE
;
383 bb_table_length
= atoi (optarg
);
384 if (bb_table_length
< 0)
390 bsd_style_output
= TRUE
;
393 /* This output is intended to follow the GNU standards document. */
394 printf (_("GNU gprof %s\n"), VERSION
);
395 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
397 This program is free software. This program has absolutely no warranty.\n"));
400 output_width
= atoi (optarg
);
401 if (output_width
< 1)
407 bb_annotate_all_lines
= TRUE
;
410 create_annotation_files
= TRUE
;
413 ignore_zeros
= FALSE
;
418 sym_id_add (optarg
, EXCL_EXEC
);
419 output_style
|= STYLE_EXEC_COUNTS
;
423 output_style
&= ~STYLE_EXEC_COUNTS
;
425 user_specified
|= STYLE_ANNOTATED_SOURCE
;
427 case OPTION_DEMANGLE
:
431 enum demangling_styles style
;
433 style
= cplus_demangle_name_to_style (optarg
);
434 if (style
== unknown_demangling
)
437 _("%s: unknown demangling style `%s'\n"),
442 cplus_demangle_set_style (style
);
445 case OPTION_NO_DEMANGLE
:
453 /* Don't allow both ordering options, they modify the arc data in-place. */
454 if ((user_specified
& STYLE_FUNCTION_ORDER
)
455 && (user_specified
& STYLE_FILE_ORDER
))
458 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
463 /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
464 if (output_style
& STYLE_SUMMARY_FILE
)
466 line_granularity
= 1;
469 /* append value of GPROF_PATH to source search list if set: */
470 str
= (char *) getenv ("GPROF_PATH");
473 search_list_append (&src_search_list
, str
);
478 a_out_name
= argv
[optind
++];
482 gmon_name
= argv
[optind
++];
486 * Turn off default functions:
488 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
490 sym_id_add (*sp
, EXCL_TIME
);
491 sym_id_add (*sp
, EXCL_GRAPH
);
493 sym_id_add (*sp
, EXCL_FLAT
);
498 * For line-by-line profiling, also want to keep those
499 * functions off the flat profile:
501 if (line_granularity
)
503 for (sp
= &default_excluded_list
[0]; *sp
; sp
++)
505 sym_id_add (*sp
, EXCL_FLAT
);
510 * Read symbol table from core file:
512 core_init (a_out_name
);
515 * If we should ignore direct function calls, we need to load
516 * to core's text-space:
518 if (ignore_direct_calls
)
520 core_get_text_space (core_bfd
);
524 * Create symbols from core image:
526 if (line_granularity
)
528 core_create_line_syms (core_bfd
);
532 core_create_function_syms (core_bfd
);
536 * Translate sym specs into syms:
540 if (file_format
== FF_PROF
)
542 #ifdef PROF_SUPPORT_IMPLEMENTED
544 * Get information about mon.out file(s):
548 mon_out_read (gmon_name
);
551 gmon_name
= argv
[optind
];
554 while (optind
++ < argc
);
557 _("%s: sorry, file format `prof' is not yet supported\n"),
565 * Get information about gmon.out file(s):
569 gmon_out_read (gmon_name
);
572 gmon_name
= argv
[optind
];
575 while (optind
++ < argc
);
579 * If user did not specify output style, try to guess something
582 if (output_style
== 0)
584 if (gmon_input
& (INPUT_HISTOGRAM
| INPUT_CALL_GRAPH
))
586 output_style
= STYLE_FLAT_PROFILE
| STYLE_CALL_GRAPH
;
590 output_style
= STYLE_EXEC_COUNTS
;
592 output_style
&= ~user_specified
;
596 * Dump a gmon.sum file if requested (before any other processing!):
598 if (output_style
& STYLE_SUMMARY_FILE
)
600 gmon_out_write (GMONSUM
);
603 if (gmon_input
& INPUT_HISTOGRAM
)
605 hist_assign_samples ();
608 if (gmon_input
& INPUT_CALL_GRAPH
)
613 /* do some simple sanity checks: */
615 if ((output_style
& STYLE_FLAT_PROFILE
)
616 && !(gmon_input
& INPUT_HISTOGRAM
))
618 fprintf (stderr
, _("%s: gmon.out file is missing histogram\n"), whoami
);
622 if ((output_style
& STYLE_CALL_GRAPH
) && !(gmon_input
& INPUT_CALL_GRAPH
))
625 _("%s: gmon.out file is missing call-graph data\n"), whoami
);
629 /* output whatever user whishes to see: */
631 if (cg
&& (output_style
& STYLE_CALL_GRAPH
) && bsd_style_output
)
633 cg_print (cg
); /* print the dynamic profile */
636 if (output_style
& STYLE_FLAT_PROFILE
)
638 hist_print (); /* print the flat profile */
641 if (cg
&& (output_style
& STYLE_CALL_GRAPH
))
643 if (!bsd_style_output
)
645 cg_print (cg
); /* print the dynamic profile */
650 if (output_style
& STYLE_EXEC_COUNTS
)
652 print_exec_counts ();
655 if (output_style
& STYLE_ANNOTATED_SOURCE
)
657 print_annotated_source ();
659 if (output_style
& STYLE_FUNCTION_ORDER
)
661 cg_print_function_ordering ();
663 if (output_style
& STYLE_FILE_ORDER
)
665 cg_print_file_ordering ();