diff --git a/_fixtures/closurecontents.go b/_fixtures/closurecontents.go new file mode 100644 index 00000000..1317cab1 --- /dev/null +++ b/_fixtures/closurecontents.go @@ -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() +} diff --git a/pkg/dwarf/godwarf/type.go b/pkg/dwarf/godwarf/type.go index f0afb76c..f4d0bfaa 100644 --- a/pkg/dwarf/godwarf/type.go +++ b/pkg/dwarf/godwarf/type.go @@ -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. diff --git a/pkg/dwarf/reader/variables.go b/pkg/dwarf/reader/variables.go index b47b7011..601c6ba0 100644 --- a/pkg/dwarf/reader/variables.go +++ b/pkg/dwarf/reader/variables.go @@ -24,6 +24,7 @@ const ( VariablesSkipInlinedSubroutines VariablesTrustDeclLine VariablesNoDeclLineCheck + VariablesOnlyCaptured ) // Variables returns a list of variables contained inside 'root'. diff --git a/pkg/proc/bininfo.go b/pkg/proc/bininfo.go index bf89a712..6be55f82 100644 --- a/pkg/proc/bininfo.go +++ b/pkg/proc/bininfo.go @@ -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 { diff --git a/pkg/proc/proc_test.go b/pkg/proc/proc_test.go index 70f3a300..02be77f4 100644 --- a/pkg/proc/proc_test.go +++ b/pkg/proc/proc_test.go @@ -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") + } + } + } + }) +} diff --git a/pkg/proc/variables.go b/pkg/proc/variables.go index 854fe7ee..542a2057 100644 --- a/pkg/proc/variables.go +++ b/pkg/proc/variables.go @@ -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. diff --git a/service/api/prettyprint.go b/service/api/prettyprint.go index 2f6cc68e..001ab17f 100644 --- a/service/api/prettyprint.go +++ b/service/api/prettyprint.go @@ -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) diff --git a/service/api/types.go b/service/api/types.go index 4ffc919b..83930444 100644 --- a/service/api/types.go +++ b/service/api/types.go @@ -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 diff --git a/service/dap/server.go b/service/dap/server.go index 5e0956c9..b8565dce 100644 --- a/service/dap/server.go +++ b/service/dap/server.go @@ -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) }