Update copyright date per last commit.
[external/binutils.git] / gprof / gprof.c
1 /*
2  * Copyright (c) 1983, 1998, 2001, 2002 Regents of the University of California.
3  * All rights reserved.
4  *
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.
18  */
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"
35
36 static void usage PARAMS ((FILE *, int)) ATTRIBUTE_NORETURN;
37 int main PARAMS ((int, char **));
38
39 const char *whoami;
40 const char *function_mapping_file;
41 const char *a_out_name = A_OUTNAME;
42 long hz = HZ_WRONG;
43
44 /*
45  * Default options values:
46  */
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;
61
62 boolean first_output = true;
63
64 char copyright[] =
65  "@(#) Copyright (c) 1983 Regents of the University of California.\n\
66  All rights reserved.\n";
67
68 static char *gmon_name = GMONNAME;      /* profile filename */
69
70 bfd *abfd;
71
72 /*
73  * Functions that get excluded by default:
74  */
75 static char *default_excluded_list[] =
76 {
77   "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
78   "__mcleanup",
79   "<locore>", "<hicore>",
80   0
81 };
82
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.  */
85
86 #define OPTION_DEMANGLE         (150)
87 #define OPTION_NO_DEMANGLE      (OPTION_DEMANGLE + 1)
88
89 static struct option long_options[] =
90 {
91   {"line", no_argument, 0, 'l'},
92   {"no-static", no_argument, 0, 'a'},
93   {"ignore-non-functions", no_argument, 0, 'D'},
94
95     /* output styles: */
96
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'},
109
110     /* various options to affect output: */
111
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'},
125     /*
126      * These are for backwards-compatibility only.  Their functionality
127      * is provided by the output style options already:
128      */
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'},
134
135     /* miscellaneous: */
136
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}
144 };
145
146
147 static void
148 usage (stream, status)
149      FILE *stream;
150      int status;
151 {
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);
170 }
171
172
173 int
174 main (argc, argv)
175      int argc;
176      char **argv;
177 {
178   char **sp, *str;
179   Sym **cg = 0;
180   int ch, user_specified = 0;
181
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);
190
191   whoami = argv[0];
192   xmalloc_set_program_name (whoami);
193
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)
198     {
199       switch (ch)
200         {
201         case 'a':
202           ignore_static_funcs = true;
203           break;
204         case 'A':
205           if (optarg)
206             {
207               sym_id_add (optarg, INCL_ANNO);
208             }
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)
224             {
225               sym_id_add (optarg, INCL_EXEC);
226             }
227           output_style |= STYLE_EXEC_COUNTS;
228           user_specified |= STYLE_EXEC_COUNTS;
229           break;
230         case 'd':
231           if (optarg)
232             {
233               debug_level |= atoi (optarg);
234               debug_level |= ANYDEBUG;
235             }
236           else
237             {
238               debug_level = ~0;
239             }
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)
275             {
276               sym_id_add (optarg, EXCL_ANNO);
277               output_style |= STYLE_ANNOTATED_SOURCE;
278             }
279           else
280             {
281               output_style &= ~STYLE_ANNOTATED_SOURCE;
282             }
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])
305             {
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);
325             }
326           break;
327         case 'p':
328           if (optarg)
329             {
330               sym_id_add (optarg, INCL_FLAT);
331             }
332           output_style |= STYLE_FLAT_PROFILE;
333           user_specified |= STYLE_FLAT_PROFILE;
334           break;
335         case 'P':
336           if (optarg)
337             {
338               sym_id_add (optarg, EXCL_FLAT);
339               output_style |= STYLE_FLAT_PROFILE;
340             }
341           else
342             {
343               output_style &= ~STYLE_FLAT_PROFILE;
344             }
345           user_specified |= STYLE_FLAT_PROFILE;
346           break;
347         case 'q':
348           if (optarg)
349             {
350               if (strchr (optarg, '/'))
351                 {
352                   sym_id_add (optarg, INCL_ARCS);
353                 }
354               else
355                 {
356                   sym_id_add (optarg, INCL_GRAPH);
357                 }
358             }
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)
373             {
374               if (strchr (optarg, '/'))
375                 {
376                   sym_id_add (optarg, EXCL_ARCS);
377                 }
378               else
379                 {
380                   sym_id_add (optarg, EXCL_GRAPH);
381                 }
382               output_style |= STYLE_CALL_GRAPH;
383             }
384           else
385             {
386               output_style &= ~STYLE_CALL_GRAPH;
387             }
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)
397             {
398               bb_table_length = 0;
399             }
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)
414             {
415               output_width = 1;
416             }
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)
429             {
430               sym_id_add (optarg, EXCL_EXEC);
431               output_style |= STYLE_EXEC_COUNTS;
432             }
433           else
434             {
435               output_style &= ~STYLE_EXEC_COUNTS;
436             }
437           user_specified |= STYLE_ANNOTATED_SOURCE;
438           break;
439         case OPTION_DEMANGLE:
440           demangle = true;
441           if (optarg != NULL)
442             {
443               enum demangling_styles style;
444
445               style = cplus_demangle_name_to_style (optarg);
446               if (style == unknown_demangling)
447                 {
448                   fprintf (stderr,
449                            _("%s: unknown demangling style `%s'\n"),
450                            whoami, optarg);
451                   xexit (1);
452                 }
453
454               cplus_demangle_set_style (style);
455            }
456           break;
457         case OPTION_NO_DEMANGLE:
458           demangle = false;
459           break;
460         default:
461           usage (stderr, 1);
462         }
463     }
464
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))
468     {
469       fprintf (stderr,_("\
470 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
471                whoami);
472       done (1);
473     }
474
475   /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
476   if (output_style & STYLE_SUMMARY_FILE)
477     {
478       line_granularity = 1;
479     }
480
481   /* append value of GPROF_PATH to source search list if set: */
482   str = (char *) getenv ("GPROF_PATH");
483   if (str)
484     {
485       search_list_append (&src_search_list, str);
486     }
487
488   if (optind < argc)
489     {
490       a_out_name = argv[optind++];
491     }
492   if (optind < argc)
493     {
494       gmon_name = argv[optind++];
495     }
496
497   /*
498    * Turn off default functions:
499    */
500   for (sp = &default_excluded_list[0]; *sp; sp++)
501     {
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
507     }
508
509   /*
510    * For line-by-line profiling, also want to keep those
511    * functions off the flat profile:
512    */
513   if (line_granularity)
514     {
515       for (sp = &default_excluded_list[0]; *sp; sp++)
516         {
517           sym_id_add (*sp, EXCL_FLAT);
518         }
519     }
520
521   /*
522    * Read symbol table from core file:
523    */
524   core_init (a_out_name);
525
526   /*
527    * If we should ignore direct function calls, we need to load
528    * to core's text-space:
529    */
530   if (ignore_direct_calls)
531     {
532       core_get_text_space (core_bfd);
533     }
534
535   /*
536    * Create symbols from core image:
537    */
538   if (line_granularity)
539     {
540       core_create_line_syms (core_bfd);
541     }
542   else
543     {
544       core_create_function_syms (core_bfd);
545     }
546
547   /*
548    * Translate sym specs into syms:
549    */
550   sym_id_parse ();
551
552   if (file_format == FF_PROF)
553     {
554 #ifdef PROF_SUPPORT_IMPLEMENTED
555       /*
556        * Get information about mon.out file(s):
557        */
558       do
559         {
560           mon_out_read (gmon_name);
561           if (optind < argc)
562             {
563               gmon_name = argv[optind];
564             }
565         }
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
573     }
574   else
575     {
576       /*
577        * Get information about gmon.out file(s):
578        */
579       do
580         {
581           gmon_out_read (gmon_name);
582           if (optind < argc)
583             {
584               gmon_name = argv[optind];
585             }
586         }
587       while (optind++ < argc);
588     }
589
590   /*
591    * If user did not specify output style, try to guess something
592    * reasonable:
593    */
594   if (output_style == 0)
595     {
596       if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
597         {
598           output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
599         }
600       else
601         {
602           output_style = STYLE_EXEC_COUNTS;
603         }
604       output_style &= ~user_specified;
605     }
606
607   /*
608    * Dump a gmon.sum file if requested (before any other processing!):
609    */
610   if (output_style & STYLE_SUMMARY_FILE)
611     {
612       gmon_out_write (GMONSUM);
613     }
614
615   if (gmon_input & INPUT_HISTOGRAM)
616     {
617       hist_assign_samples ();
618     }
619
620   if (gmon_input & INPUT_CALL_GRAPH)
621     {
622       cg = cg_assemble ();
623     }
624
625   /* do some simple sanity checks: */
626
627   if ((output_style & STYLE_FLAT_PROFILE)
628       && !(gmon_input & INPUT_HISTOGRAM))
629     {
630       fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
631       done (1);
632     }
633
634   if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
635     {
636       fprintf (stderr,
637                _("%s: gmon.out file is missing call-graph data\n"), whoami);
638       done (1);
639     }
640
641   /* output whatever user whishes to see: */
642
643   if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
644     {
645       cg_print (cg);            /* print the dynamic profile */
646     }
647
648   if (output_style & STYLE_FLAT_PROFILE)
649     {
650       hist_print ();            /* print the flat profile */
651     }
652
653   if (cg && (output_style & STYLE_CALL_GRAPH))
654     {
655       if (!bsd_style_output)
656         {
657           cg_print (cg);        /* print the dynamic profile */
658         }
659       cg_print_index ();
660     }
661
662   if (output_style & STYLE_EXEC_COUNTS)
663     {
664       print_exec_counts ();
665     }
666
667   if (output_style & STYLE_ANNOTATED_SOURCE)
668     {
669       print_annotated_source ();
670     }
671   if (output_style & STYLE_FUNCTION_ORDER)
672     {
673       cg_print_function_ordering ();
674     }
675   if (output_style & STYLE_FILE_ORDER)
676     {
677       cg_print_file_ordering ();
678     }
679   return 0;
680 }
681
682 void
683 done (status)
684      int status;
685 {
686   exit (status);
687 }