github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/dwarf/reader/variables.go (about)

     1  package reader
     2  
     3  import (
     4  	"debug/dwarf"
     5  
     6  	"github.com/go-delve/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  		// Variables are considered to be visible starting on the line after the
    58  		// line they are declared on. Function arguments are an exception - the line
    59  		// they are declared on does not matter; they are considered to be
    60  		// visible throughout the function.
    61  		declLine, varHasDeclLine := root.Val(dwarf.AttrDeclLine).(int64)
    62  		checkDeclLine :=
    63  			root.Tag != dwarf.TagFormalParameter && // var is not a function argument
    64  				varHasDeclLine && // we know the DeclLine
    65  				(flags&VariablesNoDeclLineCheck == 0) // we were not explicitly instructed to ignore DeclLine
    66  
    67  		varVisible := !checkDeclLine || (line >= int(declLine)+1) // +1 because visibility starts on the line after DeclLine
    68  		if varVisible {
    69  			return append(v, Variable{root, depth})
    70  		}
    71  		return v
    72  	}
    73  }