xref: /netbsd-src/external/gpl3/binutils.old/dist/gprof/gprof.c (revision 867d70fc718005c0918b8b8b2f9d7f2d52d0a0db)
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 bfd_boolean bsd_style_output = FALSE;
65 bfd_boolean demangle = TRUE;
66 bfd_boolean ignore_direct_calls = FALSE;
67 bfd_boolean ignore_static_funcs = FALSE;
68 bfd_boolean ignore_zeros = TRUE;
69 bfd_boolean line_granularity = FALSE;
70 bfd_boolean print_descriptions = TRUE;
71 bfd_boolean print_path = FALSE;
72 bfd_boolean ignore_non_functions = FALSE;
73 bfd_boolean inline_file_names = FALSE;
74 File_Format file_format = FF_AUTO;
75 
76 bfd_boolean 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
162 usage (FILE *stream, int status)
163 {
164   fprintf (stream, _("\
165 Usage: %s [-[abcDhilLsTvwxyz]] [-[ACeEfFJnNOpPqSQZ][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
186 main (int argc, char **argv)
187 {
188   char **sp, *str;
189   Sym **cg = 0;
190   int ch, user_specified = 0;
191 
192 #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
193   setlocale (LC_MESSAGES, "");
194 #endif
195 #if defined (HAVE_SETLOCALE)
196   setlocale (LC_CTYPE, "");
197 #endif
198 #ifdef ENABLE_NLS
199   bindtextdomain (PACKAGE, LOCALEDIR);
200   textdomain (PACKAGE);
201 #endif
202 
203   whoami = argv[0];
204   xmalloc_set_program_name (whoami);
205 
206   expandargv (&argc, &argv);
207 
208   while ((ch = getopt_long (argc, argv,
209 	"aA::bBcC::d::De:E:f:F:hiI:J::k:lLm:n:N:O:p::P::q::Q::rR:sS:t:Tvw:xyzZ::",
210 			    long_options, 0))
211 	 != EOF)
212     {
213       switch (ch)
214 	{
215 	case 'a':
216 	  ignore_static_funcs = TRUE;
217 	  break;
218 	case 'A':
219 	  if (optarg)
220 	    {
221 	      sym_id_add (optarg, INCL_ANNO);
222 	    }
223 	  output_style |= STYLE_ANNOTATED_SOURCE;
224 	  user_specified |= STYLE_ANNOTATED_SOURCE;
225 	  break;
226 	case 'b':
227 	  print_descriptions = FALSE;
228 	  break;
229 	case 'B':
230 	  output_style |= STYLE_CALL_GRAPH;
231 	  user_specified |= STYLE_CALL_GRAPH;
232 	  break;
233 	case 'c':
234 	  ignore_direct_calls = TRUE;
235 	  break;
236 	case 'C':
237 	  if (optarg)
238 	    {
239 	      sym_id_add (optarg, INCL_EXEC);
240 	    }
241 	  output_style |= STYLE_EXEC_COUNTS;
242 	  user_specified |= STYLE_EXEC_COUNTS;
243 	  break;
244 	case 'd':
245 	  if (optarg)
246 	    {
247 	      debug_level |= atoi (optarg);
248 	      debug_level |= ANYDEBUG;
249 	    }
250 	  else
251 	    {
252 	      debug_level = ~0;
253 	    }
254 	  DBG (ANYDEBUG, printf ("[main] debug-level=0x%x\n", debug_level));
255 #ifndef DEBUG
256 	  printf (_("%s: debugging not supported; -d ignored\n"), whoami);
257 #endif	/* DEBUG */
258 	  break;
259 	case 'D':
260 	  ignore_non_functions = TRUE;
261 	  break;
262 	case 'E':
263 	  sym_id_add (optarg, EXCL_TIME);
264 	  /* Fall through.  */
265 	case 'e':
266 	  sym_id_add (optarg, EXCL_GRAPH);
267 	  break;
268 	case 'F':
269 	  sym_id_add (optarg, INCL_TIME);
270 	  /* Fall through.  */
271 	case 'f':
272 	  sym_id_add (optarg, INCL_GRAPH);
273 	  break;
274 	case 'g':
275 	  sym_id_add (optarg, EXCL_FLAT);
276 	  break;
277 	case 'G':
278 	  sym_id_add (optarg, INCL_FLAT);
279 	  break;
280 	case 'h':
281 	  usage (stdout, 0);
282 	case 'i':
283 	  output_style |= STYLE_GMON_INFO;
284 	  user_specified |= STYLE_GMON_INFO;
285 	  break;
286 	case 'I':
287 	  search_list_append (&src_search_list, optarg);
288 	  break;
289 	case 'J':
290 	  if (optarg)
291 	    {
292 	      sym_id_add (optarg, EXCL_ANNO);
293 	      output_style |= STYLE_ANNOTATED_SOURCE;
294 	    }
295 	  else
296 	    {
297 	      output_style &= ~STYLE_ANNOTATED_SOURCE;
298 	    }
299 	  user_specified |= STYLE_ANNOTATED_SOURCE;
300 	  break;
301 	case 'k':
302 	  sym_id_add (optarg, EXCL_ARCS);
303 	  break;
304 	case 'l':
305 	  line_granularity = TRUE;
306 	  break;
307 	case 'L':
308 	  print_path = TRUE;
309 	  break;
310 	case 'm':
311 	  bb_min_calls = (unsigned long) strtoul (optarg, (char **) NULL, 10);
312 	  break;
313 	case 'n':
314 	  sym_id_add (optarg, INCL_TIME);
315 	  break;
316 	case 'N':
317 	  sym_id_add (optarg, EXCL_TIME);
318 	  break;
319 	case 'O':
320 	  switch (optarg[0])
321 	    {
322 	    case 'a':
323 	      file_format = FF_AUTO;
324 	      break;
325 	    case 'm':
326 	      file_format = FF_MAGIC;
327 	      break;
328 	    case 'b':
329 	      file_format = FF_BSD;
330 	      break;
331 	    case '4':
332 	      file_format = FF_BSD44;
333 	      break;
334 	    case 'p':
335 	      file_format = FF_PROF;
336 	      break;
337 	    default:
338 	      fprintf (stderr, _("%s: unknown file format %s\n"),
339 		       optarg, whoami);
340 	      done (1);
341 	    }
342 	  break;
343 	case 'p':
344 	  if (optarg)
345 	    {
346 	      sym_id_add (optarg, INCL_FLAT);
347 	    }
348 	  output_style |= STYLE_FLAT_PROFILE;
349 	  user_specified |= STYLE_FLAT_PROFILE;
350 	  break;
351 	case 'P':
352 	  if (optarg)
353 	    {
354 	      sym_id_add (optarg, EXCL_FLAT);
355 	      output_style |= STYLE_FLAT_PROFILE;
356 	    }
357 	  else
358 	    {
359 	      output_style &= ~STYLE_FLAT_PROFILE;
360 	    }
361 	  user_specified |= STYLE_FLAT_PROFILE;
362 	  break;
363 	case 'q':
364 	  if (optarg)
365 	    {
366 	      if (strchr (optarg, '/'))
367 		{
368 		  sym_id_add (optarg, INCL_ARCS);
369 		}
370 	      else
371 		{
372 		  sym_id_add (optarg, INCL_GRAPH);
373 		}
374 	    }
375 	  output_style |= STYLE_CALL_GRAPH;
376 	  user_specified |= STYLE_CALL_GRAPH;
377 	  break;
378 	case 'r':
379 	  output_style |= STYLE_FUNCTION_ORDER;
380 	  user_specified |= STYLE_FUNCTION_ORDER;
381 	  break;
382 	case 'R':
383 	  output_style |= STYLE_FILE_ORDER;
384 	  user_specified |= STYLE_FILE_ORDER;
385 	  function_mapping_file = optarg;
386 	  break;
387 	case 'Q':
388 	  if (optarg)
389 	    {
390 	      if (strchr (optarg, '/'))
391 		{
392 		  sym_id_add (optarg, EXCL_ARCS);
393 		}
394 	      else
395 		{
396 		  sym_id_add (optarg, EXCL_GRAPH);
397 		}
398 	      output_style |= STYLE_CALL_GRAPH;
399 	    }
400 	  else
401 	    {
402 	      output_style &= ~STYLE_CALL_GRAPH;
403 	    }
404 	  user_specified |= STYLE_CALL_GRAPH;
405 	  break;
406 	case 's':
407 	  output_style |= STYLE_SUMMARY_FILE;
408 	  user_specified |= STYLE_SUMMARY_FILE;
409 	  break;
410 	case 'S':
411 	  external_symbol_table = optarg;
412 	  DBG (AOUTDEBUG, printf ("external-symbol-table: %s\n", optarg));
413 	  break;
414 	case 't':
415 	  bb_table_length = atoi (optarg);
416 	  if (bb_table_length < 0)
417 	    {
418 	      bb_table_length = 0;
419 	    }
420 	  break;
421 	case 'T':
422 	  bsd_style_output = TRUE;
423 	  break;
424 	case 'v':
425 	  /* This output is intended to follow the GNU standards document.  */
426 	  printf (_("GNU gprof %s\n"), BFD_VERSION_STRING);
427 	  printf (_("Based on BSD gprof, copyright 1983 Regents of the University of California.\n"));
428 	  printf (_("\
429 This program is free software.  This program has absolutely no warranty.\n"));
430 	  done (0);
431 	case 'w':
432 	  output_width = atoi (optarg);
433 	  if (output_width < 1)
434 	    {
435 	      output_width = 1;
436 	    }
437 	  break;
438 	case 'x':
439 	  bb_annotate_all_lines = TRUE;
440 	  break;
441 	case 'y':
442 	  create_annotation_files = TRUE;
443 	  break;
444 	case 'z':
445 	  ignore_zeros = FALSE;
446 	  break;
447 	case 'Z':
448 	  if (optarg)
449 	    {
450 	      sym_id_add (optarg, EXCL_EXEC);
451 	      output_style |= STYLE_EXEC_COUNTS;
452 	    }
453 	  else
454 	    {
455 	      output_style &= ~STYLE_EXEC_COUNTS;
456 	    }
457 	  user_specified |= STYLE_ANNOTATED_SOURCE;
458 	  break;
459 	case OPTION_DEMANGLE:
460 	  demangle = TRUE;
461 	  if (optarg != NULL)
462 	    {
463 	      enum demangling_styles style;
464 
465 	      style = cplus_demangle_name_to_style (optarg);
466 	      if (style == unknown_demangling)
467 		{
468 		  fprintf (stderr,
469 			   _("%s: unknown demangling style `%s'\n"),
470 			   whoami, optarg);
471 		  xexit (1);
472 		}
473 
474 	      cplus_demangle_set_style (style);
475 	   }
476 	  break;
477 	case OPTION_NO_DEMANGLE:
478 	  demangle = FALSE;
479 	  break;
480 	case OPTION_INLINE_FILE_NAMES:
481 	  inline_file_names = TRUE;
482 	  break;
483 	default:
484 	  usage (stderr, 1);
485 	}
486     }
487 
488   /* Don't allow both ordering options, they modify the arc data in-place.  */
489   if ((user_specified & STYLE_FUNCTION_ORDER)
490       && (user_specified & STYLE_FILE_ORDER))
491     {
492       fprintf (stderr,_("\
493 %s: Only one of --function-ordering and --file-ordering may be specified.\n"),
494 	       whoami);
495       done (1);
496     }
497 
498   /* --sum implies --line, otherwise we'd lose basic block counts in
499        gmon.sum */
500   if (output_style & STYLE_SUMMARY_FILE)
501     line_granularity = 1;
502 
503   /* append value of GPROF_PATH to source search list if set: */
504   str = (char *) getenv ("GPROF_PATH");
505   if (str)
506     search_list_append (&src_search_list, str);
507 
508   if (optind < argc)
509     a_out_name = argv[optind++];
510 
511   if (optind < argc)
512     gmon_name = argv[optind++];
513 
514   /* Turn off default functions.  */
515   for (sp = &default_excluded_list[0]; *sp; sp++)
516     {
517       sym_id_add (*sp, EXCL_TIME);
518       sym_id_add (*sp, EXCL_GRAPH);
519       sym_id_add (*sp, EXCL_FLAT);
520     }
521 
522   /* Read symbol table from core file.  */
523   core_init (a_out_name);
524 
525   /* If we should ignore direct function calls, we need to load to
526      core's text-space.  */
527   if (ignore_direct_calls)
528     core_get_text_space (core_bfd);
529 
530   /* Create symbols from core image.  */
531   if (external_symbol_table)
532     core_create_syms_from (external_symbol_table);
533   else if (line_granularity)
534     core_create_line_syms ();
535   else
536     core_create_function_syms ();
537 
538   /* Translate sym specs into syms.  */
539   sym_id_parse ();
540 
541   if (file_format == FF_PROF)
542     {
543       fprintf (stderr,
544 	       _("%s: sorry, file format `prof' is not yet supported\n"),
545 	       whoami);
546       done (1);
547     }
548   else
549     {
550       /* Get information about gmon.out file(s).  */
551       do
552 	{
553 	  gmon_out_read (gmon_name);
554 	  if (optind < argc)
555 	    gmon_name = argv[optind];
556 	}
557       while (optind++ < argc);
558     }
559 
560   /* If user did not specify output style, try to guess something
561      reasonable.  */
562   if (output_style == 0)
563     {
564       if (gmon_input & (INPUT_HISTOGRAM | INPUT_CALL_GRAPH))
565 	{
566 	  if (gmon_input & INPUT_HISTOGRAM)
567 	    output_style |= STYLE_FLAT_PROFILE;
568 	  if (gmon_input & INPUT_CALL_GRAPH)
569 	    output_style |= STYLE_CALL_GRAPH;
570 	}
571       else
572 	output_style = STYLE_EXEC_COUNTS;
573 
574       output_style &= ~user_specified;
575     }
576 
577   /* Dump a gmon.sum file if requested (before any other
578      processing!)  */
579   if (output_style & STYLE_SUMMARY_FILE)
580     {
581       gmon_out_write (GMONSUM);
582     }
583 
584   if (gmon_input & INPUT_HISTOGRAM)
585     {
586       hist_assign_samples ();
587     }
588 
589   if (gmon_input & INPUT_CALL_GRAPH)
590     {
591       cg = cg_assemble ();
592     }
593 
594   /* Do some simple sanity checks.  */
595   if ((output_style & STYLE_FLAT_PROFILE)
596       && !(gmon_input & INPUT_HISTOGRAM))
597     {
598       fprintf (stderr, _("%s: gmon.out file is missing histogram\n"), whoami);
599       done (1);
600     }
601 
602   if ((output_style & STYLE_CALL_GRAPH) && !(gmon_input & INPUT_CALL_GRAPH))
603     {
604       fprintf (stderr,
605 	       _("%s: gmon.out file is missing call-graph data\n"), whoami);
606       done (1);
607     }
608 
609   /* Output whatever user whishes to see.  */
610   if (cg && (output_style & STYLE_CALL_GRAPH) && bsd_style_output)
611     {
612       /* Print the dynamic profile.  */
613       cg_print (cg);
614     }
615 
616   if (output_style & STYLE_FLAT_PROFILE)
617     {
618       /* Print the flat profile.  */
619       hist_print ();
620     }
621 
622   if (cg && (output_style & STYLE_CALL_GRAPH))
623     {
624       if (!bsd_style_output)
625 	{
626 	  /* Print the dynamic profile.  */
627 	  cg_print (cg);
628 	}
629       cg_print_index ();
630     }
631 
632   if (output_style & STYLE_EXEC_COUNTS)
633     print_exec_counts ();
634 
635   if (output_style & STYLE_ANNOTATED_SOURCE)
636     print_annotated_source ();
637 
638   if (output_style & STYLE_FUNCTION_ORDER)
639     cg_print_function_ordering ();
640 
641   if (output_style & STYLE_FILE_ORDER)
642     cg_print_file_ordering ();
643 
644   return 0;
645 }
646 
647 void
648 done (int status)
649 {
650   exit (status);
651 }
652