From c94ade9e77a811d3ff75ffc2889a65375262ab1f Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 23:55:42 -0300 Subject: trace: names for PLT stubs and GOT slots; a call followed down; the vDSO is run through For a disassembly that follows a call into libc. A stub is open@plt and a slot open@got, from the relocations (libc's own, IFUNC and nameless, by the function they resolve to); telescope.got says what a slot holds and whether it is still lazy. A library function's entry is named even when its mapping was not known yet, by the alias the program imports. The chain holds through the stub's jump, the callee's first instruction, the syscall and the loader's resolver: ld.so's unwind section lacks the entry that ends it, which lost every frame there. A syscall is tied to its instruction's moment, and each frame's call is recorded with the row before it. Stepped by instruction, a call into the vDSO is one moment and its return the next: its clock loop, single-stepped, made three million moments of three reads. Co-Authored-By: Claude Fable 5.2 Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa --- src/trace/Debugger.zig | 52 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 12 deletions(-) (limited to 'src/trace/Debugger.zig') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index f4f945b..886b0fc 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -219,9 +219,28 @@ pub fn cont(d: *Debugger) !Stop { return d.report(try d.run(d.free())); } -/// One instruction. +/// One instruction; a call or jump into the vDSO runs to its return, as +/// one (`stepOne`). pub fn stepi(d: *Debugger) !Stop { - return d.report(try d.run(P.SINGLESTEP)); + return d.report(try d.stepOne()); +} + +/// One instruction, but one that enters the vDSO (the kernel's code mapped +/// into the process: `clock_gettime`, `gettimeofday`) from outside runs it +/// through to where it returns, the return address on top of the stack: +/// its loops retry while the kernel's time data changes under them, which +/// a stop at each instruction makes them do for as long as stops are slow. +fn stepOne(d: *Debugger) !Stop { + const from = d.regs.rip; + const stop = try d.run(P.SINGLESTEP); + if (stop != .step or !d.inVdso(d.regs.rip) or d.inVdso(from)) return stop; + var word: [8]u8 = undefined; + if (d.read(d.regs.rsp, &word) != 8) return stop; + return d.runTo(std.mem.readInt(u64, &word, .little), d.regs.rsp); +} + +fn inVdso(d: *const Debugger, addr: u64) bool { + return if (d.syms.mappingAt(addr)) |m| std.mem.eql(u8, m.name, "[vdso]") else false; } /// To the next source line: instructions until the line is another, a call @@ -231,7 +250,7 @@ pub fn step(d: *Debugger) !Stop { const tid = d.proc.tid; while (true) { const before = d.regs; - var stop = try d.run(P.SINGLESTEP); + var stop = try d.stepOne(); if (stop == .step and d.proc.tid == tid) { if (d.lineAt(d.regs.rip)) |here| { if (from == null or from.?.key != here.key or from.?.line != here.line) return d.report(.step); @@ -538,6 +557,8 @@ fn emitCode(d: *Debugger) !void { const target: ?u64 = if (from) |addr| (if (d.read(addr, &word) == 8) std.mem.readInt(u64, &word, .little) else null) else b.target; if (b.kind != 0) if (target) |to| { try d.nameAddress(to); + // Through memory: the slot it reads, where it has a name ("open@got"). + if (b.indirect != 0 and b.kind != 3) if (from) |slot| if (d.syms.symbolAt(slot) != null) try d.nameAddress(slot); try d.emit(.{ .kind = .branch, .aux = b.kind, .a = .{ to, b.taken, d.regs.rip, 0, 0, 0 } }, ""); }; } @@ -643,12 +664,14 @@ fn emitTelescope(d: *Debugger) !void { } } -/// 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`. +/// The call that returns to `ret`, the instruction before it and the one +/// at `ret`, recorded as instructions, once: a caller's rows in a descent, +/// and 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 ((try d.described.getOrPut(d.gpa, event.codeId(ret))).found_existing) return; + // Where its function begins: its symbol's, else its call frame information's (a stripped library's). + const sym = d.syms.symbolAt(ret) orelse if (d.syms.functionAt(ret -% 1)) |f| Symbols.Symbol{ .name = "", .addr = f.start, .size = f.len } else 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 { @@ -657,8 +680,7 @@ fn emitCall(d: *Debugger, ret: u64) !void { } }.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])); + for (k -| 2..k + 1) |n| _ = try d.emitInstruction(insnAt, sym.addr + starts[n], if (n > 0) @intCast(starts[n] - starts[n - 1]) else 0); } /// Follows `value` as a pointer: a pointer into code gets its name and @@ -678,12 +700,18 @@ fn chase(d: *Debugger, value: u64) !void { } } -/// The string whose id is `addr`: its name ("main+4"), recorded once. +/// The string whose id is `addr`: its name ("main+4"), recorded once. An +/// address in a mapping not read yet (the syscalls that made it not +/// followed) has its mappings read first, and one with no name is tried +/// again the next time. fn nameAddress(d: *Debugger, addr: u64) !void { - if ((try d.described.getOrPut(d.gpa, addr)).found_existing) return; + if (d.described.contains(addr)) return; + d.know(addr); var buf: [256]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); - if (d.syms.label(&w, addr) catch false) try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered()); + if (!(d.syms.label(&w, addr) catch false)) return; + try d.described.put(d.gpa, addr, {}); + try d.emit(.{ .kind = .string, .a = .{ addr, 0, 0, 0, 0, 0 } }, w.buffered()); } fn emitStop(d: *Debugger, stop: Stop) !void { -- cgit v1.3