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