-
Notifications
You must be signed in to change notification settings - Fork 1
/
dbg.go
262 lines (249 loc) · 5.81 KB
/
dbg.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package dmgo
import (
"fmt"
"reflect"
"strings"
)
const (
dbgStateNewCmd int = iota
dbgStateInCmd
dbgStateRunWithBreakpoints
dbgStateRunNoBreakpoints
)
const (
breakOpChange int = iota
breakOpEq
breakOpNeq
// TODO: add these once we parse vals/args into ints and such
// breakOpGt
// breakOpGte
// breakOpLt
// breakOpLte
)
var breakOpsMap = map[string]int{
"change": breakOpChange,
"=": breakOpEq,
"==": breakOpEq,
"!=": breakOpNeq,
// ">": breakOpGt,
// ">=": breakOpGte,
// "<": breakOpLt,
// "<=": breakOpLte,
}
type breakpoint struct {
fieldPath string
breakVal string
op int
}
type debugger struct {
keysJustPressed []rune
keys [256]bool
lineBuf []byte
state int
breakpoints []breakpoint
}
func lookupValue(root reflect.Value, lookups []string) (reflect.Value, bool) {
v := root
t := root.Type()
for i := range lookups {
if t.Kind() != reflect.Struct {
fmt.Println("field", lookups[i], "is not a struct but field name lookup was asked for")
}
_, ok := t.FieldByName(lookups[i])
if !ok {
fmt.Println("field", lookups[i], "not found")
return reflect.Value{}, false
}
v = v.FieldByName(lookups[i])
t = v.Type()
}
return v, true
}
func getField(emu Emulator, path string) (reflect.Value, bool) {
root := reflect.Indirect(reflect.ValueOf(emu))
return lookupValue(root, strings.Split(path, "."))
}
func getMethod(emu Emulator, path string) (reflect.Value, bool) {
root := reflect.Indirect(reflect.ValueOf(emu))
v := root
parts := strings.Split(path, ".")
if len(parts) > 1 {
var ok bool
v, ok = lookupValue(root, parts[:len(parts)-1])
if !ok {
return reflect.Value{}, false
}
}
t := v.Type()
var ok bool
_, ok = t.MethodByName(parts[len(parts)-1])
if ok {
return v.MethodByName(parts[len(parts)-1]), true
}
// also allow pointer receivers
v = v.Addr()
t = v.Type()
_, ok = t.MethodByName(parts[len(parts)-1])
if ok {
return v.MethodByName(parts[len(parts)-1]), true
}
fmt.Println("method not found or private")
return reflect.Value{}, false
}
func strIndexOf(strs []string, str string) int {
for i := range strs {
if strs[i] == str {
return i
}
}
return -1
}
var dbgCmdMap = map[string]func(*debugger, Emulator, []string){
"run": func(d *debugger, emu Emulator, arg []string) {
if len(d.breakpoints) > 0 {
d.state = dbgStateRunWithBreakpoints
} else {
d.state = dbgStateRunNoBreakpoints
}
},
"x": func(d *debugger, emu Emulator, arg []string) {
if len(arg) == 0 {
fmt.Println("usage: x FIELD_PATH")
return
}
if v, ok := getField(emu, arg[0]); ok {
fmt.Println(v)
}
},
"break": func(d *debugger, emu Emulator, arg []string) {
if len(arg) == 0 {
fmt.Println("usage: break FIELD_NAME OP [VAL]")
return
}
v, field_ok := getField(emu, arg[0])
if !field_ok {
return
}
opStr := "change"
if len(arg) > 1 {
opStr = arg[1]
}
op, op_ok := breakOpsMap[opStr]
if !op_ok {
fmt.Println("bad OP arg for break")
return
}
var valStr string
if op != breakOpChange {
if len(arg) < 3 {
fmt.Println("need val for break op of", opStr)
return
}
valStr = arg[2]
} else {
valStr = fmt.Sprintf("%v", v) // change works like != lastVal
}
bp := breakpoint{fieldPath: arg[0], op: op, breakVal: valStr}
d.breakpoints = append(d.breakpoints, bp)
},
"call": func(d *debugger, emu Emulator, arg []string) {
if len(arg) == 0 {
fmt.Println("usage: call METHOD_PATH")
return
}
if len(arg) > 1 {
fmt.Println("method args not yet impl")
return
}
if v, ok := getMethod(emu, arg[0]); ok {
results := v.Call([]reflect.Value{})
if len(results) > 0 {
fmt.Println(results)
}
}
},
}
func (d *debugger) step(emu Emulator) {
if d.state == dbgStateRunNoBreakpoints {
emu.Step()
} else if d.state == dbgStateRunWithBreakpoints {
for i := range d.breakpoints {
bp := &d.breakpoints[i]
f, ok := getField(emu, bp.fieldPath)
if !ok {
fmt.Println("couldn't find field listed in breakpoint, something screwy's going on...")
d.state = dbgStateNewCmd
return
}
valStr := fmt.Sprintf("%v", f)
// fmt.Println("checking bp for", bp.fieldPath, "val is", valStr)
switch bp.op {
case breakOpChange:
if valStr != bp.breakVal {
fmt.Println("hit breakpoint:", bp.fieldPath, "changed from", bp.breakVal, "to", valStr)
bp.breakVal = valStr
d.state = dbgStateNewCmd
return
}
case breakOpEq:
if valStr == bp.breakVal {
fmt.Println("hit breakpoint:", f, "==", valStr)
d.state = dbgStateNewCmd
return
}
case breakOpNeq:
if valStr != bp.breakVal {
fmt.Println("hit breakpoint:", f, "!=", bp.breakVal, "- now", valStr)
d.state = dbgStateNewCmd
return
}
default:
fmt.Println("unexpected bp op, something screwy's going on...")
d.state = dbgStateNewCmd
return
}
}
emu.Step()
} else if d.state == dbgStateNewCmd {
d.lineBuf = d.lineBuf[:0]
d.state = dbgStateInCmd
fmt.Printf("\n> ")
} else if d.state == dbgStateInCmd {
for _, r := range d.keysJustPressed {
switch r {
case '\b':
if len(d.lineBuf) > 0 {
d.lineBuf = d.lineBuf[:len(d.lineBuf)-1]
fmt.Print("\b \b")
}
case '\n':
fmt.Println()
d.state = dbgStateNewCmd
fields := strings.Fields(string(d.lineBuf))
if len(fields) > 0 {
if cmd, ok := dbgCmdMap[fields[0]]; ok {
cmd(d, emu, fields[1:])
} else {
fmt.Println("unknown cmd")
}
}
default:
d.lineBuf = append(d.lineBuf, byte(r))
fmt.Printf("%c", r)
}
}
d.keysJustPressed = d.keysJustPressed[:0]
}
}
func (d *debugger) updateInput(keys []bool) {
for i := range d.keys {
if keys[i] && !d.keys[i] {
if rune(i) == '`' {
d.state = dbgStateNewCmd
} else {
d.keysJustPressed = append(d.keysJustPressed, rune(i))
}
}
d.keys[i] = keys[i]
}
}