|
| 1 | +__precompile__() |
1 | 2 | module DebuggerFramework
|
| 3 | + using TerminalUI |
| 4 | + |
| 5 | + abstract StackFrame |
2 | 6 |
|
3 |
| -# package code goes here |
| 7 | + function print_var(io::IO, name, val::Nullable, undef_callback) |
| 8 | + print(" | ") |
| 9 | + if isnull(val) |
| 10 | + @assert false |
| 11 | + else |
| 12 | + val = get(val) |
| 13 | + T = typeof(val) |
| 14 | + val = repr(val) |
| 15 | + if length(val) > 150 |
| 16 | + val = Suppressed("$(length(val)) bytes of output") |
| 17 | + end |
| 18 | + println(io, name, "::", T, " = ", val) |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + function print_locals(io::IO, args...) |
| 23 | + end |
| 24 | + |
| 25 | + print_locdesc(io, frame) = println(io, locdesc(frame)) |
| 26 | + function print_frame(_, io::IO, num, frame) |
| 27 | + print(io, "[$num] ") |
| 28 | + print_locdesc(io, frame) |
| 29 | + print_locals(io, frame) |
| 30 | + end |
| 31 | + |
| 32 | + function print_backtrace(state) |
| 33 | + for (num, frame) in enumerate(state.stack) |
| 34 | + print_frame(state, Base.pipe_writer(state.terminal), num, frame) |
| 35 | + end |
| 36 | + end |
| 37 | + |
| 38 | + print_backtrace(state, _::Void) = nothing |
| 39 | + |
| 40 | + function execute_command(state, frame, ::Val{:bt}, cmd) |
| 41 | + print_backtrace(state) |
| 42 | + println() |
| 43 | + return false |
| 44 | + end |
| 45 | + |
| 46 | + function execute_command(state, interp, ::Union{Val{:f},Val{:fr}}, command) |
| 47 | + subcmds = split(command,' ')[2:end] |
| 48 | + if isempty(subcmds) || subcmds[1] == "v" |
| 49 | + print_frame(state, Base.pipe_writer(state.terminal), state.level, state.stack[state.level]) |
| 50 | + return false |
| 51 | + else |
| 52 | + new_level = parse(Int, subcmds[1]) |
| 53 | + new_stack_idx = length(state.stack)-(new_level-1) |
| 54 | + if new_stack_idx > length(state.stack) || new_stack_idx < 1 |
| 55 | + print_with_color(:red, STDERR, "Not a valid frame index\n") |
| 56 | + return false |
| 57 | + end |
| 58 | + state.level = new_level |
| 59 | + end |
| 60 | + return true |
| 61 | + end |
| 62 | + |
| 63 | + """ |
| 64 | + Start debugging the specified code in the the specified environment. |
| 65 | + The second argument should default to the global environment if such |
| 66 | + an environment exists for the language in question. |
| 67 | + """ |
| 68 | + function debug |
| 69 | + end |
| 70 | + |
| 71 | + type DebuggerState |
| 72 | + stack |
| 73 | + level |
| 74 | + main_mode |
| 75 | + terminal |
| 76 | + end |
| 77 | + |
| 78 | + function print_status_synthtic(io, state, frame, lines_before, total_lines) |
| 79 | + return 0 |
| 80 | + end |
| 81 | + |
| 82 | + haslocinfo(frame) = false |
| 83 | + locdesc(frame) = "unknown function" |
| 84 | + |
| 85 | + print_status(io, state) = print_status(io, state, state.stack[state.level]) |
| 86 | + function print_status(io, state, frame) |
| 87 | + # Buffer to avoid flickering |
| 88 | + outbuf = IOBuffer() |
| 89 | + print_with_color(:bold, outbuf, "In ", locdesc(frame), "\n") |
| 90 | + if haslocinfo(frame) |
| 91 | + # Print location here |
| 92 | + else |
| 93 | + buf = IOBuffer() |
| 94 | + active_line = print_status_synthtic(buf, state, frame, 2, 5) |
| 95 | + code = split(String(take!(buf)),'\n') |
| 96 | + @assert active_line <= length(code) |
| 97 | + for (lineno, line) in enumerate(code) |
| 98 | + if lineno == active_line |
| 99 | + print_with_color(:yellow, outbuf, "=> ", bold = true); println(outbuf, line) |
| 100 | + else |
| 101 | + print_with_color(:bold, outbuf, "? "); println(outbuf, line) |
| 102 | + end |
| 103 | + end |
| 104 | + end |
| 105 | + print(io, String(take!(outbuf))) |
| 106 | + end |
| 107 | + |
| 108 | + immutable AbstractDiagnostic; end |
| 109 | + |
| 110 | + function execute_command |
| 111 | + end |
| 112 | + |
| 113 | + using Base: LineEdit, REPL |
| 114 | + function RunDebugger(stack, terminal = Base.active_repl.t) |
| 115 | + promptname(level, name) = "$level|$name > " |
| 116 | + |
| 117 | + repl = Base.active_repl |
| 118 | + state = DebuggerState(stack, 1, nothing, terminal) |
| 119 | + |
| 120 | + # Setup debug panel |
| 121 | + panel = LineEdit.Prompt(promptname(state.level, "debug"); |
| 122 | + prompt_prefix="\e[38;5;166m", |
| 123 | + prompt_suffix=Base.text_colors[:white], |
| 124 | + on_enter = s->true) |
| 125 | + |
| 126 | + # For now use the regular REPL completion provider |
| 127 | + replc = Base.REPL.REPLCompletionProvider() |
| 128 | + |
| 129 | + |
| 130 | + panel.hist = REPL.REPLHistoryProvider(Dict{Symbol,Any}(:debug => panel)) |
| 131 | + Base.REPL.history_reset_state(panel.hist) |
| 132 | + |
| 133 | + search_prompt, skeymap = Base.LineEdit.setup_search_keymap(panel.hist) |
| 134 | + search_prompt.complete = Base.REPL.LatexCompletions() |
| 135 | + |
| 136 | + state.main_mode = panel |
| 137 | + |
| 138 | + panel.on_done = (s,buf,ok)->begin |
| 139 | + line = String(take!(buf)) |
| 140 | + old_level = state.level |
| 141 | + if !ok || strip(line) == "q" |
| 142 | + LineEdit.transition(s, :abort) |
| 143 | + LineEdit.reset_state(s) |
| 144 | + return false |
| 145 | + end |
| 146 | + if isempty(strip(line)) && length(panel.hist.history) > 0 |
| 147 | + command = panel.hist.history[end] |
| 148 | + else |
| 149 | + command = strip(line) |
| 150 | + end |
| 151 | + do_print_status = true |
| 152 | + cmd1 = split(command,' ')[1] |
| 153 | + do_print_status = try |
| 154 | + execute_command(state, state.stack[state.level], Val{Symbol(cmd1)}(), command) |
| 155 | + catch err |
| 156 | + isa(err, AbstractDiagnostic) || rethrow(err) |
| 157 | + caught = false |
| 158 | + for interp_idx in length(state.top_interp.stack):-1:1 |
| 159 | + if process_exception!(state.top_interp.stack[interp_idx], err, interp_idx == length(top_interp.stack)) |
| 160 | + interp = state.top_interp = state.top_interp.stack[interp_idx] |
| 161 | + resize!(state.top_interp.stack, interp_idx) |
| 162 | + caught = true |
| 163 | + break |
| 164 | + end |
| 165 | + end |
| 166 | + !caught && rethrow(err) |
| 167 | + display_diagnostic(STDERR, state.interp.code, err) |
| 168 | + println(STDERR) |
| 169 | + LineEdit.reset_state(s) |
| 170 | + return true |
| 171 | + end |
| 172 | + if old_level != state.level |
| 173 | + panel.prompt = promptname(state.level,"debug") |
| 174 | + end |
| 175 | + LineEdit.reset_state(s) |
| 176 | + if isempty(state.stack) |
| 177 | + LineEdit.transition(s, :abort) |
| 178 | + LineEdit.reset_state(s) |
| 179 | + return false |
| 180 | + end |
| 181 | + if do_print_status |
| 182 | + print_status(Base.pipe_writer(terminal), state) |
| 183 | + end |
| 184 | + return true |
| 185 | + end |
| 186 | + |
| 187 | + const all_commands = ("q", "s", "si", "finish", "bt", "loc", "ind", "shadow", |
| 188 | + "up", "down", "ns", "nc", "n", "se") |
| 189 | + |
| 190 | + const repl_switch = Dict{Any,Any}( |
| 191 | + '`' => function (s,args...) |
| 192 | + if isempty(s) || position(LineEdit.buffer(s)) == 0 |
| 193 | + prompt = language_specific_prompt(state, state.interp) |
| 194 | + buf = copy(LineEdit.buffer(s)) |
| 195 | + LineEdit.transition(s, prompt) do |
| 196 | + LineEdit.state(s, prompt).input_buffer = buf |
| 197 | + end |
| 198 | + else |
| 199 | + LineEdit.edit_insert(s,key) |
| 200 | + end |
| 201 | + end |
| 202 | + ) |
| 203 | + |
| 204 | + b = Dict{Any,Any}[skeymap, LineEdit.history_keymap, LineEdit.default_keymap, LineEdit.escape_defaults] |
| 205 | + panel.keymap_dict = LineEdit.keymap([repl_switch;b]) |
| 206 | + |
| 207 | + # Skip evaluated values (e.g. constants) |
| 208 | + print_status(Base.pipe_writer(terminal), state) |
| 209 | + Base.REPL.run_interface(terminal, LineEdit.ModalInterface([panel,search_prompt])) |
| 210 | + end |
4 | 211 |
|
5 | 212 | end # module
|
0 commit comments