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