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 * 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.
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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1983, 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)gprof.c 8.1 (Berkeley) 6/6/93";
42 #include <sys/cdefs.h>
43 __FBSDID("$FreeBSD$");
52 static int valcmp(const void *, const void *);
54 static struct gmonhdr gmonhdr;
59 main(int argc, char **argv)
69 while ( *argv != 0 && **argv == '-' ) {
80 cyclethreshold = atoi( *++argv );
85 debug |= atoi( *++argv );
88 printf("[main] debug = %d\n", debug);
89 # else /* not DEBUG */
90 printf("gprof: -d ignored\n");
95 addlist( Elist , *argv );
97 addlist( elist , *argv );
101 addlist( elist , *++argv );
106 addlist( Flist , *argv );
108 addlist( flist , *argv );
112 addlist( flist , *++argv );
116 addlist( kfromlist , *++argv );
117 addlist( ktolist , *++argv );
147 a_outname = A_OUTNAME;
153 gmonname = (char *) malloc(strlen(a_outname)+6);
154 strcpy(gmonname, a_outname);
155 strcat(gmonname, ".gmon");
158 * get information from the executable file.
160 if ((Kflag && kernel_getnfile(a_outname, &defaultEs) == -1) ||
161 (!Kflag && elf_getnfile(a_outname, &defaultEs) == -1 &&
162 aout_getnfile(a_outname, &defaultEs) == -1))
163 errx(1, "%s: bad format", a_outname);
167 qsort(nl, nname, sizeof(nltype), valcmp);
169 * turn off default functions
171 for ( sp = defaultEs ; *sp ; sp++ ) {
173 addlist( Elist , *sp );
175 addlist( elist , *sp );
178 * get information about mon.out file(s).
181 getpfile( gmonname );
185 } while ( *argv++ != 0 );
187 * how many ticks per second?
188 * if we can't tell, report time in ticks.
192 fprintf(stderr, "time is in ticks, not seconds\n");
195 * dump out a gmon.sum file if requested
201 * assign samples to procedures
205 * assemble the dynamic profile
207 timesortnlp = doarcs();
209 * print the dynamic profile
212 printgprof( timesortnlp );
215 * print the flat profile
228 * information from a gmon.out file is in two parts:
229 * an array of sampling hits within pc ranges,
233 getpfile(char *filename)
238 pfile = openpfile(filename);
241 * the rest of the file consists of
242 * a bunch of <from,self,count> tuples.
244 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
246 if ( debug & SAMPLEDEBUG ) {
247 printf( "[getpfile] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
248 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
260 openpfile(char *filename)
267 if((pfile = fopen(filename, "r")) == NULL)
268 err(1, "%s", filename);
269 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
270 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
271 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) )
272 errx(1, "%s: incompatible with first gmon file", filename);
274 if ( gmonhdr.version == GMONVERSION ) {
275 rate = gmonhdr.profrate;
276 size = sizeof(struct gmonhdr);
278 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
279 size = sizeof(struct ophdr);
280 gmonhdr.profrate = rate = hertz();
281 gmonhdr.version = GMONVERSION;
285 } else if (hz != rate)
286 errx(0, "%s: profile clock rate (%d) %s (%ld) in first gmon file",
287 filename, rate, "incompatible with clock rate", hz);
288 if ( gmonhdr.histcounter_type == 0 ) {
289 /* Historical case. The type was u_short (2 bytes in practice). */
290 histcounter_type = 16;
291 histcounter_size = 2;
293 histcounter_type = gmonhdr.histcounter_type;
294 histcounter_size = abs(histcounter_type) / CHAR_BIT;
296 s_lowpc = (unsigned long) gmonhdr.lpc;
297 s_highpc = (unsigned long) gmonhdr.hpc;
298 lowpc = (unsigned long)gmonhdr.lpc / HISTORICAL_SCALE_2;
299 highpc = (unsigned long)gmonhdr.hpc / HISTORICAL_SCALE_2;
300 sampbytes = gmonhdr.ncnt - size;
301 nsamples = sampbytes / histcounter_size;
303 if ( debug & SAMPLEDEBUG ) {
304 printf( "[openpfile] hdr.lpc 0x%lx hdr.hpc 0x%lx hdr.ncnt %d\n",
305 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
306 printf( "[openpfile] s_lowpc 0x%lx s_highpc 0x%lx\n" ,
307 s_lowpc , s_highpc );
308 printf( "[openpfile] lowpc 0x%lx highpc 0x%lx\n" ,
310 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
311 sampbytes , nsamples );
312 printf( "[openpfile] sample rate %ld\n" , hz );
319 tally(struct rawarc *rawp)
324 parentp = nllookup( rawp -> raw_frompc );
325 childp = nllookup( rawp -> raw_selfpc );
326 if ( parentp == 0 || childp == 0 )
329 && onlist( kfromlist , parentp -> name )
330 && onlist( ktolist , childp -> name ) ) {
333 childp -> ncall += rawp -> raw_count;
335 if ( debug & TALLYDEBUG ) {
336 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
337 parentp -> name , childp -> name , rawp -> raw_count );
340 addarc( parentp , childp , rawp -> raw_count );
344 * dump out the gmon.sum file
347 dumpsum(const char *sumfile)
349 register nltype *nlp;
350 register arctype *arcp;
354 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
355 err( 1 , "%s" , sumfile );
357 * dump the header; use the last header read in
359 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
360 err( 1 , "%s" , sumfile );
364 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
365 err( 1 , "%s" , sumfile );
367 * dump the normalized raw arc information
369 for ( nlp = nl ; nlp < npe ; nlp++ ) {
370 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
371 arc.raw_frompc = arcp -> arc_parentp -> value;
372 arc.raw_selfpc = arcp -> arc_childp -> value;
373 arc.raw_count = arcp -> arc_count;
374 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 )
375 err( 1 , "%s" , sumfile );
377 if ( debug & SAMPLEDEBUG ) {
378 printf( "[dumpsum] frompc 0x%lx selfpc 0x%lx count %ld\n" ,
379 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
388 valcmp(const void *v1, const void *v2)
390 const nltype *p1 = (const nltype *)v1;
391 const nltype *p2 = (const nltype *)v2;
393 if ( p1 -> value < p2 -> value ) {
396 if ( p1 -> value > p2 -> value ) {
403 readsamples(FILE *pfile)
409 samples = (double *) calloc(nsamples, sizeof(double));
411 errx(0, "no room for %d sample pc's", nsamples);
413 for (i = 0; i < nsamples; i++) {
414 fread(&sample, histcounter_size, 1, pfile);
417 switch ( histcounter_type ) {
419 samples[i] += *(int8_t *)&sample;
422 samples[i] += *(u_int8_t *)&sample;
425 samples[i] += *(int16_t *)&sample;
428 samples[i] += *(u_int16_t *)&sample;
431 samples[i] += *(int32_t *)&sample;
434 samples[i] += *(u_int32_t *)&sample;
437 samples[i] += *(int64_t *)&sample;
440 samples[i] += *(u_int64_t *)&sample;
443 err(1, "unsupported histogram counter type %d", histcounter_type);
447 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
451 * Assign samples to the procedures to which they belong.
453 * There are three cases as to where pcl and pch can be
454 * with respect to the routine entry addresses svalue0 and svalue1
455 * as shown in the following diagram. overlap computes the
456 * distance between the arrows, the fraction of the sample
457 * that is to be credited to the routine which starts at svalue0.
463 * +-----------------------------------------------+
465 * | ->| |<- ->| |<- ->| |<- |
467 * +---------+ +---------+ +---------+
471 * pcl pch pcl pch pcl pch
473 * For the vax we assert that samples will never fall in the first
474 * two bytes of any routine, since that is the entry mask,
475 * thus we give call alignentries() to adjust the entry points if
476 * the entry mask falls in one bucket but the code for the routine
477 * doesn't start until the next bucket. In conjunction with the
478 * alignment of routine addresses, this should allow us to have
479 * only one sample for every four bytes of text space and never
480 * have any overlap (the two end cases, above).
488 unsigned long pcl, pch;
490 unsigned long overlap;
491 unsigned long svalue0, svalue1;
493 /* read samples and assign to namelist symbols */
494 scale = highpc - lowpc;
497 for (i = 0, j = 1; i < nsamples; i++) {
501 pcl = lowpc + (unsigned long)(scale * i);
502 pch = lowpc + (unsigned long)(scale * (i + 1));
505 if ( debug & SAMPLEDEBUG ) {
506 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
511 for (j = j - 1; j < nname; j++) {
512 svalue0 = nl[j].svalue;
513 svalue1 = nl[j+1].svalue;
515 * if high end of tick is below entry address,
521 * if low end of tick into next routine,
522 * go for next routine.
526 overlap = min(pch, svalue1) - max(pcl, svalue0);
529 if (debug & SAMPLEDEBUG) {
530 printf("[asgnsamples] (0x%lx->0x%lx-0x%lx) %s gets %f ticks %lu overlap\n",
531 nl[j].value / HISTORICAL_SCALE_2,
532 svalue0, svalue1, nl[j].name,
533 overlap * thetime / scale, overlap);
536 nl[j].time += overlap * thetime / scale;
541 if (debug & SAMPLEDEBUG) {
542 printf("[asgnsamples] totime %f\n", totime);
549 min(unsigned long a, unsigned long b)
557 max(unsigned long a, unsigned long b)
565 * calculate scaled entry point addresses (to save time in asgnsamples),
566 * and possibly push the scaled entry points over the entry mask,
567 * if it turns out that the entry point is in one bucket and the code
568 * for a routine is in the next bucket.
573 register struct nl *nlp;
574 unsigned long bucket_of_entry;
575 unsigned long bucket_of_code;
577 for (nlp = nl; nlp < npe; nlp++) {
578 nlp -> svalue = nlp -> value / HISTORICAL_SCALE_2;
579 bucket_of_entry = (nlp->svalue - lowpc) / scale;
580 bucket_of_code = (nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2 -
582 if (bucket_of_entry < bucket_of_code) {
584 if (debug & SAMPLEDEBUG) {
585 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
587 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
590 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;