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.
19 #include "libiberty.h"
21 #include "search_list.h"
24 #include "basic_blocks.h"
25 #include "call_graph.h"
36 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
37 int main PARAMS ((int, char **));
40 const char *function_mapping_file;
41 const char *a_out_name = A_OUTNAME;
45 * Default options values:
49 int output_width = 80;
50 boolean bsd_style_output = false;
51 boolean demangle = true;
52 boolean discard_underscores = true;
53 boolean ignore_direct_calls = false;
54 boolean ignore_static_funcs = false;
55 boolean ignore_zeros = true;
56 boolean line_granularity = false;
57 boolean print_descriptions = true;
58 boolean print_path = false;
59 boolean ignore_non_functions = false;
60 File_Format file_format = FF_AUTO;
62 boolean first_output = true;
65 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
66 All rights reserved.\n";
68 static char *gmon_name = GMONNAME; /* profile filename */
73 * Functions that get excluded by default:
75 static char *default_excluded_list[] =
77 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
79 "<locore>", "<hicore>",
83 /* Codes used for the long options with no short synonyms. 150 isn't
84 special; it's just an arbitrary non-ASCII char value. */
86 #define OPTION_DEMANGLE (150)
87 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
89 static struct option long_options[] =
91 {"line", no_argument, 0, 'l'},
92 {"no-static", no_argument, 0, 'a'},
93 {"ignore-non-functions", no_argument, 0, 'D'},
97 {"annotated-source", optional_argument, 0, 'A'},
98 {"no-annotated-source", optional_argument, 0, 'J'},
99 {"flat-profile", optional_argument, 0, 'p'},
100 {"no-flat-profile", optional_argument, 0, 'P'},
101 {"graph", optional_argument, 0, 'q'},
102 {"no-graph", optional_argument, 0, 'Q'},
103 {"exec-counts", optional_argument, 0, 'C'},
104 {"no-exec-counts", optional_argument, 0, 'Z'},
105 {"function-ordering", no_argument, 0, 'r'},
106 {"file-ordering", required_argument, 0, 'R'},
107 {"file-info", no_argument, 0, 'i'},
108 {"sum", no_argument, 0, 's'},
110 /* various options to affect output: */
112 {"all-lines", no_argument, 0, 'x'},
113 {"demangle", optional_argument, 0, OPTION_DEMANGLE},
114 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
115 {"directory-path", required_argument, 0, 'I'},
116 {"display-unused-functions", no_argument, 0, 'z'},
117 {"min-count", required_argument, 0, 'm'},
118 {"print-path", no_argument, 0, 'L'},
119 {"separate-files", no_argument, 0, 'y'},
120 {"static-call-graph", no_argument, 0, 'c'},
121 {"table-length", required_argument, 0, 't'},
122 {"time", required_argument, 0, 'n'},
123 {"no-time", required_argument, 0, 'N'},
124 {"width", required_argument, 0, 'w'},
126 * These are for backwards-compatibility only. Their functionality
127 * is provided by the output style options already:
129 {"", required_argument, 0, 'e'},
130 {"", required_argument, 0, 'E'},
131 {"", required_argument, 0, 'f'},
132 {"", required_argument, 0, 'F'},
133 {"", required_argument, 0, 'k'},
137 {"brief", no_argument, 0, 'b'},
138 {"debug", optional_argument, 0, 'd'},
139 {"help", no_argument, 0, 'h'},
140 {"file-format", required_argument, 0, 'O'},
141 {"traditional", no_argument, 0, 'T'},
142 {"version", no_argument, 0, 'v'},
143 {0, no_argument, 0, 0}
148 usage (stream, status)
152 fprintf (stream, _("\
153 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
154 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
155 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
156 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
157 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
158 [--function-ordering] [--file-ordering]\n\
159 [--directory-path=dirs] [--display-unused-functions]\n\
160 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
161 [--no-static] [--print-path] [--separate-files]\n\
162 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
163 [--version] [--width=n] [--ignore-non-functions]\n\
164 [--demangle[=STYLE]] [--no-demangle]\n\
165 [image-file] [profile-file...]\n"),
168 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
180 int ch, user_specified = 0;
182 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
183 setlocale (LC_MESSAGES, "");
185 #if defined (HAVE_SETLOCALE)
186 setlocale (LC_CTYPE, "");
188 bindtextdomain (PACKAGE, LOCALEDIR);
189 textdomain (PACKAGE);
192 xmalloc_set_program_name (whoami);
194 while ((ch = getopt_long (argc, argv,
195 "aA::bBcCd::De:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
202 ignore_static_funcs = true;
207 sym_id_add (optarg, INCL_ANNO);
209 output_style |= STYLE_ANNOTATED_SOURCE;
210 user_specified |= STYLE_ANNOTATED_SOURCE;
213 print_descriptions = false;
216 output_style |= STYLE_CALL_GRAPH;
217 user_specified |= STYLE_CALL_GRAPH;
220 ignore_direct_calls = true;
225 sym_id_add (optarg, INCL_EXEC);
227 output_style |= STYLE_EXEC_COUNTS;
228 user_specified |= STYLE_EXEC_COUNTS;
233 debug_level |= atoi (optarg);
234 debug_level |= ANYDEBUG;
240 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
242 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
246 ignore_non_functions = true;
249 sym_id_add (optarg, EXCL_TIME);
251 sym_id_add (optarg, EXCL_GRAPH);
254 sym_id_add (optarg, INCL_TIME);
256 sym_id_add (optarg, INCL_GRAPH);
259 sym_id_add (optarg, EXCL_FLAT);
262 sym_id_add (optarg, INCL_FLAT);
267 output_style |= STYLE_GMON_INFO;
268 user_specified |= STYLE_GMON_INFO;
271 search_list_append (&src_search_list, optarg);
276 sym_id_add (optarg, EXCL_ANNO);
277 output_style |= STYLE_ANNOTATED_SOURCE;
281 output_style &= ~STYLE_ANNOTATED_SOURCE;
283 user_specified |= STYLE_ANNOTATED_SOURCE;
286 sym_id_add (optarg, EXCL_ARCS);
289 line_granularity = true;
295 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
298 sym_id_add (optarg, INCL_TIME);
301 sym_id_add (optarg, EXCL_TIME);
307 file_format = FF_AUTO;
310 file_format = FF_MAGIC;
313 file_format = FF_BSD;
316 file_format = FF_BSD44;
319 file_format = FF_PROF;
322 fprintf (stderr, _("%s: unknown file format %s\n"),
330 sym_id_add (optarg, INCL_FLAT);
332 output_style |= STYLE_FLAT_PROFILE;
333 user_specified |= STYLE_FLAT_PROFILE;
338 sym_id_add (optarg, EXCL_FLAT);
339 output_style |= STYLE_FLAT_PROFILE;
343 output_style &= ~STYLE_FLAT_PROFILE;
345 user_specified |= STYLE_FLAT_PROFILE;
350 if (strchr (optarg, '/'))
352 sym_id_add (optarg, INCL_ARCS);
356 sym_id_add (optarg, INCL_GRAPH);
359 output_style |= STYLE_CALL_GRAPH;
360 user_specified |= STYLE_CALL_GRAPH;
363 output_style |= STYLE_FUNCTION_ORDER;
364 user_specified |= STYLE_FUNCTION_ORDER;
367 output_style |= STYLE_FILE_ORDER;
368 user_specified |= STYLE_FILE_ORDER;
369 function_mapping_file = optarg;
374 if (strchr (optarg, '/'))
376 sym_id_add (optarg, EXCL_ARCS);
380 sym_id_add (optarg, EXCL_GRAPH);
382 output_style |= STYLE_CALL_GRAPH;
386 output_style &= ~STYLE_CALL_GRAPH;
388 user_specified |= STYLE_CALL_GRAPH;
391 output_style |= STYLE_SUMMARY_FILE;
392 user_specified |= STYLE_SUMMARY_FILE;
395 bb_table_length = atoi (optarg);
396 if (bb_table_length < 0)
402 bsd_style_output = true;
405 /* This output is intended to follow the GNU standards document. */
406 printf (_("GNU gprof %s\n"), VERSION);
407 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
409 This program is free software. This program has absolutely no warranty.\n"));
412 output_width = atoi (optarg);
413 if (output_width < 1)
419 bb_annotate_all_lines = true;
422 create_annotation_files = true;
425 ignore_zeros = false;
430 sym_id_add (optarg, EXCL_EXEC);
431 output_style |= STYLE_EXEC_COUNTS;
435 output_style &= ~STYLE_EXEC_COUNTS;
437 user_specified |= STYLE_ANNOTATED_SOURCE;
439 case OPTION_DEMANGLE:
443 enum demangling_styles style;
445 style = cplus_demangle_name_to_style (optarg);
446 if (style == unknown_demangling)
449 _("%s: unknown demangling style `%s'\n"),
454 cplus_demangle_set_style (style);
457 case OPTION_NO_DEMANGLE:
465 /* Don't allow both ordering options, they modify the arc data in-place. */
466 if ((user_specified & STYLE_FUNCTION_ORDER)
467 && (user_specified & STYLE_FILE_ORDER))
470 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
475 /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
476 if (output_style & STYLE_SUMMARY_FILE)
478 line_granularity = 1;
481 /* append value of GPROF_PATH to source search list if set: */
482 str = (char *) getenv ("GPROF_PATH");
485 search_list_append (&src_search_list, str);
490 a_out_name = argv[optind++];
494 gmon_name = argv[optind++];
498 * Turn off default functions:
500 for (sp = &default_excluded_list[0]; *sp; sp++)
502 sym_id_add (*sp, EXCL_TIME);
503 sym_id_add (*sp, EXCL_GRAPH);
505 sym_id_add (*sp, EXCL_FLAT);
510 * For line-by-line profiling, also want to keep those
511 * functions off the flat profile:
513 if (line_granularity)
515 for (sp = &default_excluded_list[0]; *sp; sp++)
517 sym_id_add (*sp, EXCL_FLAT);
522 * Read symbol table from core file:
524 core_init (a_out_name);
527 * If we should ignore direct function calls, we need to load
528 * to core's text-space:
530 if (ignore_direct_calls)
532 core_get_text_space (core_bfd);
536 * Create symbols from core image:
538 if (line_granularity)
540 core_create_line_syms (core_bfd);
544 core_create_function_syms (core_bfd);
548 * Translate sym specs into syms:
552 if (file_format == FF_PROF)
554 #ifdef PROF_SUPPORT_IMPLEMENTED
556 * Get information about mon.out file(s):
560 mon_out_read (gmon_name);
563 gmon_name = argv[optind];
566 while (optind++ < argc);
569 _("%s: sorry, file format `prof' is not yet supported\n"),
577 * Get information about gmon.out file(s):
581 gmon_out_read (gmon_name);
584 gmon_name = argv[optind];
587 while (optind++ < argc);
591 * If user did not specify output style, try to guess something
594 if (output_style == 0)
596 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
598 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
602 output_style = STYLE_EXEC_COUNTS;
604 output_style &= ~user_specified;
608 * Dump a gmon.sum file if requested (before any other processing!):
610 if (output_style & STYLE_SUMMARY_FILE)
612 gmon_out_write (GMONSUM);
615 if (gmon_input & INPUT_HISTOGRAM)
617 hist_assign_samples ();
620 if (gmon_input & INPUT_CALL_GRAPH)
625 /* do some simple sanity checks: */
627 if ((output_style & STYLE_FLAT_PROFILE)
628 && !(gmon_input & INPUT_HISTOGRAM))
630 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
634 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
637 _("%s: gmon.out file is missing call-graph data\n"), whoami);
641 /* output whatever user whishes to see: */
643 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
645 cg_print (cg); /* print the dynamic profile */
648 if (output_style & STYLE_FLAT_PROFILE)
650 hist_print (); /* print the flat profile */
653 if (cg && (output_style & STYLE_CALL_GRAPH))
655 if (!bsd_style_output)
657 cg_print (cg); /* print the dynamic profile */
662 if (output_style & STYLE_EXEC_COUNTS)
664 print_exec_counts ();
667 if (output_style & STYLE_ANNOTATED_SOURCE)
669 print_annotated_source ();
671 if (output_style & STYLE_FUNCTION_ORDER)
673 cg_print_function_ordering ();
675 if (output_style & STYLE_FILE_ORDER)
677 cg_print_file_ordering ();