From ed46c51fe127377eb1fde8dee83da984e0f61860 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 14:39:55 -0300 Subject: context: eight instructions above the pc and eight below; where a branch goes A function is decoded once from its start, so the instructions before the pc are known: each instruction's string now says the length of the one before it, and ztrace show prints eight above and eight below, as pwndbg does. A call, a jump or a return at the pc is a branch event with its target (named) and, for a conditional jump, whether the flags take it. --- src/trace/Debugger.zig | 118 +++++++++++++++++++++++++++++++++++++----------- src/trace/event.zig | 8 +++- src/trace/telescope.zig | 13 ++++-- 3 files changed, 107 insertions(+), 32 deletions(-) (limited to 'src/trace') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index fd5431f..13f5ce8 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -73,8 +73,10 @@ disasm: ?*const fn (code: []const u8, addr: u64, text: []u8, regs: ?*const Regs, /// back at the next: an instruction's writes, with no region watched. written: [2]Touch = undefined, written_count: usize = 0, -/// The instructions from the pc on whose text a stop records. -disasm_ahead: usize = 8, +/// The instructions above and below the pc whose text a stop records. +disasm_around: usize = 8, +/// Each function met, as where its instructions begin (`instructions`). +functions: std.AutoHashMapUnmanaged(u64, []const u32) = .empty, /// How far a stop follows pointers, from each register and from each of /// the first `telescope_words` words of the stack: the memory they lead to /// is recorded, and each that leads into code is named. 0, the library's @@ -121,7 +123,11 @@ pub const Action = enum { go, stop }; pub const Breakpoint = struct { id: u32, addr: u64, saved: u8, hook: ?Hook }; /// An instruction decoded: its length, its text ("mov rbp, rsp"), and with /// the registers given, the memory it is about to touch. -pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{} }; +pub const Insn = struct { len: u8, text: []const u8, touches: []const Touch = &.{}, branch: Branch = .{} }; +/// Where a call, a jump or a return goes. `kind`: 0 not one, 1 call, 2 +/// jump, 3 return. With `indirect` the target is read from the +/// instruction's memory operand; a return's is on the stack. +pub const Branch = extern struct { target: u64 = 0, kind: u8 = 0, taken: u8 = 1, indirect: 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 }; pub const Watch = struct { addr: u64, len: usize }; @@ -167,6 +173,9 @@ pub fn deinit(d: *Debugger) void { d.sites.deinit(d.gpa); d.described.deinit(d.gpa); d.code_len.deinit(d.gpa); + var decoded = d.functions.valueIterator(); + while (decoded.next()) |starts| d.gpa.free(starts.*); + d.functions.deinit(d.gpa); var pages = d.shadow.valueIterator(); while (pages.next()) |page| d.gpa.destroy(page.*); d.shadow.deinit(d.gpa); @@ -416,33 +425,90 @@ fn emitCode(d: *Debugger) !void { // stop after. if (d.read(d.regs.rip, code[0..15]) > 0) { 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.snapshot(t.addr, t.size); - 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; - d.written_count += 1; + if (insnAt(code[0..15], d.regs.rip, text[1..], &d.regs, &touches)) |insn| { + for (insn.touches) |t| { + try d.snapshot(t.addr, t.size); + 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; + d.written_count += 1; + } } - }; + // Where a call, a jump or a return goes, and whether it will. + const b = insn.branch; + const from: ?u64 = if (b.kind == 3) d.regs.rsp else if (b.indirect != 0 and insn.touches.len > 0) insn.touches[0].addr else null; + var word: [8]u8 = undefined; + const target: ?u64 = if (from) |addr| (if (d.read(addr, &word) == 8) std.mem.readInt(u64, &word, .little) else null) else b.target; + if (b.kind != 0) if (target) |to| { + try d.nameAddress(to); + try d.emit(.{ .kind = .branch, .aux = b.kind, .a = .{ to, b.taken, d.regs.rip, 0, 0, 0 } }, ""); + }; + } } - // Read only when an instruction not seen before is met: a loop costs nothing. - var n: ?usize = null; - var at: usize = 0; - for (0..d.disasm_ahead) |_| { - const addr = d.regs.rip + at; - if (d.code_len.get(addr)) |len| { - at += len; - continue; + // The instructions round the pc, in address order. Inside a function + // they are known both ways, the function decoded once from its start; + // elsewhere (no symbol: stripped code, a JIT's) only from the pc on. + const around = d.disasm_around; + if (d.syms.symbolAt(d.regs.rip)) |sym| if (sym.size > 0 and sym.size <= 1 << 16) { + const starts = try d.instructions(sym, insnAt); + const here: u32 = @intCast(d.regs.rip - sym.addr); + const at = std.sort.binarySearch(u32, starts, here, struct { + fn order(want: u32, have: u32) std.math.Order { + return std.math.order(want, have); + } + }.order); + if (at) |k| { + for (k -| around..@min(starts.len, k + around + 1)) |n| { + _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0); + } + return; } - if (n == null) n = d.read(d.regs.rip, code[0..@min(code.len, d.disasm_ahead * 15)]); - if (at >= n.?) break; - // A byte for the length, then the text. - const insn = insnAt(code[at..n.?], addr, text[1..], null, &.{}) orelse break; - text[0] = insn.len; - try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 1 + insn.text.len]); - try d.code_len.put(d.gpa, addr, insn.len); - at += insn.len; + }; + var addr = d.regs.rip; + var back: u8 = 0; + for (0..around + 1) |_| { + back = (try d.emitInstruction(insnAt, addr, back)) orelse break; + addr += back; + } +} + +/// Records the instruction at `addr` as a string (`event.codeId`), once: a +/// byte for its length, a byte for the length of the one before it (0: +/// not known), then its text. Returns its length; null for no instruction. +fn emitInstruction(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, addr: u64, back: u8) !?u8 { + if (d.code_len.get(addr)) |len| return len; + var code: [15]u8 = undefined; + var text: [192]u8 = undefined; + const n = d.read(addr, &code); + const insn = insnAt(code[0..n], addr, text[2..], null, &.{}) orelse return null; + text[0] = insn.len; + text[1] = back; + try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 2 + insn.text.len]); + try d.code_len.put(d.gpa, addr, insn.len); + return insn.len; +} + +/// Where each instruction of a function begins, as offsets from its start: +/// decoded once, from the first byte on, which is the only way to know +/// where the instruction before another begins. +fn instructions(d: *Debugger, sym: Symbols.Symbol, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child) ![]const u32 { + const entry = try d.functions.getOrPut(d.gpa, sym.addr); + if (entry.found_existing) return entry.value_ptr.*; + errdefer d.functions.removeByPtr(entry.key_ptr); + const code = try d.gpa.alloc(u8, sym.size); + defer d.gpa.free(code); + const n = d.read(sym.addr, code); + var starts: std.ArrayList(u32) = .empty; + errdefer starts.deinit(d.gpa); + var text: [192]u8 = undefined; + var off: usize = 0; + while (off < n) { + try starts.append(d.gpa, @intCast(off)); + // Bytes that are no instruction (padding, a table) are stepped over one at a time. + off += if (insnAt(code[off..n], sym.addr + off, &text, null, &.{})) |insn| insn.len else 1; } + entry.value_ptr.* = try starts.toOwnedSlice(d.gpa); + return entry.value_ptr.*; } /// What a viewer follows from the registers and the top of the stack, as diff --git a/src/trace/event.zig b/src/trace/event.zig index 6f7a258..dcd4aa9 100644 --- a/src/trace/event.zig +++ b/src/trace/event.zig @@ -43,6 +43,11 @@ pub const Kind = enum(u16) { /// debugger, which sees the instruction before it runs; what is there is /// recorded beside it, and what a write left there after the step. touch = 22, + /// The instruction at the pc is a call, a jump or a return: a0=where it + /// goes a1=1 when it will go there (0: a conditional jump that falls + /// through) a2=pc. aux: 1 call, 2 jump, 3 return. From a debugger, + /// before the instruction runs; the target is named like any code address. + branch = 23, // Instrumentation (compiler passes, hooks). /// a0=callee (the string id of its name, or its address when the @@ -174,7 +179,8 @@ pub fn stringId(text: []const u8) u64 { } /// The id of the string that describes the instruction at `addr`: a byte -/// for its length, then its text ("mov rbp, rsp"). The string whose id is +/// for its length, a byte for the length of the instruction before it (0 +/// when that is not known), then its text ("mov rbp, rsp"). The string whose id is /// `addr` itself is the address's name ("main+4"). Addresses stay below /// bit 47 and hashed ids have bit 63: bit 62 keeps the two apart. pub fn codeId(addr: u64) u64 { diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index efc6e43..ba7caa3 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -90,11 +90,13 @@ pub fn chase(m: Moment, value: u64, links: []Link, text: []u8) ![]Link { } /// The instruction at `addr` as the recording has it (`event.codeId`): its -/// length and its text. Null where none was recorded. -pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, text: []const u8 } { +/// length, the length of the one before it (0 when the recording does not +/// know: the one before is then not at `addr - back`), and its text. Null +/// where none was recorded. +pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, back: u8, text: []const u8 } { const s = (try m.string(event.codeId(addr), buf)) orelse return null; - if (s.len == 0) return null; - return .{ .len = s[0], .text = s[1..] }; + if (s.len < 2) return null; + return .{ .len = s[0], .back = s[1], .text = s[2..] }; } /// The call chain of a thread at a moment, by frame pointer, from what the @@ -137,7 +139,7 @@ test "a chain is followed to a string, to code, to a number, and no further than try map(&rec, 0x900000, 0x1000, 3, ""); try rec.emit(.{ .kind = .alloc, .tid = 1, .a = .{ 0x900010, 32, 0, 0, 0, 0 } }, ""); try rec.emit(.{ .kind = .string, .a = .{ 0x400123, 0, 0, 0, 0, 0 } }, "main+4"); - try rec.emit(.{ .kind = .string, .a = .{ event.codeId(0x400123), 0, 0, 0, 0, 0 } }, "\x03mov rbp, rsp"); + try rec.emit(.{ .kind = .string, .a = .{ event.codeId(0x400123), 0, 0, 0, 0, 0 } }, "\x03\x01mov rbp, rsp"); const word = struct { fn at(r: *store.Recorder, addr: u64, value: u64) !void { try r.emit(.{ .kind = .mem_write, .tid = 1, .a = .{ addr, 8, 0, 0, 0, 0 } }, std.mem.asBytes(&value)); @@ -183,6 +185,7 @@ test "a chain is followed to a string, to code, to a number, and no further than const insn = (try instruction(m, 0x400123, &text)).?; try std.testing.expectEqual(@as(u8, 3), insn.len); + try std.testing.expectEqual(@as(u8, 1), insn.back); try std.testing.expectEqualStrings("mov rbp, rsp", insn.text); try std.testing.expect(try instruction(m, 0x400126, &text) == null); } -- cgit v1.3