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