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