From 58643dab07a803dbb579d84f68419911f5a2a16d Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 23:04:17 -0300 Subject: trace: glibc's heap, read from a recording at any moment record --heap keeps the arena, the tcache and, of every chunk, its size word and the links of those on a list: free chunks stay readable, which --alloc dropped at the free. zt.heap.glibc gives the chunks in address order, the tcache's and the bins' chains (safe-linking as the stored value and the pointer it protects), the top; each with the address of the word it was read from, each chain saying how it ended. ztrace show prints it. Layouts for glibc 2.44 (no fastbins; a tcache made at the first free, counting the slots left) and 2.40; the arena found by symbol or by the struct whose next points at itself. Against pwndbg on 2.40: the same at every stop. Three bytes a moment and a sixth slower on the list by instruction. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/trace/Debugger.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/trace/Debugger.zig') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index 5310ad3..f4f945b 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -66,6 +66,8 @@ allocations: std.ArrayList(Watch) = .empty, /// The most read of them at a stop, in bytes: a stop's cost must not grow /// with the target's heap. allocation_watch: usize = 256 << 10, +/// glibc's heap, recorded at every stop as `heap.zig` reads it (`--heap`): null for not at all. +heap: ?@import("heap.zig").Watch = null, breakpoints: std.ArrayList(Breakpoint) = .empty, /// The current thread's registers at its last stop. regs: Regs = .{}, @@ -490,6 +492,7 @@ fn report(d: *Debugger, stop: Stop) !Stop { for (d.watches.items) |w| try d.snapshot(w.addr, w.len); for (d.declared.items) |w| try d.snapshot(w.addr, w.len); try d.snapshotAllocations(); + if (d.heap) |*h| try h.atStop(d); try d.emitCode(); try d.emitTelescope(); } @@ -1136,7 +1139,7 @@ pub fn cfi(d: *Debugger, pc: u64) ?Symbols.Cfi { /// The target's memory as a walk of the call chain reads it, every word it /// reads recorded, and the frame of every function it goes through /// (`emitFunction`): so a reader walks the same chain from the recording. -const Recorded = struct { +pub const Recorded = struct { d: *Debugger, failed: ?anyerror = null, /// The stack as last read, a page at a time: a walk goes up it. -- cgit v1.3