2 * Copyright (c) 1983, 1998 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"
34 const char *function_mapping_file;
35 const char *a_out_name = A_OUTNAME;
39 * Default options values:
43 int output_width = 80;
44 bool bsd_style_output = FALSE;
46 bool discard_underscores = TRUE;
47 bool ignore_direct_calls = FALSE;
48 bool ignore_static_funcs = FALSE;
49 bool ignore_zeros = TRUE;
50 bool line_granularity = FALSE;
51 bool print_descriptions = TRUE;
52 bool print_path = FALSE;
53 bool ignore_non_functions = FALSE;
54 File_Format file_format = FF_AUTO;
56 bool first_output = TRUE;
59 N_("@(#) Copyright (c) 1983 Regents of the University of California.\n\
60 All rights reserved.\n");
62 static char *gmon_name = GMONNAME; /* profile filename */
67 * Functions that get excluded by default:
69 static char *default_excluded_list[] =
71 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
73 "<locore>", "<hicore>",
77 /* Codes used for the long options with no short synonyms. 150 isn't
78 special; it's just an arbitrary non-ASCII char value. */
80 #define OPTION_DEMANGLE (150)
81 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
83 static struct option long_options[] =
85 {"line", no_argument, 0, 'l'},
86 {"no-static", no_argument, 0, 'a'},
87 {"ignore-non-functions", no_argument, 0, 'D'},
91 {"annotated-source", optional_argument, 0, 'A'},
92 {"no-annotated-source", optional_argument, 0, 'J'},
93 {"flat-profile", optional_argument, 0, 'p'},
94 {"no-flat-profile", optional_argument, 0, 'P'},
95 {"graph", optional_argument, 0, 'q'},
96 {"no-graph", optional_argument, 0, 'Q'},
97 {"exec-counts", optional_argument, 0, 'C'},
98 {"no-exec-counts", optional_argument, 0, 'Z'},
99 {"function-ordering", no_argument, 0, 'r'},
100 {"file-ordering", required_argument, 0, 'R'},
101 {"file-info", no_argument, 0, 'i'},
102 {"sum", no_argument, 0, 's'},
104 /* various options to affect output: */
106 {"all-lines", no_argument, 0, 'x'},
107 {"demangle", no_argument, 0, OPTION_DEMANGLE},
108 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
109 {"directory-path", required_argument, 0, 'I'},
110 {"display-unused-functions", no_argument, 0, 'z'},
111 {"min-count", required_argument, 0, 'm'},
112 {"print-path", no_argument, 0, 'L'},
113 {"separate-files", no_argument, 0, 'y'},
114 {"static-call-graph", no_argument, 0, 'c'},
115 {"table-length", required_argument, 0, 't'},
116 {"time", required_argument, 0, 'n'},
117 {"no-time", required_argument, 0, 'N'},
118 {"width", required_argument, 0, 'w'},
120 * These are for backwards-compatibility only. Their functionality
121 * is provided by the output style options already:
123 {"", required_argument, 0, 'e'},
124 {"", required_argument, 0, 'E'},
125 {"", required_argument, 0, 'f'},
126 {"", required_argument, 0, 'F'},
127 {"", required_argument, 0, 'k'},
131 {"brief", no_argument, 0, 'b'},
132 {"debug", optional_argument, 0, 'd'},
133 {"help", no_argument, 0, 'h'},
134 {"file-format", required_argument, 0, 'O'},
135 {"traditional", no_argument, 0, 'T'},
136 {"version", no_argument, 0, 'v'},
137 {0, no_argument, 0, 0}
142 DEFUN (usage, (stream, status), FILE * stream AND int status)
144 fprintf (stream, _("\
145 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
146 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
147 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
148 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
149 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
150 [--function-ordering] [--file-ordering]\n\
151 [--directory-path=dirs] [--display-unused-functions]\n\
152 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
153 [--no-static] [--print-path] [--separate-files]\n\
154 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
155 [--version] [--width=n] [--ignore-non-functions]\n\
156 [--demangle] [--no-demangle]\n\
157 [image-file] [profile-file...]\n"),
160 fprintf (stream, _("Report bugs to bug-gnu-utils@gnu.org\n"));
166 DEFUN (main, (argc, argv), int argc AND char **argv)
170 int ch, user_specified = 0;
172 setlocale (LC_MESSAGES, "");
173 bindtextdomain (PACKAGE, LOCALEDIR);
174 textdomain (PACKAGE);
177 xmalloc_set_program_name (whoami);
179 while ((ch = getopt_long (argc, argv,
180 "aA::bBcCdD::e:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
187 ignore_static_funcs = TRUE;
192 sym_id_add (optarg, INCL_ANNO);
194 output_style |= STYLE_ANNOTATED_SOURCE;
195 user_specified |= STYLE_ANNOTATED_SOURCE;
198 print_descriptions = FALSE;
201 output_style |= STYLE_CALL_GRAPH;
202 user_specified |= STYLE_CALL_GRAPH;
205 ignore_direct_calls = TRUE;
210 sym_id_add (optarg, INCL_EXEC);
212 output_style |= STYLE_EXEC_COUNTS;
213 user_specified |= STYLE_EXEC_COUNTS;
218 debug_level |= atoi (optarg);
219 debug_level |= ANYDEBUG;
225 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
227 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
231 ignore_non_functions = TRUE;
234 sym_id_add (optarg, EXCL_TIME);
236 sym_id_add (optarg, EXCL_GRAPH);
239 sym_id_add (optarg, INCL_TIME);
241 sym_id_add (optarg, INCL_GRAPH);
244 sym_id_add (optarg, EXCL_FLAT);
247 sym_id_add (optarg, INCL_FLAT);
252 output_style |= STYLE_GMON_INFO;
253 user_specified |= STYLE_GMON_INFO;
256 search_list_append (&src_search_list, optarg);
261 sym_id_add (optarg, EXCL_ANNO);
262 output_style |= STYLE_ANNOTATED_SOURCE;
266 output_style &= ~STYLE_ANNOTATED_SOURCE;
268 user_specified |= STYLE_ANNOTATED_SOURCE;
271 sym_id_add (optarg, EXCL_ARCS);
274 line_granularity = TRUE;
280 bb_min_calls = atoi (optarg);
283 sym_id_add (optarg, INCL_TIME);
286 sym_id_add (optarg, EXCL_TIME);
292 file_format = FF_AUTO;
295 file_format = FF_MAGIC;
298 file_format = FF_BSD;
301 file_format = FF_PROF;
304 fprintf (stderr, _("%s: unknown file format %s\n"),
312 sym_id_add (optarg, INCL_FLAT);
314 output_style |= STYLE_FLAT_PROFILE;
315 user_specified |= STYLE_FLAT_PROFILE;
320 sym_id_add (optarg, EXCL_FLAT);
321 output_style |= STYLE_FLAT_PROFILE;
325 output_style &= ~STYLE_FLAT_PROFILE;
327 user_specified |= STYLE_FLAT_PROFILE;
332 if (strchr (optarg, '/'))
334 sym_id_add (optarg, INCL_ARCS);
338 sym_id_add (optarg, INCL_GRAPH);
341 output_style |= STYLE_CALL_GRAPH;
342 user_specified |= STYLE_CALL_GRAPH;
345 output_style |= STYLE_FUNCTION_ORDER;
346 user_specified |= STYLE_FUNCTION_ORDER;
349 output_style |= STYLE_FILE_ORDER;
350 user_specified |= STYLE_FILE_ORDER;
351 function_mapping_file = optarg;
356 if (strchr (optarg, '/'))
358 sym_id_add (optarg, EXCL_ARCS);
362 sym_id_add (optarg, EXCL_GRAPH);
364 output_style |= STYLE_CALL_GRAPH;
368 output_style &= ~STYLE_CALL_GRAPH;
370 user_specified |= STYLE_CALL_GRAPH;
373 output_style |= STYLE_SUMMARY_FILE;
374 user_specified |= STYLE_SUMMARY_FILE;
377 bb_table_length = atoi (optarg);
378 if (bb_table_length < 0)
384 bsd_style_output = TRUE;
387 /* This output is intended to follow the GNU standards document. */
388 printf (_("GNU gprof %s\n"), VERSION);
389 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
391 This program is free software. This program has absolutely no warranty.\n"));
394 output_width = atoi (optarg);
395 if (output_width < 1)
401 bb_annotate_all_lines = TRUE;
404 create_annotation_files = TRUE;
407 ignore_zeros = FALSE;
412 sym_id_add (optarg, EXCL_EXEC);
413 output_style |= STYLE_EXEC_COUNTS;
417 output_style &= ~STYLE_EXEC_COUNTS;
419 user_specified |= STYLE_ANNOTATED_SOURCE;
421 case OPTION_DEMANGLE:
424 case OPTION_NO_DEMANGLE:
432 /* Don't allow both ordering options, they modify the arc data in-place. */
433 if ((user_specified & STYLE_FUNCTION_ORDER)
434 && (user_specified & STYLE_FILE_ORDER))
437 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
442 /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
443 if (output_style & STYLE_SUMMARY_FILE)
445 line_granularity = 1;
448 /* append value of GPROF_PATH to source search list if set: */
449 str = (char *) getenv ("GPROF_PATH");
452 search_list_append (&src_search_list, str);
457 a_out_name = argv[optind++];
461 gmon_name = argv[optind++];
465 * Turn off default functions:
467 for (sp = &default_excluded_list[0]; *sp; sp++)
469 sym_id_add (*sp, EXCL_TIME);
470 sym_id_add (*sp, EXCL_GRAPH);
472 sym_id_add (*sp, EXCL_FLAT);
477 * For line-by-line profiling, also want to keep those
478 * functions off the flat profile:
480 if (line_granularity)
482 for (sp = &default_excluded_list[0]; *sp; sp++)
484 sym_id_add (*sp, EXCL_FLAT);
489 * Read symbol table from core file:
491 core_init (a_out_name);
494 * If we should ignore direct function calls, we need to load
495 * to core's text-space:
497 if (ignore_direct_calls)
499 core_get_text_space (core_bfd);
503 * Create symbols from core image:
505 if (line_granularity)
507 core_create_line_syms (core_bfd);
511 core_create_function_syms (core_bfd);
515 * Translate sym specs into syms:
519 if (file_format == FF_PROF)
521 #ifdef PROF_SUPPORT_IMPLEMENTED
523 * Get information about mon.out file(s):
527 mon_out_read (gmon_name);
530 gmon_name = argv[optind];
533 while (optind++ < argc);
536 _("%s: sorry, file format `prof' is not yet supported\n"),
544 * Get information about gmon.out file(s):
548 gmon_out_read (gmon_name);
551 gmon_name = argv[optind];
554 while (optind++ < argc);
558 * If user did not specify output style, try to guess something
561 if (output_style == 0)
563 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
565 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
569 output_style = STYLE_EXEC_COUNTS;
571 output_style &= ~user_specified;
575 * Dump a gmon.sum file if requested (before any other processing!):
577 if (output_style & STYLE_SUMMARY_FILE)
579 gmon_out_write (GMONSUM);
582 if (gmon_input & INPUT_HISTOGRAM)
584 hist_assign_samples ();
587 if (gmon_input & INPUT_CALL_GRAPH)
592 /* do some simple sanity checks: */
594 if ((output_style & STYLE_FLAT_PROFILE)
595 && !(gmon_input & INPUT_HISTOGRAM))
597 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
601 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
604 _("%s: gmon.out file is missing call-graph data\n"), whoami);
608 /* output whatever user whishes to see: */
610 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
612 cg_print (cg); /* print the dynamic profile */
615 if (output_style & STYLE_FLAT_PROFILE)
617 hist_print (); /* print the flat profile */
620 if (cg && (output_style & STYLE_CALL_GRAPH))
622 if (!bsd_style_output)
624 cg_print (cg); /* print the dynamic profile */
629 if (output_style & STYLE_EXEC_COUNTS)
631 print_exec_counts ();
634 if (output_style & STYLE_ANNOTATED_SOURCE)
636 print_annotated_source ();
638 if (output_style & STYLE_FUNCTION_ORDER)
640 cg_print_function_ordering ();
642 if (output_style & STYLE_FILE_ORDER)
644 cg_print_file_ordering ();