No empty .Rs/.Re
[netbsd-mini2440.git] / external / gpl3 / binutils / dist / gprof / gprof.c
blob42018bb86d2c9e9728cc58e8b70014338123153b
1 /*
2 * Copyright (c) 1983, 1993, 1998, 2001, 2002
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
30 #include "gprof.h"
31 #include "libiberty.h"
32 #include "bfdver.h"
33 #include "search_list.h"
34 #include "source.h"
35 #include "symtab.h"
36 #include "basic_blocks.h"
37 #include "call_graph.h"
38 #include "cg_arcs.h"
39 #include "cg_print.h"
40 #include "corefile.h"
41 #include "gmon_io.h"
42 #include "hertz.h"
43 #include "hist.h"
44 #include "sym_ids.h"
45 #include "demangle.h"
46 #include "getopt.h"
48 static void usage (FILE *, int) ATTRIBUTE_NORETURN;
50 #include <stdlib.h>
52 const char *whoami;
53 const char *function_mapping_file;
54 const char *a_out_name = A_OUTNAME;
55 long hz = HZ_WRONG;
58 * Default options values:
60 int debug_level = 0;
61 int output_style = 0;
62 int output_width = 80;
63 bfd_boolean bsd_style_output = FALSE;
64 bfd_boolean demangle = TRUE;
65 bfd_boolean ignore_direct_calls = FALSE;
66 bfd_boolean ignore_static_funcs = FALSE;
67 bfd_boolean ignore_zeros = TRUE;
68 bfd_boolean line_granularity = FALSE;
69 bfd_boolean print_descriptions = TRUE;
70 bfd_boolean print_path = FALSE;
71 bfd_boolean ignore_non_functions = FALSE;
72 File_Format file_format = FF_AUTO;
74 bfd_boolean first_output = TRUE;
76 char copyright[] =
77 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
78 All rights reserved.\n";
80 static char *gmon_name = GMONNAME; /* profile filename */
83 * Functions that get excluded by default:
85 static char *default_excluded_list[] =
87 "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
88 "__mcleanup",
89 "<locore>", "<hicore>",
93 /* Codes used for the long options with no short synonyms. 150 isn't
94 special; it's just an arbitrary non-ASCII char value. */
96 #define OPTION_DEMANGLE (150)
97 #define OPTION_NO_DEMANGLE (OPTION_DEMANGLE + 1)
99 static struct option long_options[] =
101 {"line", no_argument, 0, 'l'},
102 {"no-static", no_argument, 0, 'a'},
103 {"ignore-non-functions", no_argument, 0, 'D'},
105 /* output styles: */
107 {"annotated-source", optional_argument, 0, 'A'},
108 {"no-annotated-source", optional_argument, 0, 'J'},
109 {"flat-profile", optional_argument, 0, 'p'},
110 {"no-flat-profile", optional_argument, 0, 'P'},
111 {"graph", optional_argument, 0, 'q'},
112 {"no-graph", optional_argument, 0, 'Q'},
113 {"exec-counts", optional_argument, 0, 'C'},
114 {"no-exec-counts", optional_argument, 0, 'Z'},
115 {"function-ordering", no_argument, 0, 'r'},
116 {"file-ordering", required_argument, 0, 'R'},
117 {"file-info", no_argument, 0, 'i'},
118 {"sum", no_argument, 0, 's'},
120 /* various options to affect output: */
122 {"all-lines", no_argument, 0, 'x'},
123 {"demangle", optional_argument, 0, OPTION_DEMANGLE},
124 {"no-demangle", no_argument, 0, OPTION_NO_DEMANGLE},
125 {"directory-path", required_argument, 0, 'I'},
126 {"display-unused-functions", no_argument, 0, 'z'},
127 {"min-count", required_argument, 0, 'm'},
128 {"print-path", no_argument, 0, 'L'},
129 {"separate-files", no_argument, 0, 'y'},
130 {"static-call-graph", no_argument, 0, 'c'},
131 {"table-length", required_argument, 0, 't'},
132 {"time", required_argument, 0, 'n'},
133 {"no-time", required_argument, 0, 'N'},
134 {"width", required_argument, 0, 'w'},
136 * These are for backwards-compatibility only. Their functionality
137 * is provided by the output style options already:
139 {"", required_argument, 0, 'e'},
140 {"", required_argument, 0, 'E'},
141 {"", required_argument, 0, 'f'},
142 {"", required_argument, 0, 'F'},
143 {"", required_argument, 0, 'k'},
145 /* miscellaneous: */
147 {"brief", no_argument, 0, 'b'},
148 {"debug", optional_argument, 0, 'd'},
149 {"help", no_argument, 0, 'h'},
150 {"file-format", required_argument, 0, 'O'},
151 {"traditional", no_argument, 0, 'T'},
152 {"version", no_argument, 0, 'v'},
153 {0, no_argument, 0, 0}
157 static void
158 usage (FILE *stream, int status)
160 fprintf (stream, _("\
161 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
162 [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
163 [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
164 [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
165 [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
166 [--function-ordering] [--file-ordering]\n\
167 [--directory-path=dirs] [--display-unused-functions]\n\
168 [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
169 [--no-static] [--print-path] [--separate-files]\n\
170 [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
171 [--version] [--width=n] [--ignore-non-functions]\n\
172 [--demangle[=STYLE]] [--no-demangle] [@FILE]\n\
173 [image-file] [profile-file...]\n"),
174 whoami);
175 if (REPORT_BUGS_TO[0] && status == 0)
176 fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
177 done (status);
182 main (int argc, char **argv)
184 char **sp, *str;
185 Sym **cg = 0;
186 int ch, user_specified = 0;
188 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
189 setlocale (LC_MESSAGES, "");
190 #endif
191 #if defined (HAVE_SETLOCALE)
192 setlocale (LC_CTYPE, "");
193 #endif
194 #ifdef ENABLE_NLS
195 bindtextdomain (PACKAGE, LOCALEDIR);
196 textdomain (PACKAGE);
197 #endif
199 whoami = argv[0];
200 xmalloc_set_program_name (whoami);
202 expandargv (&argc, &argv);
204 while ((ch = getopt_long (argc, argv,
205 "aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:st:Tvw:xyzZ::",
206 long_options, 0))
207 != EOF)
209 switch (ch)
211 case 'a':
212 ignore_static_funcs = TRUE;
213 break;
214 case 'A':
215 if (optarg)
217 sym_id_add (optarg, INCL_ANNO);
219 output_style |= STYLE_ANNOTATED_SOURCE;
220 user_specified |= STYLE_ANNOTATED_SOURCE;
221 break;
222 case 'b':
223 print_descriptions = FALSE;
224 break;
225 case 'B':
226 output_style |= STYLE_CALL_GRAPH;
227 user_specified |= STYLE_CALL_GRAPH;
228 break;
229 case 'c':
230 ignore_direct_calls = TRUE;
231 break;
232 case 'C':
233 if (optarg)
235 sym_id_add (optarg, INCL_EXEC);
237 output_style |= STYLE_EXEC_COUNTS;
238 user_specified |= STYLE_EXEC_COUNTS;
239 break;
240 case 'd':
241 if (optarg)
243 debug_level |= atoi (optarg);
244 debug_level |= ANYDEBUG;
246 else
248 debug_level = ~0;
250 DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
251 #ifndef DEBUG
252 printf (_("%s: debugging not supported; -d ignored\n"), whoami);
253 #endif /* DEBUG */
254 break;
255 case 'D':
256 ignore_non_functions = TRUE;
257 break;
258 case 'E':
259 sym_id_add (optarg, EXCL_TIME);
260 case 'e':
261 sym_id_add (optarg, EXCL_GRAPH);
262 break;
263 case 'F':
264 sym_id_add (optarg, INCL_TIME);
265 case 'f':
266 sym_id_add (optarg, INCL_GRAPH);
267 break;
268 case 'g':
269 sym_id_add (optarg, EXCL_FLAT);
270 break;
271 case 'G':
272 sym_id_add (optarg, INCL_FLAT);
273 break;
274 case 'h':
275 usage (stdout, 0);
276 case 'i':
277 output_style |= STYLE_GMON_INFO;
278 user_specified |= STYLE_GMON_INFO;
279 break;
280 case 'I':
281 search_list_append (&src_search_list, optarg);
282 break;
283 case 'J':
284 if (optarg)
286 sym_id_add (optarg, EXCL_ANNO);
287 output_style |= STYLE_ANNOTATED_SOURCE;
289 else
291 output_style &= ~STYLE_ANNOTATED_SOURCE;
293 user_specified |= STYLE_ANNOTATED_SOURCE;
294 break;
295 case 'k':
296 sym_id_add (optarg, EXCL_ARCS);
297 break;
298 case 'l':
299 line_granularity = TRUE;
300 break;
301 case 'L':
302 print_path = TRUE;
303 break;
304 case 'm':
305 bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
306 break;
307 case 'n':
308 sym_id_add (optarg, INCL_TIME);
309 break;
310 case 'N':
311 sym_id_add (optarg, EXCL_TIME);
312 break;
313 case 'O':
314 switch (optarg[0])
316 case 'a':
317 file_format = FF_AUTO;
318 break;
319 case 'm':
320 file_format = FF_MAGIC;
321 break;
322 case 'b':
323 file_format = FF_BSD;
324 break;
325 case '4':
326 file_format = FF_BSD44;
327 break;
328 case 'p':
329 file_format = FF_PROF;
330 break;
331 default:
332 fprintf (stderr, _("%s: unknown file format %s\n"),
333 optarg, whoami);
334 done (1);
336 break;
337 case 'p':
338 if (optarg)
340 sym_id_add (optarg, INCL_FLAT);
342 output_style |= STYLE_FLAT_PROFILE;
343 user_specified |= STYLE_FLAT_PROFILE;
344 break;
345 case 'P':
346 if (optarg)
348 sym_id_add (optarg, EXCL_FLAT);
349 output_style |= STYLE_FLAT_PROFILE;
351 else
353 output_style &= ~STYLE_FLAT_PROFILE;
355 user_specified |= STYLE_FLAT_PROFILE;
356 break;
357 case 'q':
358 if (optarg)
360 if (strchr (optarg, '/'))
362 sym_id_add (optarg, INCL_ARCS);
364 else
366 sym_id_add (optarg, INCL_GRAPH);
369 output_style |= STYLE_CALL_GRAPH;
370 user_specified |= STYLE_CALL_GRAPH;
371 break;
372 case 'r':
373 output_style |= STYLE_FUNCTION_ORDER;
374 user_specified |= STYLE_FUNCTION_ORDER;
375 break;
376 case 'R':
377 output_style |= STYLE_FILE_ORDER;
378 user_specified |= STYLE_FILE_ORDER;
379 function_mapping_file = optarg;
380 break;
381 case 'Q':
382 if (optarg)
384 if (strchr (optarg, '/'))
386 sym_id_add (optarg, EXCL_ARCS);
388 else
390 sym_id_add (optarg, EXCL_GRAPH);
392 output_style |= STYLE_CALL_GRAPH;
394 else
396 output_style &= ~STYLE_CALL_GRAPH;
398 user_specified |= STYLE_CALL_GRAPH;
399 break;
400 case 's':
401 output_style |= STYLE_SUMMARY_FILE;
402 user_specified |= STYLE_SUMMARY_FILE;
403 break;
404 case 't':
405 bb_table_length = atoi (optarg);
406 if (bb_table_length < 0)
408 bb_table_length = 0;
410 break;
411 case 'T':
412 bsd_style_output = TRUE;
413 break;
414 case 'v':
415 /* This output is intended to follow the GNU standards document. */
416 printf (_("GNU gprof %s\n"), BFD_VERSION_STRING);
417 printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
418 printf (_("\
419 This program is free software. This program has absolutely no warranty.\n"));
420 done (0);
421 case 'w':
422 output_width = atoi (optarg);
423 if (output_width < 1)
425 output_width = 1;
427 break;
428 case 'x':
429 bb_annotate_all_lines = TRUE;
430 break;
431 case 'y':
432 create_annotation_files = TRUE;
433 break;
434 case 'z':
435 ignore_zeros = FALSE;
436 break;
437 case 'Z':
438 if (optarg)
440 sym_id_add (optarg, EXCL_EXEC);
441 output_style |= STYLE_EXEC_COUNTS;
443 else
445 output_style &= ~STYLE_EXEC_COUNTS;
447 user_specified |= STYLE_ANNOTATED_SOURCE;
448 break;
449 case OPTION_DEMANGLE:
450 demangle = TRUE;
451 if (optarg != NULL)
453 enum demangling_styles style;
455 style = cplus_demangle_name_to_style (optarg);
456 if (style == unknown_demangling)
458 fprintf (stderr,
459 _("%s: unknown demangling style `%s'\n"),
460 whoami, optarg);
461 xexit (1);
464 cplus_demangle_set_style (style);
466 break;
467 case OPTION_NO_DEMANGLE:
468 demangle = FALSE;
469 break;
470 default:
471 usage (stderr, 1);
475 /* Don't allow both ordering options, they modify the arc data in-place. */
476 if ((user_specified & STYLE_FUNCTION_ORDER)
477 && (user_specified & STYLE_FILE_ORDER))
479 fprintf (stderr,_("\
480 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
481 whoami);
482 done (1);
485 /* --sum implies --line, otherwise we'd lose basic block counts in
486 gmon.sum */
487 if (output_style & STYLE_SUMMARY_FILE)
488 line_granularity = 1;
490 /* append value of GPROF_PATH to source search list if set: */
491 str = (char *) getenv ("GPROF_PATH");
492 if (str)
493 search_list_append (&src_search_list, str);
495 if (optind < argc)
496 a_out_name = argv[optind++];
498 if (optind < argc)
499 gmon_name = argv[optind++];
501 /* Turn off default functions. */
502 for (sp = &default_excluded_list[0]; *sp; sp++)
504 sym_id_add (*sp, EXCL_TIME);
505 sym_id_add (*sp, EXCL_GRAPH);
506 sym_id_add (*sp, EXCL_FLAT);
509 /* Read symbol table from core file. */
510 core_init (a_out_name);
512 /* If we should ignore direct function calls, we need to load to
513 core's text-space. */
514 if (ignore_direct_calls)
515 core_get_text_space (core_bfd);
517 /* Create symbols from core image. */
518 if (line_granularity)
519 core_create_line_syms ();
520 else
521 core_create_function_syms ();
523 /* Translate sym specs into syms. */
524 sym_id_parse ();
526 if (file_format == FF_PROF)
528 fprintf (stderr,
529 _("%s: sorry, file format `prof' is not yet supported\n"),
530 whoami);
531 done (1);
533 else
535 /* Get information about gmon.out file(s). */
538 gmon_out_read (gmon_name);
539 if (optind < argc)
540 gmon_name = argv[optind];
542 while (optind++ < argc);
545 /* If user did not specify output style, try to guess something
546 reasonable. */
547 if (output_style == 0)
549 if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
551 if (gmon_input & INPUT_HISTOGRAM)
552 output_style |= STYLE_FLAT_PROFILE;
553 if (gmon_input & INPUT_CALL_GRAPH)
554 output_style |= STYLE_CALL_GRAPH;
556 else
557 output_style = STYLE_EXEC_COUNTS;
559 output_style &= ~user_specified;
562 /* Dump a gmon.sum file if requested (before any other
563 processing!) */
564 if (output_style & STYLE_SUMMARY_FILE)
566 gmon_out_write (GMONSUM);
569 if (gmon_input & INPUT_HISTOGRAM)
571 hist_assign_samples ();
574 if (gmon_input & INPUT_CALL_GRAPH)
576 cg = cg_assemble ();
579 /* Do some simple sanity checks. */
580 if ((output_style & STYLE_FLAT_PROFILE)
581 && !(gmon_input & INPUT_HISTOGRAM))
583 fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
584 done (1);
587 if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
589 fprintf (stderr,
590 _("%s: gmon.out file is missing call-graph data\n"), whoami);
591 done (1);
594 /* Output whatever user whishes to see. */
595 if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
597 /* Print the dynamic profile. */
598 cg_print (cg);
601 if (output_style & STYLE_FLAT_PROFILE)
603 /* Print the flat profile. */
604 hist_print ();
607 if (cg && (output_style & STYLE_CALL_GRAPH))
609 if (!bsd_style_output)
611 /* Print the dynamic profile. */
612 cg_print (cg);
614 cg_print_index ();
617 if (output_style & STYLE_EXEC_COUNTS)
618 print_exec_counts ();
620 if (output_style & STYLE_ANNOTATED_SOURCE)
621 print_annotated_source ();
623 if (output_style & STYLE_FUNCTION_ORDER)
624 cg_print_function_ordering ();
626 if (output_style & STYLE_FILE_ORDER)
627 cg_print_file_ordering ();
629 return 0;
632 void
633 done (int status)
635 exit (status);