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