summaryrefslogtreecommitdiff
path: root/src/trace
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/trace
parent5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (diff)
downloadcomputeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.tar.gz
computeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.zip
debugger: follow natural V8 deoptimization with adaptive live views
Diffstat (limited to 'src/trace')
-rw-r--r--src/trace/Debugger.zig22
-rw-r--r--src/trace/Symbols.zig59
-rw-r--r--src/trace/control.zig1
3 files changed, 75 insertions, 7 deletions
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 = "",
};