Clean up the dwarf code.
This commit is contained in:
parent
87a478a361
commit
95f57b01e2
5 changed files with 50 additions and 54 deletions
|
|
@ -392,7 +392,8 @@ int dwarflookuptag(Dwarf*, ulong, ulong, DwarfSym*);
|
||||||
int dwarfenumunit(Dwarf*, ulong, DwarfSym*);
|
int dwarfenumunit(Dwarf*, ulong, DwarfSym*);
|
||||||
int dwarfseeksym(Dwarf*, ulong, ulong, DwarfSym*);
|
int dwarfseeksym(Dwarf*, ulong, ulong, DwarfSym*);
|
||||||
int dwarfenum(Dwarf*, DwarfSym*);
|
int dwarfenum(Dwarf*, DwarfSym*);
|
||||||
int dwarfnextsym(Dwarf*, DwarfSym*, int);
|
int dwarfnextsym(Dwarf*, DwarfSym*);
|
||||||
|
int dwarfnextsymat(Dwarf*, DwarfSym*, int);
|
||||||
int dwarfpctoline(Dwarf*, ulong, char**, char**, char**, ulong*, ulong*, ulong*);
|
int dwarfpctoline(Dwarf*, ulong, char**, char**, char**, ulong*, ulong*, ulong*);
|
||||||
int dwarfunwind(Dwarf*, ulong, DwarfExpr*, DwarfExpr*, DwarfExpr*, int);
|
int dwarfunwind(Dwarf*, ulong, DwarfExpr*, DwarfExpr*, DwarfExpr*, int);
|
||||||
ulong dwarfget1(DwarfBuf*);
|
ulong dwarfget1(DwarfBuf*);
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ main(int argc, char **argv)
|
||||||
if(dwarfenum(d, &s) < 0)
|
if(dwarfenum(d, &s) < 0)
|
||||||
sysfatal("dwarfenumall: %r");
|
sysfatal("dwarfenumall: %r");
|
||||||
|
|
||||||
while(dwarfnextsym(d, &s, 1) == 1){
|
while(dwarfnextsym(d, &s) == 1){
|
||||||
switch(s.attrs.tag){
|
switch(s.attrs.tag){
|
||||||
case TagCompileUnit:
|
case TagCompileUnit:
|
||||||
print("compileunit %s\n", s.attrs.name);
|
print("compileunit %s\n", s.attrs.name);
|
||||||
|
|
|
||||||
|
|
@ -121,13 +121,10 @@ dwarflookupnameinunit(Dwarf *d, ulong unit, char *name, DwarfSym *s)
|
||||||
if(dwarfenumunit(d, unit, s) < 0)
|
if(dwarfenumunit(d, unit, s) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
dwarfnextsym(d, s, 1); /* s is now the CompileUnit */
|
dwarfnextsymat(d, s, 0); /* s is now the CompileUnit */
|
||||||
if(dwarfnextsym(d, s, 1) == 1){ /* s is now the first child of the compile unit */
|
while(dwarfnextsymat(d, s, 1) == 1)
|
||||||
do{
|
if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
|
||||||
if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
|
return 0;
|
||||||
return 0;
|
|
||||||
}while(dwarfnextsym(d, s, 0) == 1);
|
|
||||||
}
|
|
||||||
werrstr("symbol '%s' not found", name);
|
werrstr("symbol '%s' not found", name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -137,12 +134,9 @@ int
|
||||||
dwarflookupsubname(Dwarf *d, DwarfSym *parent, char *name, DwarfSym *s)
|
dwarflookupsubname(Dwarf *d, DwarfSym *parent, char *name, DwarfSym *s)
|
||||||
{
|
{
|
||||||
*s = *parent;
|
*s = *parent;
|
||||||
dwarfnextsym(d, s, 1);
|
while(dwarfnextsymat(d, s, parent->depth+1))
|
||||||
if(s->depth == parent->depth+1)
|
if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
|
||||||
do{
|
return 0;
|
||||||
if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
|
|
||||||
return 0;
|
|
||||||
}while(dwarfnextsym(d, s, 0) == 1);
|
|
||||||
werrstr("symbol '%s' not found", name);
|
werrstr("symbol '%s' not found", name);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -153,16 +147,12 @@ dwarflookuptag(Dwarf *d, ulong unit, ulong tag, DwarfSym *s)
|
||||||
if(dwarfenumunit(d, unit, s) < 0)
|
if(dwarfenumunit(d, unit, s) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
dwarfnextsym(d, s, 1); /* s is now the CompileUnit */
|
dwarfnextsymat(d, s, 0); /* s is now the CompileUnit */
|
||||||
if(s->attrs.tag == tag)
|
if(s->attrs.tag == tag)
|
||||||
return 0;
|
return 0;
|
||||||
|
while(dwarfnextsymat(d, s, 1) == 1)
|
||||||
if(dwarfnextsym(d, s, 1) == 1){ /* s is now the first child of the compile unit */
|
if(s->attrs.tag == tag)
|
||||||
do{
|
return 0;
|
||||||
if(s->attrs.tag == tag)
|
|
||||||
return 0;
|
|
||||||
}while(dwarfnextsym(d, s, 0) == 1);
|
|
||||||
}
|
|
||||||
werrstr("symbol with tag 0x%lux not found", tag);
|
werrstr("symbol with tag 0x%lux not found", tag);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
@ -173,7 +163,7 @@ dwarfseeksym(Dwarf *d, ulong unit, ulong off, DwarfSym *s)
|
||||||
if(dwarfenumunit(d, unit, s) < 0)
|
if(dwarfenumunit(d, unit, s) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
s->b.p = d->info.data + unit + off;
|
s->b.p = d->info.data + unit + off;
|
||||||
if(dwarfnextsym(d, s, 1) != 1)
|
if(dwarfnextsymat(d, s, 0) != 1)
|
||||||
return -1;
|
return -1;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
@ -184,17 +174,15 @@ dwarflookupfn(Dwarf *d, ulong unit, ulong pc, DwarfSym *s)
|
||||||
if(dwarfenumunit(d, unit, s) < 0)
|
if(dwarfenumunit(d, unit, s) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if(dwarfnextsym(d, s, 1) != 1)
|
if(dwarfnextsymat(d, s, 0) != 1)
|
||||||
return -1;
|
return -1;
|
||||||
/* s is now the CompileUnit */
|
/* s is now the CompileUnit */
|
||||||
|
|
||||||
if(dwarfnextsym(d, s, 1) == 1){ /* s is now the first child of the compile unit */
|
while(dwarfnextsymat(d, s, 1) == 1){
|
||||||
do{
|
if(s->attrs.tag != TagSubprogram)
|
||||||
if(s->attrs.tag != TagSubprogram)
|
continue;
|
||||||
continue;
|
if(s->attrs.lowpc <= pc && pc < s->attrs.highpc)
|
||||||
if(s->attrs.lowpc <= pc && pc < s->attrs.highpc)
|
return 0;
|
||||||
return 0;
|
|
||||||
}while(dwarfnextsym(d, s, 0) == 1);
|
|
||||||
}
|
}
|
||||||
werrstr("fn containing pc 0x%lux not found", pc);
|
werrstr("fn containing pc 0x%lux not found", pc);
|
||||||
return -1;
|
return -1;
|
||||||
|
|
@ -248,8 +236,8 @@ dwarfenum(Dwarf *d, DwarfSym *s)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
int
|
||||||
_dwarfnextsym(Dwarf *d, DwarfSym *s)
|
dwarfnextsym(Dwarf *d, DwarfSym *s)
|
||||||
{
|
{
|
||||||
ulong num;
|
ulong num;
|
||||||
DwarfAbbrev *a;
|
DwarfAbbrev *a;
|
||||||
|
|
@ -288,31 +276,39 @@ top:
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
dwarfnextsym(Dwarf *d, DwarfSym *s, int recurse)
|
dwarfnextsymat(Dwarf *d, DwarfSym *s, int depth)
|
||||||
{
|
{
|
||||||
int r;
|
int r;
|
||||||
int depth;
|
DwarfSym t;
|
||||||
ulong sib;
|
uint sib;
|
||||||
|
|
||||||
if(recurse)
|
if(s->depth == depth && s->attrs.have.sibling){
|
||||||
return _dwarfnextsym(d, s);
|
|
||||||
|
|
||||||
depth = s->depth;
|
|
||||||
if(s->attrs.have.sibling){
|
|
||||||
sib = s->attrs.sibling;
|
sib = s->attrs.sibling;
|
||||||
if(sib < d->info.len && d->info.data+sib >= s->b.p)
|
if(sib < d->info.len && d->info.data+sib >= s->b.p)
|
||||||
s->b.p = d->info.data+sib;
|
s->b.p = d->info.data+sib;
|
||||||
s->attrs.haskids = 0;
|
s->attrs.haskids = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
do{
|
/*
|
||||||
r = _dwarfnextsym(d, s);
|
* The funny game with t and s make sure that
|
||||||
if(r <= 0)
|
* if we get to the end of a run of a particular
|
||||||
|
* depth, we leave s so that a call to nextsymat with depth-1
|
||||||
|
* will actually produce the desired guy. We could change
|
||||||
|
* the interface to dwarfnextsym instead, but I'm scared
|
||||||
|
* to touch it.
|
||||||
|
*/
|
||||||
|
t = *s;
|
||||||
|
for(;;){
|
||||||
|
if((r = dwarfnextsym(d, &t)) != 1)
|
||||||
return r;
|
return r;
|
||||||
}while(s->depth != depth);
|
if(t.depth < depth){
|
||||||
if(s->depth < depth)
|
/* went too far - nothing to see */
|
||||||
return 0;
|
return 0;
|
||||||
return 1;
|
}
|
||||||
|
*s = t;
|
||||||
|
if(t.depth == depth)
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
typedef struct Parse Parse;
|
typedef struct Parse Parse;
|
||||||
|
|
|
||||||
|
|
@ -44,7 +44,7 @@ _dwarfnametounit(Dwarf *d, char *name, DwarfBlock *bl, DwarfSym *s)
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
s->b.p = d->info.data + unit + off;
|
s->b.p = d->info.data + unit + off;
|
||||||
if(dwarfnextsym(d, s, 1) < 0)
|
if(dwarfnextsym(d, s) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
if(s->attrs.name==nil || strcmp(s->attrs.name, name)!=0){
|
if(s->attrs.name==nil || strcmp(s->attrs.name, name)!=0){
|
||||||
werrstr("unexpected name %#q in lookup for %#q", s->attrs.name, name);
|
werrstr("unexpected name %#q in lookup for %#q", s->attrs.name, name);
|
||||||
|
|
|
||||||
|
|
@ -119,7 +119,7 @@ dwarflenum(Fhdr *fhdr, Symbol *p, char *name, uint j, Loc l, Symbol *s)
|
||||||
depth = 1;
|
depth = 1;
|
||||||
|
|
||||||
bpoff = 8;
|
bpoff = 8;
|
||||||
while(dwarfnextsym(fhdr->dwarf, &ds, 1) == 1 && depth < ds.depth){
|
while(dwarfnextsym(fhdr->dwarf, &ds) == 1 && depth < ds.depth){
|
||||||
if(ds.attrs.tag != TagVariable){
|
if(ds.attrs.tag != TagVariable){
|
||||||
if(ds.attrs.tag != TagFormalParameter
|
if(ds.attrs.tag != TagFormalParameter
|
||||||
&& ds.attrs.tag != TagUnspecifiedParameters)
|
&& ds.attrs.tag != TagUnspecifiedParameters)
|
||||||
|
|
@ -200,9 +200,8 @@ dwarfsyminit(Fhdr *fp)
|
||||||
if(dwarfenum(d, &s) < 0)
|
if(dwarfenum(d, &s) < 0)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
while(dwarfnextsym(d, &s, s.depth!=1) == 1){
|
dwarfnextsymat(d, &s, 0);
|
||||||
if(s.depth != 1)
|
while(dwarfnextsymat(d, &s, 1) == 1){
|
||||||
continue;
|
|
||||||
if(s.attrs.name == nil)
|
if(s.attrs.name == nil)
|
||||||
continue;
|
continue;
|
||||||
switch(s.attrs.tag){
|
switch(s.attrs.tag){
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue