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 ++++++++------- gui/README.md | 7 ++++ gui/bench/RESULTS.md | 17 ++++++++++ gui/src/app.zig | 2 +- gui/targets/segments.c | 9 ++++- gui/test/lenses.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 206 insertions(+), 17 deletions(-) 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 diff --git a/gui/README.md b/gui/README.md index 288261e..36e4a1c 100644 --- a/gui/README.md +++ b/gui/README.md @@ -91,6 +91,12 @@ surrounding rows dim. Shift-wheel, headers and gaps scroll the lens chain. Right-click sends stack addresses to the stack pane and every other address to memory. Matching addresses get staggered background highlights: the selected occurrence appears first, followed by its peers. Lens ancestry keeps its connectors. +Stack offsets use the same hover/click behavior as their storage addresses. +Disassembly brackets also link simple 64-bit base/index/scale/displacement addresses, +including `lea`, using the selected stop's registers only at its PC. RIP-relative +and absolute addresses work on other rows too. Future/past register values, +segment overrides and 32-bit addressing are not inferred. Truncated brackets +have no hidden hit target. These links work in executable lenses too. Memory's two header controls choose 8/16/32/64-bit groups and hexadecimal, unsigned, signed or floating-point values (little endian). `memory-format SIZE @@ -99,6 +105,7 @@ floats require 4 or 8 bytes. Unknown bytes stay unknown; these controls only change rendering. Recognized pointer fields are clickable across all their bytes, including 8-bit view: left-click opens a lens; right-click opens stack or memory. Wide values remain reachable with Shift-h/l. +Hex bytes have no persistent group background; hover and access highlights remain. Lenses keep an opaque reading surface, with a native GPU backdrop shader for edge refraction, subtle color separation and a cyan rim. A canvas-sized texture and one reusable lens snapshot diff --git a/gui/bench/RESULTS.md b/gui/bench/RESULTS.md index 54ecf4e..af16175 100644 --- a/gui/bench/RESULTS.md +++ b/gui/bench/RESULTS.md @@ -196,3 +196,20 @@ The updated native session (GUI20122, d8 20207) remains at `Deoptimizer::New+4` `0x7f24dbbe3f74`, with wrong-map origin `0x7f247ac00ffa` and JS guard `0x7f247ac00c14`; recording `/tmp/ztrace-gui-20122-9224949592653.ztrace`. A native 8-bit hexdump click opened the saved-stack-pointer lens successfully. + +Stack offsets now expose their storage addresses to the shared hover/click path. +Hex bytes no longer have persistent group backgrounds. Disassembly brackets link +simple 64-bit effective addresses, including LEA, only from registers at the +selected moment's PC; RIP-relative and absolute addresses also work on other rows. +The shared renderer suppresses links when the complete bracket is not displayed. +Native and headless checks cover hover, both buttons, indexed operands, future +register operands staying inactive, and historical inspection. The GUI suite +passes with `-j1`; the shared-renderer suite exits successfully, including gallery +comparisons (its usual headless Vulkan warnings remain). Independent code and +screenshot review found no blocking issues. V8 timings were not remeasured. + +Reloaded live session: GUI25666, d8 25705, recording +`/tmp/ztrace-gui-25666-14341833166407.ztrace`. The target is stopped at +`Deoptimizer::New+4`, `0x7f99e5be3f74`, on `deoptimizer.cc:290`. +`rdx = 0x7f9984c00ffa` exactly matches the naturally optimized `jitWork` wrong-map +exit return; originating JS context uses guard `0x7f9984c00c14` at `jit.js:3`. diff --git a/gui/src/app.zig b/gui/src/app.zig index c9600b8..f5ebf3d 100644 --- a/gui/src/app.zig +++ b/gui/src/app.zig @@ -1283,7 +1283,7 @@ fn memoryRow(u: ui.Ui, m: zt.store.Moment, addr: u64, y: f32, room: f32, ascii: if (changed == null) changed = .{ @intCast(j - offset), @intCast(j - offset) } else changed.?[1] = @intCast(j - offset); }; const read: f32 = if (touch) |t| if (t.addr < addr +| offset +| 8 and t.addr +| t.len > addr +| offset) 1 else 0 else 0; - run.* = .{ .first = @intCast(offset), .len = 8, .kind = if (canim.rows.regionOf(m, word) != .none) .pointer else .number, .read = read, .heat = if (changed != null) heat else .none, .changed = changed }; + run.* = .{ .first = @intCast(offset), .len = 8, .background = false, .kind = if (canim.rows.regionOf(m, word) != .none) .pointer else .number, .read = read, .heat = if (changed != null) heat else .none, .changed = changed }; } const shift = canim.rows.addressRoom(addr); const text_x: f32 = if (unit == 1 and format == .hex) blk: { diff --git a/gui/targets/segments.c b/gui/targets/segments.c index c52ad3b..b1c51aa 100644 --- a/gui/targets/segments.c +++ b/gui/targets/segments.c @@ -9,6 +9,13 @@ struct node head, middle, tail; volatile uint64_t counter; uint64_t *stack_pointer; __attribute__((noinline)) void checkpoint(void) { __asm__ volatile("" ::: "memory"); } +__attribute__((naked, noinline)) void address_probe(uint64_t *stack, struct node *data, size_t index) { + __asm__ volatile( + ".globl operand_stack\noperand_stack: lea 8(%rdi), %rax\n" + ".globl operand_indexed\noperand_indexed: lea (%rsi,%rdx,8), %rax\n" + ".globl operand_relative\noperand_relative: mov head(%rip), %rax\n" + "ret\n"); +} int main(void) { uint64_t stack_value = 42; @@ -24,5 +31,5 @@ int main(void) { head = (struct node){&middle, 1}; middle = (struct node){&tail, 2}; tail = (struct node){&head, 3}; - for (;;) { ++counter; checkpoint(); } + for (;;) { ++counter; checkpoint(); address_probe(&stack_value, &head, 2); } } diff --git a/gui/test/lenses.py b/gui/test/lenses.py index 9d287fa..528c656 100644 --- a/gui/test/lenses.py +++ b/gui/test/lenses.py @@ -27,6 +27,33 @@ console break checkpoint console continue await execution 1 frames 40 +# Offsets, including negative/zero/positive ones, address their stack storage. +move 0 0 +frames 30 +screenshot {out / 'offset-before.png'} +move 185 863 +frames 30 +screenshot {out / 'offset-hover.png'} +expect-different-screenshot {out / 'offset-before.png'} {out / 'offset-hover.png'} 156 852 42 24 +click 250 863 +print lenses[0].address +key escape +click 185 863 +print lenses[0].address +key escape +console mouse-button 3 185 863 +expect focus stack +print nav[1].place.address +expect nav[1].place.follow false +key f +frames 40 +click 185 935 +print lenses[0].address +key escape +click 185 959 +print lenses[0].address +key escape +expect execution 1 console lens {code} expect lens_count 1 expect lenses[0].first {code} @@ -141,6 +168,60 @@ frames 30 print stops expect execution 1 key escape +# Computed operands use this stop's registers; future register operands stay inert. +hold left_shift +key f +release left_shift +console break operand_stack +key c +await execution 2 +expect pc {symbols['operand_stack']} +console set source false +console set annotations false +console goto code {symbols['operand_stack']} +frames 40 +click 400 180 +expect lens_count 0 +move 0 0 +frames 30 +screenshot {out / 'operand-before.png'} +move 400 156 +frames 30 +screenshot {out / 'operand-hover.png'} +expect-different-screenshot {out / 'operand-before.png'} {out / 'operand-hover.png'} 348 144 108 24 +click 400 156 +expect lens_count 1 +print lenses[0].address +key escape +console mouse-button 3 400 156 +expect focus stack +print nav[1].place.address +# RIP-relative storage is independent of future register values. +click 400 204 +expect lenses[0].address {head} +key escape +console mouse-button 3 400 204 +expect focus memory +expect nav[2].place.address {head} +key s +await execution 3 +expect pc {symbols['operand_indexed']} +frames 40 +click 400 180 +expect lenses[0].address {head + 16} +key escape +console mouse-button 3 400 180 +expect focus memory +expect nav[2].place.address {head + 16} +# Historical operands use historical registers, without another execution. +key left_bracket +expect following false +console goto code {symbols['operand_stack']} +frames 40 +click 400 156 +print lenses[0].address +key escape +expect execution 3 expect-no-log CommandFailed expect-no-log backdrop shader ''' @@ -155,9 +236,13 @@ expect-no-log backdrop shader first = int(re.search(r'lenses\[0\].first = (\d+)', stderr)[1]) assert first > code, (first, code) stack_addresses = re.findall(r'(?:lenses\[0\].address|nav\[1\].place.address) = (\d+)', stderr) - assert len(stack_addresses) == 2 and stack_addresses[0] == stack_addresses[1], stack_addresses + assert len(stack_addresses) == 10, stack_addresses + base, negative, followed, zero, positive, pointer, pointer_followed, operand, operand_followed, historical = map(int, stack_addresses) + assert base == negative == followed and zero == base + 24 and positive == base + 32, stack_addresses + assert pointer == pointer_followed, stack_addresses + assert operand == operand_followed == historical == pointer + 8, stack_addresses stops = re.findall(r'stops = (\d+)', stderr) assert len(stops) == 2 and stops[0] == stops[1], stops for recording in Path('/tmp').glob(f'ztrace-gui-{run.pid}-*.ztrace'): recording.unlink() - print(('Native' if a.native else 'Headless') + ': code/data lenses, context scrolling, pointer recursion, memory formats and historical isolation passed') + print(('Native' if a.native else 'Headless') + ': lenses, stack offsets, computed operands, pointer bytes, formats and historical isolation passed') -- cgit v1.3