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/cli/record.zig | 19 ++++++++++++++++++- src/cli/show.zig | 11 ++++++++++- 2 files changed, 28 insertions(+), 2 deletions(-) (limited to 'src/cli') diff --git a/src/cli/record.zig b/src/cli/record.zig index 8f002f4..d3d7a64 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -10,7 +10,7 @@ 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] \\ [--around BYTES] [--disasm ROWS] [--steps N] - \\ [--max-size BYTES] [--strace] + \\ [--max-size BYTES] [--strace] [--heap] \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ \\Runs PROGRAM under ptrace into a recording (default out.ztrace). --by @@ -39,6 +39,11 @@ pub const usage = \\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. + \\--heap also records glibc's heap at every stop, as `ztrace show` reads it + \\(pwndbg's heap and bins): each chunk's size word, the main arena, the + \\tcache, and the links of every chunk on a list; at most 4096 chunks and + \\256 entries a list. glibc 2.40 and 2.44 only, linked dynamically or + \\statically; the main arena's heap, the stopped thread's tcache. \\--steps ends the recording after N moments, and --max-size when the \\file is that large (default 1G). Sizes take a suffix: 64K, 4G. \\ @@ -67,6 +72,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var capture: ?usize = null; var max_bytes: ?u64 = null; var strace = false; + var heap = false; var from: ?[]const u8 = null; var breaks: std.ArrayList([]const u8) = .empty; var watches: std.ArrayList([]const u8) = .empty; @@ -85,6 +91,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { by = .syscall; } else if (std.mem.eql(u8, arg, "--strace")) { strace = true; + } else if (std.mem.eql(u8, arg, "--heap")) { + heap = true; } else if (producer.take(args, &i)) { // Its own argument, when it has one, is taken. } else if (value == null) { @@ -147,6 +155,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { d.telescope_words = @max(d.telescope_words, stack_bytes / 8); d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; + if (heap) d.heap = .{}; if (capture) |n| d.capture = n; for (breaks.items) |where| { if (std.mem.startsWith(u8, where, "usdt:")) { @@ -214,6 +223,14 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { try rec.close(); const size = (try std.Io.Dir.cwd().statFile(init.io, out, .{})).size; if (full) std.debug.print("{s}: full at {d} bytes, the program stopped there: record less (--by line, syscall or break; fewer -w; a smaller --stack or -s) or raise --max-size\n", .{ out, size }); + if (d.heap) |h| if (h.glibc) |g| { + std.debug.print("heap: glibc {d}.{d}{s}, main_arena {x}{s}\n", .{ g.major, g.minor, if (g.by_size != 0) " (no version in the binary: by the size of main_arena)" else "", g.arena, if (h.tcache == null) ", no tcache seen" else "" }); + } else switch (h.trouble) { + .none => std.debug.print("heap: not recorded: no glibc malloc found\n", .{}), + .no_version => std.debug.print("heap: not recorded: no glibc version in the C library, nor a main_arena symbol\n", .{}), + .no_layout => std.debug.print("heap: not recorded: no layout known for glibc {d}.{d} (known: 2.40, 2.44)\n", .{ h.version[0], h.version[1] }), + .no_arena => std.debug.print("heap: not recorded: more than one place in glibc looks like the main arena\n", .{}), + }; std.debug.print("{s}: {d} moments, {d} events, {d} bytes ({d} bytes/moment), {d:.1} ms, {d:.0} moments/s, exit {d}\n", .{ out, moments, events, size, diff --git a/src/cli/show.zig b/src/cli/show.zig index 1657644..7019113 100644 --- a/src/cli/show.zig +++ b/src/cli/show.zig @@ -11,7 +11,10 @@ pub const usage = \\pwndbg's way: each thread's registers, every one followed as far as it \\points (a name for code, a string where there is one), the instructions \\at the pc, the top of the stack followed likewise, the source line and - \\the open calls; then the live heap, the named variables with their + \\the open calls; then glibc's heap when it was recorded (record --heap: + \\its chunks, the tcache and the bins, each chunk on a list followed by its + \\link as stored, in brackets, where safe-linking changed it), the live + \\allocations, the named variables with their \\bytes, and the mapped regions. On a terminal a pointer is coloured by \\where it points: code red, stack yellow, heap blue, data purple, \\writable-and-executable underlined. @@ -140,6 +143,12 @@ pub fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io. } } + // glibc's heap (`record --heap`), with the tcache of the thread that stopped. + var last_tid: u32 = 0; + var events = try m.events(arena); + while (events.next()) |item| last_tid = item.ev.tid; + if (try zt.heap.glibc(arena, m, last_tid)) |h| try zt.heap.print(h, w); + const heap = try m.heap(arena); if (heap.len > 0) try w.print("\nheap: {d} live\n", .{heap.len}); for (heap) |a| try w.print(" {x:0>12} {d:>8} bytes, by {x}, event {d}\n", .{ a.ptr, a.size, a.pc, a.born }); -- cgit v1.3