diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 13:36:32 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 14:12:08 -0300 |
| commit | 02aa51a6f54235cf18e627dbbfe21f21e502eb0b (patch) | |
| tree | a53416bdf0e5bb0ce80da63176fe13393c502a2c /src/cli | |
| parent | 16895292becf82f493aaced2b10f889bad436156 (diff) | |
| download | computeranim-02aa51a6f54235cf18e627dbbfe21f21e502eb0b.tar.gz computeranim-02aa51a6f54235cf18e627dbbfe21f21e502eb0b.zip | |
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.
Diffstat (limited to 'src/cli')
| -rw-r--r-- | src/cli/dbg.zig | 3 | ||||
| -rw-r--r-- | src/cli/disasm.c | 20 | ||||
| -rw-r--r-- | src/cli/disasm.zig | 12 | ||||
| -rw-r--r-- | src/cli/record.zig | 15 | ||||
| -rw-r--r-- | src/cli/show.zig | 62 |
5 files changed, 103 insertions, 9 deletions
diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig index 02cebbc..e59c40a 100644 --- a/src/cli/dbg.zig +++ b/src/cli/dbg.zig @@ -3,6 +3,7 @@ const std = @import("std"); const zt = @import("ztrace"); const util = @import("util.zig"); +const disasm = @import("disasm.zig"); const fail = util.fail; const Moment = zt.store.Moment; @@ -82,6 +83,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { try zt.Debugger.spawn(init.gpa, init.io, try util.resolve(init, args[i]), try util.targetArgv(arena, args[i..]), if (produced) |p| p.envp.ptr else init.minimal.environ.block.slice.ptr, sink); defer s.d.deinit(); if (produced) |p| s.d.ring = p.ring; + s.d.disasm = disasm.one; + s.d.telescope = 3; defer s.stop_payload.deinit(init.gpa); s.d.stack_watch = stack_bytes; if (capture) |n| s.d.capture = n; diff --git a/src/cli/disasm.c b/src/cli/disasm.c new file mode 100644 index 0000000..db19059 --- /dev/null +++ b/src/cli/disasm.c @@ -0,0 +1,20 @@ +// The one thing asked of capstone, in C so that its structs stay its own. +#include <capstone/capstone.h> +#include <stdio.h> + +static csh handle; + +// The instruction at `code`, which is at `addr`: its text into `text`, its +// length returned. 0 where there is no instruction. +size_t zt_disasm(const uint8_t *code, size_t len, uint64_t addr, char *text, size_t cap) { + if (!handle) { + if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK) return 0; + cs_option(handle, CS_OPT_SYNTAX, CS_OPT_SYNTAX_INTEL); + } + cs_insn *insn; + if (cs_disasm(handle, code, len, addr, 1, &insn) != 1) return 0; + size_t size = insn->size; + snprintf(text, cap, insn->op_str[0] ? "%s %s" : "%s", insn->mnemonic, insn->op_str); + cs_free(insn, 1); + return size; +} diff --git a/src/cli/disasm.zig b/src/cli/disasm.zig new file mode 100644 index 0000000..5c0d27b --- /dev/null +++ b/src/cli/disasm.zig @@ -0,0 +1,12 @@ +//! Disassembly for the debugger: capstone, through src/cli/disasm.c. +const std = @import("std"); +const zt = @import("ztrace"); + +extern fn zt_disasm(code: [*]const u8, len: usize, addr: u64, text: [*]u8, cap: usize) usize; + +/// What `Debugger.disasm` is set to. +pub fn one(code: []const u8, addr: u64, text: []u8) ?zt.Debugger.Insn { + const len = zt_disasm(code.ptr, code.len, addr, text.ptr, text.len); + if (len == 0) return null; + return .{ .len = @intCast(len), .text = std.mem.sliceTo(text, 0) }; +} diff --git a/src/cli/record.zig b/src/cli/record.zig index f508ddc..f5281ca 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -3,11 +3,13 @@ const std = @import("std"); const zt = @import("ztrace"); const util = @import("util.zig"); +const disasm = @import("disasm.zig"); const fail = util.fail; pub const usage = \\usage: ztrace record [-o FILE] [--by insn|line|syscall|break] [-b WHERE]... [-w WHAT[,LEN]]... - \\ [--stack BYTES] [-s BYTES] [--steps N] [--max-size BYTES] [--strace] + \\ [--stack BYTES] [-s BYTES] [--telescope DEPTH] [--steps N] + \\ [--max-size BYTES] [--strace] \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ \\Runs PROGRAM under ptrace into a recording (default out.ztrace). --by @@ -27,6 +29,11 @@ pub const usage = \\that changed since the stop before: the --stack bytes above the stack \\pointer (default 128) and each -w region, an address or a symbol, as \\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. \\--steps ends the recording after N moments, and --max-size when the \\file is that large (default 1G). Sizes take a suffix: 64K, 4G. \\ @@ -44,6 +51,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var breaks: std.ArrayList([]const u8) = .empty; var watches: std.ArrayList([]const u8) = .empty; var producer: util.Producer = .{}; + var telescope: u8 = 3; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; @@ -71,6 +79,9 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { } else if (std.mem.eql(u8, arg, "-w")) { try watches.append(arena, value.?); i += 1; + } 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, "--steps")) { max_moments = std.fmt.parseInt(u64, value.?, 0) catch fail("--steps needs a number\n", .{}); i += 1; @@ -96,6 +107,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var d = try zt.Debugger.spawn(init.gpa, init.io, path, argv, if (produced) |p| p.envp.ptr else init.minimal.environ.block.slice.ptr, if (strace) printer.sink() else rec.sink()); defer d.deinit(); if (produced) |p| d.ring = p.ring; + d.disasm = disasm.one; + d.telescope = telescope; d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; if (capture) |n| d.capture = n; 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 }); |
