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