diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 16:05:20 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 16:43:58 -0300 |
| commit | 165bb6c1344b98de7bef1d3c2b72147b4a40797a (patch) | |
| tree | 0e70e6222700063f604f78673f6ab3a5bfb9242d /anim/src | |
| parent | 66a2a7cf733f6abc1f79561c52d901ce67019fb1 (diff) | |
| download | computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.tar.gz computeranim-165bb6c1344b98de7bef1d3c2b72147b4a40797a.zip | |
canim: the renderer speaks the design language
The blocks, their states and the tokens carry the catalogue's names:
atoms in ui, chain/reg/slot in rows, insn with its line column, jump lane
and annotations (read, write, taken or not, a call with its arguments, a
return, a syscall) in disasm, bytes and hexdump with field paper and the
reading column, the three splits and relayout in stage, the ties (thread,
link and pin, projection, echo). A lens is a band over the whole context,
its drawing left and its bytes right. Frames' spans on the stack, each
instruction's line and a callee's parameter count come from the tracer.
A gallery scene draws every block; the test compares six of its pages
with pictures kept in test/gallery, drawn from a recording kept there
too. The scenes are ported; the motion checks report no pop and no
metronome in any of them.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'anim/src')
| -rw-r--r-- | anim/src/Recording.zig | 89 | ||||
| -rw-r--r-- | anim/src/array.zig | 94 | ||||
| -rw-r--r-- | anim/src/calls.zig | 12 | ||||
| -rw-r--r-- | anim/src/context.zig | 600 | ||||
| -rw-r--r-- | anim/src/cursor.zig | 40 | ||||
| -rw-r--r-- | anim/src/disasm.zig | 419 | ||||
| -rw-r--r-- | anim/src/heap.zig | 105 | ||||
| -rw-r--r-- | anim/src/hexdump.zig | 233 | ||||
| -rw-r--r-- | anim/src/memory.zig | 444 | ||||
| -rw-r--r-- | anim/src/nodes.zig | 330 | ||||
| -rw-r--r-- | anim/src/registers.zig | 117 | ||||
| -rw-r--r-- | anim/src/root.zig | 97 | ||||
| -rw-r--r-- | anim/src/rows.zig | 367 | ||||
| -rw-r--r-- | anim/src/source.zig | 108 | ||||
| -rw-r--r-- | anim/src/stage.zig | 84 | ||||
| -rw-r--r-- | anim/src/syscalls.zig | 51 | ||||
| -rw-r--r-- | anim/src/theme.zig | 419 | ||||
| -rw-r--r-- | anim/src/ties.zig | 283 | ||||
| -rw-r--r-- | anim/src/ui.zig | 508 | ||||
| -rw-r--r-- | anim/src/words.zig | 19 |
20 files changed, 2398 insertions, 2021 deletions
diff --git a/anim/src/Recording.zig b/anim/src/Recording.zig index aeaacf9..92bb917 100644 --- a/anim/src/Recording.zig +++ b/anim/src/Recording.zig @@ -63,6 +63,18 @@ pub const Born = struct { ptr: u64, size: u64, moment: u64, event: u64 }; /// A run of bytes a moment changed, or read. pub const Span = struct { addr: u64, len: u64 }; +/// What the instruction at a moment's pc is about to do to memory (the +/// debugger's `touch`): `how` 1 reads there, 2 writes there, 3 both. +pub const Touch = struct { addr: u64, len: u64, how: u8 }; + +/// Where the instruction at a moment's pc goes, if it is a call, a jump +/// or a return (the debugger's `branch`), and whether it will. +pub const Branch = struct { to: u64, taken: bool, kind: enum(u8) { none, call, jump, ret } }; + +/// The source line an instruction was compiled from, as the recording saw +/// the program stopped there. +pub const CodeLine = struct { addr: u64, line: u32 }; + /// Where a cursor's step may end. pub const Stop = enum { /// The next moment, whatever the recorder made one: an instruction, a line. @@ -105,6 +117,12 @@ allocations: []Born = &.{}, registers: []u32 = &.{}, write_spans: Spans = .{}, read_spans: Spans = .{}, +/// For each moment, what the instruction at its pc touches and where it branches. +insn_touches: [][2]?Touch = &.{}, +insn_branches: []?Branch = &.{}, +/// Every instruction address the program stopped at in its own source, +/// with that line, in address order. +code_lines: []CodeLine = &.{}, strings: std.AutoHashMapUnmanaged(u64, []const u8) = .empty, arena: std.heap.ArenaAllocator, @@ -151,11 +169,35 @@ fn load(bytes: []const u8, file: []const u8, source: []const u8) !*Recording { } /// The instruction at `addr` as the debugger disassembled it at moment -/// `i` (`text` is Intel syntax, in `buf`); null where it recorded none. -/// `back` is how long the instruction before it in its function is (0: not known). -pub fn instruction(r: *const Recording, i: u64, addr: u64, buf: []u8) ?struct { len: u8, back: u8, text: []const u8 } { +/// `i` (`text` is Intel syntax, in `buf`: give it 212 bytes or more); null +/// where it recorded none. `back` is how long the instruction before it in +/// 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 } { const found = (zt.telescope.instruction(r.moment(i), addr, buf) catch return null) orelse return null; - return .{ .len = found.len, .back = found.back, .text = found.text }; + 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 }; +} + +/// The source line the instruction at `addr` was compiled from, for a +/// recording whose instructions do not carry it (`instruction` gives it +/// where they do): where the program stopped there, the line it was on; +/// else the line of the nearest instruction before it that it stopped at, +/// close by. Null where nothing is known. +pub fn lineOf(r: *const Recording, addr: u64) ?u32 { + var lo: usize = 0; + var hi: usize = r.code_lines.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (r.code_lines[mid].addr <= addr) lo = mid + 1 else hi = mid; + } + if (lo == 0) return null; + const near = r.code_lines[lo - 1]; + return if (addr - near.addr <= 96) near.line else null; } /// The first line a source panel of `fit` lines shows when the program is @@ -200,6 +242,12 @@ fn index(r: *Recording) !void { var write_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; + // 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 branch_list: std.ArrayList(?Branch) = .empty; + var code_lines: std.AutoHashMapUnmanaged(u64, u32) = .empty; + defer code_lines.deinit(gpa); var open_calls: std.ArrayList(u32) = .empty; defer open_calls.deinit(gpa); // A recording of instructions tells no calls: they are read off the @@ -230,7 +278,10 @@ fn index(r: *Recording) !void { // An instrumented program's own events are drained before the // debugger reports its stop: what follows its line is the debugger's. var debugger_spoke = false; + 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 }); while (events.next()) |item| { const ev = item.ev; count += 1; @@ -281,6 +332,16 @@ fn index(r: *Recording) !void { if (ev.a[0] >> 63 != 0) debugger_spoke = true; }, .step, .breakpoint => stepped = 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) }; + }, + .branch => branched = .{ .to = ev.a[0], .taken = ev.a[1] != 0, .kind = switch (ev.aux) { + 1 => .call, + 2 => .jump, + 3 => .ret, + else => .none, + } }, else => {}, } } @@ -316,6 +377,10 @@ fn index(r: *Recording) !void { 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); try reached.append(arena, ours and line_file != null); + try touch_list.append(arena, touched); + pending_reads = 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); if (!told and stepped and regs != null and before != null) { @@ -380,6 +445,17 @@ fn index(r: *Recording) !void { r.allocations = allocations.items; r.registers = registers.items; r.write_spans = .{ .all = writes.items, .ends = write_ends.items }; + r.insn_touches = touch_list.items; + r.insn_branches = branch_list.items; + r.code_lines = try arena.alloc(CodeLine, code_lines.count()); + var it = code_lines.iterator(); + var n: usize = 0; + while (it.next()) |e| : (n += 1) r.code_lines[n] = .{ .addr = e.key_ptr.*, .line = e.value_ptr.* }; + std.mem.sort(CodeLine, r.code_lines, {}, struct { + fn less(_: void, a: CodeLine, b: CodeLine) bool { + return a.addr < b.addr; + } + }.less); r.read_spans = .{ .all = reads.items, .ends = read_ends.items }; } @@ -392,8 +468,9 @@ fn intern(r: *Recording, text: []const u8) !u64 { pub const Function = struct { name: []const u8, - /// Whether it has parameters: `main(void)` has none. + /// Whether it has parameters: `main(void)` has none; and how many. takes: bool, + params: usize = 0, /// Its declaration's first line (from 1) and last. first: u32, last: u32, @@ -413,7 +490,7 @@ 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"), .first = @intCast(d.start_line + 1), .last = @intCast(d.end_line + 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, .first = @intCast(d.start_line + 1), .last = @intCast(d.end_line + 1) }); } return found.items; } diff --git a/anim/src/array.zig b/anim/src/array.zig index 5d6b471..b4472e7 100644 --- a/anim/src/array.zig +++ b/anim/src/array.zig @@ -13,6 +13,10 @@ const ui = @import("ui.zig"); const cursors = @import("cursor.zig"); const Recording = @import("Recording.zig"); const objects = @import("objects.zig"); +const context = @import("context.zig"); +const hexdump = @import("hexdump.zig"); +const ties = @import("ties.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; @@ -26,7 +30,7 @@ fn flightTime(units: f32) f32 { pub const Array = struct { cursor: cursors.Cursor, - box: Rect, + stage: *const Stage, /// The array's name in the program. variable: []const u8, /// Variables that index it, shown as tags in the cell they point at. @@ -44,31 +48,42 @@ pub const Array = struct { }; pub fn draw(g: *const Array, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; + const box = g.stage.box(at, .band); + if (box.h < theme.metric.context.tag + 24) return; + const u = context.at(f, at, box); const now = g.cursor.now(at); const rec = g.cursor.rec; const moment = now.shown(.idea); const v = rec.variable(f.scratch, moment, g.variable) orelse { - _ = u.panel(g.box.w, g.box.h, g.variable, .{ .focus = g.focus }); + _ = u.panel(box.w, box.h, g.variable, .{ .focus = g.focus }); return; }; const kind = rec.typeOf(f.scratch, v.type); - // Its declaration, and where it is: the address the context's pointers to it end in. - const body = u.panel(g.box.w, g.box.h, g.variable, .{ .focus = g.focus, .note = u.print("{s} at \u{2026}{x:0>4}", .{ objects.declare(f.scratch, rec, v), v.addr & 0xffff }) }); + // Its declaration in the tag, and in the right half its bytes. + _ = u.panel(box.w, box.h, objects.declare(f.scratch, rec, v), .{ .focus = g.focus }); + const m = theme.metric.context; + const half = theme.layout.state.x - theme.layout.program.x; // An array of numbers; anything else is read as 4-byte ones. const elem: u64 = if (kind != null and kind.? == .array) kind.?.array.elem.size() orelse 4 else 4; const signed = kind == null or kind.? != .array or kind.?.array.elem.* != .scalar or kind.?.array.elem.scalar.kind == .int; const count: usize = @intCast(@min(v.size / elem, 32)); if (count == 0) return; + { + const first = v.addr & ~@as(u64, 15); + const rows_n = @min(theme.layout.rowsIn(box.h), (v.addr + v.size - first + 15) / 16); + u.text(.sans_medium, theme.size.sans.tag, half + 12, m.tag - 6, "its bytes", t.primary); + u.text(.sans, theme.size.sans.micro, half + 12 + u.width(.sans_medium, theme.size.sans.tag, "its bytes") + 12, m.tag - 6, u.print("{x} to {x}, an element a run", .{ v.addr, v.addr + v.size - 1 }), t.faint); + hexdump.draw(u, now, half, m.tag + m.pad, first, rows_n, .{ .array = .{ .addr = v.addr, .len = v.size, .elem = @intCast(elem) }, .region = context.ink(context.regionOf(moment, v.addr)) }); + } - const room = body.w - 2 * theme.metric.panel.pad_x; - const hero = @as(f32, @floatFromInt(count)) * theme.metric.hero_cell[0] <= room; - // Beside the context there is less room: the cells are as wide as fit. - const compact = !hero and @as(f32, @floatFromInt(count)) * theme.metric.cell[0] > room; - const cell: [2]f32 = if (hero) theme.metric.hero_cell else if (compact) .{ @floor(room / @as(f32, @floatFromInt(count))), theme.metric.cell[1] } else theme.metric.cell; - const size: f32 = if (hero) theme.size.mono.hero else if (compact) theme.size.mono.data else theme.size.mono.value; - const left = (body.w - @as(f32, @floatFromInt(count)) * cell[0]) / 2; - const top = body.y + (body.h - cell[1]) / 2 - @as(f32, if (compact) 30 else 0); + // The cells, in the left half: a size up as the subject. + const subject = box.h >= theme.layout.split.subject.band.h - 1; + const compact = !subject; + const cell: [2]f32 = if (subject) .{ 96, 72 } else .{ 80, 44 }; + const size: f32 = if (subject) theme.size.mono.flight else theme.size.mono.small; + const label: f32 = if (subject) theme.size.mono.small else theme.size.mono.context; + const left = (half - 32 - @as(f32, @floatFromInt(count)) * cell[0]) / 2; + const top = m.tag + @as(f32, if (subject) 44 else 16) + label * 1.2 + 4; const walking = now.walking(); // What moved: a cell written with what another cell held, which this step read. @@ -102,38 +117,42 @@ pub const Array = struct { var heat = now.written(addr, elem, .idea, delay); // The value on its way is the anticipation: no frame as well. if (flown != null) heat.arm = 0; - const ink = u.written(r, heat, theme.color.raised); + const ink = u.cellWritten(r, heat, theme.color.raised); // Staging: the compare is seen (framed as read) before anything moves. - if (now.wasRead(addr, elem) and now.writeAge(addr, elem) != 0) u.readMark(r, now.reading(.idea, 0)); - u.text(.mono, if (compact) theme.size.mono.context else theme.size.mono.small, r.x + @as(f32, if (compact) 6 else 14), r.y + @as(f32, if (compact) 24 else 34), u.print("{d}", .{i}), if (ink.face == .mono_bold) ink.ink else ui.rgb(t.faint)); + if (now.wasRead(addr, elem) and now.writeAge(addr, elem) != 0) u.cellRead(r, now.reading(.idea, 0)); + // Its index over it, where a node has its address: the projection lands there. + const index_text = u.print("{d}", .{i}); + const ly = r.y - 4 - label * 1.2; + u.text(.mono, label, r.x + (r.w - @as(f32, @floatFromInt(index_text.len)) * label * 3 / 5) / 2, ly + label * 0.96, index_text, t.faint); + ties.anchor(u, .{ .node_address = addr }, .{ .x = r.x, .y = ly, .w = r.w, .h = label * 1.2 }); + ties.anchor(u, .{ .node_part = addr }, r); // Until its new value lands a cell keeps the old one, faint once // the new is in the air: a copy does not erase. const shown = if (pending) old[i] else values[i]; - const digits: Rect = .{ .x = r.x, .y = r.y + 18, .w = r.w, .h = r.h - 18 }; + const digits = r; const leaving = pending and flown != null and (!walking or now.since(.idea, delay) >= -(flightTime(cell[0]) + fl.lift)); if (shown) |n| u.label(ink.face, size, digits, .center, u.print("{d}", .{n}), if (leaving) ui.rgb(t.faint) else ink.ink) else u.unknown(digits); } - // Index variables: tags that slide from cell to cell as the idea strikes. - for (g.indices, 0..) |name, nth| { + // Index variables: pins under the cell they index, sliding from + // cell to cell as the idea strikes; two in one cell share its pin. + var at_cell: [32]std.ArrayList([]const u8) = @splat(.empty); + var slide: [32]?f32 = @splat(null); + for (g.indices) |name| { const iv = rec.variable(f.scratch, now.moment(), name) orelse continue; const here = number(now.moment(), iv.addr, @min(iv.size, 8), true) orelse continue; - var x = left + (@as(f32, @floatFromInt(here)) + 1) * cell[0]; - if (number(now.before(), iv.addr, @min(iv.size, 8), true)) |was| { - x = std.math.lerp(left + (@as(f32, @floatFromInt(was)) + 1) * cell[0], x, cursors.standard(now.moving(.idea, 0, time.move))); - } if (here < 0 or here >= count) continue; - // Under the tags of the indices before it that are in the same cell. - var slot: f32 = 0; - for (g.indices[0..nth]) |other| { - const ov = rec.variable(f.scratch, moment, other) orelse continue; - if (number(moment, ov.addr, @min(ov.size, 8), true) == here) slot += 1; - } - const w = u.width(.mono_bold, theme.size.mono.small, name) + 16; - // In a narrow cell the tags hang under it, not in it. - const tag: Rect = if (compact) .{ .x = x - cell[0] / 2 - w / 2, .y = top + cell[1] + 8 + slot * 38, .w = w, .h = 34 } else .{ .x = x - 2 - 10 - w, .y = top + 10 + slot * 38, .w = w, .h = 34 }; - u.fill(tag, t.ink); - u.label(.mono_bold, theme.size.mono.small, tag, .center, name, t.primary); + const k: usize = @intCast(here); + at_cell[k].append(f.scratch, name) catch {}; + if (number(now.before(), iv.addr, @min(iv.size, 8), true)) |was| if (was != here and was >= 0 and was < count) { + slide[k] = @floatFromInt(was); + }; + } + for (at_cell, 0..) |names, k| { + if (names.items.len == 0) continue; + var x = left + (@as(f32, @floatFromInt(k)) + 0.5) * cell[0]; + if (slide[k]) |was| x = std.math.lerp(left + (was + 0.5) * cell[0], x, cursors.standard(now.moving(.idea, 0, time.move))); + u.pinUnder(x, top + cell[1], names.items, t.primary, label); } // Values in flight, over everything: one over, the next under. @@ -164,12 +183,13 @@ pub const Array = struct { const way: f32 = if (k % 2 == 0) -1 else 1; const c = sk.math.mix(from, to, eased) + ui.Vec{ 0, way * rise * @sin(std.math.pi * eased) }; const grown = std.math.lerp(1, fl.lift_scale, lifted); - const w = (if (compact) cell[0] + 4 else @max(theme.metric.flight_card[0], cell[0] * 0.6)) * grown; - const h = (if (compact) theme.metric.flight_card[1] - 8 else theme.metric.flight_card[1]) * grown; + const w = (cell[0] + 4) * grown; + const h = (cell[1] + 4) * grown; const card: Rect = .{ .x = c[0] - w / 2, .y = c[1] - h / 2, .w = w, .h = h }; u.fill(card, theme.color.page); u.frame(card, t.bright, theme.stroke.line); - if (values[flight.to]) |n| u.label(.mono_bold, if (compact) theme.size.mono.value else theme.size.mono.flight, card, .center, u.print("{d}", .{n}), t.bright); + if (values[flight.to]) |n| u.label(.mono_bold, size, card, .center, u.print("{d}", .{n}), t.bright); + _ = compact; } } }; diff --git a/anim/src/calls.zig b/anim/src/calls.zig index af0893d..88bca71 100644 --- a/anim/src/calls.zig +++ b/anim/src/calls.zig @@ -11,6 +11,8 @@ const ui = @import("ui.zig"); const cursors = @import("cursor.zig"); const Cursor = cursors.Cursor; const Recording = @import("Recording.zig"); +const context = @import("context.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; @@ -22,13 +24,15 @@ const badge_after = 0.20; pub const Tree = struct { cursor: Cursor, - box: Rect, + stage: *const Stage, /// The function whose calls are the tree. function: []const u8, focus: bool = false, pub fn draw(k: *const Tree, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; + const box = k.stage.box(at, .band); + if (box.h < theme.metric.context.tag + 24) return; + const u = context.at(f, at, box); const now = k.cursor.now(at); const rec = k.cursor.rec; const m = struct { @@ -36,7 +40,7 @@ pub const Tree = struct { const level = 44; const badge = 24; }; - const body = u.panel(k.box.w, k.box.h, u.print("calls of {s}", .{k.function}), .{ .focus = k.focus, .note = "each call a node \u{00b7} its result on the shoulder \u{00b7} the frames are the stack's" }); + const body = u.panel(box.w, box.h, u.print("calls of {s}", .{k.function}), .{ .focus = k.focus, .note = "each call a node \u{00b7} its result on the shoulder \u{00b7} the frames are the stack's" }); const nodes = layout(f.scratch, rec, k.function) catch return; if (nodes.len == 0) return; var leaves: f32 = 0; @@ -95,7 +99,7 @@ pub const Tree = struct { // Done: what it returned, on its shoulder; struck as it lands. const badge: Rect = .{ .x = cx + 10, .y = cy - m.radius - 12, .w = m.badge, .h = m.badge }; const heat: ui.Heat = if (n.call.exit.? > now.stops[1]) now.heat(0, null, since - badge_after) else .none; - const ink = u.written(badge, heat, theme.color.page); + const ink = u.cellWritten(badge, heat, theme.color.page); if (heat.level < 1) u.frame(badge, t.faint, theme.stroke.rule); u.label(ink.face, size, badge, .center, u.print("{d}", .{@as(i64, @bitCast(n.call.result))}), if (heat.level >= 1) ink.ink else ui.rgb(t.primary)); } diff --git a/anim/src/context.zig b/anim/src/context.zig index 8d59112..092d76a 100644 --- a/anim/src/context.zig +++ b/anim/src/context.zig @@ -1,116 +1,66 @@ -//! The debugger's context, as pwndbg shows it, and always on screen: every -//! register followed as far as it points (`Registers`), the top of the -//! stack as a telescope (`Stack`), the call chain (`Backtrace`), and the -//! instructions at the pc (`Disasm`). An address's ink is where it points; -//! a chain reads "this, which points to that, which holds this"; code is -//! named and strings are quoted. A scene's lens sits beside this; it never -//! replaces it. -//! -//! Ink says what a thing is, paper what happened to it: a register written -//! is armed (a thin frame), struck (solid behind the value, its digits -//! bold in ink), and then keeps a tick at its left for three steps; a -//! value read is framed. The pc's row is the current mark of the CPU's -//! level: it moves on last, with the source's. +//! The state column (design/README.md, "3. Panels"): `registers`, all +//! seventeen, each followed as far as it points; `stack`, the words from +//! rsp with the frames they belong to as `call` spans down its right edge +//! and the registers that point at a word as `pin`s in its lane; and +//! `backtrace`, the call chain as rows, a panel of its own only in the +//! `none` split. Ink says what a thing is, paper what happened to it. //! Reads `Moment.regs`, `Moment.read`, the mappings (through -//! `zt.telescope`), and the names and instructions the debugger recorded. +//! `zt.telescope`), and the names the debugger recorded. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const zt = @import("ztrace"); const ui = @import("ui.zig"); +const rows = @import("rows.zig"); const cursors = @import("cursor.zig"); const Recording = @import("Recording.zig"); const syscalls = @import("syscalls.zig"); const ties = @import("ties.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; const t = theme.color.text; const m = theme.metric.context; -const size = theme.size.mono.context; -const adv = size * 3 / 5; -/// What an address into `region` is inked in. -pub fn ink(region: zt.telescope.Region) u24 { +pub const ink = rows.ink; +pub const regionOf = rows.regionOf; + +/// The registers' tag: the legend of what an address's ink says, always on screen, as pwndbg's is. +pub const legend = blk: { const c = theme.color.region; - return switch (region) { - .none => c.number, - .code, .rwx => c.code_, - .stack => c.stack, - .heap => c.heap, - .data => c.data, - .rodata => c.rodata, + break :blk [_]ui.Legend{ + .{ .text = "stack", .ink = ui.rgb(c.stack) }, + .{ .text = "heap", .ink = ui.rgb(c.heap) }, + .{ .text = "code", .ink = ui.rgb(c.code_) }, + .{ .text = "data", .ink = ui.rgb(c.data) }, + .{ .text = "rwx", .ink = ui.rgb(c.data), .under = true }, + .{ .text = "read-only", .ink = ui.rgb(c.rodata) }, }; -} +}; -pub fn regionOf(moment: Recording.Moment, value: u64) zt.telescope.Region { - return zt.telescope.region(moment, value) catch .none; +/// A panel's `Ui`, moved to where the stage has its box now. +pub fn at(f: *sk.Frame, placed: anim.Placed, box: Rect) ui.Ui { + return (ui.Ui{ .f = f, .at = placed }).moved(box.x, box.y); } -/// Where a mark goes behind a value drawn from `x` on the row at `y`. -pub fn cell(x: f32, y: f32, moment: Recording.Moment, value: u64) Rect { - return .{ .x = x - 3, .y = y + 2, .w = ui.Value.of(value, regionOf(moment, value) != .none).width() + 6, .h = m.row - 4 }; +pub fn veiled(u: ui.Ui, box: Rect, amount: f32) void { + // Staging: what cannot be read at a sprint is dimmed, not removed. + u.veil(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - theme.stroke.rule }, amount); } -/// `value` followed as far as it points at `moment`, along the row at `y` -/// from `x`: an address in its region's ink; an arrow to what it points -/// to; an arrow back before what the chain holds at its end (a number, by -/// its class; a string, quoted); a code address's name after it. `first` -/// is the ink of the first link while a strike is under it. A chain that -/// runs out of row, or was still a pointer where the recording stopped -/// following, ends in an arrow and dots: nothing is taken from its middle. -pub fn chain(u: ui.Ui, x: f32, y: f32, moment: Recording.Moment, value: u64, max_x: f32, first: ?ui.Ink) void { - var links_buf: [6]zt.telescope.Link = undefined; - var text_buf: [48]u8 = undefined; - const links = zt.telescope.chase(moment, value, &links_buf, &text_buf) catch return; - const base = theme.baseline(y, m.row, size); - const mid = y + m.row / 2; - var at = x; - for (links, 0..) |link, i| { - const pointer = link.region != .none; - const w = ui.Value.of(link.value, pointer).width(); - // Room for it, and for saying that there is more. - const more: f32 = if (i + 1 < links.len or link.text.len > 0) m.arrow_w + 3 * adv else 0; - if (i > 0 and at + m.arrow_w + w + more > max_x) return dots(u, at, mid, base); - if (i > 0) { - u.chainArrow(at, mid, !pointer); - at += m.arrow_w; - } - const end = u.value(at, base, link.value, pointer, if (i == 0 and first != null) first.? else .{ .ink = ui.rgb(ink(link.region)) }); - // Writable and executable: worth a look of its own. - if (link.region == .rwx) u.fill(.{ .x = at, .y = base + 3, .w = w, .h = theme.stroke.rule }, ink(.rwx)); - at = end; - if (link.text.len > 0) { - const fits: usize = @intFromFloat(@max((max_x - at - @as(f32, if (link.string) m.arrow_w else 10)) / adv, 0)); - if (link.string) { - // What a pointer leads to, when it is text: quoted, cut to the room there is. - if (fits < m.string_min + 2) return; - u.chainArrow(at, mid, true); - const whole = link.text.len + 2 <= fits; - u.text(.mono, size, at + m.arrow_w, base, if (whole) u.print("\"{s}\"", .{link.text}) else u.print("\"{s}\u{2026}", .{link.text[0 .. fits - 2]}), theme.color.string); - } else if (fits >= 3) u.text(.mono, size, at + 10, base, u.print("{s}", .{link.text[0..@min(link.text.len, fits)]}), t.primary); - return; - } - // Still a pointer where the recording stopped following. - if (i + 1 == links.len and pointer and link.region != .code and link.region != .rwx and at + m.arrow_w + 3 * adv <= max_x) return dots(u, at, mid, base); - } +/// The rows a panel `box` shows: while a relayout moves it, one more, cut by its edge. +pub fn rowsOf(box: Rect) usize { + return @intFromFloat(@max(@ceil((box.h - m.tag - 2 * m.pad) / m.row), 0)); } -fn dots(u: ui.Ui, x: f32, mid: f32, base: f32) void { - u.chainArrow(x, mid, false); - u.text(.mono, size, x + m.arrow_w, base, "\u{2026}", t.faint); -} - -fn veiled(u: ui.Ui, body: Rect, amount: f32) void { - // Staging: what cannot be read at a sprint is dimmed, not removed. - u.veil(.{ .x = theme.stroke.rule, .y = body.y, .w = body.w - 2 * theme.stroke.rule, .h = body.h - theme.stroke.rule }, amount); +pub fn clipBody(u: ui.Ui, box: Rect) void { + u.clip(.{ .x = theme.stroke.rule, .y = m.tag + m.pad - 1, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - 2 * m.pad + 2 }); } pub const Registers = struct { cursor: cursors.Cursor, - box: Rect = theme.layout.registers, - /// The rows, in order: none is hidden and they never reorder. - names: []const []const u8 = &theme.metric.all_registers, + stage: *const Stage, /// A step that makes a system call frames, as read, the registers /// that carry it, one at a time; and rax is struck when it returns. syscalls: bool = false, @@ -121,9 +71,10 @@ pub const Registers = struct { const Shown = struct { regs: zt.Regs, moment: Recording.Moment }; - pub fn draw(r: *const Registers, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = r.cursor.now(at); + pub fn draw(r: *const Registers, f: *sk.Frame, placed: anim.Placed) void { + const box = r.stage.box(placed, .registers); + const u = at(f, placed, box); + const now = r.cursor.now(placed); const rec = r.cursor.rec; var is: Shown = .{ .regs = (now.moment().regs(rec.tid) catch null) orelse zt.Regs{}, .moment = now.moment() }; var was: Shown = .{ .regs = (now.before().regs(rec.tid) catch null) orelse is.regs, .moment = now.before() }; @@ -133,14 +84,22 @@ pub const Registers = struct { if (syscalls.latest(rec, now.stops[0])) |n| is = shownAt(rec, rec.syscalls[n].enter) orelse is; if (syscalls.latest(rec, now.stops[1])) |n| was = shownAt(rec, rec.syscalls[n].enter) orelse was; } - const body = u.panel(r.box.w, r.box.h, "registers", .{ .focus = r.focus, .legend = true }); - // A link from a register leaves from the panel's edge, not through its name. - ties.anchor(u, .{ .register = 1 << 20 }, .{ .x = 0, .y = 0, .w = r.box.w, .h = r.box.h }); - const room: usize = @intFromFloat(@floor((body.h - 2 * m.pad) / m.row)); - // Overlapping action: of the registers one step wrote, each strikes a little after the one above it. - var struck: f32 = 0; - for (r.names[0..@min(r.names.len, room)], 0..) |reg, i| r.row(u, now, is, was, reg, body.y + m.pad + @as(f32, @floatFromInt(i)) * m.row, &struck); - veiled(u, body, now.veil); + const body = u.panel(box.w, box.h, "registers", .{ .focus = r.focus, .legend = &legend }); + // A link leaves from the panel's edge, not through a name. + ties.anchor(u, .registers, .{ .x = 0, .y = 0, .w = box.w, .h = box.h }); + clipBody(u, box); + defer u.unclip(); + const shown = @min(rowsOf(box), theme.metric.all_registers.len); + // Overlapping action: of the registers one step wrote, each strikes 2 f after the one above it. + for (theme.metric.all_registers[0..shown], 0..) |name, i| { + const y = body.y + m.pad + @as(f32, @floatFromInt(i)) * m.row; + if (std.mem.eql(u8, name, "rip")) { + r.rip(u, now, y, box.w, is, was); + continue; + } + r.row(u, now, is, was, name, y, box.w); + } + veiled(u, box, now.veil); } fn shownAt(rec: *const Recording, i: u64) ?Shown { @@ -148,338 +107,229 @@ pub const Registers = struct { return .{ .regs = (moment.regs(rec.tid) catch return null) orelse return null, .moment = moment }; } - fn row(r: *const Registers, u: ui.Ui, now: cursors.Now, is: Shown, was: Shown, reg: []const u8, y: f32, struck: *f32) void { + /// rip: where execution is, a current mark like the source's and the + /// disassembly's; it moves with them, at the advance. + fn rip(r: *const Registers, u: ui.Ui, now: cursors.Now, y: f32, w: f32, is: Shown, was: Shown) void { const rec = r.cursor.rec; - const base = theme.baseline(y, m.row, size); - const max_x = r.box.w - m.side; - if (std.mem.eql(u8, reg, "rip")) { - // Never written: where execution is, a current mark like the - // source's and the disassembly's. It follows them: its value - // changes as the mark moves on, a few frames behind. - const current = theme.mark(.current); - u.fill(.{ .x = theme.stroke.rule, .y = y, .w = r.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); - u.fill(.{ .x = theme.stroke.rule, .y = y, .w = m.name_w - 2, .h = m.row }, current.solid); - u.text(.mono_bold, size, m.side, base, reg, current.on_solid); - const at = now.marks[if (now.advancingAfter(2 * time.stagger) >= 0.5) 1 else 0]; - return chain(u, m.chain_x, y, rec.moment(at), rec.register(at, "rip"), max_x, null); - } - u.text(.mono, size, m.side, base, reg, t.dim); + const moment = now.marks[if (now.advancing() >= 0.5) 1 else 0]; + const landed = now.landed(.cpu, 0); + const flags = (if (landed) is else was).regs.eflags; + // The flags a step changed are ticked, for three steps. + const eflags_index = comptime index("eflags").?; + // Until this step's change lands, the ticks are the last one's: no effect before its cause. + const age = if (landed) now.registerAge(eflags_index) else now.registerAgeFrom(eflags_index, 1); + var changed: u64 = 0; + if (age) |a| changed = rec.register(now.stops[a], "eflags") ^ rec.register(now.stops[a + 1], "eflags"); + const held = if (r.quiet) 0 else now.register(eflags_index, 0).level; + rows.rip(u, y, w, rec.moment(moment), rec.register(moment, "rip"), flags, changed, held, 0); + } + + fn row(r: *const Registers, u: ui.Ui, now: cursors.Now, is: Shown, was: Shown, name: []const u8, y: f32, w: f32) void { + const i = index(name) orelse return; + const value_now = is.regs.named(name) orelse return; + const value_was = was.regs.named(name) orelse value_now; + var marks: rows.Marks = .{}; var landed = now.landed(.cpu, 0); - if (std.mem.eql(u8, reg, "flags")) { - // The value, then the eight names: a set flag upper case and - // bright on a chip, a clear one lower case and faint. - const flags = (if (landed) is else was).regs.eflags; - var x = u.value(m.chain_x + adv, base, flags, false, .{}) + 2 * adv; - inline for (.{ .{ "cf", "CF", 0 }, .{ "pf", "PF", 2 }, .{ "af", "AF", 4 }, .{ "zf", "ZF", 6 }, .{ "sf", "SF", 7 }, .{ "if", "IF", 9 }, .{ "df", "DF", 10 }, .{ "of", "OF", 11 } }) |flag| { - const set = flags >> flag[2] & 1 != 0; - if (set) u.fill(.{ .x = x - 3, .y = y + 2, .w = 2 * adv + 6, .h = m.row - 4 }, theme.color.raised); - u.text(if (set) .mono_bold else .mono, size, x, base, if (set) flag[1] else flag[0], if (set) t.bright else t.faint); - // Secondary action: the tick of one this step changed. It only fades. - if (!r.quiet and landed and set != (was.regs.eflags >> flag[2] & 1 != 0)) u.faded(now.since(.cpu, 0) / time.quick).fill(.{ .x = x - m.tick - 5, .y = y + 3, .w = m.tick, .h = m.row - 6 }, theme.mark(.write).solid); - x += 4 * adv; - } - return; - } - const index = for (zt.Regs.names, 0..) |known, i| { - if (std.mem.eql(u8, known, reg)) break i; - } else return; - const value_now = is.regs.named(reg) orelse return; - const value_was = was.regs.named(reg) orelse value_now; - var mark: ?ui.Ink = null; if (if (r.syscalls) syscalls.inStep(now) else null) |made| { const plan = syscalls.Plan.of(made); - const is_rax = std.mem.eql(u8, reg, "rax"); - const carrier: ?usize = if (is_rax) 0 else for (syscalls.carried(made), 1..) |name, k| { - if (std.mem.eql(u8, name, reg)) break k; + const is_rax = std.mem.eql(u8, name, "rax"); + const carrier: ?usize = if (is_rax) 0 else for (syscalls.carried(made), 1..) |carried, k| { + if (std.mem.eql(u8, carried, name)) break k; } else null; // What carries the call is framed as read, one register at a // time: they are what crosses the line. const framed = now.time - plan.registers - @as(f32, @floatFromInt(carrier orelse 0)) * time.flight.second; - const read = @min(std.math.clamp(framed / time.read.in, 0, 1), now.reading(.cpu, 0)); landed = framed >= 0; + if (carrier != null) { + marks.read = 0; + marks.read_shows = @min(std.math.clamp(framed / time.read.in, 0, 1), now.reading(.cpu, 0)); + } if (is_rax) { // rax: the call's number going in, the result coming back. const back = now.time - plan.returns; const result = back >= time.strike.in / 2.0; const value: u64 = if (result) value_now else if (landed) made.nr else value_was; - const shown = if (result) is.moment else was.moment; - if (back < -time.strike.arm) u.readFrame(cell(m.chain_x, y, shown, value), read) else mark = u.struck(cell(m.chain_x, y, shown, value), now.heat(0, null, back)); - return chain(u, m.chain_x, y, shown, value, max_x, mark); + if (back >= -time.strike.arm) { + marks = .{ .first = now.heat(0, null, back) }; + if (back >= 0 and now.walking()) marks.extend = back; + } + const hops = rows.reg(u, y, w, name, if (result) is.moment else was.moment, value, marks); + return anchor(u, i, hops, y); } - if (carrier != null) u.readFrame(cell(m.chain_x, y, if (landed) is.moment else was.moment, if (landed) value_now else value_was), read); } else if (r.quiet) { // Unmarked: the values change as the mark moves on. landed = now.advancing() >= 0.5; } else { - const age = now.registerAge(index); - const delay = if (age == 0) @min(struck.* * time.stagger, time.stagger_max) else 0; - if (age == 0) struck.* += 1; + const age = now.registerAge(i); + const delay = if (age == 0) ties.strikeDelay(now, i) else 0; landed = now.landed(.cpu, delay); - mark = u.struck(cell(m.chain_x, y, if (landed) is.moment else was.moment, if (landed) value_now else value_was), now.register(index, delay)); + marks.first = now.register(i, delay); + // A register written: the rest of its chain is followed again. + const since = now.since(.cpu, delay); + if (age == 0 and now.walking() and since >= 0) marks.extend = since; } - ties.anchor(u, .{ .register = index }, cell(m.chain_x, y, if (landed) is.moment else was.moment, if (landed) value_now else value_was)); - chain(u, m.chain_x, y, if (landed) is.moment else was.moment, if (landed) value_now else value_was, max_x, mark); + const hops = rows.reg(u, y, w, name, if (landed) is.moment else was.moment, if (landed) value_now else value_was, marks); + anchor(u, i, hops, y); + } + + fn anchor(u: ui.Ui, i: usize, hops: rows.Hops, y: f32) void { + if (hops.n > 0) ties.anchor(u, .{ .register = i }, .{ .x = hops.x[0], .y = y, .w = hops.w[0], .h = m.row }); } }; -/// The top of the stack: eight words from the stack pointer up, low -/// addresses on top, each followed as far as it points. In the lane at the -/// left a register's plaque sits at the row it points at. The window is -/// anchored at `rsp`: when a step moves it the rows slide (what persists -/// slides), and the words that come into view are the new frame's. +/// A register's place in `Regs.names`. +pub fn index(name: []const u8) ?usize { + for (zt.Regs.names, 0..) |known, i| if (std.mem.eql(u8, known, name)) return i; + return null; +} + +/// One frame of the call chain, on the stack: its function's pc, and the +/// slots that are the frame, from `lo` to `hi` (the last; null: its end is +/// not known, past what the recording holds). +pub const Frame = struct { pc: u64, lo: u64, hi: ?u64 }; + +/// The frames at moment `i`, newest first, with their extents, as the +/// tracer reads them off the frame-pointer chain. +pub fn frames(rec: *const Recording, i: u64, out: []Frame) []Frame { + const moment = rec.moment(i); + const regs = (moment.regs(rec.tid) catch return out[0..0]) orelse return out[0..0]; + var buf: [24]zt.Debugger.Frame = undefined; + const found = zt.telescope.frames(moment, ®s, buf[0..@min(buf.len, out.len)]) catch return out[0..0]; + for (found, out[0..found.len]) |f, *o| o.* = .{ .pc = f.pc, .lo = f.low, .hi = if (f.high > f.low) f.high - 8 else null }; + return out[0..found.len]; +} + +/// A frame's name: its function's (no offset), or its module's and offset where there is no symbol. +pub fn frameName(u: ui.Ui, rec: *const Recording, pc: u64) []const u8 { + const name = rec.strings.get(pc) orelse return u.print("{x}", .{pc}); + if (std.mem.indexOfScalar(u8, name, '!') != null) return ui.symbol(u, name); + return name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len]; +} + +/// The registers (of the sixteen) that hold `addr` in `regs`. +fn pointing(u: ui.Ui, regs: zt.Regs, addr: u64) []const []const u8 { + var out: std.ArrayList([]const u8) = .empty; + for (theme.metric.all_registers[0..16]) |name| { + if (regs.named(name) == addr) out.append(u.f.scratch, name) catch break; + } + return out.items; +} + pub const Stack = struct { cursor: cursors.Cursor, - box: Rect = theme.layout.stack, + stage: *const Stage, focus: bool = false, - /// The lane: two plaques side by side. - const lane = 104; - - pub fn draw(k: *const Stack, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = k.cursor.now(at); + pub fn draw(k: *const Stack, f: *sk.Frame, placed: anim.Placed) void { + const box = k.stage.box(placed, .stack); + const u = at(f, placed, box); + const now = k.cursor.now(placed); const rec = k.cursor.rec; - const body = u.panel(k.box.w, k.box.h, "stack", .{ .focus = k.focus, .note = "words from rsp \u{00b7} low addresses on top" }); + const slots = rowsOf(box); + const body = u.panel(box.w, box.h, "stack", .{ .focus = k.focus, .note = u.print("{s} words from rsp \u{00b7} the frames they belong to", .{switch (slots) { + 4 => "four", + 8 => "eight", + else => u.print("{d}", .{slots}), + }}) }); const is = (now.moment().regs(rec.tid) catch null) orelse return; const was = (now.before().regs(rec.tid) catch null) orelse is; - const calls = rec.stack(f.scratch, now.stop(.cpu)); - // Rows it has moved by, still to slide: the stack pointer is the CPU's, and moves with it. - const p = cursors.glide(now.moving(.cpu, 0, time.move)); + // The window is rsp's: when a step moves it the rows slide, with the + // stack's words, a level after the registers (one onset at a time). + const landed = now.landed(.memory, 0); + const p = cursors.glide(now.moving(.memory, 0, time.move)); const moved = @as(f32, @floatFromInt(@as(i64, @bitCast(is.rsp -% was.rsp)))) / 8; const slide: f32 = if (@abs(moved) > 16) 0 else moved * (1 - p); const top = body.y + m.pad; - const room: i64 = @intFromFloat(@floor((body.h - 2 * m.pad) / m.row)); - u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = k.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); + clipBody(u, box); + defer u.unclip(); + // The addresses' shared digits: the rows read by their ends. + const lead = ui.shared(&.{ is.rsp, is.rsp + 8 * @as(u64, slots) }); + const memory = now.shown(.memory); var n: i64 = @as(i64, @intFromFloat(@floor(-slide))) - 1; - while (n <= room - @as(i64, @intFromFloat(@floor(slide)))) : (n += 1) { + while (n <= @as(i64, @intCast(slots)) - @as(i64, @intFromFloat(@floor(slide)))) : (n += 1) { const y = top + (@as(f32, @floatFromInt(n)) + slide) * m.row; - if (y + m.row < body.y or y > body.y + body.h) continue; + if (y + m.row < body.y or y > box.h) continue; const addr = is.rsp +% @as(u64, @bitCast(n * 8)); - const base = theme.baseline(y, m.row, size); - const end = u.value(lane, base, addr, true, .{ .ink = ui.rgb(theme.color.region.stack) }); - // The word there: memory, struck a beat after the registers. - const landed = now.landed(.memory, 0); - const moment = if (landed) now.moment() else now.before(); - const held = (Recording.word(moment, addr, 8) catch null) orelse { - u.unknown(.{ .x = end + m.arrow_w, .y = y, .w = 3 * adv, .h = m.row }); - continue; - }; - const value_x = end + m.arrow_w; - ties.anchor(u, .{ .word = addr }, .{ .x = Stack.lane - 4, .y = y + 1, .w = k.box.w - Stack.lane - 4, .h = m.row - 2 }); - const mark = u.struck(cell(value_x, y, moment, held), now.written(addr, 8, .memory, 0)); - if (now.wasRead(addr, 8) and now.writeAge(addr, 8) != 0) u.readFrame(cell(value_x, y, moment, held), now.reading(.memory, 0)); - // What the frame pointer says a word is, at the row's end. - var note: []const u8 = ""; - for (calls) |call| { - if (call.sp == addr) note = "return address"; - if (call.sp == addr +% 8 and addr == (if (now.landed(.cpu, 0)) is else was).rbp) note = u.print("{s}'s saved rbp", .{rec.name(call.function)}); + // Who points here: a pin in the lane, landing with its link. + const names = pins(u, now, is, was, addr); + var marks: rows.Marks = .{ .lead = lead, .first = now.written(addr, 8, .memory, 0) }; + if (now.wasRead(addr, 8) and now.writeAge(addr, 8) != 0) { + marks.read = 0; + marks.read_shows = now.reading(.memory, 0); } - const note_w = u.width(.sans, theme.size.sans.micro, note); - u.text(.sans, theme.size.sans.micro, k.box.w - m.side - note_w, base, note, t.faint); - u.chainArrow(end, y + m.row / 2, regionOf(moment, held) == .none); - chain(u, value_x, y, moment, held, k.box.w - m.side - (if (note.len > 0) note_w + 12 else 0), mark); - } - // The lane: who points at a row. A plaque slides to the row its - // register now points at, as the register is struck. - inline for (.{ "rsp", "rbp" }, 0..) |reg, i| { - const rows = [2]f32{ - @as(f32, @floatFromInt(@as(i64, @bitCast(@field(was, reg) -% was.rsp)))) / 8, - @as(f32, @floatFromInt(@as(i64, @bitCast(@field(is, reg) -% is.rsp)))) / 8, - }; - const visible = [2]bool{ rows[0] >= 0 and rows[0] < @as(f32, @floatFromInt(room)), rows[1] >= 0 and rows[1] < @as(f32, @floatFromInt(room)) }; - if (visible[0] or visible[1]) { - const at_row = if (visible[0] and visible[1]) std.math.lerp(rows[0], rows[1], p) else if (visible[1]) rows[1] else rows[0]; - const v = if (!visible[0]) u.faded(p) else if (!visible[1]) u.faded(1 - p) else u; - const plaque: Rect = .{ .x = 8 + @as(f32, @floatFromInt(i)) * 46, .y = top + at_row * m.row + 2, .w = 44, .h = m.row - 4 }; - v.fill(plaque, theme.color.page); - v.frame(plaque, t.primary, theme.stroke.rule); - v.label(.mono, size, plaque, .center, reg, t.primary); + const hops = rows.slot(u, y, box.w, memory, addr, names.names, names.shows, marks); + // Only a row on show is a place a tie can land. + if (y >= top - 1 and y + m.row <= box.h - m.pad + 1) { + ties.anchor(u, .{ .word = addr }, .{ .x = theme.metric.slot.chain, .y = y, .w = if (hops.n > 0) hops.x[0] + hops.w[0] - theme.metric.slot.chain else 120, .h = m.row }); + ties.anchor(u, .{ .slot_pin = addr }, .{ .x = theme.metric.slot.lane, .y = y, .w = theme.metric.slot.lane_end - theme.metric.slot.lane, .h = m.row }); } } - u.unclip(); - veiled(u, body, now.veil); + // The backtrace, drawn on the words that are each frame: it changes with the CPU. + var buf: [16]Frame = undefined; + const chain = frames(rec, if (landed) now.stops[0] else now.stops[1], &buf); + // Placed as the rows are: against rsp after the step, and slid. + const rsp = is.rsp; + for (chain, 0..) |frame, i| { + const first = @as(f32, @floatFromInt(@as(i64, @bitCast(frame.lo -% rsp)))) / 8 + slide; + if (first >= @as(f32, @floatFromInt(slots))) break; + const last = if (frame.hi) |hi| @as(f32, @floatFromInt(@as(i64, @bitCast(hi -% rsp)))) / 8 + slide else std.math.inf(f32); + const open = last >= @as(f32, @floatFromInt(slots)); + const y1 = top + (@min(last, @as(f32, @floatFromInt(slots)) - 1) + 1) * m.row; + rows.span(u, box.w, top + @max(first, 0) * m.row, y1, frameName(u, rec, frame.pc), i == 0, open); + } + veiled(u, box, now.veil); } -}; -/// The call chain, a row a frame, newest on top, walked by frame pointer -/// from what the debugger recorded: `#0` on a gold plaque, the function -/// and the offset in it, and for the program's own frame the file and -/// line. Three rows; a deeper chain is counted in the tag. It is the idea -/// of the stack, and changes last: a call's row arrives after the others -/// have made room; a return's leaves, and then they close up. -pub const Backtrace = struct { - cursor: cursors.Cursor, - box: Rect = theme.layout.backtrace, - focus: bool = false, + const Pins = struct { names: []const []const u8, shows: f32 }; - pub fn draw(b: *const Backtrace, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = b.cursor.now(at); - const rec = b.cursor.rec; - var buf: [2][24]u64 = undefined; - const before = b.frames(now.stops[1], &buf[0]); - const after = b.frames(now.stops[0], &buf[1]); - const deep = if (now.landed(.idea, 0)) after.len else before.len; - const body = u.panel(b.box.w, b.box.h, "backtrace", .{ .focus = b.focus, .note = if (deep > 3) u.print("newest on top \u{00b7} {d} frames", .{deep}) else "newest on top" }); - const top = body.y + m.pad; - const room: f32 = @floor((body.h - 2 * m.pad) / m.row); - u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = b.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); - defer u.unclip(); - // A call: the rows make room, then the new one fades in. A return: - // its row fades out, then the rest close up. - const called = after.len > before.len; - const d: f32 = @floatFromInt(@max(after.len, before.len) - @min(after.len, before.len)); - const makes_room = cursors.standard(now.moving(.idea, time.quick, time.cascade)); - const arrives = now.moving(.idea, time.snap, time.quick); - const closes = cursors.standard(std.math.clamp(now.moving(.idea, -time.quick, time.cascade), 0, 1)); - const shown = if (called or now.moving(.idea, 0, time.quick) >= 1) after else before; - const leaving = !called and shown.ptr == before.ptr; - const current = theme.mark(.current); - for (shown, 0..) |pc, i| { - const new = called and @as(f32, @floatFromInt(i)) < d; - const gone = leaving and @as(f32, @floatFromInt(i)) < d; - // Where it is: among the frames after the step, or (a row on its way out) before it. - const place = if (called) @as(f32, @floatFromInt(i)) - (1 - makes_room) * d else if (leaving) @as(f32, @floatFromInt(i)) else @as(f32, @floatFromInt(i)) + (1 - closes) * d; - if (place >= room or (new and arrives <= 0)) continue; - const y = top + @max(place, if (new) @as(f32, @floatFromInt(i)) else place) * m.row; - const v = if (new) u.faded(arrives) else if (gone) u.faded(1 - now.moving(.idea, 0, time.quick)) else u; - const base = theme.baseline(y, m.row, size); - // Gold is the newest frame's, once it has arrived. - const newest = if (called and arrives < 1) @as(f32, @floatFromInt(i)) == d else i == 0; - if (newest) { - v.fill(.{ .x = theme.stroke.rule, .y = y, .w = b.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); - v.fill(.{ .x = theme.stroke.rule, .y = y, .w = 44, .h = m.row }, current.solid); - } - v.text(if (newest) .mono_bold else .mono, size, m.side, base, u.print("#{d}", .{i}), if (newest) current.on_solid else t.faint); - const name = rec.strings.get(pc) orelse u.print("{x}", .{pc}); - v.text(.mono, size, m.side + 4 * adv, base, name[0..@min(name.len, 22)], t.primary); - // The program's own frame says where in its source it is. - if (i == 0) v.text(.mono, size, m.side + 22 * adv, base, u.print("{s}:{d}", .{ rec.file, rec.own[now.marked()] }), t.dim); + /// The registers whose value is `addr`: before the step, and after it + /// once their link has landed (`ties.pinShows`). + fn pins(u: ui.Ui, now: cursors.Now, is: zt.Regs, was: zt.Regs, addr: u64) Pins { + var out: std.ArrayList([]const u8) = .empty; + var shows: f32 = 0; + for (theme.metric.all_registers[0..16]) |name| { + const i = index(name).?; + const before = was.named(name) == addr; + const after = is.named(name) == addr; + if (!before and !after) continue; + const s = ties.pinShows(now, i, .slot); + const this = if (before and after) 1 else if (after) s.new else s.old; + if (this < 0.5) continue; + out.append(u.f.scratch, name) catch break; + shows = @max(shows, this); } - veiled(u, body, now.veil); - } - - /// The pcs of the call chain at moment `i`: the pc, then each return address outwards. - fn frames(b: *const Backtrace, i: u64, out: []u64) []const u64 { - const rec = b.cursor.rec; - const moment = rec.moment(i); - const regs = (moment.regs(rec.tid) catch return &.{}) orelse return &.{}; - return zt.telescope.backtrace(moment, ®s, out) catch &.{}; + return .{ .names = out.items, .shows = shows }; } }; -/// The instructions round the pc, as the debugger disassembled them, in -/// address order: as many above it as under it. The pc's row is the current mark of the CPU's level and stays -/// where it is: when execution moves on to an instruction in view the -/// rows slide up under it; when it goes elsewhere they give way to the -/// rows there. -pub const Disasm = struct { +pub const Backtrace = struct { cursor: cursors.Cursor, - box: Rect, + stage: *const Stage, focus: bool = false, - const Line = struct { addr: u64, text: []const u8 }; - - pub fn draw(d: *const Disasm, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = d.cursor.now(at); - const rec = d.cursor.rec; - // It follows the source's mark, a stagger behind. - const going = now.advancingAfter(time.stagger); - const pc = [2]u64{ rec.register(now.marks[0], "rip"), rec.register(now.marks[1], "rip") }; - // Where it is: the function's name, in the tag. - const name = rec.strings.get(pc[if (going >= 0.5) 1 else 0]) orelse ""; - const body = u.panel(d.box.w, d.box.h, "disassembly", .{ .focus = d.focus, .note = name[0 .. std.mem.indexOfScalar(u8, name, '+') orelse name.len] }); - const room: usize = @intFromFloat(@floor((body.h - 2 * m.pad) / m.row)); - // As many rows above the pc as under it: what just ran, and what is to come. - const trail = (room -| 1) / 2; - const from = d.listing(u, now.marks[0], room, trail); - const to = d.listing(u, now.marks[1], room, trail); - // The instruction it goes to is in view under the pc: the rows slide up to it. - const ahead: usize = for (from[@min(trail, from.len)..], 0..) |line, i| { - if (i > 0 and line.addr == pc[1]) break i; - } else 0; - const top = body.y + m.pad; - const current = theme.mark(.current); - const out = now.fires(.disasm); - const mark_y = top + @as(f32, @floatFromInt(trail)) * m.row; - u.fill(.{ .x = theme.stroke.rule, .y = mark_y, .w = d.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); - u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = d.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); - if (ahead > 0) { - d.rows(u, to, top + (1 - cursors.standard(going)) * @as(f32, @floatFromInt(ahead)) * m.row, pc[1], going >= 0.5); - } else if (pc[0] == pc[1] or going >= 1) { - d.rows(u, to, top, pc[1], true); - } else { - // Elsewhere: what leaves has left before what replaces it arrives. - d.rows(u.faded(@abs(2 * going - 1)), if (going >= 0.5) to else from, top, pc[if (going >= 0.5) 1 else 0], true); - } - u.unclip(); - // The marker, on its plaque: the one thing here that does not move. - u.fill(.{ .x = theme.stroke.rule - out, .y = mark_y - out, .w = 26 + 2 * out, .h = m.row + 2 * out }, current.solid); - sk.draw.polygon(u.f, &.{ .{ 10, mark_y + 6 }, .{ 20, mark_y + m.row / 2 }, .{ 10, mark_y + m.row - 6 } }, u.at.pen(.{ .fill = ui.rgb(current.on_solid) })); - ties.anchor(u, .pc, .{ .x = 0, .y = mark_y, .w = 26, .h = m.row }); - veiled(u, body, now.veil); - } - - fn rows(d: *const Disasm, u: ui.Ui, lines: []const Line, top: f32, pc: u64, lit: bool) void { - const rec = d.cursor.rec; - // An offset in its function, to match rip and the backtrace: the pc's, and from it the others'. - const name = rec.strings.get(pc) orelse ""; - const plus = std.mem.lastIndexOfScalar(u8, name, '+'); - const pc_offset: ?i64 = if (plus) |at| std.fmt.parseInt(i64, name[at + 1 ..], 10) catch null else null; - for (lines, 0..) |line, i| { - if (line.text.len == 0) continue; - const y = top + @as(f32, @floatFromInt(i)) * m.row; - const base = theme.baseline(y, m.row, size); - const on = lit and line.addr == pc; - const end = u.value(34, base, line.addr, true, .{ .ink = ui.rgb(theme.color.region.code_) }); - if (pc_offset) |offset| { - const here = offset + @as(i64, @bitCast(line.addr -% pc)); - if (here >= 0) u.text(.mono, size, end + adv, base, u.print("+{d}", .{here}), t.faint); - } - // The mnemonic, as a keyword; then what it works on. - const space = std.mem.indexOfScalar(u8, line.text, ' ') orelse line.text.len; - // After a twelve-digit address and its offset. - const text_x = 34 + 18 * adv; - const fits: usize = @intFromFloat(@max((d.box.w - m.side - text_x) / adv, 0)); - u.text(.mono_bold, size, text_x, base, line.text[0..@min(space, fits)], if (on) t.bright else theme.color.code.keyword); - if (space < @min(line.text.len, fits)) u.text(.mono, size, text_x + @max(@as(f32, @floatFromInt(space)), 5) * adv, base, line.text[space..@min(line.text.len, fits)], if (on) t.bright else theme.color.code.plain); - } - } - - /// The rows at moment `i`, in address order as pwndbg lists them: up - /// to `trail` instructions above the pc (each as long as the recording - /// says the one before is), the one at the pc, and those after it. - /// Rows above that the recording does not know are left empty: the - /// pc's row keeps its place. - fn listing(d: *const Disasm, u: ui.Ui, i: u64, room: usize, trail: usize) []const Line { - const rec = d.cursor.rec; - const lines = u.f.scratch.alloc(Line, @max(room, trail + 1)) catch return &.{}; - const pc = rec.register(i, "rip"); - var buf: [96]u8 = undefined; - var n = trail; - var addr = pc; - @memset(lines[0..trail], .{ .addr = 0, .text = "" }); - while (n > 0) { - const here = rec.instruction(i, addr, &buf) orelse break; - if (here.back == 0) break; - addr -= here.back; - const above = rec.instruction(i, addr, &buf) orelse break; - n -= 1; - lines[n] = .{ .addr = addr, .text = u.print("{s}", .{above.text}) }; - } - n = trail; - addr = pc; - while (n < lines.len) : (n += 1) { - const one = rec.instruction(i, addr, &buf) orelse break; - lines[n] = .{ .addr = addr, .text = u.print("{s}", .{one.text}) }; - addr += one.len; + pub fn draw(b: *const Backtrace, f: *sk.Frame, placed: anim.Placed) void { + const box = b.stage.box(placed, .backtrace); + if (box.h < m.tag) return; + const u = at(f, placed, box); + const now = b.cursor.now(placed); + const rec = b.cursor.rec; + var buf: [24]Frame = undefined; + const chain = frames(rec, now.stop(.cpu), &buf); + const room = rowsOf(box); + const body = u.panel(box.w, box.h, "backtrace", .{ .focus = b.focus, .note = if (chain.len > room) u.print("newest on top \u{00b7} {d} more", .{chain.len - room}) else "newest on top" }); + clipBody(u, box); + defer u.unclip(); + for (chain[0..@min(chain.len, room)], 0..) |frame, i| { + const y = body.y + m.pad + @as(f32, @floatFromInt(i)) * m.row; + const name = ui.symbol(u, rec.strings.get(frame.pc) orelse u.print("{x}", .{frame.pc})); + // The program's own frame says where in its source it is. + const where = if (i == 0) u.print("{s}:{d}", .{ rec.file, rec.own[now.marked()] }) else ""; + rows.callRow(u, y, box.w, i, name, where, i == 0); } - return lines[0..n]; + veiled(u, box, now.veil); } }; -test "a mark goes behind a value's digits" { - try std.testing.expectEqual(@as(f32, 12 * adv), ui.Value.of(0x7fffffffd8c0, true).width()); - try std.testing.expectEqual(@as(f32, adv), ui.Value.of(5, false).width()); +test "the flags' bits" { + try std.testing.expectEqual(@as(?usize, 17), index("eflags")); } diff --git a/anim/src/cursor.zig b/anim/src/cursor.zig index 2ca8a97..2f7be1a 100644 --- a/anim/src/cursor.zig +++ b/anim/src/cursor.zig @@ -19,6 +19,9 @@ const ui = @import("ui.zig"); const theme = ui.theme; const time = theme.time; +/// The writes of one step strike this far apart, in the order they happened. +pub const write_apart = 6.0 / 60.0; + /// A mark lasts this many steps, then one more to leave in. const history = time.strike.ember_steps + 2; @@ -214,10 +217,16 @@ pub const Cursor = struct { }; var plan: Plan = .{ .travel = c.travel(from, to) }; var slot: f32 = 0; - for (levels, &plan.at) |is, *strikes| { - if (is) slot += time.cascade; + for (levels, &plan.at, 0..) |is, *strikes, level| { + // A drawing made of the bytes reacts when the projection from + // them arrives: its bracket and its line after their strike, and + // for a write its own arm. + const projected = level == 2 and is and levels[1] and c.levels.idea == .data; + if (projected) slot += time.project.bracket + time.project.draw + @as(f32, if (wrote > 0) time.strike.arm else 0) else if (is) slot += time.cascade; strikes.* = @max(slot, time.cascade); } + // The writes of one step strike 6 f apart: the last is later. + if (levels[1]) slot += @min(@max(wrote - 1, 0), 3) * write_apart; if (slot > 0) { // What there is to read: a level's worth each. (In the whole // context a line changes more registers than anyone reads; @@ -489,7 +498,7 @@ pub const Now = struct { return n.registerAgeFrom(reg, 0); } - fn registerAgeFrom(n: Now, reg: usize, skip: usize) ?u64 { + pub fn registerAgeFrom(n: Now, reg: usize, skip: usize) ?u64 { for (skip..history - 1) |age| { if (n.rec.registersChanged(n.stops[age + 1], n.stops[age]) >> @intCast(reg) & 1 != 0) return age; } @@ -517,8 +526,9 @@ pub const Now = struct { const a = age orelse return .none; if (a == 0 or a > s.ember_steps) return .none; if (a < s.ember_steps) return .ember; - // Linear: a fade. Gone by the time this step is over. - return .{ .level = 1 - std.math.clamp(n.time / s.out, 0, 1) }; + // Linear: a fade, as the mark leaves: secondary action does not + // start on the fire's frame, where the step's primary one does. + return .{ .level = 1 - std.math.clamp((n.time - n.plan.advance) / s.out, 0, 1) }; } /// The mark of a thing last changed `age` steps ago (`older`: not @@ -553,9 +563,23 @@ pub const Now = struct { } /// The mark of the bytes `addr .. addr + size`, which strike with - /// `level`, `delay` seconds after its first. + /// `level`, `delay` seconds after its first, and 6 f after the write of + /// this step before theirs. pub fn written(n: Now, addr: u64, size: u64, level: Level, delay: f32) ui.Heat { - return n.heat(n.writeAge(addr, size), n.writeAgeFrom(addr, size, 1), n.since(level, delay)); + return n.heat(n.writeAge(addr, size), n.writeAgeFrom(addr, size, 1), n.since(level, delay + n.writeDelay(addr, size))); + } + + /// How far after this step's first write the one of `addr .. addr + size` is. + pub fn writeDelay(n: Now, addr: u64, size: u64) f32 { + var order: f32 = 0; + var last: ?u64 = null; + for (n.rec.written(n.stops[1], n.stops[0])) |span| { + if (span.addr < addr + size and span.addr + span.len > addr) return @min(order, 3) * write_apart; + // Bytes of one write (one store, in pieces) are one strike. + if (last == null or span.addr != last.?) order += 1; + last = span.addr + span.len; + } + return 0; } /// The mark of a register: the CPU's level. @@ -611,7 +635,7 @@ test "a write is armed, strikes, holds, cools with the advance, and leaves after try expectEqual(ui.Heat.ember, rest.heat(2, 2, 0)); try expectEqual(ui.Heat.none, rest.heat(4, 4, 0)); try expectEqual(ui.Heat.none, rest.heat(null, null, 0)); - try std.testing.expectApproxEqAbs(@as(f32, 0.5), instant(s.out / 2.0, plan).heat(s.ember_steps, s.ember_steps, 0).level, 1e-3); + try std.testing.expectApproxEqAbs(@as(f32, 0.5), instant(0.90 + s.out / 2.0, plan).heat(s.ember_steps, s.ember_steps, 0).level, 1e-3); // Struck again over its ember: armed with the ember still under it. h = instant(0.20, plan).heat(0, 1, -0.10); try expect(h.level == 1 and h.arm == 1 and h.solid == 0); diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig new file mode 100644 index 0000000..e429643 --- /dev/null +++ b/anim/src/disasm.zig @@ -0,0 +1,419 @@ +//! `disasm` (design/README.md, "3. Panels"): seventeen `insn`s in address +//! order, the pc's, eight above and eight below (eleven as the lens's +//! subject): pwndbg's way, each instruction and what its operands are now. +//! Rows above the pc are the function's own; near its start there are +//! fewer and the pc's row sits higher. Past the function's end the rows +//! are `outside`. The rows that ran in the last three steps are `ran`, +//! with the jump lane showing how control came here. The current line's +//! runs of instructions are published for the `thread`. +//! Reads the recorded instructions (`zt.telescope.instruction`), each +//! moment's `touch` and `branch`, and the registers. +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const zt = @import("ztrace"); +const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const cursors = @import("cursor.zig"); +const Recording = @import("Recording.zig"); +const ties = @import("ties.zig"); +const context = @import("context.zig"); +const Stage = @import("stage.zig").Stage; +const theme = ui.theme; +const time = theme.time; +const Rect = ui.Rect; +const t = theme.color.text; +const m = theme.metric.context; +const c = theme.metric.insn; +const size = theme.size.mono.context; +const adv = size * 3 / 5; + +/// An instruction in the window. +pub const Insn = struct { + addr: u64, + len: u8, + text: []const u8, + /// Past the function's end. + outside: bool = false, + /// The line of the program's source it was compiled from. + line: ?u32 = null, + /// A direct call's callee's parameter count. + params: ?u8 = null, + + pub fn mnemonic(i: Insn) []const u8 { + return i.text[0 .. std.mem.indexOfScalar(u8, i.text, ' ') orelse i.text.len]; + } + + pub fn operands(i: Insn) []const u8 { + return std.mem.trim(u8, i.text[i.mnemonic().len..], " "); + } +}; + +/// The window at a moment: the instructions, and which is the pc's. +const Listing = struct { rows: []Insn, pc: usize }; + +pub const Disasm = struct { + cursor: cursors.Cursor, + stage: *const Stage, + focus: bool = false, + + pub fn draw(d: *const Disasm, f: *sk.Frame, placed: anim.Placed) void { + const box = d.stage.box(placed, .disasm); + const u = context.at(f, placed, box); + const now = d.cursor.now(placed); + const rec = d.cursor.rec; + const room = theme.layout.rowsIn(box.h); + const above = (room -| 1) / 2; + // The mark: it moves last, with the source's and rip. + const going = now.advancing(); + const from = listing(u, rec, now.marks[0], room, above); + const to = listing(u, rec, now.marks[1], room, above); + + const pc = [2]u64{ rec.register(now.marks[0], "rip"), rec.register(now.marks[1], "rip") }; + const name = rec.strings.get(pc[if (going >= 0.5) 1 else 0]) orelse ""; + const body = u.panel(box.w, box.h, "disassembly", .{ .focus = d.focus, .note = name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len] }); + const top = body.y + m.pad; + u.clip(.{ .x = theme.stroke.rule, .y = m.tag, .w = box.w - 2 * theme.stroke.rule, .h = box.h - m.tag - theme.stroke.rule }); + defer u.unclip(); + + // The two windows as one list, if they are of one function: the + // rows scroll, whole rows, on `glide`; else what leaves has left + // before what replaces it arrives. + var merged: []const Insn = to.rows; + var offsets = [2]f32{ 0, 0 }; + var fade: f32 = 1; + var shown = to; + if (pc[0] != pc[1] and going < 1) { + if (merge(u, from.rows, to.rows)) |both| { + merged = both.rows; + offsets = .{ @floatFromInt(both.from), @floatFromInt(both.to) }; + } else { + merged = if (going < 0.5) from.rows else to.rows; + shown = if (going < 0.5) from else to; + fade = @abs(2 * going - 1); + } + } + const scroll = std.math.lerp(offsets[0], offsets[1], cursors.glide(going)); + const lit = if (fade < 1) u.faded(fade) else u; + + // Where the pc's plaque is: it slides from the row it was on to the + // one it goes to (in one function), or goes out and comes in. + var mark_y = top + @as(f32, @floatFromInt(shown.pc)) * m.row; + var mark_shows: f32 = fade; + if (pc[0] != pc[1] and going < 1 and fade >= 1) { + const j = std.math.lerp(@as(f32, @floatFromInt(from.pc)) + offsets[0], @as(f32, @floatFromInt(to.pc)) + offsets[1], cursors.standard(going)); + mark_y = top + (j - scroll) * m.row; + } + if (to.rows.len == 0) mark_shows = 0; + + // Which moment the current mark stands for, and the three steps before it. + const base: usize = if (going >= 0.5) 0 else 1; + const current_line = rec.own[now.marks[if (going >= 0.5) 1 else 0]]; + const lead = blk: { + var addrs: [24]u64 = undefined; + var n: usize = 0; + for (shown.rows) |row| if (!row.outside and n < addrs.len) { + addrs[n] = row.addr; + n += 1; + }; + break :blk ui.shared(addrs[0..n]); + }; + + // The pc's row: gold ember across, a plaque with an ink triangle. + const gold = theme.mark(.current); + const out = now.fires(.disasm); + if (mark_shows > 0) { + const v = u.faded(mark_shows); + v.current(.{ .x = theme.stroke.rule, .y = mark_y, .w = box.w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.pc, out); + sk.draw.polygon(u.f, &.{ .{ 20, mark_y + m.row / 2 }, .{ 10, mark_y + 6 }, .{ 10, mark_y + m.row - 6 } }, v.at.pen(.{ .fill = ui.rgb(gold.on_solid) })); + ties.anchor(u, .pc, .{ .x = 0, .y = mark_y, .w = theme.metric.plaque.pc, .h = m.row }); + } + + var last_line: ?u32 = null; + var run: usize = 0; + var run_start: ?f32 = null; + var lanes: [2]?f32 = .{ null, null }; + for (merged, 0..) |row, j| { + const y = top + (@as(f32, @floatFromInt(j)) - scroll) * m.row; + const line = if (row.outside) null else row.line; + // The thread: the current line's runs of instructions. + const on_line = line != null and line.? == current_line and mark_shows >= 1; + if (on_line and run_start == null) run_start = @max(y, top); + if (!on_line and run_start != null) { + ties.anchor(u, .{ .run = run }, .{ .x = 0, .y = run_start.?, .w = box.w, .h = @min(y, box.h - m.pad) - run_start.? }); + run += 1; + run_start = null; + } + if (y + m.row < m.tag or y > box.h) { + last_line = line; + continue; + } + // `ran`: it ran in one of the last three steps. + var ran: ?usize = null; + for (0..3) |k| { + const at_k = now.stops[@min(base + 1 + k, now.stops.len - 1)]; + if (at_k != now.stops[base] and rec.register(at_k, "rip") == row.addr) { + ran = k; + break; + } + } + const is_pc = row.addr == pc[if (going >= 0.5) 1 else 0]; + if (ran != null and !is_pc) lit.trail(c.mark, y, ran.?); + d.insn(lit, now, row, y, box.w, line, line != last_line, current_line, lead, if (is_pc) .current else if (ran) |k| .{ .ran = k } else .ahead, base); + last_line = line; + // The jump that brought control here: from its row, up or down the lane. + if (ran == 0 and !is_pc) if (rec.insn_branches[now.stops[base + 1]]) |b| if (b.kind == .jump and b.taken) { + lanes[0] = y; + }; + if (is_pc) lanes[1] = y; + } + if (run_start) |y0| ties.anchor(u, .{ .run = run }, .{ .x = 0, .y = y0, .w = box.w, .h = @min(top + (@as(f32, @floatFromInt(merged.len)) - scroll) * m.row, box.h - m.pad) - y0 }); + if (lanes[0] != null and lanes[1] != null and lanes[0].? != lanes[1].?) { + const ya = lanes[0].? + m.row / 2; + const yb = lanes[1].? + m.row / 2; + lit.route(&.{ .{ 38, ya }, .{ c.jump_lane, ya }, .{ c.jump_lane, yb }, .{ 39, yb } }, theme.color.trail[0], .{ .width = theme.stroke.rule, .head = .{ 7, 8 }, .radius = 4 }); + } + context.veiled(u, box, now.veil); + } + + const State = union(enum) { current, ran: usize, ahead }; + + /// `insn`: an instruction and its annotation, pwndbg's way. + fn insn(d: *const Disasm, u: ui.Ui, now: cursors.Now, row: Insn, y: f32, w: f32, line: ?u32, first_of_line: bool, current_line: u32, lead: usize, state: State, base: usize) void { + const rec = d.cursor.rec; + const base_y = rows.baseline(y); + const code_ink = theme.color.region.code_; + if (row.outside) { + // The function ends: a rule, and the next one's name. + u.fill(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = theme.stroke.rule }, theme.color.rule); + _ = u.address(c.address, base_y, row.addr, .{ .ink = ui.mix(code_ink, theme.color.page, theme.metric.context.lead_alpha) }); + u.text(.mono, size, c.mnemonic, base_y, row.mnemonic(), t.faint); + u.text(.mono, size, c.operands, base_y, clipText(u, row.operands(), c.operand_chars), t.faint); + if (rec.strings.get(row.addr)) |name| u.text(.mono, size, c.annotation, base_y, name[0 .. std.mem.lastIndexOfScalar(u8, name, '+') orelse name.len], t.faint); + return; + } + const on = state == .current; + _ = u.address(c.address, base_y, row.addr, .{ .ink = ui.rgb(code_ink), .lead = lead }); + if (first_of_line) if (line) |l| { + const num = u.print("{d}", .{l}); + const now_line = l == current_line; + u.text(if (now_line) .mono_bold else .mono, size, c.line + 3 * adv - @as(f32, @floatFromInt(num.len)) * adv, base_y, num, if (now_line) theme.mark(.current).solid else t.faint); + }; + u.text(.mono_bold, size, c.mnemonic, base_y, row.mnemonic(), theme.color.code.keyword); + operands(u, row, base_y); + + // The annotation: what it does, now. What it left behind is shown + // only where the step ran this instruction alone: a step by line + // runs others after it, and the state after them is not its doing. + const moment, var after = switch (state) { + .current => .{ now.stops[base], if (base == 1) now.stops[0] else null }, + .ran => |k| .{ now.stops[base + 1 + k], now.stops[base + k] }, + .ahead => .{ now.stops[base], null }, + }; + if (after) |a| if (!alone(rec, moment, a, row)) { + after = null; + }; + const room = w - m.side; + const x: f32 = c.annotation; + const branch = if (state == .ahead) null else rec.insn_branches[moment]; + const touches = if (state == .ahead) [2]?Recording.Touch{ null, null } else rec.insn_touches[moment]; + _ = on; + if (branch) |b| switch (b.kind) { + .call => { + const callee = rec.strings.get(b.to) orelse ""; + u.text(.mono, size, x, base_y, callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len], t.bright); + // Its arguments hang under it, a small `reg` a row, as many as the prototype has. + if (state == .current) { + const callee_name = callee[0 .. std.mem.lastIndexOfScalar(u8, callee, '+') orelse callee.len]; + // As many as the callee's prototype has (the debug info's, else the source's), three when neither says. + const count: usize = row.params orelse for (rec.functions) |fun| { + if (std.mem.eql(u8, fun.name, callee_name)) break fun.params; + } else 3; + const shown_moment = rec.moment(moment); + const args = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9" }; + for (args[0..@min(count, 6)], 1..) |reg, k| { + const ry = y + @as(f32, @floatFromInt(k)) * m.row; + u.text(.mono, size, x, rows.baseline(ry), reg, t.dim); + _ = rows.chain(u, x + 4 * adv, ry, shown_moment, rec.register(moment, reg), room, .{}); + } + } + return; + }, + .ret => { + u.text(.mono, size, x, base_y, "\u{2192}", t.faint); + _ = u.address(x + 2 * adv, base_y, b.to, .{ .ink = ui.rgb(code_ink), .name = rec.strings.get(b.to) orelse "" }); + return; + }, + .jump => { + // A jump always taken says nothing; a conditional one, which way. + if (std.mem.eql(u8, row.mnemonic(), "jmp")) return; + if (!b.taken) return u.text(.mono, size, x, base_y, "not taken", t.dim); + u.text(.mono, size, x, base_y, "taken", t.primary); + return; + }, + .none => {}, + }; + if (std.mem.eql(u8, row.mnemonic(), "syscall") and state != .ahead) { + const nr = rec.register(moment, "rax"); + u.text(.mono, size, x, base_y, zt.syscalls.name(nr) orelse "syscall", t.bright); + const names = zt.syscalls.names(nr); + const carriers = [_][]const u8{ "rdi", "rsi", "rdx" }; + if (state == .current) for (names[0..@min(names.len, 3)], carriers[0..@min(names.len, 3)], 1..) |arg, reg, k| { + const ry = y + @as(f32, @floatFromInt(k)) * m.row; + u.text(.mono, size, x, rows.baseline(ry), arg, t.dim); + _ = rows.chain(u, x + @as(f32, @floatFromInt(arg.len + 1)) * adv, ry, rec.moment(moment), rec.register(moment, reg), room, .{}); + }; + return; + } + // Memory: a chain from the address it computes. Before the step, + // where it will look; in the step its first hop is read (with the + // dump's run: an echo), or the value it leaves is struck. + if (touches[0]) |touch| { + // In the step that runs it: the read is this step's (an echo of + // the dump's or the stack's), and a write's value after it, struck. + const in_step = state == .current and base == 1; + const reads = touch.how & 1 != 0; + const writes = touch.how & 2 != 0; + var marks: rows.Marks = .{}; + var shown = rec.moment(moment); + if (in_step and reads and !writes) { + marks.read = 0; + marks.read_shows = now.reading(.memory, 0); + } + if (writes) if (after) |a| { + if (state == .ran or now.landed(.memory, 0)) shown = rec.moment(a); + if (in_step) { + marks.changed = 1; + marks.changed_heat = now.written(touch.addr, touch.len, .memory, 0); + } + }; + _ = rows.chain(u, x, y, shown, touch.addr, room, marks); + return; + } + // Only a register: a small `reg` of what it left there. + const reg = written(row) orelse return; + const value_at = switch (state) { + .ran => after orelse return, + .current => if (after != null and now.landed(.cpu, 0)) after.? else return, + .ahead => return, + }; + const value = rec.register(value_at, reg); + u.text(.mono, size, x, base_y, reg, t.dim); + _ = rows.chain(u, x + @as(f32, @floatFromInt(reg.len + 1)) * adv, y, rec.moment(value_at), value, room, .{}); + } +}; + +/// Whether the step from moment `i` to `after` ran the instruction `row` +/// alone: the moment after it is at the next instruction, or where it jumped. +fn alone(rec: *const Recording, i: u64, after: u64, row: Insn) bool { + if (after != i + 1) return false; + const next = rec.register(after, "rip"); + if (next == row.addr + row.len) return true; + if (rec.insn_branches[i]) |b| return b.to == next; + return false; +} + +/// `text` in `chars` characters: cut at its far end, and saying so. +fn clipText(u: ui.Ui, text: []const u8, chars: usize) []const u8 { + if (text.len <= chars) return text; + return u.print("{s}\u{2026}", .{text[0 .. chars - 1]}); +} + +/// The operands, as the disassembler writes them, 24 characters; a jump's +/// or a call's target is an `address` in code's ink. +fn operands(u: ui.Ui, row: Insn, base_y: f32) void { + const ops = row.operands(); + const mn = row.mnemonic(); + const branches = mn[0] == 'j' or std.mem.eql(u8, mn, "call"); + if (branches and std.mem.startsWith(u8, ops, "0x")) { + if (std.fmt.parseInt(u64, ops[2..], 16)) |target| { + _ = u.address(c.operands, base_y, target, .{ .ink = ui.rgb(theme.color.region.code_) }); + return; + } else |_| {} + } + u.text(.mono, size, c.operands, base_y, clipText(u, ops, c.operand_chars), theme.color.code.plain); +} + +/// The register an instruction leaves a value in, if that is all it does +/// (Intel syntax: the first operand is where it goes). +fn written(row: Insn) ?[]const u8 { + const mn = row.mnemonic(); + for ([_][]const u8{ "cmp", "test", "push", "nop", "endbr64", "call", "ret", "leave", "hlt", "syscall", "ud2", "int3" }) |none| if (std.mem.eql(u8, mn, none)) return null; + if (mn[0] == 'j' or std.mem.startsWith(u8, mn, "cmov") and false) return null; + const ops = row.operands(); + const dest = ops[0 .. std.mem.indexOfScalar(u8, ops, ',') orelse ops.len]; + return full(std.mem.trim(u8, dest, " ")); +} + +/// A register's 64-bit name, from any of its widths. +fn full(name: []const u8) ?[]const u8 { + const table = [_][2][]const u8{ + .{ "rax", "eax" }, .{ "rbx", "ebx" }, .{ "rcx", "ecx" }, .{ "rdx", "edx" }, + .{ "rsi", "esi" }, .{ "rdi", "edi" }, .{ "rbp", "ebp" }, .{ "rsp", "esp" }, + }; + for (table) |pair| { + if (std.mem.eql(u8, name, pair[0]) or std.mem.eql(u8, name, pair[1])) return pair[0]; + if (name.len == 2 and pair[0][1] == name[0] and (name[1] == 'x' or name[1] == 'l' or name[1] == 'h' or name[1] == 'i' or name[1] == 'p')) return pair[0]; + } + if (name.len >= 2 and name[0] == 'r' and std.ascii.isDigit(name[1])) { + const digits = if (name.len >= 3 and std.ascii.isDigit(name[2])) name[0..3] else name[0..2]; + for ([_][]const u8{ "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15" }) |r| if (std.mem.eql(u8, r, digits)) return r; + } + return null; +} + +/// The window at moment `i`: up to `above` of the function's own +/// instructions over the pc (fewer near its start), the pc's, and as many +/// after it as fill `room`; past the function's end, `outside`. +fn listing(u: ui.Ui, rec: *const Recording, i: u64, room: usize, above: usize) Listing { + const out = u.f.scratch.alloc(Insn, room) catch return .{ .rows = &.{}, .pc = 0 }; + const pc = rec.register(i, "rip"); + var buf: [256]u8 = undefined; + var up: [16]Insn = undefined; + var n_up: usize = 0; + var addr = pc; + while (n_up < @min(above, up.len)) { + const here = rec.instruction(i, addr, &buf) orelse break; + if (here.back == 0) break; + addr -= here.back; + const one = rec.instruction(i, addr, &buf) orelse break; + up[n_up] = .{ .addr = addr, .len = one.len, .text = u.print("{s}", .{one.text}), .line = one.line, .params = one.params }; + n_up += 1; + } + var n: usize = 0; + while (n < n_up) : (n += 1) out[n] = up[n_up - 1 - n]; + const pc_row = n; + addr = pc; + var outside = false; + while (n < room) : (n += 1) { + const one = rec.instruction(i, addr, &buf) orelse break; + if (n > pc_row and one.back == 0) outside = true; + out[n] = .{ .addr = addr, .len = one.len, .text = u.print("{s}", .{one.text}), .outside = outside, .line = one.line, .params = one.params }; + addr += one.len; + } + return .{ .rows = out[0..n], .pc = pc_row }; +} + +/// Two windows of one function as one list in address order, and where each begins in it. +fn merge(u: ui.Ui, a: []const Insn, b: []const Insn) ?struct { rows: []const Insn, from: usize, to: usize } { + if (a.len == 0 or b.len == 0) return null; + const lo = if (a[0].addr <= b[0].addr) a else b; + const hi = if (lo.ptr == a.ptr) b else a; + // Where `hi` begins in `lo`: they must overlap. + const at = for (lo, 0..) |row, k| { + if (row.addr == hi[0].addr) break k; + } else return null; + const total = @max(lo.len, at + hi.len); + const out = u.f.scratch.alloc(Insn, total) catch return null; + @memcpy(out[0..lo.len], lo); + if (at + hi.len > lo.len) @memcpy(out[lo.len..], hi[lo.len - at ..]); + return .{ .rows = out, .from = if (lo.ptr == a.ptr) 0 else at, .to = if (lo.ptr == b.ptr) 0 else at }; +} + +test "a register is known by any of its widths" { + try std.testing.expectEqualStrings("rax", full("eax").?); + try std.testing.expectEqualStrings("rdi", full("rdi").?); + try std.testing.expectEqualStrings("r10", full("r10d").?); + try std.testing.expectEqualStrings("rax", full("al").?); + try std.testing.expect(full("qword ptr [rbp - 8]") == null); +} diff --git a/anim/src/heap.zig b/anim/src/heap.zig deleted file mode 100644 index e9a0dcf..0000000 --- a/anim/src/heap.zig +++ /dev/null @@ -1,105 +0,0 @@ -//! The heap, to scale: a strip along the addresses malloc handed out, each -//! live allocation a green block with the name of what points at it, each -//! freed one hollow and dashed, the allocator's own bytes between them -//! filled, and the bytes this step wrote in vermilion where they are. -//! Reads `Moment.heap`, and the recording's index of every allocation made. -const std = @import("std"); -const sk = @import("sketch"); -const anim = @import("anim"); -const ui = @import("ui.zig"); -const cursors = @import("cursor.zig"); -const Cursor = cursors.Cursor; -const Recording = @import("Recording.zig"); -const objects = @import("objects.zig"); -const theme = ui.theme; -const Rect = ui.Rect; -const t = theme.color.text; - -pub const Heap = struct { - cursor: Cursor, - box: Rect, - /// Variables each with a pigment (by its place here), as elsewhere. - track: []const []const u8 = &.{}, - focus: bool = false, - - pub fn draw(h: *const Heap, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = h.cursor.now(at); - const rec = h.cursor.rec; - // The heap is memory: what a step did to it shows when memory strikes. - const moment = now.shown(.memory); - const at_moment = now.stop(.memory); - // Not "heap": a grid of the same bytes beside it has that name. - const body = u.panel(h.box.w, h.box.h, "heap map", .{ .focus = h.focus, .note = "to scale · low addresses on the left" }); - if (rec.allocations.len == 0) return; - // The whole run's span, so nothing moves as chunks come and go. - var lo: u64 = std.math.maxInt(u64); - var hi: u64 = 0; - for (rec.allocations) |a| { - lo = @min(lo, a.ptr); - hi = @max(hi, a.ptr + a.size); - } - lo -|= 16; - const pad = theme.metric.panel.pad_x + 8; - const scale = @min(theme.metric.heap.units_per_byte, (body.w - 2 * pad - 60) / @as(f32, @floatFromInt(hi - lo))); - const strip_h: f32 = if (body.h < 220) theme.metric.heap.strip_h_stacked else theme.metric.heap.strip_h; - const y = body.y + (body.h - strip_h) / 2 - 6; - const live = moment.heap(f.scratch) catch return; - - // Never used: a rule frame. The allocator's own bytes, filled: - // whatever lies between what it has handed out so far. - var used = lo; - for (rec.allocations) |a| { - if (a.moment <= at_moment) used = @max(used, a.ptr + a.size); - } - u.frame(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(hi - lo)) * scale, .h = strip_h }, theme.color.rule, theme.stroke.rule); - u.fill(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(used - lo)) * scale, .h = strip_h }, theme.color.rule); - // Where it goes on. - for (0..3) |i| u.fill(.{ .x = pad + @as(f32, @floatFromInt(hi - lo)) * scale + 16 + @as(f32, @floatFromInt(i)) * 14, .y = y + strip_h / 2 - 3, .w = 6, .h = 6 }, t.faint); - - for (rec.allocations, 0..) |a, i| { - if (a.moment > at_moment) continue; - // The newest allocation at this address is the one to draw. - const newest = for (rec.allocations[i + 1 ..]) |later| { - if (later.ptr == a.ptr and later.moment <= at_moment) break false; - } else true; - if (!newest) continue; - const r: Rect = .{ .x = pad + @as(f32, @floatFromInt(a.ptr - lo)) * scale, .y = y, .w = @as(f32, @floatFromInt(a.size)) * scale - 2, .h = strip_h }; - const is_live = for (live) |l| { - if (l.ptr == a.ptr and l.born == a.event) break true; - } else false; - u.fill(r, theme.color.page); - if (is_live) { - const mark = theme.mark(.allocated); - const fresh = a.moment > now.stops[1]; - const p = if (fresh) now.moving(.memory, 0, theme.time.quick) else 1; - u.faded(p).fill(r, mark.ember); - u.faded(p).frame(r, mark.solid, theme.stroke.line); - // The bytes this step wrote, where they are. - var b: u64 = 0; - while (b < a.size) : (b += 8) { - // The same strike as the words beside it, at scale: no arm, no pulse. - var heat = now.written(a.ptr + b, @min(8, a.size - b), .memory, 0); - heat.arm = 0; - heat.out = 0; - if (heat.level > 0) _ = u.written(.{ .x = r.x + @as(f32, @floatFromInt(b)) * scale + 3, .y = y + 3, .w = @as(f32, @floatFromInt(@min(8, a.size - b))) * scale - 4, .h = strip_h - 6 }, heat, null); - } - // Who points at it. - var x = r.x + 6; - for (objects.pointing(f.scratch, rec, moment, a.ptr)) |v| { - const name = rec.name(v.name); - const w = u.width(.mono, theme.size.mono.small, name); - if (x + w > r.x + r.w - 4) break; - const tracked = for (h.track, 0..) |want, k| { - if (std.mem.eql(u8, want, name)) break k; - } else null; - if (tracked) |k| u.fill(.{ .x = x - 4, .y = y + strip_h - 36, .w = w + 8, .h = 30 }, theme.color.pigment[k % theme.color.pigment.len]); - u.text(.mono, theme.size.mono.small, x, y + strip_h - 12, name, t.primary); - x += w + 12; - } - } else u.dashedFrame(r, theme.mark(.freed).solid, theme.stroke.line); - // Its address under it: the end that tells chunks apart. - u.text(.mono, theme.size.mono.small, r.x, y + strip_h + 30, u.print("{x:0>4}", .{a.ptr & 0xffff}), t.faint); - } - } -}; diff --git a/anim/src/hexdump.zig b/anim/src/hexdump.zig index e4b0908..165f241 100644 --- a/anim/src/hexdump.zig +++ b/anim/src/hexdump.zig @@ -1,114 +1,157 @@ -//! Memory as a hexdump, for a lens that shows what its idea is made of: an -//! address gutter, sixteen bytes a row, the same bytes as text; and every -//! run of bytes marked as what it is, from the types of what the program's -//! pointers reach: a struct's number plain, its pointer in the ink of -//! where it points, the allocator's own bytes between allocations faint, -//! bytes never observed two dots. A line under a run says which field it -//! is (the legend is in the tag). Bytes a step wrote are struck here a -//! beat before the idea drawn from them changes: the words are the cause. -//! Reads `Moment.read` (with what is known), `Moment.allocation`, and the -//! recording's types. +//! `hexdump` (design/README.md, "3. Panels"): memory as `bytes` rows, +//! sixteen a row, the right half of a lens: the pane its drawing is a view +//! of. A window of the region kept round the rows the step touches (five +//! beside, nine as the subject), in address order; a gap in the addresses +//! is one row of `more`'s dots and the count left out. Each row's runs +//! come from the types of what the program's pointers reach: a field's +//! bytes on raised paper in its ink, the allocator's dim, never read two +//! dots; and its reading at 640: what the runs read as, little-endian. +//! Bytes a step wrote are struck here before the drawing reacts. +//! Reads `Moment.read`, `Moment.allocation`, and the recording's types. const std = @import("std"); -const sk = @import("sketch"); -const anim = @import("anim"); const ui = @import("ui.zig"); +const rows = @import("rows.zig"); const cursors = @import("cursor.zig"); -const contexts = @import("context.zig"); const Recording = @import("Recording.zig"); const objects = @import("objects.zig"); +const ties = @import("ties.zig"); const theme = ui.theme; const Rect = ui.Rect; const t = theme.color.text; const m = theme.metric.context; -const size = theme.size.mono.context; -const adv = size * 3 / 5; -/// A byte's two digits, and the gap to the next; a wider one in the middle of a row. -const pitch = 28; -const middle = 12; +const b = theme.metric.bytes; +const adv = theme.size.mono.context * 3 / 5; -pub const Hexdump = struct { - cursor: cursors.Cursor, - box: Rect, - title: []const u8 = "heap", - /// The first row's address; rounded down to sixteen. - base: u64, - focus: bool = false, +/// What a word of the region is. +pub const Kind = enum { number, pointer, allocator, unread }; - const Kind = enum { number, pointer, allocated, allocator, unknown }; +/// The kind of the word at `addr`: a field of a struct the program's +/// pointers reach (by its type), other allocated bytes, the allocator's +/// own between allocations, or never observed. +pub fn kindOf(things: []const objects.Object, moment: Recording.Moment, addr: u64) Kind { + for (things) |o| { + if (!o.holds(addr)) continue; + for (o.type.record.fields) |field| if (o.addr + field.offset == addr) return if (field.type == .pointer) .pointer else .number; + return .number; + } + var bytes: [8]u8 = undefined; + var known: [8]bool = undefined; + moment.read(addr, &bytes, &known) catch return .unread; + if (std.mem.indexOfScalar(bool, &known, true) == null) return .unread; + return if ((moment.allocation(addr) catch null) != null) .number else .allocator; +} + +/// The rows of a region (`lo .. hi`, sixteen-aligned) a dump of `count` +/// rows shows: all of them, or a window round the first the step touched. +pub fn window(now: cursors.Now, lo: u64, hi: u64, count: usize) u64 { + const all = (hi - lo) / 16; + if (all <= count) return lo; + var touched: ?u64 = null; + for ([_][]const Recording.Span{ now.rec.written(now.stops[1], now.stops[0]), now.rec.read(now.stops[1], now.stops[0]) }) |spans| for (spans) |span| { + if (touched == null and span.addr >= lo and span.addr < hi) touched = span.addr & ~@as(u64, 15); + }; + const at = touched orelse lo; + const row = (at - lo) / 16; + const first = @min(row -| (count / 2), all - count); + return lo + first * 16; +} + +/// What a dump is of: the structs a drawing beside it draws, or an array +/// of numbers (an element a run), and the ink of the memory it is in. +pub const Of = struct { + things: []const objects.Object = &.{}, + array: ?struct { addr: u64, len: u64, elem: u8 } = null, + region: u24 = theme.color.region.heap, +}; - pub fn draw(g: *const Hexdump, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = g.cursor.now(at); - const rec = g.cursor.rec; - const body = u.panel(g.box.w, g.box.h, g.title, .{ .focus = g.focus, .note = "sixteen bytes a row \u{00b7} underlined: a field of a node; faint: the allocator's own" }); - const things = objects.reach(f.scratch, rec, now.moment(), null); - const rows: usize = @intFromFloat(@floor((body.h - 2 * m.pad) / m.row)); - const bytes_x = m.side + 14 * adv + 8; - const text_x = bytes_x + 16 * pitch + middle + 12; - for (0..rows) |row| { - const addr = (g.base & ~@as(u64, 15)) + row * 16; - const y = body.y + m.pad + @as(f32, @floatFromInt(row)) * m.row; - const base = theme.baseline(y, m.row, size); - _ = u.value(m.side, base, addr, true, .{ .ink = ui.rgb(contexts.ink(contexts.regionOf(now.moment(), addr))) }); - var is: [16]u8 = undefined; - var known: [16]bool = undefined; - now.moment().read(addr, &is, &known) catch return; - var was: [16]u8 = undefined; - var was_known: [16]bool = undefined; - now.before().read(addr, &was, &was_known) catch return; - // A word at a time: a mark, and a field's line, go under a run of bytes, not under each. - for (0..2) |half| { - const word = addr + half * 8; - const x = bytes_x + @as(f32, @floatFromInt(half)) * (8 * pitch + middle); - const span: Rect = .{ .x = x - 3, .y = y + 2, .w = 8 * pitch - 4 + 2, .h = m.row - 4 }; - @import("ties.zig").anchor(u, .{ .word = word }, span); - const landed = now.landed(.memory, 0); - const mark = u.struck(span, now.written(word, 8, .memory, 0)); - if (now.wasRead(word, 8) and now.writeAge(word, 8) != 0) u.readFrame(span, now.reading(.memory, 0)); - const kind = g.kindOf(things, now.moment(), word); - // What the eight bytes are, if they are an address: its ink. - const held = Recording.word(if (landed) now.moment() else now.before(), word, 8) catch null; - const region = if (held) |value| contexts.regionOf(now.moment(), value) else .none; - const color: ui.Color = if (mark) |struck| struck.ink else switch (kind) { - .pointer => if (region != .none) ui.rgb(contexts.ink(region)) else ui.rgb(t.dim), - .number => ui.rgb(t.primary), - .allocated => ui.rgb(t.dim), - .allocator, .unknown => ui.rgb(t.faint), +/// The dump: `count` rows from `first`, at `x` (its panel's left edge +/// is the band's middle), the rows from `y`. The rows of what the drawing +/// draws have quiet brackets. +pub fn draw(u: ui.Ui, now: cursors.Now, x: f32, y: f32, first: u64, count: usize, of: Of) void { + const v = u.moved(x, 0); + const moment = now.shown(.memory); + const lead = ui.shared(&.{ first, first + 16 * @as(u64, count -| 1) }); + for (0..count) |i| { + const addr = first + 16 * i; + const ry = y + @as(f32, @floatFromInt(i)) * m.row; + var values: [16]u8 = undefined; + var known: [16]bool = undefined; + moment.read(addr, &values, &known) catch return; + var runs: [16]rows.Run = undefined; + var n: usize = 0; + var holds_drawn = false; + var at: u64 = 0; + while (at < 16) { + const here = addr + at; + var run: rows.Run = .{ .first = @intCast(at), .len = 8, .kind = .unread }; + if (of.array) |arr| if (here >= arr.addr and here < arr.addr + arr.len) { + run.len = arr.elem; + run.kind = .number; + holds_drawn = true; + }; + if (run.kind == .unread) { + // A word, or what is left of one before an array begins. + run.len = @intCast(@min(8 - (here % 8), 16 - at)); + if (of.array) |arr| if (here < arr.addr and arr.addr < here + run.len) { + run.len = @intCast(arr.addr - here); + }; + run.kind = switch (kindOf(of.things, moment, here)) { + .number => .number, + .pointer => .pointer, + .allocator => .allocator, + .unread => .unread, + }; + for (of.things) |o| if (o.holds(here)) { + holds_drawn = true; }; - for (0..8) |i| { - const n = half * 8 + i; - const bx = x + @as(f32, @floatFromInt(i)) * pitch; - if (!(if (landed) known[n] else was_known[n])) { - u.unknown(.{ .x = bx, .y = y, .w = 2 * adv, .h = m.row }); - continue; - } - const byte = if (landed) is[n] else was[n]; - u.text(if (mark != null) .mono_bold else .mono, size, bx, base, u.print("{x:0>2}", .{byte}), color); - // The same byte as text. - const tx = text_x + @as(f32, @floatFromInt(n)) * adv; - if (std.ascii.isPrint(byte) and byte != ' ') u.text(.mono, size, tx, base, &.{byte}, t.dim) else u.fill(.{ .x = tx + 4, .y = y + m.row / 2 - 2, .w = 4, .h = 4 }, t.faint); - } - // Which field: a line under the run, in the field's ink. - if (kind == .number or kind == .pointer) u.fill(.{ .x = x, .y = y + m.row - 3, .w = 8 * pitch - 4, .h = theme.stroke.rule }, if (kind == .pointer) theme.color.region.heap else t.primary); } + run.heat = now.written(here, run.len, .memory, 0); + if (now.wasRead(here, run.len) and now.writeAge(here, run.len) != 0) run.read = now.reading(.memory, 0); + if (run.kind == .number or run.kind == .pointer) ties.anchor(v, .{ .dump_run = here }, .{ .x = rows.byteX(run.first) - 4, .y = ry, .w = rows.byteX(run.first + run.len - 1) + 28 - rows.byteX(run.first) + 4, .h = m.row }); + runs[n] = run; + n += 1; + at += run.len; } - u.veil(.{ .x = theme.stroke.rule, .y = body.y, .w = body.w - 2 * theme.stroke.rule, .h = body.h - theme.stroke.rule }, now.veil); + rows.bytes(v, ry, addr, &values, &known, runs[0..n], of.region, lead, holds_drawn, moment); + reading(v, ry, moment, &values, &known, runs[0..n]); } +} - /// What the word at `addr` is: a field of a struct the program's - /// pointers reach (by its type), other allocated bytes, the - /// allocator's own between allocations, or never observed. - fn kindOf(g: *const Hexdump, things: []const objects.Object, moment: Recording.Moment, addr: u64) Kind { - _ = g; - for (things) |o| { - if (!o.holds(addr)) continue; - for (o.type.record.fields) |field| if (o.addr + field.offset == addr) return if (field.type == .pointer) .pointer else .number; - return .allocated; +/// The reading: what the runs read as, little-endian. A number ranged +/// right in three characters; a pointer as `points` and its address; +/// `NULL` dim; an allocator's run as a note, faint. +fn reading(u: ui.Ui, y: f32, moment: Recording.Moment, values: *const [16]u8, known: *const [16]bool, runs: []const rows.Run) void { + const size = theme.size.mono.context; + const base = rows.baseline(y); + var x: f32 = b.reading; + for (runs) |run| { + var bytes: [8]u8 = @splat(0); + @memcpy(bytes[0..run.len], values[run.first..][0..run.len]); + const word = std.mem.readInt(u64, &bytes, .little); + const whole = std.mem.indexOfScalar(bool, known[run.first..][0..run.len], false) == null; + if (!whole) continue; + switch (run.kind) { + .number => { + // A field typed a number reads as its decimal. + const shift: u6 = @intCast(64 - 8 * @as(u32, @min(run.len, 8))); + const digits = u.print("{d}", .{@as(i64, @bitCast(word << shift)) >> shift}); + const w = @as(f32, @floatFromInt(digits.len)) * adv; + u.text(.mono, size, x + @max(3 * adv - w, 0), base, digits, t.primary); + x += @max(3 * adv, w) + 8; + }, + .pointer => if (word == 0) { + u.text(.mono, size, x + theme.metric.context.arrow_w, base, "NULL", t.dim); + x += theme.metric.context.arrow_w + 4 * adv + 8; + } else { + x = u.arrow(x, y + m.row / 2, .points); + x = u.address(x, base, word, .{ .ink = ui.rgb(rows.ink(rows.regionOf(moment, word))) }) + 8; + }, + .allocator => if (run.first == 8 and run.len == 8) { + // The size word of the chunk after: malloc's. + u.text(.sans, theme.size.sans.micro, x, base - 1, u.print("malloc's: a chunk of {d}", .{word & ~@as(u64, 7)}), t.faint); + return; + }, + .unread => {}, } - var bytes: [8]u8 = undefined; - var known: [8]bool = undefined; - moment.read(addr, &bytes, &known) catch return .unknown; - if (std.mem.indexOfScalar(bool, &known, true) == null) return .unknown; - return if ((moment.allocation(addr) catch null) != null) .allocated else .allocator; } -}; +} diff --git a/anim/src/memory.zig b/anim/src/memory.zig deleted file mode 100644 index 8749bb9..0000000 --- a/anim/src/memory.zig +++ /dev/null @@ -1,444 +0,0 @@ -//! The memory grid: one 8-byte word a row, low addresses on top, every row -//! at a fixed place (nothing a step does moves a row). From the left: -//! registers and tracked variables that hold a row's address (plaques that -//! slide from row to row), the address, a lane for pointer arrows, the -//! eight bytes (or the word they make), the same bytes as text, and what -//! the row is. Bytes this step wrote are armed and struck, bytes it read -//! are framed, bytes never recorded are two dots, and a word that holds -//! the address of another row on screen is framed and joined to it by an -//! arrow. With `frames`, a rule names the call each stretch of stack -//! belongs to. -//! Reads `Moment.read` (with what is known), `Moment.regs`, -//! `Moment.variables`, and the recording's index of writes, reads and calls. -const std = @import("std"); -const sk = @import("sketch"); -const anim = @import("anim"); -const zt = @import("ztrace"); -const ui = @import("ui.zig"); -const cursors = @import("cursor.zig"); -const Recording = @import("Recording.zig"); -const objects = @import("objects.zig"); -const syscalls = @import("syscalls.zig"); -const theme = ui.theme; -const time = theme.time; -const Rect = ui.Rect; -const t = theme.color.text; -const pitch = theme.metric.byte.pitch; -const side = theme.metric.byte.side; -const size = theme.size.mono.data; -const small = theme.size.mono.small; -const row_h = theme.metric.row_pitch; -/// Bytes of one write strike a frame apart, low address first. -const byte_apart = 1.0 / 60.0; - -pub const Memory = struct { - cursor: cursors.Cursor, - box: Rect, - title: []const u8 = "memory", - note: []const u8 = "one word a row · low addresses on top", - /// The first row's address; rounded down to a word. - base: u64, - /// Bytes in a grid; each word as one number; or, for a string, eight - /// characters a row, each over its byte, with nothing else beside them. - form: enum { bytes, words, text } = .bytes, - /// The bytes again as text, right of them. - text: bool = true, - /// How many bytes to show (0: as many rows as fit). - bytes: u64 = 0, - /// How many words to show at most (0: as many as fit): fewer, when a - /// camera will come nearer and must not cut a row. - rows: usize = 0, - /// Stack: a rule where each call's frame begins, with the call's name. - frames: bool = false, - /// Registers shown beside the row whose address they hold. - registers: []const []const u8 = &.{ "rsp", "rbp" }, - /// Variables each with a pigment (by its place here), shown beside the - /// row they point at: the same list a node-link view of them is given. - track: []const []const u8 = &.{}, - focus: bool = false, - - const Row = union(enum) { word: u64, elision: u64 }; - const Columns = struct { gutter: f32, cells: f32, cell_w: f32, text: f32, names: f32, digits: usize }; - - pub fn draw(g: *const Memory, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = g.cursor.now(at); - const rec = g.cursor.rec; - const body = u.panel(g.box.w, g.box.h, g.title, .{ .focus = g.focus, .note = g.note }); - if (g.form == .text) return g.string(u, now, body); - // Memory is the second level a step reaches: until its changes - // land, the bytes are those from before the step. - const moment = now.shown(.memory); - const calls = if (g.frames) rec.stack(f.scratch, now.stop(.cpu)) else &.{}; - const regs = now.shown(.cpu).regs(rec.tid) catch null; - const things = objects.reach(f.scratch, rec, moment, null); - - // Columns. The lane on the left is as wide as two plaques side by side. - var widest = [2]f32{ 0, 0 }; - for ([_][]const []const u8{ g.registers, g.track }) |names| for (names) |name| { - const w = u.width(.mono, small, name) + 20; - if (w > widest[0]) { - widest = .{ w, widest[0] }; - } else widest[1] = @max(widest[1], w); - }; - const digits: usize = if (g.box.w < 1100) 8 else 12; - const gutter_x = theme.metric.panel.pad_x + @max(theme.metric.mark_lane, widest[0] + widest[1] + 8 + 14); - const cells_x = gutter_x + ui.hexWidth(digits, small) + theme.metric.arrow_lane + 2 * theme.stroke.lane_pitch; - const cell_w: f32 = if (g.form == .words) ui.hexWidth(16, size) + 24 else 8 * pitch - 2; - const text_x = cells_x + cell_w + 22; - const cols: Columns = .{ .gutter = gutter_x, .cells = cells_x, .cell_w = cell_w, .text = text_x, .names = if (g.text and g.form == .bytes) text_x + 8 * ui.advance(size) + 30 else text_x, .digits = digits }; - - // Rows, as many as fit, each where it will always be. Words the - // recording never saw at all are one row of dots; a word it will - // see later has its place already. - const ever = rec.moment(rec.end()); - var rows: std.ArrayList(Row) = .empty; - var ys: std.ArrayList(f32) = .empty; - var y = body.y + theme.metric.panel.pad_y; - const bottom = body.y + body.h - 8; - var addr = g.base & ~@as(u64, 7); - var words: usize = 0; - while (y + row_h <= bottom and (g.rows == 0 or words < g.rows)) : (words += 1) { - var unseen: u64 = 0; - while (!g.frames and unseen < 4096 and !knownAny(ever, addr + unseen)) unseen += 8; - if (unseen > 8) { - rows.append(f.scratch, .{ .elision = unseen }) catch break; - ys.append(f.scratch, y) catch break; - y += theme.metric.elision_h + 8; - addr += unseen; - if (unseen >= 4096) break; - continue; - } - rows.append(f.scratch, .{ .word = addr }) catch break; - ys.append(f.scratch, y) catch break; - y += row_h; - addr += 8; - } - const grid: Grid = .{ .rows = rows.items, .ys = ys.items }; - - // Pointers first: frames and arrows under nothing, in one run of shapes. - var lane: f32 = 0; - for (rows.items, ys.items) |row, row_y| { - if (row != .word) continue; - const fresh = now.walking() and now.writeAge(row.word, 8) == 0; - const since = now.since(.memory, 0); - const new = pointee(grid, now.moment(), row.word); - const old = pointee(grid, now.before(), row.word); - const held = if (now.landed(.memory, 0)) new else old; - if (held == null and !(fresh and old != null)) continue; - const cy = row_y + row_h / 2; - const out = theme.metric.pointer_frame_outset; - const frame: Rect = .{ .x = cells_x - out, .y = row_y + (row_h - side) / 2 - out, .w = cell_w + 2 * out, .h = side + 2 * out }; - const x_lane = cells_x - out - 18 - lane; - lane = @mod(lane + theme.stroke.lane_pitch, 3 * theme.stroke.lane_pitch); - const purple = theme.mark(.pointer).solid; - const way = struct { - fn to(scratch: std.mem.Allocator, from_x: f32, from_y: f32, lane_x: f32, to_x: f32, to_y: f32) []const ui.Vec { - const points = scratch.alloc(ui.Vec, 4) catch return &.{}; - points[0..4].* = .{ .{ from_x, from_y }, .{ lane_x, from_y }, .{ lane_x, to_y }, .{ to_x, to_y } }; - return points; - } - }.to; - if (!fresh or old == new) { - // As it stands; at a run a new arrow still grows to its target. - if (held) |to_y| u.arrow(way(f.scratch, frame.x, cy, x_lane, cells_x - 2, to_y + row_h / 2), purple, .{ .progress = if (now.writeAge(row.word, 8) == 0) cursors.standard(now.moving(.memory, 0, time.redirect.head)) else 1 }); - } else { - // A pointer rewritten. Anticipation: with the arm its old - // arrow goes dashed and faint. Follow-through: the word - // strikes, then the new arrow grows along its lane, and - // the row it lands on answers with a bright frame. - if (old) |to_y| { - const ghost = if (since < -time.strike.arm) u else u.faded(1 - std.math.clamp((since - time.redirect.ghost_hold) / time.redirect.ghost_out, 0, 1)); - ghost.arrow(way(f.scratch, frame.x, cy, x_lane, cells_x - 2, to_y + row_h / 2), if (since < -time.strike.arm) ui.rgb(purple) else ui.rgb(t.faint), .{ .dashed = since >= -time.strike.arm, .head = if (since < -time.strike.arm) theme.stroke.head else null, .width = if (since < -time.strike.arm) theme.stroke.arrow else theme.stroke.line }); - } - if (new) |to_y| { - const drawn = std.math.clamp((since - time.snap) / time.redirect.head, 0, 1); - u.arrow(way(f.scratch, frame.x, cy, x_lane, cells_x - 2, to_y + row_h / 2), purple, .{ .progress = cursors.standard(drawn) }); - const landed = since - time.snap - time.redirect.head; - if (landed >= 0 and landed < time.redirect.landing) u.faded(1 - landed / time.redirect.landing).frame(.{ .x = frame.x, .y = to_y + (row_h - side) / 2 - out, .w = frame.w, .h = frame.h }, theme.mark(.attention).solid, theme.stroke.line); - } - } - if (held != null) u.frame(frame, purple, theme.stroke.arrow); - } - - for (rows.items, ys.items, 0..) |row, row_y, i| switch (row) { - .elision => |bytes| u.elision(cells_x, row_y, if (bytes >= 4096) "nothing seen here yet" else u.print("{d} bytes not seen", .{bytes})), - .word => |a| { - g.word(u, now, things, calls, a, row_y, cols); - // Where a call's frame begins: a rule in the gap above its - // first row, and its name. No row moves to make room for it. - if (!g.frames) continue; - const owner = frameOf(calls, regs, a) orelse continue; - if (i > 0 and rows.items[i - 1] == .word and frameOf(calls, regs, rows.items[i - 1].word) == owner) continue; - const call = calls[owner]; - const name = if (call.arg) |arg| u.print("{s}({d})", .{ rec.name(call.function), @as(i64, @bitCast(arg)) }) else rec.name(call.function); - const w = u.width(.sans_medium, theme.size.sans.micro, name); - const rule_y = row_y + (row_h - side) / 2 - 8; - u.fill(.{ .x = gutter_x, .y = rule_y, .w = g.box.w - theme.metric.panel.pad_x - gutter_x - w - 12, .h = theme.stroke.rule }, if (owner == calls.len - 1) theme.mark(.current).solid else theme.color.rule); - u.text(.sans_medium, theme.size.sans.micro, g.box.w - theme.metric.panel.pad_x - w, rule_y + 8, name, if (owner == calls.len - 1) t.primary else t.dim); - }, - }; - g.plaques(u, now, grid, gutter_x); - // Staging: bytes cannot be read at a sprint. - u.veil(.{ .x = theme.stroke.rule, .y = body.y, .w = body.w - 2 * theme.stroke.rule, .h = body.h - theme.stroke.rule }, now.veil); - } - - const Grid = struct { - rows: []const Row, - ys: []const f32, - - fn rowY(grid: Grid, addr: u64) ?f32 { - for (grid.rows, grid.ys) |row, y| if (row == .word and row.word == addr & ~@as(u64, 7)) return y; - return null; - } - }; - - /// Where the row is that the word at `addr` points at, at `moment`. - fn pointee(grid: Grid, moment: Recording.Moment, addr: u64) ?f32 { - const target = (Recording.word(moment, addr, 8) catch null) orelse return null; - if (target == addr or target == 0) return null; - return grid.rowY(target); - } - - /// Left of the gutter: who holds a row's address. What persists - /// slides: a plaque moves to its new row as its level strikes, and - /// fades only where it has no row to come from or go to. - fn plaques(g: *const Memory, u: ui.Ui, now: cursors.Now, grid: Grid, gutter_x: f32) void { - const rec = g.cursor.rec; - const states = [2]Recording.Moment{ now.before(), now.moment() }; - const indices = [2]u64{ now.stops[1], now.stops[0] }; - // In each state, the row each name is at (registers, then tracked variables). - const count = g.registers.len + g.track.len; - const held = u.f.scratch.alloc([2]?u64, count) catch return; - for (states, indices, 0..) |state, index, k| { - const regs = state.regs(rec.tid) catch null; - for (g.registers, 0..) |reg, i| held[i][k] = if (regs) |r| r.named(reg) else null; - for (g.track, g.registers.len..) |name, i| { - held[i][k] = null; - const v = rec.variable(u.f.scratch, state, name) orelse continue; - if (rec.assigned(v, index)) held[i][k] = Recording.word(state, v.addr, 8) catch null; - } - } - for (0..count) |i| { - const is_register = i < g.registers.len; - const name = if (is_register) g.registers[i] else g.track[i - g.registers.len]; - const w = u.width(.mono, small, name) + 20; - var places: [2]?ui.Vec = .{ null, null }; - for (&places, 0..) |*place, k| { - const addr = held[i][k] orelse continue; - const y = grid.rowY(addr) orelse continue; - if (addr & 7 != 0) continue; - // Right to left, in order, among those on the same row. - var x = gutter_x - 14 - w; - for (0..i) |other| if (held[other][k] == addr) { - x -= u.width(.mono, small, if (other < g.registers.len) g.registers[other] else g.track[other - g.registers.len]) + 28; - }; - place.* = .{ x, y + (row_h - side) / 2 + 3 }; - } - const p = cursors.standard(now.moving(if (is_register) .cpu else .memory, 0, time.move)); - const at_place = if (places[0] != null and places[1] != null) sk.math.mix(places[0].?, places[1].?, p) else places[1] orelse places[0] orelse continue; - const v = if (places[0] == null) u.faded(p) else if (places[1] == null) u.faded(1 - p) else u; - const plaque: Rect = .{ .x = at_place[0], .y = at_place[1], .w = w, .h = side - 6 }; - if (is_register) { - v.fill(plaque, theme.color.raised); - v.frame(plaque, theme.mark(.pointer).solid, theme.stroke.rule); - } else v.fill(plaque, theme.color.pigment[(i - g.registers.len) % theme.color.pigment.len]); - v.label(.mono, small, plaque, .center, name, t.primary); - } - } - - /// A string: the character over its byte, eight to a row. - fn string(g: *const Memory, u: ui.Ui, now: cursors.Now, body: Rect) void { - const h = theme.metric.byte.text_h; - const gutter_x = theme.metric.panel.pad_x; - const cells_x = gutter_x + ui.hexWidth(8, small) + 24; - // A system call's work on these bytes: the kernel fills them (a - // read), or reads them (a write), once the call is down there. - const call = syscalls.inStep(now); - const plan: syscalls.Plan = if (call) |made| .of(made) else .{}; - const filled: ?Recording.Span = if (call) |made| syscalls.fills(made) else null; - const kernel_read: ?Recording.Span = if (call) |made| syscalls.bufferRead(made) else null; - var addr = g.base & ~@as(u64, 7); - var y = body.y + theme.metric.panel.pad_y + 8; - const end = if (g.bytes == 0) std.math.maxInt(u64) else g.base + g.bytes; - while (y + h <= body.y + body.h - 8 and addr < end) : ({ - y += h + 20; - addr += 8; - }) { - u.address(gutter_x, theme.baseline(y, h, small), addr, 8); - var bytes: [8]u8 = undefined; - var known: [8]bool = undefined; - now.moment().read(addr, &bytes, &known) catch return; - var old: [8]u8 = undefined; - var was_known: [8]bool = undefined; - now.before().read(addr, &old, &was_known) catch return; - // The bytes a syscall read, as one frame round their run. - var first_read: ?usize = null; - var last_read: usize = 0; - const read_shows = now.held(plan.work); - for (0..8) |i| { - const cell: Rect = .{ .x = cells_x + @as(f32, @floatFromInt(i)) * pitch, .y = y, .w = side, .h = h }; - const a = addr + i; - if (a >= end) break; - // Copied by the kernel two frames apart; by the program, one. - const by_kernel = filled != null and Recording.touches(&.{filled.?}, a, 1); - const delay = if (by_kernel) @as(f32, @floatFromInt(a - filled.?.addr)) * syscalls.Plan.byte else @as(f32, @floatFromInt(@min(now.writeOffset(a), 16))) * byte_apart; - const landed = now.landed(.memory, delay); - if (!(if (landed) known[i] else was_known[i])) { - u.unknown(cell); - continue; - } - const byte = if (landed) bytes[i] else old[i]; - // Seen for the first time and not written: it begins, so it fades in; it is not struck. - const cell_u = if (!was_known[i] and now.writeAge(a, 1) != 0) u.faded(now.since(.memory, 0) / time.quick) else u; - const was_read = kernel_read != null and Recording.touches(&.{kernel_read.?}, a, 1); - var heat = now.written(a, 1, .memory, delay); - // The flow up from the kernel is their anticipation: no arm, and no pulse. - if (by_kernel) heat.arm = 0; - if (by_kernel) heat.out = 0; - // What is being read is not also "changed a while ago". - if (was_read) heat.level *= 1 - read_shows; - const ink = cell_u.written(cell, heat, theme.color.raised); - if (was_read) { - cell_u.faded(read_shows).fill(cell, theme.mark(.read).ember); - if (first_read == null) first_read = i; - last_read = i; - } - const top: Rect = .{ .x = cell.x, .y = y + 6, .w = side, .h = 42 }; - if (std.ascii.isPrint(byte) and byte != ' ') { - cell_u.label(ink.face, size, top, .center, &.{byte}, ink.ink); - } else if (byte == '\n') { - cell_u.label(ink.face, size, top, .center, "\\n", ink.ink); - } else if (byte == ' ') { - // A space, drawn: the font's own is nothing to see. - const sx = cell.x + side / 2 - 8; - cell_u.fill(.{ .x = sx, .y = y + 34, .w = 16, .h = 3 }, ink.ink); - cell_u.fill(.{ .x = sx, .y = y + 28, .w = 3, .h = 6 }, ink.ink); - cell_u.fill(.{ .x = sx + 13, .y = y + 28, .w = 3, .h = 6 }, ink.ink); - } - cell_u.label(.mono, small, .{ .x = cell.x, .y = y + 46, .w = side, .h = 34 }, .center, u.print("{x:0>2}", .{byte}), if (ink.face == .mono_bold) ink.ink else ui.rgb(t.dim)); - } - if (first_read) |from| u.faded(read_shows).frame(.{ .x = cells_x + @as(f32, @floatFromInt(from)) * pitch - 4, .y = y - 4, .w = @as(f32, @floatFromInt(last_read - from + 1)) * pitch + 6, .h = h + 8 }, theme.mark(.read).solid, theme.stroke.line); - } - } - - fn word(g: *const Memory, u: ui.Ui, now: cursors.Now, things: []const objects.Object, calls: []const Recording.Call, addr: u64, y: f32, cols: Columns) void { - const rec = g.cursor.rec; - const cell_y = y + (row_h - side) / 2; - const base = theme.baseline(cell_y, side, size); - u.address(cols.gutter, theme.baseline(cell_y, side, small), addr, cols.digits); - - var bytes: [8]u8 = undefined; - var known: [8]bool = undefined; - now.moment().read(addr, &bytes, &known) catch return; - var old: [8]u8 = undefined; - var was_known: [8]bool = undefined; - now.before().read(addr, &old, &was_known) catch return; - const moment = now.shown(.memory); - const read_shows = now.reading(.memory, 0); - if (g.form == .words) { - const whole: Rect = .{ .x = cols.cells, .y = cell_y, .w = cols.cell_w, .h = side }; - const landed = now.landed(.memory, 0); - const is_known = std.mem.indexOfScalar(bool, if (landed) &known else &was_known, false) == null; - const heat = now.written(addr, 8, .memory, 0); - if (!is_known) { - _ = u.written(whole, heat, null); - u.unknown(whole); - } else { - // Known for the first time and not written: it fades in, with no mark. - const v = if (std.mem.indexOfScalar(bool, &was_known, false) != null and now.writeAge(addr, 8) != 0) u.faded(now.since(.memory, 0) / time.quick) else u; - const ink = v.written(whole, heat, theme.color.raised); - if (now.wasRead(addr, 8) and now.writeAge(addr, 8) != 0) u.readMark(whole, read_shows); - v.hex(size, whole.x + (whole.w - ui.hexWidth(16, size)) / 2, base, std.mem.readInt(u64, if (landed) &bytes else &old, .little), 16, ink); - } - } else for (0..8) |i| { - const cell: Rect = .{ .x = cols.cells + @as(f32, @floatFromInt(i)) * pitch, .y = cell_y, .w = side, .h = side }; - const a = addr + i; - const delay = @as(f32, @floatFromInt(@min(now.writeOffset(a), 16))) * byte_apart; - const landed = now.landed(.memory, delay); - const heat = now.written(a, 1, .memory, delay); - if (!(if (landed) known[i] else was_known[i])) { - _ = u.written(cell, heat, null); - u.unknown(cell); - continue; - } - const byte = if (landed) bytes[i] else old[i]; - const v = if (!was_known[i] and now.writeAge(a, 1) != 0) u.faded(now.since(.memory, 0) / time.quick) else u; - var ink = v.written(cell, heat, theme.color.raised); - if (now.wasRead(a, 1) and now.writeAge(a, 1) != 0) u.readMark(cell, read_shows); - // A zero byte is quiet, so data stands out of padding. - if (byte == 0 and ink.face == .mono and heat.solid == 0) ink.ink = ui.rgb(t.faint); - v.label(ink.face, size, cell, .center, u.print("{x:0>2}", .{byte}), ink.ink); - if (g.text) { - const tx = cols.text + @as(f32, @floatFromInt(i)) * ui.advance(size); - if (std.ascii.isPrint(byte) and byte != ' ') { - v.text(.mono, size, tx, base, &.{byte}, t.primary); - } else v.fill(.{ .x = tx + 7, .y = cell_y + side / 2 - 2, .w = 4, .h = 4 }, t.faint); - } - } - - // What the row is: a variable, a struct's field, what a call left. - if (variableAt(u, rec, moment, addr)) |v| { - u.text(.mono, small, cols.names, base, rec.name(v.name), t.primary); - } else if (fieldAt(things, addr)) |found| { - u.text(.mono, small, cols.names, base, found.field.name, t.dim); - if (found.field.type == .scalar) { - if ((Recording.word(moment, addr, @intCast(@min(8, found.field.type.size() orelse 8))) catch null)) |n| u.text(.mono, small, cols.names + 6 * ui.advance(small), base, u.print("= {d}", .{@as(i64, @bitCast(n))}), t.dim); - } else if (found.field.type == .pointer and (Recording.word(moment, addr, 8) catch null) == 0) u.text(.mono, small, cols.names + 6 * ui.advance(small), base, "= NULL", t.dim); - } else for (calls) |call| { - if (call.sp == addr) u.text(.sans, theme.size.sans.note, cols.names, base, "return to", t.dim); - } - } -}; - -/// Whether any byte of the word at `addr` was ever recorded. -fn knownAny(moment: Recording.Moment, addr: u64) bool { - var bytes: [8]u8 = undefined; - var known: [8]bool = undefined; - moment.read(addr, &bytes, &known) catch return false; - return std.mem.indexOfScalar(bool, &known, true) != null; -} - -/// The call whose frame holds stack address `addr`: the newest whose -/// return address sits at or above it. Null below the stack pointer. -pub fn frameOf(calls: []const Recording.Call, regs: ?zt.Regs, addr: u64) ?usize { - if (regs) |r| if (addr < r.rsp & ~@as(u64, 7)) return null; - var i = calls.len; - while (i > 0) { - i -= 1; - if (calls[i].sp >= addr) return i; - } - return null; -} - -/// The variable that begins in the word at `addr`. -fn variableAt(u: ui.Ui, rec: *const Recording, moment: Recording.Moment, addr: u64) ?Recording.Variable { - const all = moment.variables(u.f.scratch) catch return null; - for (all) |v| if (v.addr >= addr and v.addr < addr + 8 and rec.name(v.name).len > 0) return v; - return null; -} - -const Found = struct { object: objects.Object, field: Recording.types.Field }; - -/// The struct field that begins at `addr`, of the structs pointers reach. -fn fieldAt(things: []const objects.Object, addr: u64) ?Found { - for (things) |o| { - if (!o.holds(addr)) continue; - for (o.type.record.fields) |f| if (o.addr + f.offset == addr) return .{ .object = o, .field = f }; - } - return null; -} - -test "a stack address belongs to the newest frame at or above it" { - const calls = [_]Recording.Call{ - .{ .function = 1, .parent = null, .depth = 0, .sp = 0x1000, .enter = 0 }, - .{ .function = 2, .parent = 0, .depth = 1, .sp = 0x0fc0, .enter = 1 }, - }; - const regs: zt.Regs = .{ .rsp = 0x0fa0 }; - try std.testing.expectEqual(@as(?usize, 1), frameOf(&calls, regs, 0x0fa0)); - try std.testing.expectEqual(@as(?usize, 1), frameOf(&calls, regs, 0x0fc0)); - try std.testing.expectEqual(@as(?usize, 0), frameOf(&calls, regs, 0x0fc8)); - try std.testing.expectEqual(@as(?usize, 0), frameOf(&calls, regs, 0x1000)); - try std.testing.expectEqual(@as(?usize, null), frameOf(&calls, regs, 0x1008)); - try std.testing.expectEqual(@as(?usize, null), frameOf(&calls, regs, 0x0f98)); -} diff --git a/anim/src/nodes.zig b/anim/src/nodes.zig index 1326837..e6ba663 100644 --- a/anim/src/nodes.zig +++ b/anim/src/nodes.zig @@ -1,273 +1,257 @@ -//! Data structures as nodes and arrows: the structs the program's pointers -//! reach from a root variable (`objects.reach`: no offsets are given by -//! hand, the recording's type descriptions say which fields are pointers -//! and which is the value). A node's place is its allocation's (the order -//! it was made in), so nodes stay where they are while pointers change: a -//! list reversing is its arrows turning round. Variables that point at a -//! node are tags under it, tracked ones in their pigment, and slide from -//! node to node as they change; one that holds NULL waits at the row's -//! end. A node whose memory was freed is hollow and dashed. -//! This is the idea drawn from memory: it changes a beat after the bytes do. +//! `nodes`, a lens (design/README.md, "3. Panels"): the structs the +//! program's pointers reach from a root variable (`objects.reach`: the +//! recording's types say which fields are pointers and which is the +//! value), drawn as boxes of a value and a pointer with arrows between +//! them, in the band's left half; and in its right half the `hexdump` of +//! the bytes they are drawn from. A node's place is its allocation's, so +//! nodes stay where they are while pointers change: a list reversing is +//! its arrows turning round. Above each node its address, the same digits +//! as its row in the dump; under it its `pin`: the registers, and the +//! scene's variables, that hold its address. This is the idea drawn from +//! memory: it reacts when the step's projection arrives from its bytes. //! Reads `Moment.variables`, `Moment.read`, `Moment.allocation`. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); +const zt = @import("ztrace"); const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const context = @import("context.zig"); const cursors = @import("cursor.zig"); const Recording = @import("Recording.zig"); const objects = @import("objects.zig"); +const hexdump = @import("hexdump.zig"); +const ties = @import("ties.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; const t = theme.color.text; -/// A node of a lens beside the context. -const size = struct { - const w = 116; - const h = 44; - const pointer_w = 44; -}; +const m = theme.metric.context; pub const Nodes = struct { cursor: cursors.Cursor, - box: Rect, + stage: *const Stage, title: []const u8 = "the list", - note: []const u8 = "what the code means", /// The variable the structure hangs from. root: []const u8, - /// Variables each with a pigment (by its place here): the same list a - /// memory grid of them is given. + /// The scene's variables that point at nodes, named in their pins with the registers. track: []const []const u8 = &.{}, - /// How nodes are placed: in a row (a list), or a row for each distance - /// from the root (a tree, a graph). Either way a node's column is its own. - arrange: enum { row, levels } = .row, - focus: bool = false, + /// No registers in the pins: an instrumented program's registers say nothing of it. + quiet: bool = false, + focus: bool = true, - pub fn draw(g: *const Nodes, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = g.cursor.now(at); + pub fn draw(g: *const Nodes, f: *sk.Frame, placed: anim.Placed) void { + const box = g.stage.box(placed, .band); + if (box.h < m.tag + m.row) return; + const u = context.at(f, placed, box); + const now = g.cursor.now(placed); const rec = g.cursor.rec; + const heap = theme.color.region.heap; + const legend = [_]ui.Legend{ + .{ .text = "value", .ink = ui.rgb(t.primary) }, + .{ .text = "next", .ink = ui.rgb(heap) }, + .{ .text = "malloc's", .ink = ui.rgb(t.dim) }, + .{ .text = "never read", .ink = ui.rgb(t.faint) }, + }; + _ = u.panel(box.w, box.h, g.title, .{ .focus = g.focus, .legend = &legend }); + const half = theme.layout.state.x - theme.layout.program.x; const moment = now.shown(.idea); - const body = u.panel(g.box.w, g.box.h, g.title, .{ .focus = g.focus, .note = g.note }); const things = objects.reach(f.scratch, rec, moment, null); const before = objects.reach(f.scratch, rec, now.before(), null); + + // The dump: the heap the nodes are in, from the first one's chunk to the last's. + var lo: u64 = std.math.maxInt(u64); + var hi: u64 = 0; + for (rec.allocations) |a| if (things.len > 0 and a.size == things[0].type.record.size) { + lo = @min(lo, (a.ptr - 16) & ~@as(u64, 15)); + hi = @max(hi, (a.ptr + a.size + 15) & ~@as(u64, 15)); + }; + const count = theme.layout.rowsIn(box.h); + if (lo < hi) { + const first = hexdump.window(now, lo, hi, count); + const shown = @min(count, (hi - first) / 16); + const base = theme.size.sans.tag; + u.text(.sans_medium, base, half + 12, m.tag - 6, "its bytes", t.primary); + u.text(.sans, theme.size.sans.micro, half + 12 + u.width(.sans_medium, base, "its bytes") + 12, m.tag - 6, u.print("the heap, {x} to {x}", .{ first, first + 16 * shown - 1 }), t.faint); + hexdump.draw(u, now, half, m.tag + m.pad, first, shown, .{ .things = things }); + } if (things.len == 0) { const root = rec.variable(f.scratch, moment, g.root); const empty = root != null and (Recording.word(moment, root.?.addr, 8) catch null) == 0; - return u.text(.sans, theme.size.sans.note, theme.metric.panel.pad_x, body.y + 52, if (empty) u.print("empty: {s} is NULL", .{g.root}) else "nothing yet", t.faint); + u.text(.sans, theme.size.sans.micro, 24, m.tag + 40, if (empty) u.print("empty: {s} is NULL", .{g.root}) else "nothing yet", t.faint); + return; } - // Columns: one for every node the program will ever make, so no - // node moves when another comes or goes. And one more, where a - // variable that holds NULL waits. + // The drawing: one place for every node the program will ever make, + // so none moves when another comes or goes. + const subject = box.h >= theme.layout.split.subject.band.h - 1; + const size = if (subject) theme.metric.node.subject else theme.metric.node.beside; + const label: f32 = if (subject) theme.size.mono.small else theme.size.mono.context; + const value_size: f32 = if (subject) theme.size.mono.flight else theme.size.mono.small; var slots: usize = 0; for (rec.allocations) |a| slots += @intFromBool(a.size == things[0].type.record.size); slots = @max(slots, 1); - const count: f32 = @floatFromInt(slots); - const park_w = 76; - // As a lens beside the context a node is 116 by 44; a gap for an arrow. - const node_w = @min(size.w, @floor((body.w - 2 * 20 - park_w) / count) - 28); - const gap = @min(56, (body.w - 2 * 20 - park_w - count * node_w) / count); - const left: f32 = 24; - const row_y = body.y + 36; - const park: ui.Vec = .{ left + count * (node_w + gap), row_y - 12 }; - + const n: f32 = @floatFromInt(slots); + const x0 = (half - 32 - n * size[0] - (n - 1) * size[2]) / 2; + const ly = m.tag + @as(f32, if (subject) 44 else 16); + const ny = ly + label * 1.2 + 4; const places = f.scratch.alloc(Rect, things.len) catch return; for (things, places, 0..) |o, *place, i| { - const level: f32 = if (g.arrange == .levels) @floatFromInt(distance(f.scratch, rec, moment, things, g.root, i)) else 0; - place.* = .{ .x = left + @as(f32, @floatFromInt(o.slot orelse i)) * (node_w + gap), .y = if (g.arrange == .levels) body.y + 40 + level * (size.h + 56) else row_y, .w = node_w, .h = size.h }; - @import("ties.zig").anchor(u, .{ .node = o.addr }, place.*); - // Where it is in the heap, under it: the address the registers' chains to it show, in heap's ink. - _ = u.value(place.x, place.y + place.h + 20, o.addr, true, .{ .ink = ui.rgb(theme.color.region.heap) }); + place.* = .{ .x = x0 + @as(f32, @floatFromInt(o.slot orelse i)) * (size[0] + size[2]), .y = ny, .w = size[0], .h = size[1] }; + ties.anchor(u, .{ .node = o.addr }, place.*); } - // Arrows under the nodes they leave. + // Arrows first, under the nodes' edges. const since = now.since(.idea, 0); - var lane: f32 = 0; for (things, places) |o, place| for (o.type.record.fields) |field| { if (field.type != .pointer) continue; const from_addr = o.addr + field.offset; - const start = ui.Vec{ place.x + place.w - pointerW(place.w) / 2, place.y + size.h / 2 }; + const start = ui.Vec{ place.x + place.w - theme.metric.node.pointer_w / 2 + 6, place.y + place.h / 2 }; const new = if (Recording.word(now.moment(), from_addr, 8) catch null) |target| placeOf(things, places, target) else null; const old = if (Recording.word(now.before(), from_addr, 8) catch null) |target| placeOf(things, places, target) else null; const fresh = now.writeAge(from_addr, 8) == 0; - lane += 1; - const purple = theme.color.region.heap; + const ink = ui.rgb(heap); + const head = theme.metric.node.arrow_head; if (!fresh or !now.walking()) { - // As it stands; at a run a new arrow still grows to its node. - if (if (now.landed(.idea, 0)) new else old) |to| u.arrow(route(u, start, place, to, lane), purple, .{ .progress = if (fresh) cursors.standard(now.moving(.idea, 0, time.redirect.head)) else 1 }); + if (if (now.landed(.idea, 0)) new else old) |to| u.route(route(u, start, place, to), ink, .{ .width = theme.stroke.line, .head = head, .progress = if (fresh) cursors.standard(now.moving(.idea, 0, time.redirect.head)) else 1 }); continue; } - // A pointer rewritten, a beat after its word in memory. - // Anticipation: as the square is armed the old arrow goes - // dashed and faint. Follow-through: the square strikes, the - // new arrow grows to the node it now points at, and that - // node answers with a bright frame. The old one lingers. + // A pointer rewritten. Anticipation: as its half is armed the + // old arrow goes dashed and faint. Then the half strikes, the + // new arrow grows to the node it now points at, and that node + // answers with a bright frame. The old one lingers, then goes. const armed = since >= -time.strike.arm; if (old) |to| { const ghost = if (armed) u.faded(1 - std.math.clamp((since - time.redirect.ghost_hold) / time.redirect.ghost_out, 0, 1)) else u; - ghost.arrow(route(u, start, place, to, 0), if (armed) ui.rgb(t.faint) else ui.rgb(purple), .{ .dashed = armed, .head = if (armed) null else theme.stroke.head, .width = if (armed) theme.stroke.line else theme.stroke.arrow }); + ghost.route(route(u, start, place, to), if (armed) ui.rgb(t.faint) else ink, .{ .dashed = armed, .head = if (armed) null else head, .width = if (armed) theme.stroke.rule else theme.stroke.line }); } if (new) |to| { - u.arrow(route(u, start, place, to, lane), purple, .{ .progress = cursors.standard(std.math.clamp((since - time.snap) / time.redirect.head, 0, 1)) }); + u.route(route(u, start, place, to), ink, .{ .width = theme.stroke.line, .head = head, .progress = cursors.standard(std.math.clamp((since - time.snap) / time.redirect.head, 0, 1)) }); const landed = since - time.snap - time.redirect.head; - const out = theme.metric.pointer_frame_outset; - if (landed >= 0 and landed < time.redirect.landing) u.faded(1 - landed / time.redirect.landing).frame(.{ .x = to.x - out, .y = to.y - out, .w = to.w + 2 * out, .h = to.h + 2 * out }, theme.mark(.attention).solid, theme.stroke.line); + const out = 4; + if (landed >= 0 and landed < time.redirect.landing) u.faded(1 - landed / time.redirect.landing).frame(.{ .x = to.x - out, .y = to.y - out, .w = to.w + 2 * out, .h = to.h + 2 * out }, theme.mark(.attention).solid, theme.stroke.rule); } }; for (things, places) |o, place| { - const new = for (before) |b| { - if (b.addr == o.addr) break false; + const new = for (before) |was| { + if (was.addr == o.addr) break false; } else true; // What begins fades in; it does not pop. - g.node(if (new) u.faded(now.moving(.idea, 0, time.quick)) else u, now, o, place); + g.node(if (new) u.faded(now.moving(.idea, 0, time.quick)) else u, now, o, place, label, value_size); } - g.tags(u, now, things, places, park); } - fn node(g: *const Nodes, u: ui.Ui, now: cursors.Now, o: objects.Object, r: Rect) void { - _ = g; + fn node(g: *const Nodes, u: ui.Ui, now: cursors.Now, o: objects.Object, r: Rect, label: f32, value_size: f32) void { const moment = now.shown(.idea); - const value_w = r.w - pointerW(r.w); - const value: Rect = .{ .x = r.x, .y = r.y, .w = value_w, .h = r.h }; - const pointer: Rect = .{ .x = r.x + value_w, .y = r.y, .w = pointerW(r.w), .h = r.h }; + const heap = theme.color.region.heap; + const pw = theme.metric.node.pointer_w; + const value: Rect = .{ .x = r.x, .y = r.y, .w = r.w - pw - 2, .h = r.h }; + const pointer: Rect = .{ .x = r.x + r.w - pw, .y = r.y, .w = pw, .h = r.h }; + // Its address over it, in heap's ink: the digits of its row in the dump. + const digits = u.print("{x}", .{o.addr}); + const lw = @as(f32, @floatFromInt(digits.len)) * label * 3 / 5; + const ly = r.y - 4 - label * 1.2; + u.text(.mono, label, r.x + (r.w - lw) / 2, ly + label * 0.96, digits, heap); + ties.anchor(u, .{ .node_address = o.addr }, .{ .x = r.x, .y = ly, .w = r.w, .h = label * 1.2 }); if (o.freed) { // Handed back: its bytes are still there, and nobody owns them. u.dashedFrame(r, theme.mark(.freed).solid, theme.stroke.line); } else { - // A node is its own colour whoever points at it: pigment is for the names. u.fill(value, theme.color.raised); - u.fill(pointer, ui.mix(theme.color.raised, theme.color.page, 0.45)); - u.fill(.{ .x = pointer.x, .y = r.y, .w = theme.stroke.rule, .h = r.h }, theme.color.page); + u.fill(pointer, theme.color.well); } if (o.valueField()) |field| { const bytes: usize = @intCast(@min(8, field.type.size() orelse 8)); + const at = o.addr + field.offset; + ties.anchor(u, .{ .node_part = at }, value); var ink: ui.Ink = .{}; - if (!o.freed) ink = u.written(value, now.written(o.addr + field.offset, bytes, .idea, 0), null); - if (Recording.word(moment, o.addr + field.offset, bytes) catch null) |n| { + if (!o.freed) ink = u.cellWritten(value, now.written(at, bytes, .idea, 0), theme.color.raised); + if (now.wasRead(at, bytes) and now.writeAge(at, bytes) != 0) u.cellRead(value, now.reading(.idea, 0)); + if (Recording.word(moment, at, bytes) catch null) |n| { const shift: u6 = @intCast(64 - 8 * bytes); const signed = @as(i64, @bitCast(n << shift)) >> shift; - u.label(ink.face, theme.size.mono.small, value, .center, u.print("{d}", .{signed}), if (o.freed) ui.rgb(t.faint) else ink.ink); + u.label(ink.face, value_size, value, .center, u.print("{d}", .{signed}), if (o.freed) ui.rgb(t.faint) else ink.ink); } else u.unknown(value); } // The first pointer field: the square its arrow leaves from, or a slash for null. for (o.type.record.fields) |field| { if (field.type != .pointer) continue; - const held = Recording.word(moment, o.addr + field.offset, 8) catch null; - // On the solid of a strike the square is ink, like digits. - const struck = !o.freed and u.written(pointer, now.written(o.addr + field.offset, 8, .idea, 0), null).face == .mono_bold; + const at = o.addr + field.offset; + ties.anchor(u, .{ .node_part = at }, pointer); + const held = Recording.word(moment, at, 8) catch null; + const struck = !o.freed and u.cellWritten(pointer, now.written(at, 8, .idea, 0), theme.color.well).face == .mono_bold; + if (now.wasRead(at, 8) and now.writeAge(at, 8) != 0) u.cellRead(pointer, now.reading(.idea, 0)); + const sq = theme.metric.node.origin; if (held == null) { u.unknown(pointer); } else if (held.? == 0) { - u.line(.{ pointer.x + 12, pointer.y + pointer.h - 10 }, .{ pointer.x + pointer.w - 12, pointer.y + 10 }, if (struck) t.ink else t.dim, theme.stroke.line); - } else u.fill(.{ .x = pointer.x + pointer.w / 2 - 7, .y = pointer.y + pointer.h / 2 - 7, .w = 14, .h = 14 }, if (struck) t.ink else theme.color.region.heap); + u.line(.{ pointer.x + 10, pointer.y + pointer.h - 8 }, .{ pointer.x + pointer.w - 10, pointer.y + 8 }, if (struck) t.ink else t.dim, theme.stroke.rule); + } else u.fill(.{ .x = pointer.x + pointer.w / 2 - sq / 2, .y = pointer.y + pointer.h / 2 - sq / 2, .w = sq, .h = sq }, if (struck) t.ink else heap); break; } + // Who points here: a pin under it, landing with its link. + const names = g.pins(u, now, o.addr); + const cx = r.x + r.w / 2; + ties.anchor(u, .{ .node_pin = o.addr }, .{ .x = cx - 1, .y = r.y + r.h, .w = 2, .h = label + 10 }); + if (names.names.len > 0 and names.shows > 0) u.faded(names.shows).pinUnder(cx, r.y + r.h, names.names, heap, label); } - /// The names: the root and the tracked variables, each a tag under - /// the node it points at, or at the row's end when it holds NULL. - /// What persists slides: a tag goes to its new place as the idea - /// strikes, and fades only where it has nowhere to come from. - fn tags(g: *const Nodes, u: ui.Ui, now: cursors.Now, things: []const objects.Object, places: []const Rect, park: ui.Vec) void { + const Pins = struct { names: []const []const u8, shows: f32 }; + + /// The registers that hold `addr` (before this step, and after it once + /// their link has landed), then the scene's variables that do. + fn pins(g: *const Nodes, u: ui.Ui, now: cursors.Now, addr: u64) Pins { const rec = g.cursor.rec; - const small = theme.size.mono.context; - const h = 22; - const states = [2]Recording.Moment{ now.before(), now.moment() }; - const count = 1 + g.track.len; - // In each state, where each name points: an address, 0 for NULL, null for nowhere to draw. - const held = u.f.scratch.alloc([2]?u64, count) catch return; - var parked = false; - for (0..count) |i| for (states, 0..) |state, k| { - held[i][k] = null; - const v = rec.variable(u.f.scratch, state, if (i == 0) g.root else g.track[i - 1]) orelse continue; - if (!rec.assigned(v, state.index)) continue; - const to = (Recording.word(state, v.addr, 8) catch null) orelse continue; - if (to == 0 or placeOf(things, places, to) != null) held[i][k] = to; - if (to == 0) parked = true; - }; - if (parked) u.text(.mono, small, park[0], park[1] + 16, "NULL", t.faint); - const p = cursors.glide(now.moving(.idea, 0, time.move)); - for (0..count) |i| { - const name = if (i == 0) g.root else g.track[i - 1]; - var at_place: [2]?ui.Vec = .{ null, null }; - for (&at_place, 0..) |*place, k| { - const to = held[i][k] orelse continue; - // Under the names already at the same place. - var above: f32 = 0; - for (0..i) |other| above += @floatFromInt(@intFromBool(held[other][k] == to)); - // Beside the names already at the same place: under the node's address, or at the row's end. - var width: f32 = 0; - for (0..i) |other| if (held[other][k] == to) { - width += u.width(.mono, small, if (other == 0) g.root else g.track[other - 1]) + 16 + 4; - }; - place.* = if (to == 0) .{ park[0], park[1] + 24 + above * (h + 2) } else if (placeOf(things, places, to)) |r| .{ r.x + width, r.y + r.h + 28 } else null; - } - const xy = if (at_place[0] != null and at_place[1] != null) sk.math.mix(at_place[0].?, at_place[1].?, p) else at_place[1] orelse at_place[0] orelse continue; - const v = if (at_place[0] == null) u.faded(p) else if (at_place[1] == null) u.faded(1 - p) else u; - const tag: Rect = .{ .x = xy[0], .y = xy[1], .w = u.width(.mono, small, name) + 16, .h = h }; - if (i > 0) v.fill(tag, theme.color.pigment[(i - 1) % theme.color.pigment.len]) else v.frame(tag, t.faint, theme.stroke.rule); - v.label(.mono, small, tag, .center, name, t.primary); + var out: std.ArrayList([]const u8) = .empty; + var shows: f32 = 0; + const is = (now.moment().regs(rec.tid) catch null) orelse zt.Regs{}; + const was = (now.before().regs(rec.tid) catch null) orelse is; + // The scene's variables first: what the narration names. + const moment = now.shown(.idea); + for (g.track) |name| { + const v = rec.variable(u.f.scratch, moment, name) orelse continue; + if (!rec.assigned(v, moment.index)) continue; + if ((Recording.word(moment, v.addr, 8) catch null) != addr) continue; + out.append(u.f.scratch, name) catch break; + shows = 1; } + if (!g.quiet) for (theme.metric.all_registers[0..16]) |name| { + const i = context.index(name).?; + const before = was.named(name) == addr; + const after = is.named(name) == addr; + if (!before and !after) continue; + const s = ties.pinShows(now, i, .node); + const this = if (before and after) 1 else if (after) s.new else s.old; + if (this < 0.5) continue; + out.append(u.f.scratch, name) catch break; + shows = @max(shows, this); + }; + return .{ .names = out.items, .shows = shows }; } }; -/// The pointer's square, of a node `w` wide. -fn pointerW(w: f32) f32 { - return @min(size.pointer_w, @round(w * 0.4)); -} - fn placeOf(things: []const objects.Object, places: []const Rect, addr: u64) ?Rect { for (things, places) |o, place| if (o.addr == addr) return place; return null; } -/// From the pointer square at `start` (in the node at `from`) to the left -/// edge of the node at `to`: straight to the neighbour on the right, else -/// up and over, in a lane of its own. -fn route(u: ui.Ui, start: ui.Vec, from: Rect, to: Rect, lane: f32) []const ui.Vec { - const enter = ui.Vec{ to.x - 2, to.y + to.h / 2 }; - const out = u.f.scratch.alloc(ui.Vec, 5) catch return &.{}; - if (@abs(to.y - from.y) < 1 and to.x > from.x and to.x - (from.x + from.w) < 100) { +/// From the pointer square at `start` (in the node at `from`) to the node +/// at `to`: straight to a node on its right; to one on its left, under +/// the nodes, in from below. +fn route(u: ui.Ui, start: ui.Vec, from: Rect, to: Rect) []const ui.Vec { + const out = u.f.scratch.alloc(ui.Vec, 4) catch return &.{}; + if (to.x > from.x) { out[0] = start; - out[1] = enter; + out[1] = .{ to.x - 3, to.y + to.h / 2 }; return out[0..2]; } - // Over the top when going back, under when going on past a neighbour. - const over = to.x < from.x or @abs(to.y - from.y) > 1; - const y = if (over) @min(from.y, to.y) - 12 - @mod(lane, 2) * 8 else @max(from.y + from.h, to.y + to.h) + 64 + @mod(lane, 2) * theme.stroke.lane_pitch; - out[0] = .{ start[0], if (over) from.y else from.y + from.h }; - out[1] = .{ start[0], y }; - out[2] = .{ to.x - 26, y }; - out[3] = .{ to.x - 26, enter[1] }; - out[4] = enter; + const under = from.y + from.h + 6; + out[0] = .{ start[0], from.y + from.h }; + out[1] = .{ start[0], under }; + const into = to.x + (to.w - theme.metric.node.pointer_w) / 2; + out[2] = .{ into, under }; + out[3] = .{ into, to.y + to.h + 1 }; return out; } - -/// How many pointers from the root variable to `things[i]`, at least. -fn distance(scratch: std.mem.Allocator, rec: *const Recording, moment: Recording.Moment, things: []const objects.Object, root: []const u8, i: usize) usize { - const dist = scratch.alloc(usize, things.len) catch return 0; - @memset(dist, things.len); - if (rec.variable(scratch, moment, root)) |v| if (Recording.word(moment, v.addr, 8) catch null) |first| { - for (things, 0..) |o, k| if (o.addr == first) { - dist[k] = 0; - }; - }; - // Few nodes: relax until nothing changes. - var changed = true; - while (changed) { - changed = false; - for (things, 0..) |o, k| { - if (dist[k] == things.len) continue; - for (o.type.record.fields) |field| { - if (field.type != .pointer) continue; - const target = (Recording.word(moment, o.addr + field.offset, 8) catch null) orelse continue; - for (things, 0..) |other, j| if (other.addr == target and dist[j] > dist[k] + 1) { - dist[j] = dist[k] + 1; - changed = true; - }; - } - } - } - return if (dist[i] == things.len) 0 else dist[i]; -} diff --git a/anim/src/registers.zig b/anim/src/registers.zig deleted file mode 100644 index 5f08d6f..0000000 --- a/anim/src/registers.zig +++ /dev/null @@ -1,117 +0,0 @@ -//! The variables of the innermost call, as a table: names, types as C -//! spells them, values, each struck when memory is (a variable is -//! memory). The registers are `context.Registers`. -//! Reads `Moment.variables`, `Moment.read`. -const std = @import("std"); -const sk = @import("sketch"); -const anim = @import("anim"); -const ui = @import("ui.zig"); -const cursors = @import("cursor.zig"); -const Cursor = cursors.Cursor; -const Recording = @import("Recording.zig"); -const objects = @import("objects.zig"); -const theme = ui.theme; -const Rect = ui.Rect; -const size = theme.size.mono.dense; -const m = struct { - const row_h = theme.metric.dense.row_h; - const name_x = theme.metric.panel.pad_x; - const value_pad = 4; -}; -/// Of two things one step does the same to, the second follows the first. -const second = theme.time.flight.second; - -pub const Locals = struct { - cursor: Cursor, - box: Rect, - /// Names to show, in order; empty for every variable of the innermost - /// call, then the globals. - names: []const []const u8 = &.{}, - focus: bool = false, - - pub fn draw(l: *const Locals, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; - const now = l.cursor.now(at); - const rec = l.cursor.rec; - const moment = now.moment(); - const calls = rec.stack(f.scratch, now.stops[0]); - const where = if (calls.len > 0) u.print("{s}()", .{rec.name(calls[calls.len - 1].function)}) else ""; - const body = u.panel(l.box.w, l.box.h, "locals", .{ .focus = l.focus, .note = where }); - const all = moment.variables(f.scratch) catch return; - const depth: u32 = @intCast(calls.len); - - var y = body.y + 8; - const room: usize = @intFromFloat(@floor((body.h - 16) / m.row_h)); - var shown: usize = 0; - var struck: f32 = 0; - if (l.names.len > 0) { - for (l.names) |name| { - const v = rec.variable(f.scratch, moment, name) orelse continue; - if (shown < room) l.row(u, now, v, y, &struck); - y += m.row_h; - shown += 1; - } - return; - } - // The innermost call's, then the globals. - for ([_]u32{ depth, 0 }) |want| for (all) |v| { - if (v.depth != want or rec.name(v.name).len == 0 or shown == room) continue; - l.row(u, now, v, y, &struck); - y += m.row_h; - shown += 1; - }; - } - - fn row(l: *const Locals, u: ui.Ui, now: cursors.Now, v: Recording.Variable, y: f32, struck: *f32) void { - const rec = l.cursor.rec; - const base = theme.baseline(y, m.row_h, size); - const adv = ui.advance(size); - const small = size; - const name = rec.name(v.name); - u.text(.mono, size, m.name_x, base, name, theme.color.text.primary); - const type_x = m.name_x + @max(5, @as(f32, @floatFromInt(name.len)) + 2) * adv; - const kind = if (Recording.types.parse(u.f.scratch, rec.strings.get(v.type) orelse "")) |t| objects.spell(u.f.scratch, t) else |_| ""; - u.text(.mono, small, type_x, base, kind[0..@min(kind.len, 8)], theme.color.text.faint); - - const value_x = type_x + 9 * ui.advance(small); - // A variable is memory: it changes when memory does, the second - // one a step wrote a little after the first. - const age = now.writeAge(v.addr, v.size); - const delay = if (age == 0) struck.* * second else 0; - if (age == 0) struck.* += 1; - const landed = now.landed(.memory, delay); - // Not yet assigned in this call: its bytes are not its value. - const text = (if (rec.assigned(v, if (landed) now.stops[0] else now.stops[1])) describe(u, rec, if (landed) now.moment() else now.before(), v) else null) orelse { - _ = u.written(.{ .x = value_x - m.value_pad, .y = y + 2, .w = 3 * adv + 2 * m.value_pad, .h = m.row_h - 4 }, now.written(v.addr, v.size, .memory, delay), null); - return u.unknown(.{ .x = value_x, .y = y, .w = 3 * adv, .h = m.row_h }); - }; - const cell: Rect = .{ .x = value_x - m.value_pad, .y = y + 2, .w = @as(f32, @floatFromInt(text.len)) * adv + 2 * m.value_pad, .h = m.row_h - 4 }; - const ink = u.written(cell, now.written(v.addr, v.size, .memory, delay), null); - if (now.wasRead(v.addr, v.size) and age != 0) u.readMark(cell, now.reading(.memory, 0)); - u.text(ink.face, size, value_x, base, text, ink.ink); - } -}; - -/// A variable's value as its type says to read it: a number in decimal, a -/// pointer in hex, an array as its first elements. -fn describe(u: ui.Ui, rec: *const Recording, moment: Recording.Moment, v: Recording.Variable) ?[]const u8 { - const t = rec.typeOf(u.f.scratch, v.type) orelse return plain(u, moment, v.addr, v.size, false); - return switch (t) { - .scalar => |s| plain(u, moment, v.addr, v.size, s.kind == .int), - .pointer => blk: { - const to = (Recording.word(moment, v.addr, 8) catch return null) orelse return null; - var buf: [20]u8 = undefined; - break :blk if (to == 0) "NULL" else u.print("{s}", .{ui.grouped(&buf, to)}); - }, - .array => |a| u.print("[{d}]", .{a.len}), - else => null, - }; -} - -fn plain(u: ui.Ui, moment: Recording.Moment, addr: u64, bytes: u64, signed: bool) ?[]const u8 { - if (bytes == 0 or bytes > 8) return null; - const raw = (Recording.word(moment, addr, @intCast(bytes)) catch return null) orelse return null; - if (!signed) return u.print("{d}", .{raw}); - const shift: u6 = @intCast(64 - 8 * bytes); - return u.print("{d}", .{@as(i64, @bitCast(raw << shift)) >> shift}); -} diff --git a/anim/src/root.zig b/anim/src/root.zig index 82cf8d3..a69cda9 100644 --- a/anim/src/root.zig +++ b/anim/src/root.zig @@ -4,8 +4,8 @@ //! const canim = @import("canim"); //! const rec = canim.open(recordings.sort); //! const at = canim.cursor(s, rec, 0); // where the picture is -//! const ctx = canim.context(s, at, .{}); // the debugger's whole context -//! canim.establish(s, .{ ctx.clock, ctx.source, ctx.disasm, ctx.registers, ctx.stack, ctx.backtrace }); +//! const ctx = canim.context(s, at, .{ .split = canim.layout.split.beside }); // the debugger's whole context +//! canim.establish(s, .{ ctx.registers, ctx.stack, ctx.source, ctx.disasm, ctx.ties }); //! at.walk(s, .line, rec.call("sort", 0).enter); // play the program forward //! //! Every component is an `anim` object (`fadeIn`, `move`, ...) that draws @@ -26,19 +26,20 @@ pub const Now = cursors.Now; pub const Plan = cursors.Plan; pub const Levels = cursors.Levels; +pub const rows = @import("rows.zig"); +pub const Stage = @import("stage.zig").Stage; pub const Source = @import("source.zig").Source; const contexts = @import("context.zig"); pub const Registers = contexts.Registers; pub const Stack = contexts.Stack; pub const Backtrace = contexts.Backtrace; -pub const Disasm = contexts.Disasm; -pub const Locals = @import("registers.zig").Locals; +/// The registers' tag: what an address's ink says. +pub const contextLegend = contexts.legend; +pub const Disasm = @import("disasm.zig").Disasm; pub const calls = @import("calls.zig"); pub const CallTree = calls.Tree; -pub const Memory = @import("memory.zig").Memory; pub const Nodes = @import("nodes.zig").Nodes; -pub const Heap = @import("heap.zig").Heap; -pub const Hexdump = @import("hexdump.zig").Hexdump; +pub const hexdump = @import("hexdump.zig"); const tying = @import("ties.zig"); pub const Ties = tying.Ties; pub const Place = tying.Place; @@ -186,79 +187,67 @@ pub const Camera = struct { } }; -/// The debugger's whole context, the same in every scene: the state -/// column (every register followed as far as it points, the stack as a -/// telescope, the backtrace), and in the program column the source (all -/// of it, in a window) and the disassembly at the pc; and the clock. -/// A scene adds its lens at `split.lens`: a lens is never shown instead -/// of the machine it is drawn from. +/// The debugger's whole context, the same in every scene (design/README.md, +/// "5. The frame"): the state column (every register followed as far as it +/// points, the stack with its frames, the backtrace where there is room), +/// the program column (the whole source in a window, the disassembly at +/// the pc), the ties between them, and the clock. A scene adds its lens in +/// the band (`stage`): a lens is never shown instead of the machine. pub const Context = struct { + stage: *Stage, clock: anim.Obj, source: anim.Obj, disasm: anim.Obj, registers: anim.Obj, stack: anim.Obj, backtrace: anim.Obj, + /// The ties: add it after the lens, with `ties`. + cursor: Cursor, + quiet: bool, pub const Options = struct { - /// How the program column is shared (`layout.split`). + /// How the stage is shared (`layout.split`); `Stage.relayout` changes it. split: layout.Split = layout.split.beside, /// A program that makes system calls: see `Registers.syscalls`. syscalls: bool = false, /// An instrumented program: see `Registers.quiet`. quiet: bool = false, }; + + /// The ties between the panes, drawn over everything added before. + pub fn ties(c: Context, s: *anim.Frame) anim.Obj { + return add(s, Ties{ .cursor = c.cursor, .stage = c.stage, .quiet = c.quiet }); + } }; pub fn context(s: *anim.Frame, at: Cursor, options: Context.Options) Context { - const source_at = add(s, Source{ .cursor = at, .box = options.split.source }); - const disasm_at = add(s, Disasm{ .cursor = at, .box = options.split.disasm }); - const registers_at = add(s, Registers{ .cursor = at, .syscalls = options.syscalls, .quiet = options.quiet }); - const stack_at = add(s, Stack{ .cursor = at }); - const backtrace_at = add(s, Backtrace{ .cursor = at }); - return .{ .clock = add(s, Clock{ .cursor = at }), .source = source_at, .disasm = disasm_at, .registers = registers_at, .stack = stack_at, .backtrace = backtrace_at }; + const stage = Stage.init(s, options.split); + return .{ + .stage = stage, + .cursor = at, + .quiet = options.quiet, + .source = add(s, Source{ .cursor = at, .stage = stage }), + .disasm = add(s, Disasm{ .cursor = at, .stage = stage }), + .registers = add(s, Registers{ .cursor = at, .stage = stage, .syscalls = options.syscalls, .quiet = options.quiet }), + .stack = add(s, Stack{ .cursor = at, .stage = stage }), + .backtrace = add(s, Backtrace{ .cursor = at, .stage = stage }), + .clock = add(s, Clock{ .cursor = at }), + }; } -/// Any component (a struct of this module with a `box`), in the scene -/// where its box says. The functions below are this, by name. +/// Any component of this module in the scene: one with a `box` where its +/// box says; one on the stage (it reads its box there each frame) at the origin. pub fn add(s: *anim.Frame, component: anytype) anim.Obj { - return ui.place(s, component, component.box.x, component.box.y); + if (@hasField(@TypeOf(component), "box")) return ui.place(s, component, component.box.x, component.box.y); + return ui.place(s, component, 0, 0); } -pub fn source(s: *anim.Frame, options: Source) anim.Obj { - return add(s, options); -} -pub fn registers(s: *anim.Frame, options: Registers) anim.Obj { - return add(s, options); -} -pub fn stack(s: *anim.Frame, options: Stack) anim.Obj { - return add(s, options); -} -pub fn disasm(s: *anim.Frame, options: Disasm) anim.Obj { - return add(s, options); -} -pub fn locals(s: *anim.Frame, options: Locals) anim.Obj { - return add(s, options); -} pub fn callTree(s: *anim.Frame, options: CallTree) anim.Obj { return add(s, options); } -pub fn memory(s: *anim.Frame, options: Memory) anim.Obj { - return add(s, options); -} pub fn nodes(s: *anim.Frame, options: Nodes) anim.Obj { return add(s, options); } -pub fn heap(s: *anim.Frame, options: Heap) anim.Obj { - return add(s, options); -} -pub fn hexdump(s: *anim.Frame, options: Hexdump) anim.Obj { - return add(s, options); -} -/// The connections between panes. Add it after every view it is to join. -pub fn ties(s: *anim.Frame, options: Ties) anim.Obj { - return add(s, options); -} pub fn array(s: *anim.Frame, options: Array) anim.Obj { return add(s, options); } @@ -288,14 +277,14 @@ test { _ = Recording; _ = cursors; _ = @import("source.zig"); - _ = @import("registers.zig"); + _ = @import("stage.zig"); + _ = rows; _ = contexts; + _ = @import("disasm.zig"); _ = tying; _ = @import("hexdump.zig"); _ = calls; - _ = @import("memory.zig"); _ = @import("nodes.zig"); - _ = @import("heap.zig"); _ = @import("array.zig"); _ = @import("syscalls.zig"); _ = objects; diff --git a/anim/src/rows.zig b/anim/src/rows.zig new file mode 100644 index 0000000..063301a --- /dev/null +++ b/anim/src/rows.zig @@ -0,0 +1,367 @@ +//! The catalogue's rows (design/README.md, "2. Rows"): every row is 24 +//! high and one line, made of atoms, and none wraps: what does not fit is +//! cut at its far end and says so. A row draws what it is given, in the +//! states it is given; the panels (`context.zig`, `disasm.zig`, +//! `source.zig`, `hexdump.zig`) read the recording and the cursor and say +//! which. x is from the panel's left edge, y is the row's top. +const std = @import("std"); +const sk = @import("sketch"); +const zt = @import("ztrace"); +const ui = @import("ui.zig"); +const Recording = @import("Recording.zig"); +const theme = ui.theme; +const Rect = ui.Rect; +const t = theme.color.text; +const m = theme.metric.context; +const size = theme.size.mono.context; +const adv = size * 3 / 5; +const row_h = m.row; + +/// What an address into `region` is inked in. +pub fn ink(region: zt.telescope.Region) u24 { + const c = theme.color.region; + return switch (region) { + .none => c.number, + .code => c.code_, + // Writable and executable: its region's ink (data's, here) and an underline. + .rwx => c.data, + .stack => c.stack, + .heap => c.heap, + .data => c.data, + .rodata => c.rodata, + }; +} + +pub fn regionOf(moment: Recording.Moment, value: u64) zt.telescope.Region { + return zt.telescope.region(moment, value) catch .none; +} + +pub fn baseline(y: f32) f32 { + return theme.baseline(y, row_h, size); +} + +/// The marks on a chain's hops. +pub const Marks = struct { + /// The first hop: a register (or a word) written: armed, struck, held. + first: ui.Heat = .none, + /// A later hop, the memory under a pointer that did not change: its + /// tick sits before that hop alone. + changed: ?usize = null, + changed_heat: ui.Heat = .ember, + /// A hop read, and how much its frame shows. + read: ?usize = null, + read_shows: f32 = 0, + /// `extend`: seconds since the first hop was struck; the hops after it + /// are followed again 4 f apart, each fading in over 6 f. + extend: ?f32 = null, + /// The first hop's `lead` (a column of addresses). + lead: usize = 0, + /// Where the first hop's tick goes, left of its digits: 9 after a + /// name, 5 after an arrow. + tick: f32 = 9, +}; + +/// Where a chain's hops were drawn: x and width of each one's digits. +pub const Hops = struct { + x: [6]f32 = undefined, + w: [6]f32 = undefined, + n: usize = 0, + end: f32 = 0, +}; + +/// `chain`: a telescope, `address (points address)* (holds value)?`, of +/// `value` at `moment`, from `x` on the row at `y`, cut where `room` +/// ends. A code address ends it with its name, a string in quotes; depth +/// is the recording's. Cut at its far end: a string is shortened first, +/// then the chain ends early in `more`; nothing is taken from the middle. +pub fn chain(u: ui.Ui, x: f32, y: f32, moment: Recording.Moment, value: u64, room: f32, marks: Marks) Hops { + var links_buf: [6]zt.telescope.Link = undefined; + var text_buf: [48]u8 = undefined; + const links = zt.telescope.chase(moment, value, &links_buf, &text_buf) catch return .{}; + const base = baseline(y); + const mid = y + row_h / 2; + var hops: Hops = .{}; + var at = x; + for (links, 0..) |link, i| { + const pointer = link.region != .none; + const shown = ui.Value.of(link.value, pointer); + const w = shown.width(); + // The hops after a struck first one are followed again, one at a time. + const v = if (i > 0 and marks.extend != null) u.faded(std.math.clamp((marks.extend.? - @as(f32, @floatFromInt(i - 1)) * theme.time.unroll) / (6.0 / 60.0), 0, 1)) else u; + if (i > 0) { + const named: f32 = if (link.region == .code and link.text.len > 0 and !link.string) 10 + @as(f32, @floatFromInt(link.text.len)) * adv else 0; + // Room for it, and for saying there is more. + if (pointer and at + m.arrow_w + w + named + 40 > room) { + _ = v.arrow(at, mid, .more); + break; + } + if (!pointer and at + m.arrow_w + w > room) { + // The value it ends on has no room: it says so. + if (at + 2 * adv <= room) v.text(.mono, size, at + 4, base, "\u{2026}", t.faint); + break; + } + at = v.arrow(at, mid, if (pointer) .points else .holds); + } + const marked: Rect = .{ .x = at, .y = y, .w = w, .h = row_h }; + var face_ink: ui.Ink = .{ .ink = ui.rgb(if (pointer) ink(link.region) else t.primary) }; + if (i == 0) { + if (v.written(marked, marks.first, marks.tick)) |struck| face_ink = struck; + } else if (marks.changed == i) { + if (v.written(marked, marks.changed_heat, 5)) |struck| face_ink = struck; + } + if (marks.read == i) v.read(marked, marks.read_shows); + if (pointer) { + _ = v.address(at, base, link.value, .{ .ink = face_ink.ink, .face = face_ink.face, .lead = if (i == 0) marks.lead else 0, .rwx = link.region == .rwx }); + } else _ = v.value(at, base, link.value, false, face_ink); + if (hops.n < hops.x.len) { + hops.x[hops.n] = at; + hops.w[hops.n] = w; + hops.n += 1; + } + at += w; + if (!pointer and shown.decimal) at += 8 + @as(f32, @floatFromInt(std.fmt.count("{d}", .{link.value}))) * adv; + if (link.text.len == 0) { + // Still a pointer where the recording stopped following. + if (i + 1 == links.len and pointer and link.region != .code and link.region != .rwx and at + m.arrow_w + 24 <= room) at = v.arrow(at, mid, .more); + continue; + } + if (link.string) { + // What a pointer leads to, when it is text: quoted, cut to the room + // there is (never under six characters), with `…` and no closing quote. + const fits: usize = @intFromFloat(@max((room - at - m.arrow_w) / adv, 0)); + if (fits < m.string_min + 2) break; + at = v.arrow(at, mid, .holds); + const whole = link.text.len + 2 <= fits; + const text = if (whole) u.print("\"{s}\"", .{link.text}) else u.print("\"{s}\u{2026}", .{link.text[0 .. fits - 2]}); + v.text(.mono, size, at, base, text, theme.color.string); + at += @as(f32, @floatFromInt(std.unicode.utf8CountCodepoints(text) catch text.len)) * adv; + } else { + // A code address's name. + const name = ui.symbol(u, link.text); + const fits: usize = @intFromFloat(@max((room - at - 10) / adv, 0)); + if (fits >= 3) { + v.text(.mono, size, at + 10, base, name[0..@min(name.len, fits)], t.primary); + at += 10 + @as(f32, @floatFromInt(@min(name.len, fits))) * adv; + } + } + break; + } + hops.end = at; + return hops; +} + +/// The cell a mark goes on for `value` drawn at `x` in a row at `y`. +pub fn cell(x: f32, y: f32, moment: Recording.Moment, value: u64) Rect { + return .{ .x = x, .y = y, .w = ui.Value.of(value, regionOf(moment, value) != .none).width(), .h = row_h }; +} + +/// `reg`: a register's name (dim) 12 in, its chain from 72, room to 12 +/// from the edge `w`. +pub fn reg(u: ui.Ui, y: f32, w: f32, name: []const u8, moment: Recording.Moment, value: u64, marks: Marks) Hops { + u.text(.mono, size, m.side, baseline(y), name, t.dim); + return chain(u, m.chain_x, y, moment, value, w - m.side, marks); +} + +/// The flags, in rip's row: their names, the value. +pub const Flag = struct { name: []const u8, bit: u6 }; +pub const flags = [_]Flag{ .{ .name = "CF", .bit = 0 }, .{ .name = "PF", .bit = 2 }, .{ .name = "AF", .bit = 4 }, .{ .name = "ZF", .bit = 6 }, .{ .name = "SF", .bit = 7 }, .{ .name = "IF", .bit = 9 }, .{ .name = "DF", .bit = 10 }, .{ .name = "OF", .bit = 11 } }; + +/// `reg` rip: gold plaque on the name, ember across, the address in +/// code's ink and its name; never struck. At its right end the `flags`, +/// eight names on a pitch of 42: set, upper case on a chip, bold, bright; +/// clear, lower case, dim; changed, a tick (`changed`: a bit per flag, and +/// how held). The value of eflags is not drawn: the names are the value. +pub fn rip(u: ui.Ui, y: f32, w: f32, moment: Recording.Moment, pc: u64, eflags: u64, changed: u64, held: f32, fire: f32) void { + const gold = theme.mark(.current); + u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, theme.metric.plaque.rip, fire); + u.text(.mono_bold, size, m.side, baseline(y), "rip", gold.on_solid); + const fl = theme.metric.flags; + var x = w - m.side - @as(f32, flags.len) * fl.pitch + 10; + _ = chain(u, m.chain_x, y, moment, pc, x - 16, .{}); + for (flags) |flag| { + const set = eflags >> flag.bit & 1 != 0; + if (changed >> flag.bit & 1 != 0 and held > 0) u.fill(.{ .x = x - 7, .y = y + 3, .w = m.tick, .h = row_h - 6 }, ui.mix(gold.ember, theme.mark(.write).solid, held)); + if (set) { + u.chip(.{ .x = x, .y = y + 2, .w = fl.chip[0], .h = fl.chip[1] }); + u.text(.mono_bold, size, x + 4, baseline(y), flag.name, t.bright); + } else u.text(.mono, size, x + 4, baseline(y), u.print("{c}{c}", .{ std.ascii.toLower(flag.name[0]), std.ascii.toLower(flag.name[1]) }), ui.mix(t.dim, gold.ember, 0.2)); + x += fl.pitch; + } +} + +/// `slot`: a word of the stack: a lane 112 wide for `row` pins (`pins`, +/// the registers that hold its address), then its chain from 122, +/// beginning with the slot's own address in stack ink with `lead`, room +/// to 136 from the right edge `w` (the `call` spans'). +pub fn slot(u: ui.Ui, y: f32, w: f32, moment: Recording.Moment, addr: u64, pins: []const []const u8, pin_shows: f32, marks: Marks) Hops { + const s = theme.metric.slot; + if (pins.len > 0 and pin_shows > 0) u.faded(pin_shows).pinRow(s.lane, y, pins, theme.color.region.stack); + var lead = marks; + lead.first = .none; + const base = baseline(y); + const end = u.address(s.chain, base, addr, .{ .ink = ui.rgb(theme.color.region.stack), .lead = marks.lead }); + const held = (Recording.word(moment, addr, 8) catch null) orelse { + _ = u.arrow(end, y + row_h / 2, .points); + u.unknown(.{ .x = end + m.arrow_w, .y = y, .w = 24, .h = row_h }); + return .{ .end = end + m.arrow_w + 24 }; + }; + // The word there: a pointer, or what the slot holds. + const pointer = regionOf(moment, held) != .none; + const at = u.arrow(end, y + row_h / 2, if (pointer) .points else .holds); + lead.lead = 0; + lead.first = marks.first; + lead.tick = 5; + return chain(u, at, y, moment, held, w - s.room, lead); +} + +/// `call` `span`: one frame of the call chain on the stack, a bracket 14 +/// in from the panel's right edge `w` down the slots `y0 .. y1` that are +/// the frame, and the function's name left of it (Go Medium 18). `newest` +/// gold and bright; older, rule and dim. `open`: it runs past the panel. +pub fn span(u: ui.Ui, w: f32, y0: f32, y1: f32, name: []const u8, newest: bool, open: bool) void { + const x = w - theme.metric.slot.span; + const c = if (newest) theme.mark(.current).solid else theme.color.rule; + u.bracket(x, y0 + 3, y1 - @as(f32, if (open) 0 else 3), c, theme.metric.bracket.quiet, -1, open); + const micro = theme.size.sans.micro; + const mid = (y0 + y1) / 2 + 6; + u.text(.sans_medium, micro, x - 14 - u.width(.sans_medium, micro, name), mid, name, if (newest) t.bright else t.dim); +} + +/// `call` `row`: `#n` (a gold plaque on the newest), the name and offset +/// in primary at 60, file and line in dim at 276. +pub fn callRow(u: ui.Ui, y: f32, w: f32, n: usize, name: []const u8, where: []const u8, newest: bool) void { + const gold = theme.mark(.current); + if (newest) u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, 44, 0); + u.text(if (newest) .mono_bold else .mono, size, m.side, baseline(y), u.print("#{d}", .{n}), if (newest) gold.on_solid else t.faint); + u.text(.mono, size, 60, baseline(y), name[0..@min(name.len, 17)], t.primary); + u.text(.mono, size, 276, baseline(y), where, t.dim); +} + +/// `line`: a line of source, its number ranged right in three characters +/// (faint), the text from 64 in the four code inks, 67 characters and +/// then a faint `…`. `current`: a gold plaque 50 wide on the number (its +/// edges out `fire`), ember across; `marked`: the number as on the plaque, +/// which its panel draws where the mark is (it slides). `trail`: the bar +/// (0 newest). +pub fn line(u: ui.Ui, y: f32, w: f32, number: u32, text: []const u8, styles: []const u8, state: union(enum) { plain, current: f32, marked, trail: usize }) void { + const gold = theme.mark(.current); + switch (state) { + .current => |fire| u.current(.{ .x = theme.stroke.rule, .y = y, .w = w - 2 * theme.stroke.rule, .h = row_h }, theme.metric.plaque.line, fire), + .trail => |k| u.trail(theme.stroke.rule, y, k), + .plain, .marked => {}, + } + const on = state == .current or state == .marked; + const num = u.print("{d}", .{number}); + u.text(if (on) .mono_bold else .mono, size, m.line_number_end - @as(f32, @floatFromInt(num.len)) * adv, baseline(y), num, if (on) gold.on_solid else t.faint); + const limit: usize = m.source_chars; + const end = @min(text.len, limit); + var i: usize = 0; + while (i < end) { + var j = i + 1; + while (j < end and styles[j] == styles[i]) j += 1; + if (std.mem.indexOfNone(u8, text[i..j], " ") != null) { + const class = styles[i]; + u.text(codeFace(class), size, m.line_text + @as(f32, @floatFromInt(i)) * adv, baseline(y), text[i..j], codeInk(class)); + } + i = j; + } + if (text.len > limit) u.text(.mono, size, m.line_text + @as(f32, @floatFromInt(limit)) * adv, baseline(y), "\u{2026}", t.faint); +} + +/// pardes's classes: none, keyword, string, number, comment. +fn codeInk(class: u8) u24 { + const c = theme.color.code; + return switch (class) { + 1 => c.keyword, + 2, 3 => c.literal, + 4 => c.comment, + else => c.plain, + }; +} + +fn codeFace(class: u8) ui.Face { + return switch (class) { + 1 => .mono_bold, + 4 => .mono_italic, + else => .mono, + }; +} + +/// A run of a `bytes` row: what the bytes are, from the types of what the +/// program's pointers reach. +pub const Run = struct { + first: u8, + len: u8, + kind: enum { number, pointer, allocator, unread }, + /// Its mark: struck (heat), read (shows), held is in the heat. + heat: ui.Heat = .none, + read: f32 = 0, +}; + +/// `bytes`: a hexdump row of sixteen bytes at `addr`: a bracket at 7 +/// (`bracket`: quiet on a row that holds a drawn thing), the address in +/// region ink with `lead` at 18 (at 60% on a row that holds no drawn +/// thing), the bytes from 132 on a pitch of 30, 12 more after the +/// eighth; a field's bytes on raised paper in their ink (a pointer's in +/// the region it points into), an allocator's dim with no paper, never +/// read two dots a byte; a word's padding bytes faint. `reading` is drawn +/// by the caller at 640. +pub fn bytes(u: ui.Ui, y: f32, addr: u64, values: *const [16]u8, known: *const [16]bool, runs: []const Run, region: u24, lead: usize, holds_drawn: bool, moment: Recording.Moment) void { + const b = theme.metric.bytes; + const base = baseline(y); + _ = u.address(b.address, base, addr, .{ .ink = if (holds_drawn) ui.rgb(region) else ui.mix(region, theme.color.page, theme.metric.context.lead_alpha), .lead = lead }); + if (holds_drawn) u.bracket(b.bracket, y + 3, y + row_h - 3, ui.mix(region, theme.color.page, theme.metric.context.lead_alpha), theme.metric.bracket.quiet, 1, false); + for (runs) |run| { + const x0 = byteX(run.first) - 4; + const x1 = byteX(run.first + run.len - 1) + 28; + const r: Rect = .{ .x = x0, .y = y + 1, .w = x1 - x0, .h = row_h - 2 }; + const field = run.kind == .number or run.kind == .pointer; + if (field) u.fill(r, theme.color.raised); + // Marks per run, never per byte: struck (the paper solid), read (a + // frame 3 out), held (a tick in the gap at the run's left). + var face: ui.Face = .mono; + var struck_ink: ?ui.Color = null; + const h = run.heat; + const held = h.level * (1 - (if (h.level >= 1) h.solid * h.in else 0)); + if (held > 0) u.fill(.{ .x = x0 - 7, .y = y + 3, .w = m.tick, .h = row_h - 6 }, ui.mix(theme.color.page, theme.mark(.write).solid, held)); + if (h.arm > 0) u.faded(h.arm).frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = row_h + 2 }, theme.mark(.write).solid, 2); + const solid = if (h.level >= 1) h.solid * h.in else 0; + if (solid > 0) { + u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, ui.mix(theme.color.raised, theme.mark(.write).solid, solid)); + if (solid >= 0.5) { + face = .mono_bold; + struck_ink = ui.rgb(theme.mark(.write).on_solid); + } + } + if (run.read > 0) u.faded(run.read).frame(.{ .x = x0 - 3, .y = y - 1, .w = x1 - x0 + 6, .h = row_h + 2 }, theme.mark(.read).solid, 2); + // A little-endian word: the bytes above its highest non-zero one are its padding. + var top: usize = 0; + for (0..run.len) |j| if (values[run.first + j] != 0) { + top = j; + }; + const word: u64 = if (run.len == 8) std.mem.readInt(u64, values[run.first..][0..8], .little) else 0; + const run_ink: ui.Color = switch (run.kind) { + .number => ui.rgb(t.primary), + .pointer => ui.rgb(if (run.len == 8) ink(regionOf(moment, word)) else t.primary), + .allocator => ui.rgb(t.dim), + .unread => ui.rgb(t.faint), + }; + for (0..run.len) |j| { + const i = run.first + j; + const x = byteX(@intCast(i)); + if (run.kind == .unread or !known[i]) { + u.fill(.{ .x = x + 5, .y = y + 11, .w = 4, .h = 4 }, t.faint); + u.fill(.{ .x = x + 15, .y = y + 11, .w = 4, .h = 4 }, t.faint); + continue; + } + const c = struck_ink orelse if (j <= top) run_ink else ui.mix(run_ink, theme.color.page, theme.metric.context.pad_alpha); + u.text(face, size, x, base, u.print("{x:0>2}", .{values[i]}), c); + } + } +} + +/// Where byte `i` of a `bytes` row is. +pub fn byteX(i: u8) f32 { + const b = theme.metric.bytes; + return b.first + @as(f32, @floatFromInt(i)) * b.pitch + @as(f32, if (i >= 8) b.middle else 0); +} diff --git a/anim/src/source.zig b/anim/src/source.zig index 6c10b50..51059a7 100644 --- a/anim/src/source.zig +++ b/anim/src/source.zig @@ -1,57 +1,57 @@ -//! The source panel: the program's text as pardes colours it, line numbers, -//! the current mark (gold: a band along the line and a plaque over its -//! number), and the three lines before it (the trail, gold cooling). The -//! mark fires on the line about to run, and moves on last, when what the -//! line did has been seen: a slide to a line near by, a jump (out, then -//! in) into a call or out of one. -//! Reads `Moment.line` (through the recording's index of it). +//! `source` (design/README.md, "3. Panels"): a window on the whole file, +//! scrolled by whole lines to keep the current line in its middle third: +//! fourteen lines, eight beside a lens, ten when the lens is the subject. +//! Its rows are `line`s, pardes's colours; the current mark is a gold +//! plaque on the number and its ember across, the three lines before it +//! the trail. The mark fires on the line about to run and moves on last: +//! a slide to a line near by, a jump (out, then in) into a call or out +//! of one. Reads `Moment.line` (through the recording's index of it). const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const pardes = @import("pardes-syntax"); const ui = @import("ui.zig"); +const rows = @import("rows.zig"); +const context = @import("context.zig"); +const ties = @import("ties.zig"); const cursors = @import("cursor.zig"); +const Stage = @import("stage.zig").Stage; const Cursor = cursors.Cursor; const theme = ui.theme; const Rect = ui.Rect; +const m = theme.metric.context; const gpa = std.heap.smp_allocator; pub const Source = struct { cursor: Cursor, - box: Rect, + stage: *const Stage, focus: bool = false, - pub fn draw(p: *const Source, f: *sk.Frame, at: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = at }; + pub fn draw(p: *const Source, f: *sk.Frame, placed: anim.Placed) void { + const box = p.stage.box(placed, .source); + const u = context.at(f, placed, box); const rec = p.cursor.rec; - const now = p.cursor.now(at); - const size = theme.size.mono.context; - const adv = ui.advance(size); + const now = p.cursor.now(placed); const text = prepared(rec.source, rec.file) orelse return; - const fit: u32 = @intFromFloat(@floor((p.box.h - m.tag - 2 * m.pad) / m.row)); + const fit: u32 = @intCast(theme.layout.rowsIn(box.h)); // The mark: leaving the line of one moment for that of the next. const was = rec.own[now.marks[0]]; const here = rec.own[now.marks[1]]; - const on_line = rec.own[now.marked()]; // The window: where the line it is on has it, and (as the mark // moves on) gliding to where the next one does. It never cuts. const going = now.advancing(); const windows = [2]f32{ @floatFromInt(rec.viewFirst(fit, was)), @floatFromInt(rec.viewFirst(fit, here)) }; const first = std.math.lerp(windows[0], windows[1], cursors.glide(going)); const from: u32 = @intFromFloat(@floor(first)); - const last: u32 = @min(from + fit, rec.line_count); - const body = u.panel(p.box.w, p.box.h, rec.file, .{ .focus = p.focus, .note = if (rec.line_count > fit) u.print("lines {d} to {d} of {d}", .{ from, @min(from + fit - 1, rec.line_count), rec.line_count }) else u.print("{d} lines", .{rec.line_count}) }); + const last: u32 = @min(from + @as(u32, @intCast(context.rowsOf(box))), rec.line_count); + const shown_first: u32 = @intFromFloat(windows[if (going >= 0.5) 1 else 0]); + const body = u.panel(box.w, box.h, rec.file, .{ .focus = p.focus, .note = if (rec.line_count > fit) u.print("lines {d} to {d} of {d}", .{ shown_first, @min(shown_first + fit - 1, rec.line_count), rec.line_count }) else u.print("{d} lines", .{rec.line_count}) }); const top = body.y + m.pad; - const gutter = 3 * adv + 22; - const code_x = gutter + 12; - const max_chars: usize = @intFromFloat(@floor((p.box.w - code_x - m.side) / adv)); // Where the mark is, and how much of it. - const current = theme.mark(.current); const travel = now.plan.travel; const moved = going * travel; - const scrolls = windows[0] != windows[1]; var mark_line = here; var mark_at: f32 = @floatFromInt(here); var shows: f32 = 1; @@ -73,11 +73,15 @@ pub const Source = struct { } } const mark_y = top + (mark_at - first) * m.row; - u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = p.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); - const marked = u.faded(shows); - if (shows > 0) marked.fill(.{ .x = theme.stroke.rule, .y = mark_y, .w = p.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); + context.clipBody(u, box); + defer u.unclip(); + // The plaque over the number. Fire: on the line about to run its + // edges go out and come back, once, and nothing else moves. + if (shows > 0) u.faded(shows).current(.{ .x = theme.stroke.rule, .y = mark_y, .w = box.w - 2 * theme.stroke.rule, .h = m.row }, theme.metric.plaque.line, now.fires(.source)); + if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= box.h) ties.anchor(u, .source, .{ .x = 0, .y = mark_y, .w = box.w, .h = m.row }); // The trail: the last three other lines it was on. It shifts without animation. + const on_line = rec.own[now.marked()]; var trail: [3]u32 = @splat(0); var found: usize = 0; var back = now.marked(); @@ -88,62 +92,19 @@ pub const Source = struct { trail[found] = l; found += 1; } - for (trail[0..found], 0..) |l, k| u.fill(.{ .x = theme.stroke.rule, .y = top + (@as(f32, @floatFromInt(l)) - first) * m.row + 2, .w = 4, .h = m.row - 4 }, theme.color.trail[k]); - - // The plaque over the number. Fire: on the line about to run its - // edges go out and come back, once, and nothing else moves. - const out = now.fires(.source); - if (shows > 0) marked.fill(.{ .x = theme.stroke.rule - out, .y = mark_y - out, .w = gutter - 2 + 2 * out, .h = m.row + 2 * out }, current.solid); - if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= body.y + body.h) @import("ties.zig").anchor(u, .source, .{ .x = 0, .y = mark_y, .w = gutter, .h = m.row }); var line = @max(from, 1); while (line <= last) : (line += 1) { const y = top + (@as(f32, @floatFromInt(line)) - first) * m.row; - const row: Rect = .{ .x = 0, .y = y, .w = gutter - 10, .h = m.row }; - // The number under the plaque is the one in ink. - const on = shows >= 0.5 and line == mark_line and !(scrolls and going > 0 and going < 1 and line != here); - u.label(if (on) .mono_bold else .mono, size, row, .right, u.print("{d}", .{line}), if (on) current.on_solid else theme.color.text.faint); const span = text.lines[line - 1]; - const bytes = text.bytes[span[0]..span[1]]; - const styles = text.styles[span[0]..span[1]]; - // Runs of one class, each set where its column is. - var i: usize = 0; - const end = @min(bytes.len, max_chars); - while (i < end) { - var j = i + 1; - while (j < end and styles[j] == styles[i]) j += 1; - const class: pardes.Syn = @enumFromInt(styles[i]); - const x = code_x + @as(f32, @floatFromInt(i)) * adv; - if (std.mem.indexOfNone(u8, bytes[i..j], " ") != null) u.text(face(class), size, x, theme.baseline(y, m.row, size), bytes[i..j], ink(class)); - i = j; - } - // A line longer than the panel is cut, and says so. - if (bytes.len > max_chars) u.text(.mono, size, code_x + @as(f32, @floatFromInt(max_chars)) * adv, theme.baseline(y, m.row, size), "\u{2026}", theme.color.text.faint); + const k = std.mem.indexOfScalar(u32, trail[0..found], line); + const on = shows >= 0.5 and line == mark_line; + rows.line(u, y, box.w, line, text.bytes[span[0]..span[1]], text.styles[span[0]..span[1]], if (on) .marked else if (k) |n| .{ .trail = n } else .plain); } - u.unclip(); + context.veiled(u, box, now.veil); } }; -const m = theme.metric.context; - -fn ink(class: pardes.Syn) u24 { - const c = theme.color.code; - return switch (class) { - .none => c.plain, - .keyword => c.keyword, - .string, .number => c.literal, - .comment => c.comment, - }; -} - -fn face(class: pardes.Syn) ui.Face { - return switch (class) { - .keyword => .mono_bold, - .comment => .mono_italic, - else => .mono, - }; -} - /// A source ready to draw: a class for each byte, and where each line is. const Text = struct { bytes: []const u8, @@ -190,4 +151,7 @@ test "a source is cut into lines and classed byte by byte" { try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @enumFromInt(text.styles[at]))); try std.testing.expectEqual(pardes.Syn.number, @as(pardes.Syn, @enumFromInt(text.styles[at + 7]))); try std.testing.expectEqual(pardes.Syn.comment, @as(pardes.Syn, @enumFromInt(text.styles[at + 10]))); + // rows.line reads these classes by number. + try std.testing.expectEqual(@as(u8, 1), @intFromEnum(pardes.Syn.keyword)); + try std.testing.expectEqual(@as(u8, 4), @intFromEnum(pardes.Syn.comment)); } diff --git a/anim/src/stage.zig b/anim/src/stage.zig new file mode 100644 index 0000000..31a149a --- /dev/null +++ b/anim/src/stage.zig @@ -0,0 +1,84 @@ +//! The `stage` (design/README.md, "5. The frame"): where every panel is, +//! by the scene's split, and `relayout`, the one move of the layout: a +//! change of split, 48 f on `glide`, with nothing else moving under it. +//! Every panel of the context reads its box here each frame, so a +//! relayout moves them all together. +const std = @import("std"); +const anim = @import("anim"); +const ui = @import("ui.zig"); +const theme = ui.theme; +const Split = theme.layout.Split; +const Rect = theme.Rect; + +pub const Part = enum { band, source, disasm, registers, stack, backtrace }; + +pub const Stage = struct { + first: Split, + moves: [8]Move = undefined, + count: usize = 0, + + const Move = struct { from: Split, to: Split, p: anim.Value }; + + /// A stage at `split`, made where a scene's components can keep a pointer to it. + pub fn init(s: *anim.Frame, split: Split) *Stage { + return s.new(Stage{ .first = split }); + } + + /// The split the stage ends in, after every move so far. + pub fn last(st: *const Stage) Split { + return if (st.count == 0) st.first else st.moves[st.count - 1].to; + } + + /// A change of split: every panel glides to its place in `to`. + pub fn relayout(st: *Stage, s: *anim.Frame, to: Split) void { + const p = s.value(0); + st.moves[st.count] = .{ .from = st.last(), .to = to, .p = p }; + st.count += 1; + s.play(.{p.to(1)}, .{ .seconds = theme.time.relayout, .ease = anim.ease.linear }); + } + + /// The split as it stands at this instant. + pub fn now(st: *const Stage, at: anim.Placed) Split { + var split = st.first; + for (st.moves[0..st.count]) |m| { + const p = at.value(m.p); + if (p <= 0) continue; + split = between(m.from, m.to, theme.ease(.glide, p)); + } + return split; + } + + /// Whether a relayout is under way: nothing else moves under it. + pub fn moving(st: *const Stage, at: anim.Placed) bool { + for (st.moves[0..st.count]) |m| { + const p = at.value(m.p); + if (p > 0 and p < 1) return true; + } + return false; + } + + pub fn box(st: *const Stage, at: anim.Placed, part: Part) Rect { + const split = st.now(at); + return switch (part) { + inline else => |tag| @field(split, @tagName(tag)), + }; + } +}; + +fn between(a: Split, b: Split, p: f32) Split { + var out = b; + inline for (.{ "band", "source", "disasm", "registers", "stack", "backtrace" }) |name| { + const x = @field(a, name); + const y = @field(b, name); + @field(out, name) = .{ .x = std.math.lerp(x.x, y.x, p), .y = std.math.lerp(x.y, y.y, p), .w = std.math.lerp(x.w, y.w, p), .h = std.math.lerp(x.h, y.h, p) }; + } + out.dump_rows = if (p < 0.5) a.dump_rows else b.dump_rows; + return out; +} + +test "a relayout passes through the splits it joins" { + const sp = theme.layout.split; + const half = between(sp.beside, sp.subject, 0.5); + try std.testing.expectEqual(@as(f32, 200), half.band.h); + try std.testing.expectEqual(sp.subject.stack, between(sp.beside, sp.subject, 1).stack); +} diff --git a/anim/src/syscalls.zig b/anim/src/syscalls.zig index 9136ebb..a19324f 100644 --- a/anim/src/syscalls.zig +++ b/anim/src/syscalls.zig @@ -13,6 +13,8 @@ const zt = @import("ztrace"); const ui = @import("ui.zig"); const cursors = @import("cursor.zig"); const Recording = @import("Recording.zig"); +const context = @import("context.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; @@ -135,24 +137,28 @@ pub fn fills(call: Recording.Syscall) ?Recording.Span { /// side and returns. pub const Crossing = struct { cursor: cursors.Cursor, - box: Rect = theme.layout.split.beside.lens, + stage: *const Stage, focus: bool = false, const row_h = theme.metric.context.row; const size = theme.size.mono.context; pub fn draw(c: *const Crossing, f: *sk.Frame, placed: anim.Placed) void { - const u: ui.Ui = .{ .f = f, .at = placed }; + const box = c.stage.box(placed, .band); + if (box.h < theme.metric.context.tag + 24) return; + const u = context.at(f, placed, box); const now = c.cursor.now(placed); const rec = c.cursor.rec; - const body = u.panel(c.box.w, c.box.h, "kernel", .{ .focus = c.focus, .note = "the other side of a system call" }); + const body = u.panel(box.w, box.h, "kernel", .{ .focus = c.focus, .note = "the other side of a system call" }); u.fill(.{ .x = theme.stroke.rule, .y = body.y, .w = body.w - 2 * theme.stroke.rule, .h = body.h - theme.stroke.rule }, theme.color.kernel); + context.clipBody(u, box); + defer u.unclip(); u.text(.sans, theme.size.sans.micro, 12, body.y + 22, "calls so far", k.dim); const newest = latest(rec, now.stops[0]) orelse return; const live = rec.syscalls[newest].enter > now.stops[1] and now.walking(); const p = Plan.of(rec.syscalls[newest]); const t: f32 = if (live) now.time else std.math.inf(f32); - if (t >= p.enters) c.arrows(u, body, t, p, rec.syscalls[newest]); + if (t >= p.enters) arrows(u, body, t, p, rec.syscalls[newest]); // No effect before its cause: until the arrow is through the // line the kernel knows nothing of this call, and shows the last. if (t < p.name) { @@ -243,32 +249,31 @@ pub const Crossing = struct { /// The crossing itself: in from the side the registers are on as the /// call enters, and back out as it returns. Vellum outside the /// kernel, ink inside it. Machines do not lob: straight. - fn arrows(c: *const Crossing, u: ui.Ui, body: Rect, t: f32, p: Plan, made: Recording.Syscall) void { - const edge = c.box.w; + /// The crossing itself: up out of the registers below the band (the + /// ones that carried it), into the kernel, as the call enters; and back + /// down as it returns. Machines do not lob: straight. + fn arrows(u: ui.Ui, body: Rect, t: f32, p: Plan, made: Recording.Syscall) void { + const x = theme.layout.state.x - theme.layout.program.x + 40; + const bottom = body.y + body.h; + const reach = 64; // Off promptly, and settling in the kernel. - const down = cursors.standard(std.math.clamp((t - p.enters) / Plan.cross, 0, 1)); + const up = cursors.standard(std.math.clamp((t - p.enters) / Plan.cross, 0, 1)); const gone = std.math.clamp((t - p.returns - theme.time.quick) / theme.time.read.out, 0, 1); const v = u.faded(1 - gone); - if (t < p.back) across(v, body.y + 190, edge + 26, edge + 26 - down * 86, edge); - if (made.exit != null and t > p.back) across(v, body.y + 226, edge - 60, edge - 60 + cursors.standard(std.math.clamp((t - p.back) / Plan.cross, 0, 1)) * 86, edge); + if (t < p.back) along(v, x, bottom, bottom - up * reach); + if (made.exit != null and t > p.back) along(v, x + 40, bottom - reach, bottom - reach + cursors.standard(std.math.clamp((t - p.back) / Plan.cross, 0, 1)) * reach); } - /// An arrow along the row at `y` from `from` to `to`, in the colour of - /// the ground it is over: ink inside the kernel (left of `edge`), vellum outside. - fn across(u: ui.Ui, y: f32, from: f32, to: f32, edge: f32) void { + /// An arrow down the column at `x` from `from` to `to`, in ink: it is inside the kernel. + fn along(u: ui.Ui, x: f32, from: f32, to: f32) void { if (@abs(to - from) < 1) return; const w = theme.stroke.arrow; const head = theme.stroke.flow_head; const dir: f32 = if (to > from) 1 else -1; const neck = to - dir * (head[0] - 2); - const lo = @min(from, neck); - const hi = @max(from, neck); - if ((neck - from) * dir > 0) { - if (lo < edge) u.fill(.{ .x = lo, .y = y - w / 2, .w = @min(hi, edge) - lo, .h = w }, k.ink); - if (hi > edge) u.fill(.{ .x = @max(lo, edge), .y = y - w / 2, .w = hi - @max(lo, edge), .h = w }, theme.color.kernel); - } + if ((neck - from) * dir > 0) u.fill(.{ .x = x - w / 2, .y = @min(from, neck), .w = w, .h = @abs(neck - from) }, k.ink); if (@abs(to - from) < head[0]) return; - sk.draw.polygon(u.f, &.{ .{ to, y }, .{ to - dir * head[0], y - head[1] / 2 }, .{ to - dir * head[0], y + head[1] / 2 } }, u.at.pen(.{ .fill = if (to - dir * head[0] / 2 < edge) ui.rgb(k.ink) else ui.rgb(theme.color.kernel) })); + sk.draw.polygon(u.f, &.{ .{ x, to }, .{ x - head[1] / 2, to - dir * head[0] }, .{ x + head[1] / 2, to - dir * head[0] } }, u.at.pen(.{ .fill = ui.rgb(k.ink) })); } }; @@ -304,13 +309,9 @@ fn value(u: ui.Ui, made: Recording.Syscall, kind: u8, arg: usize, text_used: *bo }, else => {}, } - return grouped(u, a); + return u.print("{x}", .{a}); } -/// Hex in groups of four, as everywhere. -fn grouped(u: ui.Ui, n: u64) []const u8 { - return ui.grouped(u.f.scratch.alloc(u8, 20) catch return "?", n); -} /// What came back: a count, an address, or an error by name. fn result(u: ui.Ui, made: Recording.Syscall) []const u8 { @@ -319,7 +320,7 @@ fn result(u: ui.Ui, made: Recording.Syscall) []const u8 { if (std.enums.fromInt(std.os.linux.E, -ret)) |e| return u.print("{d} E{t}", .{ ret, e }); return u.print("{d}", .{ret}); } - return if (ret > 0xffffff or ret < 0) grouped(u, made.result) else u.print("{d}", .{ret}); + return if (ret > 0xffffff or ret < 0) u.print("{x}", .{made.result}) else u.print("{d}", .{ret}); } pub const Timeline = struct { diff --git a/anim/src/theme.zig b/anim/src/theme.zig index 9b18e59..ac01bd7 100644 --- a/anim/src/theme.zig +++ b/anim/src/theme.zig @@ -59,41 +59,51 @@ pub fn cols(first: u8, n: u8, y: f32, h: f32) Rect { return .{ .x = colX(first), .y = canvas.stage.y + y, .w = span(n), .h = h }; } -/// How views share the stage. The order in each is the order of abstraction: -/// source is leftmost, then the CPU, then memory; a lower level is never left -/// of or above a higher one, and the kernel is always underneath. +/// The stage (design/README.md, "5. The frame"): the lens band across the +/// top, and under it two columns. The program column (left): the source +/// over the disassembly, what was written over what runs. The state column +/// (right): the registers over the stack, what they act on. The split is +/// the one thing about the layout a scene chooses. pub const layout = struct { - /// Two columns of 880. The program column: the lens, the source, the - /// disassembly, top to bottom (the idea, its text, its instructions). - /// The state column: what they act on, the same in every frame of - /// every scene. + /// Two columns of 880, at x 64 and 976; the gutter between them belongs to the ties. pub const program = cols(0, 6, 0, canvas.stage.h); pub const state = cols(6, 6, 0, canvas.stage.h); - /// The state column: every register (sixteen, rip, the flags), eight - /// words of stack, three frames of backtrace. - pub const registers = rows(state, 0, 18); - pub const stack = below(registers, 8); - pub const backtrace = below(stack, 3); - - /// How the program column is shared: the one thing about the layout a - /// scene chooses. `lens` has no height where there is none. - pub const Split = struct { lens: Rect, source: Rect, disasm: Rect }; + /// Where every panel is in one split. `band` (the lens: a drawing and + /// the bytes it is drawn from, across both columns) and `backtrace` + /// have no height where the split has none. + pub const Split = struct { + band: Rect, + source: Rect, + disasm: Rect, + registers: Rect, + stack: Rect, + backtrace: Rect, + /// `bytes` rows of the lens's dump. + dump_rows: u32, + }; pub const split = struct { - /// The machine alone: the source over the disassembly, eight - /// instructions above the pc and eight below. - pub const none: Split = .{ .lens = .{ .x = program.x, .y = program.y, .w = program.w, .h = 0 }, .source = rows(program, 0, 14), .disasm = below(rows(program, 0, 14), 17) }; - /// The default: the machine is the subject, the lens beside it. - /// (The user's direction after the design's revision: more of the - /// disassembly, so the source's window is shorter than the design's.) - pub const beside: Split = shared(312, 8, 10); - /// The idea is the subject: the lens a size up. - pub const subject: Split = shared(432, 5, 8); + /// The machine alone: fourteen lines over seventeen instructions; + /// the registers, eight words and four frames of backtrace. + pub const none: Split = blk: { + const registers = rows(state, 0, 17); + const stack = below(registers, 8); + break :blk .{ .band = .{ .x = canvas.stage.x, .y = canvas.stage.y, .w = canvas.stage.w, .h = 0 }, .source = rows(program, 0, 14), .disasm = below(rows(program, 0, 14), 17), .registers = registers, .stack = stack, .backtrace = below(stack, 4), .dump_rows = 0 }; + }; + /// The machine is the subject and the lens beside it: a band of + /// 152 and a dump of five rows; eight lines over seventeen + /// instructions; the registers over eight words. + pub const beside: Split = shared(152, 8, 17, 8, 5); + /// The idea is the subject: a band of 248, the drawing a size up + /// and a dump of nine; ten lines over eleven instructions; four words. + pub const subject: Split = shared(248, 10, 11, 4, 9); - fn shared(lens_h: f32, source_rows: f32, disasm_rows: f32) Split { - const lens: Rect = .{ .x = program.x, .y = program.y, .w = program.w, .h = lens_h }; - const source: Rect = .{ .x = program.x, .y = lens.y + lens.h + canvas.row_gap, .w = program.w, .h = panelH(source_rows) }; - return .{ .lens = lens, .source = source, .disasm = below(source, disasm_rows) }; + fn shared(band_h: f32, source_rows: f32, disasm_rows: f32, slots: f32, dump_rows: u32) Split { + const band: Rect = .{ .x = canvas.stage.x, .y = canvas.stage.y, .w = canvas.stage.w, .h = band_h }; + const top = band.y + band.h + canvas.row_gap; + const source: Rect = .{ .x = program.x, .y = top, .w = program.w, .h = panelH(source_rows) }; + const registers: Rect = .{ .x = state.x, .y = top, .w = state.w, .h = panelH(17) }; + return .{ .band = band, .source = source, .disasm = below(source, disasm_rows), .registers = registers, .stack = below(registers, slots), .backtrace = .{ .x = state.x, .y = canvas.stage.y + canvas.stage.h, .w = state.w, .h = 0 }, .dump_rows = dump_rows }; } }; @@ -102,6 +112,11 @@ pub const layout = struct { return metric.context.tag + 2 * metric.context.pad + n * metric.context.row; } + /// How many rows a panel `h` high holds. + pub fn rowsIn(h: f32) usize { + return @intFromFloat(@max(@floor((h - metric.context.tag - 2 * metric.context.pad) / metric.context.row), 0)); + } + fn rows(column: Rect, y: f32, n: f32) Rect { return .{ .x = column.x, .y = column.y + y, .w = column.w, .h = panelH(n) }; } @@ -197,9 +212,14 @@ pub const color = struct { 0x2f5fc4, // azurite }; - /// Drawn over everything that is not the subject, to dim it. + /// A node's pointer half. + pub const well: u24 = 0x101a48; + + /// Drawn over everything that is not the subject, to dim it: for a + /// beat, and (deeper) under the camera. pub const veil: u24 = page; - pub const veil_alpha: f32 = 0.65; + pub const veil_alpha: f32 = 0.50; + pub const veil_alpha_camera: f32 = 0.65; }; /// What a mark means. The same everywhere: a byte, a register, a source line, @@ -211,8 +231,9 @@ pub const Role = enum { write, /// Read by this step. read, - /// Holds an address, and the arrow to it. - pointer, + /// This register now points at that: a line 3 wide in the ink of the + /// region pointed into, ending in the target's pin. + link, /// Handed out by an allocator. allocated, /// Handed back. No colour: its bytes are still there, and nobody owns them. @@ -237,8 +258,8 @@ pub const Form = enum { fill, /// A frame, with its ember inside. frame, - /// A frame round the bytes and an arrow from it, elbows filleted, head filled. - arrow, + /// A line along the ties' channels, corners filleted, head filled, and a pin at its end. + line, /// Its ember inside a solid frame. block, /// Nothing inside a dashed frame; hatched where there is room. @@ -271,7 +292,8 @@ pub fn mark(role: Role) Mark { .current => .{ .solid = 0xe8c46a, .ember = 0x4a3d1c, .on_solid = t.ink, .form = .plaque }, .write => .{ .solid = 0xff6a4a, .ember = 0x5a2218, .on_solid = t.ink, .form = .fill }, .read => .{ .solid = 0x6fd6e6, .ember = 0x1c4652, .on_solid = t.ink, .form = .frame }, - .pointer => .{ .solid = 0xd59cf5, .ember = 0x45305a, .on_solid = t.ink, .form = .arrow }, + // Its colour is the region's it points into (`color.region`); this is heap's. + .link => .{ .solid = color.region.heap, .ember = color.region.heap, .on_solid = t.ink, .form = .line }, .allocated => .{ .solid = 0x7fdc9a, .ember = 0x1f4a33, .on_solid = t.ink, .form = .block }, // The dashed frame, and the hatch. .freed => .{ .solid = t.dim, .ember = color.rule, .on_solid = t.ink, .form = .hollow }, @@ -284,7 +306,7 @@ pub fn mark(role: Role) Mark { }; } -pub const roles = [_]Role{ .current, .write, .read, .pointer, .allocated, .freed, .fault, .attention, .flow, .unknown, .kernel }; +pub const roles = [_]Role{ .current, .write, .read, .link, .allocated, .freed, .fault, .attention, .flow, .unknown, .kernel }; /// WCAG relative luminance of 0xRRGGBB. pub fn luminance(rgb: u24) f64 { @@ -307,17 +329,20 @@ pub fn contrast(a: u24, b: u24) f64 { // ------------------------------------------------------------------ type -/// The Go fonts (Bigelow & Holmes; BSD-3-Clause, the Go licence), from -/// golang.org/x/image/font/gofont/ttfs or the `ttf-go` package. Mono is for -/// what the machine holds, sans for what is said about it. +/// Mono is for what the machine holds: JetBrains Mono NL (OFL; NL, because +/// the plain family's ligatures turn `->` into a sign that is not in the +/// source). Sans is for what is said about it: the Go fonts (Bigelow & +/// Holmes; BSD-3-Clause). `ui.Ui.text` sets a mono face by its size. pub const Face = struct { family: []const u8, file: []const u8 }; pub const font = struct { - /// Data and code. The renderer's built-in face. - pub const mono: Face = .{ .family = "Go Mono", .file = "Go-Mono.ttf" }; - /// Keywords, a struck value, the letters on a plaque. - pub const mono_bold: Face = .{ .family = "Go Mono", .file = "Go-Mono-Bold.ttf" }; + /// Mono at 30 and over. + pub const mono: Face = .{ .family = "JetBrains Mono NL", .file = "JetBrainsMonoNL-Regular.ttf" }; + /// All mono under 30: thin strokes on a dark ground die first in a stream. + pub const mono_medium: Face = .{ .family = "JetBrains Mono NL Medium", .file = "JetBrainsMonoNL-Medium.ttf" }; + /// Keywords, mnemonics, a struck value, plaques, pins. + pub const mono_bold: Face = .{ .family = "JetBrains Mono NL", .file = "JetBrainsMonoNL-Bold.ttf" }; /// Comments. - pub const mono_italic: Face = .{ .family = "Go Mono", .file = "Go-Mono-Italic.ttf" }; + pub const mono_italic: Face = .{ .family = "JetBrains Mono NL", .file = "JetBrainsMonoNL-Italic.ttf" }; /// Captions, notes. pub const sans: Face = .{ .family = "Go", .file = "Go-Regular.ttf" }; /// Tags, labels, the running head. @@ -327,8 +352,8 @@ pub const font = struct { }; pub const size = struct { - /// Mono sizes are multiples of 5: Go Mono advances 0.6 em, so a character - /// is a whole number of units (15, 18, 21, 27, 36), a hex pair is 36, and + /// Mono sizes are multiples of 5: the mono face advances 0.6 em, so a + /// character is a whole number of units (12, 15, 18, 21, 27, 36), and /// the layout is the same arithmetic at 1080p and at 4K. pub const mono = struct { /// The debugger's context, where a lot is on screen at once: @@ -339,9 +364,9 @@ pub const size = struct { /// Bytes, words, registers, code. The smallest size for anything the /// narration depends on; nothing mono that must be read is smaller. pub const data = 30; - /// Values in nodes and chunks. + /// Code inside a caption. pub const value = 35; - /// A value in flight. + /// A value in flight; a subject lens's values. pub const flight = 45; /// Values in hero cells, a system call's name. pub const hero = 60; @@ -370,20 +395,13 @@ pub const size = struct { pub const min_any = 18; }; -/// Width of one Go Mono character at a mono size. +/// Width of one mono character at a mono size: JetBrains Mono advances 0.6 +/// em, a whole number of units at every size here (20: 12). Hex is +/// unbroken, lower case, no `0x` on an address: a digit is an advance. pub fn monoAdvance(mono_size: u16) u16 { return mono_size * 3 / 5; } -/// Hex is set in groups of four digits, half a character apart, lower case, -/// no `0x`. A register is 16 digits, an address 12 (or its last 8 in a narrow -/// panel whose tag gives the rest); leading zeros are `text.faint`, and in an -/// address gutter only the last group is `text.primary`. -pub fn hexWidth(digits: u16, mono_size: u16) u16 { - const adv = monoAdvance(mono_size); - return digits * adv + (digits / 4 - 1) * ((adv + 1) / 2); -} - /// Baseline that centres capitals and digits of `text_size` in a box at `y`, `h` tall. pub fn baseline(y: f32, h: f32, text_size: f32) f32 { return y + h / 2 + 0.36 * text_size; @@ -417,140 +435,153 @@ pub const stroke = struct { pub const hazard = 12; /// Elision marks and the dots of an unknown byte: squares this big. pub const dot = 6; + /// The ties: the thread and a link 3, a projection 2. + pub const tie = struct { + pub const thread = 3; + pub const link = 3; + pub const projection = 2; + }; }; // ------------------------------------------------------------ components pub const metric = struct { - /// A panel is an acme window: a tag with a square box, a rule, a body. + /// A panel of a card (the title, the legend): the first pass's sizes. pub const panel = struct { - pub const tag_h = 48; - /// The box: filled `current` in the panel the narration is in, hollow elsewhere. - pub const box = 20; - pub const box_inset = 14; - pub const title_x = 48; - pub const title_baseline = 34; pub const pad_x = 20; - pub const pad_y = 16; }; - /// One byte: two hex digits in a 46 square on a 48 pitch. - pub const byte = struct { - pub const pitch = 48; - pub const side = 46; - /// A character over its byte, for strings: this tall. - pub const text_h = 84; - }; - /// Memory rows, bytes or words: 60 apart, which leaves room for a pointer's frame. - pub const row_pitch = 60; - /// A word is drawn on the footprint of its bytes (8 of them: 382 wide), so - /// bytes and their number can change into each other in place. - pub const pointer_frame_outset = 6; - /// The bar along the bottom of a cooled change. - pub const ember_bar = 6; - /// Bytes not shown: three dots and a note, this tall. - pub const elision_h = 24; - /// Left of an address gutter: register and variable marks that point at the row. - pub const mark_lane = 88; - /// Between gutter and bytes: where pointer arrows run. - pub const arrow_lane = 52; - - /// Registers, and any table of names and values. - pub const table = struct { - pub const row_h = 48; - pub const name_x = 18; - /// The value cell overhangs its digits by this on each side. - pub const value_pad = 10; - pub const flag = [2]f32{ 60, 36 }; - pub const flag_gap = 12; - }; - /// Shown unless a scene says otherwise, in this order: where it is, the - /// result, the arguments, the frame. Any other register appears when a - /// step touches it and leaves once its ember has gone. - pub const default_registers = [_][]const u8{ "rip", "rax", "rdi", "rsi", "rdx", "rcx", "rsp", "rbp" }; - pub const default_flags = [_][]const u8{ "CF", "ZF", "SF", "OF" }; - /// The registers' rows, in pwndbg's order: sixteen, rip, the flags. - pub const all_registers = [_][]const u8{ "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rip", "flags" }; - - /// The context: every panel of it is rows of 24 under a tag of 24. + /// The context: every panel of it is rows of 24 under a tag of 24 + /// (`tag`, design/README.md), 4 of padding above and below the rows, + /// 12 at the sides. pub const context = struct { pub const row = 24; pub const tag = 24; - /// Above and below the rows; at a panel's sides. - pub const pad = 6; + pub const pad = 4; pub const side = 12; - /// A register's name: five characters. Its value starts here. - pub const name_w = 60; + /// `reg`: a register's name 12 in, its chain from 72. pub const chain_x = 72; - /// Both arrows of a chain: a 2-unit shaft, a head 8 by 10. + /// `arrow`: 30 wide, a 2-unit shaft, a head 8 by 10. pub const arrow_w = 30; pub const arrow_head = [2]f32{ 8, 10 }; - /// Between groups of four hex digits: half an advance. - pub const group_gap = 6; - /// Changed a moment ago: a tick at the value's left. + /// `address` `lead`: the digits every row of a column shares, this + /// far toward the page; `byte` `pad`: a word's bytes above its + /// highest non-zero one, this far. + pub const lead_alpha = 0.6; + pub const pad_alpha = 0.45; + /// `held`: a tick 4 wide, the row less 6 tall, at the value's left. pub const tick = 4; + /// `read`: a 2-unit frame, 6 out at the sides. pub const read_frame = 2; + /// `struck`: solid 5 wider each side; its edges pulse out 3. + pub const strike_out = 5; pub const pulse_outset = 3; + /// A string is never cut shorter than this. pub const string_min = 6; + /// `line`: the number ranged right in three characters, the text from 64, 67 characters. + pub const line_number_end = 46; + pub const line_text = 64; pub const source_chars = 67; }; - pub const code = struct { - pub const line_h = 48; - /// Line numbers: three digits and this, then the code. - pub const gutter_pad = 16; - pub const text_gap = 20; - /// Characters a panel of n columns holds at `size.mono.data`: write the - /// C for a video to fit the panel it will be shown in. - pub const chars_in_5_columns = 34; - pub const chars_in_6_columns = 42; - pub const chars_in_7_columns = 51; - /// The trail bar at the left edge, and the instruction marker in an unfolded line. - pub const trail_bar = 6; - pub const unfold_indent = 36; - pub const instruction_bar = 8; + /// `insn`: columns, from the panel's left edge. + pub const insn = struct { + pub const mark = 2; + pub const jump_lane = 31; + pub const address = 40; + pub const line = 134; + pub const mnemonic = 180; + pub const operands = 264; + pub const annotation = 564; + pub const operand_chars = 24; + pub const annotation_chars = 25; + }; + + /// `bytes`: a hexdump row, from its panel's left edge (the band's right half). + pub const bytes = struct { + pub const bracket = 7; + pub const address = 18; + pub const first = 132; + /// Two digits in 24 units on a pitch of 30, 12 more after the eighth. + pub const pitch = 30; + pub const middle = 12; + pub const reading = 640; + }; + + /// `slot`: a lane for `row` pins, the chain from 122, room to 136 from + /// the right edge (which belongs to the `call` spans, 14 in). + pub const slot = struct { + pub const lane = 10; + pub const lane_end = 112; + pub const chain = 122; + pub const room = 136; + pub const span = 14; + }; + + /// `reg` rip: the flags at the right end, eight names on a pitch of 42, + /// a set one on a chip 32 by 20. + pub const flags = struct { + pub const pitch = 42; + pub const chip = [2]f32{ 32, 20 }; }; - /// Call stack cards, newest on top. - pub const card = struct { - pub const h = 56; - pub const gap = 12; - pub const spine = 12; + /// `pin`. `row`: the names, 5, a head 9 by 12. `under`: a stub 3 wide, + /// the mono size and 10 long, a head 10 by 10, the names 9 either side. + pub const pin = struct { + pub const row_head = [2]f32{ 9, 12 }; + pub const row_gap = 5; + pub const stub = 3; + pub const under_head = [2]f32{ 10, 10 }; + pub const under_names = 9; }; - /// A linked-list node: value, then a pointer compartment. + + /// `bracket`: a stroke and a serif at each end. `quiet` 2 and 5; `tie` 3 and 6 (the thread's 7). + pub const bracket = struct { + pub const quiet = [2]f32{ 2, 5 }; + pub const tie = [2]f32{ 3, 6 }; + pub const thread = [2]f32{ 3, 7 }; + }; + + /// `plaque`, gold only: the row high, this wide. + pub const plaque = struct { + pub const rip = 54; + pub const line = 50; + pub const pc = 24; + }; + + /// `nodes`: boxes 116 by 44, 28 apart beside the context; 124 by 72, + /// 22 apart as the subject. The pointer half is 48 wide, its square 10. pub const node = struct { - pub const w = 180; - pub const h = 72; - pub const pointer_w = 72; - /// The square in the pointer compartment that its arrow leaves from. - pub const origin = 14; + pub const beside = [3]f32{ 116, 44, 28 }; + pub const subject = [3]f32{ 124, 72, 22 }; + pub const pointer_w = 48; + pub const origin = 10; + pub const arrow_head = [2]f32{ 12, 10 }; }; - /// Tree and graph nodes: circles; the tree's levels this far apart. - pub const tree = struct { - pub const radius = 24; - pub const radius_solo = 36; - pub const level = 54; - pub const badge = 28; + + /// A lens's lanes: projections 4 under its tag, links 8 above its bottom edge. + pub const lens = struct { + pub const top_lane = 4; + pub const bottom_lane = 8; }; - /// Array cells: in a panel among others, and as the subject. + + /// The ties' channels: the thread in the left margin, the gutter's two + /// lanes (a link comes up the left, a projection down the right). + pub const tie = struct { + pub const thread_x = 54; + pub const link_lane = 950; + pub const projection_lane = 962; + pub const radius = 8; + pub const projection_radius = 6; + }; + + /// A lens's cell, as the first pass drew them. + pub const ember_bar = 4; + pub const pointer_frame_outset = 6; + pub const elision_h = 24; pub const cell = [2]f32{ 96, 84 }; pub const hero_cell = [2]f32{ 168, 132 }; - /// A value in flight: its card. pub const flight_card = [2]f32{ 100, 60 }; - - /// The heap, to scale. - pub const heap = struct { - pub const units_per_byte = 4; - pub const strip_h = 88; - pub const strip_h_stacked = 56; - }; - /// The page map: one square a page. - pub const page = struct { - pub const pitch = 24; - pub const side = 22; - pub const row_pitch = 42; - }; - /// The system call strip: time spent in the kernel hangs below the line. pub const timeline = struct { pub const baseline = 3; pub const block_h = 44; @@ -563,14 +594,9 @@ pub const metric = struct { pub const text_x = 110; pub const row_h = 96; }; - /// A plaque: a name in a box (hollow for a value, filled for a place). - pub const plaque = struct { - pub const h = 40; - pub const pad = 12; - }; - /// No panel shows more rows of data than this; past it, elide or zoom. - pub const max_rows = 10; + /// The registers' rows, in pwndbg's order: sixteen, then rip (with the flags). + pub const all_registers = [_][]const u8{ "rax", "rbx", "rcx", "rdx", "rsi", "rdi", "rbp", "rsp", "r8", "r9", "r10", "r11", "r12", "r13", "r14", "r15", "rip" }; /// No scene tracks more objects by pigment. pub const max_pigments = 4; }; @@ -726,14 +752,30 @@ pub const time = struct { pub const unroll = 4.0 / 60.0; /// A tie between two panes: this long to draw (after what it leaves /// from has struck), and to go, as the marks advance. + /// `link`: 6 f after the register's strike its old pin fades + /// (`pin_out`), and the line draws to the target, head first (`in`, + /// `in_far` over 400 units); the pin lands with the head and settles; + /// at the advance the line draws back into the pin (`out`). pub const link = struct { + pub const after = 6.0 / 60.0; + pub const pin_out = 0.20; pub const in = 0.40; + pub const in_far = 0.60; + pub const settle = 0.20; pub const out = 0.30; }; - /// A source line opening into its instructions, and closing. - pub const unfold = 0.40; - /// A wedge drawing from a thing to its enlargement. - pub const wedge = 0.30; + /// `project`: on a run's mark, a bracket takes its row, then the line + /// draws to the part of the drawing the bytes are; that part reacts + /// only when it arrives. Out with the advance. + pub const project = struct { + pub const bracket = 0.10; + pub const draw = 0.30; + pub const out = 0.30; + }; + /// `establish`, for a lens: its elements rise from their rows this far apart. + pub const establish_rise = 0.10; + /// `relayout`: a change of split. + pub const relayout = 0.80; /// A caption leaves, then nothing, then the next arrives rising this /// many units; never typed out, never crossfaded. The stage stays /// still `caption_lead` after it (`caption_lead_first` after a scene's @@ -846,7 +888,7 @@ test "every mark stands off the page, and letters read on it" { } } // The strokes that carry meaning on their own. - for ([_]Role{ .current, .write, .read, .pointer, .allocated }) |r| + for ([_]Role{ .current, .write, .read, .link, .allocated }) |r| try expect(contrast(mark(r).solid, color.page) >= 6.5); try expect(contrast(color.text.ink, mark(.write).solid) >= 6.5); } @@ -857,29 +899,28 @@ test "no two roles are drawn the same way" { test "mono advances are whole units" { const m = size.mono; - for ([_]u16{ m.context, m.small, m.data, m.value, m.flight, m.hero }) |s| try expect(s % 5 == 0); + for ([_]u16{ m.context, m.small, m.data, m.value, m.flight, m.hero }) |sz| try expect(sz % 5 == 0); try expect(monoAdvance(m.context) == 12 and m.context >= size.min_read); try expect(monoAdvance(m.data) == 18); - try expect(hexWidth(16, m.data) == 315); - try expect(hexWidth(12, m.small) == 196); - // A byte's two digits fit its cell; a register's sixteen fit a word's footprint. - try expect(2 * monoAdvance(m.data) <= metric.byte.side - 8); - try expect(hexWidth(16, m.data) <= 8 * metric.byte.pitch - 2 - 24); + // A byte's two digits fit its 24 units, on a pitch of 30. + try expect(2 * monoAdvance(m.context) == 24 and metric.bytes.pitch == 30); try expect(m.data >= size.min_read and size.sans.label >= size.min_read and size.sans.micro >= size.min_any); } -test "layouts fill the stage" { +test "the splits fill the stage, panels 12 apart" { try expect(colX(11) + canvas.column == canvas.width - canvas.margin_x); try expect(span(12) == canvas.stage.w); try expect(layout.program.w == 880 and layout.state.x == 976); - // The state column: 468, 228 and 108, twelve apart. - try expect(layout.registers.h == 468 and layout.stack.h == 228 and layout.backtrace.h == 108); - try expect(layout.backtrace.y + layout.backtrace.h <= canvas.stage.y + canvas.stage.h); - for ([_]layout.Split{ layout.split.none, layout.split.beside, layout.split.subject }) |split| { - try expect(split.disasm.y + split.disasm.h <= canvas.stage.y + canvas.stage.h); - try expect(split.source.y >= split.lens.y + split.lens.h); - } - try expect(layout.split.beside.source.h == 228 and layout.split.beside.disasm.h == 276); + const bottom = canvas.stage.y + canvas.stage.h; + const sp = layout.split; + // design/README.md, "5. The frame". + try expect(sp.none.source.h == 368 and sp.none.disasm.y == 428 and sp.none.disasm.y + sp.none.disasm.h == 868); + try expect(sp.none.registers.h == 440 and sp.none.stack.y == 500 and sp.none.backtrace.y == 736 and sp.none.backtrace.y + sp.none.backtrace.h == 864); + try expect(sp.beside.band.h == 152 and sp.beside.source.y == 212 and sp.beside.disasm.y == 448 and sp.beside.disasm.y + sp.beside.disasm.h == bottom); + try expect(sp.beside.registers.y == 212 and sp.beside.stack.y == 664 and sp.beside.stack.y + sp.beside.stack.h == bottom); + try expect(sp.subject.band.h == 248 and sp.subject.source.y == 308 and sp.subject.disasm.y == 592 and sp.subject.disasm.y + sp.subject.disasm.h == bottom); + try expect(sp.subject.stack.y == 760 and sp.subject.stack.y + sp.subject.stack.h == bottom); + try expect(layout.rowsIn(sp.beside.disasm.h) == 17 and layout.rowsIn(sp.subject.disasm.h) == 11 and layout.rowsIn(sp.subject.stack.h) == 4); } test "easing runs from 0 to 1 without turning back" { @@ -901,7 +942,7 @@ test "easing runs from 0 to 1 without turning back" { test "durations are whole frames" { const t = time; - for ([_]f32{ t.snap, t.quick, t.step, t.step_per_row, t.step_max, t.move, t.travel, t.layout, t.camera, t.camera_far, t.fire, t.cascade, t.read_value, t.read_more, t.rest, t.strike.arm, t.strike.in, t.strike.pulse, t.strike.hold, t.strike.cool, t.strike.out, t.read.out, t.current_jump, t.flight.lift, t.flight.near, t.flight.second, t.veil, t.unfold, t.wedge, t.caption_in, t.caption_out, t.caption_gap, t.walk, t.run, t.sprint, t.phrase_rest, t.before_cut }) |d| { + for ([_]f32{ t.snap, t.quick, t.step, t.step_per_row, t.step_max, t.move, t.travel, t.layout, t.camera, t.camera_far, t.fire, t.cascade, t.read_value, t.read_more, t.rest, t.strike.arm, t.strike.in, t.strike.pulse, t.strike.hold, t.strike.cool, t.strike.out, t.read.out, t.current_jump, t.flight.lift, t.flight.near, t.flight.second, t.veil, t.link.after, t.link.pin_out, t.link.in, t.link.in_far, t.link.settle, t.link.out, t.project.bracket, t.project.draw, t.project.out, t.establish_rise, t.relayout, t.caption_in, t.caption_out, t.caption_gap, t.walk, t.run, t.sprint, t.phrase_rest, t.before_cut }) |d| { const frames = d * t.fps; try expect(@abs(frames - @round(frames)) < 1e-3); } diff --git a/anim/src/ties.zig b/anim/src/ties.zig index 426a667..d18b996 100644 --- a/anim/src/ties.zig +++ b/anim/src/ties.zig @@ -1,57 +1,81 @@ -//! Ties: one pane is a view of another, and a line between them says so. -//! Any component can say where a thing of its own is on screen (`anchor`: -//! a register's row, a word of memory, a node, the source's current line, -//! the pc's row), and `Ties`, added last, draws the connections between -//! places in different panels, routed along the gutters and never across -//! a panel's text: +//! Ties (design/README.md, "4. Ties"): the panes are one machine at +//! different levels, and a tie says which thing in one pane a thing in +//! another is. Panels publish the `place`s of their things as they draw +//! (`anchor`); `Ties`, added after them, draws between places, along the +//! channels only, never across a letter or another tie: //! -//! - standing ties, declared by the scene (the source line and the -//! instruction it is at: the gold thread); -//! - the step's link: from the register a step has just struck to the -//! row or node whose address it now holds, in the ink of where it -//! points. It draws after the strike and goes as the marks advance; -//! - a word a step wrote and the node it is part of: the bytes are the -//! cause, the idea drawn from them the effect. +//! - `thread` (standing): the source's current line is these +//! instructions. Gold, 3: a bracket on the line, one on each run of its +//! instructions in the disassembly, a line down the left margin. +//! - `link` (for the step that set it): this register now points at +//! that. In the ink of the region pointed into, 3 wide, up the gutter's +//! left lane, head on the target; it ends in the target's `pin` and +//! leaves it behind (`pinShows` times the pin for its owner). +//! - `projection` (for the step): these bytes are that part of the +//! drawing. In the mark's colour, 2 wide: a bracket on the dump's row, +//! a line down the gutter's right lane and along the lens's top lane, +//! a bracket over the node's address. +//! - `echo`: the same mark at the same instant in two panels; no line. //! -//! A tie arrives, stays and leaves in the theme's time (`time.link`). +//! Budget: one thread; for a step one link and one projection. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); const zt = @import("ztrace"); const ui = @import("ui.zig"); const cursors = @import("cursor.zig"); -const contexts = @import("context.zig"); -const Recording = @import("Recording.zig"); +const rows = @import("rows.zig"); +const Stage = @import("stage.zig").Stage; const theme = ui.theme; const time = theme.time; const Rect = ui.Rect; /// A thing with a place on screen. pub const Place = union(enum) { - /// A register's value, by its place in `Regs.names`. + /// A register's value (its first hop), by its place in `Regs.names`. register: usize, - /// The word of memory at this address, wherever it is shown. + /// The registers panel: a link leaves from its edge. + registers, + /// A stack slot's row, by its address. word: u64, - /// The object at this address, as an idea. + /// The pin lane of a stack slot, by its address. + slot_pin: u64, + /// A drawn node, by its address; its address over it; the pin under it. node: u64, - /// The source's current line; the disassembly's. + node_address: u64, + node_pin: u64, + /// A part of a node (its value, its pointer half), by the address of the field it is drawn from. + node_part: u64, + /// A run of bytes in the lens's dump, by its address. + dump_run: u64, + /// The source's current line; the disassembly's pc row; each run of + /// the current line's instructions in the disassembly. source, pc, + run: usize, fn key(p: Place) u64 { + const low = (1 << 56) - 1; return switch (p) { - .register => |i| 1 << 60 | @as(u64, i), - .word => |a| 2 << 60 | (a & (1 << 60) - 1), - .node => |a| 3 << 60 | (a & (1 << 60) - 1), - .source => 4 << 60, - .pc => 5 << 60, + .register => |i| 1 << 56 | @as(u64, i), + .registers => 2 << 56, + .word => |a| 3 << 56 | (a & low), + .slot_pin => |a| 4 << 56 | (a & low), + .node => |a| 5 << 56 | (a & low), + .node_address => |a| 6 << 56 | (a & low), + .node_pin => |a| 7 << 56 | (a & low), + .node_part => |a| 8 << 56 | (a & low), + .dump_run => |a| 9 << 56 | (a & low), + .source => 10 << 56, + .pc => 11 << 56, + .run => |k| 12 << 56 | @as(u64, k), }; } }; /// What this frame's components have said so far, in canvas pixels: left, top, right, bottom. /// shortcut: one table for the process, emptied by `Ties.draw`; a scene with two players at once wants it on the frame. -var table: [256]struct { key: u64, box: [4]f32 } = undefined; +var table: [512]struct { key: u64, box: [4]f32 } = undefined; var said: usize = 0; /// `r` (in `u`'s units) is where `place` is, this frame. @@ -68,72 +92,185 @@ fn find(place: Place) ?[4]f32 { return null; } +/// When register `i` strikes in this step: the registers one step wrote +/// strike 6 f apart, in the registers' order; from the fourth on with the +/// third, within the CPU's slot of the cascade. +pub fn strikeDelay(now: cursors.Now, i: usize) f32 { + var before: f32 = 0; + for (theme.metric.all_registers[0..16]) |name| { + const k = @import("context.zig").index(name).?; + if (k == i) break; + if (now.registerAge(k) == 0) before += 1; + } + return @min(before * cursors.write_apart, time.cascade); +} + +/// How long a link draws: to a slot near by, or across to the lens. +pub fn linkTime(target: enum { slot, node }) f32 { + return if (target == .slot) time.link.in else time.link.in_far; +} + +/// How much a pin of register `i` shows at the place it pointed at +/// before this step (`old`) and at the one it points at now (`new`): +/// 6 f after its strike the old pin fades (12 f); the new lands with +/// its link's head and settles (12 f). At a run or a sprint it moves +/// with its level; at rest, and for a register the step did not write, +/// both are 1. +pub fn pinShows(now: cursors.Now, i: usize, target: enum { slot, node }) struct { old: f32, new: f32 } { + if (now.resting() or now.registerAge(i) != 0) return .{ .old = 1, .new = 1 }; + const since = now.since(.cpu, strikeDelay(now, i)); + if (!now.walking()) return if (since >= 0) .{ .old = 0, .new = 1 } else .{ .old = 1, .new = 0 }; + const l = time.link; + const arrives = l.after + linkTime(if (target == .slot) .slot else .node); + return .{ + .old = 1 - std.math.clamp((since - l.after) / l.pin_out, 0, 1), + .new = std.math.clamp((since - arrives) / l.settle, 0, 1), + }; +} + pub const Ties = struct { cursor: cursors.Cursor, - box: Rect = theme.canvas.stage, - /// Ties that stand for the whole scene. - standing: []const [2]Place = &.{.{ .source, .pc }}, + stage: *const Stage, + box: Rect = .{ .x = 0, .y = 0, .w = theme.canvas.width, .h = theme.canvas.height }, + /// The thread: whether the scene draws it (a scene with no source has none). + threaded: bool = true, + /// No links: the registers are not marked (`Registers.quiet`). + quiet: bool = false, pub fn draw(g: *const Ties, f: *sk.Frame, at: anim.Placed) void { defer said = 0; const now = g.cursor.now(at); - const rec = g.cursor.rec; - // In canvas pixels, `k` of them a unit: anchors come from panels anywhere. + // In canvas pixels, `k` of them a unit: places come from panels anywhere. var raw: ui.Ui = .{ .f = f, .at = at }; raw.at.xf = .identity; const k = at.scale; - // The gutter between the two columns, and the margin left of the program's. - const gutter = at.xf.apply(.{ theme.layout.state.x - 16 - g.box.x, 0 })[0]; - const margin = at.xf.apply(.{ -12, 0 })[0]; + const split = g.stage.now(at); + if (g.threaded) thread(raw, at, k); + if (!now.walking()) return; + if (!g.quiet) g.link(raw, at, now, k, split); + g.projection(raw, at, now, k, split); + } - for (g.standing) |tie| { - const a = find(tie[0]) orelse continue; - const b = find(tie[1]) orelse continue; - // The gold thread: two views of where execution is, joined down the margin. - const lane = @min(margin, @min(a[0], b[0]) - 6 * k); - raw.arrow(&.{ .{ a[0], (a[1] + a[3]) / 2 }, .{ lane, (a[1] + a[3]) / 2 }, .{ lane, (b[1] + b[3]) / 2 }, .{ b[0], (b[1] + b[3]) / 2 } }, theme.mark(.current).solid, .{ .width = theme.stroke.rule * k, .head = null }); + /// The gold thread: the current line and its instructions, one mark. + fn thread(raw: ui.Ui, at: anim.Placed, k: f32) void { + const line = find(.source) orelse return; + const x = at.xf.apply(.{ theme.metric.tie.thread_x, 0 })[0]; + const gold = theme.mark(.current).solid; + const kind = [2]f32{ theme.metric.bracket.thread[0] * k, theme.metric.bracket.thread[1] * k }; + raw.bracket(x, line[1] + 2 * k, line[3] - 2 * k, gold, kind, 1, false); + var bottom = line[3] - 2 * k; + var top = line[1] + 2 * k; + var n: usize = 0; + while (find(.{ .run = n })) |run| : (n += 1) { + raw.bracket(x, run[1] + 2 * k, run[3] - 2 * k, gold, kind, 1, false); + bottom = @max(bottom, run[3] - 2 * k); + top = @min(top, run[1] + 2 * k); } - if (!now.walking()) return; - // The step's link: the first register it struck that now holds - // the address of something on screen. It draws once the strike - // is in, and goes as the marks advance. - const leaves = 1 - std.math.clamp((now.time - now.plan.advance) / time.link.out, 0, 1); + if (n > 0) raw.fill(.{ .x = x - kind[0] / 2, .y = top, .w = kind[0], .h = bottom - top }, gold); + } + + /// The step's link: the first register it struck that now holds the + /// address of a pinned thing (a stack slot, a node). rip and rsp are + /// where execution and the stack are, and have none. + fn link(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split) void { + const rec = g.cursor.rec; const regs = (now.moment().regs(rec.tid) catch null) orelse return; - for (zt.Regs.names, 0..) |name, index| { - if (now.registerAge(index) != 0 or std.mem.eql(u8, name, "rip") or std.mem.eql(u8, name, "rsp") or std.mem.eql(u8, name, "rbp")) continue; + const edge = find(.registers) orelse return; + for (theme.metric.all_registers[0..16]) |name| { + const i = @import("context.zig").index(name).?; + if (now.registerAge(i) != 0 or std.mem.eql(u8, name, "rsp")) continue; const value = regs.named(name) orelse continue; - const from = find(.{ .register = index }) orelse continue; - const to = find(.{ .node = value }) orelse find(.{ .word = value & ~@as(u64, 7) }) orelse continue; - const drawn = cursors.standard(std.math.clamp((now.since(.cpu, 0) - time.strike.in) / time.link.in, 0, 1)); - const color = contexts.ink(contexts.regionOf(now.moment(), value)); + const from = find(.{ .register = i }) orelse continue; + const node = find(.{ .node_pin = value }); + const target = node orelse find(.{ .slot_pin = value }) orelse continue; + const since = now.since(.cpu, strikeDelay(now, i)); + const l = time.link; + const drawn = cursors.standard(std.math.clamp((since - l.after) / linkTime(if (node != null) .node else .slot), 0, 1)); + const back = std.math.clamp((now.time - now.plan.advance) / l.out, 0, 1); + if (drawn <= 0 or back >= 1) return; + const color = rows.ink(rows.regionOf(now.moment(), value)); const y = (from[1] + from[3]) / 2; - const to_y = (to[1] + to[3]) / 2; - // Out of the panel's edge, along the gutter, to the side of what it lands on. - const edge = find(.{ .register = 1 << 20 }) orelse from; - const lands_at = if (to[2] < gutter) to[2] + 2 * k else to[0] - 2 * k; - raw.faded(leaves).arrow(&.{ .{ edge[0], y }, .{ gutter, y }, .{ gutter, to_y }, .{ lands_at, to_y } }, color, .{ .width = theme.stroke.line * k, .head = .{ 12 * k, 10 * k }, .progress = drawn }); - // What it lands on answers with a frame. - if (drawn >= 1) raw.faded(leaves).frame(.{ .x = to[0] - 2 * k, .y = to[1] - 2 * k, .w = to[2] - to[0] + 4 * k, .h = to[3] - to[1] + 4 * k }, color, theme.stroke.rule * k); - break; - } - // A word written and the node it is part of: the bytes, then the idea. - for (rec.written(now.stops[1], now.stops[0])) |span| { - const word = find(.{ .word = span.addr & ~@as(u64, 7) }) orelse continue; - const node = for (table[0..said]) |entry| { - // The node whose bytes hold the word: nodes are sixteen bytes here. - if (entry.key >> 60 == 3 and (span.addr & (1 << 60) - 1) -% (entry.key & (1 << 60) - 1) < 16) break entry.box; - } else continue; - const drawn = cursors.standard(std.math.clamp(now.since(.memory, 0) / time.cascade, 0, 1)); - const x = (word[0] + word[2]) / 2; - const node_x = (node[0] + node[2]) / 2; - const between = (node[3] + word[1]) / 2 + 18 * k; - raw.faded(leaves).arrow(&.{ .{ x, word[1] }, .{ x, between }, .{ node_x, between }, .{ node_x, node[3] + 2 * k } }, theme.mark(.write).solid, .{ .width = theme.stroke.line * k, .head = .{ 12 * k, 10 * k }, .progress = drawn }); - break; + const lane = at.xf.apply(.{ theme.metric.tie.link_lane, 0 })[0]; + var points: [5]ui.Vec = undefined; + var n: usize = 0; + points[0] = .{ edge[0] + 2 * k, y }; + points[1] = .{ lane, y }; + if (node) |pin| { + // Up the gutter, along the lens's bottom lane, up into the pin under the node. + const bottom = at.xf.apply(.{ 0, split.band.y + split.band.h - theme.metric.lens.bottom_lane })[1]; + const cx = (pin[0] + pin[2]) / 2; + points[2] = .{ lane, bottom }; + points[3] = .{ cx, bottom }; + points[4] = .{ cx, pin[1] }; + n = 5; + } else { + // Along the gutter to the slot's row, and into its pin lane. + const mid = (target[1] + target[3]) / 2; + points[2] = .{ lane, mid }; + points[3] = .{ target[0] - 2 * k, mid }; + n = 4; + } + raw.route(points[0..n], color, .{ .width = theme.stroke.tie.link * k, .head = .{ 10 * k, 10 * k }, .progress = drawn, .from = cursors.standard(back), .radius = theme.metric.tie.radius * k }); + return; } } + + /// The step's projection: the first run of bytes it read or wrote in + /// the lens's dump that a part of the drawing is made of. + fn projection(g: *const Ties, raw: ui.Ui, at: anim.Placed, now: cursors.Now, k: f32, split: theme.layout.Split) void { + const rec = g.cursor.rec; + const kinds = [2]struct { spans: []const @import("Recording.zig").Span, role: theme.Role }{ + .{ .spans = rec.written(now.stops[1], now.stops[0]), .role = .write }, + .{ .spans = rec.read(now.stops[1], now.stops[0]), .role = .read }, + }; + for (kinds) |kind| for (kind.spans) |span| { + const word = span.addr & ~@as(u64, 7); + const run = find(.{ .dump_run = word }) orelse continue; + const part = find(.{ .node_part = word }) orelse continue; + const node_addr = nodeOf(word) orelse continue; + const over = find(.{ .node_address = node_addr }) orelse continue; + const p = time.project; + const since = now.since(.memory, 0); + const bracket = std.math.clamp(since / p.bracket, 0, 1); + const drawn = cursors.standard(std.math.clamp((since - p.bracket) / p.draw, 0, 1)); + const out = 1 - std.math.clamp((now.time - now.plan.advance) / p.out, 0, 1); + if (bracket <= 0 or out <= 0) return; + const color = theme.mark(kind.role).solid; + const v = raw.faded(out); + const tie = [2]f32{ theme.metric.bracket.tie[0] * k, theme.metric.bracket.tie[1] * k }; + // The bracket takes the row: x 7 of the dump, the row's height. + const bx = run[0] - (rows.byteX(@intCast(word % 16)) - 4 - theme.metric.bytes.bracket) * k; + v.faded(bracket).bracket(bx, run[1] + 2 * k, run[3] - 2 * k, color, tie, 1, false); + const lane = at.xf.apply(.{ theme.metric.tie.projection_lane, 0 })[0]; + const top = at.xf.apply(.{ 0, split.band.y + theme.metric.context.tag + theme.metric.lens.top_lane })[1]; + const cx = (over[0] + over[2]) / 2; + const y = (run[1] + run[3]) / 2; + v.route(&.{ .{ bx - 1 * k, y }, .{ lane, y }, .{ lane, top }, .{ cx, top }, .{ cx, over[1] - 4 * k } }, color, .{ .width = theme.stroke.tie.projection * k, .head = null, .progress = drawn, .radius = theme.metric.tie.projection_radius * k }); + // Over the node's address: a bracket, serifs down. + if (drawn >= 1) { + v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = over[2] - over[0], .h = 3 * k }, color); + v.fill(.{ .x = over[0], .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); + v.fill(.{ .x = over[2] - 3 * k, .y = over[1] - 5 * k, .w = 3 * k, .h = 8 * k }, color); + } + _ = part; + return; + }; + } }; +/// The node a field's address belongs to: the drawn node at or below it, within a record. +fn nodeOf(addr: u64) ?u64 { + var best: ?u64 = null; + for (table[0..said]) |entry| { + if (entry.key >> 56 != 6) continue; + const a = entry.key & ((1 << 56) - 1); + if (a <= addr and addr - a < 64 and (best == null or a > best.?)) best = a; + } + return best; +} + test "places have keys of their own" { try std.testing.expect((Place{ .word = 0x1000 }).key() != (Place{ .node = 0x1000 }).key()); try std.testing.expect((Place{ .source = {} }).key() != (Place{ .pc = {} }).key()); + try std.testing.expect((Place{ .run = 0 }).key() != (Place{ .run = 1 }).key()); } diff --git a/anim/src/ui.zig b/anim/src/ui.zig index 9d92e28..48d2f64 100644 --- a/anim/src/ui.zig +++ b/anim/src/ui.zig @@ -1,7 +1,8 @@ //! What every component draws with, in the design's units (1920 by 1080; a //! component's origin is its own top left corner): rects and rules snapped -//! to pixels, text in the design's faces set on a baseline, hex in groups -//! of four, the panel, the marks (written, read, unknown), and arrows. +//! to pixels, text in the design's faces set on a baseline, and the +//! catalogue's atoms (design/README.md, "1. Atoms"): `address`, `value`, +//! `arrow`, `byte`, the `mark`s, `plaque`, `pin`, `bracket`, and a panel's `tag`. const std = @import("std"); const sk = @import("sketch"); const anim = @import("anim"); @@ -71,38 +72,32 @@ fn set(face: Face, size: f32) Face { }; } -/// `n` in hex, a space between groups of four from the right, into `out` -/// (20 bytes is enough): nobody reads twelve digits unbroken. -pub fn grouped(out: []u8, n: u64) []const u8 { - var buf: [16]u8 = undefined; - const digits = std.fmt.bufPrint(&buf, "{x}", .{n}) catch unreachable; - var at: usize = 0; - for (digits, 0..) |d, i| { - if (i > 0 and (digits.len - i) % 4 == 0) { - out[at] = ' '; - at += 1; - } - out[at] = d; - at += 1; - } - return out[0..at]; -} - /// Width of one mono character at `size`: the mono faces advance 0.6 em, a /// whole number of pixels at the design's sizes. pub fn advance(size: f32) f32 { return size * 3 / 5; } -/// The space between groups of four hex digits: half a character, a whole unit. -pub fn hexGap(size: f32) f32 { - return @ceil(advance(size) / 2); +/// How many leading hex digits every address of a column shares (an +/// address's `lead`), leaving at least two: the eye reads the rest. +pub fn shared(addrs: []const u64) usize { + if (addrs.len == 0) return 0; + var bufs: [2][16]u8 = undefined; + const first = std.fmt.bufPrint(&bufs[0], "{x}", .{addrs[0]}) catch unreachable; + var k: usize = first.len -| 2; + for (addrs[1..]) |a| { + const d = std.fmt.bufPrint(&bufs[1], "{x}", .{a}) catch unreachable; + if (d.len != first.len) return 0; + var same: usize = 0; + while (same < k and d[same] == first[same]) same += 1; + k = same; + } + return k; } -/// Width of `digits` hex digits in groups of four, half a character apart. -pub fn hexWidth(digits: usize, size: f32) f32 { - const groups: f32 = @floatFromInt((digits + 3) / 4); - return @as(f32, @floatFromInt(digits)) * advance(size) + (groups - 1) * hexGap(size); +/// The digits of an address, as long as it is. +pub fn digits(addr: u64) usize { + return if (addr == 0) 1 else (64 - @clz(addr) + 3) / 4; } pub const Align = enum { left, center, right }; @@ -130,6 +125,13 @@ pub const Ui = struct { return out; } + /// The same, its origin moved to `x, y`: a row or a panel drawn at its place. + pub fn moved(u: Ui, x: f32, y: f32) Ui { + var out = u; + out.at.xf = out.at.xf.mul(.translate(x, y)); + return out; + } + fn point(u: Ui, x: f32, y: f32) Vec { return u.at.xf.apply(.{ x, y }); } @@ -200,103 +202,150 @@ pub const Ui = struct { return u.f.format(fmt, args); } - /// `value` as `digits` hex digits, in groups of four half a character - /// apart, from `x` rightwards. - pub fn hex(u: Ui, size: f32, x: f32, baseline: f32, v: u64, digits: usize, ink: Ink) void { - var buf: [16]u8 = undefined; - const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{v}) catch unreachable; - const shown = all[16 - digits ..]; - // Leading zeros, but never the last digit. - var zeros: usize = 0; - while (zeros < digits - 1 and shown[zeros] == '0') zeros += 1; - const adv = advance(size); - var at_x = x; - var i: usize = 0; - while (i < digits) { - // A group ends where the digits left are a multiple of four. - const end = i + ((digits - i - 1) % 4) + 1; - const split = std.math.clamp(zeros, i, end); - u.text(ink.face, size, at_x, baseline, shown[i..split], ink.lead); - u.text(ink.face, size, at_x + @as(f32, @floatFromInt(split - i)) * adv, baseline, shown[split..end], ink.ink); - at_x += @as(f32, @floatFromInt(end - i)) * adv + hexGap(size); - i = end; - } + /// For tools/motion.py, which plots how much of each panel changes + /// from frame to frame: with CANIM_PANELS in the environment, every + /// frame tells where on the canvas (as fractions of it) `r` is. + pub fn region(u: Ui, name: []const u8, r: Rect) void { + if (u.f.host.env("CANIM_PANELS") == null) return; + const a = u.point(r.x, r.y); + const b = u.point(r.x + r.w, r.y + r.h); + const w = @floor(u.f.in.window_width); + const h = @floor(u.f.in.window_height); + std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name }); } - /// An address in a gutter: its last group is the end that changes from - /// row to row, so only that one is in full ink. - pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, digits: usize) void { - const size = theme.size.mono.small; - const adv = advance(size); - var buf: [16]u8 = undefined; - const all = std.fmt.bufPrint(&buf, "{x:0>16}", .{addr}) catch unreachable; - const shown = all[16 - digits ..]; - var i: usize = 0; - var at_x = x; - while (i < digits) : (i += 4) { - u.text(.mono, size, at_x, baseline, shown[i .. i + 4], if (i + 4 == digits) theme.color.text.primary else theme.color.text.faint); - at_x += 4 * adv + hexGap(size); - } - } + // ---------------------------------------------------------- atoms - /// An acme window `w` by `h` at the origin: a tag one row high (a box, - /// gold where the narration is; the title; a note ranged right, or the - /// legend of the regions), and a rule round the body. Returns the body. - pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: bool = false }) Rect { + /// `tag`: a panel's title bar, 24 high, raised: a 12-unit box 8 in + /// (solid gold where the narration is, `focus`; a faint outline + /// elsewhere), the title in Go Medium 20, and ranged right a `note` + /// (Go 18, faint) or a `legend` (words in Go Medium 18, each in its + /// ink, 14 apart). With the rule round the body it is a panel, an acme + /// window `w` by `h` at the origin. Returns the body. + pub fn panel(u: Ui, w: f32, h: f32, title: []const u8, options: struct { focus: bool = false, note: []const u8 = "", legend: []const Legend = &.{} }) Rect { u.region(title, .{ .x = 0, .y = 0, .w = w, .h = h }); const m = theme.metric.context; const t = theme.color.text; u.fill(.{ .x = 0, .y = 0, .w = w, .h = m.tag }, theme.color.raised); - u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, theme.color.rule, theme.stroke.rule); + u.frame(.{ .x = 0, .y = 0, .w = w, .h = h }, if (options.focus) t.dim else theme.color.rule, theme.stroke.rule); const box: Rect = .{ .x = 8, .y = 6, .w = 12, .h = 12 }; if (options.focus) u.fill(box, theme.mark(.current).solid) else u.frame(box, t.faint, theme.stroke.rule); - const base = theme.baseline(0, m.tag, theme.size.sans.tag); + const base = m.tag - 6; u.text(.sans_medium, theme.size.sans.tag, 28, base, title, if (options.focus) t.primary else t.dim); const micro = theme.size.sans.micro; - if (options.legend) { - // What an address's ink says, always on screen, as pwndbg's legend is. - const c = theme.color.region; - var x = w - 10; - inline for (.{ .{ "read-only", c.rodata }, .{ "rwx", c.code_ }, .{ "data", c.data }, .{ "code", c.code_ }, .{ "heap", c.heap }, .{ "stack", c.stack } }) |entry| { - x -= u.width(.sans_medium, micro, entry[0]); - u.text(.sans_medium, micro, x, base, entry[0], entry[1]); - if (comptime std.mem.eql(u8, entry[0], "rwx")) u.fill(.{ .x = x, .y = base + 3, .w = u.width(.sans_medium, micro, entry[0]), .h = theme.stroke.rule }, entry[1]); - x -= 16; + if (options.legend.len > 0) { + var x = w - 10 + 14; + for (options.legend) |entry| x -= u.width(.sans_medium, micro, entry.text) + 14; + for (options.legend) |entry| { + const ew = u.width(.sans_medium, micro, entry.text); + u.text(.sans_medium, micro, x, base, entry.text, entry.ink); + if (entry.under) u.fill(.{ .x = x, .y = base + 3, .w = ew, .h = theme.stroke.rule }, entry.ink); + x += ew + 14; } } else u.text(.sans, micro, w - 10 - u.width(.sans, micro, options.note), base, options.note, t.faint); return .{ .x = 0, .y = m.tag, .w = w, .h = h - m.tag }; } - /// For tools/motion.py, which plots how much of each panel changes - /// from frame to frame: with CANIM_PANELS in the environment, every - /// frame tells where on the canvas (as fractions of it) `r` is. - pub fn region(u: Ui, name: []const u8, r: Rect) void { - if (u.f.host.env("CANIM_PANELS") == null) return; - const a = u.point(r.x, r.y); - const b = u.point(r.x + r.w, r.y + r.h); - const w = @floor(u.f.in.window_width); - const h = @floor(u.f.in.window_height); - std.debug.print("canim-panel {d:.4} {d:.4} {d:.4} {d:.4} {s}\n", .{ a[0] / w, a[1] / h, b[0] / w, b[1] / h, name }); + /// `address`: lower-case hex, unbroken, as long as it is, no `0x`, in + /// the ink of where it points. `lead`: the first n digits at 60% of + /// the ink toward the page (a column's shared digits). `name`: a code + /// address's `name+offset` after it, primary, 10 on. `rwx`: a 2-unit + /// underline. Returns where it ends (after the name). + pub fn address(u: Ui, x: f32, baseline: f32, addr: u64, a: Address) f32 { + const size = theme.size.mono.context; + var buf: [16]u8 = undefined; + const d = std.fmt.bufPrint(&buf, "{x}", .{addr}) catch unreachable; + const lead = @min(a.lead, d.len -| 2); + const adv = advance(size); + if (lead > 0) u.text(a.face, size, x, baseline, d[0..lead], mix(a.ink, theme.color.page, theme.metric.context.lead_alpha)); + u.text(a.face, size, x + @as(f32, @floatFromInt(lead)) * adv, baseline, d[lead..], a.ink); + const end = x + @as(f32, @floatFromInt(d.len)) * adv; + if (a.rwx) u.fill(.{ .x = x, .y = baseline + 3, .w = end - x, .h = theme.stroke.rule }, a.ink); + if (a.name.len == 0) return end; + const name = symbol(u, a.name); + u.text(.mono, size, end + 10, baseline, name, theme.color.text.primary); + return end + 10 + @as(f32, @floatFromInt(name.len)) * adv; + } + + /// `value`: what is not an address, set by its class (`Value.of`), from + /// `x` at mono 20: a digit; a small number in hex and its decimal 8 + /// after it, faint; a large one in hex after `0x`; a small negative. + /// `pointer`: an address, in `ink`. Returns where its digits end (the + /// faint decimal apart: `Value.width` is what a mark goes behind). + pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 { + const size = theme.size.mono.context; + const shown = Value.of(v, pointer); + u.text(ink.face, size, x, baseline, u.print("{s}{s}", .{ shown.prefix, shown.digits() }), ink.ink); + const end = x + shown.width(); + if (shown.decimal) u.text(.mono, size, end + 8, baseline, u.print("{d}", .{v}), theme.color.text.faint); + return end; + } + + /// `arrow`: the sign between two things in a chain, 30 wide from `x` + /// on the row whose middle is `mid`, faint: grammar, not data. + /// Returns where it ends. + pub fn arrow(u: Ui, x: f32, mid: f32, kind: Arrow) f32 { + const c = theme.color.text.faint; + const head = theme.metric.context.arrow_head; + const pen = u.at.pen(.{ .fill = rgb(c) }); + switch (kind) { + .points, .more => { + u.fill(.{ .x = x + 6, .y = mid - 1, .w = 11, .h = 2 }, c); + sk.draw.polygon(u.f, &.{ .{ x + 24, mid }, .{ x + 24 - head[0], mid - head[1] / 2 }, .{ x + 24 - head[0], mid + head[1] / 2 } }, pen); + if (kind == .more) for (0..3) |d| u.fill(.{ .x = x + 32 + @as(f32, @floatFromInt(d)) * 8, .y = mid + 2, .w = 3, .h = 3 }, c); + }, + .holds => { + sk.draw.polygon(u.f, &.{ .{ x + 6, mid }, .{ x + 6 + head[0], mid - head[1] / 2 }, .{ x + 6 + head[0], mid + head[1] / 2 } }, pen); + u.fill(.{ .x = x + 13, .y = mid - 1, .w = 11, .h = 2 }, c); + }, + } + return x + theme.metric.context.arrow_w + @as(f32, if (kind == .more) 24 else 0); } - /// The ground of a thing a step wrote, as hot as `h` says: armed (a - /// frame, the old value still in it), struck (the fill coming in, - /// its edges pulsing out and back), solid, cooling to its ember with a - /// bar along the bottom, `ground` (null: nothing) once that has gone. - /// Returns what to set its digits in. - pub fn written(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink { + // ---------------------------------------------------------- marks + + /// `mark` on a value in the context (mono 20), as hot as `h` says: + /// `armed` (a 2-unit vermilion frame round the old value), `struck` + /// (solid vermilion behind it, 5 wider each side, its edges pulsing out + /// 3; the digits bold, in ink: the ink this returns), then cooled to + /// nothing, and `held`: a tick 4 wide at the value's left for three + /// steps. A held change never sits under the digits it marks, whose + /// ink is where they point. `r` is the digits: x, width, and the row; + /// the tick is `tick` units left of them (5 after a chain's arrow). + pub fn written(u: Ui, r: Rect, h: Heat, tick: f32) ?Ink { + const w = theme.mark(.write); + const m = theme.metric.context; + // The tick is what a strike cools to: not under it while it is solid. + const held = h.level * (1 - (if (h.level >= 1) h.solid * h.in else 0)); + if (held > 0) u.fill(.{ .x = r.x - tick, .y = r.y + 3, .w = m.tick, .h = r.h - 6 }, mix(theme.color.page, w.solid, held)); + if (h.arm > 0) u.faded(h.arm).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, w.solid, m.read_frame); + const solid = if (h.level >= 1) h.solid * h.in else 0; + if (solid <= 0) return null; + const out = @min(h.out, m.pulse_outset); + u.fill(.{ .x = r.x - m.strike_out - out, .y = r.y + 1 - out, .w = r.w + 2 * m.strike_out + 2 * out, .h = r.h - 2 + 2 * out }, mix(theme.color.page, w.solid, solid)); + return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + } + + /// `read` in the context: a 2-unit frame in read's colour, 6 out at + /// the sides, no fill; `shows` of it (0 to 1: it fades in and out). + pub fn read(u: Ui, r: Rect, shows: f32) void { + if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 6, .y = r.y, .w = r.w + 12, .h = r.h }, theme.mark(.read).solid, theme.metric.context.read_frame); + } + + /// `mark` on a lens's cell: armed (a 3-unit frame), struck (a solid + /// fill coming in, its edges out 4 and back), cooled to its ember and + /// a vermilion bar 4 high along its bottom edge (`held`), `ground` + /// (null: nothing) once that has gone. Returns what to set its digits in. + pub fn cellWritten(u: Ui, r: Rect, h: Heat, ground: ?u24) Ink { const w = theme.mark(.write); const under: Color = rgb(ground orelse theme.color.page); - var body = if (h.level < 1) mix(under, w.ember, h.level) else mix(w.ember, w.solid, h.solid); + // No mark under coloured digits but a strike's: it cools to its ground, and the bar stays. + var body = if (h.level < 1) under else mix(under, w.solid, h.solid); // Timing: a strike arrives over a tenth of a second, not in a frame. - if (h.in < 1) body = mix(if (h.from == .ember) rgb(w.ember) else under, body, h.in); - // The pulse is an outset, the same on a byte and on a register, - // and the digits do not move with it. - // shortcut: a neighbour drawn after covers its side of the outset; struck cells in two passes when that shows. + if (h.in < 1) body = mix(under, body, h.in); if (h.level > 0 or ground != null) u.fill(.{ .x = r.x - h.out, .y = r.y - h.out, .w = r.w + 2 * h.out, .h = r.h + 2 * h.out }, body); const bar = theme.metric.ember_bar; - if (h.level >= 1 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, w.solid); - // Anticipation: where the write will land, before it does. + if (h.level > 0 and (h.in >= 1 or h.from == .ember)) u.fill(.{ .x = r.x, .y = r.y + r.h - bar, .w = r.w, .h = bar }, mix(under, w.solid, @min(h.level, 1))); if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, theme.stroke.line); // Bold ink on the solid. The face changes at the middle of the // cool, where the colour is moving fastest and hides it. @@ -306,91 +355,80 @@ pub const Ui = struct { return .{ .ink = mix(cooled.ink, w.on_solid, heat), .lead = mix(cooled.lead, w.on_solid, heat), .face = if (heat >= 0.5) .mono_bold else .mono }; } - /// A thing a step read: its ember inside a frame, `shows` of it (0 to - /// 1: it fades in and out, and is never armed or pulsed). - pub fn readMark(u: Ui, r: Rect, shows: f32) void { - if (shows <= 0) return; - const m = theme.mark(.read); - const v = u.faded(shows); - v.fill(r, m.ember); - v.frame(r, m.solid, theme.stroke.line); + /// `read` on a lens's cell: a 2-unit frame 2 out, no fill. + pub fn cellRead(u: Ui, r: Rect, shows: f32) void { + if (shows > 0) u.faded(shows).frame(.{ .x = r.x - 2, .y = r.y - 2, .w = r.w + 4, .h = r.h + 4 }, theme.mark(.read).solid, theme.stroke.rule); } - /// The same in the context, at mono 20, where a value has no cell of - /// its own: armed is a thin frame; a strike is solid behind the value - /// (its edges out a little and back), and for as long as it is solid - /// "written now" is all the value says: the ink this returns, bold on - /// the solid. It cools to nothing, and what is left for three steps is - /// a tick in the gap at the value's left: a held change never sits - /// under the digits it marks, whose ink is where they point. - pub fn struck(u: Ui, r: Rect, h: Heat) ?Ink { - const w = theme.mark(.write); - const m = theme.metric.context; - if (h.level > 0) u.fill(.{ .x = r.x - m.tick - 2, .y = r.y + 1, .w = m.tick, .h = r.h - 2 }, mix(theme.color.page, w.solid, h.level)); - if (h.arm > 0) u.faded(h.arm).frame(r, w.solid, m.read_frame); - const solid = if (h.level >= 1) h.solid * h.in else 0; - if (solid <= 0) return null; - const out = @min(h.out, m.pulse_outset); - u.fill(.{ .x = r.x - out, .y = r.y - out, .w = r.w + 2 * out, .h = r.h + 2 * out }, mix(theme.color.page, w.solid, solid)); - return if (solid >= 0.5) Ink.all(w.on_solid, .mono_bold) else null; + /// `current` on a row: gold ember across `r`, and a gold plaque `w` + /// wide at its left, its edges out `out` units (the fire). + pub fn current(u: Ui, r: Rect, w: f32, out: f32) void { + const gold = theme.mark(.current); + u.fill(r, gold.ember); + u.fill(.{ .x = r.x - out, .y = r.y - out, .w = w + 2 * out, .h = r.h + 2 * out }, gold.solid); } - /// A thing a step read, in the context: a thin frame outside the digits, and no fill. - pub fn readFrame(u: Ui, r: Rect, shows: f32) void { - if (shows > 0) u.faded(shows).frame(r, theme.mark(.read).solid, theme.metric.context.read_frame); + /// `trail`: a 4-unit bar at a row's left edge, `k` steps old (0 newest). + pub fn trail(u: Ui, x: f32, y: f32, k: usize) void { + if (k < theme.color.trail.len) u.fill(.{ .x = x, .y = y + 1, .w = 4, .h = theme.metric.context.row - 2 }, theme.color.trail[k]); } - /// One arrow of a chain, `metric.context.arrow_w` wide from `x`, on the - /// row whose middle is `y`: pointing right ("points to") or back - /// ("holds"). Faint: it is grammar, not data. - pub fn chainArrow(u: Ui, x: f32, y: f32, back: bool) void { - const m = theme.metric.context; - const c = theme.color.text.faint; - const a = x + 5; - const b = x + m.arrow_w - 5; - const head = m.arrow_head; - const pen = u.at.pen(.{ .fill = rgb(c) }); - if (back) { - u.fill(.{ .x = a + head[0] - 1, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); - sk.draw.polygon(u.f, &.{ .{ a, y }, .{ a + head[0], y - head[1] / 2 }, .{ a + head[0], y + head[1] / 2 } }, pen); - } else { - u.fill(.{ .x = a, .y = y - 1, .w = b - a - head[0] + 1, .h = 2 }, c); - sk.draw.polygon(u.f, &.{ .{ b, y }, .{ b - head[0], y - head[1] / 2 }, .{ b - head[0], y + head[1] / 2 } }, pen); - } + /// `unknown`: never observed: two dots where the digits would be, centred in `r`. + pub fn unknown(u: Ui, r: Rect) void { + const d = 4; + const y = r.y + (r.h - d) / 2; + u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint); + u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint); } - /// A value as its class is set (design/README.md, "What a value shows - /// as"), from `x` on `baseline` at mono 20: an address (`pointer`) as - /// long as it is, in `ink`; a number under 10 as the digit; up to - /// 0xffff as hex with its decimal after it, faint; larger, hex with - /// `0x`; within 4096 of 2^64, a small negative. Returns where it ends (the faint - /// decimal apart: `Value.width` is what a mark goes behind). - pub fn value(u: Ui, x: f32, baseline: f32, v: u64, pointer: bool, ink: Ink) f32 { + /// `plaque` `chip`: raised, `r`, behind a set flag. + pub fn chip(u: Ui, r: Rect) void { + u.fill(r, theme.color.raised); + } + + /// `pin` `row`: a register's names at the row it points at, bold, + /// primary, then 5 on a head 9 by 12 in the target's region ink, + /// pointing right at the row's address. Three names, then a count. + pub fn pinRow(u: Ui, x: f32, y: f32, names: []const []const u8, ink: anytype) void { const size = theme.size.mono.context; - const adv = advance(size); - const shown = Value.of(v, pointer); - var at = x; - if (shown.prefix.len > 0) { - u.text(ink.face, size, at, baseline, shown.prefix, ink.ink); - at += @as(f32, @floatFromInt(shown.prefix.len)) * adv; - } - var i: usize = 0; - const digits = shown.digits(); - while (i < digits.len) { - // A group ends where the digits left are a multiple of four. - const end = if (shown.grouped) i + ((digits.len - i - 1) % 4) + 1 else digits.len; - u.text(ink.face, size, at, baseline, u.print("{s}", .{digits[i..end]}), ink.ink); - at += @as(f32, @floatFromInt(end - i)) * adv + @as(f32, if (end < digits.len) theme.metric.context.group_gap else 0); - i = end; - } - if (shown.decimal) { - u.text(.mono, size, at + theme.metric.context.group_gap, baseline, u.print("{d}", .{v}), theme.color.text.faint); - at += theme.metric.context.group_gap + @as(f32, @floatFromInt(std.fmt.count("{d}", .{v}))) * adv; - } - return at; + const p = theme.metric.pin; + const shown = joined(u, names); + u.text(.mono_bold, size, x, theme.baseline(y, theme.metric.context.row, size), shown, theme.color.text.primary); + const hx = x + u.width(.mono, size, shown) + p.row_gap; + const mid = y + theme.metric.context.row / 2; + sk.draw.polygon(u.f, &.{ .{ hx + p.row_head[0], mid }, .{ hx, mid - p.row_head[1] / 2 }, .{ hx, mid + p.row_head[1] / 2 } }, u.at.pen(.{ .fill = rgb(ink) })); } - /// The veil over what cannot be read at the pace: `amount` of it, 0 to 1. + /// `pin` `under`: a stub 3 wide rising `size` + 10 to a node's bottom + /// edge at (`cx`, `y`), a head 10 by 10 on it, the first name left of + /// the stub and the others right, bold, primary. + pub fn pinUnder(u: Ui, cx: f32, y: f32, names: []const []const u8, ink: anytype, size: f32) void { + const p = theme.metric.pin; + const rise = size + 10; + u.fill(.{ .x = cx - p.stub / 2, .y = y + p.under_head[0] - 1, .w = p.stub, .h = rise - p.under_head[0] + 1 }, ink); + sk.draw.polygon(u.f, &.{ .{ cx, y + 1 }, .{ cx - p.under_head[1] / 2, y + 1 + p.under_head[0] }, .{ cx + p.under_head[1] / 2, y + 1 + p.under_head[0] } }, u.at.pen(.{ .fill = rgb(ink) })); + if (names.len == 0) return; + const by = y + 8 + size * 0.86; + u.text(.mono_bold, size, cx - p.under_names - u.width(.mono, size, names[0]), by, names[0], theme.color.text.primary); + // One name a side: a node's neighbour has its own. The rest are a count. + if (names.len > 1) u.text(.mono_bold, size, cx + p.under_names, by, if (names.len > 2) u.print("{s} +{d}", .{ names[1], names.len - 2 }) else names[1], theme.color.text.primary); + } + + /// `bracket`: a span marked at its edge, a stroke down `y0 .. y1` at + /// `x` and a serif at each end toward the span (right of the stroke, + /// or left with `side` -1). `open`: no bottom serif (a frame that + /// runs on past the panel). + pub fn bracket(u: Ui, x: f32, y0: f32, y1: f32, ink: anytype, kind: [2]f32, side: f32, open: bool) void { + const sw = kind[0]; + const serif = kind[1]; + u.fill(.{ .x = x - sw / 2, .y = y0, .w = sw, .h = y1 - y0 }, ink); + const sx = if (side > 0) x - sw / 2 else x + sw / 2 - serif - sw; + u.fill(.{ .x = sx, .y = y0, .w = serif + sw, .h = sw }, ink); + if (!open) u.fill(.{ .x = sx, .y = y1 - sw, .w = serif + sw, .h = sw }, ink); + } + + /// The veil over what cannot be read at the pace, or is not the + /// subject: `amount` of it, 0 to 1, at `alpha` (`color.veil_alpha`). pub fn veil(u: Ui, r: Rect, amount: f32) void { if (amount > 0) u.fill(r, rgb(theme.color.veil).alphaFloat(theme.color.veil_alpha * std.math.clamp(amount, 0, 1))); } @@ -406,38 +444,12 @@ pub const Ui = struct { u.f.clip(null); } - /// Never observed: two dots where the digits would be, centred in `r`. - pub fn unknown(u: Ui, r: Rect) void { - const d = theme.stroke.dot; - const y = r.y + (r.h - d) / 2; - u.fill(.{ .x = r.x + r.w / 2 - d - 3, .y = y, .w = d, .h = d }, theme.color.text.faint); - u.fill(.{ .x = r.x + r.w / 2 + 3, .y = y, .w = d, .h = d }, theme.color.text.faint); - } - - /// Bytes that are there and not shown: three dots and what they are. - pub fn elision(u: Ui, x: f32, y: f32, note: []const u8) void { - const d = theme.stroke.dot; - const h = theme.metric.elision_h; - for (0..3) |i| u.fill(.{ .x = x + @as(f32, @floatFromInt(i)) * 14, .y = y + (h - d) / 2, .w = d, .h = d }, theme.color.text.faint); - u.text(.sans, theme.size.sans.micro, x + 56, theme.baseline(y, h, theme.size.sans.micro), note, theme.color.text.faint); - } - - /// A name in a box 40 tall at `x, y`: hollow (a value) with `fill` - /// null, else filled (a place, a tracked object). Returns its width. - pub fn plaque(u: Ui, x: f32, y: f32, face: Face, size: f32, bytes: []const u8, ink: anytype, fill_color: ?u24, stroke: ?u24) f32 { - const m = theme.metric.plaque; - const w = u.width(face, size, bytes) + 2 * m.pad; - const r: Rect = .{ .x = x, .y = y, .w = w, .h = m.h }; - if (fill_color) |c| u.fill(r, c); - if (stroke) |c| u.frame(r, c, theme.stroke.rule); - u.text(face, size, x + m.pad, theme.baseline(y, m.h, size), bytes, ink); - return w; - } - - /// A path through `points` with filleted corners, `progress` of its - /// length drawn (an arrow growing to its target), dashed or whole, with - /// a filled head at its end or none. - pub fn arrow(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1 }) void { + /// A path through `points` with corners of radius `radius`, `progress` + /// of its length drawn from its start (a tie drawing head first, a node's + /// arrow growing to its target), dashed or whole, with a filled head at + /// its end or none. `from`: the share of its length left undrawn at its + /// start (a tie drawing back into its pin). + pub fn route(u: Ui, points: []const Vec, c: anytype, options: struct { width: f32 = theme.stroke.arrow, head: ?[2]f32 = theme.stroke.head, dashed: bool = false, progress: f32 = 1, from: f32 = 0, radius: f32 = theme.stroke.elbow_radius }) void { if (points.len < 2 or options.progress <= 0) return; var path: std.ArrayList(Vec) = .empty; const gpa = u.f.scratch; @@ -447,7 +459,7 @@ pub const Ui = struct { const a = points[i - 1]; const b = points[i]; const z = points[i + 1]; - const r = @min(theme.stroke.elbow_radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2); + const r = @min(options.radius, @min(sk.math.distance(a, b), sk.math.distance(b, z)) / 2); const from = b + sk.math.withLength(a - b, r); const to = b + sk.math.withLength(z - b, r); for (0..7) |k| { @@ -457,22 +469,27 @@ pub const Ui = struct { } path.append(gpa, points[points.len - 1]) catch return; - // Cut to `progress` of its length, and short of the head. + // Cut to `progress` of its length, short of the head, and from `from` of it. var total: f32 = 0; for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| total += sk.math.distance(a, b); const head_len: f32 = if (options.head) |h| h[0] else 0; const tip_at = total * std.math.clamp(options.progress, 0, 1); - var keep = @max(tip_at - @max(head_len - 2, 0), 0); + const keep = @max(tip_at - @max(head_len - 2, 0), 0); + const start_at = total * std.math.clamp(options.from, 0, 1); var tip = path.items[0]; var dir: Vec = sk.math.normalize(path.items[1] - path.items[0]); var body: std.ArrayList(Vec) = .empty; - body.append(gpa, path.items[0]) catch return; var walked: f32 = 0; for (path.items[0 .. path.items.len - 1], path.items[1..]) |a, b| { const len = sk.math.distance(a, b); if (len == 0) continue; - if (keep > 0) body.append(gpa, if (keep >= len) b else sk.math.mix(a, b, keep / len)) catch return; - keep -= len; + // The part of this segment between `start_at` and `keep`. + const lo = std.math.clamp((start_at - walked) / len, 0, 1); + const hi = std.math.clamp((keep - walked) / len, 0, 1); + if (hi > lo) { + if (body.items.len == 0) body.append(gpa, sk.math.mix(a, b, lo)) catch return; + body.append(gpa, sk.math.mix(a, b, hi)) catch return; + } if (walked <= tip_at) { dir = sk.math.normalize(b - a); tip = if (tip_at - walked >= len) b else sk.math.mix(a, b, (tip_at - walked) / len); @@ -485,13 +502,13 @@ pub const Ui = struct { if (body.items.len > 1) { if (options.dashed) sk.draw.dashed(u.f, body.items, theme.stroke.dash, stroke) else sk.draw.polyline(u.f, body.items, stroke); } - if (options.head) |h| { + if (options.head) |h| if (tip_at > start_at) { const back = tip - sk.math.scale(dir, h[0]); const side = sk.math.scale(sk.math.perp(dir), h[1] / 2); var solid = pen; solid.stroke = null; sk.draw.polygon(u.f, &.{ tip, back + side, back - side }, solid); - } + }; } /// A straight line between two points (a tree's edge). @@ -509,11 +526,12 @@ pub const Value = struct { buf: [20]u8 = undefined, len: usize = 0, prefix: []const u8 = "", - /// In groups of four from the right. - grouped: bool = false, /// Its decimal follows, faint: a small number that may be either. decimal: bool = false, + /// The class decides: an address (`pointer`) is hex as long as it is; + /// a digit is itself; a small number is hex with `0x` and its decimal; + /// a large one, hex with `0x`; within 4096 of 2^64, a small negative. pub fn of(v: u64, pointer: bool) Value { var shown: Value = .{}; const negative = !pointer and v > std.math.maxInt(u64) - 4096; @@ -523,8 +541,6 @@ pub const Value = struct { shown.len = (std.fmt.bufPrint(&shown.buf, "{d}", .{v}) catch unreachable).len; } else { shown.len = (std.fmt.bufPrint(&shown.buf, "{x}", .{v}) catch unreachable).len; - // The user's direction: hex unbroken, as a debugger prints it (the design had groups of four). - shown.grouped = false; shown.decimal = !pointer and v <= 0xffff; if (!pointer) shown.prefix = "0x"; } @@ -537,12 +553,45 @@ pub const Value = struct { /// Units its digits take at mono 20 (the faint decimal apart). pub fn width(shown: *const Value) f32 { - const adv = advance(theme.size.mono.context); - const groups: f32 = if (shown.grouped) @floatFromInt((shown.len + 3) / 4) else 1; - return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * adv + (groups - 1) * theme.metric.context.group_gap; + return @as(f32, @floatFromInt(shown.prefix.len + shown.len)) * advance(theme.size.mono.context); } }; +/// `address`'s options. +pub const Address = struct { + ink: Color = rgb(theme.color.region.code_), + lead: usize = 0, + name: []const u8 = "", + rwx: bool = false, + face: Face = .mono, +}; + +/// The kinds of `arrow`: "which points to", "which holds", "and goes on". +pub const Arrow = enum { points, holds, more }; + +/// A word of a `tag`'s legend, in its ink. +pub const Legend = struct { text: []const u8, ink: Color, under: bool = false }; + +/// A code address's name as the catalogue writes it: `name+offset`, and +/// for an address with no symbol its module, `libc+27781` (the recording +/// says `libc.so.6!27781`). +pub fn symbol(u: Ui, name: []const u8) []const u8 { + const bang = std.mem.indexOfScalar(u8, name, '!') orelse return name; + const after = name[bang + 1 ..]; + // A symbol of the module's: its own name is enough. + for (after) |ch| if (!std.ascii.isHex(ch)) return after; + const module = name[0..bang]; + return u.print("{s}+{s}", .{ module[0 .. std.mem.indexOfScalar(u8, module, '.') orelse module.len], after }); +} + +/// Names joined by a space: three, then a count. +fn joined(u: Ui, names: []const []const u8) []const u8 { + var out: []const u8 = ""; + for (names[0..@min(names.len, 3)], 0..) |n, i| out = if (i == 0) n else u.print("{s} {s}", .{ out, n }); + if (names.len > 3) out = u.print("{s} +{d}", .{ out, names.len - 3 }); + return out; +} + /// How hot a written thing is, from `cursor.Now.heat`. pub const Heat = struct { /// 0 no mark, up to 1 the mark in full (less while it leaves). @@ -583,16 +632,9 @@ test "a value is set by its class" { try std.testing.expect(address.prefix.len == 0 and !address.decimal); } -test "hex is grouped in fours" { - var buf: [20]u8 = undefined; - try std.testing.expectEqualStrings("7fff ffff d8c0", grouped(&buf, 0x7fffffffd8c0)); - try std.testing.expectEqualStrings("8000", grouped(&buf, 0x8000)); - try std.testing.expectEqualStrings("1 0000", grouped(&buf, 0x10000)); -} - -test "hex widths are whole units at the design's sizes" { - try std.testing.expectEqual(@as(f32, 18), advance(theme.size.mono.data)); - try std.testing.expectEqual(@as(f32, 315), hexWidth(16, theme.size.mono.data)); - try std.testing.expectEqual(@as(f32, 196), hexWidth(12, theme.size.mono.small)); - try std.testing.expectEqual(@as(f32, 36), hexWidth(2, theme.size.mono.data)); +test "a column's addresses share their leading digits, leaving two" { + try std.testing.expectEqual(@as(usize, 5), shared(&.{ 0x1001910, 0x1001937 })); + try std.testing.expectEqual(@as(usize, 6), shared(&.{0x28b41070})); + try std.testing.expectEqual(@as(usize, 0), shared(&.{ 0x1001910, 0x7ffe0000 })); + try std.testing.expectEqual(@as(usize, 12), digits(0x7ffe970f17b0)); } diff --git a/anim/src/words.zig b/anim/src/words.zig index ff9c5c1..5601156 100644 --- a/anim/src/words.zig +++ b/anim/src/words.zig @@ -36,7 +36,8 @@ pub const Clock = struct { u.fill(.{ .x = stage.x, .y = c.clock_y - 2, .w = stage.w * now.place, .h = 4 }, theme.mark(.current).solid); const size = theme.size.mono.context; const base = c.clock_y + 28; - const count = u.print("moment {d} of {d}", .{ now.stops[0] -| now.way[0], now.way[1] -| now.way[0] }); + // The count is where the current mark is: it changes as the mark moves on. + const count = u.print("moment {d} of {d}", .{ now.marked() -| now.way[0], now.way[1] -| now.way[0] }); const count_x = stage.x + stage.w - u.width(.mono, size, count); u.text(.mono, size, count_x, base, count, t.faint); // Secondary action: the pace, said quietly. It only fades. @@ -133,9 +134,9 @@ pub const Veil = struct { /// it means: the same in every picture. pub const Legend = struct { box: Rect, - entries: []const Entry = &.{ .current, .written, .changed, .read, .pointer, .allocated, .freed, .unknown, .kernel }, + entries: []const Entry = &.{ .current, .written, .changed, .read, .link, .allocated, .freed, .unknown, .kernel }, - pub const Entry = enum { current, written, changed, read, pointer, allocated, freed, unknown, kernel }; + pub const Entry = enum { current, written, changed, read, link, allocated, freed, unknown, kernel }; pub fn draw(l: *const Legend, f: *sk.Frame, at: anim.Placed) void { const u: ui.Ui = .{ .f = f, .at = at }; @@ -152,7 +153,7 @@ pub const Legend = struct { .written => .{ "written", "by this step" }, .changed => .{ "changed", "a moment ago" }, .read => .{ "read", "by this step" }, - .pointer => .{ "pointer", "holds an address" }, + .link => .{ "link", "now points at that" }, .allocated => .{ "allocated", "handed out" }, .freed => .{ "freed", "handed back" }, .unknown => .{ "not seen", "never observed" }, @@ -174,18 +175,14 @@ pub const Legend = struct { u.label(.mono_bold, size, r, .center, "rip", theme.mark(.current).on_solid); }, .written, .changed => for ([_]Rect{ half, other }, [_][]const u8{ "2a", "00" }) |cell, digits| { - const ink = u.written(cell, if (entry == .written) .{ .level = 1, .solid = 1 } else .ember, null); + const ink = u.cellWritten(cell, if (entry == .written) .{ .level = 1, .solid = 1 } else .ember, theme.color.raised); u.label(ink.face, size, cell, .center, digits, ink.ink); }, .read => for ([_]Rect{ half, other }, [_][]const u8{ "2a", "00" }) |cell, digits| { - u.readMark(cell, 1); + u.cellRead(cell, 1); u.label(.mono, size, cell, .center, digits, t.primary); }, - .pointer => { - u.fill(half, theme.color.raised); - u.frame(half, theme.mark(.pointer).solid, theme.stroke.arrow); - u.arrow(&.{ .{ r.x + 46, r.y + 23 }, .{ r.x + r.w, r.y + 23 } }, theme.mark(.pointer).solid, .{}); - }, + .link => u.route(&.{ .{ r.x, r.y + 23 }, .{ r.x + r.w, r.y + 23 } }, theme.color.region.heap, .{ .width = theme.stroke.tie.link, .head = .{ 10, 10 } }), .allocated => { u.fill(r, theme.mark(.allocated).ember); u.frame(r, theme.mark(.allocated).solid, theme.stroke.line); |
