From a7504c6bb2ec9e906891e42bc3df28b276e67143 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 12:09:27 -0300 Subject: the backends together: a ring under the debugger, strings that do not collide ztrace record and dbg take --ring, --preload and --alloc: the target gets the ring ztrace run gives it, and the debugger drains it while the target runs (polling for the stop, so a full ring cannot hold both) and before it reports a stop. A program compiled by ztrace cc, with the agent preloaded, stopped at breakpoints, is one recording in order; test-together checks it. Recording such a program showed the debugger's file names and the pass's strings both counted from 1: event.stringId (a hash, top bit set) is now what every producer without a table of its own uses. --- src/trace/Debugger.zig | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) (limited to 'src/trace/Debugger.zig') diff --git a/src/trace/Debugger.zig b/src/trace/Debugger.zig index b2cdd5e..d69adcc 100644 --- a/src/trace/Debugger.zig +++ b/src/trace/Debugger.zig @@ -29,6 +29,7 @@ const event = @import("event.zig"); const Event = event.Event; const Regs = event.Regs; const ptrace = @import("ptrace.zig"); +const Ring = @import("Ring.zig"); const Symbols = @import("Symbols.zig"); const syscalls_table = @import("syscalls.zig"); const P = linux.PTRACE; @@ -37,6 +38,10 @@ gpa: std.mem.Allocator, proc: ptrace.Process, syms: Symbols, sink: ?event.Sink, +/// The target's ring, when it carries a producer too (instrumentation, the +/// agent): drained into the sink while the target runs and before a stop +/// is reported, so its events and the stops are one recording, in order. +ring: ?*Ring = null, /// What a syscall does to the running target: nothing (the fastest), an /// event, or a stop as well. syscalls: enum { ignore, record, stop } = .ignore, @@ -207,12 +212,32 @@ fn resumeThread(d: *Debugger, request: u32) !ptrace.Stop { if (d.breakpointAt(d.regs.rip)) |bp| { try d.proc.write(bp.addr, &.{bp.saved}); try d.proc.run(P.SINGLESTEP); - const stop = try d.proc.wait(d.proc.tid); + const stop = try d.wait(d.proc.tid); if (!d.proc.gone) try d.proc.write(bp.addr, &.{0xcc}); if (request == P.SINGLESTEP or stop != .trap) return stop; } try d.proc.run(request); - return d.proc.waitAny(); + return d.waitAny(); +} + +fn waitAny(d: *Debugger) !ptrace.Stop { + return d.wait(if (d.proc.threads > 1) -1 else d.proc.pid); +} + +/// The next stop. With a ring it is polled for and the ring drained +/// meanwhile: a target waiting for room in a full ring would never stop. +fn wait(d: *Debugger, tid: linux.pid_t) !ptrace.Stop { + const ring = d.ring orelse return d.proc.wait(tid); + var idle: u32 = 0; + while (true) { + if (try d.proc.poll(tid)) |stop| { + _ = try ring.drain(d.sink.?); + return stop; + } + // A step is back in microseconds: look again before sleeping. + idle = if (try ring.drain(d.sink.?) > 0) 0 else idle + 1; + if (idle > 64) ring.wait(200 * std.time.ns_per_us); + } } /// Runs the target with `request` until something stops it for the caller. @@ -224,7 +249,7 @@ fn run(d: *Debugger, request: u32) !Stop { while (true) { const before = d.regs; const tid = d.proc.tid; - const raw = if (resumes) try d.resumeThread(request) else try d.proc.waitAny(); + const raw = if (resumes) try d.resumeThread(request) else try d.waitAny(); resumes = true; switch (raw) { .exited, .killed => { @@ -386,7 +411,7 @@ fn emitLine(d: *Debugger) !void { return err; }; if (named.found_existing) d.gpa.free(path) else { - named.value_ptr.* = d.files.count(); + 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.*); -- cgit v1.3