summaryrefslogtreecommitdiff
path: root/src/trace/Debugger.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 20:08:00 -0300
committerGabriel Schneider <[email protected]>2026-11-11 20:08:07 -0300
commit0ec2376cae2fdfc50e90d930ebc86c7a5b7f5594 (patch)
treebb820b7e39994177b6abc503ab5b980603d34513 /src/trace/Debugger.zig
parentcc2059e39047cafe1886dc17084855ddd0c52b01 (diff)
downloadcomputeranim-0ec2376cae2fdfc50e90d930ebc86c7a5b7f5594.tar.gz
computeranim-0ec2376cae2fdfc50e90d930ebc86c7a5b7f5594.zip
trace: what push, call and ret touch; names for data; the stack's roles, sharper
For a disassembly that points at the stack word it touched. The stack's own instructions have touches (push, call and enter write at rsp-8; pop and ret read at rsp; leave at rbp), and a rep string instruction its whole range. An address a global or static object holds is named (head+0, libc.so.6!main_arena+96). The saved rbp has its role at the push that makes it; every local in a shared word is given; the stack protector's copy is found by seeing its store; ztrace show labels each stack word. Six bytes a moment and 3% slower on the list by instruction. Co-Authored-By: Claude Fable 5.2 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'src/trace/Debugger.zig')
-rw-r--r--src/trace/Debugger.zig107
1 files changed, 97 insertions, 10 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig
index 4ca7eca..729b93a 100644
--- a/src/trace/Debugger.zig
+++ b/src/trace/Debugger.zig
@@ -107,8 +107,8 @@ telescope_words: usize = 16,
/// the 32 at it) or an instruction touches, no further than its mapping:
/// the region a viewer shows round what a pointer leads to.
telescope_around: usize = 0,
-/// The addresses already named, and the instructions already recorded
-/// with the length of each.
+/// The addresses already named (and the data addresses looked up that have
+/// no name), and the instructions already recorded with the length of each.
described: std.AutoHashMapUnmanaged(u64, void) = .empty,
code_len: std.AutoHashMapUnmanaged(u64, u8) = .empty,
/// The call frame information at each pc a recorded walk met, its
@@ -158,7 +158,11 @@ pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.
/// a jump on a condition.
pub const Branch = extern struct { target: u64 = 0, kind: u8 = 0, taken: u8 = 1, indirect: u8 = 0, conditional: u8 = 0 };
/// Memory an instruction touches: `how` is 1 for a read, 2 a write, 3 both.
-pub const Touch = extern struct { addr: u64, size: u8, how: u8 };
+/// The memory its operands name, and what the stack's own instructions
+/// touch without naming it: a push's word and a call's return address
+/// under rsp, a pop's and a return's at rsp, leave's saved rbp at rbp. A
+/// `rep` string instruction's is the range its count covers.
+pub const Touch = extern struct { addr: u64, size: u32, how: u8 };
pub const Watch = struct { addr: u64, len: usize };
const Page = struct {
@@ -537,12 +541,25 @@ fn snapshotRound(d: *Debugger, addr: u64, size: usize) !void {
try d.snapshot(lo, @min(m.end, addr + size + d.telescope_around) - lo);
}
+/// The most of one touch a stop records: a `rep` string instruction's
+/// range can be megabytes, and stepped by instruction it does one element
+/// a step.
+const touch_max = 4096;
+
+/// `snapshotRound` of a touch, `touch_max` of it at most from where a
+/// `rep` begins: its start, or its end with the direction flag set.
+// shortcut: of a `rep` past 4 KiB run between two stops (by line, by breakpoint) the rest of what it wrote is not recorded, upgrade by reading the range in pages at the stop after when someone records memset by line.
+fn snapshotTouch(d: *Debugger, t: Touch) !void {
+ const n = @min(t.size, touch_max);
+ try d.snapshotRound(if (d.regs.eflags & 0x400 != 0) t.addr + t.size - n else t.addr, n);
+}
+
/// The instructions at the pc and after it, each a string (`event.codeId`)
/// recorded once: what a viewer shows as the disassembly.
fn emitCode(d: *Debugger) !void {
const insnAt = d.disasm orelse return;
// What the instruction before wrote is there to read now.
- for (d.written[0..d.written_count]) |t| try d.snapshotRound(t.addr, t.size);
+ for (d.written[0..d.written_count]) |t| try d.snapshotTouch(t);
d.written_count = 0;
var code: [256]u8 = undefined;
var text: [192]u8 = undefined;
@@ -553,7 +570,8 @@ fn emitCode(d: *Debugger) !void {
var touches: [2]Touch = undefined;
if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| {
for (insn.touches) |t| {
- try d.snapshotRound(t.addr, t.size);
+ try d.snapshotTouch(t);
+ try d.nameData(t.addr);
try d.emit(.{ .kind = .touch, .aux = t.how, .a = .{ t.addr, t.size, d.regs.rip, 0, 0, 0 } }, "");
if (t.how & 2 != 0) {
d.written[d.written_count] = t;
@@ -837,6 +855,8 @@ fn chase(d: *Debugger, value: u64) !void {
const m = d.syms.mappingAt(v) orelse return;
if (m.prot & 1 == 0) return;
if (m.prot & 4 != 0) return d.nameAddress(v);
+ // A file's data, or the zeroes after it; not "[stack]" nor "[heap]".
+ if (m.name.len == 0 or m.name[0] == '/') try d.nameData(v);
const lo = @max(m.start, v -| d.telescope_around) & ~@as(u64, 7);
const bytes = try d.snap(lo, @min(m.end, v + 32 + d.telescope_around) - lo);
const off: usize = @intCast(v - lo);
@@ -859,6 +879,16 @@ fn nameAddress(d: *Debugger, addr: u64) !void {
try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered());
}
+/// `nameAddress` of a data address that a global or static object of the
+/// symbol tables holds: "head+0", "libc.so.6!main_arena+96". One a module's
+/// symbols do not hold (a string literal, the GOT's header) is looked up
+/// once; one outside every module (the stack, the heap) never.
+fn nameData(d: *Debugger, addr: u64) !void {
+ if (d.syms.moduleAt(addr) == null or d.described.contains(addr)) return;
+ if (d.syms.symbolAt(addr) != null) return d.nameAddress(addr);
+ try d.described.put(d.gpa, addr, {});
+}
+
fn emitStop(d: *Debugger, stop: Stop) !void {
const r = &d.regs;
const regs = std.mem.asBytes(r);
@@ -1387,27 +1417,84 @@ pub const Recorded = struct {
/// `Symbols.Function.base`), how many rows (2 bytes), the rows
/// (`Symbols.Row`), then each local (`Symbols.Local`): its offset from the
/// base, its size, the stretch of the function's code it is in, from and
-/// to (4 bytes each), its name and a zero.
+/// to (4 bytes each), its name and a zero. The copy of the stack
+/// protector's value it keeps (`canary`) is a local with no name, from its
+/// store on; a function with no debug info then has the CFA for its base.
fn emitFunction(d: *Debugger, f: Symbols.Function) !void {
if ((try d.described.getOrPut(d.gpa, event.frameId(f.start))).found_existing) return;
+ const own = d.canary(f, if (f.base == Symbols.no_base) Symbols.base_cfa else f.base);
+ const base = if (own != null and f.base == Symbols.no_base) Symbols.base_cfa else f.base;
var text: std.Io.Writer.Allocating = .init(d.gpa);
defer text.deinit();
const w = &text.writer;
- try w.writeInt(u32, std.math.cast(u32, f.len) orelse 0, .little);
- try w.writeByte(f.base);
+ const len = std.math.cast(u32, f.len) orelse 0;
+ try w.writeInt(u32, len, .little);
+ try w.writeByte(base);
try w.writeInt(u16, @intCast(@min(f.rows.len, std.math.maxInt(u16))), .little);
try w.writeAll(std.mem.sliceAsBytes(f.rows[0..@min(f.rows.len, std.math.maxInt(u16))]));
- for (f.locals) |local| {
+ const kept: [1]Symbols.Local = .{.{ .name = "", .offset = if (own) |c| c.offset else 0, .size = 8, .from = if (own) |c| c.from else 0, .to = len }};
+ // Locals by a base that is not the one written are no use to a reader.
+ for ([_][]const Symbols.Local{ if (base == f.base) f.locals else &.{}, kept[0..@intFromBool(own != null)] }) |list| for (list) |local| {
try w.writeInt(i32, local.offset, .little);
try w.writeInt(u32, local.size, .little);
try w.writeInt(u32, local.from, .little);
try w.writeInt(u32, local.to, .little);
try w.writeAll(local.name);
try w.writeByte(0);
- }
+ };
try d.emit(.{ .kind = .string, .a = .{ event.frameId(f.start), 0, 0, 0, 0, 0 } }, text.written());
}
+/// Where a function keeps its copy of the stack protector's value: the
+/// word its prologue stores what it read at `fs:[0x28]` into (`mov rax,
+/// fs:[0x28]` then `mov [rbp - 8], rax`), seen by decoding its first 64
+/// instructions, once. Its offset from the frame base `base` (a register,
+/// or the CFA), and how far into the function the store is. Null without
+/// one, or where a reader could not place it: stored by a register that is
+/// neither the base nor, with the CFA for base, the one the CFA is kept in
+/// (rsp in a function that keeps rbp).
+fn canary(d: *Debugger, f: Symbols.Function, base: u8) ?struct { offset: i32, from: u32 } {
+ const insnAt = d.disasm orelse return null;
+ var code: [1024]u8 = undefined;
+ const n = d.read(f.start, code[0..@min(code.len, f.len)]);
+ // Registers whose values tell, in a touch's address, which it was made from.
+ const by_rbp: u64 = 1 << 44;
+ const by_rsp: u64 = 2 << 44;
+ const by_fs: u64 = 3 << 44;
+ const regs: Regs = .{ .rbp = by_rbp, .rsp = by_rsp, .fs_base = by_fs };
+ var text: [192]u8 = undefined;
+ var touches: [2]Touch = undefined;
+ // The register the value was read into, as the text names it.
+ var held: [8]u8 = undefined;
+ var held_len: usize = 0;
+ var off: usize = 0;
+ for (0..64) |_| {
+ const at = off;
+ const insn = insnAt(code[off..n], f.start + off, &text, &regs, &touches) orelse return null;
+ off += insn.len;
+ if (held_len == 0) {
+ const comma = std.mem.indexOfScalar(u8, insn.text, ',') orelse continue;
+ for (insn.touches) |t| if (t.addr == by_fs + 0x28 and t.size == 8 and t.how == 1 and std.mem.startsWith(u8, insn.text, "mov ") and comma - 4 <= held.len) {
+ held_len = comma - 4;
+ @memcpy(held[0..held_len], insn.text[4..comma]);
+ };
+ continue;
+ }
+ // The next store of that register.
+ if (insn.touches.len == 0 or insn.touches[0].how != 2 or insn.text.len < held_len + 2) continue;
+ const tail = insn.text[insn.text.len - held_len - 2 ..];
+ if (!std.mem.eql(u8, tail[2..], held[0..held_len]) or !std.mem.eql(u8, tail[0..2], ", ")) continue;
+ const t = insn.touches[0];
+ const from_rbp: i64 = @bitCast(t.addr -% by_rbp);
+ const from_rsp: i64 = @bitCast(t.addr -% by_rsp);
+ const reg: u8, const disp: i64 = if (@abs(from_rbp) < 1 << 32) .{ 6, from_rbp } else if (@abs(from_rsp) < 1 << 32) .{ 7, from_rsp } else return null;
+ const row = f.rowAt(f.start + at);
+ const offset = if (base == Symbols.base_cfa and row.cfa_reg == reg) disp - row.cfa else if (base == reg) disp else return null;
+ return .{ .offset = std.math.cast(i32, offset) orelse return null, .from = @intCast(at) };
+ }
+ return null;
+}
+
/// Where the process's first stack is: the stack pointer at its exec, where
/// argc is, then the `argc` pointers of argv and a zero, the `envc` of envp
/// and a zero, and the `auxc` entries of the auxiliary vector (two words