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 *);
55 static struct gmonhdr gmonhdr;
72 while ( *argv != 0 && **argv == '-' ) {
83 cyclethreshold = atoi( *++argv );
88 debug |= atoi( *++argv );
91 printf("[main] debug = %d\n", debug);
92 # else /* not DEBUG */
93 printf("gprof: -d ignored\n");
98 addlist( Elist , *argv );
100 addlist( elist , *argv );
104 addlist( elist , *++argv );
109 addlist( Flist , *argv );
111 addlist( flist , *argv );
115 addlist( flist , *++argv );
119 addlist( kfromlist , *++argv );
120 addlist( ktolist , *++argv );
150 a_outname = A_OUTNAME;
156 gmonname = (char *) malloc(strlen(a_outname)+6);
157 strcpy(gmonname, a_outname);
158 strcat(gmonname, ".gmon");
161 * get information from the executable file.
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);
170 qsort(nl, nname, sizeof(nltype), valcmp);
172 * turn off default functions
174 for ( sp = defaultEs ; *sp ; sp++ ) {
176 addlist( Elist , *sp );
178 addlist( elist , *sp );
181 * get information about mon.out file(s).
184 getpfile( gmonname );
188 } while ( *argv++ != 0 );
190 * how many ticks per second?
191 * if we can't tell, report time in ticks.
195 fprintf(stderr, "time is in ticks, not seconds\n");
198 * dump out a gmon.sum file if requested
204 * assign samples to procedures
208 * assemble the dynamic profile
210 timesortnlp = doarcs();
212 * print the dynamic profile
215 printgprof( timesortnlp );
218 * print the flat profile
231 * information from a gmon.out file is in two parts:
232 * an array of sampling hits within pc ranges,
243 pfile = openpfile(filename);
246 * the rest of the file consists of
247 * a bunch of <from,self,count> tuples.
249 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
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 );
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);
280 if ( gmonhdr.version == GMONVERSION ) {
281 rate = gmonhdr.profrate;
282 size = sizeof(struct gmonhdr);
284 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
285 size = sizeof(struct ophdr);
286 gmonhdr.profrate = rate = hertz();
287 gmonhdr.version = GMONVERSION;
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;
299 histcounter_type = gmonhdr.histcounter_type;
300 histcounter_size = abs(histcounter_type) / CHAR_BIT;
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;
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" ,
316 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
317 sampbytes , nsamples );
318 printf( "[openpfile] sample rate %ld\n" , hz );
331 parentp = nllookup( rawp -> raw_frompc );
332 childp = nllookup( rawp -> raw_selfpc );
333 if ( parentp == 0 || childp == 0 )
336 && onlist( kfromlist , parentp -> name )
337 && onlist( ktolist , childp -> name ) ) {
340 childp -> ncall += rawp -> raw_count;
342 if ( debug & TALLYDEBUG ) {
343 printf( "[tally] arc from %s to %s traversed %ld times\n" ,
344 parentp -> name , childp -> name , rawp -> raw_count );
347 addarc( parentp , childp , rawp -> raw_count );
351 * dump out the gmon.sum file
357 register nltype *nlp;
358 register arctype *arcp;
362 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL )
363 err( 1 , "%s" , sumfile );
365 * dump the header; use the last header read in
367 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 )
368 err( 1 , "%s" , sumfile );
372 if (fwrite(samples, histcounter_size, nsamples, sfile) != nsamples)
373 err( 1 , "%s" , sumfile );
375 * dump the normalized raw arc information
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 );
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 );
400 const nltype *p1 = (const nltype *)v1;
401 const nltype *p2 = (const nltype *)v2;
403 if ( p1 -> value < p2 -> value ) {
406 if ( p1 -> value > p2 -> value ) {
420 samples = (double *) calloc(nsamples, sizeof(double));
422 errx(0, "no room for %d sample pc's", nsamples);
424 for (i = 0; i < nsamples; i++) {
425 fread(&sample, histcounter_size, 1, pfile);
428 switch ( histcounter_type ) {
430 samples[i] += *(int8_t *)&sample;
433 samples[i] += *(u_int8_t *)&sample;
436 samples[i] += *(int16_t *)&sample;
439 samples[i] += *(u_int16_t *)&sample;
442 samples[i] += *(int32_t *)&sample;
445 samples[i] += *(u_int32_t *)&sample;
448 samples[i] += *(int64_t *)&sample;
451 samples[i] += *(u_int64_t *)&sample;
454 err(1, "unsupported histogram counter type %d", histcounter_type);
458 errx(1, "unexpected EOF after reading %d/%d samples", --i , nsamples );
462 * Assign samples to the procedures to which they belong.
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.
474 * +-----------------------------------------------+
476 * | ->| |<- ->| |<- ->| |<- |
478 * +---------+ +---------+ +---------+
482 * pcl pch pcl pch pcl pch
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).
499 unsigned long pcl, pch;
501 unsigned long overlap;
502 unsigned long svalue0, svalue1;
504 /* read samples and assign to namelist symbols */
505 scale = highpc - lowpc;
508 for (i = 0, j = 1; i < nsamples; i++) {
512 pcl = lowpc + (unsigned long)(scale * i);
513 pch = lowpc + (unsigned long)(scale * (i + 1));
516 if ( debug & SAMPLEDEBUG ) {
517 printf( "[asgnsamples] pcl 0x%lx pch 0x%lx ccnt %.0f\n" ,
522 for (j = j - 1; j < nname; j++) {
523 svalue0 = nl[j].svalue;
524 svalue1 = nl[j+1].svalue;
526 * if high end of tick is below entry address,
532 * if low end of tick into next routine,
533 * go for next routine.
537 overlap = min(pch, svalue1) - max(pcl, svalue0);
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);
547 nl[j].time += overlap * time / scale;
552 if (debug & SAMPLEDEBUG) {
553 printf("[asgnsamples] totime %f\n", totime);
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.
586 register struct nl *nlp;
587 unsigned long bucket_of_entry;
588 unsigned long bucket_of_code;
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 -
595 if (bucket_of_entry < bucket_of_code) {
597 if (debug & SAMPLEDEBUG) {
598 printf("[alignentries] pushing svalue 0x%lx to 0x%lx\n",
600 nlp->svalue + OFFSET_OF_CODE / HISTORICAL_SCALE_2);
603 nlp->svalue += OFFSET_OF_CODE / HISTORICAL_SCALE_2;