/
Infiltrator.jl
941 lines (819 loc) · 27.6 KB
/
Infiltrator.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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
module Infiltrator
let README = normpath(dirname(@__DIR__), "README.md")
include_dependency(README)
@doc read(README, String) Infiltrator
end
using REPL, UUIDs, InteractiveUtils
using REPL.LineEdit: getproperty
using REPL.LineEdit
using Markdown
export @infiltrate, @infiltry, @exfiltrate, @withstore, safehouse, exfiltrated, infiltrate
const REPL_HOOKED = Ref{Bool}(false)
const INFILTRATION_LOCK = Ref{ReentrantLock}()
function __init__()
clear_store!(store)
INFILTRATION_LOCK[] = ReentrantLock()
if VERSION >= v"1.5.0-DEV.282"
if isdefined(Base, :active_repl_backend)
pushfirst!(Base.active_repl_backend.ast_transforms, ast_transformer())
REPL_HOOKED[] = true
else
atreplinit() do repl
@async begin
iter = 0
# wait for active_repl_backend to exist
while !isdefined(Base, :active_repl_backend) && iter < 20
sleep(0.05)
iter += 1
end
if isdefined(Base, :active_repl_backend)
pushfirst!(Base.active_repl_backend.ast_transforms, ast_transformer())
REPL_HOOKED[] = true
end
end
end
end
end
end
"""
@infiltrate
@infiltrate condition::Bool
`@infiltrate` sets an infiltration point (or breakpoint).
When the infiltration point is hit, it will drop you into an interactive REPL session that
lets you inspect local variables and the call stack as well as execute arbitrary statements
in the context of the current functions module.
This macro also accepts an optional argument `cond` that must evaluate to a boolean,
and then this macro will serve as a "conditional breakpoint", which starts inspections only
when its condition is `true`. For example:
@infiltrate false # does not infiltrate
"""
macro infiltrate(cond = true)
if __module__ === Core.Compiler && !isdefined(Core.Compiler, :Dict)
# XXX Dict isn't available in Core.Compiler, so make it available now
Core.eval(Core.Compiler, :(using .Main: Dict))
Core.eval(Core.Compiler, :(setindex!(x::Dict, args...) = Main.setindex!(x, args...)))
end
quote
if $(esc(cond))
$(start_prompt)($(__module__), Base.@locals, $(String(__source__.file)), $(__source__.line))
end
end
end
"""
infiltrate(mod, locals, file, line)
Function form of `@infiltrate`. Use this to conditionally infiltrate package code without
using e.g. Revise (because this version is valid during precompilation).
This would typically be used as
```julia
if isdefined(Main, :Infiltrator)
Main.infiltrate(@__MODULE__, Base.@locals, @__FILE__, @__LINE__)
end
```
"""
function infiltrate(mod, locals, file, line)
start_prompt(mod, locals, string(file), line)
end
"""
@exfiltrate
Assigns all local variables into the global storage.
"""
macro exfiltrate()
quote
for (k, v) in Base.@locals
try
Core.eval(getfield(getfield($(@__MODULE__), :store), :store), Expr(:(=), k, QuoteNode(v)))
catch err
println(stderr, "Assignment to store variable failed.")
Base.display_error(stderr, err, catch_backtrace())
end
end
end
end
"""
@infiltry expr
Wraps expression in a try block, infiltrate if an exception is raised.
Equivalent to:
```julia
try
expr
catch
@infiltrate
rethrow()
end
```
If `expr` is of the form `x = f(...)`, we take `x` out of the scope
```julia
x = try
f(...)
catch
@infiltrate
rethrow()
end
```
"""
macro infiltry(expr)
infiltrator_catch_and_rethrow = quote
$(Infiltrator.start_prompt)($(__module__), Base.@locals, $(String(__source__.file)), $(__source__.line), ex, catch_backtrace())
rethrow()
end
return if Meta.isexpr(expr, :(=))
var = first(expr.args)
func = last(expr.args)
quote
$(esc(var)) = try
$(esc(func))
catch ex
$(infiltrator_catch_and_rethrow)
end
end
else
quote
try
$(esc(expr))
catch ex
$(infiltrator_catch_and_rethrow)
end
end
end
end
const prompt_color = if Sys.iswindows()
"\e[33m"
else
"\e[38;5;166m"
end
# these are used for easier testing
const TEST_TERMINAL_REF = Ref{Any}(nothing)
const TEST_REPL_REF = Ref{Any}(nothing)
const TEST_NOSTACK = Ref{Any}(false)
"""
toggle_async_check(enabled)
Enable or disable the check for safe REPL mode switching. May result in a non-functional REPL.
"""
toggle_async_check(enabled) = CHECK_TASK[] = enabled
const CHECK_TASK = Ref{Bool}(true)
mutable struct Session
store::Module
exiting::Bool
disabled::Set
conditions::Dict
end
function Base.show(io::IO, s::Session)
n = length(get_store_names(s))
print(io, "Safehouse ($(n) variable$(n == 1 ? "" : "s"))")
end
function Base.show(io::IO, ::MIME"text/plain", s::Session)
names = get_store_names(s)
n = length(names)
println(io, "Safehouse with $(n) stored variable$(n == 1 ? "" : "s"):")
for (i, (var, val)) in enumerate(names)
if i > 6
println(io, "...")
break
end
one_line_show(io, var, val)
end
end
function Base.getproperty(sp::Session, s::Symbol)
m = getfield(sp, :store)
if isdefined(m, s)
getproperty(m, s)
else
throw(UndefVarError(s))
end
end
Base.propertynames(s::Session) = keys(get_store_names(s))
const store_docstring = """
safehouse
exfiltrated
Infiltrator.store
Global storage for storing values while `@infiltrate`ing or `@exfiltrate`ing.
Also see [`clear_store!`](@ref), [`set_store!`](@ref), and [`@withstore`](@ref) for
safehouse-related functionality.
"""
@doc store_docstring
const store = Session(Module(), false, Set(), Dict())
@doc store_docstring
const safehouse = store
@doc store_docstring
const exfiltrated = store
"""
@withstore ex
Evaluates the expression `ex` in the context of the global store.
Mainly intended for interactive use, as changes to the store's state will not
propagate into the returned expression.
"""
macro withstore(ex)
withstore(ex)
end
function withstore(ex)
ns = get_store_names(store)
return Expr(:let,
Expr(:block, map(x->Expr(:(=), x...), [(k, maybe_quote(v)) for (k, v) in ns])...),
Expr(:block, ex)
)
end
"""
clear_disabled!(s = safehouse)
Clear all disabled infiltration points.
"""
function clear_disabled!(s = store)
empty!(getfield(s, :disabled))
return nothing
end
"""
clear_conditions!(s = safehouse)
Clear all conditional infiltration points.
"""
function clear_conditions!(s = store)
empty!(getfield(s, :conditions))
return nothing
end
"""
end_session!(s = safehouse)
End this infiltration session (reverts the effect of `@exit` in the `debug>` REPL).
Only needs to be manually called on Julia versions prior to 1.5.
"""
function end_session!(s::Session = store)
setfield!(s, :exiting, false)
return nothing
end
"""
clear_store!(s = safehouse)
Reset the store used for global symbols.
"""
clear_store!(s::Session = store) = set_store!(s, Module())
"""
set_store!(s = safehouse, m::Module)
Set the module backing the store `s`.
"""
set_store!(m::Module) = set_store!(store, m)
function set_store!(s::Session, m::Module)
setfield!(s, :store, m)
nothing
end
function start_prompt(mod, locals, file, fileline, ex = nothing, bt = nothing;
terminal = TEST_TERMINAL_REF[],
repl = TEST_REPL_REF[],
nostack = TEST_NOSTACK[]
)
lock(INFILTRATION_LOCK[])
try
getfield(store, :exiting) && return
spot = (file, fileline)
spot in getfield(store, :disabled) && return
cs = getfield(store, :conditions)
f = get(cs, spot, nothing)
# Use invokelatest because we may have added f before returning to the REPL.
isnothing(f) || Base.invokelatest(f, locals) || return
if terminal === nothing || repl === nothing
active_repl_backend = nothing
if isdefined(Base, :active_repl) && isdefined(Base.active_repl, :t) && isdefined(Base, :active_repl_backend)
repl = Base.active_repl
terminal = Base.active_repl.t
active_repl_backend = Base.active_repl_backend
else
println("Infiltrator.jl needs a fully-functional Julia REPL.")
return
end
end
io = Base.pipe_writer(terminal)
trace = stacktrace()
start = something(findlast(x -> x.func === Symbol("start_prompt"), trace), 0) + 2
last = something(findfirst(x -> x.func === StackTraces.top_level_scope_sym, trace),
length(trace))
trace = trace[start:last]
bt = bt === nothing ? nothing : crop_backtrace(bt)
if CHECK_TASK[] && !active_repl_backend.in_eval
b = IOBuffer()
c = IOContext(b, :color=>get(io, :color, false))
printstyled(c, "ERROR: "; color=Base.error_color())
if length(trace) > 0
sf = trace[1]
print(c, "Disabling the infiltration point in ")
printstyled(c, sprint(Base.StackTraces.show_spec_linfo, sf); color=:cyan)
print(c, " at ")
if sf.file !== Symbol("")
print(c, basename(string(sf.file)), ":")
if sf.line >= 0
print(c, sf.line)
else
print(c, "?")
end
else
print(c, "unknown")
end
println(c, ".")
else
println(c, "Disabling this infiltration point.")
end
printstyled(c, """
Infiltrating fully asynchronous code is currently not possible. You may be trying
to infiltrate an """; color=:light_black)
printstyled(c, "@async "; color=:cyan)
printstyled(c, """
task or are using inline evaluation in VS Code or Juno.
Infiltration points can be re-enabled with """; color=:light_black)
printstyled(c, "Infiltrator.clear_disabled!() "; color=:cyan)
printstyled(c, """
and
this check is toggled with """, color=:light_black)
printstyled(c, "Infiltrator.toggle_async_check(false)", color=:cyan)
printstyled(c, ".\n", color=:light_black)
print(io, String(take!(b)))
push!(getfield(store, :disabled), ((file, fileline)))
return
end
if ex !== nothing
printstyled(io, "EXCEPTION"; color=:red, bold=true)
print(io, " while infiltrating ")
else
print(io, "Infiltrating ")
end
if Threads.nthreads() > 1
print(io, "(on thread $(Threads.threadid())) ")
end
if length(trace) > 0
if nostack
print(io, "<unknown>")
else
print_verbose_stackframe(io, trace[1])
end
else
print(io, "top-level frame")
end
println(io)
println(io)
debugprompt(mod, locals, trace, terminal, repl, ex, bt; nostack=nostack, file=file, fileline=fileline)
println(io)
finally
unlock(INFILTRATION_LOCK[])
end
end
const HELP_TEXT = md"""
Code entered here is evaluated in the current function's scope. Changes to local variables are not
possible; global variables can only be changed with `eval`/`@eval`.
All assignments will end up in the `safehouse`.
The following commands are special cased:
- `?`: Print this help text.
- `@trace`: Print the current stack trace.
- `@locals`: Print local variables. `@locals x y` only prints `x` and `y`.
- `@exception`: Print the exception that triggered the current `@infiltry` session, if any.
- `@exfiltrate`: Save all local variables into the store. `@exfiltrate x y` saves `x` and `y`;
this variant can also exfiltrate variables defined in the `infil>` REPL.
- `@toggle`: Toggle infiltrating at this `@infiltrate` spot (clear all with `Infiltrator.clear_disabled!()`).
- `@cond expr`: Infiltrate at this `@infiltrate` spot only if `expr` evaluates to true (clear all with `Infiltrator.clear_conditions!()`). Only local variables can be accessed here.
- `@continue`: Continue to the next infiltration point or exit (shortcut: Ctrl-D).
- `@doc symbol`: Get help for `symbol` (same as in the normal Julia REPL).
- `@exit`: Stop infiltrating for the remainder of this session and exit (on Julia versions prior to
1.5 this needs to be manually cleared with `Infiltrator.end_session!()`).
"""
function show_help(io)
show(io, MIME("text/plain"), HELP_TEXT)
println(io, '\n')
end
function strlimit(str, limit = 30)
strwidth = 0
for (i, c) in enumerate(str)
strwidth += textwidth(c)
strwidth >= limit && return string(first(str, i - 1), "…")
end
return str
end
function show_locals(io, locals, selected::AbstractSet = Set())
if isempty(locals)
println(io, "No local variables in this scope.\n")
return
end
for (var, val) in locals
if isempty(selected) || var in selected
one_line_show(io, var, val)
end
end
println(io)
end
get_pmacro_args(mname, line) =
Set(Symbol.(filter!(x -> x != mname, strip.(filter!(!isempty, split(line, ' '))))))
function show_locals(io, locals, sline::AbstractString)
selected = get_pmacro_args("@locals", sline)
show_locals(io, locals, selected)
end
function exfiltrate_locals(io, evalmod, locals, sline::AbstractString)
selected = get_pmacro_args("@exfiltrate", sline)
locals_only = isempty(selected)
iter = locals_only ? keys(locals) : selected
n = length(iter)
println(io, "Exfiltrating $(n) local variable$(n == 1 ? "" : "s") into the safehouse.\n")
for k in iter
if locals_only
v = locals[k]
else
string(k) in ("@__MODULE__", "#@__MODULE__", "anonymous") && continue
if !isdefined(evalmod, k)
printstyled(io, "Warning: "; bold = true, color = :red)
println(io, "Binding `$k` not defined in this context. Refusing to exfiltrate.")
continue
end
v = getfield(evalmod, k)
end
try
Core.eval(getfield(store, :store), Expr(:(=), k, QuoteNode(v)))
catch err
println(io, "Assignment to store variable failed.")
Base.display_error(io, err, catch_backtrace())
end
end
end
function one_line_show(io::IO, var, val)
width = max(displaysize(io)[2], 40)
prefix = string("- ", strlimit(string(var), width ÷ 3), "::", strlimit(string(typeof(val)), width ÷ 3), " = ")
println(io, prefix, strlimit(repr(val), max(width - textwidth(prefix), 10)))
end
function is_complete(s)
ast = Base.parse_input_line(String(take!(copy(LineEdit.buffer(s)))), depwarn=false)
return !(isa(ast, Expr) && ast.head === :incomplete)
end
function init_transient_eval_module(mod, locals)
modns = get_module_names(mod)
newmod = Module()
# assign source code module name
modname = nameof(mod)
if modname != :anonymous
Core.eval(newmod, Expr(:(=), modname, mod))
end
# spoof @__MODULE__
Core.eval(newmod, Expr(:macro,
Expr(:call, Symbol("__MODULE__")),
Expr(:block, mod))
)
# insert local variables into current scope
Core.eval(newmod, Expr(:block, map(x->Expr(:(=), x...), [(k, maybe_quote(v)) for (k, v) in locals])...))
# insert variables in safehouse
Core.eval(newmod, Expr(:block, map(x->Expr(:(=), x...), [(k, maybe_quote(v)) for (k, v) in get_store_names() if !isdefined(newmod, k)])...))
# insert all bindings from the source module that aren't already defined in the eval module
Core.eval(newmod, Expr(:block, map(x->Expr(:(=), x...), [(k, maybe_quote(v)) for (k, v) in modns if !isdefined(newmod, k)])...))
return newmod
end
const PROMPT = Ref{Any}()
function debugprompt(mod, locals, trace, terminal, repl, ex, bt; nostack = false, file, fileline)
io = Base.pipe_writer(terminal)
evalmod = init_transient_eval_module(mod, locals)
try
if isassigned(PROMPT)
panel = PROMPT[]
panel.complete = InfiltratorCompletionProvider(mod, evalmod)
else
panel = PROMPT[] = REPL.LineEdit.Prompt("infil> ";
prompt_prefix = prompt_color,
prompt_suffix = Base.text_colors[:normal],
complete = InfiltratorCompletionProvider(mod, evalmod),
on_enter = is_complete)
panel.hist = REPL.REPLHistoryProvider(Dict{Symbol,Any}(:Infiltrator => panel))
REPL.history_reset_state(panel.hist)
end
search_prompt, skeymap = LineEdit.setup_search_keymap(panel.hist)
search_prompt.complete = REPL.LatexCompletions()
panel.on_done = (s, buf, ok) -> begin
if !ok
LineEdit.transition(s, :abort)
REPL.LineEdit.reset_state(s)
return false
end
line = String(take!(buf))
if isempty(line)
println(io)
LineEdit.reset_state(s)
return true
end
sline = strip(line)
if sline == "?"
show_help(io)
LineEdit.reset_state(s)
return true
elseif sline == "@trace"
print_verbose_stacktrace(io, trace, nostack ? 0 : 100)
LineEdit.reset_state(s)
return true
elseif startswith(sline, "@locals")
show_locals(io, locals, sline)
LineEdit.reset_state(s)
return true
elseif startswith(sline, "@exfiltrate")
exfiltrate_locals(io, evalmod, locals, sline)
LineEdit.reset_state(s)
return true
elseif sline == "@exception"
if ex !== nothing && bt !== nothing
Base.display_error(io, ex, nostack ? bt[1:1] : bt)
else
println(io, "No exception available")
end
println(io)
LineEdit.reset_state(s)
return true
elseif sline == "@toggle"
spot = (file, fileline)
ds = getfield(store, :disabled)
cs = getfield(store, :conditions)
if spot in ds
delete!(ds, spot)
if haskey(cs, spot)
println(io, "Conditionally enabled infiltration at this infiltration point.\n")
else
println(io, "Enabled infiltration at this infiltration point.\n")
end
else
push!(ds, spot)
println(io, "Disabled infiltration at this infiltration point.\n")
end
LineEdit.reset_state(s)
return true
elseif startswith(sline, "@cond")
spot = (file, fileline)
ds = getfield(store, :disabled)
cs = getfield(store, :conditions)
rest = lstrip(sline[6:end])
try
expr = Base.parse_input_line(rest)
@assert expr.head == :toplevel
# Unwrap the :toplevel node and replace with a :block
expr = Expr(:block, expr.args...)
# Wrap the expr in a closure.
# We need to gensym a new name to avoid this error:
# cannot declare #1#2 constant; it already has a value
# Pass a locals dict into the function so we access the locals at the
# next infiltration point rather than capturing the locals here.
fname = gensym(:cond)
locals_dict = gensym(:locals)
expr = quote
function $(fname)($locals_dict)
$(
( Expr(:(=), x, Expr(:ref, locals_dict, Expr(:call, Symbol, string(x))))
for x in keys(locals)
)...
)
$(expr)
end
$fname
end
# Eval and save in the dict.
result = Core.eval(evalmod, expr)
cs[spot] = result
# Remove the spot from the disabled set.
delete!(ds, spot)
println(io, "Conditionally enabled infiltration at this infiltration point.\n")
LineEdit.reset_state(s)
return true
catch err
# If we get an error, just punt to the eval code to handle the error.
line = rest
end
elseif sline == "@exit"
setfield!(store, :exiting, true)
if !REPL_HOOKED[]
printstyled(
io,
">> Restore infiltration capabilities with `Infiltrator.end_session!()` or you will not be able to enter a new session!\n";
color=:red,
bold=true
)
end
LineEdit.transition(s, :abort)
LineEdit.reset_state(s)
return true
elseif sline == "@continue"
LineEdit.transition(s, :abort)
LineEdit.reset_state(s)
return true
end
ok = true
result = nothing
expr = try
Base.parse_input_line(line)
catch err
if get(io, :color, false)
println(io, "Parsing error.")
else
printstyled(io, "Parsing error.", color = Base.error_color())
end
LineEdit.reset_state(s)
return false
end
@static if VERSION >= v"1.2.0-DEV.253"
try
result = interpret(expr, evalmod)
catch err
ok = false
result = @static if VERSION >= v"1.7"
Base.current_exceptions(current_task(); backtrace = true)
else
Base.catch_stack()
end
if nostack
result = map(result) do (err, bt)
@static if VERSION >= v"1.8-"
return (exception=err, backtrace=crop_backtrace(bt))
else
return err, []
end
end
else
result = map(result) do (err, bt)
@static if VERSION >= v"1.8-"
return (exception=err, backtrace=crop_backtrace(bt))
else
return err, crop_backtrace(bt)
end
end
end
if isdefined(Base, :ExceptionStack)
result = Base.ExceptionStack(result)
end
end
if !ok || !REPL.ends_with_semicolon(line)
REPL.print_response(repl, (result, !ok), true, true)
end
else
try
result = interpret(expr, evalmod)
catch err
ok = false
result = (err, nostack ? Any[] : crop_backtrace(catch_backtrace()))
end
if !ok || !REPL.ends_with_semicolon(line)
REPL.print_response(repl, ok ? result : result[1], ok ? nothing : result[2], true, true)
end
end
println(io)
LineEdit.reset_state(s)
return true
end
panel.keymap_dict = LineEdit.keymap(Dict{Any,Any}[skeymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults])
REPL.run_interface(terminal, REPL.LineEdit.ModalInterface([panel, search_prompt]))
catch e
e isa InterruptException || rethrow(e)
end
end
get_store_names(s = store) = get_module_names(getfield(s, :store), m -> names(m, all = true))
function get_module_names(m::Module, get_names = all_names)
ns = get_names(m)
out = Dict()
for n in ns
if isdefined(m, n) && !Base.isdeprecated(m, n) && n !== :eval && n !== :include && n !== :anonymous
out[n] = getfield(m, n)
end
end
out
end
function print_verbose_stacktrace(io, st, limit = 100)
if isempty(st)
println(io, "Toplevel scope\n")
return
end
len = length(st)
for (i, sf) in enumerate(st)
if i > limit
limit > 0 && println(io, "\n<< omitted $(len - limit) stack frames >>")
break
end
print_verbose_stackframe(io, sf, i, len)
end
println(io)
end
function print_verbose_stackframe(io, sf::StackTraces.StackFrame, i, len)
num_padding = ceil(Int, log10(len))
padding = num_padding + 3
print(io, "[", lpad(i, num_padding), "] ")
print_verbose_stackframe(io, sf, padding)
end
function print_verbose_stackframe(io, sf::StackTraces.StackFrame, padding = 2)
Base.StackTraces.show_spec_linfo(IOContext(io, :backtrace=>true), sf)
println(io)
sf.func == StackTraces.top_level_scope_sym && return
print(io, ' '^padding)
printstyled(io, "at ", color=:light_black)
file, line = string(sf.file), sf.line
file = fixup_stdlib_path(file)
file = something(Base.find_source_file(file), file)
printstyled(io, normpath(file), ":", line, '\n', color=:light_black)
end
# https://github.com/JuliaLang/julia/blob/1fb28ad8768cfdc077e968df7adf5716ae8eb9ab/base/methodshow.jl#L134-L148
function fixup_stdlib_path(path::String)
BUILD_STDLIB_PATH = isdefined(Sys, :BUILD_STDLIB_PATH) ?
Sys.BUILD_STDLIB_PATH :
dirname(abspath(joinpath(String((@which uuid1()).file), "..", "..", "..")))
STDLIB = Sys.STDLIB::String
if BUILD_STDLIB_PATH != STDLIB
# BUILD_STDLIB_PATH gets defined in sysinfo.jl
npath = normpath(path)
npath′ = replace(npath, normpath(BUILD_STDLIB_PATH) => normpath(STDLIB))
return npath == npath′ ? path : npath′
end
return path
end
function all_names(m)
symbols = Set(Symbol[])
seen = Set(Module[])
pred = (x, v) -> isdefined(m, x) && !Base.isdeprecated(m, x) && getfield(m, x) === v
# anonymous modules may not be rooted in any other modules
all_names(m, pred, symbols, seen)
for mod in values(Base.loaded_modules)
all_names(mod, pred, symbols, seen)
end
return symbols
end
function all_names(m, pred, symbols = Set(Symbol[]), seen = Set(Module[]))
push!(seen, m)
ns = names(m; all = true, imported = true)
for n in ns
isdefined(m, n) || continue
Base.isdeprecated(m, n) && continue
val = getfield(m, n)
val isa Core.IntrinsicFunction && continue
if val isa Module && !(val in seen)
all_names(val, pred, symbols, seen)
end
if pred(n, val)
push!(symbols, n)
end
end
return symbols
end
function maybe_quote(@nospecialize x)
is_ir_construct(x) && return QuoteNode(x)
isa(x, Expr) && return QuoteNode(x)
isa(x, Symbol) && return QuoteNode(x)
isa(x, GlobalRef) && return QuoteNode(x)
return x
end
let ir_constructs = collect(DataType,
filter(map(n->getfield(Core.IR,n), names(Core.IR))) do x
@nospecialize x
return isa(x, DataType)
end)
@eval is_ir_construct(@nospecialize x) = typeof(x) in $ir_constructs
end
function interpret(expr, evalmod)
out = Core.eval(evalmod, :(ans = $(expr)))
return out
end
function ast_transformer()
return function (@nospecialize(ex),)
if ex isa Expr
return Expr(:try, ex, false, false, :($(@__MODULE__).end_session!()))
end
return ex
end
end
# backtraces
function find_first_topelevel_scope(bt::Vector{<:Union{Base.InterpreterIP,Ptr{Cvoid}}})
for (i, ip) in enumerate(bt)
st = Base.StackTraces.lookup(ip)
ind = findfirst(st) do frame
linfo = frame.linfo
if linfo isa Core.CodeInfo
linetable = linfo.linetable
if isa(linetable, Vector) && length(linetable) ≥ 1
lin = first(linetable)
if isa(lin, Core.LineInfoNode) && lin.method === StackTraces.top_level_scope_sym
return true
end
end
else
return frame.func === StackTraces.top_level_scope_sym
end
end
ind === nothing || return i
end
return
end
function crop_backtrace(bt)
i = find_first_topelevel_scope(bt)
return bt[1:(i === nothing ? end : i)]
end
# completions
mutable struct InfiltratorCompletionProvider <: REPL.CompletionProvider
mod::Module
localmod::Module
end
function LineEdit.complete_line(c::InfiltratorCompletionProvider, s)
partial = REPL.beforecursor(s.input_buffer)
full = LineEdit.input_string(s)
ret, range, should_complete = completions(c, full, partial)
return ret, partial[range], should_complete
end
function completions(c::InfiltratorCompletionProvider, full, partial)
# repl backend completions
comps, range, should_complete = REPL.REPLCompletions.completions(full, lastindex(partial), c.mod)
ret = map(REPL.REPLCompletions.completion_text, comps)
# completions for local variables
comps, range, should_complete = REPL.REPLCompletions.completions(full, lastindex(partial), c.localmod)
prepend!(ret, map(REPL.REPLCompletions.completion_text, comps))
# completions for safehouse variables
comps, range, should_complete = REPL.REPLCompletions.completions(full, lastindex(partial), getfield(store, :store))
prepend!(ret, map(REPL.REPLCompletions.completion_text, comps))
# Infiltrator commands completions
commands = ["?", "@trace", "@locals", "@toggle", "@exit", "@continue", "@exfiltrate", "@exception"]
prepend!(ret, filter!(c -> startswith(c, partial), commands))
unique!(ret), range, should_complete
end
end # module