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 "search_list.h"
25 #include "basic_blocks.h"
26 #include "call_graph.h"
37 const char *function_mapping_file;
38 const char *a_out_name = A_OUTNAME;
42 * Default options values:
46 int output_width = 80;
47 boolean bsd_style_output = false;
48 boolean demangle = true;
49 boolean discard_underscores = true;
50 boolean ignore_direct_calls = false;
51 boolean ignore_static_funcs = false;
52 boolean ignore_zeros = true;
53 boolean line_granularity = false;
54 boolean print_descriptions = true;
55 boolean print_path = false;
56 boolean ignore_non_functions = false;
57 File_Format file_format = FF_AUTO;
59 boolean first_output = true;
62 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
63 All rights reserved.\n";
65 static char *gmon_name = GMONNAME; /* profile filename */
70 * Functions that get excluded by default:
72 static char *default_excluded_list[] =
74 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
76 "<locore>", "<hicore>",
80 /* Codes used for the long options with no short synonyms. 150 isn't
81 special; it's just an arbitrary non-ASCII char value. */
83 #define OPTION_DEMANGLE (150)
84 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
86 static struct option long_options[] =
88 {"line", no_argument, 0, 'l'},
89 {"no-static", no_argument, 0, 'a'},
90 {"ignore-non-functions", no_argument, 0, 'D'},
94 {"annotated-source", optional_argument, 0, 'A'},
95 {"no-annotated-source", optional_argument, 0, 'J'},
96 {"flat-profile", optional_argument, 0, 'p'},
97 {"no-flat-profile", optional_argument, 0, 'P'},
98 {"graph", optional_argument, 0, 'q'},
99 {"no-graph", optional_argument, 0, 'Q'},
100 {"exec-counts", optional_argument, 0, 'C'},
101 {"no-exec-counts", optional_argument, 0, 'Z'},
102 {"function-ordering", no_argument, 0, 'r'},
103 {"file-ordering", required_argument, 0, 'R'},
104 {"file-info", no_argument, 0, 'i'},
105 {"sum", no_argument, 0, 's'},
107 /* various options to affect output: */
109 {"all-lines", no_argument, 0, 'x'},
110 {"demangle", optional_argument, 0, OPTION_DEMANGLE},
111 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
112 {"directory-path", required_argument, 0, 'I'},
113 {"display-unused-functions", no_argument, 0, 'z'},
114 {"min-count", required_argument, 0, 'm'},
115 {"print-path", no_argument, 0, 'L'},
116 {"separate-files", no_argument, 0, 'y'},
117 {"static-call-graph", no_argument, 0, 'c'},
118 {"table-length", required_argument, 0, 't'},
119 {"time", required_argument, 0, 'n'},
120 {"no-time", required_argument, 0, 'N'},
121 {"width", required_argument, 0, 'w'},
123 * These are for backwards-compatibility only. Their functionality
124 * is provided by the output style options already:
126 {"", required_argument, 0, 'e'},
127 {"", required_argument, 0, 'E'},
128 {"", required_argument, 0, 'f'},
129 {"", required_argument, 0, 'F'},
130 {"", required_argument, 0, 'k'},
134 {"brief", no_argument, 0, 'b'},
135 {"debug", optional_argument, 0, 'd'},
136 {"help", no_argument, 0, 'h'},
137 {"file-format", required_argument, 0, 'O'},
138 {"traditional", no_argument, 0, 'T'},
139 {"version", no_argument, 0, 'v'},
140 {0, no_argument, 0, 0}
145 DEFUN (usage, (stream, status), FILE * stream AND int status)
147 fprintf (stream, _("\
148 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
149 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
150 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
151 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
152 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
153 [--function-ordering] [--file-ordering]\n\
154 [--directory-path=dirs] [--display-unused-functions]\n\
155 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
156 [--no-static] [--print-path] [--separate-files]\n\
157 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
158 [--version] [--width=n] [--ignore-non-functions]\n\
159 [--demangle[=STYLE]] [--no-demangle]\n\
160 [image-file] [profile-file...]\n"),
163 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
169 DEFUN (main, (argc, argv), int argc AND char **argv)
173 int ch, user_specified = 0;
175 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
176 setlocale (LC_MESSAGES, "");
178 #if defined (HAVE_SETLOCALE)
179 setlocale (LC_CTYPE, "");
181 bindtextdomain (PACKAGE, LOCALEDIR);
182 textdomain (PACKAGE);
185 xmalloc_set_program_name (whoami);
187 while ((ch = getopt_long (argc, argv,
188 "aA::bBcCd::De:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
195 ignore_static_funcs = true;
200 sym_id_add (optarg, INCL_ANNO);
202 output_style |= STYLE_ANNOTATED_SOURCE;
203 user_specified |= STYLE_ANNOTATED_SOURCE;
206 print_descriptions = false;
209 output_style |= STYLE_CALL_GRAPH;
210 user_specified |= STYLE_CALL_GRAPH;
213 ignore_direct_calls = true;
218 sym_id_add (optarg, INCL_EXEC);
220 output_style |= STYLE_EXEC_COUNTS;
221 user_specified |= STYLE_EXEC_COUNTS;
226 debug_level |= atoi (optarg);
227 debug_level |= ANYDEBUG;
233 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
235 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
239 ignore_non_functions = true;
242 sym_id_add (optarg, EXCL_TIME);
244 sym_id_add (optarg, EXCL_GRAPH);
247 sym_id_add (optarg, INCL_TIME);
249 sym_id_add (optarg, INCL_GRAPH);
252 sym_id_add (optarg, EXCL_FLAT);
255 sym_id_add (optarg, INCL_FLAT);
260 output_style |= STYLE_GMON_INFO;
261 user_specified |= STYLE_GMON_INFO;
264 search_list_append (&src_search_list, optarg);
269 sym_id_add (optarg, EXCL_ANNO);
270 output_style |= STYLE_ANNOTATED_SOURCE;
274 output_style &= ~STYLE_ANNOTATED_SOURCE;
276 user_specified |= STYLE_ANNOTATED_SOURCE;
279 sym_id_add (optarg, EXCL_ARCS);
282 line_granularity = true;
288 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
291 sym_id_add (optarg, INCL_TIME);
294 sym_id_add (optarg, EXCL_TIME);
300 file_format = FF_AUTO;
303 file_format = FF_MAGIC;
306 file_format = FF_BSD;
309 file_format = FF_BSD44;
312 file_format = FF_PROF;
315 fprintf (stderr, _("%s: unknown file format %s\n"),
323 sym_id_add (optarg, INCL_FLAT);
325 output_style |= STYLE_FLAT_PROFILE;
326 user_specified |= STYLE_FLAT_PROFILE;
331 sym_id_add (optarg, EXCL_FLAT);
332 output_style |= STYLE_FLAT_PROFILE;
336 output_style &= ~STYLE_FLAT_PROFILE;
338 user_specified |= STYLE_FLAT_PROFILE;
343 if (strchr (optarg, '/'))
345 sym_id_add (optarg, INCL_ARCS);
349 sym_id_add (optarg, INCL_GRAPH);
352 output_style |= STYLE_CALL_GRAPH;
353 user_specified |= STYLE_CALL_GRAPH;
356 output_style |= STYLE_FUNCTION_ORDER;
357 user_specified |= STYLE_FUNCTION_ORDER;
360 output_style |= STYLE_FILE_ORDER;
361 user_specified |= STYLE_FILE_ORDER;
362 function_mapping_file = optarg;
367 if (strchr (optarg, '/'))
369 sym_id_add (optarg, EXCL_ARCS);
373 sym_id_add (optarg, EXCL_GRAPH);
375 output_style |= STYLE_CALL_GRAPH;
379 output_style &= ~STYLE_CALL_GRAPH;
381 user_specified |= STYLE_CALL_GRAPH;
384 output_style |= STYLE_SUMMARY_FILE;
385 user_specified |= STYLE_SUMMARY_FILE;
388 bb_table_length = atoi (optarg);
389 if (bb_table_length < 0)
395 bsd_style_output = true;
398 /* This output is intended to follow the GNU standards document. */
399 printf (_("GNU gprof %s\n"), VERSION);
400 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
402 This program is free software. This program has absolutely no warranty.\n"));
405 output_width = atoi (optarg);
406 if (output_width < 1)
412 bb_annotate_all_lines = true;
415 create_annotation_files = true;
418 ignore_zeros = false;
423 sym_id_add (optarg, EXCL_EXEC);
424 output_style |= STYLE_EXEC_COUNTS;
428 output_style &= ~STYLE_EXEC_COUNTS;
430 user_specified |= STYLE_ANNOTATED_SOURCE;
432 case OPTION_DEMANGLE:
436 enum demangling_styles style;
438 style = cplus_demangle_name_to_style (optarg);
439 if (style == unknown_demangling)
442 _("%s: unknown demangling style `%s'\n"),
447 cplus_demangle_set_style (style);
450 case OPTION_NO_DEMANGLE:
458 /* Don't allow both ordering options, they modify the arc data in-place. */
459 if ((user_specified & STYLE_FUNCTION_ORDER)
460 && (user_specified & STYLE_FILE_ORDER))
463 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
468 /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
469 if (output_style & STYLE_SUMMARY_FILE)
471 line_granularity = 1;
474 /* append value of GPROF_PATH to source search list if set: */
475 str = (char *) getenv ("GPROF_PATH");
478 search_list_append (&src_search_list, str);
483 a_out_name = argv[optind++];
487 gmon_name = argv[optind++];
491 * Turn off default functions:
493 for (sp = &default_excluded_list[0]; *sp; sp++)
495 sym_id_add (*sp, EXCL_TIME);
496 sym_id_add (*sp, EXCL_GRAPH);
498 sym_id_add (*sp, EXCL_FLAT);
503 * For line-by-line profiling, also want to keep those
504 * functions off the flat profile:
506 if (line_granularity)
508 for (sp = &default_excluded_list[0]; *sp; sp++)
510 sym_id_add (*sp, EXCL_FLAT);
515 * Read symbol table from core file:
517 core_init (a_out_name);
520 * If we should ignore direct function calls, we need to load
521 * to core's text-space:
523 if (ignore_direct_calls)
525 core_get_text_space (core_bfd);
529 * Create symbols from core image:
531 if (line_granularity)
533 core_create_line_syms (core_bfd);
537 core_create_function_syms (core_bfd);
541 * Translate sym specs into syms:
545 if (file_format == FF_PROF)
547 #ifdef PROF_SUPPORT_IMPLEMENTED
549 * Get information about mon.out file(s):
553 mon_out_read (gmon_name);
556 gmon_name = argv[optind];
559 while (optind++ < argc);
562 _("%s: sorry, file format `prof' is not yet supported\n"),
570 * Get information about gmon.out file(s):
574 gmon_out_read (gmon_name);
577 gmon_name = argv[optind];
580 while (optind++ < argc);
584 * If user did not specify output style, try to guess something
587 if (output_style == 0)
589 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
591 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
595 output_style = STYLE_EXEC_COUNTS;
597 output_style &= ~user_specified;
601 * Dump a gmon.sum file if requested (before any other processing!):
603 if (output_style & STYLE_SUMMARY_FILE)
605 gmon_out_write (GMONSUM);
608 if (gmon_input & INPUT_HISTOGRAM)
610 hist_assign_samples ();
613 if (gmon_input & INPUT_CALL_GRAPH)
618 /* do some simple sanity checks: */
620 if ((output_style & STYLE_FLAT_PROFILE)
621 && !(gmon_input & INPUT_HISTOGRAM))
623 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
627 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
630 _("%s: gmon.out file is missing call-graph data\n"), whoami);
634 /* output whatever user whishes to see: */
636 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
638 cg_print (cg); /* print the dynamic profile */
641 if (output_style & STYLE_FLAT_PROFILE)
643 hist_print (); /* print the flat profile */
646 if (cg && (output_style & STYLE_CALL_GRAPH))
648 if (!bsd_style_output)
650 cg_print (cg); /* print the dynamic profile */
655 if (output_style & STYLE_EXEC_COUNTS)
657 print_exec_counts ();
660 if (output_style & STYLE_ANNOTATED_SOURCE)
662 print_annotated_source ();
664 if (output_style & STYLE_FUNCTION_ORDER)
666 cg_print_function_ordering ();
668 if (output_style & STYLE_FILE_ORDER)
670 cg_print_file_ordering ();