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