diff options
Diffstat (limited to 'anim/src/context.zig')
| -rw-r--r-- | anim/src/context.zig | 485 |
1 files changed, 485 insertions, 0 deletions
diff --git a/anim/src/context.zig b/anim/src/context.zig new file mode 100644 index 0000000..8d59112 --- /dev/null +++ b/anim/src/context.zig @@ -0,0 +1,485 @@ +//! 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. +//! Reads `Moment.regs`, `Moment.read`, the mappings (through +//! `zt.telescope`), and the names and instructions 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 cursors = @import("cursor.zig"); +const Recording = @import("Recording.zig"); +const syscalls = @import("syscalls.zig"); +const ties = @import("ties.zig"); +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 { + 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, + }; +} + +pub fn regionOf(moment: Recording.Moment, value: u64) zt.telescope.Region { + return zt.telescope.region(moment, value) catch .none; +} + +/// 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 }; +} + +/// `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); + } +} + +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 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, + /// 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, + /// No strikes: for a program whose instrumentation uses the registers + /// between any two lines, so that nearly all of them change at every one. + quiet: bool = false, + focus: bool = false, + + 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); + 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() }; + if (r.syscalls) { + // The registers as the newest call crossed with them: the + // library's own use of them after it returns is not the story. + 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); + } + + fn shownAt(rec: *const Recording, i: u64) ?Shown { + const moment = rec.moment(i); + 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 { + 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); + 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; + } 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 (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 (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; + 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)); + } + 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); + } +}; + +/// 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. +pub const Stack = struct { + cursor: cursors.Cursor, + box: Rect = theme.layout.stack, + 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); + 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 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)); + 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 }); + var n: i64 = @as(i64, @intFromFloat(@floor(-slide))) - 1; + while (n <= room - @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; + 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)}); + } + 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); + } + } + u.unclip(); + veiled(u, body, 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, + + 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); + } + 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 &.{}; + } +}; + +/// 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 { + cursor: cursors.Cursor, + box: Rect, + 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; + } + return lines[0..n]; + } +}; + +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()); +} |
