diff options
Diffstat (limited to 'src/cli/record.zig')
| -rw-r--r-- | src/cli/record.zig | 19 |
1 files changed, 18 insertions, 1 deletions
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, |
