summaryrefslogtreecommitdiff
path: root/src/trace/telescope.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 14:35:56 -0300
committerGabriel Schneider <[email protected]>2026-11-10 14:39:55 -0300
commit97ce7f485fc0419df693fdf16d634e9e9a6085fc (patch)
tree9d36463512024242665c7836b94a4de4e541b3bf /src/trace/telescope.zig
parentf8740f6996eefc235af066a6686f52847c158a6b (diff)
downloadcomputeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.tar.gz
computeranim-97ce7f485fc0419df693fdf16d634e9e9a6085fc.zip
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.
Diffstat (limited to 'src/trace/telescope.zig')
-rw-r--r--src/trace/telescope.zig19
1 files changed, 19 insertions, 0 deletions
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(.{});