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