From 4b76c25dc90417a9d6c3d4cd5af0dd3d4c087520 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 13 Nov 2026 13:56:12 -0300 Subject: debugger: link computed operands and stack offsets --- anim/src/disasm.zig | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++-- anim/src/rows.zig | 26 +++++++++++-------- 2 files changed, 86 insertions(+), 13 deletions(-) (limited to 'anim') diff --git a/anim/src/disasm.zig b/anim/src/disasm.zig index b3049f1..3748eac 100644 --- a/anim/src/disasm.zig +++ b/anim/src/disasm.zig @@ -1058,6 +1058,42 @@ fn parseImmediate(o: []const u8) ?u64 { return if (negative) 0 -% v else v; } +/// An explicit 64-bit address, using registers only at this moment's PC. +fn operandAddress(row: Insn, regs: zt.Regs) ?u64 { + const ops = row.operands(); + if (row.padding() or std.mem.eql(u8, row.mnemonic(), "pop")) return null; + // shortcut: segment overrides and address-size overrides stay inert; add them when needed. + if (std.mem.indexOfScalar(u8, ops, ':') != null) return null; + const open = std.mem.indexOfScalar(u8, ops, '[') orelse return null; + const close = std.mem.indexOfScalarPos(u8, ops, open, ']') orelse return null; + if (std.mem.indexOfScalarPos(u8, ops, close + 1, '[') != null) return null; + var rest = std.mem.trim(u8, ops[open + 1 .. close], " "); + if (rest.len == 0) return null; + var result: u64 = 0; + var subtract = false; + while (rest.len > 0) { + const end = std.mem.indexOfAny(u8, rest, "+-") orelse rest.len; + const term = std.mem.trim(u8, rest[0..end], " "); + const star = std.mem.indexOfScalar(u8, term, '*') orelse term.len; + const name = std.mem.trim(u8, term[0..star], " "); + const value = if (std.mem.eql(u8, name, "rip")) row.addr +% row.len else if (full(name)) |reg| blk: { + if (row.addr != regs.rip or !std.mem.eql(u8, name, reg)) return null; + break :blk regs.named(reg) orelse return null; + } else parseImmediate(name) orelse return null; + const scale = if (star < term.len) blk: { + if (full(name) == null) return null; + const n = parseImmediate(std.mem.trim(u8, term[star + 1 ..], " ")) orelse return null; + if (n != 1 and n != 2 and n != 4 and n != 8) return null; + break :blk n; + } else @as(u64, 1); + result = if (subtract) result -% (value *% scale) else result +% (value *% scale); + if (end == rest.len) return result; + subtract = rest[end] == '-'; + rest = std.mem.trim(u8, rest[end + 1 ..], " "); + } + return null; +} + /// `v` as a number `bytes` wide: signed and in decimal when small, else hex. fn number(u: ui.Ui, v: u64, bytes: u8) []const u8 { const bits: u7 = @as(u7, @min(bytes, 8)) * 8; @@ -1244,7 +1280,9 @@ pub fn liveRow(at: ui.Ui, row: Insn, y: f32, width: f32, moment: Recording.Momen var name_buf: [192]u8 = undefined; const name = if (target(row)) |to| (moment.string(to, &name_buf) catch null) orelse "" else ""; const annotated = options.current or options.executed; - operands(u, row, shorn(u, row), name, base_y, (if (annotated) annotationColumn() else room) - gap - c.operands, if (pad) t.faint else t.primary); + var ops = shorn(u, row); + ops.address = operandAddress(row, regs); + operands(u, row, ops, name, base_y, (if (annotated) annotationColumn() else room) - gap - c.operands, if (pad) t.faint else t.primary); if (!annotated) return; var x = annotationColumn(); if (options.branch) |branch| { @@ -1308,7 +1346,7 @@ pub fn bareRow(u: ui.Ui, row: Insn, y: f32) void { /// register outside the brackets has the access's size and the mnemonic /// does not fix it, how many bytes it touches (`bytes`), drawn as byte /// cells after the `]` that ends at `after`. -const Operands = struct { text: []const u8, after: usize = 0, bytes: u8 = 0 }; +const Operands = struct { text: []const u8, after: usize = 0, bytes: u8 = 0, address: ?u64 = null }; fn shorn(u: ui.Ui, row: Insn) Operands { var ops = row.operands(); @@ -1406,9 +1444,16 @@ fn operands(u: ui.Ui, row: Insn, ops: Operands, stub: []const u8, base_y: f32, w return; } const text = ops.text; + var shown: usize = 0; + defer if (ops.address) |address| { + const open = std.mem.indexOfScalar(u8, text, '[').?; + const close = std.mem.indexOfScalarPos(u8, text, open, ']').?; + if (close < shown) u.hitAddress(address, .{ .x = c.operands + @as(f32, @floatFromInt(open)) * adv, .y = base_y - rows.baseline(0), .w = @as(f32, @floatFromInt(close + 1 - open)) * adv, .h = m.row }); + }; const extra = cellsWidth(u, ops.bytes); const at_after = c.operands + @as(f32, @floatFromInt(ops.after)) * adv; if (@as(f32, @floatFromInt(text.len)) * adv + extra <= width) { + shown = text.len; if (ops.bytes == 0) return u.text(.mono, size, c.operands, base_y, text, ink); u.text(.mono, size, c.operands, base_y, text[0..ops.after], ink); cells(u, at_after, base_y, ops.bytes); @@ -1422,6 +1467,7 @@ fn operands(u: ui.Ui, row: Insn, ops: Operands, stub: []const u8, base_y: f32, w if (std.mem.indexOfScalarPos(u8, text, open, ']') orelse text.len >= cut) cut = open; } while (cut > 0 and text[cut - 1] == ' ') cut -= 1; + shown = cut; const shown_cells = ops.bytes > 0 and cut >= ops.after; u.text(.mono, size, c.operands, base_y, text[0..if (shown_cells) ops.after else cut], ink); var end = c.operands + @as(f32, @floatFromInt(cut)) * adv; @@ -1566,3 +1612,26 @@ test "a register is known by any of its widths" { try std.testing.expectEqualStrings("rax", full("al").?); try std.testing.expect(full("qword ptr [rbp - 8]") == null); } + +test "operand addresses use current registers, never future or past register values" { + const regs: zt.Regs = .{ .rip = 0x1000, .rbp = 0x8000, .rax = 0x4000, .rcx = 3 }; + for ([_]struct { []const u8, ?u64 }{ + .{ "mov rax, qword ptr [rbp - 8]", 0x7ff8 }, + .{ "lea rdi, [rax + rcx*8 + 0x10]", 0x4028 }, + .{ "mov rax, [rip - 0x10]", 0xff7 }, + .{ "mov rax, [rcx - 8]", 0xffff_ffff_ffff_fffb }, + .{ "mov rax, [0x8000]", 0x8000 }, + .{ "mov rax, fs:[0x28]", null }, + .{ "mov rax, [eax + 8]", null }, + .{ "mov rax, [rax + xmm0*8]", null }, + .{ "mov rax, [rax + rcx*3]", null }, + .{ "mov rax, [rax +]", null }, + .{ "mov rax, [rax + unknown]", null }, + .{ "pop qword ptr [rsp]", null }, + .{ "nop dword ptr [rax]", null }, + }) |case| try std.testing.expectEqual(case[1], operandAddress(.{ .addr = regs.rip, .len = 7, .text = case[0] }, regs)); + for ([_]u64{ 0xff0, 0x1010 }) |addr| { + try std.testing.expectEqual(@as(?u64, null), operandAddress(.{ .addr = addr, .len = 7, .text = "lea rdi, [rax + rcx*8]" }, regs)); + try std.testing.expectEqual(@as(?u64, addr + 7 + 0x10), operandAddress(.{ .addr = addr, .len = 7, .text = "mov rax, [rip + 0x10]" }, regs)); + } +} diff --git a/anim/src/rows.zig b/anim/src/rows.zig index 3d0c75c..aaa6b00 100644 --- a/anim/src/rows.zig +++ b/anim/src/rows.zig @@ -364,9 +364,9 @@ pub fn slot(u: ui.Ui, y: f32, w: f32, moment: Recording.Moment, addr: u64, from_ if (pins.len > 0 and pin_shows > 0) u.faded(pin_shows).pinRow(s.lane, y, pins, theme.color.region.stack); // An offset that changes with rsp (the window held, or moved by more than the slide) crossfades as a role does. if (was) |o| if (o.from_rsp) |old| if (old != from_rsp and (o.offset_going orelse o.going) < 1) { - offset(u.faded(1 - (o.offset_going orelse o.going)), y, old); - offset(u.faded(o.offset_going orelse o.going), y, from_rsp); - } else offset(u, y, from_rsp) else offset(u, y, from_rsp) else offset(u, y, from_rsp); + offset(u.faded(1 - (o.offset_going orelse o.going)), y, addr, old); + offset(u.faded(o.offset_going orelse o.going), y, addr, from_rsp); + } else offset(u, y, addr, from_rsp) else offset(u, y, addr, from_rsp) else offset(u, y, addr, from_rsp); var lead = marks; lead.first = .none; const base = baseline(y); @@ -406,10 +406,13 @@ pub fn hexMark(n: u64) []const u8 { } /// A slot's offset from rsp, `from_rsp` bytes: hex, faint, ranged right to 172. -fn offset(u: ui.Ui, y: f32, from_rsp: i64) void { - const p = hexMark(@abs(from_rsp)); - const text = if (from_rsp < 0) u.print("-{s}{x}", .{ p, @as(u64, @intCast(-from_rsp)) }) else u.print("+{s}{x}", .{ p, @as(u64, @intCast(from_rsp)) }); - u.text(.mono, size, theme.metric.slot.offset - u.width(.mono, size, text), baseline(y), text, t.faint); +fn offset(u: ui.Ui, y: f32, addr: u64, from_rsp: i64) void { + const magnitude = @abs(from_rsp); + const text = u.print("{s}{s}{x}", .{ if (from_rsp < 0) "-" else "+", hexMark(magnitude), magnitude }); + const width = u.width(.mono, size, text); + const x = theme.metric.slot.offset - width; + u.text(.mono, size, x, baseline(y), text, t.faint); + u.hitAddress(addr, .{ .x = x, .y = y, .w = width, .h = row_h }); } /// `slot` `free`: a word above rsp the stack has not reached, shown while @@ -420,9 +423,9 @@ pub fn free(u: ui.Ui, y: f32, moment: Recording.Moment, addr: u64, from_rsp: i64 const base = baseline(y); // An offset whose base the step moves crossfades, as a slot's does. if (was) |o| if (o.from_rsp) |old| if (old != from_rsp and o.going < 1) { - offset(u.faded(1 - o.going), y, old); - offset(u.faded(o.going), y, from_rsp); - } else offset(u, y, from_rsp) else offset(u, y, from_rsp) else offset(u, y, from_rsp); + offset(u.faded(1 - o.going), y, addr, old); + offset(u.faded(o.going), y, addr, from_rsp); + } else offset(u, y, addr, from_rsp) else offset(u, y, addr, from_rsp) else offset(u, y, addr, from_rsp); const end = u.address(s.address, base, addr, .{ .ink = ui.mix(theme.color.region.stack, theme.color.page, theme.metric.context.lead_alpha), .lead = lead }); const word = (Recording.word(moment, addr, 8) catch null) orelse return u.unknown(.{ .x = end + m.arrow_w, .y = y, .w = 24, .h = row_h }); u.text(.mono, size, end + m.arrow_w, base, u.print("{x}", .{word}), t.faint); @@ -544,6 +547,7 @@ pub const Run = struct { first: u8, len: u8, kind: enum { number, pointer, text, loose, unread }, + background: bool = true, /// Its mark: struck (heat), read (shows), held is in the heat. heat: ui.Heat = .none, read: f32 = 0, @@ -587,7 +591,7 @@ pub fn bytesAt(at: ui.Ui, y: f32, addr: u64, values: *const [16]u8, known: *cons 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 or run.kind == .text; - if (field) u.fill(r, theme.color.raised); + if (field and run.background) u.fill(r, theme.color.raised); // Read is per run (a frame 3 out). A write's marks cover the bytes // whose value changed, first to last (`changed`): armed, then struck // a byte a frame from the low address (the softened paper, the -- cgit v1.3