diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 16:03:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 16:05:20 -0300 |
| commit | 66a2a7cf733f6abc1f79561c52d901ce67019fb1 (patch) | |
| tree | b261d533887b37fb5287ad384d4f845348b35d81 /src/trace/Debugger.zig | |
| parent | e0c4bfcc0395a561804e33942028c519f4e267f7 (diff) | |
| download | computeranim-66a2a7cf733f6abc1f79561c52d901ce67019fb1.tar.gz computeranim-66a2a7cf733f6abc1f79561c52d901ce67019fb1.zip | |
trace: what the context draws is in the recording; SQLite on the ladder
An instruction's string carries its source line and, for a direct call,
how many parameters the callee takes (DWARF); telescope.frames gives each
frame's extent on the stack; with --alloc the debugger watches each live
allocation and the allocator's header before it, changes only, 256 KiB a
stop at most. C++ names are shown and found as the source has them (the
command links the C++ runtime for its demangler).
SQLite 3.50.4 through every backend, measured (README, bench/sqlite.sh).
It found two faults: a file compiled by its full path was recorded under
a path joined onto the compile directory, and ztrace run noticed a
target's exit up to 10 ms late.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'src/trace/Debugger.zig')
| -rw-r--r-- | src/trace/Debugger.zig | 130 |
1 files changed, 108 insertions, 22 deletions
diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index bf24f74..bbdaee6 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -19,8 +19,9 @@ //! no file ("[stack]"). //! //! Memory is never recorded wholesale: only the watched regions, the stack -//! window and what syscalls point at, cut at `capture`, and of those only -//! the words that changed since they were last sent. +//! window, the live allocations a ring tells of and what syscalls point +//! at, cut at `capture`, and of those only the words that changed since +//! they were last sent. const Debugger = @This(); const std = @import("std"); @@ -33,6 +34,7 @@ const Ring = @import("Ring.zig"); const notes = @import("usdt.zig"); const Symbols = @import("Symbols.zig"); const syscalls_table = @import("syscalls.zig"); +const Line = @import("store.zig").Line; const P = linux.PTRACE; gpa: std.mem.Allocator, @@ -58,6 +60,12 @@ captured: std.ArrayList(u8) = .empty, watches: std.ArrayList(Watch) = .empty, /// The variables the target declared through its ring, watched like them. declared: std.ArrayList(Watch) = .empty, +/// The live allocations the target tells of through its ring (`--alloc`), +/// by address: watched too, each with the allocator's header before it. +allocations: std.ArrayList(Watch) = .empty, +/// The most read of them at a stop, in bytes: a stop's cost must not grow +/// with the target's heap. +allocation_watch: usize = 256 << 10, breakpoints: std.ArrayList(Breakpoint) = .empty, /// The current thread's registers at its last stop. regs: Regs = .{}, @@ -171,6 +179,7 @@ pub fn deinit(d: *Debugger) void { d.syms.deinit(); d.watches.deinit(d.gpa); d.declared.deinit(d.gpa); + d.allocations.deinit(d.gpa); d.breakpoints.deinit(d.gpa); for (d.sites.items) |site| d.gpa.destroy(site); d.sites.deinit(d.gpa); @@ -299,9 +308,47 @@ fn tap(ctx: *anyopaque, ev: *const Event, payload: []const u8) anyerror!void { if (d.declared.items.len >= 256) _ = d.declared.orderedRemove(0); try d.declared.append(d.gpa, w); } + // An allocation is watched from its birth to its free, kept in address order. + if (ev.kind == .alloc or ev.kind == .free) { + const items = d.allocations.items; + const at = std.sort.lowerBound(Watch, items, ev.a[0], struct { + fn order(want: u64, have: Watch) std.math.Order { + return std.math.order(want, have.addr); + } + }.order); + const found = at < items.len and items[at].addr == ev.a[0]; + if (ev.kind == .free) { + if (found) _ = d.allocations.orderedRemove(at); + } else if (found) items[at].len = @intCast(ev.a[1]) else try d.allocations.insert(d.gpa, at, .{ .addr = ev.a[0], .len = @intCast(ev.a[1]) }); + } return d.sink.?.emit(ev.*, payload); } +/// The live allocations as they are now, each with the 16 bytes before it +/// (glibc's chunk header, musl's), and of a large one its first 4 KiB, as +/// a declared variable: neighbours are read as one region, and what is +/// sent of them is what changed. +fn snapshotAllocations(d: *Debugger) !void { + var from: u64 = 0; + var to: u64 = 0; + var room = d.allocation_watch; + for (d.allocations.items) |a| { + const start = a.addr -| 16; + const end = a.addr + @min(a.len, 4096); + // shortcut: past `allocation_watch` the allocations highest in memory go unread, upgrade by reading only what the instructions' `touch`es wrote. + if (end - start > room) break; + room -= end - start; + if (start <= to) { + to = @max(to, end); + continue; + } + if (to > from) try d.snapshot(from, to - from); + from = start; + to = end; + } + if (to > from) try d.snapshot(from, to - from); +} + /// Runs the target with `request` until something stops it for the caller. /// What does not (a recorded syscall, a thread's birth, a hook that says /// go) is sent as an event here; the stop itself is for `report`. @@ -426,6 +473,7 @@ fn report(d: *Debugger, stop: Stop) !Stop { if (d.stack_watch > 0) try d.snapshot(d.regs.rsp, d.stack_watch); for (d.watches.items) |w| try d.snapshot(w.addr, w.len); for (d.declared.items) |w| try d.snapshot(w.addr, w.len); + try d.snapshotAllocations(); try d.emitCode(); try d.emitTelescope(); } @@ -496,16 +544,29 @@ fn emitCode(d: *Debugger) !void { /// Records the instruction at `addr` as a string (`event.codeId`), once: a /// byte for its length, a byte for the length of the one before it (0: -/// not known), then its text. Returns its length; null for no instruction. +/// not known), then its text; and when either is known, a zero, its source +/// line (`store.Line`) and a byte for how many parameters a direct call's +/// callee takes, plus one (0: not known). Returns its length; null for no +/// instruction. fn emitInstruction(d: *Debugger, insnAt: @typeInfo(@TypeOf(d.disasm)).optional.child, addr: u64, back: u8) !?u8 { if (d.code_len.get(addr)) |len| return len; var code: [15]u8 = undefined; - var text: [192]u8 = undefined; + var text: [2 + 192 + 2 + @sizeOf(Line)]u8 = undefined; const n = d.read(addr, &code); - const insn = insnAt(code[0..n], addr, text[2..], null, &.{}) orelse return null; + const insn = insnAt(code[0..n], addr, text[2..][0..192], null, &.{}) orelse return null; text[0] = insn.len; text[1] = back; - try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0 .. 2 + insn.text.len]); + var len = 2 + insn.text.len; + const b = insn.branch; + const params: u8 = if (b.kind == 1 and b.indirect == 0 and b.target != 0) (if (d.syms.parameters(b.target)) |p| p +| 1 else 0) else 0; + const line: Line = if (d.lineAt(addr)) |l| .{ .file = try d.fileId(l), .line = l.line, .column = l.column } else .{}; + if (line.line != 0 or params != 0) { + text[len] = 0; + @memcpy(text[len + 1 ..][0..@sizeOf(Line)], std.mem.asBytes(&line)); + text[len + 1 + @sizeOf(Line)] = params; + len += 2 + @sizeOf(Line); + } + try d.emit(.{ .kind = .string, .a = .{ event.codeId(addr), 0, 0, 0, 0, 0 } }, text[0..len]); try d.code_len.put(d.gpa, addr, insn.len); return insn.len; } @@ -627,21 +688,26 @@ pub fn decode(d: *Debugger, nr: u16) ![]const u8 { return text[0 .. text.len - @intFromBool(text.len == @sizeOf(Regs))]; } +/// The string id of a line's file: its path, recorded the first time. +fn fileId(d: *Debugger, l: Symbols.Line) !u64 { + if (d.file_ids.get(l.key)) |id| return id; + // A file compiled by its full path names itself whole. + const path = if (std.fs.path.isAbsolute(l.name)) try d.gpa.dupe(u8, l.name) else try std.fs.path.join(d.gpa, &.{ l.dir, l.name }); + const named = d.files.getOrPut(d.gpa, path) catch |err| { + d.gpa.free(path); + return err; + }; + if (named.found_existing) d.gpa.free(path) else { + named.value_ptr.* = event.stringId(path); + try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path); + } + try d.file_ids.put(d.gpa, l.key, named.value_ptr.*); + return named.value_ptr.*; +} + fn emitLine(d: *Debugger) !void { const l = d.lineAt(d.regs.rip) orelse return; - const file = d.file_ids.get(l.key) orelse id: { - const path = try std.fs.path.join(d.gpa, &.{ l.dir, l.name }); - const named = d.files.getOrPut(d.gpa, path) catch |err| { - d.gpa.free(path); - return err; - }; - if (named.found_existing) d.gpa.free(path) else { - named.value_ptr.* = event.stringId(path); - try d.emit(.{ .kind = .string, .a = .{ named.value_ptr.*, 0, 0, 0, 0, 0 } }, path); - } - try d.file_ids.put(d.gpa, l.key, named.value_ptr.*); - break :id named.value_ptr.*; - }; + const file = try d.fileId(l); if (file == d.last_line.file and l.line == d.last_line.line) return; d.last_line = .{ .file = file, .line = l.line }; try d.emit(.{ .kind = .line, .a = .{ file, l.line, l.column, d.regs.rip, 0, 0 } }, ""); @@ -904,19 +970,39 @@ fn know(d: *Debugger, addr: u64) void { /// caller's address is still on top of the stack. // shortcut: a function that keeps no frame hides its caller (a leaf, or one stopped inside its prologue), upgrade with the unwind tables (`std.debug.Dwarf.Unwind`). pub fn backtrace(mem: anytype, regs: *const Regs, at_entry: bool, out: []u64) []u64 { + return walk(u64, mem, regs, at_entry, out); +} + +/// A frame of the call chain: its pc, and the stack it holds, from `low` +/// (the stack pointer, for the newest) up to `high`, just past its return +/// address: where the caller's stack pointer was before the call (the +/// CFA). `high` is `low` where the frame's end is not known. +pub const Frame = struct { pc: u64, low: u64, high: u64 }; + +/// `backtrace`, each frame with its extent on the stack. +pub fn frames(mem: anytype, regs: *const Regs, at_entry: bool, out: []Frame) []Frame { + return walk(Frame, mem, regs, at_entry, out); +} + +fn walk(comptime T: type, mem: anytype, regs: *const Regs, at_entry: bool, out: []T) []T { var n: usize = 0; var pc = regs.rip; var fp = regs.rbp; + var low = regs.rsp; var frame: [16]u8 = undefined; if (at_entry and out.len > 1 and mem.read(regs.rsp, frame[0..8]) == 8) { - out[0] = pc; + // Its frame is the return address alone. + out[0] = if (T == u64) pc else .{ .pc = pc, .low = low, .high = low + 8 }; n = 1; + low += 8; pc = std.mem.readInt(u64, frame[0..8], .little); } while (n < out.len) { - out[n] = pc; + const known = fp != 0 and mem.read(fp, &frame) == 16; + out[n] = if (T == u64) pc else .{ .pc = pc, .low = low, .high = if (known and fp >= low) fp + 16 else low }; n += 1; - if (fp == 0 or mem.read(fp, &frame) != 16) break; + if (!known) break; + low = @max(low, fp + 16); const next = std.mem.readInt(u64, frame[0..8], .little); pc = std.mem.readInt(u64, frame[8..16], .little); // Frames lie up the stack: one that does not is not a frame. |
