2 * Copyright (c) 1983 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"
33 #define VERSION "2.7.1"
36 const char *function_mapping_file;
37 const char *a_out_name = A_OUTNAME;
41 * Default options values:
45 int output_width = 80;
46 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", "__mcleanup",
73 "<locore>", "<hicore>",
77 static struct option long_options[] =
79 {"line", no_argument, 0, 'l'},
80 {"no-static", no_argument, 0, 'a'},
81 {"ignore-non-functions", no_argument, 0, 'D'},
85 {"annotated-source", optional_argument, 0, 'A'},
86 {"no-annotated-source", optional_argument, 0, 'J'},
87 {"flat-profile", optional_argument, 0, 'p'},
88 {"no-flat-profile", optional_argument, 0, 'P'},
89 {"graph", optional_argument, 0, 'q'},
90 {"no-graph", optional_argument, 0, 'Q'},
91 {"exec-counts", optional_argument, 0, 'C'},
92 {"no-exec-counts", optional_argument, 0, 'Z'},
93 {"function-ordering", no_argument, 0, 'r'},
94 {"file-ordering", required_argument, 0, 'R'},
95 {"file-info", no_argument, 0, 'i'},
96 {"sum", no_argument, 0, 's'},
98 /* various options to affect output: */
100 {"all-lines", no_argument, 0, 'x'},
101 {"directory-path", required_argument, 0, 'I'},
102 {"display-unused-functions", no_argument, 0, 'z'},
103 {"min-count", required_argument, 0, 'm'},
104 {"print-path", no_argument, 0, 'L'},
105 {"separate-files", no_argument, 0, 'y'},
106 {"static-call-graph", no_argument, 0, 'c'},
107 {"table-length", required_argument, 0, 't'},
108 {"time", required_argument, 0, 'n'},
109 {"no-time", required_argument, 0, 'N'},
110 {"width", required_argument, 0, 'w'},
112 * These are for backwards-compatibility only. Their functionality
113 * is provided by the output style options already:
115 {"", required_argument, 0, 'e'},
116 {"", required_argument, 0, 'E'},
117 {"", required_argument, 0, 'f'},
118 {"", required_argument, 0, 'F'},
119 {"", required_argument, 0, 'k'},
123 {"brief", no_argument, 0, 'b'},
124 {"debug", optional_argument, 0, 'd'},
125 {"help", no_argument, 0, 'h'},
126 {"file-format", required_argument, 0, 'O'},
127 {"traditional", no_argument, 0, 'T'},
128 {"version", no_argument, 0, 'v'},
129 {0, no_argument, 0, 0}
134 DEFUN (usage, (stream, status), FILE * stream AND int status)
137 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
138 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
139 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
140 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
141 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
142 [--function-ordering] [--file-ordering]\n\
143 [--directory-path=dirs] [--display-unused-functions]\n\
144 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
145 [--no-static] [--print-path] [--separate-files]\n\
146 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
147 [--version] [--width=n] [--ignore-non-functions]\n\
148 [image-file] [profile-file...]\n",
151 fprintf (stream, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
157 DEFUN (main, (argc, argv), int argc AND char **argv)
161 int ch, user_specified = 0;
164 xmalloc_set_program_name (whoami);
166 while ((ch = getopt_long (argc, argv,
167 "aA::bBcCdD::e:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
174 ignore_static_funcs = TRUE;
179 sym_id_add (optarg, INCL_ANNO);
181 output_style |= STYLE_ANNOTATED_SOURCE;
182 user_specified |= STYLE_ANNOTATED_SOURCE;
185 print_descriptions = FALSE;
188 output_style |= STYLE_CALL_GRAPH;
189 user_specified |= STYLE_CALL_GRAPH;
192 ignore_direct_calls = TRUE;
197 sym_id_add (optarg, INCL_EXEC);
199 output_style |= STYLE_EXEC_COUNTS;
200 user_specified |= STYLE_EXEC_COUNTS;
205 debug_level |= atoi (optarg);
206 debug_level |= ANYDEBUG;
212 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
214 printf ("%s: debugging not supported; -d ignored\n", whoami);
218 ignore_non_functions = TRUE;
221 sym_id_add (optarg, EXCL_TIME);
223 sym_id_add (optarg, EXCL_GRAPH);
226 sym_id_add (optarg, INCL_TIME);
228 sym_id_add (optarg, INCL_GRAPH);
231 sym_id_add (optarg, EXCL_FLAT);
234 sym_id_add (optarg, INCL_FLAT);
239 output_style |= STYLE_GMON_INFO;
240 user_specified |= STYLE_GMON_INFO;
243 search_list_append (&src_search_list, optarg);
248 sym_id_add (optarg, EXCL_ANNO);
249 output_style |= STYLE_ANNOTATED_SOURCE;
253 output_style &= ~STYLE_ANNOTATED_SOURCE;
255 user_specified |= STYLE_ANNOTATED_SOURCE;
258 sym_id_add (optarg, EXCL_ARCS);
261 line_granularity = TRUE;
267 bb_min_calls = atoi (optarg);
270 sym_id_add (optarg, INCL_TIME);
273 sym_id_add (optarg, EXCL_TIME);
279 file_format = FF_AUTO;
282 file_format = FF_MAGIC;
285 file_format = FF_BSD;
288 file_format = FF_PROF;
291 fprintf (stderr, "%s: unknown file format %s\n",
299 sym_id_add (optarg, INCL_FLAT);
301 output_style |= STYLE_FLAT_PROFILE;
302 user_specified |= STYLE_FLAT_PROFILE;
307 sym_id_add (optarg, EXCL_FLAT);
308 output_style |= STYLE_FLAT_PROFILE;
312 output_style &= ~STYLE_FLAT_PROFILE;
314 user_specified |= STYLE_FLAT_PROFILE;
319 if (strchr (optarg, '/'))
321 sym_id_add (optarg, INCL_ARCS);
325 sym_id_add (optarg, INCL_GRAPH);
328 output_style |= STYLE_CALL_GRAPH;
329 user_specified |= STYLE_CALL_GRAPH;
332 output_style |= STYLE_FUNCTION_ORDER;
333 user_specified |= STYLE_FUNCTION_ORDER;
336 output_style |= STYLE_FILE_ORDER;
337 user_specified |= STYLE_FILE_ORDER;
338 function_mapping_file = optarg;
343 if (strchr (optarg, '/'))
345 sym_id_add (optarg, EXCL_ARCS);
349 sym_id_add (optarg, EXCL_GRAPH);
351 output_style |= STYLE_CALL_GRAPH;
355 output_style &= ~STYLE_CALL_GRAPH;
357 user_specified |= STYLE_CALL_GRAPH;
360 output_style |= STYLE_SUMMARY_FILE;
361 user_specified |= STYLE_SUMMARY_FILE;
364 bb_table_length = atoi (optarg);
365 if (bb_table_length < 0)
371 bsd_style_output = TRUE;
374 /* This output is intended to follow the GNU standards document. */
375 printf ("GNU gprof %s\n", VERSION);
376 printf ("Copyright 1996 Free Software Foundation, Inc.\n");
378 This program is free software; you may redistribute it under the terms of\n\
379 the GNU General Public License. This program has absolutely no warranty.\n");
382 output_width = atoi (optarg);
383 if (output_width < 1)
389 bb_annotate_all_lines = TRUE;
392 create_annotation_files = TRUE;
395 ignore_zeros = FALSE;
400 sym_id_add (optarg, EXCL_EXEC);
401 output_style |= STYLE_EXEC_COUNTS;
405 output_style &= ~STYLE_EXEC_COUNTS;
407 user_specified |= STYLE_ANNOTATED_SOURCE;
414 /* Don't allow both ordering options, they modify the arc data in-place. */
415 if ((user_specified & STYLE_FUNCTION_ORDER)
416 && (user_specified & STYLE_FILE_ORDER))
419 %s: Only one of --function-ordering and --file-ordering may be specified.\n",
424 /* append value of GPROF_PATH to source search list if set: */
425 str = (char *) getenv ("GPROF_PATH");
428 search_list_append (&src_search_list, str);
433 a_out_name = argv[optind++];
437 gmon_name = argv[optind++];
441 * Turn off default functions:
443 for (sp = &default_excluded_list[0]; *sp; sp++)
445 sym_id_add (*sp, EXCL_TIME);
446 sym_id_add (*sp, EXCL_GRAPH);
448 sym_id_add (*sp, EXCL_FLAT);
453 * For line-by-line profiling, also want to keep those
454 * functions off the flat profile:
456 if (line_granularity)
458 for (sp = &default_excluded_list[0]; *sp; sp++)
460 sym_id_add (*sp, EXCL_FLAT);
465 * Read symbol table from core file:
467 core_init (a_out_name);
470 * If we should ignore direct function calls, we need to load
471 * to core's text-space:
473 if (ignore_direct_calls)
475 core_get_text_space (core_bfd);
479 * Create symbols from core image:
481 if (line_granularity)
483 core_create_line_syms (core_bfd);
487 core_create_function_syms (core_bfd);
491 * Translate sym specs into syms:
495 if (file_format == FF_PROF)
497 #ifdef PROF_SUPPORT_IMPLEMENTED
499 * Get information about mon.out file(s):
503 mon_out_read (gmon_name);
506 gmon_name = argv[optind];
509 while (optind++ < argc);
512 "%s: sorry, file format `prof' is not yet supported\n",
520 * Get information about gmon.out file(s):
524 gmon_out_read (gmon_name);
527 gmon_name = argv[optind];
530 while (optind++ < argc);
534 * If user did not specify output style, try to guess something
537 if (output_style == 0)
539 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
541 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
545 output_style = STYLE_EXEC_COUNTS;
547 output_style &= ~user_specified;
551 * Dump a gmon.sum file if requested (before any other processing!):
553 if (output_style & STYLE_SUMMARY_FILE)
555 gmon_out_write (GMONSUM);
558 if (gmon_input & INPUT_HISTOGRAM)
560 hist_assign_samples ();
563 if (gmon_input & INPUT_CALL_GRAPH)
568 /* do some simple sanity checks: */
570 if ((output_style & STYLE_FLAT_PROFILE)
571 && !(gmon_input & INPUT_HISTOGRAM))
573 fprintf (stderr, "%s: gmon.out file is missing histogram\n", whoami);
577 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
580 "%s: gmon.out file is missing call-graph data\n", whoami);
584 /* output whatever user whishes to see: */
586 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
588 cg_print (cg); /* print the dynamic profile */
591 if (output_style & STYLE_FLAT_PROFILE)
593 hist_print (); /* print the flat profile */
596 if (cg && (output_style & STYLE_CALL_GRAPH))
598 if (!bsd_style_output)
600 cg_print (cg); /* print the dynamic profile */
605 if (output_style & STYLE_EXEC_COUNTS)
607 print_exec_counts ();
610 if (output_style & STYLE_ANNOTATED_SOURCE)
612 print_annotated_source ();
614 if (output_style & STYLE_FUNCTION_ORDER)
616 cg_print_function_ordering ();
618 if (output_style & STYLE_FILE_ORDER)
620 cg_print_file_ordering ();