diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 22:02:01 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 22:02:01 -0300 |
| commit | 36bb1c332a124cb4d437dd8386b267ee58310a5d (patch) | |
| tree | c6760488e17ab443c63b730e6ec70ed43a010634 /src | |
| parent | 0a13f5210756e5a42ebf983943d87a1922cda0e3 (diff) | |
| download | computeranim-36bb1c332a124cb4d437dd8386b267ee58310a5d.tar.gz computeranim-36bb1c332a124cb4d437dd8386b267ee58310a5d.zip | |
record: how much of the context a stop keeps is asked for
--around BYTES of memory each side of what a pointer leads to, for a
viewer that shows the region and not the word; --disasm ROWS above and
below the pc; and every word of the --stack bytes is followed as a
pointer, not the first sixteen.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src')
| -rw-r--r-- | src/cli/record.zig | 22 | ||||
| -rw-r--r-- | src/trace/Debugger.zig | 11 | ||||
| -rw-r--r-- | src/trace/debugger_test.zig | 25 |
3 files changed, 51 insertions, 7 deletions
diff --git a/src/cli/record.zig b/src/cli/record.zig index 7c1371a..91b17d2 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -8,7 +8,8 @@ const fail = util.fail; pub const usage = \\usage: ztrace record [-o FILE] [--by insn|line|syscall|break] [-b WHERE]... [-w WHAT[,LEN]]... - \\ [--from WHERE] [--stack BYTES] [-s BYTES] [--telescope DEPTH] [--steps N] + \\ [--from WHERE] [--stack BYTES] [-s BYTES] [--telescope DEPTH] + \\ [--around BYTES] [--disasm ROWS] [--steps N] \\ [--max-size BYTES] [--strace] \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ @@ -33,9 +34,10 @@ pub const usage = \\long as the symbol unless LEN says. \\At every stop the registers and the top of the stack are also followed \\as pointers, --telescope deep (default 3, 0 for not at all): what they - \\lead to is recorded, code they lead to is named, and the instructions - \\at the pc are disassembled, so that a viewer can show what pwndbg's - \\context shows. + \\lead to is recorded with --around bytes each side of it (default 0: + \\the 32 at the place), code they lead to is named, and the instructions + \\at the pc are disassembled, --disasm rows above it and below (default + \\8), so that a viewer can show what pwndbg's context shows. \\--steps ends the recording after N moments, and --max-size when the \\file is that large (default 1G). Sizes take a suffix: 64K, 4G. \\ @@ -69,6 +71,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var watches: std.ArrayList([]const u8) = .empty; var producer: util.Producer = .{}; var telescope: u8 = 3; + var around: usize = 0; + var disasm_rows: usize = 8; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; @@ -102,6 +106,12 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { } else if (std.mem.eql(u8, arg, "--telescope")) { telescope = std.fmt.parseInt(u8, value.?, 10) catch fail("--telescope needs a depth\n", .{}); i += 1; + } else if (std.mem.eql(u8, arg, "--around")) { + around = std.fmt.parseIntSizeSuffix(value.?, 10) catch fail("--around needs a size\n", .{}); + i += 1; + } else if (std.mem.eql(u8, arg, "--disasm")) { + disasm_rows = std.fmt.parseInt(usize, value.?, 10) catch fail("--disasm needs a number of rows\n", .{}); + i += 1; } else if (std.mem.eql(u8, arg, "--steps")) { max_moments = std.fmt.parseInt(u64, value.?, 0) catch fail("--steps needs a number\n", .{}); i += 1; @@ -130,6 +140,10 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { d.disasm = disasm.one; d.syms.demangle = util.demangle; d.telescope = telescope; + d.telescope_around = around; + d.disasm_around = disasm_rows; + // Every word of the stack that is recorded is followed, not the first sixteen only. + d.telescope_words = @max(d.telescope_words, stack_bytes / 8); d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; if (capture) |n| d.capture = n; diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index a38c337..bdcc4cc 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -93,6 +93,10 @@ functions: std.AutoHashMapUnmanaged(u64, []const u32) = .empty, /// default: not at all (the commands ask for 3). telescope: u8 = 0, telescope_words: usize = 16, +/// The bytes recorded each side of a place the telescope reaches, beside +/// the 32 at it and no further than its mapping: the region a viewer +/// shows round what a pointer leads to. +telescope_around: usize = 0, /// The addresses already named, and the instructions already recorded /// with the length of each. described: std.AutoHashMapUnmanaged(u64, void) = .empty, @@ -605,7 +609,7 @@ fn emitTelescope(d: *Debugger) !void { if (d.telescope == 0) return; const r = &d.regs; for ([_]u64{ r.rip, r.rax, r.rbx, r.rcx, r.rdx, r.rsi, r.rdi, r.rbp, r.rsp, r.r8, r.r9, r.r10, r.r11, r.r12, r.r13, r.r14, r.r15 }) |value| try d.chase(value); - var words: [64]u64 = undefined; + var words: [256]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 @@ -651,8 +655,9 @@ fn chase(d: *Debugger, value: u64) !void { const m = d.syms.mappingAt(v) orelse return; if (m.prot & 1 == 0) return; if (m.prot & 4 != 0) return d.nameAddress(v); - const bytes = try d.snap(v, 32); - const off: usize = @intCast(v & 7); + const lo = @max(m.start, v -| d.telescope_around) & ~@as(u64, 7); + const bytes = try d.snap(lo, @min(m.end, v + 32 + d.telescope_around) - lo); + const off: usize = @intCast(v - lo); if (bytes.len < off + 8) return; v = std.mem.readInt(u64, bytes[off..][0..8], .little); } diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index 512dc6f..5d43d8c 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -173,6 +173,31 @@ test "step walks the source lines, in and out of calls, as gdb does" { try std.testing.expectEqualStrings("main", d2.syms.symbolAt(chain[1]).?.name); } +test "the telescope records the region round what a pointer leads to" { + var log: Log = .{}; + defer log.deinit(); + var d = try spawn(targets.list, "3", log.sink()); + defer d.deinit(); + d.telescope = 1; + d.telescope_around = 16; + // The heap is a mapping the debugger learns of from the program's brk. + d.syscalls = .record; + // At `list_done(head)`: rdi leads to a node on the heap, malloc's header before it. + _ = try d.breakAt(try d.resolve("list_done"), null); + _ = try d.cont(); + const node = d.regs.rdi; + // Sixteen bytes before it to sixteen past its 32, in however many writes. + var seen: u64 = 0; + for (log.events.items) |e| { + if (e.ev.kind != .mem_write) continue; + for (0..64) |i| { + const at = node - 16 + i; + if (at >= e.ev.a[0] and at < e.ev.a[0] + e.payload.len) seen |= @as(u64, 1) << @intCast(i); + } + } + try std.testing.expectEqual(~@as(u64, 0), seen); +} + test "a hook snapshots the linked list; the recording's last moment has what the program built" { var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); |
