forked from libsv/go-bt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
debugger.go
317 lines (267 loc) · 9.73 KB
/
debugger.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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// Package debug comment
package debug
import "github.com/libsv/go-bt/v2/bscript/interpreter"
type (
// ThreadStateFunc debug handler for a threads state.
ThreadStateFunc func(state *interpreter.State)
// StackFunc debug handler for stack operations.
StackFunc func(state *interpreter.State, data []byte)
// ExecutionErrorFunc debug handler for execution failure.
ExecutionErrorFunc func(state *interpreter.State, err error)
)
type debugOpts struct {
rewind bool
}
// DefaultDebugger exposes attachment points via the way of functions, which
// are to be appended to via a series of function calls.
type DefaultDebugger interface {
AttachBeforeExecute(ThreadStateFunc)
AttachAfterExecute(ThreadStateFunc)
AttachBeforeStep(ThreadStateFunc)
AttachAfterStep(ThreadStateFunc)
AttachBeforeExecuteOpcode(ThreadStateFunc)
AttachAfterExecuteOpcode(ThreadStateFunc)
AttachBeforeScriptChange(ThreadStateFunc)
AttachAfterScriptChange(ThreadStateFunc)
AttachAfterSuccess(ThreadStateFunc)
AttachAfterError(ExecutionErrorFunc)
AttachBeforeStackPush(StackFunc)
AttachAfterStackPush(StackFunc)
AttachBeforeStackPop(ThreadStateFunc)
AttachAfterStackPop(StackFunc)
interpreter.Debugger
}
type debugger struct {
beforeExecuteFns []ThreadStateFunc
afterExecuteFns []ThreadStateFunc
beforeStepFns []ThreadStateFunc
afterStepFns []ThreadStateFunc
beforeExecuteOpcodeFns []ThreadStateFunc
afterExecuteOpcodeFns []ThreadStateFunc
beforeScriptChangeFns []ThreadStateFunc
afterScriptChangeFns []ThreadStateFunc
afterSuccessFns []ThreadStateFunc
afterErrorFns []ExecutionErrorFunc
beforeStackPushFns []StackFunc
afterStackPushFns []StackFunc
beforeStackPopFns []ThreadStateFunc
afterStackPopFns []StackFunc
}
// NewDebugger returns an empty debugger which is to be configured with the `Attach`
// functions.
//
// Example usage:
//
// debugger := debug.NewDebugger()
// debugger.AttachBeforeExecuteOpcode(func (state *interpreter.State) {
// fmt.Println(state.DataStack)
// })
// debugger.AttachAfterStackPush(func (state *interpreter.State, data []byte) {
// fmt.Println(hex.EncodeToString(data))
// })
// engine.Execute(interpreter.WithDebugger(debugger))
func NewDebugger(oo ...DebuggerOptionFunc) DefaultDebugger {
opts := &debugOpts{}
for _, o := range oo {
o(opts)
}
return &debugger{
beforeExecuteFns: make([]ThreadStateFunc, 0),
afterExecuteFns: make([]ThreadStateFunc, 0),
beforeStepFns: make([]ThreadStateFunc, 0),
afterStepFns: make([]ThreadStateFunc, 0),
beforeExecuteOpcodeFns: make([]ThreadStateFunc, 0),
afterExecuteOpcodeFns: make([]ThreadStateFunc, 0),
afterSuccessFns: make([]ThreadStateFunc, 0),
afterErrorFns: make([]ExecutionErrorFunc, 0),
beforeStackPushFns: make([]StackFunc, 0),
afterStackPushFns: make([]StackFunc, 0),
beforeStackPopFns: make([]ThreadStateFunc, 0),
afterStackPopFns: make([]StackFunc, 0),
}
}
// AttachBeforeExecute attach the provided function to be executed before
// interpreter execution.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeExecute(fn ThreadStateFunc) {
d.beforeExecuteFns = append(d.beforeExecuteFns, fn)
}
// AttachAfterExecute attach the provided function to be executed after
// all scripts have completed execution, but before the final stack value
// is checked.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterExecute(fn ThreadStateFunc) {
d.afterExecuteFns = append(d.afterExecuteFns, fn)
}
// AttachBeforeStep attach the provided function to be executed before a thread
// begins a step.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeStep(fn ThreadStateFunc) {
d.beforeStepFns = append(d.beforeStepFns, fn)
}
// AttachAfterStep attach the provided function to be executed after a thread
// finishes a step.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterStep(fn ThreadStateFunc) {
d.afterStepFns = append(d.afterStepFns, fn)
}
// AttachBeforeExecuteOpcode attach the provided function to be executed before
// an opcodes execution.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeExecuteOpcode(fn ThreadStateFunc) {
d.beforeExecuteOpcodeFns = append(d.beforeExecuteOpcodeFns, fn)
}
// AttachAfterExecuteOpcode attach the provided function to be executed after
// an opcodes execution.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterExecuteOpcode(fn ThreadStateFunc) {
d.afterExecuteOpcodeFns = append(d.afterExecuteOpcodeFns, fn)
}
// AttachBeforeScriptChange attach the provided function to be executed after
// a scripts execution has finished, just before the thread shifts to the
// next.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeScriptChange(fn ThreadStateFunc) {
d.beforeScriptChangeFns = append(d.beforeScriptChangeFns, fn)
}
// AttachAfterScriptChange attach the provided function to be executed after
// a scripts execution has finished, just after the thread shifts to the
// next.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterScriptChange(fn ThreadStateFunc) {
d.afterScriptChangeFns = append(d.afterScriptChangeFns, fn)
}
// AttachAfterSuccess attach the provided function to be executed on
// successful execution.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterSuccess(fn ThreadStateFunc) {
d.afterSuccessFns = append(d.afterSuccessFns, fn)
}
// AttachAfterError attach the provided function to be executed on execution
// error.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterError(fn ExecutionErrorFunc) {
d.afterErrorFns = append(d.afterErrorFns, fn)
}
// AttachBeforeStackPush attach the provided function to be executed just before
// a push to a stack.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeStackPush(fn StackFunc) {
d.beforeStackPushFns = append(d.beforeStackPushFns, fn)
}
// AttachAfterStackPush attach the provided function to be executed immediately
// after a push to a stack.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterStackPush(fn StackFunc) {
d.afterStackPushFns = append(d.afterStackPushFns, fn)
}
// AttachBeforeStackPop attach the provided function to be executed just before
// a pop to a stack.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachBeforeStackPop(fn ThreadStateFunc) {
d.beforeStackPopFns = append(d.beforeStackPopFns, fn)
}
// AttachAfterStackPop attach the provided function to be executed immediately
// after a pop from a stack.
// If this is called multiple times, provided funcs are executed on a
// FIFO basis.
func (d *debugger) AttachAfterStackPop(fn StackFunc) {
d.afterStackPopFns = append(d.afterStackPopFns, fn)
}
// BeforeExecute execute all before execute attachments.
func (d *debugger) BeforeExecute(state *interpreter.State) {
for _, fn := range d.beforeExecuteFns {
fn(state)
}
}
// AfterExecute execute all after execute attachments.
func (d *debugger) AfterExecute(state *interpreter.State) {
for _, fn := range d.afterExecuteFns {
fn(state)
}
}
// BeforeStep execute all before step attachments.
func (d *debugger) BeforeStep(state *interpreter.State) {
for _, fn := range d.beforeStepFns {
fn(state)
}
}
// AfterStep execute all after step attachments.
func (d *debugger) AfterStep(state *interpreter.State) {
for _, fn := range d.afterStepFns {
fn(state)
}
}
// BeforeExecuteOpcode execute all before execute opcode attachments.
func (d *debugger) BeforeExecuteOpcode(state *interpreter.State) {
for _, fn := range d.beforeExecuteOpcodeFns {
fn(state)
}
}
// AfterExecuteOpcode execute all after execute opcode attachments.
func (d *debugger) AfterExecuteOpcode(state *interpreter.State) {
for _, fn := range d.afterExecuteOpcodeFns {
fn(state)
}
}
// BeforeScriptChange execute all before script change attachments.
func (d *debugger) BeforeScriptChange(state *interpreter.State) {
for _, fn := range d.beforeScriptChangeFns {
fn(state)
}
}
// AfterScriptChange execute all after script change attachments.
func (d *debugger) AfterScriptChange(state *interpreter.State) {
for _, fn := range d.afterScriptChangeFns {
fn(state)
}
}
// AfterSuccess execute all after success attachments.
func (d *debugger) AfterSuccess(state *interpreter.State) {
for _, fn := range d.afterSuccessFns {
fn(state)
}
}
// AfterError execute all after error attachments.
func (d *debugger) AfterError(state *interpreter.State, err error) {
for _, fn := range d.afterErrorFns {
fn(state, err)
}
}
// BeforeStackPush execute all before stack push attachments.
func (d *debugger) BeforeStackPush(state *interpreter.State, data []byte) {
for _, fn := range d.beforeStackPushFns {
fn(state, data)
}
}
// AfterStackPush execute all after stack push attachments.
func (d *debugger) AfterStackPush(state *interpreter.State, data []byte) {
for _, fn := range d.afterStackPushFns {
fn(state, data)
}
}
// BeforeStackPop execute all before stack pop attachments.
func (d *debugger) BeforeStackPop(state *interpreter.State) {
for _, fn := range d.beforeStackPopFns {
fn(state)
}
}
// AfterStackPop execute all after stack pop attachments.
func (d *debugger) AfterStackPop(state *interpreter.State, data []byte) {
for _, fn := range d.afterStackPopFns {
fn(state, data)
}
}