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