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 };
41 int discard_underscores
= 1; /* Should we discard initial underscores? */
42 int bsd_style_output
= 0; /* As opposed to FSF style output */
55 while ( *argv
!= 0 && **argv
== '-' ) {
70 debug
|= atoi( *argv
);
73 printf("[main] debug = %d\n", debug
);
75 printf("%s: -d ignored\n", whoami
);
80 addlist( Elist
, *argv
);
82 addlist( elist
, *argv
);
86 addlist( elist
, *++argv
);
91 addlist( Flist
, *argv
);
93 addlist( flist
, *argv
);
97 addlist( flist
, *++argv
);
101 addlist( kfromlist
, *++argv
);
102 addlist( ktolist
, *++argv
);
108 case 'T': /* "Traditional" output format */
109 bsd_style_output
= 1;
121 a_outname
= A_OUTNAME
;
130 * turn off default functions
132 for ( sp
= &defaultEs
[0] ; *sp
; sp
++ ) {
134 addlist( Elist
, *sp
);
136 addlist( elist
, *sp
);
139 * how many ticks per second?
140 * if we can't tell, report time in ticks.
145 fprintf(stderr
, "time is in ticks, not seconds\n");
148 * get information about a.out file.
152 * get information about mon.out file(s).
155 getpfile( gmonname
);
159 } while ( *argv
++ != 0 );
161 * dump out a gmon.sum file if requested
167 * assign samples to procedures
171 * assemble the dynamic profile
173 timesortnlp
= doarcs();
175 if (bsd_style_output
) {
176 printgprof( timesortnlp
); /* print the dynamic profile */
177 printprof(); /* print the flat profile */
179 printprof(); /* print the flat profile */
180 printgprof( timesortnlp
); /* print the dynamic profile */
190 * Set up string and symbol tables from a.out.
191 * and optionally the text space.
192 * On return symbol table is sorted by value.
198 abfd
= bfd_openr (a_outname
, NULL
);
205 if (!bfd_check_format (abfd
, bfd_object
)) {
206 fprintf (stderr
, "%s: %s: bad format\n", whoami
, a_outname
);
210 /* getstrtab(nfile); */
212 gettextspace( abfd
);
213 qsort(nl
, nname
, sizeof(nltype
), valcmp
);
216 if ( debug
& AOUTDEBUG
) {
219 for (j
= 0; j
< nname
; j
++){
220 printf("[getnfile] 0X%08x\t%s\n", nl
[j
].value
, nl
[j
].name
);
227 * Read in symbol table
236 i
= get_symtab_upper_bound (abfd
); /* This will probably give us more
237 * than we need, but that's ok.
239 syms
= (asymbol
**)xmalloc (i
);
240 nosyms
= bfd_canonicalize_symtab (abfd
, syms
);
243 for (i
= 0; i
< nosyms
; i
++) {
244 if (!funcsymbol (syms
[i
]))
250 fprintf(stderr
, "%s: %s: no symbols\n", whoami
, a_outname
);
254 nl
= (nltype
*) calloc( askfor
, sizeof(nltype
) );
256 fprintf(stderr
, "%s: No room for %d bytes of symbol table\n",
257 whoami
, askfor
* sizeof(nltype
) );
261 /* pass2 - read symbols */
264 for (i
= 0; i
< nosyms
; i
++) {
265 if (!funcsymbol (syms
[i
])) {
267 if ( debug
& AOUTDEBUG
) {
268 printf( "[getsymtab] rejecting: 0x%x %s\n" ,
269 syms
[i
]->value
, syms
[i
]->name
);
274 /* Symbol offsets are always section-relative. */
275 npe
->value
= syms
[i
]->value
+ syms
[i
]->section
->vma
;
276 npe
->name
= syms
[i
]->name
;
278 /* If we see "main" without an initial '_', we assume
279 names are *not* prefixed by '_'. */
280 if (npe
->name
[0] == 'm' && discard_underscores
281 && strcmp(npe
->name
, "main") == 0)
282 discard_underscores
= 0;
285 if ( debug
& AOUTDEBUG
) {
286 printf( "[getsymtab] %d %s 0x%08x\n" ,
287 nname
, npe
-> name
, npe
-> value
);
297 * read in the text space of an a.out file
308 texsec
= bfd_get_section_by_name (abfd
, ".text");
309 if (texsec
== NULL
) {
313 textspace
= (u_char
*) malloc( texsec
->_cooked_size
);
315 if ( textspace
== 0 ) {
316 fprintf( stderr
, "%s: ran out room for %d bytes of text space: " ,
317 whoami
, texsec
->_cooked_size
);
318 fprintf( stderr
, "can't do -c\n" );
321 bfd_get_section_contents (abfd
, texsec
, textspace
, texsec
->filepos
,
322 texsec
->_cooked_size
);
325 * information from a gmon.out file is in two parts:
326 * an array of sampling hits within pc ranges,
336 pfile
= openpfile(filename
);
339 * the rest of the file consists of
340 * a bunch of <from,self,count> tuples.
342 while ( fread( &arc
, sizeof arc
, 1 , pfile
) == 1 ) {
343 arc
.raw_frompc
= bfd_get_32 (abfd
, (bfd_byte
*) &arc
.raw_frompc
);
344 arc
.raw_selfpc
= bfd_get_32 (abfd
, (bfd_byte
*) &arc
.raw_selfpc
);
345 arc
.raw_count
= bfd_get_32 (abfd
, (bfd_byte
*) &arc
.raw_count
);
347 if ( debug
& SAMPLEDEBUG
) {
348 printf( "[getpfile] frompc 0x%x selfpc 0x%x count %d\n" ,
349 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
367 if((pfile
= fopen(filename
, "r")) == NULL
) {
371 fread(&tmp
, sizeof(struct hdr
), 1, pfile
);
372 tmp
.lowpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &tmp
.lowpc
);
373 tmp
.highpc
= (UNIT
*)bfd_get_32 (abfd
, (bfd_byte
*) &tmp
.highpc
);
374 tmp
.ncnt
= bfd_get_32 (abfd
, (bfd_byte
*) &tmp
.ncnt
);
376 if ( s_highpc
!= 0 && ( tmp
.lowpc
!= h
.lowpc
||
377 tmp
.highpc
!= h
.highpc
|| tmp
.ncnt
!= h
.ncnt
) ) {
378 fprintf(stderr
, "%s: incompatible with first gmon file\n", filename
);
382 s_lowpc
= (unsigned long) h
.lowpc
;
383 s_highpc
= (unsigned long) h
.highpc
;
384 lowpc
= (unsigned long)h
.lowpc
/ sizeof(UNIT
);
385 highpc
= (unsigned long)h
.highpc
/ sizeof(UNIT
);
386 sampbytes
= h
.ncnt
- sizeof(struct hdr
);
387 nsamples
= sampbytes
/ sizeof (UNIT
);
389 if ( debug
& SAMPLEDEBUG
) {
390 printf( "[openpfile] hdr.lowpc 0x%x hdr.highpc 0x%x hdr.ncnt %d\n",
391 h
.lowpc
, h
.highpc
, h
.ncnt
);
392 printf( "[openpfile] s_lowpc 0x%x s_highpc 0x%x\n" ,
393 s_lowpc
, s_highpc
);
394 printf( "[openpfile] lowpc 0x%x highpc 0x%x\n" ,
396 printf( "[openpfile] sampbytes %d nsamples %d\n" ,
397 sampbytes
, nsamples
);
409 parentp
= nllookup( rawp
-> raw_frompc
);
410 childp
= nllookup( rawp
-> raw_selfpc
);
412 && onlist( kfromlist
, parentp
-> name
)
413 && onlist( ktolist
, childp
-> name
) ) {
416 childp
-> ncall
+= rawp
-> raw_count
;
418 if ( debug
& TALLYDEBUG
) {
419 printf( "[tally] arc from %s to %s traversed %d times\n" ,
420 parentp
-> name
, childp
-> name
, rawp
-> raw_count
);
423 addarc( parentp
, childp
, rawp
-> raw_count
);
427 * dump out the gmon.sum file
432 register nltype
*nlp
;
433 register arctype
*arcp
;
437 if ( ( sfile
= fopen ( sumfile
, "w" ) ) == NULL
) {
442 * dump the header; use the last header read in
444 if ( fwrite( &h
, sizeof h
, 1 , sfile
) != 1 ) {
451 if (fwrite(samples
, sizeof (UNIT
), nsamples
, sfile
) != nsamples
) {
456 * dump the normalized raw arc information
458 for ( nlp
= nl
; nlp
< npe
; nlp
++ ) {
459 for ( arcp
= nlp
-> children
; arcp
; arcp
= arcp
-> arc_childlist
) {
460 arc
.raw_frompc
= arcp
-> arc_parentp
-> value
;
461 arc
.raw_selfpc
= arcp
-> arc_childp
-> value
;
462 arc
.raw_count
= arcp
-> arc_count
;
463 if ( fwrite ( &arc
, sizeof arc
, 1 , sfile
) != 1 ) {
468 if ( debug
& SAMPLEDEBUG
) {
469 printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" ,
470 arc
.raw_frompc
, arc
.raw_selfpc
, arc
.raw_count
);
481 if ( p1
-> value
< p2
-> value
) {
484 if ( p1
-> value
> p2
-> value
) {
497 samples
= (UNIT
*) malloc (sampbytes
* sizeof(UNIT
));
499 fprintf( stderr
, "%s: No room for %d sample pc's\n",
500 whoami
, sampbytes
/ sizeof (UNIT
));
503 memset (samples
, 0, sampbytes
* sizeof(UNIT
));
505 for (i
= 0; i
< nsamples
; i
++) {
506 fread(&sample
, sizeof (UNIT
), 1, pfile
);
507 sample
= bfd_get_16 (abfd
, (bfd_byte
*) &sample
);
510 samples
[i
] += sample
;
514 "%s: unexpected EOF after reading %d/%d samples\n",
515 whoami
, --i
, nsamples
);
521 * Assign samples to the procedures to which they belong.
523 * There are three cases as to where pcl and pch can be
524 * with respect to the routine entry addresses svalue0 and svalue1
525 * as shown in the following diagram. overlap computes the
526 * distance between the arrows, the fraction of the sample
527 * that is to be credited to the routine which starts at svalue0.
533 * +-----------------------------------------------+
535 * | ->| |<- ->| |<- ->| |<- |
537 * +---------+ +---------+ +---------+
541 * pcl pch pcl pch pcl pch
543 * For the vax we assert that samples will never fall in the first
544 * two bytes of any routine, since that is the entry mask,
545 * thus we give call alignentries() to adjust the entry points if
546 * the entry mask falls in one bucket but the code for the routine
547 * doesn't start until the next bucket. In conjunction with the
548 * alignment of routine addresses, this should allow us to have
549 * only one sample for every four bytes of text space and never
550 * have any overlap (the two end cases, above).
557 unsigned long pcl
, pch
;
559 unsigned long overlap
;
560 unsigned long svalue0
, svalue1
;
562 /* read samples and assign to namelist symbols */
563 scale
= highpc
- lowpc
;
566 for (i
= 0, j
= 1; i
< nsamples
; i
++) {
570 pcl
= lowpc
+ scale
* i
;
571 pch
= lowpc
+ scale
* (i
+ 1);
574 if ( debug
& SAMPLEDEBUG
) {
575 printf( "[asgnsamples] pcl 0x%x pch 0x%x ccnt %d\n" ,
580 for (j
= j
- 1; j
< nname
; j
++) {
581 svalue0
= nl
[j
].svalue
;
582 svalue1
= nl
[j
+1].svalue
;
584 * if high end of tick is below entry address,
590 * if low end of tick into next routine,
591 * go for next routine.
595 overlap
= min(pch
, svalue1
) - max(pcl
, svalue0
);
598 if (debug
& SAMPLEDEBUG
) {
599 printf("[asgnsamples] (0x%x->0x%x-0x%x) %s gets %f ticks %d overlap\n",
600 nl
[j
].value
/sizeof(UNIT
), svalue0
, svalue1
,
602 overlap
* time
/ scale
, overlap
);
605 nl
[j
].time
+= overlap
* time
/ scale
;
610 if (debug
& SAMPLEDEBUG
) {
611 printf("[asgnsamples] totime %f\n", totime
);
636 * calculate scaled entry point addresses (to save time in asgnsamples),
637 * and possibly push the scaled entry points over the entry mask,
638 * if it turns out that the entry point is in one bucket and the code
639 * for a routine is in the next bucket.
643 register struct nl
*nlp
;
644 unsigned long bucket_of_entry
;
645 unsigned long bucket_of_code
;
647 for (nlp
= nl
; nlp
< npe
; nlp
++) {
648 nlp
-> svalue
= nlp
-> value
/ sizeof(UNIT
);
649 bucket_of_entry
= (nlp
->svalue
- lowpc
) / scale
;
650 bucket_of_code
= (nlp
->svalue
+ UNITS_TO_CODE
- lowpc
) / scale
;
651 if (bucket_of_entry
< bucket_of_code
) {
653 if (debug
& SAMPLEDEBUG
) {
654 printf("[alignentries] pushing svalue 0x%x to 0x%x\n",
655 nlp
->svalue
, nlp
->svalue
+ UNITS_TO_CODE
);
658 nlp
->svalue
+= UNITS_TO_CODE
;
667 extern char *strtab
; /* string table from a.out */
668 extern int aflag
; /* if static functions aren't desired */
673 * must be a text symbol,
674 * and static text symbols don't qualify if aflag set.
681 if (!aflag
&& (symp
->flags
&BSF_LOCAL
)) {
683 fprintf (stderr
, "%s(%d): %s: not a function\n", __FILE__
, __LINE__
, symp
->name
);
688 * can't have any `funny' characters in name,
689 * where `funny' includes `.', .o file names
690 * and `$', pascal labels.
695 for (name
= symp
->name
; *name
; name
++) {
696 if ( *name
== '.' || *name
== '$' ) {
701 i
= bfd_decode_symclass (symp
);
702 #if defined(DEBUG) && 0
703 if (i
!= 'T' && i
!= 't')
704 fprintf (stderr
, "%s(%d): %s is of class %c\n", __FILE__
, __LINE__
, symp
->name
, i
);
707 return (i
== 'T' || i
== 't');
This page took 0.047872 seconds and 4 git commands to generate.