From 97ce7f485fc0419df693fdf16d634e9e9a6085fc Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 14:35:56 -0300 Subject: context: what an instruction is about to touch, and the call chain At a stop the instruction at the pc is decoded against the registers it will run with (capstone's operands): a touch event for each memory operand (address, size, read or write), what is there recorded with it, and what a write left there read back at the next stop. An instruction-level recording so has every memory write of the stepped thread, nothing watched. The frames of the call chain are recorded and their return addresses named; telescope.backtrace walks them from the recording and ztrace show prints them. Process.read no longer panics on address 0. --- src/trace/telescope.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/trace/telescope.zig') diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index f4cf09a..efc6e43 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -97,6 +97,25 @@ pub fn instruction(m: Moment, addr: u64, buf: []u8) !?struct { len: u8, text: [] return .{ .len = s[0], .text = s[1..] }; } +/// The call chain of a thread at a moment, by frame pointer, from what the +/// recording holds of its stack: the pc, then each return address outwards. +/// It ends where the frames were not recorded. +pub fn backtrace(m: Moment, regs: *const event.Regs, out: []u64) ![]u64 { + const Memory = struct { + m: Moment, + pub fn read(mem: @This(), addr: u64, buf: []u8) usize { + var known: [16]bool = undefined; + mem.m.read(addr, buf, known[0..buf.len]) catch return 0; + return if (std.mem.indexOfScalar(bool, known[0..buf.len], false) == null) buf.len else 0; + } + }; + // At a function's first instruction its frame is not made yet: the + // return address is on top of the stack. + var name: [256]u8 = undefined; + const at_entry = if (try m.string(regs.rip, &name)) |n| std.mem.endsWith(u8, n, "+0") else false; + return @import("Debugger.zig").backtrace(Memory{ .m = m }, regs, at_entry, out); +} + test "a chain is followed to a string, to code, to a number, and no further than is known" { const gpa = std.testing.allocator; var tmp = std.testing.tmpDir(.{}); -- cgit v1.3