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