goalc: add debug server (#4365)

This adds a debug server to `goalc` that sends JSON over the socket to
communicate with an external debugger using the Debug Adapter Protocol.

This lets us debug GOAL code in a proper debugger with breakpoints, step
over, step in and step out per line, stack frames and supports watches
for global symbols, registers and local variables (local variables only
work within the most recent stack frame). Special registers (`r13`,
`r14`, `r15`, argument registers, etc.) are tracked separately and the
current process register even displays the type of the current `pp` if
possible.

Watches that track addresses holding a reference type generate a list of
field names according to the object's type. All fields will show their
name, type and value and, depending on the type, will try to infer extra
info like symbol names/values, function names for `function` fields,
enum values and more. This also works nested, so any field that is also
a reference type can also be accessed and display its fields, etc.
Dynamic arrays are also supported where possible, e.g. in
`inline-array-class` children and boxed arrays, it will figure out the
value of the `length` field and access the memory up to that point so
all the elements can be accessed and viewed from the `data` field.

Our VS Code extension implements the DAP in
open-goal/opengoal-vscode#375.

Using it is as simple as connecting a REPL to a running game instance
with `(lt)`, compiling with `(mi)` and, with the extension installed,
pressing F5 in VS Code to start the debugger. By default, it will try to
connect to the game that the active `.gc` file is from, the socket port
is different per game (8128 for Jak 1, 8129 for Jak 2, 8130 for Jak 3).
The `launch.json` was updated with two entries for this, the second
entry lets you pick the game/port manually if desired.

~~Not tested on Windows.~~ Only supports x86 for now.
This commit is contained in:
Hat Kid
2026-07-27 22:28:28 +02:00
committed by GitHub
parent e0dc11554d
commit fe2086acfb
17 changed files with 2515 additions and 42 deletions
+99
View File
@@ -7,6 +7,7 @@
#include "CodeGenerator.h"
#include <algorithm>
#include <stdexcept>
#include <unordered_set>
@@ -19,6 +20,103 @@
using namespace emitter;
namespace {
void record_local_variables(const FunctionEnv* func, FunctionDebugInfo* debug) {
const auto& allocations = func->allocations();
if (!allocations.ok || allocations.ass_as_ranges.empty()) {
return;
}
// ireg id -> source name, parameters first, then anything bound in a lexical scope
std::unordered_map<int, std::pair<std::string, bool>> names;
for (const auto& [symbol, reg_val] : func->params) {
if (reg_val) {
names[reg_val->ireg().id] = {symbol.name_ptr, true};
}
}
for (const auto& env : func->child_envs()) {
auto* lexical = dynamic_cast<LexicalEnv*>(env.get());
if (!lexical) {
continue;
}
for (const auto& [symbol, reg_val] : lexical->vars) {
if (reg_val && names.find(reg_val->ireg().id) == names.end()) {
names[reg_val->ireg().id] = {symbol.name_ptr, false};
}
}
}
if (names.empty()) {
return;
}
const int instruction_count = int(func->code().size());
for (const auto& [ireg_id, name_info] : names) {
if (ireg_id < 0 || ireg_id >= int(allocations.ass_as_ranges.size()) ||
ireg_id >= int(func->reg_vals().size())) {
continue;
}
const auto& range = allocations.ass_as_ranges.at(ireg_id);
LocalVariableDebugInfo local;
local.name = name_info.first;
local.is_parameter = name_info.second;
local.type = func->reg_vals().at(ireg_id)->type();
for (int instr = 0; instr < instruction_count; instr++) {
if (!range.is_live_at_instr(instr)) {
continue;
}
const auto& assignment = range.get(instr);
if (!assignment.is_assigned()) {
continue;
}
VariableLocation here;
here.start_ir = instr;
here.end_ir = instr;
if (assignment.kind == Assignment::Kind::REGISTER) {
here.kind = VariableLocation::Kind::REGISTER;
here.reg = assignment.reg.id();
} else if (assignment.kind == Assignment::Kind::STACK) {
here.kind = VariableLocation::Kind::STACK;
// spilled variables sit at rsp + slot * 8, matching how the spill ops address them
here.stack_offset = allocations.get_slot_for_spill(assignment.stack_slot) * GPR_SIZE;
} else {
continue;
}
// extend the previous interval instead of starting a new one where nothing changed
if (!local.locations.empty()) {
auto& previous = local.locations.back();
if (previous.end_ir == instr - 1 && previous.kind == here.kind &&
previous.reg == here.reg && previous.stack_offset == here.stack_offset) {
previous.end_ir = instr;
continue;
}
}
local.locations.push_back(here);
}
if (!local.locations.empty()) {
debug->locals.push_back(std::move(local));
}
}
// parameters first, then alphabetically
std::sort(debug->locals.begin(), debug->locals.end(),
[](const LocalVariableDebugInfo& a, const LocalVariableDebugInfo& b) {
if (a.is_parameter != b.is_parameter) {
return a.is_parameter;
}
return a.name < b.name;
});
}
} // namespace
CodeGenerator::CodeGenerator(FileEnv* env,
DebugInfo* debug_info,
GameVersion version,
@@ -48,6 +146,7 @@ std::vector<u8> CodeGenerator::run(const TypeSystem* ts) {
for (auto& x : f->code()) {
rec.debug->ir_strings.push_back(x->print());
}
record_local_variables(f.get(), rec.debug);
}
// next, add all static objects.