]>
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.
24 "@(#) Copyright (c) 1983 Regents of the University of California.\n\
25 All rights reserved.\n";
29 static char sccsid
[] = "@(#)gprof.c 5.6 (Berkeley) 6/1/90";
39 * things which get -E excluded by default.
41 char *defaultEs
[] = { "mcount" , "__mcleanup" , 0 };
43 int discard_underscores
= 1; /* Should we discard initial underscores? */
44 int bsd_style_output
= 0; /* As opposed to FSF style output */
58 while ( *argv
!= 0 && **argv
== '-' ) {
73 debug
|= atoi( *argv
);
76 printf("[main] debug = %d\n", debug
);
78 printf("%s: -d ignored\n", whoami
);
83 addlist( Elist
, *argv
);
85 addlist( elist
, *argv
);
89 addlist( elist
, *++argv
);
94 addlist( Flist
, *argv
);
96 addlist( flist
, *argv
);
100 addlist( flist
, *++argv
);
104 addlist( kfromlist
, *++argv
);
105 addlist( ktolist
, *++argv
);
111 case 'T': /* "Traditional" output format */
112 bsd_style_output
= 1;
115 printf ("gprof version %s\n", VERSION
);
123 Usage: %s [-a] [-b] [-c] [-d[num]] [-E function-name] [-e function-name]\n\
124 [-F function-name] [-f function-name] [-k from to] [-s] [-T] [-z]\n\
125 [image-file] [profile-file...]\n", whoami
);
134 a_outname
= A_OUTNAME
;
143 * turn off default functions
145 for ( sp
= &defaultEs
[0] ; *sp
; sp
++ ) {
147 addlist( Elist
, *sp
);
149 addlist( elist
, *sp
);
152 * how many ticks per second?
153 * if we can't tell, report time in ticks.
158 fprintf(stderr
, "time is in ticks, not seconds\n");
161 * get information about a.out file.
165 * get information about mon.out file(s).
168 getpfile( gmonname
);
172 } while ( *argv
++ != 0 );
174 * dump out a gmon.sum file if requested
180 * assign samples to procedures
184 * assemble the dynamic profile
186 timesortnlp
= doarcs();
188 if (bsd_style_output
) {
189 printgprof( timesortnlp
); /* print the dynamic profile */
190 printprof(); /* print the flat profile */
192 printprof(); /* print the flat profile */
193 printgprof( timesortnlp
); /* print the dynamic profile */
203 * Set up string and symbol tables from a.out.
204 * and optionally the text space.
205 * On return symbol table is sorted by value.
211 abfd
= bfd_openr (a_outname
, NULL
);
218 if (!bfd_check_format (abfd
, bfd_object
)) {
219 fprintf (stderr
, "%s: %s: bad format\n", whoami
, a_outname
);
223 /* getstrtab(nfile); */
225 gettextspace( abfd
);
226 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
229 if ( debug
& AOUTDEBUG
) {
232 for (j
= 0; j
< nname
; j
++){
233 printf("[getnfile] 0X%08x\t%s\n", nl
[j
].value
, nl
[j
].name
);
240 * Read in symbol table
249 i
= get_symtab_upper_bound (abfd
); /* This will probably give us more
250 * than we need, but that's ok.
252 syms
= (asymbol
**)xmalloc (i
);
253 nosyms
= bfd_canonicalize_symtab (abfd
, syms
);
256 for (i
= 0; i
< nosyms
; i
++) {
257 if (!funcsymbol (syms
[i
]))
263 fprintf(stderr
, "%s: %s: no symbols\n", whoami
, a_outname
);
267 nl
= (nltype
*) calloc( askfor
, sizeof(nltype
) );
269 fprintf(stderr
, "%s: No room for %d bytes of symbol table\n",
270 whoami
, askfor
* sizeof(nltype
) );
274 /* pass2 - read symbols */
277 for (i
= 0; i
< nosyms
; i
++) {
278 if (!funcsymbol (syms
[i
])) {
280 if ( debug
& AOUTDEBUG
) {
281 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
282 syms
[i
]->value
, syms
[i
]->name
);
287 /* Symbol offsets are always section-relative. */
288 npe
->value
= syms
[i
]->value
+ syms
[i
]->section
->vma
;
289 npe
->name
= syms
[i
]->name
;
291 /* If we see "main" without an initial '_', we assume
292 names are *not* prefixed by '_'. */
293 if (npe
->name
[0] == 'm' && discard_underscores
294 && strcmp(npe
->name
, "main") == 0)
295 discard_underscores
= 0;
298 if ( debug
& AOUTDEBUG
) {
299 printf( "[getsymtab] %d %s 0x%08x\n" ,
300 nname
, npe
-> name
, npe
-> value
);
310 * read in the text space of an a.out file
321 texsec
= bfd_get_section_by_name (abfd
, ".text");
322 if (texsec
== NULL
) {
326 textspace
= (u_char
*) malloc( texsec
->_cooked_size
);
328 if ( textspace
== 0 ) {
329 fprintf( stderr
, "%s: ran out room for %d bytes of text space: " ,
330 whoami
, texsec
->_cooked_size
);
331 fprintf( stderr
, "can't do -c\n" );
334 bfd_get_section_contents (abfd
, texsec
, textspace
, texsec
->filepos
,
335 texsec
->_cooked_size
);
338 * information from a gmon.out file is in two parts:
339 * an array of sampling hits within pc ranges,
348 struct veryrawarc rawarc
;
350 pfile
= openpfile(filename
);
353 * the rest of the file consists of
354 * a bunch of <from,self,count> tuples.
356 while ( fread( &rawarc
, sizeof rawarc
, 1 , pfile
) == 1 ) {
357 arc
.raw_frompc
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_frompc
);
358 arc
.raw_selfpc
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_selfpc
);
359 arc
.raw_count
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_count
);
361 if ( debug
& SAMPLEDEBUG
) {
362 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
363 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
382 if((pfile
= fopen(filename
, "r")) == NULL
) {
386 if (sizeof(struct rawhdr
) != fread(&raw
, 1, sizeof(struct rawhdr
), pfile
))
388 fprintf(stderr
, "%s: file too short to be a gmon file\n", filename
);
391 tmp
.lowpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &raw
.lowpc
[0]);
392 tmp
.highpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &raw
.highpc
[0]);
393 tmp
.ncnt
= bfd_get_32 (abfd
, (bfd_byte
*) &raw
.ncnt
[0]);
395 if ( s_highpc
!= 0 && ( tmp
.lowpc
!= h
.lowpc
||
396 tmp
.highpc
!= h
.highpc
|| tmp
.ncnt
!= h
.ncnt
) ) {
397 fprintf(stderr
, "%s: incompatible with first gmon file\n", filename
);
401 s_lowpc
= (unsigned long) h
.lowpc
;
402 s_highpc
= (unsigned long) h
.highpc
;
403 lowpc
= (unsigned long)h
.lowpc
/ sizeof(UNIT
);
404 highpc
= (unsigned long)h
.highpc
/ sizeof(UNIT
);
405 sampbytes
= h
.ncnt
- sizeof(struct rawhdr
);
406 nsamples
= sampbytes
/ sizeof (UNIT
);
408 if ( debug
& SAMPLEDEBUG
) {
409 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
410 h
.lowpc
, h
.highpc
, h
.ncnt
);
411 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
412 s_lowpc
, s_highpc
);
413 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
415 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
416 sampbytes
, nsamples
);
428 parentp
= nllookup( rawp
-> raw_frompc
);
429 childp
= nllookup( rawp
-> raw_selfpc
);
431 && onlist( kfromlist
, parentp
-> name
)
432 && onlist( ktolist
, childp
-> name
) ) {
435 childp
-> ncall
+= rawp
-> raw_count
;
437 if ( debug
& TALLYDEBUG
) {
438 printf( "[tally] arc from %s to %s traversed %d times\n" ,
439 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
442 addarc( parentp
, childp
, rawp
-> raw_count
);
446 * dump out the gmon.sum file
451 register nltype
*nlp
;
452 register arctype
*arcp
;
456 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
461 * dump the header; use the last header read in
463 if ( fwrite( &h
, sizeof h
, 1 , sfile
) != 1 ) {
470 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
475 * dump the normalized raw arc information
477 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
478 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
479 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
480 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
481 arc
.raw_count
= arcp
-> arc_count
;
482 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
487 if ( debug
& SAMPLEDEBUG
) {
488 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
489 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
500 if ( p1
-> value
< p2
-> value
) {
503 if ( p1
-> value
> p2
-> value
) {
516 samples
= (int *) calloc (nsamples
, sizeof(int));
518 fprintf( stderr
, "%s: No room for %d sample pc's\n",
523 for (i
= 0; i
< nsamples
; i
++) {
527 fread(raw
, sizeof (raw
), 1, pfile
);
528 value
= bfd_get_16 (abfd
, (bfd_byte
*) raw
);
535 "%s: unexpected EOF after reading %d/%d samples\n",
536 whoami
, --i
, nsamples
);
542 * Assign samples to the procedures to which they belong.
544 * There are three cases as to where pcl and pch can be
545 * with respect to the routine entry addresses svalue0 and svalue1
546 * as shown in the following diagram. overlap computes the
547 * distance between the arrows, the fraction of the sample
548 * that is to be credited to the routine which starts at svalue0.
554 * +-----------------------------------------------+
556 * | ->| |<- ->| |<- ->| |<- |
558 * +---------+ +---------+ +---------+
562 * pcl pch pcl pch pcl pch
564 * For the vax we assert that samples will never fall in the first
565 * two bytes of any routine, since that is the entry mask,
566 * thus we give call alignentries() to adjust the entry points if
567 * the entry mask falls in one bucket but the code for the routine
568 * doesn't start until the next bucket. In conjunction with the
569 * alignment of routine addresses, this should allow us to have
570 * only one sample for every four bytes of text space and never
571 * have any overlap (the two end cases, above).
578 unsigned long pcl
, pch
;
580 unsigned long overlap
;
581 unsigned long svalue0
, svalue1
;
583 /* read samples and assign to namelist symbols */
584 scale
= highpc
- lowpc
;
585 scale
/= nsamples
- 1;
587 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
591 pcl
= lowpc
+ scale
* i
;
592 pch
= lowpc
+ scale
* (i
+ 1);
595 if ( debug
& SAMPLEDEBUG
) {
596 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
601 for (j
= j
- 1; j
< nname
; j
++) {
602 svalue0
= nl
[j
].svalue
;
603 svalue1
= nl
[j
+1].svalue
;
605 * if high end of tick is below entry address,
611 * if low end of tick into next routine,
612 * go for next routine.
616 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
619 if (debug
& SAMPLEDEBUG
) {
620 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
621 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
623 overlap
* time
/ scale
, overlap
);
626 nl
[j
].time
+= overlap
* time
/ scale
;
631 if (debug
& SAMPLEDEBUG
) {
632 printf("[asgnsamples] totime %f\n", totime
);
657 * calculate scaled entry point addresses (to save time in asgnsamples),
658 * and possibly push the scaled entry points over the entry mask,
659 * if it turns out that the entry point is in one bucket and the code
660 * for a routine is in the next bucket.
664 register struct nl
*nlp
;
665 unsigned long bucket_of_entry
;
666 unsigned long bucket_of_code
;
668 for (nlp
= nl
; nlp
< npe
; nlp
++) {
669 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
670 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
671 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
672 if (bucket_of_entry
< bucket_of_code
) {
674 if (debug
& SAMPLEDEBUG
) {
675 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
676 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
679 nlp
->svalue
+= UNITS_TO_CODE
;
688 extern char *strtab
; /* string table from a.out */
689 extern int aflag
; /* if static functions aren't desired */
695 * must be a text symbol,
696 * and static text symbols don't qualify if aflag set.
703 if (aflag
&& (symp
->flags
&BSF_LOCAL
)) {
705 fprintf (stderr
, "%s(%d): %s: not a function\n", __FILE__
, __LINE__
, symp
->name
);
710 symprefix
= bfd_get_symbol_leading_char (abfd
);
711 i
= bfd_decode_symclass (symp
);
712 #if defined(DEBUG) && 0
713 if (i
!= 'T' && i
!= 't')
714 fprintf (stderr
, "%s(%d): %s is of class %c\n", __FILE__
, __LINE__
, symp
->name
, i
);
718 * Any external text symbol should be okay. (Only problem would be
719 * variables in the text section.)
726 * 't' is static text; -a says to ignore it. So if it's not
727 * a static text symbol, *or* it is and the user gave -a, we
731 if (i
!= 't' || aflag
)
735 * can't have any `funny' characters in name,
736 * where `funny' includes `.', .o file names
737 * and `$', pascal labels.
742 for (name
= symp
->name
; *name
; name
++) {
743 if ( *name
== '.' || *name
== '$' ) {
748 /* On systems where the C compiler adds an underscore to all names,
749 * static names without underscores seem usually to be labels in
750 * hand written assembler in the library. We don't want these
751 * names. This is certainly necessary on a Sparc running SunOS 4.1
752 * (try profiling a program that does a lot of division). I don't
753 * know whether it has harmful side effects on other systems.
754 * Perhaps it should be made configurable.
757 if (symprefix
&& symprefix
!= *symp
->name
)