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 (FILE *, int) ATTRIBUTE_NORETURN;
50 const char *function_mapping_file;
51 const char *a_out_name = A_OUTNAME;
55 * Default options values:
59 int output_width = 80;
60 bfd_boolean bsd_style_output = FALSE;
61 bfd_boolean demangle = TRUE;
62 bfd_boolean discard_underscores = 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",
87 "<locore>", "<hicore>",
91 /* Codes used for the long options with no short synonyms. 150 isn't
92 special; it's just an arbitrary non-ASCII char value. */
94 #define OPTION_DEMANGLE (150)
95 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
97 static struct option long_options[] =
99 {"line", no_argument, 0, 'l'},
100 {"no-static", no_argument, 0, 'a'},
101 {"ignore-non-functions", no_argument, 0, 'D'},
105 {"annotated-source", optional_argument, 0, 'A'},
106 {"no-annotated-source", optional_argument, 0, 'J'},
107 {"flat-profile", optional_argument, 0, 'p'},
108 {"no-flat-profile", optional_argument, 0, 'P'},
109 {"graph", optional_argument, 0, 'q'},
110 {"no-graph", optional_argument, 0, 'Q'},
111 {"exec-counts", optional_argument, 0, 'C'},
112 {"no-exec-counts", optional_argument, 0, 'Z'},
113 {"function-ordering", no_argument, 0, 'r'},
114 {"file-ordering", required_argument, 0, 'R'},
115 {"file-info", no_argument, 0, 'i'},
116 {"sum", no_argument, 0, 's'},
118 /* various options to affect output: */
120 {"all-lines", no_argument, 0, 'x'},
121 {"demangle", optional_argument, 0, OPTION_DEMANGLE},
122 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
123 {"directory-path", required_argument, 0, 'I'},
124 {"display-unused-functions", no_argument, 0, 'z'},
125 {"min-count", required_argument, 0, 'm'},
126 {"print-path", no_argument, 0, 'L'},
127 {"separate-files", no_argument, 0, 'y'},
128 {"static-call-graph", no_argument, 0, 'c'},
129 {"table-length", required_argument, 0, 't'},
130 {"time", required_argument, 0, 'n'},
131 {"no-time", required_argument, 0, 'N'},
132 {"width", required_argument, 0, 'w'},
134 * These are for backwards-compatibility only. Their functionality
135 * is provided by the output style options already:
137 {"", required_argument, 0, 'e'},
138 {"", required_argument, 0, 'E'},
139 {"", required_argument, 0, 'f'},
140 {"", required_argument, 0, 'F'},
141 {"", required_argument, 0, 'k'},
145 {"brief", no_argument, 0, 'b'},
146 {"debug", optional_argument, 0, 'd'},
147 {"help", no_argument, 0, 'h'},
148 {"file-format", required_argument, 0, 'O'},
149 {"traditional", no_argument, 0, 'T'},
150 {"version", no_argument, 0, 'v'},
151 {0, no_argument, 0, 0}
156 usage (FILE *stream, int status)
158 fprintf (stream, _("\
159 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
160 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
161 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
162 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
163 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
164 [--function-ordering] [--file-ordering]\n\
165 [--directory-path=dirs] [--display-unused-functions]\n\
166 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
167 [--no-static] [--print-path] [--separate-files]\n\
168 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
169 [--version] [--width=n] [--ignore-non-functions]\n\
170 [--demangle[=STYLE]] [--no-demangle]\n\
171 [image-file] [profile-file...]\n"),
174 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
180 main (int argc, char **argv)
184 int ch, user_specified = 0;
186 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
187 setlocale (LC_MESSAGES, "");
189 #if defined (HAVE_SETLOCALE)
190 setlocale (LC_CTYPE, "");
193 bindtextdomain (PACKAGE, LOCALEDIR);
194 textdomain (PACKAGE);
198 xmalloc_set_program_name (whoami);
200 while ((ch = getopt_long (argc, argv,
201 "aA::bBcCd::De:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
208 ignore_static_funcs = TRUE;
213 sym_id_add (optarg, INCL_ANNO);
215 output_style |= STYLE_ANNOTATED_SOURCE;
216 user_specified |= STYLE_ANNOTATED_SOURCE;
219 print_descriptions = FALSE;
222 output_style |= STYLE_CALL_GRAPH;
223 user_specified |= STYLE_CALL_GRAPH;
226 ignore_direct_calls = TRUE;
231 sym_id_add (optarg, INCL_EXEC);
233 output_style |= STYLE_EXEC_COUNTS;
234 user_specified |= STYLE_EXEC_COUNTS;
239 debug_level |= atoi (optarg);
240 debug_level |= ANYDEBUG;
246 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
248 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
252 ignore_non_functions = TRUE;
255 sym_id_add (optarg, EXCL_TIME);
257 sym_id_add (optarg, EXCL_GRAPH);
260 sym_id_add (optarg, INCL_TIME);
262 sym_id_add (optarg, INCL_GRAPH);
265 sym_id_add (optarg, EXCL_FLAT);
268 sym_id_add (optarg, INCL_FLAT);
273 output_style |= STYLE_GMON_INFO;
274 user_specified |= STYLE_GMON_INFO;
277 search_list_append (&src_search_list, optarg);
282 sym_id_add (optarg, EXCL_ANNO);
283 output_style |= STYLE_ANNOTATED_SOURCE;
287 output_style &= ~STYLE_ANNOTATED_SOURCE;
289 user_specified |= STYLE_ANNOTATED_SOURCE;
292 sym_id_add (optarg, EXCL_ARCS);
295 line_granularity = TRUE;
301 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
304 sym_id_add (optarg, INCL_TIME);
307 sym_id_add (optarg, EXCL_TIME);
313 file_format = FF_AUTO;
316 file_format = FF_MAGIC;
319 file_format = FF_BSD;
322 file_format = FF_BSD44;
325 file_format = FF_PROF;
328 fprintf (stderr, _("%s: unknown file format %s\n"),
336 sym_id_add (optarg, INCL_FLAT);
338 output_style |= STYLE_FLAT_PROFILE;
339 user_specified |= STYLE_FLAT_PROFILE;
344 sym_id_add (optarg, EXCL_FLAT);
345 output_style |= STYLE_FLAT_PROFILE;
349 output_style &= ~STYLE_FLAT_PROFILE;
351 user_specified |= STYLE_FLAT_PROFILE;
356 if (strchr (optarg, '/'))
358 sym_id_add (optarg, INCL_ARCS);
362 sym_id_add (optarg, INCL_GRAPH);
365 output_style |= STYLE_CALL_GRAPH;
366 user_specified |= STYLE_CALL_GRAPH;
369 output_style |= STYLE_FUNCTION_ORDER;
370 user_specified |= STYLE_FUNCTION_ORDER;
373 output_style |= STYLE_FILE_ORDER;
374 user_specified |= STYLE_FILE_ORDER;
375 function_mapping_file = optarg;
380 if (strchr (optarg, '/'))
382 sym_id_add (optarg, EXCL_ARCS);
386 sym_id_add (optarg, EXCL_GRAPH);
388 output_style |= STYLE_CALL_GRAPH;
392 output_style &= ~STYLE_CALL_GRAPH;
394 user_specified |= STYLE_CALL_GRAPH;
397 output_style |= STYLE_SUMMARY_FILE;
398 user_specified |= STYLE_SUMMARY_FILE;
401 bb_table_length = atoi (optarg);
402 if (bb_table_length < 0)
408 bsd_style_output = TRUE;
411 /* This output is intended to follow the GNU standards document. */
412 printf (_("GNU gprof %s\n"), VERSION);
413 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
415 This program is free software. This program has absolutely no warranty.\n"));
418 output_width = atoi (optarg);
419 if (output_width < 1)
425 bb_annotate_all_lines = TRUE;
428 create_annotation_files = TRUE;
431 ignore_zeros = FALSE;
436 sym_id_add (optarg, EXCL_EXEC);
437 output_style |= STYLE_EXEC_COUNTS;
441 output_style &= ~STYLE_EXEC_COUNTS;
443 user_specified |= STYLE_ANNOTATED_SOURCE;
445 case OPTION_DEMANGLE:
449 enum demangling_styles style;
451 style = cplus_demangle_name_to_style (optarg);
452 if (style == unknown_demangling)
455 _("%s: unknown demangling style `%s'\n"),
460 cplus_demangle_set_style (style);
463 case OPTION_NO_DEMANGLE:
471 /* Don't allow both ordering options, they modify the arc data in-place. */
472 if ((user_specified & STYLE_FUNCTION_ORDER)
473 && (user_specified & STYLE_FILE_ORDER))
476 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
481 /* --sum implies --line, otherwise we'd lose basic block counts in
483 if (output_style & STYLE_SUMMARY_FILE)
484 line_granularity = 1;
486 /* append value of GPROF_PATH to source search list if set: */
487 str = (char *) getenv ("GPROF_PATH");
489 search_list_append (&src_search_list, str);
492 a_out_name = argv[optind++];
495 gmon_name = argv[optind++];
497 /* Turn off default functions. */
498 for (sp = &default_excluded_list[0]; *sp; sp++)
500 sym_id_add (*sp, EXCL_TIME);
501 sym_id_add (*sp, EXCL_GRAPH);
502 sym_id_add (*sp, EXCL_FLAT);
505 /* Read symbol table from core file. */
506 core_init (a_out_name);
508 /* If we should ignore direct function calls, we need to load to
509 core's text-space. */
510 if (ignore_direct_calls)
511 core_get_text_space (core_bfd);
513 /* Create symbols from core image. */
514 if (line_granularity)
515 core_create_line_syms ();
517 core_create_function_syms ();
519 /* Translate sym specs into syms. */
522 if (file_format == FF_PROF)
525 _("%s: sorry, file format `prof' is not yet supported\n"),
531 /* Get information about gmon.out file(s). */
534 gmon_out_read (gmon_name);
536 gmon_name = argv[optind];
538 while (optind++ < argc);
541 /* If user did not specify output style, try to guess something
543 if (output_style == 0)
545 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
546 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
548 output_style = STYLE_EXEC_COUNTS;
550 output_style &= ~user_specified;
553 /* Dump a gmon.sum file if requested (before any other
555 if (output_style & STYLE_SUMMARY_FILE)
557 gmon_out_write (GMONSUM);
560 if (gmon_input & INPUT_HISTOGRAM)
562 hist_assign_samples ();
565 if (gmon_input & INPUT_CALL_GRAPH)
570 /* Do some simple sanity checks. */
571 if ((output_style & STYLE_FLAT_PROFILE)
572 && !(gmon_input & INPUT_HISTOGRAM))
574 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
578 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
581 _("%s: gmon.out file is missing call-graph data\n"), whoami);
585 /* Output whatever user whishes to see. */
586 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
588 /* Print the dynamic profile. */
592 if (output_style & STYLE_FLAT_PROFILE)
594 /* Print the flat profile. */
598 if (cg && (output_style & STYLE_CALL_GRAPH))
600 if (!bsd_style_output)
602 /* Print the dynamic profile. */
608 if (output_style & STYLE_EXEC_COUNTS)
609 print_exec_counts ();
611 if (output_style & STYLE_ANNOTATED_SOURCE)
612 print_annotated_source ();
614 if (output_style & STYLE_FUNCTION_ORDER)
615 cg_print_function_ordering ();
617 if (output_style & STYLE_FILE_ORDER)
618 cg_print_file_ordering ();