Tue Feb 3 14:25:25 1998 Brent Baccala <baccala@freesoft.org>
[external/binutils.git] / gprof / gprof.c
1 /*
2  * Copyright (c) 1983 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 "getopt.h"
20 #include "libiberty.h"
21 #include "gprof.h"
22 #include "basic_blocks.h"
23 #include "call_graph.h"
24 #include "cg_arcs.h"
25 #include "cg_print.h"
26 #include "core.h"
27 #include "gmon_io.h"
28 #include "hertz.h"
29 #include "hist.h"
30 #include "source.h"
31 #include "sym_ids.h"
32
33 const char *whoami;
34 const char *function_mapping_file;
35 const char *a_out_name = A_OUTNAME;
36 long hz = HZ_WRONG;
37
38 /*
39  * Default options values:
40  */
41 int debug_level = 0;
42 int output_style = 0;
43 int output_width = 80;
44 bool bsd_style_output = FALSE;
45 bool discard_underscores = TRUE;
46 bool ignore_direct_calls = FALSE;
47 bool ignore_static_funcs = FALSE;
48 bool ignore_zeros = TRUE;
49 bool line_granularity = FALSE;
50 bool print_descriptions = TRUE;
51 bool print_path = FALSE;
52 bool ignore_non_functions = FALSE;
53 File_Format file_format = FF_AUTO;
54
55 bool first_output = TRUE;
56
57 char copyright[] =
58 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
59  All rights reserved.\n";
60
61 static char *gmon_name = GMONNAME;      /* profile filename */
62
63 bfd *abfd;
64
65 /*
66  * Functions that get excluded by default:
67  */
68 static char *default_excluded_list[] =
69 {
70   "_gprof_mcount", "mcount", "_mcount", "__mcount", "__mcount_internal",
71   "__mcleanup",
72   "<locore>", "<hicore>",
73   0
74 };
75
76 static struct option long_options[] =
77 {
78   {"line", no_argument, 0, 'l'},
79   {"no-static", no_argument, 0, 'a'},
80   {"ignore-non-functions", no_argument, 0, 'D'},
81
82     /* output styles: */
83
84   {"annotated-source", optional_argument, 0, 'A'},
85   {"no-annotated-source", optional_argument, 0, 'J'},
86   {"flat-profile", optional_argument, 0, 'p'},
87   {"no-flat-profile", optional_argument, 0, 'P'},
88   {"graph", optional_argument, 0, 'q'},
89   {"no-graph", optional_argument, 0, 'Q'},
90   {"exec-counts", optional_argument, 0, 'C'},
91   {"no-exec-counts", optional_argument, 0, 'Z'},
92   {"function-ordering", no_argument, 0, 'r'},
93   {"file-ordering", required_argument, 0, 'R'},
94   {"file-info", no_argument, 0, 'i'},
95   {"sum", no_argument, 0, 's'},
96
97     /* various options to affect output: */
98
99   {"all-lines", no_argument, 0, 'x'},
100   {"directory-path", required_argument, 0, 'I'},
101   {"display-unused-functions", no_argument, 0, 'z'},
102   {"min-count", required_argument, 0, 'm'},
103   {"print-path", no_argument, 0, 'L'},
104   {"separate-files", no_argument, 0, 'y'},
105   {"static-call-graph", no_argument, 0, 'c'},
106   {"table-length", required_argument, 0, 't'},
107   {"time", required_argument, 0, 'n'},
108   {"no-time", required_argument, 0, 'N'},
109   {"width", required_argument, 0, 'w'},
110     /*
111      * These are for backwards-compatibility only.  Their functionality
112      * is provided by the output style options already:
113      */
114   {"", required_argument, 0, 'e'},
115   {"", required_argument, 0, 'E'},
116   {"", required_argument, 0, 'f'},
117   {"", required_argument, 0, 'F'},
118   {"", required_argument, 0, 'k'},
119
120     /* miscellaneous: */
121
122   {"brief", no_argument, 0, 'b'},
123   {"debug", optional_argument, 0, 'd'},
124   {"help", no_argument, 0, 'h'},
125   {"file-format", required_argument, 0, 'O'},
126   {"traditional", no_argument, 0, 'T'},
127   {"version", no_argument, 0, 'v'},
128   {0, no_argument, 0, 0}
129 };
130
131
132 static void
133 DEFUN (usage, (stream, status), FILE * stream AND int status)
134 {
135   fprintf (stream, "\
136 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqQZ][name]] [-I dirs]\n\
137         [-d[num]] [-k from/to] [-m min-count] [-t table-length]\n\
138         [--[no-]annotated-source[=name]] [--[no-]exec-counts[=name]]\n\
139         [--[no-]flat-profile[=name]] [--[no-]graph[=name]]\n\
140         [--[no-]time=name] [--all-lines] [--brief] [--debug[=level]]\n\
141         [--function-ordering] [--file-ordering]\n\
142         [--directory-path=dirs] [--display-unused-functions]\n\
143         [--file-format=name] [--file-info] [--help] [--line] [--min-count=n]\n\
144         [--no-static] [--print-path] [--separate-files]\n\
145         [--static-call-graph] [--sum] [--table-length=len] [--traditional]\n\
146         [--version] [--width=n] [--ignore-non-functions]\n\
147         [image-file] [profile-file...]\n",
148            whoami);
149   if (status == 0)
150     fprintf (stream, "Report bugs to bug-gnu-utils@prep.ai.mit.edu\n");
151   done (status);
152 }
153
154
155 int
156 DEFUN (main, (argc, argv), int argc AND char **argv)
157 {
158   char **sp, *str;
159   Sym **cg = 0;
160   int ch, user_specified = 0;
161
162   whoami = argv[0];
163   xmalloc_set_program_name (whoami);
164
165   while ((ch = getopt_long (argc, argv,
166         "aA::bBcCdD::e:E:f:F:hiI:J::k:lLm:n::N::O:p::P::q::Q::st:Tvw:xyzZ::",
167                             long_options, 0))
168          != EOF)
169     {
170       switch (ch)
171         {
172         case 'a':
173           ignore_static_funcs = TRUE;
174           break;
175         case 'A':
176           if (optarg)
177             {
178               sym_id_add (optarg, INCL_ANNO);
179             }
180           output_style |= STYLE_ANNOTATED_SOURCE;
181           user_specified |= STYLE_ANNOTATED_SOURCE;
182           break;
183         case 'b':
184           print_descriptions = FALSE;
185           break;
186         case 'B':
187           output_style |= STYLE_CALL_GRAPH;
188           user_specified |= STYLE_CALL_GRAPH;
189           break;
190         case 'c':
191           ignore_direct_calls = TRUE;
192           break;
193         case 'C':
194           if (optarg)
195             {
196               sym_id_add (optarg, INCL_EXEC);
197             }
198           output_style |= STYLE_EXEC_COUNTS;
199           user_specified |= STYLE_EXEC_COUNTS;
200           break;
201         case 'd':
202           if (optarg)
203             {
204               debug_level |= atoi (optarg);
205               debug_level |= ANYDEBUG;
206             }
207           else
208             {
209               debug_level = ~0;
210             }
211           DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
212 #ifndef DEBUG
213           printf ("%s: debugging not supported; -d ignored\n", whoami);
214 #endif  /* DEBUG */
215           break;
216         case 'D':
217           ignore_non_functions = TRUE;
218           break;
219         case 'E':
220           sym_id_add (optarg, EXCL_TIME);
221         case 'e':
222           sym_id_add (optarg, EXCL_GRAPH);
223           break;
224         case 'F':
225           sym_id_add (optarg, INCL_TIME);
226         case 'f':
227           sym_id_add (optarg, INCL_GRAPH);
228           break;
229         case 'g':
230           sym_id_add (optarg, EXCL_FLAT);
231           break;
232         case 'G':
233           sym_id_add (optarg, INCL_FLAT);
234           break;
235         case 'h':
236           usage (stdout, 0);
237         case 'i':
238           output_style |= STYLE_GMON_INFO;
239           user_specified |= STYLE_GMON_INFO;
240           break;
241         case 'I':
242           search_list_append (&src_search_list, optarg);
243           break;
244         case 'J':
245           if (optarg)
246             {
247               sym_id_add (optarg, EXCL_ANNO);
248               output_style |= STYLE_ANNOTATED_SOURCE;
249             }
250           else
251             {
252               output_style &= ~STYLE_ANNOTATED_SOURCE;
253             }
254           user_specified |= STYLE_ANNOTATED_SOURCE;
255           break;
256         case 'k':
257           sym_id_add (optarg, EXCL_ARCS);
258           break;
259         case 'l':
260           line_granularity = TRUE;
261           break;
262         case 'L':
263           print_path = TRUE;
264           break;
265         case 'm':
266           bb_min_calls = atoi (optarg);
267           break;
268         case 'n':
269           sym_id_add (optarg, INCL_TIME);
270           break;
271         case 'N':
272           sym_id_add (optarg, EXCL_TIME);
273           break;
274         case 'O':
275           switch (optarg[0])
276             {
277             case 'a':
278               file_format = FF_AUTO;
279               break;
280             case 'm':
281               file_format = FF_MAGIC;
282               break;
283             case 'b':
284               file_format = FF_BSD;
285               break;
286             case 'p':
287               file_format = FF_PROF;
288               break;
289             default:
290               fprintf (stderr, "%s: unknown file format %s\n",
291                        optarg, whoami);
292               done (1);
293             }
294           break;
295         case 'p':
296           if (optarg)
297             {
298               sym_id_add (optarg, INCL_FLAT);
299             }
300           output_style |= STYLE_FLAT_PROFILE;
301           user_specified |= STYLE_FLAT_PROFILE;
302           break;
303         case 'P':
304           if (optarg)
305             {
306               sym_id_add (optarg, EXCL_FLAT);
307               output_style |= STYLE_FLAT_PROFILE;
308             }
309           else
310             {
311               output_style &= ~STYLE_FLAT_PROFILE;
312             }
313           user_specified |= STYLE_FLAT_PROFILE;
314           break;
315         case 'q':
316           if (optarg)
317             {
318               if (strchr (optarg, '/'))
319                 {
320                   sym_id_add (optarg, INCL_ARCS);
321                 }
322               else
323                 {
324                   sym_id_add (optarg, INCL_GRAPH);
325                 }
326             }
327           output_style |= STYLE_CALL_GRAPH;
328           user_specified |= STYLE_CALL_GRAPH;
329           break;
330         case 'r':
331           output_style |= STYLE_FUNCTION_ORDER;
332           user_specified |= STYLE_FUNCTION_ORDER;
333           break;
334         case 'R':
335           output_style |= STYLE_FILE_ORDER;
336           user_specified |= STYLE_FILE_ORDER;
337           function_mapping_file = optarg;
338           break;
339         case 'Q':
340           if (optarg)
341             {
342               if (strchr (optarg, '/'))
343                 {
344                   sym_id_add (optarg, EXCL_ARCS);
345                 }
346               else
347                 {
348                   sym_id_add (optarg, EXCL_GRAPH);
349                 }
350               output_style |= STYLE_CALL_GRAPH;
351             }
352           else
353             {
354               output_style &= ~STYLE_CALL_GRAPH;
355             }
356           user_specified |= STYLE_CALL_GRAPH;
357           break;
358         case 's':
359           output_style |= STYLE_SUMMARY_FILE;
360           user_specified |= STYLE_SUMMARY_FILE;
361           break;
362         case 't':
363           bb_table_length = atoi (optarg);
364           if (bb_table_length < 0)
365             {
366               bb_table_length = 0;
367             }
368           break;
369         case 'T':
370           bsd_style_output = TRUE;
371           break;
372         case 'v':
373           /* This output is intended to follow the GNU standards document.  */
374           printf ("GNU gprof %s\n", VERSION);
375           printf ("Based on BSD gprof, copyright 1983 Regents of the University of California.\n");
376           printf ("\
377 This program is free software.  This program has absolutely no warranty.\n");
378           done (0);
379         case 'w':
380           output_width = atoi (optarg);
381           if (output_width < 1)
382             {
383               output_width = 1;
384             }
385           break;
386         case 'x':
387           bb_annotate_all_lines = TRUE;
388           break;
389         case 'y':
390           create_annotation_files = TRUE;
391           break;
392         case 'z':
393           ignore_zeros = FALSE;
394           break;
395         case 'Z':
396           if (optarg)
397             {
398               sym_id_add (optarg, EXCL_EXEC);
399               output_style |= STYLE_EXEC_COUNTS;
400             }
401           else
402             {
403               output_style &= ~STYLE_EXEC_COUNTS;
404             }
405           user_specified |= STYLE_ANNOTATED_SOURCE;
406           break;
407         default:
408           usage (stderr, 1);
409         }
410     }
411
412   /* Don't allow both ordering options, they modify the arc data in-place.  */
413   if ((user_specified & STYLE_FUNCTION_ORDER)
414       && (user_specified & STYLE_FILE_ORDER))
415     {
416       fprintf (stderr,"\
417 %s: Only one of --function-ordering and --file-ordering may be specified.\n",
418                whoami);
419       done (1);
420     }
421
422   /* --sum implies --line, otherwise we'd lose b-b counts in gmon.sum */
423   if (output_style & STYLE_SUMMARY_FILE)
424     {
425       line_granularity = 1;
426     }
427
428   /* append value of GPROF_PATH to source search list if set: */
429   str = (char *) getenv ("GPROF_PATH");
430   if (str)
431     {
432       search_list_append (&src_search_list, str);
433     }
434
435   if (optind < argc)
436     {
437       a_out_name = argv[optind++];
438     }
439   if (optind < argc)
440     {
441       gmon_name = argv[optind++];
442     }
443
444   /*
445    * Turn off default functions:
446    */
447   for (sp = &default_excluded_list[0]; *sp; sp++)
448     {
449       sym_id_add (*sp, EXCL_TIME);
450       sym_id_add (*sp, EXCL_GRAPH);
451 #ifdef __alpha__
452       sym_id_add (*sp, EXCL_FLAT);
453 #endif
454     }
455
456   /*
457    * For line-by-line profiling, also want to keep those
458    * functions off the flat profile:
459    */
460   if (line_granularity)
461     {
462       for (sp = &default_excluded_list[0]; *sp; sp++)
463         {
464           sym_id_add (*sp, EXCL_FLAT);
465         }
466     }
467
468   /*
469    * Read symbol table from core file:
470    */
471   core_init (a_out_name);
472
473   /*
474    * If we should ignore direct function calls, we need to load
475    * to core's text-space:
476    */
477   if (ignore_direct_calls)
478     {
479       core_get_text_space (core_bfd);
480     }
481
482   /*
483    * Create symbols from core image:
484    */
485   if (line_granularity)
486     {
487       core_create_line_syms (core_bfd);
488     }
489   else
490     {
491       core_create_function_syms (core_bfd);
492     }
493
494   /*
495    * Translate sym specs into syms:
496    */
497   sym_id_parse ();
498
499   if (file_format == FF_PROF)
500     {
501 #ifdef PROF_SUPPORT_IMPLEMENTED
502       /*
503        * Get information about mon.out file(s):
504        */
505       do
506         {
507           mon_out_read (gmon_name);
508           if (optind < argc)
509             {
510               gmon_name = argv[optind];
511             }
512         }
513       while (optind++ < argc);
514 #else
515       fprintf (stderr,
516                "%s: sorry, file format `prof' is not yet supported\n",
517                whoami);
518       done (1);
519 #endif
520     }
521   else
522     {
523       /*
524        * Get information about gmon.out file(s):
525        */
526       do
527         {
528           gmon_out_read (gmon_name);
529           if (optind < argc)
530             {
531               gmon_name = argv[optind];
532             }
533         }
534       while (optind++ < argc);
535     }
536
537   /*
538    * If user did not specify output style, try to guess something
539    * reasonable:
540    */
541   if (output_style == 0)
542     {
543       if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
544         {
545           output_style = STYLE_FLAT_PROFILE | STYLE_CALL_GRAPH;
546         }
547       else
548         {
549           output_style = STYLE_EXEC_COUNTS;
550         }
551       output_style &= ~user_specified;
552     }
553
554   /*
555    * Dump a gmon.sum file if requested (before any other processing!):
556    */
557   if (output_style & STYLE_SUMMARY_FILE)
558     {
559       gmon_out_write (GMONSUM);
560     }
561
562   if (gmon_input & INPUT_HISTOGRAM)
563     {
564       hist_assign_samples ();
565     }
566
567   if (gmon_input & INPUT_CALL_GRAPH)
568     {
569       cg = cg_assemble ();
570     }
571
572   /* do some simple sanity checks: */
573
574   if ((output_style & STYLE_FLAT_PROFILE)
575       && !(gmon_input & INPUT_HISTOGRAM))
576     {
577       fprintf (stderr, "%s: gmon.out file is missing histogram\n", whoami);
578       done (1);
579     }
580
581   if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
582     {
583       fprintf (stderr,
584                "%s: gmon.out file is missing call-graph data\n", whoami);
585       done (1);
586     }
587
588   /* output whatever user whishes to see: */
589
590   if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
591     {
592       cg_print (cg);            /* print the dynamic profile */
593     }
594
595   if (output_style & STYLE_FLAT_PROFILE)
596     {
597       hist_print ();            /* print the flat profile */
598     }
599
600   if (cg && (output_style & STYLE_CALL_GRAPH))
601     {
602       if (!bsd_style_output)
603         {
604           cg_print (cg);        /* print the dynamic profile */
605         }
606       cg_print_index ();
607     }
608
609   if (output_style & STYLE_EXEC_COUNTS)
610     {
611       print_exec_counts ();
612     }
613
614   if (output_style & STYLE_ANNOTATED_SOURCE)
615     {
616       print_annotated_source ();
617     }
618   if (output_style & STYLE_FUNCTION_ORDER)
619     {
620       cg_print_function_ordering ();
621     }
622   if (output_style & STYLE_FILE_ORDER)
623     {
624       cg_print_file_ordering ();
625     }
626   return 0;
627 }
628
629 void
630 done (status)
631      int status;
632 {
633   exit (status);
634 }