proc: support reading captured variables of closures (#3682)
Supports showing captured variables for function closures on versions of Go that export informations about the closure struct (Go >= 1.23) Fixes #3612
This commit is contained in:
parent
6f1f549c71
commit
bbcea6b9f4
25
_fixtures/closurecontents.go
Normal file
25
_fixtures/closurecontents.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func makeAcc(scale int) func(x int) int {
|
||||
a := 0
|
||||
return func(x int) int {
|
||||
a += x * scale
|
||||
return a
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
acc := makeAcc(3)
|
||||
runtime.Breakpoint()
|
||||
fmt.Println(acc(1))
|
||||
runtime.Breakpoint()
|
||||
fmt.Println(acc(2))
|
||||
runtime.Breakpoint()
|
||||
fmt.Println(acc(6))
|
||||
runtime.Breakpoint()
|
||||
}
|
@ -27,6 +27,7 @@ const (
|
||||
AttrGoRuntimeType dwarf.Attr = 0x2904
|
||||
AttrGoPackageName dwarf.Attr = 0x2905
|
||||
AttrGoDictIndex dwarf.Attr = 0x2906
|
||||
AttrGoClosureOffset dwarf.Attr = 0x2907
|
||||
)
|
||||
|
||||
// Basic type encodings -- the value for AttrEncoding in a TagBaseType Entry.
|
||||
|
@ -24,6 +24,7 @@ const (
|
||||
VariablesSkipInlinedSubroutines
|
||||
VariablesTrustDeclLine
|
||||
VariablesNoDeclLineCheck
|
||||
VariablesOnlyCaptured
|
||||
)
|
||||
|
||||
// Variables returns a list of variables contained inside 'root'.
|
||||
|
@ -503,6 +503,8 @@ type Function struct {
|
||||
|
||||
// InlinedCalls lists all inlined calls to this function
|
||||
InlinedCalls []InlinedCall
|
||||
// closureStructType is the cached struct type for closures for this function
|
||||
closureStructTypeCached *godwarf.StructType
|
||||
}
|
||||
|
||||
// instRange returns the indexes in fn.Name of the type parameter
|
||||
@ -639,6 +641,45 @@ func (fn *Function) privateRuntime() bool {
|
||||
return len(name) > n && name[:n] == "runtime." && !('A' <= name[n] && name[n] <= 'Z')
|
||||
}
|
||||
|
||||
func (fn *Function) closureStructType(bi *BinaryInfo) *godwarf.StructType {
|
||||
if fn.closureStructTypeCached != nil {
|
||||
return fn.closureStructTypeCached
|
||||
}
|
||||
dwarfTree, err := fn.cu.image.getDwarfTree(fn.offset)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
st := &godwarf.StructType{
|
||||
Kind: "struct",
|
||||
}
|
||||
vars := reader.Variables(dwarfTree, 0, 0, reader.VariablesNoDeclLineCheck|reader.VariablesSkipInlinedSubroutines)
|
||||
for _, v := range vars {
|
||||
off, ok := v.Val(godwarf.AttrGoClosureOffset).(int64)
|
||||
if ok {
|
||||
n, _ := v.Val(dwarf.AttrName).(string)
|
||||
typ, err := v.Type(fn.cu.image.dwarf, fn.cu.image.index, fn.cu.image.typeCache)
|
||||
if err == nil {
|
||||
sz := typ.Common().ByteSize
|
||||
st.Field = append(st.Field, &godwarf.StructField{
|
||||
Name: n,
|
||||
Type: typ,
|
||||
ByteOffset: off,
|
||||
ByteSize: sz,
|
||||
BitOffset: off * 8,
|
||||
BitSize: sz * 8,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(st.Field) > 0 {
|
||||
lf := st.Field[len(st.Field)-1]
|
||||
st.ByteSize = lf.ByteOffset + lf.Type.Common().ByteSize
|
||||
}
|
||||
fn.closureStructTypeCached = st
|
||||
return st
|
||||
}
|
||||
|
||||
type constantsMap map[dwarfRef]*constantType
|
||||
|
||||
type constantType struct {
|
||||
|
@ -6178,3 +6178,40 @@ func TestPanicLine(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestReadClosure(t *testing.T) {
|
||||
if !goversion.VersionAfterOrEqual(runtime.Version(), 1, 23) {
|
||||
t.Skip("not implemented")
|
||||
}
|
||||
withTestProcess("closurecontents", t, func(p *proc.Target, grp *proc.TargetGroup, fixture protest.Fixture) {
|
||||
avalues := []int64{0, 3, 9, 27}
|
||||
for i := 0; i < 4; i++ {
|
||||
assertNoError(grp.Continue(), t, "Continue()")
|
||||
accV := evalVariable(p, t, "acc")
|
||||
t.Log(api.ConvertVar(accV).MultilineString("", ""))
|
||||
if len(accV.Children) != 2 {
|
||||
t.Error("wrong number of children")
|
||||
} else {
|
||||
found := 0
|
||||
for j := range accV.Children {
|
||||
v := &accV.Children[j]
|
||||
switch v.Name {
|
||||
case "scale":
|
||||
found++
|
||||
if val, _ := constant.Int64Val(v.Value); val != 3 {
|
||||
t.Error("wrong value for scale")
|
||||
}
|
||||
case "a":
|
||||
found++
|
||||
if val, _ := constant.Int64Val(v.Value); val != avalues[i] {
|
||||
t.Errorf("wrong value for a: %d", val)
|
||||
}
|
||||
}
|
||||
}
|
||||
if found != 2 {
|
||||
t.Error("wrong captured variables")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
@ -1389,8 +1389,15 @@ func (v *Variable) loadValueInternal(recurseLevel int, cfg LoadConfig) {
|
||||
break
|
||||
}
|
||||
f, _ := v.toField(field)
|
||||
f.Name = field.Name
|
||||
if t.StructName == "" && len(f.Name) > 0 && f.Name[0] == '&' && f.Kind == reflect.Ptr {
|
||||
// This struct is a closure struct and the field is actually a variable
|
||||
// captured by reference.
|
||||
f = f.maybeDereference()
|
||||
f.Flags |= VariableEscaped
|
||||
f.Name = field.Name[1:]
|
||||
}
|
||||
v.Children = append(v.Children, *f)
|
||||
v.Children[i].Name = field.Name
|
||||
v.Children[i].loadValueInternal(recurseLevel+1, cfg)
|
||||
}
|
||||
}
|
||||
@ -1435,7 +1442,7 @@ func (v *Variable) loadValueInternal(recurseLevel int, cfg LoadConfig) {
|
||||
v.FloatSpecial = FloatIsNaN
|
||||
}
|
||||
case reflect.Func:
|
||||
v.readFunctionPtr()
|
||||
v.loadFunctionPtr(recurseLevel, cfg)
|
||||
default:
|
||||
v.Unreadable = fmt.Errorf("unknown or unsupported kind: %q", v.Kind.String())
|
||||
}
|
||||
@ -1915,7 +1922,7 @@ func (v *Variable) writeCopy(srcv *Variable) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (v *Variable) readFunctionPtr() {
|
||||
func (v *Variable) loadFunctionPtr(recurseLevel int, cfg LoadConfig) {
|
||||
// dereference pointer to find function pc
|
||||
v.closureAddr = v.funcvalAddr()
|
||||
if v.Unreadable != nil {
|
||||
@ -1941,6 +1948,14 @@ func (v *Variable) readFunctionPtr() {
|
||||
}
|
||||
|
||||
v.Value = constant.MakeString(fn.Name)
|
||||
cst := fn.closureStructType(v.bi)
|
||||
v.Len = int64(len(cst.Field))
|
||||
|
||||
if recurseLevel <= cfg.MaxVariableRecurse {
|
||||
v2 := v.newVariable("", v.closureAddr, cst, v.mem)
|
||||
v2.loadValueInternal(recurseLevel, cfg)
|
||||
v.Children = v2.Children
|
||||
}
|
||||
}
|
||||
|
||||
// funcvalAddr reads the address of the funcval contained in a function variable.
|
||||
|
@ -176,6 +176,15 @@ func (v *Variable) writeTo(buf io.Writer, flags prettyFlags, indent, fmtstr stri
|
||||
fmt.Fprint(buf, "nil")
|
||||
} else {
|
||||
fmt.Fprintf(buf, "%s", v.Value)
|
||||
if flags.newlines() && len(v.Children) > 0 {
|
||||
fmt.Fprintf(buf, " {\n")
|
||||
for i := range v.Children {
|
||||
fmt.Fprintf(buf, "%s%s%s %s = ", indent, indentString, v.Children[i].Name, v.Children[i].typeStr(flags))
|
||||
v.Children[i].writeTo(buf, flags.set(prettyTop, false).set(prettyIncludeType, false), indent+indentString, fmtstr)
|
||||
fmt.Fprintf(buf, "\n")
|
||||
}
|
||||
fmt.Fprintf(buf, "%s}", indent)
|
||||
}
|
||||
}
|
||||
default:
|
||||
v.writeBasicType(buf, fmtstr)
|
||||
|
@ -321,12 +321,12 @@ type Variable struct {
|
||||
// Function variables will store the name of the function in this field
|
||||
Value string `json:"value"`
|
||||
|
||||
// Number of elements in an array or a slice, number of keys for a map, number of struct members for a struct, length of strings
|
||||
// Number of elements in an array or a slice, number of keys for a map, number of struct members for a struct, length of strings, number of captured variables for functions
|
||||
Len int64 `json:"len"`
|
||||
// Cap value for slices
|
||||
Cap int64 `json:"cap"`
|
||||
|
||||
// Array and slice elements, member fields of structs, key/value pairs of maps, value of complex numbers
|
||||
// Array and slice elements, member fields of structs, key/value pairs of maps, value of complex numbers, captured variables of functions.
|
||||
// The Name field in this slice will always be the empty string except for structs (when it will be the field name) and for complex numbers (when it will be "real" and "imaginary")
|
||||
// For maps each map entry will have to items in this slice, even numbered items will represent map keys and odd numbered items will represent their values
|
||||
// This field's length is capped at proc.maxArrayValues for slices and arrays and 2*proc.maxArrayValues for maps, in the circumstances where the cap takes effect len(Children) != Len
|
||||
|
@ -2746,7 +2746,7 @@ func (s *Session) convertVariableWithOpts(v *proc.Variable, qualifiedNameOrExpr
|
||||
variablesReference = maybeCreateVariableHandle(v)
|
||||
}
|
||||
}
|
||||
case reflect.Struct:
|
||||
case reflect.Struct, reflect.Func:
|
||||
if v.Len > int64(len(v.Children)) { // Not fully loaded
|
||||
if len(v.Children) == 0 { // Fully missing
|
||||
value = reloadVariable(v, qualifiedNameOrExpr)
|
||||
@ -2771,7 +2771,7 @@ func (s *Session) convertVariableWithOpts(v *proc.Variable, qualifiedNameOrExpr
|
||||
v.Children[1].Kind = reflect.Float64
|
||||
}
|
||||
fallthrough
|
||||
default: // Complex, Scalar, Chan, Func
|
||||
default: // Complex, Scalar, Chan
|
||||
if len(v.Children) > 0 {
|
||||
variablesReference = maybeCreateVariableHandle(v)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user