Skip to content

Commit f7195fa

Browse files
committed
gocore: skip non-Go CUs
gocore can only handle DIEs produced by the Go toolchain. Skip CUs from other languages, e.g. C++ via cgo. Change-Id: If1ca99065ac73efe6aeef6284bbfd9df6a37dccd Reviewed-on: https://go-review.googlesource.com/107777 Run-TryBot: Heschi Kreinick <heschi@google.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
1 parent 7fa577e commit f7195fa

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

gocore/dwarf.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func (p *Process) readDWARFTypes() {
2222
r := d.Reader()
2323
var types []*Type
2424
for e, err := r.Next(); e != nil && err == nil; e, err = r.Next() {
25+
if isNonGoCU(e) {
26+
r.SkipChildren()
27+
continue
28+
}
2529
switch e.Tag {
2630
case dwarf.TagArrayType, dwarf.TagPointerType, dwarf.TagStructType, dwarf.TagBaseType, dwarf.TagSubroutineType, dwarf.TagTypedef:
2731
dt, err := d.Type(e.Offset)
@@ -91,7 +95,6 @@ func (p *Process) readDWARFTypes() {
9195
}
9296

9397
// Copy info from base types into typedefs.
94-
r = d.Reader()
9598
for dt, t := range p.dwarfMap {
9699
tt, ok := dt.(*dwarf.TypedefType)
97100
if !ok {
@@ -185,6 +188,14 @@ func (p *Process) readDWARFTypes() {
185188
}
186189
}
187190

191+
func isNonGoCU(e *dwarf.Entry) bool {
192+
if e.Tag != dwarf.TagCompileUnit {
193+
return false
194+
}
195+
prod := e.AttrField(dwarf.AttrProducer).Val.(string)
196+
return !strings.Contains(prod, "Go cmd/compile")
197+
}
198+
188199
// dwarfSize is used to compute the size of a DWARF type.
189200
// dt.Size() is wrong when it returns a negative number.
190201
// This function implements just enough to correct the bad behavior.
@@ -201,7 +212,7 @@ func dwarfSize(dt dwarf.Type, ptrSize int64) int64 {
201212
case *dwarf.TypedefType:
202213
return dwarfSize(x.Type, ptrSize)
203214
default:
204-
panic(fmt.Sprintf("unhandled: %T", x))
215+
panic(fmt.Sprintf("unhandled: %s, %T", x, x))
205216
}
206217
}
207218

@@ -378,6 +389,11 @@ func (p *Process) readGlobals() {
378389
d, _ := p.proc.DWARF()
379390
r := d.Reader()
380391
for e, err := r.Next(); e != nil && err == nil; e, err = r.Next() {
392+
if isNonGoCU(e) {
393+
r.SkipChildren()
394+
continue
395+
}
396+
381397
if e.Tag != dwarf.TagVariable {
382398
continue
383399
}
@@ -435,6 +451,11 @@ func (p *Process) readStackVars() {
435451
d, _ := p.proc.DWARF()
436452
r := d.Reader()
437453
for e, err := r.Next(); e != nil && err == nil; e, err = r.Next() {
454+
if isNonGoCU(e) {
455+
r.SkipChildren()
456+
continue
457+
}
458+
438459
if e.Tag == dwarf.TagSubprogram {
439460
lowpc := e.AttrField(dwarf.AttrLowpc)
440461
highpc := e.AttrField(dwarf.AttrHighpc)

0 commit comments

Comments
 (0)