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