From b4ab1d770edb06d3eeebfd290bbf97725e55b7fa Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 22:51:17 -0300 Subject: trace: the call chain by the unwind tables; what each word of the stack is backtrace and frames follow .debug_frame or .eh_frame where there is one (PLT entries expanded into exact rows) and the frame pointer where there is none: the caller is no longer lost in a prologue, in a function that keeps no frame, in a PLT entry or inside libc's syscall wrappers. Against gdb: the same chains on fib and on SQLite, where the old walk lost __sbrk and unixSync. A recording holds, once a function, its unwind rows and its locals on the stack (DW_OP_fbreg, with the block each lives in), and the process's first stack pointer. telescope.slots says of each word what it is: a saved register and which, a return address, a local by name, argc, argv, envp, auxv. Four bytes a moment on the list by instruction, and no slower. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/trace/debugger_test.zig | 150 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 148 insertions(+), 2 deletions(-) (limited to 'src/trace/debugger_test.zig') diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index 5d43d8c..e20f864 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -173,6 +173,152 @@ test "step walks the source lines, in and out of calls, as gdb does" { try std.testing.expectEqualStrings("main", d2.syms.symbolAt(chain[1]).?.name); } +/// The target's memory with no call frame information: what the call chain +/// is walked by without it, the frame pointer alone. +const FramePointer = struct { + d: *Debugger, + pub fn read(mem: FramePointer, addr: u64, buf: []u8) usize { + return mem.d.read(addr, buf); + } +}; + +fn named(d: *Debugger, pc: u64) []const u8 { + return if (d.syms.symbolAt(pc)) |sym| sym.name else ""; +} + +test "the call chain by the unwind tables: through a frameless wrapper, at a function's first instruction" { + var d = try spawn(targets.fib, "5", null); + defer d.deinit(); + // A brk from malloc, inside libc: its wrapper keeps no frame, so the + // frame pointer's walk passes over its caller. gdb shows the same chain. + d.syscalls = .stop; + var by_cfi: [32]u64 = undefined; + var by_fp: [32]u64 = undefined; + const cfi, const fp = find: while (true) switch (try d.cont()) { + .syscall_enter => |nr| if (nr == @backingInt(linux.SYS.brk)) { + const chain = Debugger.backtrace(&d, &d.regs, false, &by_cfi); + for (chain) |pc| if (std.mem.eql(u8, named(&d, pc), "main")) break :find .{ chain, Debugger.backtrace(FramePointer{ .d = &d }, &d.regs, false, &by_fp) }; + }, + .exited => return error.NoBrkFromMain, + else => {}, + }; + try std.testing.expect(cfi.len > fp.len); + // Every frame the frame pointer finds is found, in order, and the walks end alike. + var at: usize = 0; + for (fp) |pc| { + at = (std.mem.indexOfScalarPos(u64, cfi, at, pc) orelse return error.FrameLost) + 1; + } + try std.testing.expectEqual(fp[fp.len - 1], cfi[cfi.len - 1]); + d.syscalls = .ignore; + + // At fib's first instruction its caller is main's frame still, which + // the frame pointer's walk, not told so, passes over. + var d2 = try spawn(targets.fib, "5", null); + defer d2.deinit(); + _ = try d2.breakAt(d2.symbol("fib").?.addr, null); + _ = try d2.cont(); + try std.testing.expectEqualStrings("main", named(&d2, Debugger.backtrace(&d2, &d2.regs, false, &by_cfi)[1])); + try std.testing.expect(!std.mem.eql(u8, "main", named(&d2, Debugger.backtrace(FramePointer{ .d = &d2 }, &d2.regs, false, &by_fp)[1]))); +} + +test "a recording tells each stack word's role: saved registers, return addresses, locals, the process's start" { + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + var dir_buf: [std.Io.Dir.max_path_bytes]u8 = undefined; + const dir = dir_buf[0..try tmp.dir.realPath(io, &dir_buf)]; + const path = try std.fs.path.join(gpa, &.{ dir, "roles.ztrace" }); + defer gpa.free(path); + + var rec = try zt.Recorder.create(gpa, io, path); + { + errdefer rec.close() catch {}; + var d = try spawn(targets.list_O0, "2", rec.sink()); + defer d.deinit(); + // DWARF's locals: at -O0 each parameter and variable has a place by the frame base. + const push = d.syms.functionAt(d.symbol("push").?.addr).?; + try std.testing.expectEqual(@as(u8, 6), push.base); + try std.testing.expectEqual(@as(usize, 3), push.locals.len); + for (push.locals, [_][]const u8{ "head", "value", "n" }) |local, name| { + try std.testing.expectEqualStrings(name, local.name); + try std.testing.expectEqual(@as(u32, 8), local.size); + try std.testing.expect(local.offset < 0); + } + // main's i is its loop's, a block within it. + for (d.syms.functionAt(d.symbol("main").?.addr).?.locals) |local| { + if (std.mem.eql(u8, local.name, "i")) try std.testing.expect(local.from > 0 and local.to < 0x1000); + } + d.telescope = 1; + d.stack_watch = 4096; + // In push's body: its frame made, its locals above the stack pointer. + _ = try d.breakAt(try d.resolve("list.c:13"), null); + _ = try d.cont(); + try std.testing.expectEqual(@as(u8, 0), try finish(&d)); + } + try rec.close(); + + var trace = try zt.Trace.open(gpa, io, path); + defer trace.close(); + const m = find: for (0..trace.len) |i| { + const m = try trace.moment(i); + var events = try m.events(gpa); + defer events.deinit(gpa); + while (events.next()) |item| if (item.ev.kind == .breakpoint) break :find m; + } else return error.NoBreakpoint; + const tids = try m.threads(gpa); + defer gpa.free(tids); + const regs = (try m.regs(tids[0])).?; + var found: [8]Debugger.Frame = undefined; + const chain = try zt.telescope.frames(m, ®s, &found); + try std.testing.expect(chain.len >= 3); + const Slot = zt.telescope.Slot; + var buf: [512]Slot = undefined; + const words = try zt.telescope.slots(m, ®s, regs.rsp, &buf); + + // push's frame: its locals, rbp saved under the return address. + var seen: [3]bool = @splat(false); + for (words, 0..) |w, i| { + const addr = regs.rsp + 8 * i; + if (addr >= chain[0].high) break; + try std.testing.expectEqual(@as(?u8, 0), w.frame); + if (w.role == .local) for ([_][]const u8{ "head", "value", "n" }, &seen) |name, *s| { + if (std.mem.eql(u8, w.role.local.name, name)) s.* = true; + }; + } + try std.testing.expectEqual([3]bool{ true, true, true }, seen); + const at = struct { + fn word(all: []const Slot, rsp: u64, addr: u64) Slot { + return all[(addr - rsp) / 8]; + } + }.word; + try std.testing.expectEqual(zt.telescope.Role.return_address, at(words, regs.rsp, chain[0].high - 8).role); + try std.testing.expectEqualStrings("rbp", at(words, regs.rsp, chain[0].high - 16).role.saved); + // main's frame, next: its own locals, and its return address into libc. + try std.testing.expectEqual(@as(?u8, 1), at(words, regs.rsp, chain[1].low).frame); + try std.testing.expectEqual(zt.telescope.Role.return_address, at(words, regs.rsp, chain[1].high - 8).role); + // The call is in the loop: its i is named too. + var in_main: [2]bool = @splat(false); + for (words[(chain[1].low - regs.rsp) / 8 .. (chain[1].high - regs.rsp) / 8]) |w| { + if (w.role == .local) for ([_][]const u8{ "head", "i" }, &in_main) |name, *seen_it| { + if (std.mem.eql(u8, w.role.local.name, name)) seen_it.* = true; + }; + } + try std.testing.expectEqual([2]bool{ true, true }, in_main); + + // The process's first stack: argc 2, ./list-O0 and "2", no environment, then the auxiliary vector. + var start_at: ?usize = null; + for (words, 0..) |w, i| if (w.role == .argc) { + start_at = i; + break; + }; + const s = words[start_at orelse return error.NoArgc ..]; + for ([_]zt.telescope.Role{ .argc, .{ .argv = 0 }, .{ .argv = 1 }, .{ .argv = 2 }, .{ .envp = 0 }, .{ .auxv = 0 }, .{ .auxv = 1 } }, s[0..7]) |want, have| { + try std.testing.expectEqual(want, have.role); + } + var argc: [8]u8 = undefined; + try m.read(regs.rsp + 8 * start_at.?, &argc, null); + try std.testing.expectEqual(@as(u64, 2), std.mem.readInt(u64, &argc, .little)); +} + test "the telescope records the region round what a pointer leads to" { var log: Log = .{}; defer log.deinit(); @@ -307,10 +453,10 @@ test "a watch sends a word when it changes, and each moment keeps the region as } try std.testing.expectEqual(@as(usize, 9), writes); try std.testing.expect(log.count(.step) > 100); - // Lines come from one file here, defined once. + // Lines come from one file here, defined once; the one other string is where the stack began. try std.testing.expect(log.count(.line) > 100); for (log.events.items) |e| { - if (e.ev.kind == .string) try std.testing.expect(std.mem.endsWith(u8, e.payload, ".zig")); + if (e.ev.kind == .string and e.ev.a[0] != zt.event.frameId(0)) try std.testing.expect(std.mem.endsWith(u8, e.payload, ".zig")); } var trace = try zt.Trace.open(gpa, io, path); -- cgit v1.3