Skip to content

Commit

Permalink
internal/gocore: use AttrGoKind to fix named slice type
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
WangLeonard authored and gopherbot committed Sep 10, 2024
1 parent fbc6857 commit 2917a7a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
14 changes: 10 additions & 4 deletions internal/gocore/dwarf.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"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()
Expand All @@ -34,6 +38,9 @@ func (p *Process) readDWARFTypes() {
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)
}
Expand Down Expand Up @@ -112,13 +119,12 @@ func (p *Process) readDWARFTypes() {
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
Expand Down
7 changes: 6 additions & 1 deletion internal/gocore/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package gocore

import (
"fmt"
"reflect"
"regexp"
"strings"

Expand All @@ -18,7 +19,11 @@ import (
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
Expand Down

0 comments on commit 2917a7a

Please sign in to comment.