/
ui.jl
562 lines (511 loc) · 19.4 KB
/
ui.jl
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
const rebug_prompt_string = "rebug> "
const rebug_prompt_ref = Ref{Union{LineEdit.Prompt,Nothing}}(nothing) # set by __init__
const interpret_prompt_ref = Ref{Union{LineEdit.Prompt,Nothing}}(nothing) # set by __init__
# For debugging
function logaction(msg)
open("/tmp/rebugger.log", "a") do io
println(io, "* ", msg)
flush(io)
end
nothing
end
dummy() = nothing
const dummymethod = first(methods(dummy))
const dummyuuid = UUID(UInt128(0))
uuidextractor(str) = match(r"getstored\(\"([a-z0-9\-]+)\"\)", str)
mutable struct RebugHeader <: AbstractHeader
warnmsg::String
errmsg::String
uuid::UUID
current_method::Method
nlines::Int # size of the printed header
end
RebugHeader() = RebugHeader("", "", dummyuuid, dummymethod, 0)
mutable struct InterpretHeader <: AbstractHeader
frame::Union{Nothing,Frame}
leveloffset::Int
val
bt
warnmsg::String
errmsg::String
nlines::Int # size of the printed header
end
InterpretHeader() = InterpretHeader(nothing, 0, nothing, nothing, "", "", 0)
struct DummyAST end # fictive input for the put!/take! evaluation by the InterpretREPL backend
header(s::LineEdit.MIState) = header(mode(s).repl)
header(repl::HeaderREPL) = repl.header
header(::LineEditREPL) = header(rebug_prompt_ref[].repl) # default to the Rebug header
# Custom methods
set_method!(header::RebugHeader, method::Method) = header.current_method = method
function set_uuid!(header::RebugHeader, uuid::UUID)
if haskey(stored, uuid)
header.uuid = uuid
header.current_method = stored[uuid].method
else
header.uuid = dummyuuid
header.current_method = dummymethod
end
uuid
end
function set_uuid!(header::RebugHeader, str::AbstractString)
m = uuidextractor(str)
uuid = if m isa RegexMatch && length(m.captures) == 1
UUID(m.captures[1])
else
dummyuuid
end
set_uuid!(header, uuid)
end
struct FakePrompt{Buf<:IO}
input_buffer::Buf
end
LineEdit.mode(p::FakePrompt) = rebug_prompt_ref[]
"""
stepin(s)
Given a buffer `s` representing a string and "point" (the seek position) set at a call expression,
replace the contents of the buffer with a `let` expression that wraps the *body* of the callee.
For example, if `s` has contents
<some code>
if x > 0.5
^fcomplex(x)
<more code>
where in the above `^` indicates `position(s)` ("point"), and if the definition of `fcomplex` is
function fcomplex(x::A, y=1, z=""; kw1=3.2) where A<:AbstractArray{T} where T
<body>
end
rewrite `s` so that its contents are
@eval ModuleOf_fcomplex let (x, y, z, kw1, A, T) = Main.Rebugger.getstored(id)
<body>
end
where `Rebugger.getstored` returns has been pre-loaded with the values that would have been
set when you called `fcomplex(x)` in `s` above.
This line can be edited and `eval`ed at the REPL to analyze or improve `fcomplex`,
or can be used for further `stepin` calls.
"""
function stepin(s::LineEdit.MIState)
# Add the command we're tracing to the history. That way we can go "up the call stack".
pos = position(s)
cmd = String(take!(copy(LineEdit.buffer(s))))
add_history(s, cmd)
# Analyze the command string and step in
local uuid, letcmd
try
uuid, letcmd = stepin(LineEdit.buffer(s))
catch err
repl = rebug_prompt_ref[].repl
handled = false
if err isa StashingFailed
repl.header.warnmsg = "Execution did not reach point"
handled = true
elseif err isa Meta.ParseError || err isa StepException
repl.header.warnmsg = "Expression at point is not a call expression"
handled = true
elseif err isa EvalException
io = IOBuffer()
showerror(io, err.exception)
errstr = String(take!(io))
repl.header.errmsg = "$errstr while evaluating $(err.exprstring)"
handled = true
elseif err isa DefMissing
repl.header.errmsg = "The expression for method $(err.method) was unavailable. Perhaps it was untracked or generated by code."
handled = true
end
if handled
buf = LineEdit.buffer(s)
LineEdit.edit_clear(buf)
write(buf, cmd)
seek(buf, pos)
return nothing
end
rethrow(err)
end
set_uuid!(header(s), uuid)
LineEdit.edit_clear(s)
LineEdit.edit_insert(s, letcmd)
return nothing
end
function capture_stacktrace(s)
if mode(s) isa LineEdit.PrefixHistoryPrompt
# history search, re-enter with the corresponding mode
LineEdit.accept_result(s, mode(s))
return capture_stacktrace(s)
end
cmdstring = LineEdit.content(s)
add_history(s, cmdstring)
print(REPL.terminal(s), '\n')
expr = Meta.parse(cmdstring)
local uuids
try
uuids = capture_stacktrace(expr)
catch err
print(stderr, err)
return nothing
end
io = IOBuffer()
buf = FakePrompt(io)
hp = mode(s).hist
for uuid in uuids
println(io, generate_let_command(uuid))
REPL.add_history(hp, buf)
take!(io)
end
hp.cur_idx = length(hp.history) + 1
if !isempty(uuids)
set_uuid!(header(s), uuids[end])
print(REPL.terminal(s), '\n')
LineEdit.edit_clear(s)
LineEdit.enter_prefix_search(s, find_prompt(s, LineEdit.PrefixHistoryPrompt), true)
end
return nothing
end
function add_history(s, str::AbstractString)
io = IOBuffer()
buf = FakePrompt(io)
hp = mode(s).hist
println(io, str)
REPL.add_history(hp, buf)
end
function interpret(s)
hdr = header(s)
hdr.bt = nothing
term = REPL.terminal(s)
cmdstring = LineEdit.content(s)
isempty(cmdstring) && return :done
add_history(s, cmdstring)
assigns, expr, display_result = simplecall(cmdstring)
tupleexpr = JuliaInterpreter.extract_args(Main, expr)
callargs = Core.eval(Main, tupleexpr) # get the elements of the call
callexpr = Expr(:call, callargs...)
frame = JuliaInterpreter.enter_call_expr(callexpr)
if frame === nothing
local val
try
hdr.val = Core.eval(Main, callexpr)
catch err
hdr.val = err
hdr.bt = catch_backtrace()
end
else
# frame = JuliaInterpreter.maybe_step_through_wrapper!(frame)
hdr.frame = frame
deflines = expression_lines(frame)
print(term, '\n') # to advance beyond the user's input line
nlines = 0
try
while true
HeaderREPLs.clear_nlines(term, nlines)
print_header(term, hdr)
if hdr.leveloffset == 0
nlines = show_code(term, frame, deflines, nlines)
else
f, Δ = frameoffset(frame, hdr.leveloffset)
hdr.leveloffset -= Δ
nlines = show_code(term, f, expression_lines(f), nlines)
end
cmd = read(term, Char)
if cmd == '?'
hdr.warnmsg = """
Commands:
space: next line
enter: continue to next breakpoint or completion
→: step in to next call
←: finish frame and return to caller
↑: display the caller frame
↓: display the callee frame
b: insert breakpoint at current line
c: insert conditional breakpoint at current line
r: remove breakpoint at current line
d: disable breakpoint at current line
e: enable breakpoint at current line
o: open current position in editor
q: abort (returns nothing)"""
elseif cmd == ' '
hdr.leveloffset = 0
ret = debug_command(frame, :n)
if ret === nothing
hdr.val = JuliaInterpreter.get_return(root(frame))
break
end
frame, deflines = refresh(frame, ret, deflines)
elseif cmd == '\n' || cmd == '\r'
hdr.leveloffset = 0
ret = debug_command(frame, :c)
if ret === nothing
hdr.val = JuliaInterpreter.get_return(root(frame))
break
end
frame, deflines = refresh(frame, ret, deflines)
elseif cmd == 'q'
hdr.val = nothing
break
elseif cmd == 'o'
f, Δ = frameoffset(frame, hdr.leveloffset)
file, line = whereis(f)
edit(file, line)
elseif cmd == '\e' # escape codes
nxtcmd = read(term, Char)
nxtcmd == "O" && (nxtcmd = '[') # normalize escape code
cmd *= nxtcmd
while nxtcmd == '['
nxtcmd = read(term, Char)
cmd *= nxtcmd
end
if cmd == "\e[C" # right arrow
hdr.leveloffset = 0
ret = debug_command(frame, :s)
if ret === nothing
# Trying to "step in" at program exit
hdr.val = JuliaInterpreter.get_return(frame)
break
end
frame, deflines = refresh(frame, ret, deflines)
elseif cmd == "\e[D" # left arrow
hdr.leveloffset = 0
ret = debug_command(frame, :finish)
if ret === nothing
hdr.val = JuliaInterpreter.get_return(root(frame))
break
end
frame, deflines = refresh(frame, ret, deflines)
elseif cmd == "\e[A" # up arrow
hdr.leveloffset += 1
elseif cmd == "\e[B" # down arrow
hdr.leveloffset = max(0, hdr.leveloffset - 1)
end
elseif cmd == 'b' || cmd == 'c'
if cmd == 'b'
cond = nothing
else
print(term, "enter condition: ")
condstr = ""
c = read(term, Char)
while c != '\n' && c != '\r'
print(term, c)
condstr *= c
c = read(term, Char)
end
condex = Base.parse_input_line(condstr; filename="condition")
cond = (JuliaInterpreter.moduleof(frame), condex)
end
ret = JuliaInterpreter.whereis(frame)
if ret === nothing
@warn "Failed to find location info at current statement"
else
file, line = ret
JuliaInterpreter.breakpoint(file, line, cond)
end
elseif cmd == 'r'
breakpoint_action(remove, frame)
elseif cmd == 'd'
breakpoint_action(disable, frame)
elseif cmd == 'e'
breakpoint_action(enable, frame)
else
push!(msgs, cmd)
end
hdr.frame = frame
end
catch err
hdr.val = err
hdr.bt = catch_backtrace()
end
end
# Store the result
repl = mode(s).repl
if isdefined(repl, :backendref)
response = (display_result ? hdr.val : nothing, VERSION >= v"1.2.0-DEV.249" ? hdr.bt !== nothing : hdr.bt)
put!(repl.backendref.response_channel, response)
end
hdr.frame = nothing
# Do this last in case of errors
if assigns !== nothing && hdr.bt === nothing
Core.eval(Main, Expr(:(=), assigns, hdr.val))
end
return :done
end
function refresh(frame, ret, deflines)
@assert ret !== nothing
cframe, _ = ret
if cframe === frame
return frame, deflines
end
return cframe, expression_lines(cframe)
end
# Find the range of statement indexes that preceed a line
# `thisline` should be in "compiled" numbering
function coderange(src::CodeInfo, thisline)
codeloc = searchsortedfirst(src.linetable, LineNumberNode(thisline, ""); by=x->x.line)
if codeloc == 1
return 1:1
end
idxline = searchsortedfirst(src.codelocs, codeloc)
idxprev = searchsortedfirst(src.codelocs, codeloc-1) + 1
return idxprev:idxline
end
coderange(framecode::FrameCode, thisline) = coderange(framecode.src, thisline)
function breakpoint_action(f, frame)
ret = CodeTracking.whereis(frame)
if ret === nothing
@warn "Failed to find location info at current statement"
else
file, line = ret
for bp in JuliaInterpreter.breakpoints()
if bp isa JuliaInterpreter.BreakpointFileLocation && bp.path == file && bp.line == line
f(bp)
end
end
end
return nothing
end
function simplecall(cmdstring)
cmdstring = chomp(cmdstring)
display_result = !endswith(cmdstring, ';')
if !display_result
cmdstring = cmdstring[1:end-1]
end
expr = Meta.parse(cmdstring)
if expr.head == :(=)
assigns = expr.args[1]
expr = expr.args[2]
else
assigns = nothing
end
return assigns, expr, display_result
end
### REPL modes
function HeaderREPLs.setup_prompt(repl::HeaderREPL{RebugHeader}, hascolor::Bool)
julia_prompt = find_prompt(repl.interface, "julia")
prompt = REPL.LineEdit.Prompt(
rebug_prompt_string;
prompt_prefix = hascolor ? repl.prompt_color : "",
prompt_suffix = hascolor ?
(repl.envcolors ? Base.input_color : repl.input_color) : "",
complete = julia_prompt.complete,
on_enter = REPL.return_callback)
prompt.on_done = HeaderREPLs.respond(repl, julia_prompt) do str
Base.parse_input_line(str; filename="REBUG")
end
# hist will be handled automatically if repl.history_file is true
# keymap_dict is separate
return prompt, :rebug
end
function HeaderREPLs.append_keymaps!(keymaps, repl::HeaderREPL{RebugHeader})
julia_prompt = find_prompt(repl.interface, "julia")
kms = [
trigger_search_keymap(repl),
mode_termination_keymap(repl, julia_prompt),
trigger_prefix_keymap(repl),
REPL.LineEdit.history_keymap,
REPL.LineEdit.default_keymap,
REPL.LineEdit.escape_defaults,
]
append!(keymaps, kms)
end
# To get it to parse the UUID whenever we move through the history, we have to specialize
# this method
function HeaderREPLs.activate_header(header::RebugHeader, p, s, termbuf, term)
str = String(take!(copy(LineEdit.buffer(s))))
set_uuid!(header, str)
end
## Interpret also requires a separate repl
function HeaderREPLs.setup_prompt(repl::HeaderREPL{InterpretHeader}, hascolor::Bool)
julia_prompt = find_prompt(repl.interface, "julia")
prompt = REPL.LineEdit.Prompt(
"interpret> "; # should never be shown
prompt_prefix = hascolor ? repl.prompt_color : "",
prompt_suffix = hascolor ?
(repl.envcolors ? Base.input_color : repl.input_color) : "",
complete = julia_prompt.complete,
on_enter = REPL.return_callback)
prompt.on_done = HeaderREPLs.respond(repl, julia_prompt) do str
return DummyAST()
end
return prompt, :interpret
end
function HeaderREPLs.append_keymaps!(keymaps, repl::HeaderREPL{InterpretHeader})
# No keymaps
return keymaps
end
function HeaderREPLs.activate_header(header::InterpretHeader, p, s, termbuf, term)
end
function Base.put!(c::Channel, input::Tuple{DummyAST,Int})
return nothing
end
const keybindings = Dict{Symbol,String}(
:stacktrace => "\es", # Alt-s ("[s]tacktrace")
:stepin => "\ee", # Alt-e ("[e]nter")
:interpret => "\ei", # Alt-i ("[i]nterpret")
)
const modeswitches = Dict{Any,Any}(
:stacktrace => (s, o...) -> capture_stacktrace(s),
:stepin => (s, o...) -> (stepin(s); enter_rebug(s)),
:interpret => (s, o...) -> (enter_interpret(s); interpret(s)),
)
function get_rebugger_modeswitch_dict()
rebugger_modeswitch = Dict()
for (action, keybinding) in keybindings
rebugger_modeswitch[keybinding] = modeswitches[action]
end
rebugger_modeswitch
end
function add_keybindings(main_repl; override::Bool=false, kwargs...)
history_prompt = find_prompt(main_repl.interface, LineEdit.PrefixHistoryPrompt)
julia_prompt = find_prompt(main_repl.interface, "julia")
rebug_prompt = find_prompt(main_repl.interface, "rebug")
for (action, keybinding) in kwargs
if !(action in keys(keybindings))
error("$action is not a supported action.")
end
if !(keybinding isa Union{String,Vector{String}})
error("Expected the value for $action to be a String or Vector{String}, got $keybinding instead")
end
if haskey(keybindings, action)
keybindings[action] = keybinding
end
# PackageCompiler can cause these keys to be added twice (once during compilation and once by __init__),
# so put these in a try/catch (issue #62)
if action == :interpret
try
LineEdit.add_nested_key!(julia_prompt.keymap_dict, keybinding, modeswitches[action], override=override)
catch
end
else
# We need Any here because "cannot convert REPL.LineEdit.PrefixHistoryPrompt to an object of type REPL.LineEdit.Prompt"
prompts = Any[julia_prompt, rebug_prompt]
if action == :stacktrace push!(prompts, history_prompt) end
for prompt in prompts
if keybinding isa Vector
for kb in keybinding
try
LineEdit.add_nested_key!(prompt.keymap_dict, kb, modeswitches[action], override=override)
catch
end
end
else
try
LineEdit.add_nested_key!(prompt.keymap_dict, keybinding, modeswitches[action], override=override)
catch
end
end
end
end
end
end
# These work only at the `rebug>` prompt
const rebugger_keys = Dict{Any,Any}(
)
## REPL commands TODO?:
## "\em" (meta-m): create REPL line that populates Main with arguments to current method
## "\eS" (meta-S): save version at REPL to file? (a little dangerous, perhaps make it configurable as to whether this is on)
## F1 is "^[OP" (showvalues?), F4 is "^[OS" (showinputs?)
enter_rebug(s) = mode_switch(s, rebug_prompt_ref[])
enter_interpret(s) = mode_switch(s, interpret_prompt_ref[])
function mode_switch(s, other_prompt)
buf = copy(LineEdit.buffer(s))
LineEdit.edit_clear(s)
LineEdit.transition(s, other_prompt) do
LineEdit.state(s, other_prompt).input_buffer = buf
end
end
# julia_prompt = find_prompt(main_repl.interface, "julia")
# julia_prompt.keymap_dict['|'] = (s, o...) -> enter_count(s)