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";
37 * things which get -E excluded by default.
39 char *defaultEs
[] = { "mcount" , "__mcleanup" , 0 };
41 int discard_underscores
= 1; /* Should we discard initial underscores? */
42 int bsd_style_output
= 0; /* As opposed to FSF style output */
56 while ( *argv
!= 0 && **argv
== '-' ) {
71 debug
|= atoi( *argv
);
74 printf("[main] debug = %d\n", debug
);
76 printf("%s: -d ignored\n", whoami
);
81 addlist( Elist
, *argv
);
83 addlist( elist
, *argv
);
87 addlist( elist
, *++argv
);
92 addlist( Flist
, *argv
);
94 addlist( flist
, *argv
);
98 addlist( flist
, *++argv
);
102 addlist( kfromlist
, *++argv
);
103 addlist( ktolist
, *++argv
);
109 case 'T': /* "Traditional" output format */
110 bsd_style_output
= 1;
117 Usage: %s [-a] [-b] [-c] [-d[num]] [-E function-name] [-e function-name]\n\
118 [-F function-name] [-f function-name] [-k from to] [-s] [-T] [-z]\n\
119 [image-file] [profile-file...]\n", whoami
);
128 a_outname
= A_OUTNAME
;
137 * turn off default functions
139 for ( sp
= &defaultEs
[0] ; *sp
; sp
++ ) {
141 addlist( Elist
, *sp
);
143 addlist( elist
, *sp
);
146 * how many ticks per second?
147 * if we can't tell, report time in ticks.
152 fprintf(stderr
, "time is in ticks, not seconds\n");
155 * get information about a.out file.
159 * get information about mon.out file(s).
162 getpfile( gmonname
);
166 } while ( *argv
++ != 0 );
168 * dump out a gmon.sum file if requested
174 * assign samples to procedures
178 * assemble the dynamic profile
180 timesortnlp
= doarcs();
182 if (bsd_style_output
) {
183 printgprof( timesortnlp
); /* print the dynamic profile */
184 printprof(); /* print the flat profile */
186 printprof(); /* print the flat profile */
187 printgprof( timesortnlp
); /* print the dynamic profile */
197 * Set up string and symbol tables from a.out.
198 * and optionally the text space.
199 * On return symbol table is sorted by value.
205 abfd
= bfd_openr (a_outname
, NULL
);
212 if (!bfd_check_format (abfd
, bfd_object
)) {
213 fprintf (stderr
, "%s: %s: bad format\n", whoami
, a_outname
);
217 /* getstrtab(nfile); */
219 gettextspace( abfd
);
220 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
223 if ( debug
& AOUTDEBUG
) {
226 for (j
= 0; j
< nname
; j
++){
227 printf("[getnfile] 0X%08x\t%s\n", nl
[j
].value
, nl
[j
].name
);
234 * Read in symbol table
243 i
= get_symtab_upper_bound (abfd
); /* This will probably give us more
244 * than we need, but that's ok.
246 syms
= (asymbol
**)xmalloc (i
);
247 nosyms
= bfd_canonicalize_symtab (abfd
, syms
);
250 for (i
= 0; i
< nosyms
; i
++) {
251 if (!funcsymbol (syms
[i
]))
257 fprintf(stderr
, "%s: %s: no symbols\n", whoami
, a_outname
);
261 nl
= (nltype
*) calloc( askfor
, sizeof(nltype
) );
263 fprintf(stderr
, "%s: No room for %d bytes of symbol table\n",
264 whoami
, askfor
* sizeof(nltype
) );
268 /* pass2 - read symbols */
271 for (i
= 0; i
< nosyms
; i
++) {
272 if (!funcsymbol (syms
[i
])) {
274 if ( debug
& AOUTDEBUG
) {
275 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
276 syms
[i
]->value
, syms
[i
]->name
);
281 /* Symbol offsets are always section-relative. */
282 npe
->value
= syms
[i
]->value
+ syms
[i
]->section
->vma
;
283 npe
->name
= syms
[i
]->name
;
285 /* If we see "main" without an initial '_', we assume
286 names are *not* prefixed by '_'. */
287 if (npe
->name
[0] == 'm' && discard_underscores
288 && strcmp(npe
->name
, "main") == 0)
289 discard_underscores
= 0;
292 if ( debug
& AOUTDEBUG
) {
293 printf( "[getsymtab] %d %s 0x%08x\n" ,
294 nname
, npe
-> name
, npe
-> value
);
304 * read in the text space of an a.out file
315 texsec
= bfd_get_section_by_name (abfd
, ".text");
316 if (texsec
== NULL
) {
320 textspace
= (u_char
*) malloc( texsec
->_cooked_size
);
322 if ( textspace
== 0 ) {
323 fprintf( stderr
, "%s: ran out room for %d bytes of text space: " ,
324 whoami
, texsec
->_cooked_size
);
325 fprintf( stderr
, "can't do -c\n" );
328 bfd_get_section_contents (abfd
, texsec
, textspace
, texsec
->filepos
,
329 texsec
->_cooked_size
);
332 * information from a gmon.out file is in two parts:
333 * an array of sampling hits within pc ranges,
342 struct veryrawarc rawarc
;
344 pfile
= openpfile(filename
);
347 * the rest of the file consists of
348 * a bunch of <from,self,count> tuples.
350 while ( fread( &rawarc
, sizeof rawarc
, 1 , pfile
) == 1 ) {
351 arc
.raw_frompc
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_frompc
);
352 arc
.raw_selfpc
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_selfpc
);
353 arc
.raw_count
= bfd_get_32 (abfd
, (bfd_byte
*) rawarc
.raw_count
);
355 if ( debug
& SAMPLEDEBUG
) {
356 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
357 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
376 if((pfile
= fopen(filename
, "r")) == NULL
) {
380 if (sizeof(struct rawhdr
) != fread(&raw
, 1, sizeof(struct rawhdr
), pfile
))
382 fprintf(stderr
, "%s: file too short to be a gmon file\n", filename
);
385 tmp
.lowpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &raw
.lowpc
[0]);
386 tmp
.highpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &raw
.highpc
[0]);
387 tmp
.ncnt
= bfd_get_32 (abfd
, (bfd_byte
*) &raw
.ncnt
[0]);
389 if ( s_highpc
!= 0 && ( tmp
.lowpc
!= h
.lowpc
||
390 tmp
.highpc
!= h
.highpc
|| tmp
.ncnt
!= h
.ncnt
) ) {
391 fprintf(stderr
, "%s: incompatible with first gmon file\n", filename
);
395 s_lowpc
= (unsigned long) h
.lowpc
;
396 s_highpc
= (unsigned long) h
.highpc
;
397 lowpc
= (unsigned long)h
.lowpc
/ sizeof(UNIT
);
398 highpc
= (unsigned long)h
.highpc
/ sizeof(UNIT
);
399 sampbytes
= h
.ncnt
- sizeof(struct rawhdr
);
400 nsamples
= sampbytes
/ sizeof (UNIT
);
402 if ( debug
& SAMPLEDEBUG
) {
403 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
404 h
.lowpc
, h
.highpc
, h
.ncnt
);
405 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
406 s_lowpc
, s_highpc
);
407 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
409 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
410 sampbytes
, nsamples
);
422 parentp
= nllookup( rawp
-> raw_frompc
);
423 childp
= nllookup( rawp
-> raw_selfpc
);
425 && onlist( kfromlist
, parentp
-> name
)
426 && onlist( ktolist
, childp
-> name
) ) {
429 childp
-> ncall
+= rawp
-> raw_count
;
431 if ( debug
& TALLYDEBUG
) {
432 printf( "[tally] arc from %s to %s traversed %d times\n" ,
433 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
436 addarc( parentp
, childp
, rawp
-> raw_count
);
440 * dump out the gmon.sum file
445 register nltype
*nlp
;
446 register arctype
*arcp
;
450 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
455 * dump the header; use the last header read in
457 if ( fwrite( &h
, sizeof h
, 1 , sfile
) != 1 ) {
464 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
469 * dump the normalized raw arc information
471 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
472 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
473 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
474 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
475 arc
.raw_count
= arcp
-> arc_count
;
476 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
481 if ( debug
& SAMPLEDEBUG
) {
482 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
483 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
494 if ( p1
-> value
< p2
-> value
) {
497 if ( p1
-> value
> p2
-> value
) {
510 samples
= (int *) calloc (nsamples
, sizeof(int));
512 fprintf( stderr
, "%s: No room for %d sample pc's\n",
517 for (i
= 0; i
< nsamples
; i
++) {
521 fread(raw
, sizeof (raw
), 1, pfile
);
522 value
= bfd_get_16 (abfd
, (bfd_byte
*) raw
);
529 "%s: unexpected EOF after reading %d/%d samples\n",
530 whoami
, --i
, nsamples
);
536 * Assign samples to the procedures to which they belong.
538 * There are three cases as to where pcl and pch can be
539 * with respect to the routine entry addresses svalue0 and svalue1
540 * as shown in the following diagram. overlap computes the
541 * distance between the arrows, the fraction of the sample
542 * that is to be credited to the routine which starts at svalue0.
548 * +-----------------------------------------------+
550 * | ->| |<- ->| |<- ->| |<- |
552 * +---------+ +---------+ +---------+
556 * pcl pch pcl pch pcl pch
558 * For the vax we assert that samples will never fall in the first
559 * two bytes of any routine, since that is the entry mask,
560 * thus we give call alignentries() to adjust the entry points if
561 * the entry mask falls in one bucket but the code for the routine
562 * doesn't start until the next bucket. In conjunction with the
563 * alignment of routine addresses, this should allow us to have
564 * only one sample for every four bytes of text space and never
565 * have any overlap (the two end cases, above).
572 unsigned long pcl
, pch
;
574 unsigned long overlap
;
575 unsigned long svalue0
, svalue1
;
577 /* read samples and assign to namelist symbols */
578 scale
= highpc
- lowpc
;
579 scale
/= nsamples
- 1;
581 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
585 pcl
= lowpc
+ scale
* i
;
586 pch
= lowpc
+ scale
* (i
+ 1);
589 if ( debug
& SAMPLEDEBUG
) {
590 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
595 for (j
= j
- 1; j
< nname
; j
++) {
596 svalue0
= nl
[j
].svalue
;
597 svalue1
= nl
[j
+1].svalue
;
599 * if high end of tick is below entry address,
605 * if low end of tick into next routine,
606 * go for next routine.
610 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
613 if (debug
& SAMPLEDEBUG
) {
614 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
615 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
617 overlap
* time
/ scale
, overlap
);
620 nl
[j
].time
+= overlap
* time
/ scale
;
625 if (debug
& SAMPLEDEBUG
) {
626 printf("[asgnsamples] totime %f\n", totime
);
651 * calculate scaled entry point addresses (to save time in asgnsamples),
652 * and possibly push the scaled entry points over the entry mask,
653 * if it turns out that the entry point is in one bucket and the code
654 * for a routine is in the next bucket.
658 register struct nl
*nlp
;
659 unsigned long bucket_of_entry
;
660 unsigned long bucket_of_code
;
662 for (nlp
= nl
; nlp
< npe
; nlp
++) {
663 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
664 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
665 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
666 if (bucket_of_entry
< bucket_of_code
) {
668 if (debug
& SAMPLEDEBUG
) {
669 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
670 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
673 nlp
->svalue
+= UNITS_TO_CODE
;
682 extern char *strtab
; /* string table from a.out */
683 extern int aflag
; /* if static functions aren't desired */
689 * must be a text symbol,
690 * and static text symbols don't qualify if aflag set.
697 if (aflag
&& (symp
->flags
&BSF_LOCAL
)) {
699 fprintf (stderr
, "%s(%d): %s: not a function\n", __FILE__
, __LINE__
, symp
->name
);
704 symprefix
= bfd_get_symbol_leading_char (abfd
);
705 i
= bfd_decode_symclass (symp
);
706 #if defined(DEBUG) && 0
707 if (i
!= 'T' && i
!= 't')
708 fprintf (stderr
, "%s(%d): %s is of class %c\n", __FILE__
, __LINE__
, symp
->name
, i
);
712 * Any external text symbol should be okay. (Only problem would be
713 * variables in the text section.)
720 * 't' is static text; -a says to ignore it. So if it's not
721 * a static text symbol, *or* it is and the user gave -a, we
725 if (i
!= 't' || aflag
)
729 * can't have any `funny' characters in name,
730 * where `funny' includes `.', .o file names
731 * and `$', pascal labels.
736 for (name
= symp
->name
; *name
; name
++) {
737 if ( *name
== '.' || *name
== '$' ) {
742 /* On systems where the C compiler adds an underscore to all names,
743 * static names without underscores seem usually to be labels in
744 * hand written assembler in the library. We don't want these
745 * names. This is certainly necessary on a Sparc running SunOS 4.1
746 * (try profiling a program that does a lot of division). I don't
747 * know whether it has harmful side effects on other systems.
748 * Perhaps it should be made configurable.
751 if (symprefix
&& symprefix
!= *symp
->name
)
This page took 0.089002 seconds and 4 git commands to generate.