summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 20:28:33 -0300
committerGabriel Schneider <[email protected]>2026-11-10 20:28:33 -0300
commitdbfab7b5cdd76787b6cca72f792acfa3cebcbab5 (patch)
tree336f9e84c913991f037328a4132d93af5c62a544
parentdc3b58781420be6070fca896e55776bc27db905d (diff)
downloadcomputeranim-dbfab7b5cdd76787b6cca72f792acfa3cebcbab5.tar.gz
computeranim-dbfab7b5cdd76787b6cca72f792acfa3cebcbab5.zip
debugger: what a write gives the kernel is memory too; --from passes other stops
A write's buffer was recorded as the syscall's payload only: a reader of the moment's memory found there what an earlier stop had seen of it (zeros, in SQLite's page). And record --from with --by syscall took the loader's first syscall for the place and gave up its breakpoint. Both found by the first animation of a real program. Co-Authored-By: Claude Fable 5.2 <[email protected]>
-rw-r--r--README.md1
-rw-r--r--build/debugger.zig13
-rw-r--r--src/cli/record.zig17
-rw-r--r--src/trace/Debugger.zig5
-rw-r--r--src/trace/debugger_test.zig13
5 files changed, 46 insertions, 3 deletions
diff --git a/README.md b/README.md
index 5e3a761..1dd2b84 100644
--- a/README.md
+++ b/README.md
@@ -70,6 +70,7 @@ is any of them, in any order: time travel is an index.
From a debugger's recording, `zt.telescope`: `instruction(m, addr, buf)` is an instruction's text, its source `line`, and for a direct call its callee's `params` (DWARF's); `frames(m, regs, out)` is the call chain with each frame's extent on the stack, `low` to `high` (a `stack()` frame's is up to `sp + 8`).
With `--alloc`, every live allocation and the 16 bytes of allocator header before it are in `read` at each stop (its first 4 KiB; what changed, a stop at a time; 256 KiB of them at most, lowest addresses first: `Debugger.allocation_watch`).
Recorded `--by insn`, the instructions that ran between two moments are the `rip` of each moment between them; `--by line` does not keep them.
+A byte in `read` is as it was when the debugger last sent it: the stack window, the watched regions and the live allocations at every stop, a `read`'s or `write`'s buffer at that syscall (`-s` bytes of it). Memory it looked at once and not since is known and may be stale.
The recorder ends a moment at the kinds in `Recorder.cut` (by default a
step, a breakpoint, a syscall's exit, a signal, a source line, a mark). Of a
diff --git a/build/debugger.zig b/build/debugger.zig
index d164072..6fa6f8c 100644
--- a/build/debugger.zig
+++ b/build/debugger.zig
@@ -105,6 +105,19 @@ pub fn add(ctx: Ctx) void {
from_show.addFileArg(from_recording);
from_show.expectStdOutMatch(" 1 breakpoint\n");
step.dependOn(&from_show.step);
+ // ... and by syscall: the loader's own are passed, not taken for the place.
+ const from_sys = b.addRunArtifact(ctx.exe);
+ from_sys.addArgs(&.{ "record", "--by", "syscall", "--from", "fib", "-o" });
+ const from_sys_recording = from_sys.addOutputFileArg("from-sys.ztrace");
+ from_sys.addArg("--");
+ from_sys.addArtifactArg(fib);
+ from_sys.addArg("10");
+ from_sys.expectStdErrMatch("exit 0");
+ const from_sys_show = b.addRunArtifact(ctx.exe);
+ from_sys_show.addArg("show");
+ from_sys_show.addFileArg(from_sys_recording);
+ from_sys_show.expectStdOutMatch(" 1 breakpoint\n");
+ step.dependOn(&from_sys_show.step);
// A source line a moment, a watched global, to the exit code.
const lines = b.addRunArtifact(ctx.exe);
diff --git a/src/cli/record.zig b/src/cli/record.zig
index d3fb8a9..7c1371a 100644
--- a/src/cli/record.zig
+++ b/src/cli/record.zig
@@ -41,6 +41,20 @@ pub const usage =
\\
++ util.Producer.usage;
+/// Runs the target to the breakpoint `id`, or its end, and clears it:
+/// what else stops it on the way (a syscall, a signal) is recorded and passed.
+fn runTo(d: *zt.Debugger, id: u32) !zt.Debugger.Stop {
+ defer d.clear(id);
+ while (true) {
+ const stop = try d.cont();
+ switch (stop) {
+ .breakpoint => |at| if (at == id) return stop,
+ .exited, .killed => return stop,
+ else => {},
+ }
+ }
+}
+
pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
const arena = init.arena.allocator();
var out: []const u8 = "out.ztrace";
@@ -152,9 +166,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
var full = false;
while (rec.moments < max_moments) {
const stop = (if (entry) |id| stop: {
- defer d.clear(id);
entry = null;
- break :stop d.cont();
+ break :stop runTo(&d, id);
} else switch (by) {
.insn => d.stepi(),
.line => d.step(),
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index bbdaee6..b0ebcba 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -450,6 +450,11 @@ fn atSyscall(d: *Debugger, stop: Stop) !bool {
.read, .pread64 => if (@as(i64, @bitCast(d.regs.rax)) > 0) try d.snapshot(d.regs.rsi, @min(d.regs.rax, d.capture)),
else => {},
};
+ if (stop == .syscall_enter) switch (std.enums.fromInt(linux.SYS, stop.syscall_enter) orelse .close) {
+ // What the kernel is given is the target's memory as it is now: whatever was sent of it before may be stale.
+ .write, .pwrite64 => try d.snapshot(d.regs.rsi, @min(d.regs.rdx, d.capture)),
+ else => {},
+ };
if (d.on_syscall) |hook| return try hook.func(hook.ctx, d, stop) == .stop;
return d.syscalls == .stop;
}
diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig
index bfee1d6..370876a 100644
--- a/src/trace/debugger_test.zig
+++ b/src/trace/debugger_test.zig
@@ -329,12 +329,23 @@ test "the same program under glibc and under musl: the syscalls tell them apart"
try std.testing.expectEqual(@as(usize, 1), glibc.calls(.write));
try std.testing.expectEqual(@as(usize, 0), glibc.calls(.writev));
var opened_libc = false;
+ var buffer: u64 = 0;
for (glibc.events.items) |e| {
if (e.ev.kind != .syscall_enter) continue;
if (e.ev.aux == @backingInt(linux.SYS.openat) and std.mem.endsWith(u8, e.payload, "libc.so.6")) opened_libc = true;
- if (e.ev.aux == @backingInt(linux.SYS.write)) try std.testing.expectEqualStrings("small b\n", e.payload);
+ if (e.ev.aux == @backingInt(linux.SYS.write)) {
+ try std.testing.expectEqualStrings("small b\n", e.payload);
+ buffer = e.ev.a[1];
+ }
}
try std.testing.expect(opened_libc);
+ // What is written is memory too, as it was when the kernel took it.
+ var in_memory = false;
+ for (glibc.events.items) |e| {
+ if (e.ev.kind != .mem_write or e.ev.a[0] > buffer or buffer + 8 > e.ev.a[0] + e.payload.len) continue;
+ in_memory = std.mem.eql(u8, "small b\n", e.payload[buffer - e.ev.a[0] ..][0..8]);
+ }
+ try std.testing.expect(in_memory);
// The library and its mappings are known as they appear.
var mapped_libc = false;
for (glibc.events.items) |e| {