forked from wryun/es-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esdebug
executable file
·357 lines (322 loc) · 5.94 KB
/
esdebug
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#! /bin/es -p
# esdebug: a debugger for es scripts ($Revision: 1.1.1.1 $)
# TODO
# catch signals (at least sigint) better
# clean up the output
# write a man page (ha!)
fn usage {
throw error esdebug 'usage: esdebug [-x] program arg ...'
}
trapall = false
while {~ $1 -*} {
if {~ $1 -x} {
trapall = true
* = $*(2 ...)
} {
usage
}
}
if {~ $#* 0} {
usage
}
_program = $1
if {!~ $_program /* ./* ../*} {
_program = <={ access -n $_program -1e -xf $path }
}
* = $*(2 ...)
exec {<>[3] /dev/tty}
fn _print {
echo >[1=3] '>>>' $*
}
fn _get {
$&seq {echo >[1=3] -n $*} {return `{line <[0=3]}}
}
_prompt = '(esdebug) '
usage =
#
# debugger functions
#
# These are written in a funny, stylized way to avoid calls to the functions
# %seq, if, and while, because those are trapped by the debugging code.
_debug-! = eval
_debug-continue = $&seq {_stepping = false} {throw break}
_debug-step = $&seq {_stepping = true} {throw break}
_debug-quit = { exit 0 }
_debug-echo = eval echo
_debug-retry = throw retry
_debug-return = throw return
_debug-throw = throw break throw
_debug-trace = @ functions {
for (func = $functions) {
let (old = $(fn-$func)) {
$&if {!~ $#(_untraced-$func) 0} {
_print $func: already traced
} {~ $#old 0} {
_print $func: undefined
} {
$&seq {
_untraced-$func = $old
} {
noexport = $noexport _untraced-$func
} {
fn $func args {
$&seq {
_print $func $args
} {
catch @ e {
_print $func raised exception: $e
} {
let (result = <={$old $args}) {
$&seq {
_print $func '->' $result
} {
result $result
}
}
}
}
}
}
}
}
}
}
_debug-untrace = @ functions {
for (func = $functions) {
let (old = $(_untraced-$func)) {
$&if {~ $#old 0} {
_print $func: not traced
} {
fn-$func = $old
}
}
}
}
_debug-break = @ functions {
for (func = $functions) {
let (old = $(fn-$func)) {
$&if {!~ $#(_unbroken-$func) 0} {
_print $func: already broken
} {~ $#old 0} {
_print $func: undefined
} {
$&seq {
_unbroken-$func = $old
} {
noexport = $noexport _unbroken-$func
} {
fn $func args {
$&seq {
_print break in $func $args
} {
local (* = $args)
_debug break
} {
catch @ e {
$&seq {
_print $func raised exception: $e
} {
_debug break
}
} {
let (result = <={$old $args}) {
$&seq {
_print $func '->' $result
} {
_debug break
} {
result $result
}
}
}
}
}
}
}
}
}
}
_debug-unbreak = @ functions {
for (func = $functions) {
let (old = $(_unbroken-$func)) {
$&if {~ $#old 0} {
_print $func: not broken
} {
fn-$func = $old
}
}
}
}
_debug-watch = @ vars {
for (var = $vars) {
set-$var = @ {
$&seq {
_print old $var '=' $$var
} {
_print new $var '=' $*
} {
return $*
}
}
}
}
_debug-unwatch = @ vars {
for (var = $vars) {
set-$var =
}
}
_debug-trap = @ vars {
for (var = $vars) {
set-$var = @ {
$&seq {
_print old $var '=' $$var
} {
_print new $var '=' $*
} {
local (old = $$var; new = $*)
_debug break
} {
return $*
}
}
}
}
_debug-untrap = @ vars {
for (var = $vars) {
set-$var =
}
}
_debug-help = @ {
cat << 'END-OF-HELP'
command description (abbreviations)
break f set a breakpoint on entry to and exit from function f (b)
continue continue execution (c, cont, r, run)
echo expr ... echo the es expression (print, p)
quit exit esdebug (q)
throw e ... throw exception e
trace f trace calls to function f
trap v break on assignments to variable v
retry retry last statement, after catching exceptions only
return expr ... return expression from current function
unbreak f cancel breakpoint on f
untrace f cancel tracing of f
untrap v cancel trap of v
unwatch v cancel watch of v
watch v trace assignments to variable v
! cmd run es command cmd
an empty command is equivalent to step
END-OF-HELP
}
_debug-b = $_debug-break
_debug-c = $_debug-continue
_debug-cont = $_debug-continue
_debug-p = $_debug-echo
_debug-print = $_debug-echo
_debug-q = $_debug-quit
_debug-r = $_debug-continue
_debug-run = $_debug-continue
_debug-s = $_debug-step
_debug-? = $_debug-help
let (commands =) {
for (i = <=$&vars) {
if {~ $i _debug-*} {
commands = $commands $i
}
}
noexport = $noexport $commands
}
#
# the main debugger loop
#
fn-_debug = $&noreturn @ {
$&if {~ $1 break || $_stepping} {
<= {
$&while {
let (cmd = <={_get $_prompt}) {
$&seq {
$&while {~ $cmd(1) $_prompt} {
cmd = $cmd(2 ...)
}
} {
$&if {~ $#cmd 0} {
cmd = step
}
} {
$&if {~ $#(_debug-$cmd(1)) 0} {
_print unknown command: $cmd(1)
} {
$(_debug-$cmd(1)) $cmd(2 ...)
}
}
}
}
}
}
}
#
# the (debugging) interactive loop
#
history =
fn %interactive-loop dispatch {
let (result = <= true) {
catch @ e type msg {
$&if {~ $e eof} {
$&seq {
_print exiting with exit status $result
} {
_debug break
} {
return $result
}
} {
$&seq {
_print exception: $e $type $msg
} {
_debug break
} {
throw retry
}
}
} {
forever {
let (cmd = ) {
$&seq {
_print parsing input from $_program:
} {
$&while {~ $#cmd 0} {
cmd = <={%parse}
}
} {
_debug
} {
catch @ e {
$&seq {
_print exception: $e
} {
_debug
}
} {
result = <={$dispatch $cmd}
}
}
}
}
}
}
}
noexport = (
$noexport
fn-^(_print _get _debug)
_prompt _program _stepping
%interactive-loop
)
_stepping = true
if ($trapall) {
fn-%seq = $&noreturn @ {
for (i = $*) {
$&seq {_print %seq $i} {_debug} $i
}
}
noexport = $noexport %seq if while
}
. -i -v $_program $*