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";
44 static const char rcsid[] =
45 "$FreeBSD: src/usr.bin/gprof/gprof.c,v 1.11 1999/08/28 01:01:55 peter Exp $";
51 static int valcmp(const void *, const void *);
54 static struct gmonhdr gmonhdr;
70 while ( *argv != 0 && **argv == '-' ) {
81 cyclethreshold = atoi( *++argv );
84 #if defined(vax) || defined(tahoe)
87 errx(1, "-c isn't supported on this architecture yet");
93 debug |= atoi( *++argv );
96 printf("[main] debug = %d\n", debug);
98 printf("gprof: -d ignored\n");
103 addlist( Elist , *argv );
105 addlist( elist , *argv );
109 addlist( elist , *++argv );
114 addlist( Flist , *argv );
116 addlist( flist , *argv );
120 addlist( flist , *++argv );
124 addlist( kfromlist , *++argv );
125 addlist( ktolist , *++argv );
152 a_outname = A_OUTNAME;
158 gmonname = (char *) malloc(strlen(a_outname)+6);
159 strcpy(gmonname, a_outname);
160 strcat(gmonname, ".gmon");
163 * get information from the executable file.
165 if (elf_getnfile(a_outname, &defaultEs) == -1 &&
166 aout_getnfile(a_outname, &defaultEs) == -1)
167 errx(1, "%s: bad format", a_outname);
171 qsort(nl, nname, sizeof(nltype), valcmp);
173 * turn off default functions
175 for ( sp = defaultEs ; *sp ; sp++ ) {
177 addlist( Elist , *sp );
179 addlist( elist , *sp );
182 * get information about mon.out file(s).
185 getpfile( gmonname );
189 } while ( *argv++ != 0 );
191 * how many ticks per second?
192 * if we can't tell, report time in ticks.
196 fprintf(stderr, "time is in ticks, not seconds\n");
199 * dump out a gmon.sum file if requested
205 * assign samples to procedures
209 * assemble the dynamic profile
211 timesortnlp = doarcs();
213 * print the dynamic profile
216 printgprof( timesortnlp );
219 * print the flat profile
232 * information from a gmon.out file is in two parts:
233 * 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%x selfpc 0x%x count %d\n" ,
253 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
273 if((pfile = fopen(filename, "r")) == NULL) {
277 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
278 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
279 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) {
280 warnx("%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) {
297 "%s: profile clock rate (%d) %s (%d) in first gmon file\n",
298 filename, rate, "incompatible with clock rate", hz);
301 s_lowpc = (unsigned long) gmonhdr.lpc;
302 s_highpc = (unsigned long) gmonhdr.hpc;
303 lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT);
304 highpc = (unsigned long)gmonhdr.hpc / sizeof(UNIT);
305 sampbytes = gmonhdr.ncnt - size;
306 nsamples = sampbytes / sizeof (UNIT);
308 if ( debug & SAMPLEDEBUG ) {
309 printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n",
310 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
311 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
312 s_lowpc , s_highpc );
313 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
315 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
316 sampbytes , nsamples );
317 printf( "[openpfile] sample rate %d\n" , hz );
329 parentp = nllookup( rawp -> raw_frompc );
330 childp = nllookup( rawp -> raw_selfpc );
331 if ( parentp == 0 || childp == 0 )
334 && onlist( kfromlist , parentp -> name )
335 && onlist( ktolist , childp -> name ) ) {
338 childp -> ncall += rawp -> raw_count;
340 if ( debug & TALLYDEBUG ) {
341 printf( "[tally] arc from %s to %s traversed %d times\n" ,
342 parentp -> name , childp -> name , rawp -> raw_count );
345 addarc( parentp , childp , rawp -> raw_count );
349 * dump out the gmon.sum file
354 register nltype *nlp;
355 register arctype *arcp;
359 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
364 * dump the header; use the last header read in
366 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) {
373 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
378 * dump the normalized raw arc information
380 for ( nlp = nl ; nlp < npe ; nlp++ ) {
381 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
382 arc.raw_frompc = arcp -> arc_parentp -> value;
383 arc.raw_selfpc = arcp -> arc_childp -> value;
384 arc.raw_count = arcp -> arc_count;
385 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
390 if ( debug & SAMPLEDEBUG ) {
391 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
392 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
405 const nltype *p1 = (const nltype *)v1;
406 const nltype *p2 = (const nltype *)v2;
408 if ( p1 -> value < p2 -> value ) {
411 if ( p1 -> value > p2 -> value ) {
424 samples = (UNIT *) calloc(sampbytes, sizeof (UNIT));
426 warnx("no room for %d sample pc's", sampbytes / sizeof (UNIT));
430 for (i = 0; i < nsamples; i++) {
431 fread(&sample, sizeof (UNIT), 1, pfile);
434 samples[i] += sample;
437 warnx("unexpected EOF after reading %d/%d samples", --i , nsamples );
443 * Assign samples to the procedures to which they belong.
445 * There are three cases as to where pcl and pch can be
446 * with respect to the routine entry addresses svalue0 and svalue1
447 * as shown in the following diagram. overlap computes the
448 * distance between the arrows, the fraction of the sample
449 * that is to be credited to the routine which starts at svalue0.
455 * +-----------------------------------------------+
457 * | ->| |<- ->| |<- ->| |<- |
459 * +---------+ +---------+ +---------+
463 * pcl pch pcl pch pcl pch
465 * For the vax we assert that samples will never fall in the first
466 * two bytes of any routine, since that is the entry mask,
467 * thus we give call alignentries() to adjust the entry points if
468 * the entry mask falls in one bucket but the code for the routine
469 * doesn't start until the next bucket. In conjunction with the
470 * alignment of routine addresses, this should allow us to have
471 * only one sample for every four bytes of text space and never
472 * have any overlap (the two end cases, above).
479 unsigned long pcl, pch;
481 unsigned long overlap;
482 unsigned long svalue0, svalue1;
484 /* read samples and assign to namelist symbols */
485 scale = highpc - lowpc;
488 for (i = 0, j = 1; i < nsamples; i++) {
492 pcl = lowpc + (unsigned long)(scale * i);
493 pch = lowpc + (unsigned long)(scale * (i + 1));
496 if ( debug & SAMPLEDEBUG ) {
497 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
502 for (j = j - 1; j < nname; j++) {
503 svalue0 = nl[j].svalue;
504 svalue1 = nl[j+1].svalue;
506 * if high end of tick is below entry address,
512 * if low end of tick into next routine,
513 * go for next routine.
517 overlap = min(pch, svalue1) - max(pcl, svalue0);
520 if (debug & SAMPLEDEBUG) {
521 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
522 nl[j].value/sizeof(UNIT), svalue0, svalue1,
524 overlap * time / scale, overlap);
527 nl[j].time += overlap * time / scale;
532 if (debug & SAMPLEDEBUG) {
533 printf("[asgnsamples] totime %f\n", totime);
558 * calculate scaled entry point addresses (to save time in asgnsamples),
559 * and possibly push the scaled entry points over the entry mask,
560 * if it turns out that the entry point is in one bucket and the code
561 * for a routine is in the next bucket.
565 register struct nl *nlp;
566 unsigned long bucket_of_entry;
567 unsigned long bucket_of_code;
569 for (nlp = nl; nlp < npe; nlp++) {
570 nlp -> svalue = nlp -> value / sizeof(UNIT);
571 bucket_of_entry = (nlp->svalue - lowpc) / scale;
572 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
573 if (bucket_of_entry < bucket_of_code) {
575 if (debug & SAMPLEDEBUG) {
576 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
577 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
580 nlp->svalue += UNITS_TO_CODE;