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