summaryrefslogtreecommitdiff
path: root/src/trace/Symbols.zig
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/Symbols.zig
parent5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (diff)
downloadcomputeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.tar.gz
computeranim-5c0230ea5a77f85de77ad4354788cd6c27aa1ef6.zip
debugger: follow natural V8 deoptimization with adaptive live views
Diffstat (limited to 'src/trace/Symbols.zig')
-rw-r--r--src/trace/Symbols.zig59
1 files changed, 59 insertions, 0 deletions
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.