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
33 * @(#) Copyright (c) 1983, 1993 The Regents of the University of California. All rights reserved.
34 * @(#)gprof.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/usr.bin/gprof/gprof.c,v 1.11 1999/08/28 01:01:55 peter Exp $
36 * $DragonFly: src/usr.bin/gprof/gprof.c,v 1.2 2003/06/17 04:29:27 dillon Exp $
42 static int valcmp(const void *, const void *);
45 static struct gmonhdr gmonhdr;
61 while ( *argv != 0 && **argv == '-' ) {
72 cyclethreshold = atoi( *++argv );
75 #if defined(vax) || defined(tahoe)
78 errx(1, "-c isn't supported on this architecture yet");
84 debug |= atoi( *++argv );
87 printf("[main] debug = %d\n", debug);
89 printf("gprof: -d ignored\n");
94 addlist( Elist , *argv );
96 addlist( elist , *argv );
100 addlist( elist , *++argv );
105 addlist( Flist , *argv );
107 addlist( flist , *argv );
111 addlist( flist , *++argv );
115 addlist( kfromlist , *++argv );
116 addlist( ktolist , *++argv );
143 a_outname = A_OUTNAME;
149 gmonname = (char *) malloc(strlen(a_outname)+6);
150 strcpy(gmonname, a_outname);
151 strcat(gmonname, ".gmon");
154 * get information from the executable file.
156 if (elf_getnfile(a_outname, &defaultEs) == -1 &&
157 aout_getnfile(a_outname, &defaultEs) == -1)
158 errx(1, "%s: bad format", a_outname);
162 qsort(nl, nname, sizeof(nltype), valcmp);
164 * turn off default functions
166 for ( sp = defaultEs ; *sp ; sp++ ) {
168 addlist( Elist , *sp );
170 addlist( elist , *sp );
173 * get information about mon.out file(s).
176 getpfile( gmonname );
180 } while ( *argv++ != 0 );
182 * how many ticks per second?
183 * if we can't tell, report time in ticks.
187 fprintf(stderr, "time is in ticks, not seconds\n");
190 * dump out a gmon.sum file if requested
196 * assign samples to procedures
200 * assemble the dynamic profile
202 timesortnlp = doarcs();
204 * print the dynamic profile
207 printgprof( timesortnlp );
210 * print the flat profile
223 * information from a gmon.out file is in two parts:
224 * an array of sampling hits within pc ranges,
234 pfile = openpfile(filename);
237 * the rest of the file consists of
238 * a bunch of <from,self,count> tuples.
240 while ( fread( &arc , sizeof arc , 1 , pfile ) == 1 ) {
242 if ( debug & SAMPLEDEBUG ) {
243 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
244 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
264 if((pfile = fopen(filename, "r")) == NULL) {
268 fread(&tmp, sizeof(struct gmonhdr), 1, pfile);
269 if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc ||
270 tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) {
271 warnx("%s: incompatible with first gmon file", filename);
275 if ( gmonhdr.version == GMONVERSION ) {
276 rate = gmonhdr.profrate;
277 size = sizeof(struct gmonhdr);
279 fseek(pfile, sizeof(struct ophdr), SEEK_SET);
280 size = sizeof(struct ophdr);
281 gmonhdr.profrate = rate = hertz();
282 gmonhdr.version = GMONVERSION;
286 } else if (hz != rate) {
288 "%s: profile clock rate (%d) %s (%d) in first gmon file\n",
289 filename, rate, "incompatible with clock rate", hz);
292 s_lowpc = (unsigned long) gmonhdr.lpc;
293 s_highpc = (unsigned long) gmonhdr.hpc;
294 lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT);
295 highpc = (unsigned long)gmonhdr.hpc / sizeof(UNIT);
296 sampbytes = gmonhdr.ncnt - size;
297 nsamples = sampbytes / sizeof (UNIT);
299 if ( debug & SAMPLEDEBUG ) {
300 printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n",
301 gmonhdr.lpc , gmonhdr.hpc , gmonhdr.ncnt );
302 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
303 s_lowpc , s_highpc );
304 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
306 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
307 sampbytes , nsamples );
308 printf( "[openpfile] sample rate %d\n" , hz );
320 parentp = nllookup( rawp -> raw_frompc );
321 childp = nllookup( rawp -> raw_selfpc );
322 if ( parentp == 0 || childp == 0 )
325 && onlist( kfromlist , parentp -> name )
326 && onlist( ktolist , childp -> name ) ) {
329 childp -> ncall += rawp -> raw_count;
331 if ( debug & TALLYDEBUG ) {
332 printf( "[tally] arc from %s to %s traversed %d times\n" ,
333 parentp -> name , childp -> name , rawp -> raw_count );
336 addarc( parentp , childp , rawp -> raw_count );
340 * dump out the gmon.sum file
345 register nltype *nlp;
346 register arctype *arcp;
350 if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) {
355 * dump the header; use the last header read in
357 if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) {
364 if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) {
369 * dump the normalized raw arc information
371 for ( nlp = nl ; nlp < npe ; nlp++ ) {
372 for ( arcp = nlp -> children ; arcp ; arcp = arcp -> arc_childlist ) {
373 arc.raw_frompc = arcp -> arc_parentp -> value;
374 arc.raw_selfpc = arcp -> arc_childp -> value;
375 arc.raw_count = arcp -> arc_count;
376 if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) {
381 if ( debug & SAMPLEDEBUG ) {
382 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
383 arc.raw_frompc , arc.raw_selfpc , arc.raw_count );
396 const nltype *p1 = (const nltype *)v1;
397 const nltype *p2 = (const nltype *)v2;
399 if ( p1 -> value < p2 -> value ) {
402 if ( p1 -> value > p2 -> value ) {
415 samples = (UNIT *) calloc(sampbytes, sizeof (UNIT));
417 warnx("no room for %d sample pc's", sampbytes / sizeof (UNIT));
421 for (i = 0; i < nsamples; i++) {
422 fread(&sample, sizeof (UNIT), 1, pfile);
425 samples[i] += sample;
428 warnx("unexpected EOF after reading %d/%d samples", --i , nsamples );
434 * Assign samples to the procedures to which they belong.
436 * There are three cases as to where pcl and pch can be
437 * with respect to the routine entry addresses svalue0 and svalue1
438 * as shown in the following diagram. overlap computes the
439 * distance between the arrows, the fraction of the sample
440 * that is to be credited to the routine which starts at svalue0.
446 * +-----------------------------------------------+
448 * | ->| |<- ->| |<- ->| |<- |
450 * +---------+ +---------+ +---------+
454 * pcl pch pcl pch pcl pch
456 * For the vax we assert that samples will never fall in the first
457 * two bytes of any routine, since that is the entry mask,
458 * thus we give call alignentries() to adjust the entry points if
459 * the entry mask falls in one bucket but the code for the routine
460 * doesn't start until the next bucket. In conjunction with the
461 * alignment of routine addresses, this should allow us to have
462 * only one sample for every four bytes of text space and never
463 * have any overlap (the two end cases, above).
470 unsigned long pcl, pch;
472 unsigned long overlap;
473 unsigned long svalue0, svalue1;
475 /* read samples and assign to namelist symbols */
476 scale = highpc - lowpc;
479 for (i = 0, j = 1; i < nsamples; i++) {
483 pcl = lowpc + (unsigned long)(scale * i);
484 pch = lowpc + (unsigned long)(scale * (i + 1));
487 if ( debug & SAMPLEDEBUG ) {
488 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
493 for (j = j - 1; j < nname; j++) {
494 svalue0 = nl[j].svalue;
495 svalue1 = nl[j+1].svalue;
497 * if high end of tick is below entry address,
503 * if low end of tick into next routine,
504 * go for next routine.
508 overlap = min(pch, svalue1) - max(pcl, svalue0);
511 if (debug & SAMPLEDEBUG) {
512 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
513 nl[j].value/sizeof(UNIT), svalue0, svalue1,
515 overlap * time / scale, overlap);
518 nl[j].time += overlap * time / scale;
523 if (debug & SAMPLEDEBUG) {
524 printf("[asgnsamples] totime %f\n", totime);
549 * calculate scaled entry point addresses (to save time in asgnsamples),
550 * and possibly push the scaled entry points over the entry mask,
551 * if it turns out that the entry point is in one bucket and the code
552 * for a routine is in the next bucket.
556 register struct nl *nlp;
557 unsigned long bucket_of_entry;
558 unsigned long bucket_of_code;
560 for (nlp = nl; nlp < npe; nlp++) {
561 nlp -> svalue = nlp -> value / sizeof(UNIT);
562 bucket_of_entry = (nlp->svalue - lowpc) / scale;
563 bucket_of_code = (nlp->svalue + UNITS_TO_CODE - lowpc) / scale;
564 if (bucket_of_entry < bucket_of_code) {
566 if (debug & SAMPLEDEBUG) {
567 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
568 nlp->svalue, nlp->svalue + UNITS_TO_CODE);
571 nlp->svalue += UNITS_TO_CODE;