summaryrefslogtreecommitdiff
path: root/src/trace/Debugger.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 14:35:56 -0300
committerGabriel Schneider <[email protected]>2026-11-10 14:39:55 -0300
commit97ce7f485fc0419df693fdf16d634e9e9a6085fc (patch)
tree9d36463512024242665c7836b94a4de4e541b3bf /src/trace/Debugger.zig
parentf8740f6996eefc235af066a6686f52847c158a6b (diff)
downloadcomputeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.tar.gz
computeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.zip
context: what an instruction is about to touch, and the call chain
At a stop the instruction at the pc is decoded against the registers it will run with (capstone's operands): a touch event for each memory operand (address, size, read or write), what is there recorded with it, and what a write left there read back at the next stop. An instruction-level recording so has every memory write of the stepped thread, nothing watched. The frames of the call chain are recorded and their return addresses named; telescope.backtrace walks them from the recording and ztrace show prints them. Process.read no longer panics on address 0.
Diffstat (limited to 'src/trace/Debugger.zig')
-rw-r--r--src/trace/Debugger.zig46
1 files changed, 41 insertions, 5 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 41a908f..fd5431f 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -68,7 +68,11 @@ scratch: std.ArrayList(u8) = .empty,
/// Decodes one instruction into `text`; null where there is none. Whoever
/// has a disassembler sets it (the ztrace command links capstone): the
/// library has none of its own.
-disasm: ?*const fn (code: []const u8, addr: u64, text: []u8) ?Insn = null,
+disasm: ?*const fn (code: []const u8, addr: u64, text: []u8, regs: ?*const Regs, touches: []Touch) ?Insn = null,
+/// What the instruction of the last stop was about to write, to be read
+/// back at the next: an instruction's writes, with no region watched.
+written: [2]Touch = undefined,
+written_count: usize = 0,
/// The instructions from the pc on whose text a stop records.
disasm_ahead: usize = 8,
/// How far a stop follows pointers, from each register and from each of
@@ -115,8 +119,11 @@ pub const Hook = struct {
pub const Action = enum { go, stop };
pub const Breakpoint = struct { id: u32, addr: u64, saved: u8, hook: ?Hook };
-/// An instruction decoded: its length and its text ("mov rbp, rsp").
-pub const Insn = struct { len: u8, text: []const u8 };
+/// An instruction decoded: its length, its text ("mov rbp, rsp"), and with
+/// the registers given, the memory it is about to touch.
+pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{} };
+/// Memory an instruction touches: `how` is 1 for a read, 2 a write, 3 both.
+pub const Touch = extern struct { addr: u64, size: u8, how: u8 };
pub const Watch = struct { addr: u64, len: usize };
const Page = struct {
@@ -399,7 +406,25 @@ fn report(d: *Debugger, stop: Stop) !Stop {
/// recorded once: what a viewer shows as the disassembly.
fn emitCode(d: *Debugger) !void {
const insnAt = d.disasm orelse return;
+ // What the instruction before wrote is there to read now.
+ for (d.written[0..d.written_count]) |t| try d.snapshot(t.addr, t.size);
+ d.written_count = 0;
var code: [256]u8 = undefined;
+ var text: [192]u8 = undefined;
+ // The instruction at the pc, against the registers it will run with:
+ // what it reads is recorded as it is, what it writes is kept for the
+ // stop after.
+ if (d.read(d.regs.rip, code[0..15]) > 0) {
+ var touches: [2]Touch = undefined;
+ if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| for (insn.touches) |t| {
+ try d.snapshot(t.addr, t.size);
+ try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, "");
+ if (t.how & 2 != 0) {
+ d.written[d.written_count] = t;
+ d.written_count += 1;
+ }
+ };
+ }
// Read only when an instruction not seen before is met: a loop costs nothing.
var n: ?usize = null;
var at: usize = 0;
@@ -412,8 +437,7 @@ fn emitCode(d: *Debugger) !void {
if (n == null) n = d.read(d.regs.rip, code[0..@min(code.len, d.disasm_ahead * 15)]);
if (at >= n.?) break;
// A byte for the length, then the text.
- var text: [192]u8 = undefined;
- const insn = insnAt(code[at..n.?], addr, text[1..]) orelse break;
+ const insn = insnAt(code[at..n.?], addr, text[1..], null, &.{}) orelse break;
text[0] = insn.len;
try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 1 + insn.text.len]);
try d.code_len.put(d.gpa, addr, insn.len);
@@ -430,6 +454,18 @@ fn emitTelescope(d: *Debugger) !void {
var words: [64]u64 = undefined;
const n = d.read(r.rsp, std.mem.sliceAsBytes(words[0..@min(words.len, d.telescope_words)])) / 8;
for (words[0..n]) |value| try d.chase(value);
+ // The call chain: each frame's saved pointer and return address
+ // recorded, the return addresses named, so a viewer walks it again.
+ var fp = r.rbp;
+ for (0..16) |_| {
+ if (fp == 0 or fp & 7 != 0) break;
+ const frame = try d.snap(fp, 16);
+ if (frame.len < 16) break;
+ const next = std.mem.readInt(u64, frame[0..8], .little);
+ try d.chase(std.mem.readInt(u64, frame[8..16], .little));
+ if (next <= fp) break;
+ fp = next;
+ }
}
/// Follows `value` as a pointer: a pointer into code gets its name and