gprof.c revision 129657
1/* 2 * Copyright (c) 1983, 1993 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. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#ifndef lint 35static const char copyright[] = 36"@(#) Copyright (c) 1983, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38#endif /* not lint */ 39 40#if 0 41#ifndef lint 42static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93"; 43#endif /* not lint */ 44#endif 45 46#include <sys/cdefs.h> 47__FBSDID("$FreeBSD: head/usr.bin/gprof/gprof.c 129657 2004-05-24 12:44:00Z stefanf $"); 48 49#include <err.h> 50#include <limits.h> 51#include <stdint.h> 52#include <string.h> 53 54#include "gprof.h" 55 56static int valcmp(const void *, const void *); 57 58 59static struct gmonhdr gmonhdr; 60static int lflag; 61static int Lflag; 62 63int 64main(argc, argv) 65 int argc; 66 char **argv; 67{ 68 char **sp; 69 nltype **timesortnlp; 70 char **defaultEs; 71 72 --argc; 73 argv++; 74 debug = 0; 75 bflag = TRUE; 76 while ( *argv != 0 && **argv == '-' ) { 77 (*argv)++; 78 switch ( **argv ) { 79 case 'a': 80 aflag = TRUE; 81 break; 82 case 'b': 83 bflag = FALSE; 84 break; 85 case 'C': 86 Cflag = TRUE; 87 cyclethreshold = atoi( *++argv ); 88 break; 89 case 'c': 90#if 0 91 cflag = TRUE; 92#else 93 errx(1, "-c isn't supported on this architecture yet"); 94#endif 95 break; 96 case 'd': 97 dflag = TRUE; 98 setlinebuf(stdout); 99 debug |= atoi( *++argv ); 100 debug |= ANYDEBUG; 101# ifdef DEBUG 102 printf("[main] debug = %d\n", debug); 103# else /* not DEBUG */ 104 printf("gprof: -d ignored\n"); 105# endif /* DEBUG */ 106 break; 107 case 'E': 108 ++argv; 109 addlist( Elist , *argv ); 110 Eflag = TRUE; 111 addlist( elist , *argv ); 112 eflag = TRUE; 113 break; 114 case 'e': 115 addlist( elist , *++argv ); 116 eflag = TRUE; 117 break; 118 case 'F': 119 ++argv; 120 addlist( Flist , *argv ); 121 Fflag = TRUE; 122 addlist( flist , *argv ); 123 fflag = TRUE; 124 break; 125 case 'f': 126 addlist( flist , *++argv ); 127 fflag = TRUE; 128 break; 129 case 'k': 130 addlist( kfromlist , *++argv ); 131 addlist( ktolist , *++argv ); 132 kflag = TRUE; 133 break; 134 case 'K': 135 Kflag = TRUE; 136 break; 137 case 'l': 138 lflag = 1; 139 Lflag = 0; 140 break; 141 case 'L': 142 Lflag = 1; 143 lflag = 0; 144 break; 145 case 's': 146 sflag = TRUE; 147 break; 148 case 'u': 149 uflag = TRUE; 150 break; 151 case 'z': 152 zflag = TRUE; 153 break; 154 } 155 argv++; 156 } 157 if ( *argv != 0 ) { 158 a_outname = *argv; 159 argv++; 160 } else { 161 a_outname = A_OUTNAME; 162 } 163 if ( *argv != 0 ) { 164 gmonname = *argv; 165 argv++; 166 } else { 167 gmonname = (char *) malloc(strlen(a_outname)+6); 168 strcpy(gmonname, a_outname); 169 strcat(gmonname, ".gmon"); 170 } 171 /* 172 * get information from the executable file. 173 */ 174 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) || 175 (elf_getnfile(a_outname, &defaultEs) == -1 && 176 aout_getnfile(a_outname, &defaultEs) == -1)) 177 errx(1, "%s: bad format", a_outname); 178 /* 179 * sort symbol table. 180 */ 181 qsort(nl, nname, sizeof(nltype), valcmp); 182 /* 183 * turn off default functions 184 */ 185 for ( sp = defaultEs ; *sp ; sp++ ) { 186 Eflag = TRUE; 187 addlist( Elist , *sp ); 188 eflag = TRUE; 189 addlist( elist , *sp ); 190 } 191 /* 192 * get information about mon.out file(s). 193 */ 194 do { 195 getpfile( gmonname ); 196 if ( *argv != 0 ) { 197 gmonname = *argv; 198 } 199 } while ( *argv++ != 0 ); 200 /* 201 * how many ticks per second? 202 * if we can't tell, report time in ticks. 203 */ 204 if (hz == 0) { 205 hz = 1; 206 fprintf(stderr, "time is in ticks, not seconds\n"); 207 } 208 /* 209 * dump out a gmon.sum file if requested 210 */ 211 if ( sflag ) { 212 dumpsum( GMONSUM ); 213 } 214 /* 215 * assign samples to procedures 216 */ 217 asgnsamples(); 218 /* 219 * assemble the dynamic profile 220 */ 221 timesortnlp = doarcs(); 222 /* 223 * print the dynamic profile 224 */ 225 if(!lflag) { 226 printgprof( timesortnlp ); 227 } 228 /* 229 * print the flat profile 230 */ 231 if(!Lflag) { 232 printprof(); 233 } 234 /* 235 * print the index 236 */ 237 printindex(); 238 exit(0); 239} 240 241 /* 242 * information from a gmon.out file is in two parts: 243 * an array of sampling hits within pc ranges, 244 * and the arcs. 245 */ 246void 247getpfile(filename) 248 char *filename; 249{ 250 FILE *pfile; 251 FILE *openpfile(); 252 struct rawarc arc; 253 254 pfile = openpfile(filename); 255 readsamples(pfile); 256 /* 257 * the rest of the file consists of 258 * a bunch of <from,self,count> tuples. 259 */ 260 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) { 261# ifdef DEBUG 262 if ( debug & SAMPLEDEBUG ) { 263 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" , 264 arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); 265 } 266# endif /* DEBUG */ 267 /* 268 * add this arc 269 */ 270 tally( &arc ); 271 } 272 fclose(pfile); 273} 274 275FILE * 276openpfile(filename) 277 char *filename; 278{ 279 struct gmonhdr tmp; 280 FILE *pfile; 281 int size; 282 int rate; 283 284 if((pfile = fopen(filename, "r")) == NULL) 285 err(1, "%s", filename); 286 fread(&tmp, sizeof(struct gmonhdr), 1, pfile); 287 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc || 288 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) 289 errx(1, "%s: incompatible with first gmon file", filename); 290 gmonhdr = tmp; 291 if ( gmonhdr.version == GMONVERSION ) { 292 rate = gmonhdr.profrate; 293 size = sizeof(struct gmonhdr); 294 } else { 295 fseek(pfile, sizeof(struct ophdr), SEEK_SET); 296 size = sizeof(struct ophdr); 297 gmonhdr.profrate = rate = hertz(); 298 gmonhdr.version = GMONVERSION; 299 } 300 if (hz == 0) { 301 hz = rate; 302 } else if (hz != rate) 303 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file", 304 filename, rate, "incompatible with clock rate", hz); 305 if ( gmonhdr.histcounter_type == 0 ) { 306 /* Historical case. The type was u_short (2 bytes in practice). */ 307 histcounter_type = 16; 308 histcounter_size = 2; 309 } else { 310 histcounter_type = gmonhdr.histcounter_type; 311 histcounter_size = abs(histcounter_type) / CHAR_BIT; 312 } 313 s_lowpc = (unsigned long) gmonhdr.lpc; 314 s_highpc = (unsigned long) gmonhdr.hpc; 315 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2; 316 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2; 317 sampbytes = gmonhdr.ncnt - size; 318 nsamples = sampbytes / histcounter_size; 319# ifdef DEBUG 320 if ( debug & SAMPLEDEBUG ) { 321 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n", 322 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt ); 323 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" , 324 s_lowpc , s_highpc ); 325 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" , 326 lowpc , highpc ); 327 printf( "[openpfile] sampbytes %d nsamples %d\n" , 328 sampbytes , nsamples ); 329 printf( "[openpfile] sample rate %ld\n" , hz ); 330 } 331# endif /* DEBUG */ 332 return(pfile); 333} 334 335void 336tally( rawp ) 337 struct rawarc *rawp; 338{ 339 nltype *parentp; 340 nltype *childp; 341 342 parentp = nllookup( rawp -> raw_frompc ); 343 childp = nllookup( rawp -> raw_selfpc ); 344 if ( parentp == 0 || childp == 0 ) 345 return; 346 if ( kflag 347 && onlist( kfromlist , parentp -> name ) 348 && onlist( ktolist , childp -> name ) ) { 349 return; 350 } 351 childp -> ncall += rawp -> raw_count; 352# ifdef DEBUG 353 if ( debug & TALLYDEBUG ) { 354 printf( "[tally] arc from %s to %s traversed %ld times\n" , 355 parentp -> name , childp -> name , rawp -> raw_count ); 356 } 357# endif /* DEBUG */ 358 addarc( parentp , childp , rawp -> raw_count ); 359} 360 361/* 362 * dump out the gmon.sum file 363 */ 364void 365dumpsum( sumfile ) 366 char *sumfile; 367{ 368 register nltype *nlp; 369 register arctype *arcp; 370 struct rawarc arc; 371 FILE *sfile; 372 373 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) 374 err( 1 , "%s" , sumfile ); 375 /* 376 * dump the header; use the last header read in 377 */ 378 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) 379 err( 1 , "%s" , sumfile ); 380 /* 381 * dump the samples 382 */ 383 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples) 384 err( 1 , "%s" , sumfile ); 385 /* 386 * dump the normalized raw arc information 387 */ 388 for ( nlp = nl ; nlp < npe ; nlp++ ) { 389 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) { 390 arc.raw_frompc = arcp -> arc_parentp -> value; 391 arc.raw_selfpc = arcp -> arc_childp -> value; 392 arc.raw_count = arcp -> arc_count; 393 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) 394 err( 1 , "%s" , sumfile ); 395# ifdef DEBUG 396 if ( debug & SAMPLEDEBUG ) { 397 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" , 398 arc.raw_frompc , arc.raw_selfpc , arc.raw_count ); 399 } 400# endif /* DEBUG */ 401 } 402 } 403 fclose( sfile ); 404} 405 406static int 407valcmp(v1, v2) 408 const void *v1; 409 const void *v2; 410{ 411 const nltype *p1 = (const nltype *)v1; 412 const nltype *p2 = (const nltype *)v2; 413 414 if ( p1 -> value < p2 -> value ) { 415 return LESSTHAN; 416 } 417 if ( p1 -> value > p2 -> value ) { 418 return GREATERTHAN; 419 } 420 return EQUALTO; 421} 422 423void 424readsamples(pfile) 425 FILE *pfile; 426{ 427 register i; 428 intmax_t sample; 429 430 if (samples == 0) { 431 samples = (double *) calloc(nsamples, sizeof(double)); 432 if (samples == 0) 433 errx(0, "no room for %d sample pc's", nsamples); 434 } 435 for (i = 0; i < nsamples; i++) { 436 fread(&sample, histcounter_size, 1, pfile); 437 if (feof(pfile)) 438 break; 439 switch ( histcounter_type ) { 440 case -8: 441 samples[i] += *(int8_t *)&sample; 442 break; 443 case 8: 444 samples[i] += *(u_int8_t *)&sample; 445 break; 446 case -16: 447 samples[i] += *(int16_t *)&sample; 448 break; 449 case 16: 450 samples[i] += *(u_int16_t *)&sample; 451 break; 452 case -32: 453 samples[i] += *(int32_t *)&sample; 454 break; 455 case 32: 456 samples[i] += *(u_int32_t *)&sample; 457 break; 458 case -64: 459 samples[i] += *(int64_t *)&sample; 460 break; 461 case 64: 462 samples[i] += *(u_int64_t *)&sample; 463 break; 464 default: 465 err(1, "unsupported histogram counter type %d", histcounter_type); 466 } 467 } 468 if (i != nsamples) 469 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples ); 470} 471 472/* 473 * Assign samples to the procedures to which they belong. 474 * 475 * There are three cases as to where pcl and pch can be 476 * with respect to the routine entry addresses svalue0 and svalue1 477 * as shown in the following diagram. overlap computes the 478 * distance between the arrows, the fraction of the sample 479 * that is to be credited to the routine which starts at svalue0. 480 * 481 * svalue0 svalue1 482 * | | 483 * v v 484 * 485 * +-----------------------------------------------+ 486 * | | 487 * | ->| |<- ->| |<- ->| |<- | 488 * | | | | | | 489 * +---------+ +---------+ +---------+ 490 * 491 * ^ ^ ^ ^ ^ ^ 492 * | | | | | | 493 * pcl pch pcl pch pcl pch 494 * 495 * For the vax we assert that samples will never fall in the first 496 * two bytes of any routine, since that is the entry mask, 497 * thus we give call alignentries() to adjust the entry points if 498 * the entry mask falls in one bucket but the code for the routine 499 * doesn't start until the next bucket. In conjunction with the 500 * alignment of routine addresses, this should allow us to have 501 * only one sample for every four bytes of text space and never 502 * have any overlap (the two end cases, above). 503 */ 504void 505asgnsamples() 506{ 507 register int j; 508 double ccnt; 509 double time; 510 unsigned long pcl, pch; 511 register int i; 512 unsigned long overlap; 513 unsigned long svalue0, svalue1; 514 515 /* read samples and assign to namelist symbols */ 516 scale = highpc - lowpc; 517 scale /= nsamples; 518 alignentries(); 519 for (i = 0, j = 1; i < nsamples; i++) { 520 ccnt = samples[i]; 521 if (ccnt == 0) 522 continue; 523 pcl = lowpc + (unsigned long)(scale * i); 524 pch = lowpc + (unsigned long)(scale * (i + 1)); 525 time = ccnt; 526# ifdef DEBUG 527 if ( debug & SAMPLEDEBUG ) { 528 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" , 529 pcl , pch , ccnt ); 530 } 531# endif /* DEBUG */ 532 totime += time; 533 for (j = j - 1; j < nname; j++) { 534 svalue0 = nl[j].svalue; 535 svalue1 = nl[j+1].svalue; 536 /* 537 * if high end of tick is below entry address, 538 * go for next tick. 539 */ 540 if (pch < svalue0) 541 break; 542 /* 543 * if low end of tick into next routine, 544 * go for next routine. 545 */ 546 if (pcl >= svalue1) 547 continue; 548 overlap = min(pch, svalue1) - max(pcl, svalue0); 549 if (overlap > 0) { 550# ifdef DEBUG 551 if (debug & SAMPLEDEBUG) { 552 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n", 553 nl[j].value / HISTORICAL_SCALE_2, 554 svalue0, svalue1, nl[j].name, 555 overlap * time / scale, overlap); 556 } 557# endif /* DEBUG */ 558 nl[j].time += overlap * time / scale; 559 } 560 } 561 } 562# ifdef DEBUG 563 if (debug & SAMPLEDEBUG) { 564 printf("[asgnsamples] totime %f\n", totime); 565 } 566# endif /* DEBUG */ 567} 568 569 570unsigned long 571min(a, b) 572 unsigned long a,b; 573{ 574 if (a<b) 575 return(a); 576 return(b); 577} 578 579unsigned long 580max(a, b) 581 unsigned long a,b; 582{ 583 if (a>b) 584 return(a); 585 return(b); 586} 587 588 /* 589 * calculate scaled entry point addresses (to save time in asgnsamples), 590 * and possibly push the scaled entry points over the entry mask, 591 * if it turns out that the entry point is in one bucket and the code 592 * for a routine is in the next bucket. 593 */ 594void 595alignentries() 596{ 597 register struct nl *nlp; 598 unsigned long bucket_of_entry; 599 unsigned long bucket_of_code; 600 601 for (nlp = nl; nlp < npe; nlp++) { 602 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2; 603 bucket_of_entry = (nlp->svalue - lowpc) / scale; 604 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 - 605 lowpc) / scale; 606 if (bucket_of_entry < bucket_of_code) { 607# ifdef DEBUG 608 if (debug & SAMPLEDEBUG) { 609 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n", 610 nlp->svalue, 611 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2); 612 } 613# endif /* DEBUG */ 614 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2; 615 } 616 } 617} 618