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/telescope.zig | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) (limited to 'src/trace/telescope.zig') 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