summaryrefslogtreecommitdiff
path: root/anim/src/Recording.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 14:43:13 -0300
committerGabriel Schneider <[email protected]>2026-11-11 16:17:33 -0300
commit3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6 (patch)
treef1bc91879a447f178dfaba39d0df107d01e557bd /anim/src/Recording.zig
parenta72831d5c322078260314c305ddd940e4967947d (diff)
downloadcomputeranim-3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6.tar.gz
computeranim-3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6.zip
canim: a minimap in each pane; source wherever it can be had, libc's included
From the user. Each pane with more than it shows carries a strip of its whole column at its right edge, the pane's window outlined and what happens off it marked there; the corner minimap is gone and the dive folds each column of the map into its pane's strip. The source panel follows the pc into any file the build has: glibc's own, from the distro's debuginfod by an optional step that caches them by build id (zig build sources; nothing else touches the network), and musl's, from Zig's lib directory. The syscalls scene shows open64.c on the way down, and a new scene follows the same call through a Zig-built musl. A graph's counts and inks are as of the moment shown. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/src/Recording.zig')
-rw-r--r--anim/src/Recording.zig104
1 files changed, 85 insertions, 19 deletions
diff --git a/anim/src/Recording.zig b/anim/src/Recording.zig
index a6e940c..359305c 100644
--- a/anim/src/Recording.zig
+++ b/anim/src/Recording.zig
@@ -101,6 +101,15 @@ source: []const u8,
/// Its functions, as pardes outlines them, and how many lines it has.
functions: []const Function = &.{},
line_count: u32 = 0,
+/// Every file the source panel can show: the program's own first (`file`,
+/// `source`), then those the build gave it (a library's, by the path its
+/// debug info names); and the most lines any of them has.
+sources: []const Source = &.{},
+most_lines: u32 = 0,
+/// Where the source panel stands after each moment: the line the pc is on
+/// where its file is one of `sources`, else the last such line (the
+/// deepest frame that has source, a frame up).
+shown: []Shown = &.{},
/// The thread shown: the first to make an event.
tid: u32 = 0,
/// The source line `tid` is on after each moment (line 0: none yet), in
@@ -142,17 +151,39 @@ const gpa = std.heap.smp_allocator;
/// `frame` runs every frame and asks again.
var opened: std.ArrayList(*Recording) = .empty;
+/// A file the source panel can show: its path as the debug info names it
+/// (the program's own, its bare name), its text and how many lines.
+pub const Source = struct {
+ path: []const u8,
+ text: []const u8,
+ lines: u32,
+
+ /// What its tag says: the file's name.
+ pub fn name(s: Source) []const u8 {
+ return std.fs.path.basename(s.path);
+ }
+};
+
+/// A line of `sources[src]`; `here`: the pc is on it (else a frame up is).
+pub const Shown = struct { src: u16 = 0, line: u32 = 0, here: bool = false };
+
/// A recording as the build embeds it in `recordings`: a `.ztrace` file's
-/// bytes (`trace`), the source it ran (`source`) and its name (`file`).
-/// Opened and indexed the first time.
+/// bytes (`trace`), the source it ran (`source`) and its name (`file`),
+/// and the other sources its lines name that the build found (`sources`:
+/// path and text). Opened and indexed the first time.
pub fn open(program: anytype) *Recording {
for (opened.items) |r| if (r.key == program.trace.ptr) return r;
- const r = load(program.trace, program.file, program.source) catch |err| std.debug.panic("canim: cannot open the recording of {s}: {t}", .{ program.file, err });
+ const more: []const [2][]const u8 = if (@hasField(@TypeOf(program), "sources")) program.sources else &.{};
+ const r = load(program.trace, program.file, program.source, more) catch |err| std.debug.panic("canim: cannot open the recording of {s}: {t}", .{ program.file, err });
opened.append(gpa, r) catch @panic("canim: out of memory");
return r;
}
-fn load(bytes: []const u8, file: []const u8, source: []const u8) !*Recording {
+fn lineCount(text: []const u8) u32 {
+ return @intCast(std.mem.count(u8, text, "\n") + @intFromBool(text.len > 0 and text[text.len - 1] != '\n'));
+}
+
+fn load(bytes: []const u8, file: []const u8, source: []const u8, more: []const [2][]const u8) !*Recording {
// The trace reads a file: give it one in memory.
const linux = std.os.linux;
const rc = linux.memfd_create("canim", linux.MFD.CLOEXEC);
@@ -173,7 +204,12 @@ fn load(bytes: []const u8, file: []const u8, source: []const u8) !*Recording {
r.* = .{ .trace = try .open(gpa, std.Io.Threaded.global_single_threaded.io(), path), .key = bytes.ptr, .file = file, .source = source, .arena = .init(gpa) };
if (r.trace.len == 0) return error.EmptyRecording;
r.functions = try outline(r.arena.allocator(), source, file);
- r.line_count = @intCast(std.mem.count(u8, source, "\n") + @intFromBool(source.len > 0 and source[source.len - 1] != '\n'));
+ r.line_count = lineCount(source);
+ const sources = try r.arena.allocator().alloc(Source, 1 + more.len);
+ sources[0] = .{ .path = file, .text = source, .lines = r.line_count };
+ for (more, sources[1..]) |pair, *s| s.* = .{ .path = pair[0], .text = pair[1], .lines = lineCount(pair[1]) };
+ r.sources = sources;
+ for (sources) |s| r.most_lines = @max(r.most_lines, s.lines);
try r.index();
return r;
}
@@ -184,13 +220,32 @@ fn load(bytes: []const u8, file: []const u8, source: []const u8) !*Recording {
/// its function is (0: not known); `line`, the line of the program's own
/// source it was compiled from (null: another file's, or not known);
/// `params`, a direct call's callee's parameter count.
-pub fn instruction(r: *const Recording, i: u64, addr: u64, buf: []u8) ?struct { len: u8, back: u8, text: []const u8, line: ?u32, params: ?u8 } {
+pub fn instruction(r: *const Recording, i: u64, addr: u64, buf: []u8) ?struct { len: u8, back: u8, text: []const u8, line: ?u32, src: u16 = 0, params: ?u8 } {
const found = (zt.telescope.instruction(r.moment(i), addr, buf) catch return null) orelse return null;
- var line: ?u32 = null;
- if (found.line) |l| if (std.mem.eql(u8, std.fs.path.basename(r.name(l.file)), r.file)) {
- line = l.line;
- };
- return .{ .len = found.len, .back = found.back, .text = found.text, .line = line orelse r.lineOf(addr), .params = found.params };
+ if (found.line) |l| if (r.sourceOf(l.file)) |k| return .{ .len = found.len, .back = found.back, .text = found.text, .line = l.line, .src = k, .params = found.params };
+ return .{ .len = found.len, .back = found.back, .text = found.text, .line = r.lineOf(addr), .params = found.params };
+}
+
+/// The source a line's file (a string id) is, of `sources`: null where the recording has none.
+pub fn sourceOf(r: *const Recording, file: u64) ?u16 {
+ const path = r.name(file);
+ if (std.mem.eql(u8, std.fs.path.basename(path), r.file)) return 0;
+ for (r.sources, 0..) |s, k| if (k > 0 and std.mem.eql(u8, s.path, path)) return @intCast(k);
+ return null;
+}
+
+/// Where the code at `addr` is, as the debug info has it at moment `i`:
+/// its file's name and line, whatever the file (a library's too, its
+/// source in hand or not); at moment `i`'s own pc, the moment's line.
+pub fn where(r: *const Recording, i: u64, addr: u64) ?struct { file: []const u8, line: u32 } {
+ var buf: [256]u8 = undefined;
+ if (addr == r.register(i, "rip")) {
+ const l = r.lines[@min(i, r.end())];
+ if (l.line != 0) return .{ .file = std.fs.path.basename(r.name(l.file)), .line = l.line };
+ }
+ const found = (zt.telescope.instruction(r.moment(i), addr, &buf) catch null) orelse return null;
+ if (found.line) |l| return .{ .file = std.fs.path.basename(r.name(l.file)), .line = l.line };
+ return if (r.lineOf(addr)) |l| .{ .file = r.file, .line = l } else null;
}
/// The source line the instruction at `addr` was compiled from, for a
@@ -218,8 +273,9 @@ pub fn ownLine(r: *const Recording, i: u64) ?u32 {
return l.line;
}
-/// Whether the pc at moment `i` is in the program's own source: not in a
-/// library's code, a PLT stub, the vDSO (where `own` keeps the last line).
+/// Whether the pc at moment `i` is on a line the source panel can show (the
+/// program's own, or a library's whose source the build gave): not in a
+/// PLT stub, the vDSO, a library without source (where `shown` keeps the last line).
pub fn sourced(r: *const Recording, i: u64) bool {
var buf: [256]u8 = undefined;
const here = r.instruction(i, r.register(i, "rip"), &buf) orelse return r.ownLine(i) != null;
@@ -227,14 +283,20 @@ pub fn sourced(r: *const Recording, i: u64) bool {
}
/// The first line a source panel of `fit` lines shows when the program is
-/// on `line`: the whole file when it fits; else a window that has the line
-/// in its middle third, and is the same window for every line of that
-/// third, so that the text moves only when the program has moved on.
-pub fn viewFirst(r: *const Recording, fit: u32, line: u32) u32 {
- if (r.line_count <= fit or fit < 3) return 1;
+/// on `line` of a file of `lines`: the whole file when it fits; else a
+/// window that has the line in its middle third, and is the same window
+/// for every line of that third, so that the text moves only when the
+/// program has moved on.
+pub fn viewFirst(fit: u32, lines: u32, line: u32) u32 {
+ if (lines <= fit or fit < 3) return 1;
const third = fit / 3;
const band = (@max(line, 1) - 1) / third;
- return std.math.clamp(@as(u32, @intCast(@max(@as(i64, band) - 1, 0))) * third + 1, 1, r.line_count - fit + 1);
+ return std.math.clamp(@as(u32, @intCast(@max(@as(i64, band) - 1, 0))) * third + 1, 1, lines - fit + 1);
+}
+
+/// The first line of `shown`'s file a source panel of `fit` lines shows.
+pub fn shownFirst(r: *const Recording, fit: u32, shown: Shown) u32 {
+ return viewFirst(fit, r.sources[shown.src].lines, shown.line);
}
/// Runs of bytes, a list for each moment.
@@ -257,6 +319,7 @@ fn index(r: *Recording) !void {
const arena = r.arena.allocator();
var lines: std.ArrayList(store.Line) = .empty;
var own: std.ArrayList(u32) = .empty;
+ var shown: std.ArrayList(Shown) = .empty;
var reached: std.ArrayList(bool) = .empty;
var depths: std.ArrayList(u32) = .empty;
var calls: std.ArrayList(Call) = .empty;
@@ -429,6 +492,8 @@ fn index(r: *Recording) !void {
try lines.append(arena, line);
const ours = line.line != 0 and std.mem.eql(u8, std.fs.path.basename(r.name(line.file)), r.file);
try own.append(arena, if (ours) line.line else if (own.items.len > 0) own.items[own.items.len - 1] else 0);
+ const src = if (line.line != 0) r.sourceOf(line.file) else null;
+ try shown.append(arena, if (src) |k| .{ .src = k, .line = line.line, .here = true } else if (shown.items.len > 0) .{ .src = shown.items[shown.items.len - 1].src, .line = shown.items[shown.items.len - 1].line } else .{});
try reached.append(arena, ours and line_file != null);
try touch_list.append(arena, touched);
pending = touched;
@@ -509,6 +574,7 @@ fn index(r: *Recording) !void {
};
r.lines = lines.items;
r.own = own.items;
+ r.shown = shown.items;
r.reached = reached.items;
r.depths = depths.items;
r.calls = calls.items;