From 147cc03a90af3782936787bf1da54314a2a4e385 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 20:41:07 -0300 Subject: backtrace: an outer frame is on its call's line The debugger records the call each frame returns from, as an instruction with its line; canim reads it. main is at list.c:41 while reverse() runs, not at 42, and the rows of a stop deep in SQLite have their lines. Co-Authored-By: Claude Fable 5.2 --- src/trace/Debugger.zig | 22 +++++++++++++++++++++- src/trace/debugger_test.zig | 12 ++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index b0ebcba..a38c337 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -616,12 +616,32 @@ fn emitTelescope(d: *Debugger) !void { const frame = try d.snap(fp, 16); if (frame.len < 16) break; const next = std.mem.readInt(u64, frame[0..8], .little); - try d.chase(std.mem.readInt(u64, frame[8..16], .little)); + const ret = std.mem.readInt(u64, frame[8..16], .little); + try d.chase(ret); + try d.emitCall(ret); if (next <= fp) break; fp = next; } } +/// The call that returns to `ret`, and the instruction there, recorded as +/// instructions: an outer frame is on its call's line, `back` from `ret`. +fn emitCall(d: *Debugger, ret: u64) !void { + const insnAt = d.disasm orelse return; + if (d.code_len.contains(ret)) return; + const sym = d.syms.symbolAt(ret) orelse return; + if (sym.size == 0 or sym.size > 1 << 16 or ret == sym.addr) return; + const starts = try d.instructions(sym, insnAt); + const k = std.sort.binarySearch(u32, starts, @as(u32, @intCast(ret - sym.addr)), struct { + fn order(want: u32, have: u32) std.math.Order { + return std.math.order(want, have); + } + }.order) orelse return; + if (k == 0) return; + _ = try d.emitInstruction(insnAt, sym.addr + starts[k - 1], if (k > 1) @intCast(starts[k - 1] - starts[k - 2]) else 0); + _ = try d.emitInstruction(insnAt, ret, @intCast(starts[k] - starts[k - 1])); +} + /// Follows `value` as a pointer: a pointer into code gets its name and /// ends the chain, any other readable target is recorded (32 bytes: a word /// to follow, and enough to tell a string) and followed. diff --git a/src/trace/debugger_test.zig b/src/trace/debugger_test.zig index 370876a..512dc6f 100644 --- a/src/trace/debugger_test.zig +++ b/src/trace/debugger_test.zig @@ -130,6 +130,18 @@ test "a recording by `ztrace record` has the line of each instruction round the } else return error.NoCall; try std.testing.expectEqual(@as(?u8, 1), call.params); try std.testing.expectEqual(@as(u32, 5), call.line.?.line); + // The frame that called fib is on its call's line: the call is recorded, `back` from where it returns. + const tids = try m.threads(gpa); + defer gpa.free(tids); + const regs = (try m.regs(tids[0])).?; + var chain: [4]u64 = undefined; + const frames = try zt.telescope.backtrace(m, ®s, &chain); + try std.testing.expect(frames.len >= 2); + const after = (try zt.telescope.instruction(m, frames[1], &text)).?; + try std.testing.expect(after.back != 0); + const caller = (try zt.telescope.instruction(m, frames[1] - after.back, &text)).?; + try std.testing.expect(std.mem.startsWith(u8, caller.text, "call")); + try std.testing.expect(caller.line != null); } test "step walks the source lines, in and out of calls, as gdb does" { -- cgit v1.3