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