From e63b0624f57472d40af32a006c32cc47d403b822 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 18:06:30 -0300 Subject: canim: what the review found; the band as two panels; list and sort by instruction The first videos were reviewed frame by frame and the catalogue changed with them. A lens is a drawing over the program column and its view over the state column, so the stack keeps six slots, its window on the whole frame. Operands are whole. list and sort are recorded by instruction from main (record --from), so their registers strike, links reach nodes and cells, rows that ran are marked and jumps are drawn. A call's node is a projection of its return address; pins travel and keep their side; a mark covers the bytes that changed, at the element's width; a syscall's result is stored after the kernel returns it and every panel is of one moment; fast passes are veiled; the clock stands still during a step; captions get their reading time and a scene's first step comes by four seconds. Co-Authored-By: Claude Fable 5.2 --- anim/src/Recording.zig | 162 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 137 insertions(+), 25 deletions(-) (limited to 'anim/src/Recording.zig') diff --git a/anim/src/Recording.zig b/anim/src/Recording.zig index 92bb917..280d14a 100644 --- a/anim/src/Recording.zig +++ b/anim/src/Recording.zig @@ -75,6 +75,9 @@ pub const Branch = struct { to: u64, taken: bool, kind: enum(u8) { none, call, j /// the program stopped there. pub const CodeLine = struct { addr: u64, line: u32 }; +/// A variable on the stack, declared at a moment. +pub const Declared = struct { moment: u64, addr: u64 }; + /// Where a cursor's step may end. pub const Stop = enum { /// The next moment, whatever the recorder made one: an instruction, a line. @@ -113,9 +116,11 @@ calls: []Call = &.{}, syscalls: []Syscall = &.{}, marks: []Mark = &.{}, allocations: []Born = &.{}, -/// For each moment, a bit for each register of `Regs.names` it changed. -registers: []u32 = &.{}, +/// The registers of `tid` after each moment (the last known, before any). +registers: []zt.Regs = &.{}, write_spans: Spans = .{}, +/// What instructions stored, changed or not (a recording by instruction). +store_spans: Spans = .{}, read_spans: Spans = .{}, /// For each moment, what the instruction at its pc touches and where it branches. insn_touches: [][2]?Touch = &.{}, @@ -123,6 +128,11 @@ insn_branches: []?Branch = &.{}, /// Every instruction address the program stopped at in its own source, /// with that line, in address order. code_lines: []CodeLine = &.{}, +/// Each local variable's declaration, in the order they came: in a +/// recording that tells no calls, what says when a local began. +locals: []Declared = &.{}, +/// The variables declared outside the stack (globals): bytes a lens draws. +globals: []Span = &.{}, strings: std.AutoHashMapUnmanaged(u64, []const u8) = .empty, arena: std.heap.ArenaAllocator, @@ -237,14 +247,18 @@ fn index(r: *Recording) !void { var syscalls: std.ArrayList(Syscall) = .empty; var marks: std.ArrayList(Mark) = .empty; var allocations: std.ArrayList(Born) = .empty; - var registers: std.ArrayList(u32) = .empty; + var registers: std.ArrayList(zt.Regs) = .empty; var writes: std.ArrayList(Span) = .empty; var write_ends: std.ArrayList(u32) = .empty; + var stores: std.ArrayList(Span) = .empty; + var store_ends: std.ArrayList(u32) = .empty; var reads: std.ArrayList(Span) = .empty; var read_ends: std.ArrayList(u32) = .empty; var touch_list: std.ArrayList([2]?Touch) = .empty; + var locals: std.ArrayList(Declared) = .empty; + var globals: std.ArrayList(Span) = .empty; // What the instruction at a stop is about to read is read by the step after it. - var pending_reads: [2]?Touch = .{ null, null }; + var pending: [2]?Touch = .{ null, null }; var branch_list: std.ArrayList(?Branch) = .empty; var code_lines: std.AutoHashMapUnmanaged(u64, u32) = .empty; defer code_lines.deinit(gpa); @@ -274,6 +288,7 @@ fn index(r: *Recording) !void { var line_at: ?u64 = null; var line_file: ?u64 = null; var stepped = false; + var broke = false; var takes_nothing = false; // An instrumented program's own events are drained before the // debugger reports its stop: what follows its line is the debugger's. @@ -281,7 +296,7 @@ fn index(r: *Recording) !void { var touched: [2]?Touch = .{ null, null }; var branched: ?Branch = null; snapshots.clearRetainingCapacity(); - for (pending_reads) |p| if (p) |read_by| if (read_by.how & 1 != 0) try reads.append(arena, .{ .addr = read_by.addr, .len = read_by.len }); + for (pending) |p| if (p) |read_by| if (read_by.how & 1 != 0) try reads.append(arena, .{ .addr = read_by.addr, .len = read_by.len }); while (events.next()) |item| { const ev = item.ev; count += 1; @@ -315,14 +330,17 @@ fn index(r: *Recording) !void { try snapshots.append(gpa, !told or debugger_spoke); }, .mem_read => if (ev.a[1] > 0) try reads.append(arena, .{ .addr = ev.a[0], .len = ev.a[1] }), - .variable => if (open_calls.getLastOrNull()) |c| { + .variable => { + if ((zt.telescope.region(m, ev.a[0]) catch .none) == .stack) try locals.append(arena, .{ .moment = i, .addr = ev.a[0] }) else try globals.append(arena, .{ .addr = ev.a[0], .len = ev.a[1] }); // The first variable of a function that takes something is what it was given. - const takes = for (r.functions) |f| { - if (std.mem.eql(u8, f.name, r.name(calls.items[c].function))) break f.takes; - } else true; - if (takes and calls.items[c].parameter == null and ev.a[1] <= 8) { - calls.items[c].parameter = .{ .addr = ev.a[0], .len = ev.a[1] }; - calls.items[c].parameter_name = ev.a[2]; + if (open_calls.getLastOrNull()) |c| { + const takes = for (r.functions) |f| { + if (std.mem.eql(u8, f.name, r.name(calls.items[c].function))) break f.takes; + } else true; + if (takes and calls.items[c].parameter == null and ev.a[1] <= 8) { + calls.items[c].parameter = .{ .addr = ev.a[0], .len = ev.a[1] }; + calls.items[c].parameter_name = ev.a[2]; + } } }, .line => { @@ -331,7 +349,11 @@ fn index(r: *Recording) !void { // The pass counts its strings from 1; the debugger's ids are hashes with the top bit set. if (ev.a[0] >> 63 != 0) debugger_spoke = true; }, - .step, .breakpoint => stepped = true, + .step => stepped = true, + .breakpoint => { + stepped = true; + broke = true; + }, .touch => { const slot: usize = if (touched[0] == null) 0 else 1; if (touched[slot] == null) touched[slot] = .{ .addr = ev.a[0], .len = ev.a[1], .how = @intCast(ev.aux & 3) }; @@ -350,6 +372,7 @@ fn index(r: *Recording) !void { // stores: bytes seen for the first time were not written by this // step, unless it pushed them (the stack pointer went down a word). var kept = first_write; + var changed_by_store = false; for (writes.items[first_write..], snapshots.items) |span, snapshot| { var fresh = true; for (0..span.len) |b| { @@ -364,13 +387,27 @@ fn index(r: *Recording) !void { const pushed = regs != null and before != null and regs.?.rsp + 8 == before.?.rsp and span.addr <= regs.?.rsp and span.addr + span.len >= regs.?.rsp + 8; keep = if (pushed) .{ .addr = regs.?.rsp, .len = 8 } else null; } + // The store the instruction made says it to the element: below. + if (!told and stepped and stored(pending, span)) { + keep = null; + changed_by_store = true; + } if (keep) |span_kept| { writes.items[kept] = span_kept; kept += 1; } } writes.shrinkRetainingCapacity(kept); + // What the instruction at the stop before said it would store, it + // stored, the bytes it named: a write where the value changed (a + // debugger sees words change, the instruction says which element); + // a store all the same, even of the value that was there. + if (!told and stepped) for (pending) |p| if (p) |w| if (w.how & 2 != 0) { + try stores.append(arena, .{ .addr = w.addr, .len = w.len }); + if (changed_by_store) try writes.append(arena, .{ .addr = w.addr, .len = w.len }); + }; try write_ends.append(arena, @intCast(writes.items.len)); + try store_ends.append(arena, @intCast(stores.items.len)); try read_ends.append(arena, @intCast(reads.items.len)); const line = (try m.line(r.tid)) orelse store.Line{}; try lines.append(arena, line); @@ -378,10 +415,10 @@ fn index(r: *Recording) !void { try own.append(arena, if (ours) line.line else if (own.items.len > 0) own.items[own.items.len - 1] else 0); try reached.append(arena, ours and line_file != null); try touch_list.append(arena, touched); - pending_reads = touched; + pending = touched; try branch_list.append(arena, branched); if (ours and regs != null) try code_lines.put(gpa, regs.?.rip, line.line); - try registers.append(arena, if (regs != null and before != null) changedRegisters(before.?, regs.?) else 0); + try registers.append(arena, regs orelse before orelse .{}); if (!told and stepped and regs != null and before != null) { const now = regs.?; @@ -407,6 +444,19 @@ fn index(r: *Recording) !void { }; } } + // A recording that begins inside a call (`record --from`): the call + // it first stops in was made before it, and is open. + if (!told and broke and ours and calls.items.len == 0) if (regs) |now| { + var found: [2]zt.Debugger.Frame = undefined; + const frames = zt.telescope.frames(m, &now, &found) catch found[0..0]; + if (frames.len == 2 and frames[0].high > frames[0].low) { + const function = r.functionAt(line.line); + takes_nothing = function != null and !function.?.takes; + try calls.append(arena, .{ .function = try r.intern(if (function) |fun| fun.name else "?"), .parent = null, .depth = 0, .sp = frames[0].high - 8, .enter = i }); + try open_calls.append(gpa, 0); + try derived.append(gpa, frames[1].pc); + } + }; if (regs) |g| before = g; try depths.append(arena, @intCast(open_calls.items.len)); @@ -445,6 +495,7 @@ fn index(r: *Recording) !void { r.allocations = allocations.items; r.registers = registers.items; r.write_spans = .{ .all = writes.items, .ends = write_ends.items }; + r.store_spans = .{ .all = stores.items, .ends = store_ends.items }; r.insn_touches = touch_list.items; r.insn_branches = branch_list.items; r.code_lines = try arena.alloc(CodeLine, code_lines.count()); @@ -457,6 +508,8 @@ fn index(r: *Recording) !void { } }.less); r.read_spans = .{ .all = reads.items, .ends = read_ends.items }; + r.locals = locals.items; + r.globals = globals.items; } /// A string id for `text`, for names the recording did not define itself. @@ -471,6 +524,8 @@ pub const Function = struct { /// Whether it has parameters: `main(void)` has none; and how many. takes: bool, params: usize = 0, + /// Its first parameter's name ("" when it takes none). + param: []const u8 = "", /// Its declaration's first line (from 1) and last. first: u32, last: u32, @@ -490,7 +545,10 @@ pub fn outline(arena: std.mem.Allocator, source: []const u8, file: []const u8) ! if (from == paren) continue; const close = std.mem.indexOfScalarPos(u8, header, paren, ')') orelse header.len; const params = std.mem.trim(u8, header[paren + 1 .. close], " "); - try found.append(arena, .{ .name = header[from..paren], .takes = params.len > 0 and !std.mem.eql(u8, params, "void"), .params = if (params.len > 0 and !std.mem.eql(u8, params, "void")) std.mem.count(u8, params, ",") + 1 else 0, .first = @intCast(d.start_line + 1), .last = @intCast(d.end_line + 1) }); + const first_param = std.mem.trim(u8, params[0 .. std.mem.indexOfScalar(u8, params, ',') orelse params.len], " "); + var name_from = first_param.len; + while (name_from > 0 and (std.ascii.isAlphanumeric(first_param[name_from - 1]) or first_param[name_from - 1] == '_')) name_from -= 1; + try found.append(arena, .{ .name = header[from..paren], .takes = params.len > 0 and !std.mem.eql(u8, params, "void"), .params = if (params.len > 0 and !std.mem.eql(u8, params, "void")) std.mem.count(u8, params, ",") + 1 else 0, .param = first_param[name_from..], .first = @intCast(d.start_line + 1), .last = @intCast(d.end_line + 1) }); } return found.items; } @@ -531,8 +589,7 @@ pub fn moment(r: *const Recording, i: u64) Moment { /// Register `reg` of the thread shown at moment `i`; 0 when the recording /// has no registers there. pub fn register(r: *const Recording, i: u64, reg: []const u8) u64 { - const regs = (r.moment(i).regs(r.tid) catch null) orelse return 0; - return regs.named(reg) orelse 0; + return r.registers[@min(i, r.end())].named(reg) orelse 0; } /// A string id's text; "?" for one the recording never defined. @@ -572,7 +629,17 @@ pub fn resolve(r: *const Recording, scratch: std.mem.Allocator, t: types.Type) ? /// began: until then its bytes are whatever that stretch of stack held /// before, and a picture should not show them as its value. pub fn assigned(r: *const Recording, v: Variable, i: u64) bool { - if (v.depth == 0) return true; + if (v.depth == 0) { + // A recording that tells no calls: a local is its value once + // written after it was declared; a global always is. + var k = r.locals.len; + while (k > 0) { + k -= 1; + const d = r.locals[k]; + if (d.moment <= i and d.addr == v.addr) return touches(r.written(d.moment, i), v.addr, v.size) or touches(r.store_spans.of(d.moment, i), v.addr, v.size); + } + return true; + } var enter: u64 = 0; for (r.calls) |c| { if (c.enter > i) break; @@ -589,6 +656,32 @@ pub fn identity(r: *const Recording, m: Moment, addr: u64) ?usize { return null; } +/// What file descriptor `fd` is open on at moment `i`, as the `value` +/// class `fd` says it: the path the call that returned it was given, +/// quoted; the three a program starts with by name; "" when not known. +pub fn openOn(r: *const Recording, scratch: std.mem.Allocator, fd: u64, i: u64) []const u8 { + var on: []const u8 = switch (fd) { + 0 => "stdin", + 1 => "stdout", + 2 => "stderr", + else => "", + }; + const SYS = std.os.linux.SYS; + for (r.syscalls) |s| { + if (s.enter >= i) break; + switch (std.enums.fromInt(SYS, s.nr) orelse continue) { + .open, .openat, .creat => if (s.exit != null and s.exit.? <= i and s.result == fd) { + on = if (s.text.len > 0) std.fmt.allocPrint(scratch, "\"{s}\"", .{std.fs.path.basename(s.text)}) catch "" else ""; + }, + .close => if (s.args[0] == fd) { + on = ""; + }, + else => {}, + } + } + return on; +} + /// The calls open at moment `i`, outermost first, in `scratch`. /// shortcut: looks at every call made by then; a recording of a million calls wants each call's stack kept as a chain of parents. pub fn stack(r: *const Recording, scratch: std.mem.Allocator, i: u64) []const Call { @@ -676,13 +769,26 @@ pub fn read(r: *const Recording, from: u64, to: u64) []const Span { return r.read_spans.of(from, to); } -/// A bit for each register of `Regs.names` they changed. +/// A bit for each register of `Regs.names` that is not what it was: +/// a register a step changed and put back (a call's rsp) is not changed. pub fn registersChanged(r: *const Recording, from: u64, to: u64) u32 { - var mask: u32 = 0; - if (to < r.registers.len and from < to) for (r.registers[from + 1 .. to + 1]) |m| { - mask |= m; - }; - return mask; + if (to >= r.registers.len or from >= to) return 0; + return changedRegisters(r.registers[from], r.registers[to]); +} + +/// Whether bytes `span` are drawn somewhere for the step from `from` to +/// `to`: in the stack's frame round either stop's rsp and rbp, in an +/// allocation, or in a global (an array's cells). Elsewhere (the library's +/// own memory) a step's memory level has nothing to show. +pub fn drawn(r: *const Recording, span: Span, from: u64, to: u64) bool { + for ([_]u64{ from, to }) |i| { + const regs = r.registers[@min(i, r.end())]; + const low = regs.rsp -| 8; + const high = @max(regs.rbp, regs.rsp) + 16 + 8 * 8; + if (span.addr < high and span.addr + span.len > low) return true; + } + for (r.allocations) |a| if (span.addr < a.ptr + a.size and span.addr + span.len > a.ptr -| 16) return true; + return touches(r.globals, span.addr, span.len); } pub fn touches(list: []const Span, addr: u64, size: u64) bool { @@ -690,6 +796,12 @@ pub fn touches(list: []const Span, addr: u64, size: u64) bool { return false; } +/// Whether one of `by` (an instruction's touches) stores into `span`. +fn stored(by: [2]?Touch, span: Span) bool { + for (by) |p| if (p) |w| if (w.how & 2 != 0 and w.addr < span.addr + span.len and w.addr + w.len > span.addr) return true; + return false; +} + /// A bit for each register of `Regs.names` that differs between `a` and `b`. pub fn changedRegisters(a: zt.Regs, b: zt.Regs) u32 { var mask: u32 = 0; -- cgit v1.3