-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathDebugger.cpp
More file actions
343 lines (296 loc) · 12.4 KB
/
Debugger.cpp
File metadata and controls
343 lines (296 loc) · 12.4 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
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
#include <Ark/VM/Debugger.hpp>
#include <fmt/core.h>
#include <fmt/color.h>
#include <fmt/ostream.h>
#include <chrono>
#include <thread>
#include <charconv>
#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)
{
using namespace std::chrono_literals;
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, vm, context);
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
std::this_thread::sleep_for(50ms); // hack to wait for the diagnostics to be output to stderr, since we write to stdout and it's faster than stderr
}
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, "");
}
}
}
void Debugger::showStack(VM& vm, const ExecutionContext& context, const std::size_t count) const
{
std::size_t i = 1;
do
{
if (context.sp < i)
break;
const auto color = m_colorize ? fmt::fg(i % 2 == 0 ? fmt::color::forest_green : fmt::color::cornflower_blue) : fmt::text_style();
fmt::println(
m_os,
"{} -> {}",
fmt::styled(context.sp - i, color),
fmt::styled(context.stack[context.sp - i].toString(vm, /* show_as_code= */ true), color));
++i;
} while (i < count);
if (context.sp == 0)
fmt::println(m_os, "Stack is empty");
fmt::println(m_os, "");
}
void Debugger::showLocals(VM& vm, ExecutionContext& context, const std::size_t count) const
{
const std::size_t limit = context.locals[context.locals.size() - 2].size(); // -2 because we created a scope for the debugger
if (limit > 0 && count > 0)
{
fmt::println(m_os, "scope size: {}", limit);
fmt::println(m_os, "index | id | type | value");
std::size_t i = 0;
do
{
if (limit <= i)
break;
auto& [id, value] = context.locals[context.locals.size() - 2].atPosReverse(i);
const auto color = m_colorize ? fmt::fg(i % 2 == 0 ? fmt::color::forest_green : fmt::color::cornflower_blue) : fmt::text_style();
fmt::println(
m_os,
"{:>5} | {:3} | {:>9} | {}",
fmt::styled(limit - i - 1, color),
fmt::styled(id, color),
fmt::styled(std::to_string(value.valueType()), color),
fmt::styled(value.toString(vm, /* show_as_code= */ true), color));
++i;
} while (i < count);
}
else
fmt::println(m_os, "Current scope is empty");
fmt::println(m_os, "");
}
std::optional<std::string> Debugger::getCommandArg(const std::string& command, const std::string& line)
{
std::string arg = line.substr(command.size());
Utils::trimWhitespace(arg);
if (arg.empty())
return std::nullopt;
return arg;
}
std::optional<std::size_t> Debugger::parseStringAsInt(const std::string& str)
{
std::size_t result = 0;
auto [ptr, ec] = std::from_chars(str.data(), str.data() + str.size(), result);
if (ec == std::errc())
return result;
return std::nullopt;
}
std::optional<std::size_t> Debugger::getArgAndParseOrError(const std::string& command, const std::string& line, const std::size_t default_value) const
{
const auto maybe_arg = getCommandArg(command, line);
std::size_t count = default_value;
if (maybe_arg)
{
if (const auto maybe_int = parseStringAsInt(maybe_arg.value()))
count = maybe_int.value();
else
{
fmt::println(m_os, "Couldn't parse argument as an integer");
return std::nullopt;
}
}
return count;
}
std::optional<std::string> Debugger::prompt(const std::size_t ip, const std::size_t pp, VM& vm, ExecutionContext& context)
{
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.starts_with("stack"))
{
if (auto arg = getArgAndParseOrError("stack", line, /* default_value= */ 5))
showStack(vm, context, arg.value());
else
return std::nullopt;
}
else if (line.starts_with("locals"))
{
if (auto arg = getArgAndParseOrError("locals", line, /* default_value= */ 5))
showLocals(vm, context, arg.value());
else
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");
fmt::println(m_os, " stack <n=5> -- show the last n values on the stack");
fmt::println(m_os, " locals <n=5> -- show the last n values on the locals' stack");
}
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));
}
}