Fix compile time failure
[binutils.git] / gprof / gprof.c
blob75923d757c352c836315b26e5129b530791bf9df
1 /*
2 * Copyright (c) 1983, 1998, 2001, 2002 Regents of the University of California.
3 * All rights reserved.
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"
20 #include "gprof.h"
21 #include "search_list.h"
22 #include "source.h"
23 #include "symtab.h"
24 #include "basic_blocks.h"
25 #include "call_graph.h"
26 #include "cg_arcs.h"
27 #include "cg_print.h"
28 #include "corefile.h"
29 #include "gmon_io.h"
30 #include "hertz.h"
31 #include "hist.h"
32 #include "sym_ids.h"
33 #include "demangle.h"
34 #include "getopt.h"
36 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
37 int main PARAMS ((int, char **));
39 const char *whoami;
40 const char *function_mapping_file;
41 const char *a_out_name = A_OUTNAME;
42 long hz = HZ_WRONG;
45 * Default options values:
47 int debug_level = 0;
48 int output_style = 0;
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;
64 char copyright[] =
65 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
66 All rights reserved.\n";
68 static char *gmon_name = GMONNAME; /* profile filename */
70 bfd *abfd;
73 * Functions that get excluded by default:
75 static char *default_excluded_list[] =
77 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
78 "__mcleanup",
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'},
95 /* output styles: */
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'},
135 /* miscellaneous: */
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}
147 static void
148 usage (stream, status)
149 FILE *stream;
150 int 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"),
166 whoami);
167 if (status == 0)
168 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
169 done (status);
174 main (argc, argv)
175 int argc;
176 char **argv;
178 char **sp, *str;
179 Sym **cg = 0;
180 int ch, user_specified = 0;
182 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
183 setlocale (LC_MESSAGES, "");
184 #endif
185 #if defined (HAVE_SETLOCALE)
186 setlocale (LC_CTYPE, "");
187 #endif
188 bindtextdomain (PACKAGE, LOCALEDIR);
189 textdomain (PACKAGE);
191 whoami = argv[0];
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::",
196 long_options, 0))
197 != EOF)
199 switch (ch)
201 case 'a':
202 ignore_static_funcs = true;
203 break;
204 case 'A':
205 if (optarg)
207 sym_id_add (optarg, INCL_ANNO);
209 output_style |= STYLE_ANNOTATED_SOURCE;
210 user_specified |= STYLE_ANNOTATED_SOURCE;
211 break;
212 case 'b':
213 print_descriptions = false;
214 break;
215 case 'B':
216 output_style |= STYLE_CALL_GRAPH;
217 user_specified |= STYLE_CALL_GRAPH;
218 break;
219 case 'c':
220 ignore_direct_calls = true;
221 break;
222 case 'C':
223 if (optarg)
225 sym_id_add (optarg, INCL_EXEC);
227 output_style |= STYLE_EXEC_COUNTS;
228 user_specified |= STYLE_EXEC_COUNTS;
229 break;
230 case 'd':
231 if (optarg)
233 debug_level |= atoi (optarg);
234 debug_level |= ANYDEBUG;
236 else
238 debug_level = ~0;
240 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
241 #ifndef DEBUG
242 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
243 #endif /* DEBUG */
244 break;
245 case 'D':
246 ignore_non_functions = true;
247 break;
248 case 'E':
249 sym_id_add (optarg, EXCL_TIME);
250 case 'e':
251 sym_id_add (optarg, EXCL_GRAPH);
252 break;
253 case 'F':
254 sym_id_add (optarg, INCL_TIME);
255 case 'f':
256 sym_id_add (optarg, INCL_GRAPH);
257 break;
258 case 'g':
259 sym_id_add (optarg, EXCL_FLAT);
260 break;
261 case 'G':
262 sym_id_add (optarg, INCL_FLAT);
263 break;
264 case 'h':
265 usage (stdout, 0);
266 case 'i':
267 output_style |= STYLE_GMON_INFO;
268 user_specified |= STYLE_GMON_INFO;
269 break;
270 case 'I':
271 search_list_append (&src_search_list, optarg);
272 break;
273 case 'J':
274 if (optarg)
276 sym_id_add (optarg, EXCL_ANNO);
277 output_style |= STYLE_ANNOTATED_SOURCE;
279 else
281 output_style &= ~STYLE_ANNOTATED_SOURCE;
283 user_specified |= STYLE_ANNOTATED_SOURCE;
284 break;
285 case 'k':
286 sym_id_add (optarg, EXCL_ARCS);
287 break;
288 case 'l':
289 line_granularity = true;
290 break;
291 case 'L':
292 print_path = true;
293 break;
294 case 'm':
295 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
296 break;
297 case 'n':
298 sym_id_add (optarg, INCL_TIME);
299 break;
300 case 'N':
301 sym_id_add (optarg, EXCL_TIME);
302 break;
303 case 'O':
304 switch (optarg[0])
306 case 'a':
307 file_format = FF_AUTO;
308 break;
309 case 'm':
310 file_format = FF_MAGIC;
311 break;
312 case 'b':
313 file_format = FF_BSD;
314 break;
315 case '4':
316 file_format = FF_BSD44;
317 break;
318 case 'p':
319 file_format = FF_PROF;
320 break;
321 default:
322 fprintf (stderr, _("%s: unknown file format %s\n"),
323 optarg, whoami);
324 done (1);
326 break;
327 case 'p':
328 if (optarg)
330 sym_id_add (optarg, INCL_FLAT);
332 output_style |= STYLE_FLAT_PROFILE;
333 user_specified |= STYLE_FLAT_PROFILE;
334 break;
335 case 'P':
336 if (optarg)
338 sym_id_add (optarg, EXCL_FLAT);
339 output_style |= STYLE_FLAT_PROFILE;
341 else
343 output_style &= ~STYLE_FLAT_PROFILE;
345 user_specified |= STYLE_FLAT_PROFILE;
346 break;
347 case 'q':
348 if (optarg)
350 if (strchr (optarg, '/'))
352 sym_id_add (optarg, INCL_ARCS);
354 else
356 sym_id_add (optarg, INCL_GRAPH);
359 output_style |= STYLE_CALL_GRAPH;
360 user_specified |= STYLE_CALL_GRAPH;
361 break;
362 case 'r':
363 output_style |= STYLE_FUNCTION_ORDER;
364 user_specified |= STYLE_FUNCTION_ORDER;
365 break;
366 case 'R':
367 output_style |= STYLE_FILE_ORDER;
368 user_specified |= STYLE_FILE_ORDER;
369 function_mapping_file = optarg;
370 break;
371 case 'Q':
372 if (optarg)
374 if (strchr (optarg, '/'))
376 sym_id_add (optarg, EXCL_ARCS);
378 else
380 sym_id_add (optarg, EXCL_GRAPH);
382 output_style |= STYLE_CALL_GRAPH;
384 else
386 output_style &= ~STYLE_CALL_GRAPH;
388 user_specified |= STYLE_CALL_GRAPH;
389 break;
390 case 's':
391 output_style |= STYLE_SUMMARY_FILE;
392 user_specified |= STYLE_SUMMARY_FILE;
393 break;
394 case 't':
395 bb_table_length = atoi (optarg);
396 if (bb_table_length < 0)
398 bb_table_length = 0;
400 break;
401 case 'T':
402 bsd_style_output = true;
403 break;
404 case 'v':
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"));
408 printf (_("\
409 This program is free software. This program has absolutely no warranty.\n"));
410 done (0);
411 case 'w':
412 output_width = atoi (optarg);
413 if (output_width < 1)
415 output_width = 1;
417 break;
418 case 'x':
419 bb_annotate_all_lines = true;
420 break;
421 case 'y':
422 create_annotation_files = true;
423 break;
424 case 'z':
425 ignore_zeros = false;
426 break;
427 case 'Z':
428 if (optarg)
430 sym_id_add (optarg, EXCL_EXEC);
431 output_style |= STYLE_EXEC_COUNTS;
433 else
435 output_style &= ~STYLE_EXEC_COUNTS;
437 user_specified |= STYLE_ANNOTATED_SOURCE;
438 break;
439 case OPTION_DEMANGLE:
440 demangle = true;
441 if (optarg != NULL)
443 enum demangling_styles style;
445 style = cplus_demangle_name_to_style (optarg);
446 if (style == unknown_demangling)
448 fprintf (stderr,
449 _("%s: unknown demangling style `%s'\n"),
450 whoami, optarg);
451 xexit (1);
454 cplus_demangle_set_style (style);
456 break;
457 case OPTION_NO_DEMANGLE:
458 demangle = false;
459 break;
460 default:
461 usage (stderr, 1);
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))
469 fprintf (stderr,_("\
470 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
471 whoami);
472 done (1);
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");
483 if (str)
485 search_list_append (&src_search_list, str);
488 if (optind < argc)
490 a_out_name = argv[optind++];
492 if (optind < argc)
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);
504 #ifdef __alpha__
505 sym_id_add (*sp, EXCL_FLAT);
506 #endif
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);
542 else
544 core_create_function_syms (core_bfd);
548 * Translate sym specs into syms:
550 sym_id_parse ();
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);
561 if (optind < argc)
563 gmon_name = argv[optind];
566 while (optind++ < argc);
567 #else
568 fprintf (stderr,
569 _("%s: sorry, file format `prof' is not yet supported\n"),
570 whoami);
571 done (1);
572 #endif
574 else
577 * Get information about gmon.out file(s):
581 gmon_out_read (gmon_name);
582 if (optind < argc)
584 gmon_name = argv[optind];
587 while (optind++ < argc);
591 * If user did not specify output style, try to guess something
592 * reasonable:
594 if (output_style == 0)
596 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
598 output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
600 else
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)
622 cg = cg_assemble ();
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);
631 done (1);
634 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
636 fprintf (stderr,
637 _("%s: gmon.out file is missing call-graph data\n"), whoami);
638 done (1);
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 */
659 cg_print_index ();
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 ();
679 return 0;
682 void
683 done (status)
684 int status;
686 exit (status);