version 1.6, 2000/12/24 00:28:46 |
version 1.7, 2001/03/22 05:18:30 |
|
|
|
|
#include "gprof.h" |
#include "gprof.h" |
|
|
char *whoami = "gprof"; |
|
|
|
/* |
/* |
* things which get -E excluded by default. |
* things which get -E excluded by default. |
*/ |
*/ |
|
|
|
|
static struct gmonhdr gmonhdr; |
static struct gmonhdr gmonhdr; |
|
|
|
int |
main(argc, argv) |
main(argc, argv) |
int argc; |
int argc; |
char **argv; |
char **argv; |
|
|
#if defined(__i386__) || defined(__vax__) || defined(__tahoe__) || defined(__sparc__) |
#if defined(__i386__) || defined(__vax__) || defined(__tahoe__) || defined(__sparc__) |
cflag = TRUE; |
cflag = TRUE; |
#else |
#else |
fprintf(stderr, "gprof: -c isn't supported on this architecture yet\n"); |
fprintf(stderr, "%s: -c isn't supported on this architecture yet\n", __progname); |
exit(1); |
exit(1); |
#endif |
#endif |
break; |
break; |
|
|
# ifdef DEBUG |
# ifdef DEBUG |
printf("[main] debug = %d\n", debug); |
printf("[main] debug = %d\n", debug); |
# else not DEBUG |
# else not DEBUG |
printf("%s: -d ignored\n", whoami); |
warnx("-d ignored"); |
# endif DEBUG |
# endif DEBUG |
break; |
break; |
case 'E': |
case 'E': |
|
|
*/ |
*/ |
if (hz == 0) { |
if (hz == 0) { |
hz = 1; |
hz = 1; |
fprintf(stderr, "time is in ticks, not seconds\n"); |
warnx("time is in ticks, not seconds"); |
} |
} |
/* |
/* |
* dump out a gmon.sum file if requested |
* dump out a gmon.sum file if requested |
|
|
* print the index |
* print the index |
*/ |
*/ |
printindex(); |
printindex(); |
done(); |
|
|
return (0); |
} |
} |
|
|
/* |
/* |
|
|
* and optionally the text space. |
* and optionally the text space. |
* On return symbol table is sorted by value. |
* On return symbol table is sorted by value. |
*/ |
*/ |
|
void |
getnfile() |
getnfile() |
{ |
{ |
FILE *nfile; |
FILE *nfile; |
int valcmp(); |
int valcmp(); |
|
|
nfile = fopen( a_outname ,"r"); |
nfile = fopen( a_outname ,"r"); |
if (nfile == NULL) { |
if (nfile == NULL) |
perror( a_outname ); |
err(1, "fopen: %s", a_outname); |
done(); |
|
} |
|
fread(&xbuf, 1, sizeof(xbuf), nfile); |
fread(&xbuf, 1, sizeof(xbuf), nfile); |
if (N_BADMAG(xbuf)) { |
if (N_BADMAG(xbuf)) |
fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname ); |
errx(1, "%s: bad format", a_outname ); |
done(); |
|
} |
|
getstrtab(nfile); |
getstrtab(nfile); |
getsymtab(nfile); |
getsymtab(nfile); |
gettextspace( nfile ); |
gettextspace( nfile ); |
|
|
# endif DEBUG |
# endif DEBUG |
} |
} |
|
|
|
void |
getstrtab(nfile) |
getstrtab(nfile) |
FILE *nfile; |
FILE *nfile; |
{ |
{ |
|
|
fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0); |
fseek(nfile, (long)(N_SYMOFF(xbuf) + xbuf.a_syms), 0); |
if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) { |
if (fread(&ssiz, sizeof (ssiz), 1, nfile) == 0) |
fprintf(stderr, "%s: %s: no string table (old format?)\n" , |
errx(1, "%s: no string table (old format?)" , a_outname); |
whoami , a_outname ); |
|
done(); |
|
} |
|
strtab = calloc(ssiz, 1); |
strtab = calloc(ssiz, 1); |
if (strtab == NULL) { |
if (strtab == NULL) |
fprintf(stderr, "%s: %s: no room for %d bytes of string table\n", |
errx(1, "%s: no room for %ld bytes of string table", a_outname , ssiz); |
whoami , a_outname , ssiz); |
if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) |
done(); |
err(1, "%s: reading string table", a_outname); |
} |
|
if (fread(strtab+sizeof(ssiz), ssiz-sizeof(ssiz), 1, nfile) != 1) { |
|
fprintf(stderr, "%s: %s: error reading string table\n", |
|
whoami , a_outname ); |
|
done(); |
|
} |
|
} |
} |
|
|
/* |
/* |
* Read in symbol table |
* Read in symbol table |
*/ |
*/ |
|
void |
getsymtab(nfile) |
getsymtab(nfile) |
FILE *nfile; |
FILE *nfile; |
{ |
{ |
|
|
} |
} |
nname++; |
nname++; |
} |
} |
if (nname == 0) { |
if (nname == 0) |
fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname ); |
errx(1, "%s: no symbols", a_outname); |
done(); |
|
} |
|
askfor = nname + 1; |
askfor = nname + 1; |
nl = (nltype *) calloc( askfor , sizeof(nltype) ); |
nl = (nltype *) calloc( askfor , sizeof(nltype) ); |
if (nl == 0) { |
if (nl == 0) |
fprintf(stderr, "%s: No room for %d bytes of symbol table\n", |
errx(1, "No room for %d bytes of symbol table", |
whoami, askfor * sizeof(nltype) ); |
askfor * sizeof(nltype)); |
done(); |
|
} |
|
|
|
/* pass2 - read symbols */ |
/* pass2 - read symbols */ |
fseek(nfile, (long)N_SYMOFF(xbuf), 0); |
fseek(nfile, (long)N_SYMOFF(xbuf), 0); |
|
|
/* |
/* |
* read in the text space of an a.out file |
* read in the text space of an a.out file |
*/ |
*/ |
|
void |
gettextspace( nfile ) |
gettextspace( nfile ) |
FILE *nfile; |
FILE *nfile; |
{ |
{ |
|
|
} |
} |
textspace = (u_char *) malloc( xbuf.a_text ); |
textspace = (u_char *) malloc( xbuf.a_text ); |
if ( textspace == 0 ) { |
if ( textspace == 0 ) { |
fprintf( stderr , "%s: ran out room for %d bytes of text space: " , |
warnx("ran out room for %d bytes of text space: can't do -c", xbuf.a_text ); |
whoami , xbuf.a_text ); |
|
fprintf( stderr , "can't do -c\n" ); |
|
return; |
return; |
} |
} |
(void) fseek( nfile , N_TXTOFF( xbuf ) , 0 ); |
(void) fseek( nfile , N_TXTOFF( xbuf ) , 0 ); |
if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { |
if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { |
fprintf( stderr , "%s: couldn't read text space: " , whoami ); |
warnx("couldn't read text space: can't do -c"); |
fprintf( stderr , "can't do -c\n" ); |
|
free( textspace ); |
free( textspace ); |
textspace = 0; |
textspace = NULL; |
return; |
return; |
} |
} |
} |
} |
|
|
* an array of sampling hits within pc ranges, |
* an array of sampling hits within pc ranges, |
* and the arcs. |
* and the arcs. |
*/ |
*/ |
|
void |
getpfile(filename) |
getpfile(filename) |
char *filename; |
char *filename; |
{ |
{ |
|
|
int size; |
int size; |
int rate; |
int rate; |
|
|
if((pfile = fopen(filename, "r")) == NULL) { |
if((pfile = fopen(filename, "r")) == NULL) |
perror(filename); |
err(1, "fopen: %s", filename); |
done(); |
|
} |
|
fread(&tmp, sizeof(struct gmonhdr), 1, pfile); |
fread(&tmp, sizeof(struct gmonhdr), 1, pfile); |
if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc || |
if ( s_highpc != 0 && ( tmp.lpc != gmonhdr.lpc || |
tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt ) ) { |
tmp.hpc != gmonhdr.hpc || tmp.ncnt != gmonhdr.ncnt)) |
fprintf(stderr, "%s: incompatible with first gmon file\n", filename); |
errx(1, "%s: incompatible with first gmon file", filename); |
done(); |
|
} |
|
gmonhdr = tmp; |
gmonhdr = tmp; |
if ( gmonhdr.version == GMONVERSION ) { |
if ( gmonhdr.version == GMONVERSION ) { |
rate = gmonhdr.profrate; |
rate = gmonhdr.profrate; |
|
|
} |
} |
if (hz == 0) { |
if (hz == 0) { |
hz = rate; |
hz = rate; |
} else if (hz != rate) { |
} else if (hz != rate) |
fprintf(stderr, |
errx(1, "%s: profile clock rate (%d) incompatible with clock rate " |
"%s: profile clock rate (%d) %s (%d) in first gmon file\n", |
"(%ld) in first gmon file", filename, rate, hz); |
filename, rate, "incompatible with clock rate", hz); |
|
done(); |
|
} |
|
s_lowpc = (unsigned long) gmonhdr.lpc; |
s_lowpc = (unsigned long) gmonhdr.lpc; |
s_highpc = (unsigned long) gmonhdr.hpc; |
s_highpc = (unsigned long) gmonhdr.hpc; |
lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT); |
lowpc = (unsigned long)gmonhdr.lpc / sizeof(UNIT); |
|
|
return(pfile); |
return(pfile); |
} |
} |
|
|
|
void |
tally( rawp ) |
tally( rawp ) |
struct rawarc *rawp; |
struct rawarc *rawp; |
{ |
{ |
|
|
/* |
/* |
* dump out the gmon.sum file |
* dump out the gmon.sum file |
*/ |
*/ |
|
void |
dumpsum( sumfile ) |
dumpsum( sumfile ) |
char *sumfile; |
char *sumfile; |
{ |
{ |
|
|
struct rawarc arc; |
struct rawarc arc; |
FILE *sfile; |
FILE *sfile; |
|
|
if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) { |
if ( ( sfile = fopen ( sumfile , "w" ) ) == NULL ) |
perror( sumfile ); |
err(1, "fopen: %s", sumfile); |
done(); |
|
} |
|
/* |
/* |
* dump the header; use the last header read in |
* dump the header; use the last header read in |
*/ |
*/ |
if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) { |
if ( fwrite( &gmonhdr , sizeof gmonhdr , 1 , sfile ) != 1 ) |
perror( sumfile ); |
err(1, "fwrite: %s", sumfile); |
done(); |
|
} |
|
/* |
/* |
* dump the samples |
* dump the samples |
*/ |
*/ |
if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) { |
if (fwrite(samples, sizeof (UNIT), nsamples, sfile) != nsamples) |
perror( sumfile ); |
err(1, "fwrite: %s", sumfile); |
done(); |
|
} |
|
/* |
/* |
* dump the normalized raw arc information |
* dump the normalized raw arc information |
*/ |
*/ |
|
|
arc.raw_frompc = arcp -> arc_parentp -> value; |
arc.raw_frompc = arcp -> arc_parentp -> value; |
arc.raw_selfpc = arcp -> arc_childp -> value; |
arc.raw_selfpc = arcp -> arc_childp -> value; |
arc.raw_count = arcp -> arc_count; |
arc.raw_count = arcp -> arc_count; |
if ( fwrite ( &arc , sizeof arc , 1 , sfile ) != 1 ) { |
if (fwrite ( &arc , sizeof arc , 1 , sfile ) != 1) |
perror( sumfile ); |
err(1, "fwrite: %s", sumfile); |
done(); |
|
} |
|
# ifdef DEBUG |
# ifdef DEBUG |
if ( debug & SAMPLEDEBUG ) { |
if ( debug & SAMPLEDEBUG ) { |
printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" , |
printf( "[dumpsum] frompc 0x%x selfpc 0x%x count %d\n" , |
|
|
fclose( sfile ); |
fclose( sfile ); |
} |
} |
|
|
|
int |
valcmp(p1, p2) |
valcmp(p1, p2) |
nltype *p1, *p2; |
nltype *p1, *p2; |
{ |
{ |
|
|
return EQUALTO; |
return EQUALTO; |
} |
} |
|
|
|
void |
readsamples(pfile) |
readsamples(pfile) |
FILE *pfile; |
FILE *pfile; |
{ |
{ |
register i; |
|
UNIT sample; |
UNIT sample; |
|
register int i; |
|
|
if (samples == 0) { |
if (samples == 0) { |
samples = (UNIT *) calloc(sampbytes, sizeof (UNIT)); |
samples = (UNIT *) calloc(sampbytes, sizeof (UNIT)); |
if (samples == 0) { |
if (samples == 0) |
fprintf( stderr , "%s: No room for %d sample pc's\n", |
errx(1, "No room for %d sample pc's", sampbytes / sizeof (UNIT)); |
whoami , sampbytes / sizeof (UNIT)); |
|
done(); |
|
} |
|
} |
} |
for (i = 0; i < nsamples; i++) { |
for (i = 0; i < nsamples; i++) { |
fread(&sample, sizeof (UNIT), 1, pfile); |
fread(&sample, sizeof (UNIT), 1, pfile); |
|
|
break; |
break; |
samples[i] += sample; |
samples[i] += sample; |
} |
} |
if (i != nsamples) { |
if (i != nsamples) |
fprintf(stderr, |
errx(1, "unexpected EOF after reading %d/%d samples", i, nsamples ); |
"%s: unexpected EOF after reading %d/%d samples\n", |
|
whoami , i , nsamples ); |
|
done(); |
|
} |
|
} |
} |
|
|
/* |
/* |
|
|
* only one sample for every four bytes of text space and never |
* only one sample for every four bytes of text space and never |
* have any overlap (the two end cases, above). |
* have any overlap (the two end cases, above). |
*/ |
*/ |
|
void |
asgnsamples() |
asgnsamples() |
{ |
{ |
register int j; |
register int j; |
|
|
* if it turns out that the entry point is in one bucket and the code |
* if it turns out that the entry point is in one bucket and the code |
* for a routine is in the next bucket. |
* for a routine is in the next bucket. |
*/ |
*/ |
|
void |
alignentries() |
alignentries() |
{ |
{ |
register struct nl *nlp; |
register struct nl *nlp; |
|
|
return TRUE; |
return TRUE; |
} |
} |
#endif |
#endif |
while ( c = *name++ ) { |
while ((c = *name++)) |
if ( c == '.' ) { |
if (c == '.') |
return FALSE; |
return FALSE; |
} |
|
} |
|
return TRUE; |
|
} |
|
|
|
done() |
return TRUE; |
{ |
|
|
|
exit(0); |
|
} |
} |