b2c5e33f0b6eaf88acfecb4887b411c75e680a88
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";
34 char *whoami
= "gprof";
37 * things which get -E excluded by default.
39 char *defaultEs
[] = { "mcount" , "__mcleanup" , 0 };
52 while ( *argv
!= 0 && **argv
== '-' ) {
67 debug
|= atoi( *argv
);
70 printf("[main] debug = %d\n", debug
);
72 printf("%s: -d ignored\n", whoami
);
77 addlist( Elist
, *argv
);
79 addlist( elist
, *argv
);
83 addlist( elist
, *++argv
);
88 addlist( Flist
, *argv
);
90 addlist( flist
, *argv
);
94 addlist( flist
, *++argv
);
98 addlist( kfromlist
, *++argv
);
99 addlist( ktolist
, *++argv
);
115 a_outname
= A_OUTNAME
;
124 * turn off default functions
126 for ( sp
= &defaultEs
[0] ; *sp
; sp
++ ) {
128 addlist( Elist
, *sp
);
130 addlist( elist
, *sp
);
133 * how many ticks per second?
134 * if we can't tell, report time in ticks.
139 fprintf(stderr
, "time is in ticks, not seconds\n");
142 * get information about a.out file.
146 * get information about mon.out file(s).
149 getpfile( gmonname
);
153 } while ( *argv
++ != 0 );
155 * dump out a gmon.sum file if requested
161 * assign samples to procedures
165 * assemble the dynamic profile
167 timesortnlp
= doarcs();
169 * print the dynamic profile
171 printgprof( timesortnlp
);
173 * print the flat profile
184 * Set up string and symbol tables from a.out.
185 * and optionally the text space.
186 * On return symbol table is sorted by value.
192 abfd
= bfd_openr (a_outname
, NULL
);
199 if (!bfd_check_format (abfd
, bfd_object
)) {
200 fprintf (stderr
, "%s: %s: bad format\n", whoami
, a_outname
);
204 /* getstrtab(nfile); */
206 gettextspace( abfd
);
207 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
210 if ( debug
& AOUTDEBUG
) {
213 for (j
= 0; j
< nname
; j
++){
214 printf("[getnfile] 0X%08x\t%s\n", nl
[j
].value
, nl
[j
].name
);
221 * Read in symbol table
230 i
= get_symtab_upper_bound (abfd
); /* This will probably give us more
231 * than we need, but that's ok.
234 nosyms
= bfd_canonicalize_symtab (abfd
, syms
);
237 for (i
= 0; i
< nosyms
; i
++) {
238 if (!funcsymbol (syms
[i
]))
244 fprintf(stderr
, "%s: %s: no symbols\n", whoami
, a_outname
);
248 nl
= (nltype
*) calloc( askfor
, sizeof(nltype
) );
250 fprintf(stderr
, "%s: No room for %d bytes of symbol table\n",
251 whoami
, askfor
* sizeof(nltype
) );
255 /* pass2 - read symbols */
258 for (i
= 0; i
< nosyms
; i
++) {
259 if (!funcsymbol (syms
[i
])) {
261 if ( debug
& AOUTDEBUG
) {
262 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
263 syms
[i
]->value
, syms
[i
]->name
);
268 npe
->value
= syms
[i
]->value
+ syms
[i
]->section
->vma
;
269 npe
->name
= syms
[i
]->name
;
271 if ( debug
& AOUTDEBUG
) {
272 printf( "[getsymtab] %d %s 0x%08x\n" ,
273 nname
, npe
-> name
, npe
-> value
);
283 * read in the text space of an a.out file
294 texsec
= bfd_get_section_by_name (abfd
, ".text");
295 if (texsec
== NULL
) {
299 textspace
= (u_char
*) malloc( texsec
->_cooked_size
);
301 if ( textspace
== 0 ) {
302 fprintf( stderr
, "%s: ran out room for %d bytes of text space: " ,
303 whoami
, texsec
->_cooked_size
);
304 fprintf( stderr
, "can't do -c\n" );
307 bfd_get_section_contents (abfd
, texsec
, textspace
, texsec
->filepos
,
308 texsec
->_cooked_size
);
311 * information from a gmon.out file is in two parts:
312 * an array of sampling hits within pc ranges,
322 pfile
= openpfile(filename
);
325 * the rest of the file consists of
326 * a bunch of <from,self,count> tuples.
328 while ( fread( &arc
, sizeof arc
, 1 , pfile
) == 1 ) {
329 arc
.raw_frompc
= bfd_get_32 (abfd
, &arc
.raw_frompc
);
330 arc
.raw_selfpc
= bfd_get_32 (abfd
, &arc
.raw_selfpc
);
331 arc
.raw_count
= bfd_get_32 (abfd
, &arc
.raw_count
);
333 if ( debug
& SAMPLEDEBUG
) {
334 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
335 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
353 if((pfile
= fopen(filename
, "r")) == NULL
) {
357 fread(&tmp
, sizeof(struct hdr
), 1, pfile
);
358 tmp
.lowpc
= bfd_get_32 (abfd
, &tmp
.lowpc
);
359 tmp
.highpc
= bfd_get_32 (abfd
, &tmp
.highpc
);
360 tmp
.ncnt
= bfd_get_32 (abfd
, &tmp
.ncnt
);
362 if ( s_highpc
!= 0 && ( tmp
.lowpc
!= h
.lowpc
||
363 tmp
.highpc
!= h
.highpc
|| tmp
.ncnt
!= h
.ncnt
) ) {
364 fprintf(stderr
, "%s: incompatible with first gmon file\n", filename
);
368 s_lowpc
= (unsigned long) h
.lowpc
;
369 s_highpc
= (unsigned long) h
.highpc
;
370 lowpc
= (unsigned long)h
.lowpc
/ sizeof(UNIT
);
371 highpc
= (unsigned long)h
.highpc
/ sizeof(UNIT
);
372 sampbytes
= h
.ncnt
- sizeof(struct hdr
);
373 nsamples
= sampbytes
/ sizeof (UNIT
);
375 if ( debug
& SAMPLEDEBUG
) {
376 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
377 h
.lowpc
, h
.highpc
, h
.ncnt
);
378 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
379 s_lowpc
, s_highpc
);
380 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
382 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
383 sampbytes
, nsamples
);
395 parentp
= nllookup( rawp
-> raw_frompc
);
396 childp
= nllookup( rawp
-> raw_selfpc
);
398 && onlist( kfromlist
, parentp
-> name
)
399 && onlist( ktolist
, childp
-> name
) ) {
402 childp
-> ncall
+= rawp
-> raw_count
;
404 if ( debug
& TALLYDEBUG
) {
405 printf( "[tally] arc from %s to %s traversed %d times\n" ,
406 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
409 addarc( parentp
, childp
, rawp
-> raw_count
);
413 * dump out the gmon.sum file
418 register nltype
*nlp
;
419 register arctype
*arcp
;
423 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
428 * dump the header; use the last header read in
430 if ( fwrite( &h
, sizeof h
, 1 , sfile
) != 1 ) {
437 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
442 * dump the normalized raw arc information
444 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
445 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
446 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
447 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
448 arc
.raw_count
= arcp
-> arc_count
;
449 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
454 if ( debug
& SAMPLEDEBUG
) {
455 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
456 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
467 if ( p1
-> value
< p2
-> value
) {
470 if ( p1
-> value
> p2
-> value
) {
483 samples
= (UNIT
*) malloc (sampbytes
* sizeof(UNIT
));
485 fprintf( stderr
, "%s: No room for %d sample pc's\n",
486 whoami
, sampbytes
/ sizeof (UNIT
));
489 memset (samples
, 0, sampbytes
* sizeof(UNIT
));
491 for (i
= 0; i
< nsamples
; i
++) {
492 fread(&sample
, sizeof (UNIT
), 1, pfile
);
493 sample
= bfd_get_16 (abfd
, &sample
);
496 samples
[i
] += sample
;
500 "%s: unexpected EOF after reading %d/%d samples\n",
501 whoami
, --i
, nsamples
);
507 * Assign samples to the procedures to which they belong.
509 * There are three cases as to where pcl and pch can be
510 * with respect to the routine entry addresses svalue0 and svalue1
511 * as shown in the following diagram. overlap computes the
512 * distance between the arrows, the fraction of the sample
513 * that is to be credited to the routine which starts at svalue0.
519 * +-----------------------------------------------+
521 * | ->| |<- ->| |<- ->| |<- |
523 * +---------+ +---------+ +---------+
527 * pcl pch pcl pch pcl pch
529 * For the vax we assert that samples will never fall in the first
530 * two bytes of any routine, since that is the entry mask,
531 * thus we give call alignentries() to adjust the entry points if
532 * the entry mask falls in one bucket but the code for the routine
533 * doesn't start until the next bucket. In conjunction with the
534 * alignment of routine addresses, this should allow us to have
535 * only one sample for every four bytes of text space and never
536 * have any overlap (the two end cases, above).
543 unsigned long pcl
, pch
;
545 unsigned long overlap
;
546 unsigned long svalue0
, svalue1
;
548 /* read samples and assign to namelist symbols */
549 scale
= highpc
- lowpc
;
552 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
556 pcl
= lowpc
+ scale
* i
;
557 pch
= lowpc
+ scale
* (i
+ 1);
560 if ( debug
& SAMPLEDEBUG
) {
561 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
566 for (j
= j
- 1; j
< nname
; j
++) {
567 svalue0
= nl
[j
].svalue
;
568 svalue1
= nl
[j
+1].svalue
;
570 * if high end of tick is below entry address,
576 * if low end of tick into next routine,
577 * go for next routine.
581 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
584 if (debug
& SAMPLEDEBUG
) {
585 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
586 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
588 overlap
* time
/ scale
, overlap
);
591 nl
[j
].time
+= overlap
* time
/ scale
;
596 if (debug
& SAMPLEDEBUG
) {
597 printf("[asgnsamples] totime %f\n", totime
);
622 * calculate scaled entry point addresses (to save time in asgnsamples),
623 * and possibly push the scaled entry points over the entry mask,
624 * if it turns out that the entry point is in one bucket and the code
625 * for a routine is in the next bucket.
629 register struct nl
*nlp
;
630 unsigned long bucket_of_entry
;
631 unsigned long bucket_of_code
;
633 for (nlp
= nl
; nlp
< npe
; nlp
++) {
634 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
635 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
636 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
637 if (bucket_of_entry
< bucket_of_code
) {
639 if (debug
& SAMPLEDEBUG
) {
640 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
641 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
644 nlp
->svalue
+= UNITS_TO_CODE
;
653 extern char *strtab
; /* string table from a.out */
654 extern int aflag
; /* if static functions aren't desired */
659 * must be a text symbol,
660 * and static text symbols don't qualify if aflag set.
668 if (!aflag
&& (symp
->flags
&BSF_LOCAL
)) {
670 fprintf (stderr
, "%s(%d): %s: not a function\n", __FILE__
, __LINE__
, symp
->name
);
676 * can't have any `funny' characters in name,
677 * where `funny' includes `.', .o file names
678 * and `$', pascal labels.
683 for (name
= symp
->name
; *name
; name
++) {
684 if ( *name
== '.' || *name
== '$' ) {
689 i
= bfd_decode_symclass (symp
);
690 #if defined(DEBUG) && 0
691 if (i
!= 'T' && i
!= 't')
692 fprintf (stderr
, "%s(%d): %s is of class %c\n", __FILE__
, __LINE__
, symp
->name
, i
);
695 return (i
== 'T' || i
== 't');
This page took 0.044206 seconds and 4 git commands to generate.