]>
git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gprof/gprof.c
2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution
11 * and in all advertising materials mentioning features or use of this
12 * software. Neither the name of the University nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
23 All rights reserved.\n";
27 static char sccsid
[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
32 char *whoami
= "gprof";
35 * things which get -E excluded by default.
37 char *defaultEs
[] = { "mcount" , "__mcleanup" , 0 };
50 while ( *argv
!= 0 && **argv
== '-' ) {
65 debug
|= atoi( *argv
);
68 printf("[main] debug = %d\n", debug
);
70 printf("%s: -d ignored\n", whoami
);
75 addlist( Elist
, *argv
);
77 addlist( elist
, *argv
);
81 addlist( elist
, *++argv
);
86 addlist( Flist
, *argv
);
88 addlist( flist
, *argv
);
92 addlist( flist
, *++argv
);
96 addlist( kfromlist
, *++argv
);
97 addlist( ktolist
, *++argv
);
113 a_outname
= A_OUTNAME
;
122 * turn off default functions
124 for ( sp
= &defaultEs
[0] ; *sp
; sp
++ ) {
126 addlist( Elist
, *sp
);
128 addlist( elist
, *sp
);
131 * how many ticks per second?
132 * if we can't tell, report time in ticks.
137 fprintf(stderr
, "time is in ticks, not seconds\n");
140 * get information about a.out file.
144 * get information about mon.out file(s).
147 getpfile( gmonname
);
151 } while ( *argv
++ != 0 );
153 * dump out a gmon.sum file if requested
159 * assign samples to procedures
163 * assemble the dynamic profile
165 timesortnlp
= doarcs();
167 * print the dynamic profile
169 printgprof( timesortnlp
);
171 * print the flat profile
182 * Set up string and symbol tables from a.out.
183 * and optionally the text space.
184 * On return symbol table is sorted by value.
191 nfile
= fopen( a_outname
,"r");
196 fread(&xbuf
, 1, sizeof(xbuf
), nfile
);
197 if (N_BADMAG(xbuf
)) {
198 fprintf(stderr
, "%s: %s: bad format\n", whoami
, a_outname
);
203 gettextspace( nfile
);
204 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
207 if ( debug
& AOUTDEBUG
) {
210 for (j
= 0; j
< nname
; j
++){
211 printf("[getnfile] 0X%08x\t%s\n", nl
[j
].value
, nl
[j
].name
);
221 fseek(nfile
, (long)(N_SYMOFF(xbuf
) + xbuf
.a_syms
), 0);
222 if (fread(&ssiz
, sizeof (ssiz
), 1, nfile
) == 0) {
223 fprintf(stderr
, "%s: %s: no string table (old format?)\n" ,
224 whoami
, a_outname
);
227 strtab
= (char *)calloc(ssiz
, 1);
228 if (strtab
== NULL
) {
229 fprintf(stderr
, "%s: %s: no room for %d bytes of string table",
230 whoami
, a_outname
, ssiz
);
233 if (fread(strtab
+sizeof(ssiz
), ssiz
-sizeof(ssiz
), 1, nfile
) != 1) {
234 fprintf(stderr
, "%s: %s: error reading string table\n",
235 whoami
, a_outname
);
241 * Read in symbol table
250 /* pass1 - count symbols */
251 fseek(nfile
, (long)N_SYMOFF(xbuf
), 0);
253 for (i
= xbuf
.a_syms
; i
> 0; i
-= sizeof(struct nlist
)) {
254 fread(&nbuf
, sizeof(nbuf
), 1, nfile
);
255 if ( ! funcsymbol( &nbuf
) ) {
261 fprintf(stderr
, "%s: %s: no symbols\n", whoami
, a_outname
);
265 nl
= (nltype
*) calloc( askfor
, sizeof(nltype
) );
267 fprintf(stderr
, "%s: No room for %d bytes of symbol table\n",
268 whoami
, askfor
* sizeof(nltype
) );
272 /* pass2 - read symbols */
273 fseek(nfile
, (long)N_SYMOFF(xbuf
), 0);
276 for (i
= xbuf
.a_syms
; i
> 0; i
-= sizeof(struct nlist
)) {
277 fread(&nbuf
, sizeof(nbuf
), 1, nfile
);
278 if ( ! funcsymbol( &nbuf
) ) {
280 if ( debug
& AOUTDEBUG
) {
281 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
282 nbuf
.n_type
, strtab
+ nbuf
.n_un
.n_strx
);
287 npe
->value
= nbuf
.n_value
;
288 npe
->name
= strtab
+nbuf
.n_un
.n_strx
;
290 if ( debug
& AOUTDEBUG
) {
291 printf( "[getsymtab] %d %s 0x%08x\n" ,
292 nname
, npe
-> name
, npe
-> value
);
302 * read in the text space of an a.out file
304 gettextspace( nfile
)
312 textspace
= (u_char
*) malloc( xbuf
.a_text
);
313 if ( textspace
== 0 ) {
314 fprintf( stderr
, "%s: ran out room for %d bytes of text space: " ,
315 whoami
, xbuf
.a_text
);
316 fprintf( stderr
, "can't do -c\n" );
319 (void) fseek( nfile
, N_TXTOFF( xbuf
) , 0 );
320 if ( fread( textspace
, 1 , xbuf
.a_text
, nfile
) != xbuf
.a_text
) {
321 fprintf( stderr
, "%s: couldn't read text space: " , whoami
);
322 fprintf( stderr
, "can't do -c\n" );
329 * information from a gmon.out file is in two parts:
330 * an array of sampling hits within pc ranges,
340 pfile
= openpfile(filename
);
343 * the rest of the file consists of
344 * a bunch of <from,self,count> tuples.
346 while ( fread( &arc
, sizeof arc
, 1 , pfile
) == 1 ) {
348 if ( debug
& SAMPLEDEBUG
) {
349 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
350 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
368 if((pfile
= fopen(filename
, "r")) == NULL
) {
372 fread(&tmp
, sizeof(struct hdr
), 1, pfile
);
373 if ( s_highpc
!= 0 && ( tmp
.lowpc
!= h
.lowpc
||
374 tmp
.highpc
!= h
.highpc
|| tmp
.ncnt
!= h
.ncnt
) ) {
375 fprintf(stderr
, "%s: incompatible with first gmon file\n", filename
);
379 s_lowpc
= (unsigned long) h
.lowpc
;
380 s_highpc
= (unsigned long) h
.highpc
;
381 lowpc
= (unsigned long)h
.lowpc
/ sizeof(UNIT
);
382 highpc
= (unsigned long)h
.highpc
/ sizeof(UNIT
);
383 sampbytes
= h
.ncnt
- sizeof(struct hdr
);
384 nsamples
= sampbytes
/ sizeof (UNIT
);
386 if ( debug
& SAMPLEDEBUG
) {
387 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
388 h
.lowpc
, h
.highpc
, h
.ncnt
);
389 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
390 s_lowpc
, s_highpc
);
391 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
393 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
394 sampbytes
, nsamples
);
406 parentp
= nllookup( rawp
-> raw_frompc
);
407 childp
= nllookup( rawp
-> raw_selfpc
);
409 && onlist( kfromlist
, parentp
-> name
)
410 && onlist( ktolist
, childp
-> name
) ) {
413 childp
-> ncall
+= rawp
-> raw_count
;
415 if ( debug
& TALLYDEBUG
) {
416 printf( "[tally] arc from %s to %s traversed %d times\n" ,
417 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
420 addarc( parentp
, childp
, rawp
-> raw_count
);
424 * dump out the gmon.sum file
429 register nltype
*nlp
;
430 register arctype
*arcp
;
434 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
439 * dump the header; use the last header read in
441 if ( fwrite( &h
, sizeof h
, 1 , sfile
) != 1 ) {
448 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
453 * dump the normalized raw arc information
455 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
456 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
457 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
458 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
459 arc
.raw_count
= arcp
-> arc_count
;
460 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
465 if ( debug
& SAMPLEDEBUG
) {
466 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
467 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
478 if ( p1
-> value
< p2
-> value
) {
481 if ( p1
-> value
> p2
-> value
) {
494 samples
= (UNIT
*) calloc(sampbytes
, sizeof (UNIT
));
496 fprintf( stderr
, "%s: No room for %d sample pc's\n",
497 whoami
, sampbytes
/ sizeof (UNIT
));
501 for (i
= 0; i
< nsamples
; i
++) {
502 fread(&sample
, sizeof (UNIT
), 1, pfile
);
505 samples
[i
] += sample
;
509 "%s: unexpected EOF after reading %d/%d samples\n",
510 whoami
, --i
, nsamples
);
516 * Assign samples to the procedures to which they belong.
518 * There are three cases as to where pcl and pch can be
519 * with respect to the routine entry addresses svalue0 and svalue1
520 * as shown in the following diagram. overlap computes the
521 * distance between the arrows, the fraction of the sample
522 * that is to be credited to the routine which starts at svalue0.
528 * +-----------------------------------------------+
530 * | ->| |<- ->| |<- ->| |<- |
532 * +---------+ +---------+ +---------+
536 * pcl pch pcl pch pcl pch
538 * For the vax we assert that samples will never fall in the first
539 * two bytes of any routine, since that is the entry mask,
540 * thus we give call alignentries() to adjust the entry points if
541 * the entry mask falls in one bucket but the code for the routine
542 * doesn't start until the next bucket. In conjunction with the
543 * alignment of routine addresses, this should allow us to have
544 * only one sample for every four bytes of text space and never
545 * have any overlap (the two end cases, above).
552 unsigned long pcl
, pch
;
554 unsigned long overlap
;
555 unsigned long svalue0
, svalue1
;
557 /* read samples and assign to namelist symbols */
558 scale
= highpc
- lowpc
;
561 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
565 pcl
= lowpc
+ scale
* i
;
566 pch
= lowpc
+ scale
* (i
+ 1);
569 if ( debug
& SAMPLEDEBUG
) {
570 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
575 for (j
= j
- 1; j
< nname
; j
++) {
576 svalue0
= nl
[j
].svalue
;
577 svalue1
= nl
[j
+1].svalue
;
579 * if high end of tick is below entry address,
585 * if low end of tick into next routine,
586 * go for next routine.
590 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
593 if (debug
& SAMPLEDEBUG
) {
594 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
595 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
597 overlap
* time
/ scale
, overlap
);
600 nl
[j
].time
+= overlap
* time
/ scale
;
605 if (debug
& SAMPLEDEBUG
) {
606 printf("[asgnsamples] totime %f\n", totime
);
631 * calculate scaled entry point addresses (to save time in asgnsamples),
632 * and possibly push the scaled entry points over the entry mask,
633 * if it turns out that the entry point is in one bucket and the code
634 * for a routine is in the next bucket.
638 register struct nl
*nlp
;
639 unsigned long bucket_of_entry
;
640 unsigned long bucket_of_code
;
642 for (nlp
= nl
; nlp
< npe
; nlp
++) {
643 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
644 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
645 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
646 if (bucket_of_entry
< bucket_of_code
) {
648 if (debug
& SAMPLEDEBUG
) {
649 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
650 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
653 nlp
->svalue
+= UNITS_TO_CODE
;
660 struct nlist
*nlistp
;
662 extern char *strtab
; /* string table from a.out */
663 extern int aflag
; /* if static functions aren't desired */
667 * must be a text symbol,
668 * and static text symbols don't qualify if aflag set.
670 if ( ! ( ( nlistp
-> n_type
== ( N_TEXT
| N_EXT
) )
671 || ( ( nlistp
-> n_type
== N_TEXT
) && ( aflag
== 0 ) ) ) ) {
675 * can't have any `funny' characters in name,
676 * where `funny' includes `.', .o file names
677 * and `$', pascal labels.
679 for ( name
= strtab
+ nlistp
-> n_un
.n_strx
; *name
; name
+= 1 ) {
680 if ( *name
== '.' || *name
== '$' ) {