github.com/undoio/delve@v1.9.0/pkg/dwarf/reader/variables.go (about) 1 package reader 2 3 import ( 4 "debug/dwarf" 5 6 "github.com/undoio/delve/pkg/dwarf/godwarf" 7 ) 8 9 type Variable struct { 10 *godwarf.Tree 11 // Depth represents the depth of the lexical block in which this variable 12 // was declared, relative to a root scope (e.g. a function) passed to 13 // Variables(). The depth is used to figure out if a variable is shadowed at 14 // a particular pc by another one with the same name declared in an inner 15 // block. 16 Depth int 17 } 18 19 // VariablesFlags specifies some configuration flags for the Variables function. 20 type VariablesFlags uint8 21 22 const ( 23 VariablesOnlyVisible VariablesFlags = 1 << iota 24 VariablesSkipInlinedSubroutines 25 VariablesTrustDeclLine 26 VariablesNoDeclLineCheck 27 ) 28 29 // Variables returns a list of variables contained inside 'root'. 30 // 31 // If the VariablesOnlyVisible flag is set, only variables visible at 'pc' will be 32 // returned. If the VariablesSkipInlinedSubroutines is set, variables from 33 // inlined subroutines will be skipped. 34 func Variables(root *godwarf.Tree, pc uint64, line int, flags VariablesFlags) []Variable { 35 return variablesInternal(nil, root, 0, pc, line, flags) 36 } 37 38 // variablesInternal appends to 'v' variables from 'root'. The function calls 39 // itself with an incremented scope for all sub-blocks in 'root'. 40 func variablesInternal(v []Variable, root *godwarf.Tree, depth int, pc uint64, line int, flags VariablesFlags) []Variable { 41 switch root.Tag { 42 case dwarf.TagInlinedSubroutine: 43 if flags&VariablesSkipInlinedSubroutines != 0 { 44 return v 45 } 46 fallthrough 47 case dwarf.TagLexDwarfBlock, dwarf.TagSubprogram: 48 // Recurse into blocks and functions, if the respective block contains 49 // pc (or if we don't care about visibility). 50 if (flags&VariablesOnlyVisible == 0) || root.ContainsPC(pc) { 51 for _, child := range root.Children { 52 v = variablesInternal(v, child, depth+1, pc, line, flags) 53 } 54 } 55 return v 56 default: 57 o := 0 58 if root.Tag != dwarf.TagFormalParameter && (flags&VariablesTrustDeclLine != 0) { 59 // visibility for variables starts the line after declaration line, 60 // except for formal parameters, which are visible on the same line they 61 // are defined. 62 o = 1 63 } 64 if declLine, ok := root.Val(dwarf.AttrDeclLine).(int64); (flags&VariablesNoDeclLineCheck != 0) || !ok || line >= int(declLine)+o { 65 return append(v, Variable{root, depth}) 66 } 67 return v 68 } 69 }