2017-05-04 14:35:31 +00:00
|
|
|
package reader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"debug/dwarf"
|
|
|
|
|
2020-03-20 17:23:10 +00:00
|
|
|
"github.com/go-delve/delve/pkg/dwarf/godwarf"
|
|
|
|
)
|
2019-06-17 16:51:29 +00:00
|
|
|
|
2020-03-20 17:23:10 +00:00
|
|
|
type Variable struct {
|
|
|
|
*godwarf.Tree
|
|
|
|
Depth int
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 17:23:10 +00:00
|
|
|
// Variables returns a list of variables contained inside 'root'.
|
|
|
|
// If onlyVisible is true only variables visible at pc will be returned.
|
|
|
|
// If skipInlinedSubroutines is true inlined subroutines will be skipped
|
|
|
|
func Variables(root *godwarf.Tree, pc uint64, line int, onlyVisible, skipInlinedSubroutines bool) []Variable {
|
|
|
|
return variablesInternal(nil, root, 0, pc, line, onlyVisible, skipInlinedSubroutines)
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-20 17:23:10 +00:00
|
|
|
func variablesInternal(v []Variable, root *godwarf.Tree, depth int, pc uint64, line int, onlyVisible, skipInlinedSubroutines bool) []Variable {
|
|
|
|
switch root.Tag {
|
|
|
|
case dwarf.TagInlinedSubroutine:
|
|
|
|
if skipInlinedSubroutines {
|
|
|
|
return v
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
2020-03-20 17:23:10 +00:00
|
|
|
fallthrough
|
|
|
|
case dwarf.TagLexDwarfBlock, dwarf.TagSubprogram:
|
|
|
|
if !onlyVisible || root.ContainsPC(pc) {
|
|
|
|
for _, child := range root.Children {
|
|
|
|
v = variablesInternal(v, child, depth+1, pc, line, onlyVisible, skipInlinedSubroutines)
|
2017-09-08 10:31:03 +00:00
|
|
|
}
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
2020-03-20 17:23:10 +00:00
|
|
|
return v
|
|
|
|
default:
|
|
|
|
if declLine, ok := root.Val(dwarf.AttrDeclLine).(int64); !ok || line >= int(declLine) {
|
|
|
|
return append(v, Variable{root, depth})
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
2020-03-20 17:23:10 +00:00
|
|
|
return v
|
2017-05-04 14:35:31 +00:00
|
|
|
}
|
|
|
|
}
|