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 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) (limited to 'anim/src/disasm.zig') 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)); + } +} -- cgit v1.3