-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtroop_globals.go
89 lines (73 loc) · 1.45 KB
/
troop_globals.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package goof
import (
"debug/dwarf"
"reflect"
"regexp"
"sort"
"strings"
"unsafe"
"github.com/zeebo/errs"
)
var statictmpRe = regexp.MustCompile(`statictmp_\d+$`)
func (t *Troop) addGlobals() error {
reader := t.data.Reader()
for {
entry, err := reader.Next()
if err != nil {
return errs.Wrap(err)
}
if entry == nil {
break
}
if entry.Tag != dwarf.TagVariable {
continue
}
name, ok := entry.Val(dwarf.AttrName).(string)
if !ok {
continue
}
// filter out some values that aren't useful and just clutter stuff
if strings.Contains(name, "·") {
continue
}
if statictmpRe.MatchString(name) {
continue
}
loc, err := entryLocation(t.data, entry)
if err != nil {
continue
}
dtyp, err := entryType(t.data, entry)
if err != nil {
continue
}
dname := dwarfTypeName(dtyp)
if dname == "<unspecified>" || dname == "" {
continue
}
rtyp := t.types[dname]
if rtyp == nil {
continue
}
ptr := unsafe.Pointer(uintptr(loc))
t.globals[name] = reflect.NewAt(rtyp, ptr).Elem()
}
return nil
}
func (t *Troop) Globals() ([]string, error) {
if err := t.check(); err != nil {
return nil, err
}
out := make([]string, 0, len(t.globals))
for name := range t.globals {
out = append(out, name)
}
sort.Strings(out)
return out, nil
}
func (t *Troop) Global(name string) (reflect.Value, error) {
if err := t.check(); err != nil {
return reflect.Value{}, t.err
}
return t.globals[name], nil
}