summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-13 09:28:38 -0300
committerGabriel Schneider <[email protected]>2026-11-13 09:28:38 -0300
commit5c0230ea5a77f85de77ad4354788cd6c27aa1ef6 (patch)
treee01a903bdee092965b86e118dfaab85c3bd60004 /src
parent5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (diff)
downloadcomputeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.tar.gz
computeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.zip
debugger: follow natural V8 deoptimization with adaptive live views
Diffstat (limited to 'src')
-rw-r--r--src/cli/dbg.zig6
-rw-r--r--src/cli/serve.zig14
-rw-r--r--src/trace/Debugger.zig22
-rw-r--r--src/trace/Symbols.zig59
-rw-r--r--src/trace/control.zig1
5 files changed, 93 insertions, 9 deletions
diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig
index c60bff2..77ca40a 100644
--- a/src/cli/dbg.zig
+++ b/src/cli/dbg.zig
@@ -174,6 +174,12 @@ const Session = struct {
s.view = null;
_ = try if (is(cmd, "step", "s")) s.d.step() else if (is(cmd, "stepi", "si")) s.d.stepi() else s.d.cont();
try s.showStop();
+ } else if (std.mem.eql(u8, cmd, "source-map")) {
+ const from = arg orelse return error.MissingArgument;
+ const to = words.next() orelse return error.MissingArgument;
+ if (words.next() != null) return error.InvalidSourceMap;
+ try s.d.mapSource(from, to);
+ try s.rec.flush();
} else if (is(cmd, "break", "b")) {
const where = arg orelse return error.MissingArgument;
if (std.mem.startsWith(u8, where, "usdt:")) {
diff --git a/src/cli/serve.zig b/src/cli/serve.zig
index 47ec1ed..23d307b 100644
--- a/src/cli/serve.zig
+++ b/src/cli/serve.zig
@@ -29,7 +29,7 @@ const Server = struct {
}
fn reply(s: *Server, d: *zt.Debugger, status: zt.control.Status, reason: []const u8, elapsed: u64) !void {
- try zt.control.send(s.writer, .{ .status = status, .pid = d.proc.pid, .tid = @intCast(d.proc.tid), .moments = s.rec.moments, .pc = d.regs.rip, .reason = reason, .elapsed_ns = elapsed });
+ try zt.control.send(s.writer, .{ .status = status, .pid = d.proc.pid, .tid = @intCast(d.proc.tid), .moments = s.rec.moments, .pc = d.regs.rip, .reason = reason, .elapsed_ns = elapsed, .capture_ns = if (status == .running) 0 else d.capture_ns });
}
fn waiting(ctx: *anyopaque, d: *zt.Debugger) !void {
@@ -94,13 +94,16 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
const cmd = words.next() orelse continue;
if (std.mem.eql(u8, cmd, "quit")) break;
const started = std.Io.Clock.awake.now(init.io);
+ d.capture_ns = 0;
var reason_buf: [128]u8 = undefined;
const reason = execute(&d, &server, cmd, std.mem.trim(u8, words.rest(), " \t\r"), &reason_buf) catch |err| {
try server.reply(&d, if (d.done) .exited else .failed, @errorName(err), 0);
continue;
};
- if ((std.mem.eql(u8, cmd, "inspect") or std.mem.eql(u8, cmd, "symbols")) and rec.delta.items.len > 0) try rec.commit();
+ if ((std.mem.eql(u8, cmd, "inspect") or std.mem.eql(u8, cmd, "symbols") or std.mem.eql(u8, cmd, "source-map")) and rec.delta.items.len > 0) try rec.commit();
+ const flush_started = std.Io.Clock.awake.now(init.io);
try rec.flush();
+ if (d.capture_ns != 0) d.capture_ns += @intCast(flush_started.durationTo(std.Io.Clock.awake.now(init.io)).toNanoseconds());
const elapsed: u64 = @intCast(started.durationTo(std.Io.Clock.awake.now(init.io)).toNanoseconds());
try server.reply(&d, if (d.done) .exited else .stopped, reason, elapsed);
}
@@ -116,6 +119,13 @@ fn execute(d: *zt.Debugger, s: *Server, cmd: []const u8, arg: []const u8, buf: [
const sig = try std.fmt.parseInt(u32, arg, 0);
if (sig > 64) return error.InvalidSignal;
d.proc.resume_signal = sig;
+ } else if (std.mem.eql(u8, cmd, "source-map")) {
+ var words = std.mem.tokenizeScalar(u8, arg, ' ');
+ const from = words.next() orelse return error.MissingPrefix;
+ const to = words.next() orelse return error.MissingPath;
+ if (words.next() != null) return error.InvalidSourceMap;
+ try d.mapSource(from, to);
+ return "source prefix mapped; target unchanged";
} else if (std.mem.eql(u8, cmd, "symbols")) {
if (arg.len == 0) return error.MissingPath;
try d.loadJit(arg);
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 55f9cd1..08d3b30 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -77,6 +77,8 @@ asan_shadow: ?u64 = null,
/// glibc's heap, recorded at every stop as `heap.zig` reads it (`--heap`): null for not at all.
heap: ?@import("heap.zig").Watch = null,
breakpoints: std.ArrayList(Breakpoint) = .empty,
+/// Stop-state capture only; excludes waiting for the target and flushing a recorder.
+capture_ns: u64 = 0,
/// The current thread's registers at its last stop.
regs: Regs = .{},
/// The target has exited.
@@ -534,9 +536,11 @@ fn emit(d: *Debugger, ev: Event, payload: []const u8) !void {
/// the watched memory that changed, then the stop.
fn report(d: *Debugger, stop: Stop) !Stop {
if (d.sink == null) return stop;
+ const started = std.Io.Clock.awake.now(d.syms.io);
+ defer d.capture_ns = @intCast(started.durationTo(std.Io.Clock.awake.now(d.syms.io)).toNanoseconds());
if (!d.done) {
- // Code loaded or JIT-compiled while running without syscall stops.
- if (d.syms.mappingAt(d.regs.rip) == null) try d.refreshMaps();
+ // Every stop owns its mapping snapshot, including data mapped while PC stayed in known code.
+ try d.refreshMaps();
try d.emitLine();
if (d.asan and d.asan_shadow == null) try d.findShadow();
// With the 128 bytes under the stack pointer: the red zone, a leaf's locals.
@@ -653,6 +657,14 @@ pub fn inspectCode(d: *Debugger, addr: u64, count: usize) !void {
}
}
+pub fn mapSource(d: *Debugger, from: []const u8, to: []const u8) !void {
+ try d.syms.mapSource(from, to);
+ d.file_ids.clearRetainingCapacity();
+ d.code_len.clearRetainingCapacity();
+ try d.emitLine();
+ try d.emitCode();
+}
+
pub fn loadJit(d: *Debugger, path: []const u8) !void {
try d.refreshMaps();
try d.syms.loadJit(path);
@@ -983,11 +995,7 @@ pub fn decode(d: *Debugger, nr: u16) ![]const u8 {
/// The string id of a line's file: its path, recorded the first time.
fn fileId(d: *Debugger, l: Symbols.Line) !u64 {
if (d.file_ids.get(l.key)) |id| return id;
- // As the compilation saw it: its directory, the file's, its name, each
- // relative to the one before (glibc's: /usr/src/debug/glibc/glibc/csu/../sysdeps/...).
- var cwd: [4096]u8 = undefined;
- const cwd_len = if (std.fs.path.isAbsolute(l.base) or std.fs.path.isAbsolute(l.dir) or std.fs.path.isAbsolute(l.name)) 0 else try std.Io.Dir.cwd().realPath(d.syms.io, &cwd);
- const path = try std.fs.path.resolveAlloc(d.gpa, &.{ cwd[0..cwd_len], l.base, l.dir, l.name });
+ const path = try d.syms.sourcePath(l);
const named = d.files.getOrPut(d.gpa, path) catch |err| {
d.gpa.free(path);
return err;
diff --git a/src/trace/Symbols.zig b/src/trace/Symbols.zig
index f7e53ef..a7df35e 100644
--- a/src/trace/Symbols.zig
+++ b/src/trace/Symbols.zig
@@ -11,6 +11,7 @@ const Dwarf = std.debug.Dwarf;
gpa: std.mem.Allocator,
io: std.Io,
maps: Maps = .{},
+source_maps: std.ArrayList(SourceMap) = .empty,
modules: std.ArrayList(Module) = .empty,
/// The line-table row `lineAt` answered from last, as target addresses.
row: struct { start: u64 = 0, len: u64 = 0, line: ?Line = null } = .{},
@@ -23,6 +24,8 @@ demangle: ?*const fn (name: []const u8, buf: []u8) ?[]const u8 = null,
/// function that has several (`open`, not `__open64`).
imported: std.StringHashMapUnmanaged(void) = .empty,
+pub const SourceMap = struct { from: []const u8, to: []const u8 };
+
pub const Mapping = struct {
start: u64,
end: u64,
@@ -212,11 +215,67 @@ fn deinitModule(m: *Module, gpa: std.mem.Allocator) void {
gpa.free(m.path);
}
+/// Map a build-machine source prefix to its local checkout. Longest prefix wins.
+pub fn mapSource(s: *Symbols, from: []const u8, to: []const u8) !void {
+ if (from.len == 0 or to.len == 0) return error.InvalidSourceMap;
+ var cwd: [4096]u8 = undefined;
+ const n = if (std.fs.path.isAbsolute(from) and std.fs.path.isAbsolute(to)) 0 else try std.Io.Dir.cwd().realPath(s.io, &cwd);
+ const a = try std.fs.path.resolveAlloc(s.gpa, &.{ cwd[0..n], from });
+ errdefer s.gpa.free(a);
+ const b = try std.fs.path.resolveAlloc(s.gpa, &.{ cwd[0..n], to });
+ errdefer s.gpa.free(b);
+ for (s.source_maps.items) |*map| if (std.mem.eql(u8, map.from, a)) {
+ s.gpa.free(map.from);
+ s.gpa.free(map.to);
+ map.* = .{ .from = a, .to = b };
+ return;
+ };
+ try s.source_maps.append(s.gpa, .{ .from = a, .to = b });
+}
+
+/// Owned canonical source path, shared by debugger clients and recorded metadata.
+pub fn sourcePath(s: *Symbols, l: Line) ![]u8 {
+ var cwd: [4096]u8 = undefined;
+ const n = if (std.fs.path.isAbsolute(l.base) or std.fs.path.isAbsolute(l.dir) or std.fs.path.isAbsolute(l.name)) 0 else try std.Io.Dir.cwd().realPath(s.io, &cwd);
+ const path = try std.fs.path.resolveAlloc(s.gpa, &.{ cwd[0..n], l.base, l.dir, l.name });
+ errdefer s.gpa.free(path);
+ var best: ?SourceMap = null;
+ for (s.source_maps.items) |map| {
+ if (!std.mem.startsWith(u8, path, map.from)) continue;
+ if (path.len > map.from.len and path[map.from.len] != '/' and !std.mem.eql(u8, map.from, "/")) continue;
+ if (best == null or map.from.len > best.?.from.len) best = map;
+ }
+ if (best) |map| {
+ const result = try std.fs.path.join(s.gpa, &.{ map.to, std.mem.trimStart(u8, path[map.from.len..], "/") });
+ s.gpa.free(path);
+ return result;
+ }
+ return path;
+}
+
+test "source prefixes respect boundaries, normalize paths and choose the longest" {
+ var s: Symbols = .{ .gpa = std.testing.allocator, .io = undefined };
+ defer s.deinit();
+ try s.mapSource("/remote", "/local");
+ try s.mapSource("/remote/v8", "/checkout");
+ const a = try s.sourcePath(.{ .key = 0, .base = "/remote/v8/out/debug", .dir = "../../src", .name = "deoptimizer.cc", .line = 1, .column = 0 });
+ defer s.gpa.free(a);
+ try std.testing.expectEqualStrings("/checkout/src/deoptimizer.cc", a);
+ const b = try s.sourcePath(.{ .key = 0, .dir = "/remote-other", .name = "a.cc", .line = 1, .column = 0 });
+ defer s.gpa.free(b);
+ try std.testing.expectEqualStrings("/remote-other/a.cc", b);
+}
+
pub fn deinit(s: *Symbols) void {
for (s.modules.items) |*m| deinitModule(m, s.gpa);
s.modules.deinit(s.gpa);
s.imported.deinit(s.gpa);
s.free(s.maps);
+ for (s.source_maps.items) |map| {
+ s.gpa.free(map.from);
+ s.gpa.free(map.to);
+ }
+ s.source_maps.deinit(s.gpa);
}
/// Reads the process's mappings again and loads the modules that are new.
diff --git a/src/trace/control.zig b/src/trace/control.zig
index 1bfad4c..c0a7906 100644
--- a/src/trace/control.zig
+++ b/src/trace/control.zig
@@ -9,6 +9,7 @@ pub const Reply = struct {
moments: u64,
pc: u64,
elapsed_ns: u64 = 0,
+ capture_ns: u64 = 0,
reason: []const u8 = "",
};