Made gprof use bfd instead. This is the first step in allowing cross-hosted

gprof use.
This commit is contained in:
Sean Eric Fagan 1991-07-22 19:33:46 +00:00
parent 39ada6fced
commit 811e3c6a86
2 changed files with 123 additions and 111 deletions

View File

@ -185,35 +185,38 @@ main(argc, argv)
*/ */
getnfile() getnfile()
{ {
FILE *nfile; bfd *abfd;
int valcmp(); int valcmp();
abfd = bfd_openr (a_outname, NULL);
if (abfd == NULL) {
perror (a_outname);
done();
}
if (!bfd_check_format (abfd, bfd_object)) {
fprintf (stderr, "%s: %s: bad format\n", whoami, a_outname);
done();
}
/* getstrtab(nfile); */
getsymtab(abfd);
gettextspace( abfd );
qsort(nl, nname, sizeof(nltype), valcmp);
nfile = fopen( a_outname ,"r");
if (nfile == NULL) {
perror( a_outname );
done();
}
fread(&xbuf, 1, sizeof(xbuf), nfile);
if (N_BADMAG(xbuf)) {
fprintf(stderr, "%s: %s: bad format\n", whoami , a_outname );
done();
}
getstrtab(nfile);
getsymtab(nfile);
gettextspace( nfile );
qsort(nl, nname, sizeof(nltype), valcmp);
fclose(nfile);
# ifdef DEBUG # ifdef DEBUG
if ( debug & AOUTDEBUG ) { if ( debug & AOUTDEBUG ) {
register int j; register int j;
for (j = 0; j < nname; j++){ for (j = 0; j < nname; j++){
printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name); printf("[getnfile] 0X%08x\t%s\n", nl[j].value, nl[j].name);
} }
} }
# endif DEBUG # endif DEBUG
} }
#if 0
getstrtab(nfile) getstrtab(nfile)
FILE *nfile; FILE *nfile;
{ {
@ -236,30 +239,33 @@ getstrtab(nfile)
done(); done();
} }
} }
#endif /* 0 */
/* /*
* Read in symbol table * Read in symbol table
*/ */
getsymtab(nfile) getsymtab(abfd)
FILE *nfile; bfd *abfd;
{ {
register long i; register long i;
int askfor; int askfor;
struct nlist nbuf; int nosyms;
asymbol **syms;
i = get_symtab_upper_bound (abfd); /* This will probably give us more
* than we need, but that's ok.
*/
syms = malloc (i);
nosyms = bfd_canonicalize_symtab (abfd, syms);
/* pass1 - count symbols */
fseek(nfile, (long)N_SYMOFF(xbuf), 0);
nname = 0; nname = 0;
for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { for (i = 0; i < nosyms; i++) {
fread(&nbuf, sizeof(nbuf), 1, nfile); if (!funcsymbol (syms[i]))
if ( ! funcsymbol( &nbuf ) ) { continue;
continue; nname++;
}
nname++;
} }
if (nname == 0) { if (nname == 0) {
fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname ); fprintf(stderr, "%s: %s: no symbols\n", whoami , a_outname );
done(); done();
} }
askfor = nname + 1; askfor = nname + 1;
nl = (nltype *) calloc( askfor , sizeof(nltype) ); nl = (nltype *) calloc( askfor , sizeof(nltype) );
@ -270,66 +276,65 @@ getsymtab(nfile)
} }
/* pass2 - read symbols */ /* pass2 - read symbols */
fseek(nfile, (long)N_SYMOFF(xbuf), 0);
npe = nl; npe = nl;
nname = 0; nname = 0;
for (i = xbuf.a_syms; i > 0; i -= sizeof(struct nlist)) { for (i = 0; i < nosyms; i++) {
fread(&nbuf, sizeof(nbuf), 1, nfile); if (!funcsymbol (syms[i])) {
if ( ! funcsymbol( &nbuf ) ) {
# ifdef DEBUG # ifdef DEBUG
if ( debug & AOUTDEBUG ) { if ( debug & AOUTDEBUG ) {
printf( "[getsymtab] rejecting: 0x%x %s\n" , printf( "[getsymtab] rejecting: 0x%x %s\n" ,
nbuf.n_type , strtab + nbuf.n_un.n_strx ); syms[i]->value, syms[i]->name);
}
# endif DEBUG
continue;
} }
npe->value = nbuf.n_value; # endif DEBUG
npe->name = strtab+nbuf.n_un.n_strx; continue;
}
npe->value = syms[i]->value + syms[i]->section->vma;
npe->name = syms[i]->name;
# ifdef DEBUG # ifdef DEBUG
if ( debug & AOUTDEBUG ) { if ( debug & AOUTDEBUG ) {
printf( "[getsymtab] %d %s 0x%08x\n" , printf( "[getsymtab] %d %s 0x%08x\n" ,
nname , npe -> name , npe -> value ); nname , npe -> name , npe -> value );
} }
# endif DEBUG # endif DEBUG
npe++; npe++;
nname++; nname++;
} }
npe->value = -1; npe->value = -1;
} }
/* /*
* read in the text space of an a.out file * read in the text space of an a.out file
*/ */
gettextspace( nfile ) gettextspace( abfd )
FILE *nfile; bfd *abfd;
{ {
char *malloc(); asection *texsec;
if ( cflag == 0 ) { if ( cflag == 0 ) {
return; return;
} }
textspace = (u_char *) malloc( xbuf.a_text );
if ( textspace == 0 ) { texsec = bfd_get_section_by_name (abfd, ".text");
fprintf( stderr , "%s: ran out room for %d bytes of text space: " , if (texsec == NULL) {
whoami , xbuf.a_text ); return;
fprintf( stderr , "can't do -c\n" ); }
return;
} textspace = (u_char *) malloc( texsec->size );
(void) fseek( nfile , N_TXTOFF( xbuf ) , 0 );
if ( fread( textspace , 1 , xbuf.a_text , nfile ) != xbuf.a_text ) { if ( textspace == 0 ) {
fprintf( stderr , "%s: couldn't read text space: " , whoami ); fprintf( stderr , "%s: ran out room for %d bytes of text space: " ,
fprintf( stderr , "can't do -c\n" ); whoami , texsec->size);
free( textspace ); fprintf( stderr , "can't do -c\n" );
textspace = 0; return;
return; }
} bfd_get_section_contents (abfd, texsec, textspace, texsec->filepos,
texsec->size);
} }
/* /*
* information from a gmon.out file is in two parts: * information from a gmon.out file is in two parts:
* an array of sampling hits within pc ranges, * an array of sampling hits within pc ranges,
* and the arcs. * and the arcs.
*/ */
getpfile(filename) getpfile(filename)
char *filename; char *filename;
{ {
@ -656,32 +661,39 @@ alignentries()
} }
bool bool
funcsymbol( nlistp ) funcsymbol( symp )
struct nlist *nlistp; asymbol *symp;
{ {
extern char *strtab; /* string table from a.out */ extern char *strtab; /* string table from a.out */
extern int aflag; /* if static functions aren't desired */ extern int aflag; /* if static functions aren't desired */
char *name; char *name;
/* /*
* must be a text symbol, * must be a text symbol,
* and static text symbols don't qualify if aflag set. * and static text symbols don't qualify if aflag set.
*/ */
if ( ! ( ( nlistp -> n_type == ( N_TEXT | N_EXT ) )
|| ( ( nlistp -> n_type == N_TEXT ) && ( aflag == 0 ) ) ) ) { if (!symp->section)
return FALSE; return FALSE;
if (!aflag && (symp->flags&BSF_LOCAL)) {
#ifdef DEBUG
fprintf (stderr, "%s(%d): %s: not a function\n", __FILE__, __LINE__, symp->name);
#endif
return FALSE;
}
/*
* can't have any `funny' characters in name,
* where `funny' includes `.', .o file names
* and `$', pascal labels.
*/
for (name = symp->name; *name; name++) {
if ( *name == '.' || *name == '$' ) {
return FALSE;
} }
/* }
* can't have any `funny' characters in name, return TRUE;
* where `funny' includes `.', .o file names
* and `$', pascal labels.
*/
for ( name = strtab + nlistp -> n_un.n_strx ; *name ; name += 1 ) {
if ( *name == '.' || *name == '$' ) {
return FALSE;
}
}
return TRUE;
} }
done() done()

View File

@ -19,10 +19,11 @@
* @(#)gprof.h 5.9 (Berkeley) 6/1/90 * @(#)gprof.h 5.9 (Berkeley) 6/1/90
*/ */
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <a.out.h> #include "bfd.h"
#include <stdio.h>
#include "gmon.h" #include "gmon.h"
#ifdef MACHINE_H #ifdef MACHINE_H
@ -164,7 +165,6 @@ double scale; /* scale factor converting samples to pc
values: each sample covers scale bytes */ values: each sample covers scale bytes */
char *strtab; /* string table in core */ char *strtab; /* string table in core */
off_t ssiz; /* size of the string table */ off_t ssiz; /* size of the string table */
struct exec xbuf; /* exec header of a.out */
unsigned char *textspace; /* text space of a.out in core */ unsigned char *textspace; /* text space of a.out in core */
/* /*