From 0ec2376cae2fdfc50e90d930ebc86c7a5b7f5594 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 11 Nov 2026 20:08:00 -0300 Subject: trace: what push, call and ret touch; names for data; the stack's roles, sharper For a disassembly that points at the stack word it touched. The stack's own instructions have touches (push, call and enter write at rsp-8; pop and ret read at rsp; leave at rbp), and a rep string instruction its whole range. An address a global or static object holds is named (head+0, libc.so.6!main_arena+96). The saved rbp has its role at the push that makes it; every local in a shared word is given; the stack protector's copy is found by seeing its store; ztrace show labels each stack word. Six bytes a moment and 3% slower on the list by instruction. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/trace/telescope.zig | 77 ++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 63 insertions(+), 14 deletions(-) (limited to 'src/trace/telescope.zig') diff --git a/src/trace/telescope.zig b/src/trace/telescope.zig index f7e4a3d..02b637e 100644 --- a/src/trace/telescope.zig +++ b/src/trace/telescope.zig @@ -124,19 +124,25 @@ pub fn frames(m: Moment, regs: *const event.Regs, out: []Debugger.Frame) ![]Debu return Debugger.frames(Memory{ .m = m }, regs, try atEntry(m, regs), out); } +/// A local variable or parameter of a frame's function that the debug info +/// places on the stack: its name (the recording's, good while it is open), +/// its size (0: not known), and where the word is in it: 0 at its first +/// byte, 8 a word on, negative where it begins inside the word (-4: its +/// high half). +pub const Local = struct { name: []const u8, size: u32, offset: i32 }; + /// What a word of the stack is, where the recording says. pub const Role = union(enum) { none, /// A register its frame's function saved for its caller: "rbx", - /// "rbp", "r12" to "r15". + /// "rbp", "r12" to "r15"; or "canary", the copy of the stack + /// protector's value (`fs:[0x28]`) it checks before it returns. saved: []const u8, /// Where its frame's function returns to. return_address, - /// A local variable or parameter of its frame's function that the debug - /// info places on the stack: its name (the recording's, good while it - /// is open), its size (0: not known), and where the word is in it: 0 at - /// its first byte, 8 a word on, negative where it begins inside the word. - local: struct { name: []const u8, size: u32, offset: i32 }, + /// A local (the one that begins first in the word: `Slot.also` has + /// the others that share it). + local: Local, /// The process's first stack (`Debugger.Start`): argc; argv[i], the /// zero that ends them at i = argc; envp[i] likewise, at i = envc; the /// auxiliary vector's i-th word, two an entry (its type, its value), @@ -150,8 +156,11 @@ pub const Role = union(enum) { /// A word of the stack: what it is, and the frame of `frames` it lies in /// (0 the newest; null in none: under the stack pointer, or above the /// outermost frame, where the process's first stack is). A leaf's locals -/// under the stack pointer (the red zone, at -O0) are its frame's. -pub const Slot = struct { role: Role = .none, frame: ?u8 = null }; +/// under the stack pointer (the red zone, at -O0) are its frame's. Where +/// small locals share the word (two ints: `i, n`), `role` is the one that +/// begins first in it and `also` the others, in the order they begin; null +/// past the last. +pub const Slot = struct { role: Role = .none, frame: ?u8 = null, also: [7]?Local = @splat(null) }; /// What each word of a thread's stack from `low` (rounded down to a word) /// is at this moment: `out[i]` is the word at `low + 8 i`. The frames are @@ -162,7 +171,11 @@ pub const Slot = struct { role: Role = .none, frame: ?u8 = null }; /// none at all where it is in a register or a list of places, nor in a /// block whose code is in pieces, nor in a prologue or an epilogue where /// the frame base is a register the CFA is not yet or no longer kept in); -/// and the process's first stack pointer. +/// the stack protector's copy, from the store the debugger saw in the +/// function's prologue (`Debugger.canary`); and the process's first stack +/// pointer. At an instruction that saves a register (`push rbp`), the word +/// it is about to write already has the register's role, in the newest +/// frame: the call frame information says it from that instruction on. pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot { @memset(out, .{}); const base = low & ~@as(u64, 7); @@ -210,17 +223,35 @@ pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot var w = @max(at & ~@as(u64, 7), base); while (w < @min(at +% @max(size, 1), end)) : (w += 8) { const s = in(out, base, w) orelse continue; - // Two small locals in one word: the first named keeps it. - if (s.role == .none) s.role = .{ .local = .{ .name = name, .size = size, .offset = @intCast(@as(i64, @bitCast(w -% at))) } }; + const l: Local = .{ .name = name, .size = size, .offset = @intCast(@as(i64, @bitCast(w -% at))) }; + // The canary's copy is recorded as a local with no name (`Debugger.emitFunction`). + if (name.len == 0) s.* = .{ .role = .{ .saved = "canary" }, .frame = s.frame } else switch (s.role) { + .none => s.role = .{ .local = l }, + .local => share(s, l), + else => {}, + } // A leaf's locals under the stack pointer (the red zone) are its frame's too. if (s.frame == null) s.frame = @intCast(k); } }; } - for (row.saved, 0..) |at, i| { + // The newest frame is being made where its instruction saves a + // register (a `push rbp`): the word it is about to write is the + // register's already, by the row that begins after it. + var saved = row.saved; + if (k == 0) if (function(m, u.function)) |func| { + var buf: [256]u8 = undefined; + if (try instruction(m, f.pc, &buf)) |insn| { + const next = func.rowAt(f.pc + insn.len); + if (next.offset == f.pc + insn.len - func.start) for (&saved, next.saved) |*at, will| { + if (at.* == 0) at.* = will; + }; + } + }; + for (saved, 0..) |at, i| { if (at == 0 or at == Symbols.lost) continue; const s = in(out, base, cfa +% @as(u64, @bitCast(@as(i64, at) * 8))) orelse continue; - s.role = if (i == 6) .return_address else .{ .saved = Symbols.saved_names[i] }; + s.* = .{ .role = if (i == 6) .return_address else .{ .saved = Symbols.saved_names[i] }, .frame = s.frame orelse @intCast(k) }; } } const text = m.trace.strings.get(event.frameId(0)) orelse return out; @@ -233,11 +264,29 @@ pub fn slots(m: Moment, regs: *const event.Regs, low: u64, out: []Slot) ![]Slot const w = (a - start.sp) / 8; const envp = start.argc + 2; const auxv = envp + start.envc + 1; - s.role = if (w == 0) .argc else if (w < envp) .{ .argv = @intCast(w - 1) } else if (w < auxv) .{ .envp = @intCast(w - envp) } else if (w < auxv + 2 * (start.auxc + 1)) .{ .auxv = @intCast(w - auxv) } else continue; + s.* = .{ .role = if (w == 0) .argc else if (w < envp) .{ .argv = @intCast(w - 1) } else if (w < auxv) .{ .envp = @intCast(w - envp) } else if (w < auxv + 2 * (start.auxc + 1)) .{ .auxv = @intCast(w - auxv) } else continue, .frame = s.frame }; } return out; } +/// Puts `l` among the locals that share the word `s` names, in the order +/// they begin in it (a local begun in a word before begins it). +fn share(s: *Slot, l: Local) void { + var all: [8]Local = undefined; + all[0] = s.role.local; + var n: usize = 1; + for (s.also) |a| if (a) |x| { + all[n] = x; + n += 1; + }; + if (n == all.len) return; + var i = n; + while (i > 0 and all[i - 1].offset < l.offset) : (i -= 1) all[i] = all[i - 1]; + all[i] = l; + s.role = .{ .local = all[0] }; + for (&s.also, 1..) |*a, k| a.* = if (k <= n) all[k] else null; +} + /// A moment's memory as the frame walk reads it: all of it recorded, or /// none; and the call frame information the debugger recorded. const Memory = struct { -- cgit v1.3