2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
35 static const char copyright[] =
36 "@(#) Copyright (c) 1983, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
42 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
56 static int valcmp(const void *, const void *);
59 static struct gmonhdr gmonhdr;
76 while ( *argv != 0 && **argv == '-' ) {
87 cyclethreshold = atoi( *++argv );
92 debug |= atoi( *++argv );
95 printf("[main] debug = %d\n", debug);
96 # else /* not DEBUG */
97 printf("gprof: -d ignored\n");
102 addlist( Elist , *argv );
104 addlist( elist , *argv );
108 addlist( elist , *++argv );
113 addlist( Flist , *argv );
115 addlist( flist , *argv );
119 addlist( flist , *++argv );
123 addlist( kfromlist , *++argv );
124 addlist( ktolist , *++argv );
154 a_outname = A_OUTNAME;
160 gmonname = (char *) malloc(strlen(a_outname)+6);
161 strcpy(gmonname, a_outname);
162 strcat(gmonname, ".gmon");
165 * get information from the executable file.
167 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) ||
168 (!Kflag && elf_getnfile(a_outname, &defaultEs) == -1 &&
169 aout_getnfile(a_outname, &defaultEs) == -1))
170 errx(1, "%s: bad format", a_outname);
174 qsort(nl, nname, sizeof(nltype), valcmp);
176 * turn off default functions
178 for ( sp = defaultEs ; *sp ; sp++ ) {
180 addlist( Elist , *sp );
182 addlist( elist , *sp );
185 * get information about mon.out file(s).
188 getpfile( gmonname );
192 } while ( *argv++ != 0 );
194 * how many ticks per second?
195 * if we can't tell, report time in ticks.
199 fprintf(stderr, "time is in ticks, not seconds\n");
202 * dump out a gmon.sum file if requested
208 * assign samples to procedures
212 * assemble the dynamic profile
214 timesortnlp = doarcs();
216 * print the dynamic profile
219 printgprof( timesortnlp );
222 * print the flat profile
235 * information from a gmon.out file is in two parts:
236 * an array of sampling hits within pc ranges,
247 pfile = openpfile(filename);
250 * the rest of the file consists of
251 * a bunch of <from,self,count> tuples.
253 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
255 if ( debug & SAMPLEDEBUG ) {
256 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
257 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
277 if((pfile = fopen(filename, "r")) == NULL)
278 err(1, "%s", filename);
279 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
280 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
281 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
282 errx(1, "%s: incompatible with first gmon file", filename);
284 if ( gmonhdr.version == GMONVERSION ) {
285 rate = gmonhdr.profrate;
286 size = sizeof(struct gmonhdr);
288 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
289 size = sizeof(struct ophdr);
290 gmonhdr.profrate = rate = hertz();
291 gmonhdr.version = GMONVERSION;
295 } else if (hz != rate)
296 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
297 filename, rate, "incompatible with clock rate", hz);
298 if ( gmonhdr.histcounter_type == 0 ) {
299 /* Historical case. The type was u_short (2 bytes in practice). */
300 histcounter_type = 16;
301 histcounter_size = 2;
303 histcounter_type = gmonhdr.histcounter_type;
304 histcounter_size = abs(histcounter_type) / CHAR_BIT;
306 s_lowpc = (unsigned long) gmonhdr.lpc;
307 s_highpc = (unsigned long) gmonhdr.hpc;
308 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
309 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
310 sampbytes = gmonhdr.ncnt - size;
311 nsamples = sampbytes / histcounter_size;
313 if ( debug & SAMPLEDEBUG ) {
314 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
315 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
316 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
317 s_lowpc , s_highpc );
318 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
320 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
321 sampbytes , nsamples );
322 printf( "[openpfile] sample rate %ld\n" , hz );
335 parentp = nllookup( rawp -> raw_frompc );
336 childp = nllookup( rawp -> raw_selfpc );
337 if ( parentp == 0 || childp == 0 )
340 && onlist( kfromlist , parentp -> name )
341 && onlist( ktolist , childp -> name ) ) {
344 childp -> ncall += rawp -> raw_count;
346 if ( debug & TALLYDEBUG ) {
347 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
348 parentp -> name , childp -> name , rawp -> raw_count );
351 addarc( parentp , childp , rawp -> raw_count );
355 * dump out the gmon.sum file
361 register nltype *nlp;
362 register arctype *arcp;
366 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
367 err( 1 , "%s" , sumfile );
369 * dump the header; use the last header read in
371 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
372 err( 1 , "%s" , sumfile );
376 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
377 err( 1 , "%s" , sumfile );
379 * dump the normalized raw arc information
381 for ( nlp = nl ; nlp < npe ; nlp++ ) {
382 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
383 arc.raw_frompc = arcp -> arc_parentp -> value;
384 arc.raw_selfpc = arcp -> arc_childp -> value;
385 arc.raw_count = arcp -> arc_count;
386 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 )
387 err( 1 , "%s" , sumfile );
389 if ( debug & SAMPLEDEBUG ) {
390 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
391 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
404 const nltype *p1 = (const nltype *)v1;
405 const nltype *p2 = (const nltype *)v2;
407 if ( p1 -> value < p2 -> value ) {
410 if ( p1 -> value > p2 -> value ) {
424 samples = (double *) calloc(nsamples, sizeof(double));
426 errx(0, "no room for %d sample pc's", nsamples);
428 for (i = 0; i < nsamples; i++) {
429 fread(&sample, histcounter_size, 1, pfile);
432 switch ( histcounter_type ) {
434 samples[i] += *(int8_t *)&sample;
437 samples[i] += *(u_int8_t *)&sample;
440 samples[i] += *(int16_t *)&sample;
443 samples[i] += *(u_int16_t *)&sample;
446 samples[i] += *(int32_t *)&sample;
449 samples[i] += *(u_int32_t *)&sample;
452 samples[i] += *(int64_t *)&sample;
455 samples[i] += *(u_int64_t *)&sample;
458 err(1, "unsupported histogram counter type %d", histcounter_type);
462 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
466 * Assign samples to the procedures to which they belong.
468 * There are three cases as to where pcl and pch can be
469 * with respect to the routine entry addresses svalue0 and svalue1
470 * as shown in the following diagram. overlap computes the
471 * distance between the arrows, the fraction of the sample
472 * that is to be credited to the routine which starts at svalue0.
478 * +-----------------------------------------------+
480 * | ->| |<- ->| |<- ->| |<- |
482 * +---------+ +---------+ +---------+
486 * pcl pch pcl pch pcl pch
488 * For the vax we assert that samples will never fall in the first
489 * two bytes of any routine, since that is the entry mask,
490 * thus we give call alignentries() to adjust the entry points if
491 * the entry mask falls in one bucket but the code for the routine
492 * doesn't start until the next bucket. In conjunction with the
493 * alignment of routine addresses, this should allow us to have
494 * only one sample for every four bytes of text space and never
495 * have any overlap (the two end cases, above).
503 unsigned long pcl, pch;
505 unsigned long overlap;
506 unsigned long svalue0, svalue1;
508 /* read samples and assign to namelist symbols */
509 scale = highpc - lowpc;
512 for (i = 0, j = 1; i < nsamples; i++) {
516 pcl = lowpc + (unsigned long)(scale * i);
517 pch = lowpc + (unsigned long)(scale * (i + 1));
520 if ( debug & SAMPLEDEBUG ) {
521 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
526 for (j = j - 1; j < nname; j++) {
527 svalue0 = nl[j].svalue;
528 svalue1 = nl[j+1].svalue;
530 * if high end of tick is below entry address,
536 * if low end of tick into next routine,
537 * go for next routine.
541 overlap = min(pch, svalue1) - max(pcl, svalue0);
544 if (debug & SAMPLEDEBUG) {
545 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
546 nl[j].value / HISTORICAL_SCALE_2,
547 svalue0, svalue1, nl[j].name,
548 overlap * time / scale, overlap);
551 nl[j].time += overlap * time / scale;
556 if (debug & SAMPLEDEBUG) {
557 printf("[asgnsamples] totime %f\n", totime);
582 * calculate scaled entry point addresses (to save time in asgnsamples),
583 * and possibly push the scaled entry points over the entry mask,
584 * if it turns out that the entry point is in one bucket and the code
585 * for a routine is in the next bucket.
590 register struct nl *nlp;
591 unsigned long bucket_of_entry;
592 unsigned long bucket_of_code;
594 for (nlp = nl; nlp < npe; nlp++) {
595 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
596 bucket_of_entry = (nlp->svalue - lowpc) / scale;
597 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
599 if (bucket_of_entry < bucket_of_code) {
601 if (debug & SAMPLEDEBUG) {
602 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
604 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
607 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;