From 165bb6c1344b98de7bef1d3c2b72147b4a40797a Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 16:05:20 -0300 Subject: 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 --- anim/src/context.zig | 600 +++++++++++++++++++-------------------------------- 1 file changed, 225 insertions(+), 375 deletions(-) (limited to 'anim/src/context.zig') 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")); } -- cgit v1.3