1 /* 2 * Copyright (c) 1983 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 char copyright[] = 10 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 11 All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)gprof.c 5.8 (Berkeley) 02/19/92"; 16 #endif /* not lint */ 17 18 #include "gprof.h" 19 20 char *whoami = "gprof"; 21 22 /* 23 * things which get -E excluded by default. 24 */ 25 char *defaultEs[] = { "mcount" , "__mcleanup" , 0 }; 26 27 main(argc, argv) 28 int argc; 29 char **argv; 30 { 31 char **sp; 32 nltype **timesortnlp; 33 34 --argc; 35 argv++; 36 debug = 0; 37 bflag = TRUE; 38 while ( *argv != 0 && **argv == '-' ) { 39 (*argv)++; 40 switch ( **argv ) { 41 case 'a': 42 aflag = TRUE; 43 break; 44 case 'b': 45 bflag = FALSE; 46 break; 47 case 'c': 48 #if defined(vax) || defined(tahoe) 49 cflag = TRUE; 50 #else 51 fprintf(stderr, "gprof: -c isn't supported on this architecture yet\n"); 52 exit(1); 53 #endif 54 break; 55 case 'd': 56 dflag = TRUE; 57 (*argv)++; 58 debug |= atoi( *argv ); 59 debug |= ANYDEBUG; 60 # ifdef DEBUG 61 printf("[main] debug = %d\n", debug); 62 # else not DEBUG 63 printf("%s: -d ignored\n", whoami); 64 # endif DEBUG 65 break; 66 case 'E': 67 ++argv; 68 addlist( Elist , *argv ); 69 Eflag = TRUE; 70 addlist( elist , *argv ); 71 eflag = TRUE; 72 break; 73 case 'e': 74 addlist( elist , *++argv ); 75 eflag = TRUE; 76 break; 77 case 'F': 78 ++argv; 79 addlist( Flist , *argv ); 80 Fflag = TRUE; 81 addlist( flist , *argv ); 82 fflag = TRUE; 83 break; 84 case 'f': 85 addlist( flist , *++argv ); 86 fflag = TRUE; 87 break; 88 case 'k': 89 addlist( kfromlist , *++argv ); 90 addlist( ktolist , *++argv ); 91 kflag = TRUE; 92 break; 93 case 's': 94 sflag = TRUE; 95 break; 96 case 'z': 97 zflag = TRUE; 98 break; 99 } 100 argv++; 101 } 102 if ( *argv != 0 ) { 103 a_outname = *argv; 104 argv++; 105 } else { 106 a_outname = A_OUTNAME; 107 } 108 if ( *argv != 0 ) { 109 gmonname = *argv; 110 argv++; 111 } else { 112 gmonname = GMONNAME; 113 } 114 /* 115 * turn off default functions 116 */ 117 for ( sp = &defaultEs[0] ; *sp ; sp++ ) { 118 Eflag = TRUE; 119 addlist( Elist , *sp ); 120 eflag = TRUE; 121 addlist( elist , *sp ); 122 } 123 /* 124 * how many ticks per second? 125 * if we can't tell, report time in ticks. 126 */ 127 hz = hertz(); 128 if (hz == 0) { 129 hz = 1; 130 fprintf(stderr, "time is in ticks, not seconds\n"); 131 } 132 /* 133 * get information about a.out file. 134 */ 135 getnfile(); 136 /* 137 * get information about mon.out file(s). 138 */ 139 do { 140 getpfile( gmonname ); 141 if ( *argv != 0 ) { 142 gmonname = *argv; 143 } 144 } while ( *argv++ != 0 ); 145 /* 146 * dump out a gmon.sum file if requested 147 */ 148 if ( sflag ) { 149 dumpsum( GMONSUM ); 150 } 151 /* 152 * assign samples to procedures 153 */ 154 asgnsamples(); 155 /* 156 * assemble the dynamic profile 157 */ 158 timesortnlp = doarcs(); 159 /* 160 * print the dynamic profile 161 */ 162 printgprof( timesortnlp ); 163 /* 164 * print the flat profile 165 */ 166 printprof(); 167 /* 168 * print the index 169 */ 170 printindex(); 171 done(); 172 } 173 174 /* 175 * Set up string and symbol tables from a.out. 176 * and optionally the text space. 177 * On return symbol table is sorted by value. 178 */ 179 getnfile() 180 { 181 FILE *nfile; 182 int valcmp(); 183 184 nfile = fopen( a_outname ,"r"); 185 if (nfile == NULL) { 186 perror( a_outname ); 187 done(); 188 } 189 fread(&xbuf, 1, sizeof(xbuf), nfile); 190 if (N_BADMAG(xbuf)) { 191 fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname ); 192 done(); 193 } 194 getstrtab(nfile); 195 getsymtab(nfile); 196 gettextspace( nfile ); 197 qsort(nl, nname, sizeof(nltype), valcmp); 198 fclose(nfile); 199 # ifdef DEBUG 200 if ( debug & AOUTDEBUG ) { 201 register int j; 202 203 for (j = 0; j < nname; j++){ 204 printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name); 205 } 206 } 207 # endif DEBUG 208 } 209 210 getstrtab(nfile) 211 FILE *nfile; 212 { 213 214 fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0); 215 if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) { 216 fprintf(stderr, "%s: %s: no string table (old format?)\n" , 217 whoami , a_outname ); 218 done(); 219 } 220 strtab = (char *)calloc(ssiz, 1); 221 if (strtab == NULL) { 222 fprintf(stderr, "%s: %s: no room for %d bytes of string table", 223 whoami , a_outname , ssiz); 224 done(); 225 } 226 if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) { 227 fprintf(stderr, "%s: %s: error reading string table\n", 228 whoami , a_outname ); 229 done(); 230 } 231 } 232 233 /* 234 * Read in symbol table 235 */ 236 getsymtab(nfile) 237 FILE *nfile; 238 { 239 register long i; 240 int askfor; 241 struct nlist nbuf; 242 243 /* pass1 - count symbols */ 244 fseek(nfile, (long)N_SYMOFF(xbuf), 0); 245 nname = 0; 246 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { 247 fread(&nbuf, sizeof(nbuf), 1, nfile); 248 if ( ! funcsymbol( &nbuf ) ) { 249 continue; 250 } 251 nname++; 252 } 253 if (nname == 0) { 254 fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname ); 255 done(); 256 } 257 askfor = nname + 1; 258 nl = (nltype *) calloc( askfor , sizeof(nltype) ); 259 if (nl == 0) { 260 fprintf(stderr, "%s: No room for %d bytes of symbol table\n", 261 whoami, askfor * sizeof(nltype) ); 262 done(); 263 } 264 265 /* pass2 - read symbols */ 266 fseek(nfile, (long)N_SYMOFF(xbuf), 0); 267 npe = nl; 268 nname = 0; 269 for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { 270 fread(&nbuf, sizeof(nbuf), 1, nfile); 271 if ( ! funcsymbol( &nbuf ) ) { 272 # ifdef DEBUG 273 if ( debug & AOUTDEBUG ) { 274 printf( "[getsymtab] rejecting: 0x%x %s\n" , 275 nbuf.n_type , strtab + nbuf.n_un.n_strx ); 276 } 277 # endif DEBUG 278 continue; 279 } 280 npe->value = nbuf.n_value; 281 npe->name = strtab+nbuf.n_un.n_strx; 282 # ifdef DEBUG 283 if ( debug & AOUTDEBUG ) { 284 printf( "[getsymtab] %d %s 0x%08x\n" , 285 nname , npe -> name , npe -> value ); 286 } 287 # endif DEBUG 288 npe++; 289 nname++; 290 } 291 npe->value = -1; 292 } 293 294 /* 295 * read in the text space of an a.out file 296 */ 297 gettextspace( nfile ) 298 FILE *nfile; 299 { 300 char *malloc(); 301 302 if ( cflag == 0 ) { 303 return; 304 } 305 textspace = (u_char *) malloc( xbuf.a_text ); 306 if ( textspace == 0 ) { 307 fprintf( stderr , "%s: ran out room for %d bytes of text space: " , 308 whoami , xbuf.a_text ); 309 fprintf( stderr , "can't do -c\n" ); 310 return; 311 } 312 (void) fseek( nfile , N_TXTOFF( xbuf ) , 0 ); 313 if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { 314 fprintf( stderr , "%s: couldn't read text space: " , whoami ); 315 fprintf( stderr , "can't do -c\n" ); 316 free( textspace ); 317 textspace = 0; 318 return; 319 } 320 } 321 /* 322 * information from a gmon.out file is in two parts: 323 * an array of sampling hits within pc ranges, 324 * and the arcs. 325 */ 326 getpfile(filename) 327 char *filename; 328 { 329 FILE *pfile; 330 FILE *openpfile(); 331 struct rawarc arc; 332 333 pfile = openpfile(filename); 334 readsamples(pfile); 335 /* 336 * the rest of the file consists of 337 * a bunch of <from,self,count> tuples. 338 */ 339 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) { 340 # ifdef DEBUG 341 if ( debug & SAMPLEDEBUG ) { 342 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" , 343 arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); 344 } 345 # endif DEBUG 346 /* 347 * add this arc 348 */ 349 tally( &arc ); 350 } 351 fclose(pfile); 352 } 353 354 FILE * 355 openpfile(filename) 356 char *filename; 357 { 358 struct hdr tmp; 359 FILE *pfile; 360 361 if((pfile = fopen(filename, "r")) == NULL) { 362 perror(filename); 363 done(); 364 } 365 fread(&tmp, sizeof(struct hdr), 1, pfile); 366 if ( s_highpc != 0 && ( tmp.lowpc != h.lowpc || 367 tmp.highpc != h.highpc || tmp.ncnt != h.ncnt ) ) { 368 fprintf(stderr, "%s: incompatible with first gmon file\n", filename); 369 done(); 370 } 371 h = tmp; 372 s_lowpc = (unsigned long) h.lowpc; 373 s_highpc = (unsigned long) h.highpc; 374 lowpc = (unsigned long)h.lowpc / sizeof(UNIT); 375 highpc = (unsigned long)h.highpc / sizeof(UNIT); 376 sampbytes = h.ncnt - sizeof(struct hdr); 377 nsamples = sampbytes / sizeof (UNIT); 378 # ifdef DEBUG 379 if ( debug & SAMPLEDEBUG ) { 380 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n", 381 h.lowpc , h.highpc , h.ncnt ); 382 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" , 383 s_lowpc , s_highpc ); 384 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" , 385 lowpc , highpc ); 386 printf( "[openpfile] sampbytes %d nsamples %d\n" , 387 sampbytes , nsamples ); 388 } 389 # endif DEBUG 390 return(pfile); 391 } 392 393 tally( rawp ) 394 struct rawarc *rawp; 395 { 396 nltype *parentp; 397 nltype *childp; 398 399 parentp = nllookup( rawp -> raw_frompc ); 400 childp = nllookup( rawp -> raw_selfpc ); 401 if ( parentp == 0 || childp == 0 ) 402 return; 403 if ( kflag 404 && onlist( kfromlist , parentp -> name ) 405 && onlist( ktolist , childp -> name ) ) { 406 return; 407 } 408 childp -> ncall += rawp -> raw_count; 409 # ifdef DEBUG 410 if ( debug & TALLYDEBUG ) { 411 printf( "[tally] arc from %s to %s traversed %d times\n" , 412 parentp -> name , childp -> name , rawp -> raw_count ); 413 } 414 # endif DEBUG 415 addarc( parentp , childp , rawp -> raw_count ); 416 } 417 418 /* 419 * dump out the gmon.sum file 420 */ 421 dumpsum( sumfile ) 422 char *sumfile; 423 { 424 register nltype *nlp; 425 register arctype *arcp; 426 struct rawarc arc; 427 FILE *sfile; 428 429 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) { 430 perror( sumfile ); 431 done(); 432 } 433 /* 434 * dump the header; use the last header read in 435 */ 436 if ( fwrite( &h , sizeof h , 1 , sfile ) != 1 ) { 437 perror( sumfile ); 438 done(); 439 } 440 /* 441 * dump the samples 442 */ 443 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) { 444 perror( sumfile ); 445 done(); 446 } 447 /* 448 * dump the normalized raw arc information 449 */ 450 for ( nlp = nl ; nlp < npe ; nlp++ ) { 451 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) { 452 arc.raw_frompc = arcp -> arc_parentp -> value; 453 arc.raw_selfpc = arcp -> arc_childp -> value; 454 arc.raw_count = arcp -> arc_count; 455 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) { 456 perror( sumfile ); 457 done(); 458 } 459 # ifdef DEBUG 460 if ( debug & SAMPLEDEBUG ) { 461 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" , 462 arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); 463 } 464 # endif DEBUG 465 } 466 } 467 fclose( sfile ); 468 } 469 470 valcmp(p1, p2) 471 nltype *p1, *p2; 472 { 473 if ( p1 -> value < p2 -> value ) { 474 return LESSTHAN; 475 } 476 if ( p1 -> value > p2 -> value ) { 477 return GREATERTHAN; 478 } 479 return EQUALTO; 480 } 481 482 readsamples(pfile) 483 FILE *pfile; 484 { 485 register i; 486 UNIT sample; 487 488 if (samples == 0) { 489 samples = (UNIT *) calloc(sampbytes, sizeof (UNIT)); 490 if (samples == 0) { 491 fprintf( stderr , "%s: No room for %d sample pc's\n", 492 whoami , sampbytes / sizeof (UNIT)); 493 done(); 494 } 495 } 496 for (i = 0; i < nsamples; i++) { 497 fread(&sample, sizeof (UNIT), 1, pfile); 498 if (feof(pfile)) 499 break; 500 samples[i] += sample; 501 } 502 if (i != nsamples) { 503 fprintf(stderr, 504 "%s: unexpected EOF after reading %d/%d samples\n", 505 whoami , --i , nsamples ); 506 done(); 507 } 508 } 509 510 /* 511 * Assign samples to the procedures to which they belong. 512 * 513 * There are three cases as to where pcl and pch can be 514 * with respect to the routine entry addresses svalue0 and svalue1 515 * as shown in the following diagram. overlap computes the 516 * distance between the arrows, the fraction of the sample 517 * that is to be credited to the routine which starts at svalue0. 518 * 519 * svalue0 svalue1 520 * | | 521 * v v 522 * 523 * +-----------------------------------------------+ 524 * | | 525 * | ->| |<- ->| |<- ->| |<- | 526 * | | | | | | 527 * +---------+ +---------+ +---------+ 528 * 529 * ^ ^ ^ ^ ^ ^ 530 * | | | | | | 531 * pcl pch pcl pch pcl pch 532 * 533 * For the vax we assert that samples will never fall in the first 534 * two bytes of any routine, since that is the entry mask, 535 * thus we give call alignentries() to adjust the entry points if 536 * the entry mask falls in one bucket but the code for the routine 537 * doesn't start until the next bucket. In conjunction with the 538 * alignment of routine addresses, this should allow us to have 539 * only one sample for every four bytes of text space and never 540 * have any overlap (the two end cases, above). 541 */ 542 asgnsamples() 543 { 544 register int j; 545 UNIT ccnt; 546 double time; 547 unsigned long pcl, pch; 548 register int i; 549 unsigned long overlap; 550 unsigned long svalue0, svalue1; 551 552 /* read samples and assign to namelist symbols */ 553 scale = highpc - lowpc; 554 scale /= nsamples; 555 alignentries(); 556 for (i = 0, j = 1; i < nsamples; i++) { 557 ccnt = samples[i]; 558 if (ccnt == 0) 559 continue; 560 pcl = lowpc + scale * i; 561 pch = lowpc + scale * (i + 1); 562 time = ccnt; 563 # ifdef DEBUG 564 if ( debug & SAMPLEDEBUG ) { 565 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" , 566 pcl , pch , ccnt ); 567 } 568 # endif DEBUG 569 totime += time; 570 for (j = j - 1; j < nname; j++) { 571 svalue0 = nl[j].svalue; 572 svalue1 = nl[j+1].svalue; 573 /* 574 * if high end of tick is below entry address, 575 * go for next tick. 576 */ 577 if (pch < svalue0) 578 break; 579 /* 580 * if low end of tick into next routine, 581 * go for next routine. 582 */ 583 if (pcl >= svalue1) 584 continue; 585 overlap = min(pch, svalue1) - max(pcl, svalue0); 586 if (overlap > 0) { 587 # ifdef DEBUG 588 if (debug & SAMPLEDEBUG) { 589 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n", 590 nl[j].value/sizeof(UNIT), svalue0, svalue1, 591 nl[j].name, 592 overlap * time / scale, overlap); 593 } 594 # endif DEBUG 595 nl[j].time += overlap * time / scale; 596 } 597 } 598 } 599 # ifdef DEBUG 600 if (debug & SAMPLEDEBUG) { 601 printf("[asgnsamples] totime %f\n", totime); 602 } 603 # endif DEBUG 604 } 605 606 607 unsigned long 608 min(a, b) 609 unsigned long a,b; 610 { 611 if (a<b) 612 return(a); 613 return(b); 614 } 615 616 unsigned long 617 max(a, b) 618 unsigned long a,b; 619 { 620 if (a>b) 621 return(a); 622 return(b); 623 } 624 625 /* 626 * calculate scaled entry point addresses (to save time in asgnsamples), 627 * and possibly push the scaled entry points over the entry mask, 628 * if it turns out that the entry point is in one bucket and the code 629 * for a routine is in the next bucket. 630 */ 631 alignentries() 632 { 633 register struct nl *nlp; 634 unsigned long bucket_of_entry; 635 unsigned long bucket_of_code; 636 637 for (nlp = nl; nlp < npe; nlp++) { 638 nlp -> svalue = nlp -> value / sizeof(UNIT); 639 bucket_of_entry = (nlp->svalue - lowpc) / scale; 640 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale; 641 if (bucket_of_entry < bucket_of_code) { 642 # ifdef DEBUG 643 if (debug & SAMPLEDEBUG) { 644 printf("[alignentries] pushing svalue 0x%x to 0x%x\n", 645 nlp->svalue, nlp->svalue + UNITS_TO_CODE); 646 } 647 # endif DEBUG 648 nlp->svalue += UNITS_TO_CODE; 649 } 650 } 651 } 652 653 bool 654 funcsymbol( nlistp ) 655 struct nlist *nlistp; 656 { 657 extern char *strtab; /* string table from a.out */ 658 extern int aflag; /* if static functions aren't desired */ 659 char *name; 660 661 /* 662 * must be a text symbol, 663 * and static text symbols don't qualify if aflag set. 664 */ 665 if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) ) 666 || ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) { 667 return FALSE; 668 } 669 /* 670 * can't have any `funny' characters in name, 671 * where `funny' includes `.', .o file names 672 * and `$', pascal labels. 673 */ 674 for ( name = strtab + nlistp -> n_un.n_strx ; *name ; name += 1 ) { 675 if ( *name == '.' || *name == '$' ) { 676 return FALSE; 677 } 678 } 679 return TRUE; 680 } 681 682 done() 683 { 684 685 exit(0); 686 } 687