-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDebugger.cpp
More file actions
226 lines (195 loc) · 8.1 KB
/
Debugger.cpp
File metadata and controls
226 lines (195 loc) · 8.1 KB
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
#include <Ark/VM/Debugger.hpp>
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/ostream.h>
#include <Ark/State.hpp>
#include <Ark/VM/VM.hpp>
#include <Ark/Utils/Files.hpp>
#include <Ark/Compiler/Welder.hpp>
#include <Ark/Compiler/BytecodeReader.hpp>
#include <Ark/Error/Diagnostics.hpp>
namespace Ark::internal
{
Debugger::Debugger(const ExecutionContext& context, const std::vector<std::filesystem::path>& libenv, const std::vector<std::string>& symbols, const std::vector<Value>& constants) :
m_libenv(libenv),
m_symbols(symbols),
m_constants(constants),
m_os(std::cout),
m_colorize(true)
{
saveState(context);
}
Debugger::Debugger(const std::vector<std::filesystem::path>& libenv, const std::string& path_to_prompt_file, std::ostream& os, const std::vector<std::string>& symbols, const std::vector<Value>& constants) :
m_libenv(libenv),
m_symbols(symbols),
m_constants(constants),
m_os(os),
m_colorize(false),
m_prompt_stream(std::make_unique<std::ifstream>(path_to_prompt_file))
{}
void Debugger::saveState(const ExecutionContext& context)
{
m_states.emplace_back(
std::make_unique<SavedState>(
context.ip,
context.pp,
context.sp,
context.fc,
context.locals,
context.stacked_closure_scopes));
}
void Debugger::resetContextToSavedState(ExecutionContext& context)
{
const auto& [ip, pp, sp, fc, locals, closure_scopes] = *m_states.back();
context.locals = locals;
context.stacked_closure_scopes = closure_scopes;
context.ip = ip;
context.pp = pp;
context.sp = sp;
context.fc = fc;
m_states.pop_back();
}
void Debugger::run(VM& vm, ExecutionContext& context, const bool from_breakpoint)
{
if (from_breakpoint)
showContext(vm, context);
m_running = true;
const bool is_vm_running = vm.m_running;
const std::size_t ip_at_breakpoint = context.ip,
pp_at_breakpoint = context.pp;
// create dedicated scope, so that we won't be overwriting existing variables
context.locals.emplace_back(context.scopes_storage.data(), context.locals.back().storageEnd());
std::size_t last_ip = 0;
while (true)
{
std::optional<std::string> maybe_input = prompt(ip_at_breakpoint, pp_at_breakpoint);
if (maybe_input)
{
const std::string& line = maybe_input.value();
if (const auto compiled = compile(m_code + line, vm.m_state.m_pages.size()); compiled.has_value())
{
context.ip = last_ip;
context.pp = vm.m_state.m_pages.size();
vm.m_state.extendBytecode(compiled->pages, compiled->symbols, compiled->constants);
if (vm.safeRun(context) == 0)
{
// executing code worked
m_code += line + "\n";
// place ip to end of bytecode instruction (HALT)
last_ip = context.ip - 4;
const Value* maybe_value = vm.peekAndResolveAsPtr(context);
if (maybe_value != nullptr && maybe_value->valueType() != ValueType::Undefined && maybe_value->valueType() != ValueType::InstPtr)
fmt::println(
m_os,
"{}",
fmt::styled(
maybe_value->toString(vm),
m_colorize ? fmt::fg(fmt::color::chocolate) : fmt::text_style()));
}
}
}
else
break;
}
context.locals.pop_back();
// we do not want to retain code from the past executions
m_code.clear();
m_line_count = 0;
// we hit a HALT instruction that set 'running' to false, ignore that if we were still running!
vm.m_running = is_vm_running;
m_running = false;
}
void Debugger::showContext(const VM& vm, const ExecutionContext& context) const
{
// show the line where the breakpoint hit
const auto maybe_source_loc = vm.findSourceLocation(context.ip, context.pp);
if (maybe_source_loc)
{
const auto filename = vm.m_state.m_filenames[maybe_source_loc->filename_id];
if (Utils::fileExists(filename))
{
fmt::println(m_os, "");
Diagnostics::makeContext(
Diagnostics::ErrorLocation {
.filename = filename,
.start = FilePos { .line = maybe_source_loc->line, .column = 0 },
.end = std::nullopt,
.maybe_content = std::nullopt },
m_os,
/* maybe_context= */ std::nullopt,
/* colorize= */ m_colorize);
fmt::println(m_os, "");
}
}
}
std::optional<std::string> Debugger::prompt(const std::size_t ip, const std::size_t pp)
{
std::string code;
long open_parens = 0;
long open_braces = 0;
while (true)
{
const bool unfinished_block = open_parens != 0 || open_braces != 0;
fmt::print(
m_os,
"dbg[{},{}]:{:0>3}{} ",
fmt::format("pp:{}", fmt::styled(pp, m_colorize ? fmt::fg(fmt::color::green) : fmt::text_style())),
fmt::format("ip:{}", fmt::styled(ip, m_colorize ? fmt::fg(fmt::color::cyan) : fmt::text_style())),
m_line_count,
unfinished_block ? ":" : ">");
std::string line;
if (m_prompt_stream)
{
std::getline(*m_prompt_stream, line);
fmt::println(m_os, "{}", line); // because nothing is printed otherwise, and prompts get printed on the same line
}
else
std::getline(std::cin, line);
Utils::trimWhitespace(line);
if (line == "c" || line == "continue" || line.empty())
{
fmt::println(m_os, "dbg: continue");
return std::nullopt;
}
else if (line == "q" || line == "quit")
{
fmt::println(m_os, "dbg: stop");
m_quit_vm = true;
return std::nullopt;
}
else if (line == "help")
{
fmt::println(m_os, "Available commands:");
fmt::println(m_os, " help -- display this message");
fmt::println(m_os, " c, continue -- resume execution");
fmt::println(m_os, " q, quit -- quit the debugger, stopping the script execution");
}
else
{
code += line;
open_parens += Utils::countOpenEnclosures(line, '(', ')');
open_braces += Utils::countOpenEnclosures(line, '{', '}');
++m_line_count;
if (open_braces == 0 && open_parens == 0)
break;
}
}
return code;
}
std::optional<CompiledPrompt> Debugger::compile(const std::string& code, const std::size_t start_page_at_offset) const
{
Welder welder(0, m_libenv, DefaultFeatures);
if (!welder.computeASTFromStringWithKnownSymbols(code, m_symbols))
return std::nullopt;
if (!welder.generateBytecodeUsingTables(m_symbols, m_constants, start_page_at_offset))
return std::nullopt;
BytecodeReader bcr;
bcr.feed(welder.bytecode());
const auto syms = bcr.symbols();
const auto vals = bcr.values(syms);
const auto files = bcr.filenames(vals);
const auto inst_locs = bcr.instLocations(files);
const auto [pages, _] = bcr.code(inst_locs);
return std::optional(CompiledPrompt(pages, syms.symbols, vals.values));
}
}