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