This repository has been archived by the owner on Oct 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 96
/
debugger.go
304 lines (268 loc) · 8.2 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
package duktape
/*
#cgo !windows CFLAGS: -std=c99 -O3 -Wall -fomit-frame-pointer -fstrict-aliasing -Wno-unused-function
#cgo windows CFLAGS: -O3 -Wall -fomit-frame-pointer -fstrict-aliasing -Wno-unused-function
#cgo linux LDFLAGS: -lm
#cgo freebsd LDFLAGS: -lm
#cgo openbsd LDFLAGS: -lm
#cgo dragonfly LDFLAGS: -lm
#include "duktape.h"
#include <stdbool.h>
#include <stdio.h>
extern duk_size_t goDebugReadFunction(void *uData, char *buffer, duk_size_t length);
extern duk_size_t goDebugWriteFunction(void *uData, char *buffer, duk_size_t length);
extern duk_size_t goDebugPeekFunction(void *uData);
extern void goDebugReadFlushFunction(void *uData);
extern void goDebugWriteFlushFunction(void *uData);
extern duk_idx_t goDebugRequestFunction(duk_context *ctx, void *uData, duk_idx_t nvalues);
extern void goDebugDetachedFunction(duk_context *ctx, void *uData);
static void _duk_debugger_attach(duk_context *ctx, bool peek, bool readFlush, bool writeFlush, bool request, void *uData) {
duk_debugger_attach(
ctx,
goDebugReadFunction,
((duk_size_t (*)(void*, const char*, duk_size_t)) goDebugWriteFunction),
peek ? goDebugPeekFunction : NULL,
readFlush ? goDebugReadFlushFunction : NULL,
writeFlush ? goDebugWriteFlushFunction : NULL,
request ? goDebugRequestFunction : NULL,
goDebugDetachedFunction,
uData
);
}
*/
import "C"
import (
"errors"
"fmt"
"reflect"
"sync"
"unsafe"
)
type DebugReadFunc = func(uData unsafe.Pointer, buffer []byte) uint
type DebugWriteFunc = func(uData unsafe.Pointer, buffer []byte) uint
type DebugPeekFunc = func(uData unsafe.Pointer) uint
type DebugReadFlushFunc = func(uData unsafe.Pointer)
type DebugWriteFlushFunc = func(uData unsafe.Pointer)
type DebugRequestFunc = func(ctx *Context, uData unsafe.Pointer, nValues int) int
type DebugDetachedFunc = func(ctx *Context, uData unsafe.Pointer)
type DebugNotifyFunc = func(ctx *Context) int
var DukDebuggerMaxAttachments = 64 // arbitrary number of max 64 debugger attachments :)
type attachment struct {
readFunc DebugReadFunc
writeFunc DebugWriteFunc
peekFunc DebugPeekFunc
readFlushFunc DebugReadFlushFunc
writeFlushFunc DebugWriteFlushFunc
requestFunc DebugRequestFunc
detachedFunc DebugDetachedFunc
uData unsafe.Pointer
}
type Debugger struct {
m sync.Mutex
attachments []*attachment
}
var creationMutex = sync.Mutex{}
var debugger *Debugger
// Returns the Duktape debugger instance which can be attached to
// multiple Duktape contexts using the Attach method
func DukDebugger() *Debugger {
if debugger != nil {
return debugger
}
creationMutex.Lock()
defer creationMutex.Unlock()
if debugger != nil {
return debugger
}
debugger = &Debugger{
m: sync.Mutex{},
attachments: make([]*attachment, DukDebuggerMaxAttachments),
}
return debugger
}
func (d *Debugger) newAttachment(readFunc DebugReadFunc,
writeFunc DebugWriteFunc,
peekFunc DebugPeekFunc,
readFlushFunc DebugReadFlushFunc,
writeFlushFunc DebugWriteFlushFunc,
requestFunc DebugRequestFunc,
detachedFunc DebugDetachedFunc,
uData interface{}) (int, error) {
d.m.Lock()
defer d.m.Unlock()
for i := 0; i < len(d.attachments); i++ {
if d.attachments[i] == nil {
d.attachments[i] = &attachment{
readFunc: readFunc,
writeFunc: writeFunc,
peekFunc: peekFunc,
readFlushFunc: readFlushFunc,
writeFlushFunc: writeFlushFunc,
requestFunc: requestFunc,
detachedFunc: detachedFunc,
uData: unsafe.Pointer(&uData),
}
return i, nil
}
}
return -1, errors.New("no more attachment slots available")
}
func (d *Debugger) removeAttachment(slot int) (*attachment, error) {
d.m.Lock()
defer d.m.Unlock()
if slot < 0 || slot >= len(d.attachments) {
return nil, errors.New("illegal attachment requested")
}
attachment := d.attachments[slot]
if attachment == nil {
return nil, errors.New("no attachment registered for requested attachment slot")
}
d.attachments[slot] = nil
return attachment, nil
}
func (d *Debugger) getAttachment(slot int) (*attachment, error) {
d.m.Lock()
defer d.m.Unlock()
if slot < 0 || slot >= len(d.attachments) {
return nil, errors.New(fmt.Sprintf("illegal attachment requested: %d", slot))
}
attachment := d.attachments[slot]
if attachment == nil {
return nil, errors.New("no attachment registered for requested attachment slot")
}
return attachment, nil
}
// See: http://duktape.org/api.html#duk_debugger_attach
//
// All parameters are optional, except for readFunc, writeFunc.
func (d *Debugger) Attach(ctx *Context,
readFunc DebugReadFunc,
writeFunc DebugWriteFunc,
peekFunc DebugPeekFunc,
readFlushFunc DebugReadFlushFunc,
writeFlushFunc DebugWriteFlushFunc,
requestFunc DebugRequestFunc,
detachedFunc DebugDetachedFunc,
uData interface{}) error {
if readFunc == nil {
return errors.New("readFunc cannot be nil")
}
if writeFunc == nil {
return errors.New("writeFunc cannot be nil")
}
slot, err := d.newAttachment(
readFunc,
writeFunc,
peekFunc,
readFlushFunc,
writeFlushFunc,
requestFunc,
detachedFunc,
uData,
)
if err != nil {
return err
}
var peek, readFlush, writeFlush, request C.bool = peekFunc != nil,
readFlushFunc != nil, writeFlushFunc != nil, requestFunc != nil
dData := slotToPtr(slot)
C._duk_debugger_attach(
ctx.duk_context,
peek,
readFlush,
writeFlush,
request,
dData,
)
return nil
}
// See: http://duktape.org/api.html#duk_debugger_detach
func (d *Debugger) Detach(ctx *Context) {
C.duk_debugger_detach(ctx.duk_context)
}
// See: http://duktape.org/api.html#duk_debugger_cooperate
func (d *Debugger) Cooperate(ctx *Context) {
C.duk_debugger_cooperate(ctx.duk_context)
}
// See: http://duktape.org/api.html#duk_debugger_pause
func (d *Debugger) Pause(ctx *Context) {
C.duk_debugger_pause(ctx.duk_context)
}
// See: http://duktape.org/api.html#duk_debugger_notify
func (d *Debugger) Notify(ctx *Context, notifyFunc DebugNotifyFunc) int {
nvalues := notifyFunc(ctx)
return (int)(C.duk_debugger_notify(ctx.duk_context, (C.duk_idx_t)(nvalues)))
}
//export goDebugReadFunction
func goDebugReadFunction(dData unsafe.Pointer, buffer *C.char, length C.duk_size_t) C.duk_size_t {
a := ptrToAttachment(dData)
b := ptrToSlice(buffer, length)
return (C.duk_size_t)(a.readFunc(a.uData, b))
}
//export goDebugWriteFunction
func goDebugWriteFunction(dData unsafe.Pointer, buffer *C.char, length C.duk_size_t) C.duk_size_t {
a := ptrToAttachment(dData)
b := ptrToSlice(buffer, length)
return (C.duk_size_t)(a.writeFunc(a.uData, b))
}
//export goDebugPeekFunction
func goDebugPeekFunction(dData unsafe.Pointer) C.duk_size_t {
a := ptrToAttachment(dData)
return (C.duk_size_t)(a.peekFunc(a.uData))
}
//export goDebugReadFlushFunction
func goDebugReadFlushFunction(dData unsafe.Pointer) {
a := ptrToAttachment(dData)
a.readFlushFunc(a.uData)
}
//export goDebugWriteFlushFunction
func goDebugWriteFlushFunction(dData unsafe.Pointer) {
a := ptrToAttachment(dData)
a.writeFlushFunc(a.uData)
}
//export goDebugRequestFunction
func goDebugRequestFunction(ctx *C.duk_context, dData unsafe.Pointer, nvalues C.duk_idx_t) C.duk_idx_t {
a := ptrToAttachment(dData)
d := contextFromPointer(ctx)
d.transmute(unsafe.Pointer(ctx))
return (C.duk_idx_t)(a.requestFunc(d, a.uData, int(nvalues)))
}
//export goDebugDetachedFunction
func goDebugDetachedFunction(ctx *C.duk_context, dData unsafe.Pointer) {
s := ptrToSlot(dData)
debugger = DukDebugger()
a, err := debugger.removeAttachment(s)
if err != nil {
panic(err)
}
defer C.free(dData)
d := contextFromPointer(ctx)
d.transmute(unsafe.Pointer(ctx))
if a.detachedFunc != nil {
a.detachedFunc(d, a.uData)
}
}
func ptrToSlice(buffer *C.char, length C.duk_size_t) []byte {
ptr := uintptr(unsafe.Pointer(buffer))
l := int(length)
header := reflect.SliceHeader{Data: ptr, Len: l, Cap: l}
return *(*[]byte)(unsafe.Pointer(&header))
}
func ptrToSlot(dData unsafe.Pointer) int {
return int(*(*int32)(dData))
}
func slotToPtr(slot int) unsafe.Pointer {
s := uint8(slot)
dData := C.malloc(C.size_t(unsafe.Sizeof(s)))
*(*C.uint8_t)(dData) = C.uint8_t(s)
return dData
}
func ptrToAttachment(dData unsafe.Pointer) *attachment {
slot := ptrToSlot(dData)
debugger := DukDebugger()
attachment, err := debugger.getAttachment(slot)
if err != nil {
panic(err)
}
return attachment
}