internal/gocore: use AttrGoKind to fix named slice type Use AttrGoKind instead of the name matching to check the slice and string type. For golang/go#57447. Change-Id: I8765aa1a6315609b3476b8b84c27130629847235 Reviewed-on: https://go-review.googlesource.com/c/debug/+/593680 Reviewed-by: Keith Randall <khr@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com> Reviewed-by: Tim King <taking@google.com> Auto-Submit: Keith Randall <khr@golang.org> Reviewed-by: Keith Randall <khr@google.com>
diff --git a/internal/gocore/dwarf.go b/internal/gocore/dwarf.go index 463b096..967531a 100644 --- a/internal/gocore/dwarf.go +++ b/internal/gocore/dwarf.go
@@ -15,6 +15,10 @@ "golang.org/x/debug/internal/core" ) +const ( + AttrGoKind dwarf.Attr = 0x2900 +) + // read DWARF types from core dump. func (p *Process) readDWARFTypes() { d, _ := p.proc.DWARF() @@ -34,6 +38,9 @@ continue } t := &Type{Name: gocoreName(dt), Size: dwarfSize(dt, p.proc.PtrSize())} + if goKind, ok := e.Val(AttrGoKind).(int64); ok { + t.goKind = reflect.Kind(goKind) + } p.dwarfMap[dt] = t types = append(types, t) } @@ -112,13 +119,12 @@ if t.Kind != KindStruct { continue } - if t.Name == "string" { // TODO: also "struct runtime.stringStructDWARF" ? + switch t.goKind { + case reflect.String: t.Kind = KindString t.Elem = t.Fields[0].Type.Elem // TODO: check that it is always uint8. t.Fields = nil - } - if len(t.Name) >= 9 && t.Name[:9] == "struct []" || - len(t.Name) >= 2 && t.Name[:2] == "[]" { + case reflect.Slice: t.Kind = KindSlice t.Elem = t.Fields[0].Type.Elem t.Fields = nil
diff --git a/internal/gocore/type.go b/internal/gocore/type.go index 8da78bf..9f0b0ea 100644 --- a/internal/gocore/type.go +++ b/internal/gocore/type.go
@@ -6,6 +6,7 @@ import ( "fmt" + "reflect" "regexp" "strings" @@ -18,7 +19,11 @@ type Type struct { Name string Size int64 - Kind Kind + Kind Kind // common dwarf types. + // go-specific types obtained from AttrGoKind, such as string and slice. + // Kind and gokind are not correspond one to one, both need to be preserved now. + // For example, slices are described in dwarf by a 3-field struct, so its Kind is Struct and its goKind is Slice. + goKind reflect.Kind // Fields only valid for a subset of kinds. Count int64 // for kind == KindArray