From 02aa51a6f54235cf18e627dbbfe21f21e502eb0b Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 13:36:32 -0300 Subject: context: disassembly, telescopes and names in every stop; strings as one table For a viewer to show what pwndbg's context shows, the debugger records it while the memory is there: each register and the top of the stack followed as pointers three deep (--telescope), every code address met named, eight instructions from the pc disassembled by capstone (built from source for x86, into the command only; pinned as in 03-codenomicom). zt.telescope reads a chain back with the kind of memory each link points into, and ztrace show prints the context, coloured on a terminal as pwndbg colours. The thirty thousand strings of a small run showed that a string map in each keyframe costs 700 bytes of tree a string: strings are now one table for the recording, appended a batch and read whole at open. fib(12) by instruction with everything on: 9 MB, 71 000 moments/s. --- src/cli/show.zig | 62 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 54 insertions(+), 8 deletions(-) (limited to 'src/cli/show.zig') diff --git a/src/cli/show.zig b/src/cli/show.zig index ce8dec4..422fd9e 100644 --- a/src/cli/show.zig +++ b/src/cli/show.zig @@ -7,9 +7,14 @@ pub const usage = \\usage: ztrace show FILE [MOMENT] \\ \\Without MOMENT: how many moments and events, and events by kind. - \\With one (negative counts from the end): each thread's registers, source - \\line and open calls, the live heap, the named variables with their - \\bytes, and the mapped regions, as of that moment. + \\With one (negative counts from the end), the context as of that moment, + \\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 + \\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. \\ ; @@ -24,7 +29,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const n = std.fmt.parseInt(i64, args[1], 10) catch util.fail(usage, .{}); const index = if (n < 0) @as(i64, @intCast(trace.len)) + n else n; if (index < 0 or index >= trace.len) util.fail("{s}: {d} moments, no moment {d}\n", .{ args[0], trace.len, n }); - break :blk state(init.arena.allocator(), &trace, @intCast(index), w); + break :blk state(init.arena.allocator(), &trace, @intCast(index), w, std.Io.File.stdout().isTty(init.io) catch false); }; result catch |err| switch (err) { // The reader went away (`| head`): not an error. @@ -54,7 +59,27 @@ fn name(m: zt.store.Moment, arena: std.mem.Allocator, id: u64) ![]const u8 { return (try m.string(id, buf)) orelse std.fmt.allocPrint(arena, "#{x}", .{id}); } -fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.Writer) !void { +/// A value and what it leads to: `7ffd1000 —▸ 5555b010 ◂— "hello"`. +fn telescope(m: zt.store.Moment, w: *std.Io.Writer, value: u64, colour: bool) !void { + var links: [5]zt.telescope.Link = undefined; + var text: [64]u8 = undefined; + for (try zt.telescope.chase(m, value, &links, &text), 0..) |link, i| { + if (i > 0) try w.writeAll(if (link.region == .none) " ◂— " else " —▸ "); + const paint: []const u8 = if (!colour) "" else switch (link.region) { + .none => "", + .code => "\x1b[31m", + .rwx => "\x1b[4;31m", + .stack => "\x1b[33m", + .heap => "\x1b[34m", + .data => "\x1b[35m", + .rodata => "\x1b[2m", + }; + try w.print("{s}{x}{s}", .{ paint, link.value, if (paint.len > 0) "\x1b[0m" else "" }); + if (link.string) try w.print(" ◂— \"{f}\"", .{std.zig.fmtString(link.text)}) else if (link.text.len > 0) try w.print(" ({s})", .{link.text}); + } +} + +fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.Writer, colour: bool) !void { const m = try trace.moment(index); try w.print("moment {d} of {d}, {d} events so far\n", .{ index, trace.len, try m.count() }); @@ -63,10 +88,31 @@ fn state(arena: std.mem.Allocator, trace: *zt.Trace, index: u64, w: *std.Io.Writ if (try m.line(tid)) |line| try w.print(" {s}:{d}:{d}", .{ try name(m, arena, line.file), line.line, line.column }); try w.writeAll("\n"); if (try m.regs(tid)) |regs| { - inline for (zt.Regs.names, 0..) |reg, i| { - try w.print(" {s:<6} {x:0>16}{s}", .{ reg, @field(regs, reg), if (i % 3 == 2) "\n" else "" }); + inline for (zt.Regs.names) |reg| { + try w.print(" {s:<6} ", .{reg}); + if (comptime std.mem.eql(u8, reg, "eflags")) try w.print("{x}", .{regs.eflags}) else try telescope(m, w, @field(regs, reg), colour); + try w.writeAll("\n"); + } + // The instructions from the pc on, as far as the recording has them. + var pc = regs.rip; + var text: [192]u8 = undefined; + for (0..8) |i| { + const insn = (try zt.telescope.instruction(m, pc, &text)) orelse break; + try w.print("{s}{x:0>12} {s}\n", .{ if (i == 0) "\n ► " else " ", pc, insn.text }); + pc += insn.len; + } + // The top of the stack, a word a row. + var words: [8]u64 = undefined; + var known: [64]bool = undefined; + try m.read(regs.rsp, std.mem.sliceAsBytes(&words), &known); + if (known[0]) try w.writeAll("\n"); + for (words, 0..) |word, i| { + if (!known[i * 8]) break; + try w.print(" {x:0>2}:{x:0>4}│ {s} ", .{ i, i * 8, if (i == 0) "rsp" else " " }); + try telescope(m, w, regs.rsp + i * 8, colour); + _ = word; + try w.writeAll("\n"); } - if (zt.Regs.names.len % 3 != 0) try w.writeAll("\n"); } for (try m.stack(arena, tid), 0..) |frame, depth| { try w.print(" #{d} {s} from {x} sp {x}\n", .{ depth, try name(m, arena, frame.function), frame.site, frame.sp }); -- cgit v1.3