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.4 2005/04/10 20:55:38 drhodus Exp $
42 static int valcmp(const void *, const void *);
45 static struct gmonhdr gmonhdr
;
49 main(int argc
, char **argv
)
59 while ( *argv
!= 0 && **argv
== '-' ) {
70 cyclethreshold
= atoi( *++argv
);
73 #if defined(vax) || defined(tahoe)
76 errx(1, "-c isn't supported on this architecture yet");
82 debug
|= atoi( *++argv
);
85 printf("[main] debug = %d\n", debug
);
87 printf("gprof: -d ignored\n");
92 addlist( Elist
, *argv
);
94 addlist( elist
, *argv
);
98 addlist( elist
, *++argv
);
103 addlist( Flist
, *argv
);
105 addlist( flist
, *argv
);
109 addlist( flist
, *++argv
);
113 addlist( kfromlist
, *++argv
);
114 addlist( ktolist
, *++argv
);
141 a_outname
= A_OUTNAME
;
147 gmonname
= (char *) malloc(strlen(a_outname
)+6);
148 strcpy(gmonname
, a_outname
);
149 strcat(gmonname
, ".gmon");
152 * get information from the executable file.
154 if (elf_getnfile(a_outname
, &defaultEs
) == -1 &&
155 aout_getnfile(a_outname
, &defaultEs
) == -1)
156 errx(1, "%s: bad format", a_outname
);
160 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
162 * turn off default functions
164 for ( sp
= defaultEs
; *sp
; sp
++ ) {
166 addlist( Elist
, *sp
);
168 addlist( elist
, *sp
);
171 * get information about mon.out file(s).
174 getpfile( gmonname
);
178 } while ( *argv
++ != 0 );
180 * how many ticks per second?
181 * if we can't tell, report time in ticks.
185 fprintf(stderr
, "time is in ticks, not seconds\n");
188 * dump out a gmon.sum file if requested
194 * assign samples to procedures
198 * assemble the dynamic profile
200 timesortnlp
= doarcs();
202 * print the dynamic profile
205 printgprof( timesortnlp
);
208 * print the flat profile
221 * information from a gmon.out file is in two parts:
222 * an array of sampling hits within pc ranges,
225 getpfile(char *filename
)
231 pfile
= openpfile(filename
);
234 * the rest of the file consists of
235 * a bunch of <from,self,count> tuples.
237 while ( fread( &arc
, sizeof arc
, 1 , pfile
) == 1 ) {
239 if ( debug
& SAMPLEDEBUG
) {
240 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
241 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
253 openpfile(char *filename
)
260 if((pfile
= fopen(filename
, "r")) == NULL
) {
264 fread(&tmp
, sizeof(struct gmonhdr
), 1, pfile
);
265 if ( s_highpc
!= 0 && ( tmp
.lpc
!= gmonhdr
.lpc
||
266 tmp
.hpc
!= gmonhdr
.hpc
|| tmp
.ncnt
!= gmonhdr
.ncnt
) ) {
267 warnx("%s: incompatible with first gmon file", filename
);
271 if ( gmonhdr
.version
== GMONVERSION
) {
272 rate
= gmonhdr
.profrate
;
273 size
= sizeof(struct gmonhdr
);
275 fseek(pfile
, sizeof(struct ophdr
), SEEK_SET
);
276 size
= sizeof(struct ophdr
);
277 gmonhdr
.profrate
= rate
= hertz();
278 gmonhdr
.version
= GMONVERSION
;
282 } else if (hz
!= rate
) {
284 "%s: profile clock rate (%d) %s (%d) in first gmon file\n",
285 filename
, rate
, "incompatible with clock rate", hz
);
288 s_lowpc
= (unsigned long) gmonhdr
.lpc
;
289 s_highpc
= (unsigned long) gmonhdr
.hpc
;
290 lowpc
= (unsigned long)gmonhdr
.lpc
/ sizeof(UNIT
);
291 highpc
= (unsigned long)gmonhdr
.hpc
/ sizeof(UNIT
);
292 sampbytes
= gmonhdr
.ncnt
- size
;
293 nsamples
= sampbytes
/ sizeof (UNIT
);
295 if ( debug
& SAMPLEDEBUG
) {
296 printf( "[openpfile] hdr.lpc 0x%x hdr.hpc 0x%x hdr.ncnt %d\n",
297 gmonhdr
.lpc
, gmonhdr
.hpc
, gmonhdr
.ncnt
);
298 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
299 s_lowpc
, s_highpc
);
300 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
302 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
303 sampbytes
, nsamples
);
304 printf( "[openpfile] sample rate %d\n" , hz
);
310 tally(struct rawarc
*rawp
)
315 parentp
= nllookup( rawp
-> raw_frompc
);
316 childp
= nllookup( rawp
-> raw_selfpc
);
317 if ( parentp
== 0 || childp
== 0 )
320 && onlist( kfromlist
, parentp
-> name
)
321 && onlist( ktolist
, childp
-> name
) ) {
324 childp
-> ncall
+= rawp
-> raw_count
;
326 if ( debug
& TALLYDEBUG
) {
327 printf( "[tally] arc from %s to %s traversed %d times\n" ,
328 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
331 addarc( parentp
, childp
, rawp
-> raw_count
);
335 * dump out the gmon.sum file
337 dumpsum(char *sumfile
)
344 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
349 * dump the header; use the last header read in
351 if ( fwrite( &gmonhdr
, sizeof gmonhdr
, 1 , sfile
) != 1 ) {
358 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
363 * dump the normalized raw arc information
365 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
366 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
367 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
368 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
369 arc
.raw_count
= arcp
-> arc_count
;
370 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
375 if ( debug
& SAMPLEDEBUG
) {
376 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
377 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
386 valcmp(const void *v1
, const void *v2
)
388 const nltype
*p1
= (const nltype
*)v1
;
389 const nltype
*p2
= (const nltype
*)v2
;
391 if ( p1
-> value
< p2
-> value
) {
394 if ( p1
-> value
> p2
-> value
) {
400 readsamples(FILE *pfile
)
406 samples
= (UNIT
*) calloc(sampbytes
, sizeof (UNIT
));
408 warnx("no room for %d sample pc's", sampbytes
/ sizeof (UNIT
));
412 for (i
= 0; i
< nsamples
; i
++) {
413 fread(&sample
, sizeof (UNIT
), 1, pfile
);
416 samples
[i
] += sample
;
419 warnx("unexpected EOF after reading %d/%d samples", --i
, nsamples
);
425 * Assign samples to the procedures to which they belong.
427 * There are three cases as to where pcl and pch can be
428 * with respect to the routine entry addresses svalue0 and svalue1
429 * as shown in the following diagram. overlap computes the
430 * distance between the arrows, the fraction of the sample
431 * that is to be credited to the routine which starts at svalue0.
437 * +-----------------------------------------------+
439 * | ->| |<- ->| |<- ->| |<- |
441 * +---------+ +---------+ +---------+
445 * pcl pch pcl pch pcl pch
447 * For the vax we assert that samples will never fall in the first
448 * two bytes of any routine, since that is the entry mask,
449 * thus we give call alignentries() to adjust the entry points if
450 * the entry mask falls in one bucket but the code for the routine
451 * doesn't start until the next bucket. In conjunction with the
452 * alignment of routine addresses, this should allow us to have
453 * only one sample for every four bytes of text space and never
454 * have any overlap (the two end cases, above).
461 unsigned long pcl
, pch
;
463 unsigned long overlap
;
464 unsigned long svalue0
, svalue1
;
466 /* read samples and assign to namelist symbols */
467 scale
= highpc
- lowpc
;
470 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
474 pcl
= lowpc
+ (unsigned long)(scale
* i
);
475 pch
= lowpc
+ (unsigned long)(scale
* (i
+ 1));
478 if ( debug
& SAMPLEDEBUG
) {
479 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
484 for (j
= j
- 1; j
< nname
; j
++) {
485 svalue0
= nl
[j
].svalue
;
486 svalue1
= nl
[j
+1].svalue
;
488 * if high end of tick is below entry address,
494 * if low end of tick into next routine,
495 * go for next routine.
499 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
502 if (debug
& SAMPLEDEBUG
) {
503 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
504 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
506 overlap
* time
/ scale
, overlap
);
509 nl
[j
].time
+= overlap
* time
/ scale
;
514 if (debug
& SAMPLEDEBUG
) {
515 printf("[asgnsamples] totime %f\n", totime
);
522 min(unsigned long a
, unsigned long b
)
530 max(unsigned long a
, unsigned long b
)
538 * calculate scaled entry point addresses (to save time in asgnsamples),
539 * and possibly push the scaled entry points over the entry mask,
540 * if it turns out that the entry point is in one bucket and the code
541 * for a routine is in the next bucket.
546 unsigned long bucket_of_entry
;
547 unsigned long bucket_of_code
;
549 for (nlp
= nl
; nlp
< npe
; nlp
++) {
550 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
551 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
552 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
553 if (bucket_of_entry
< bucket_of_code
) {
555 if (debug
& SAMPLEDEBUG
) {
556 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
557 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
560 nlp
->svalue
+= UNITS_TO_CODE
;