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/cli/dbg.zig | 12 +++++++++--- src/cli/record.zig | 11 ++++++++--- src/cli/util.zig | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 6 deletions(-) (limited to 'src/cli') diff --git a/src/cli/dbg.zig b/src/cli/dbg.zig index 007b790..6876f84 100644 --- a/src/cli/dbg.zig +++ b/src/cli/dbg.zig @@ -7,7 +7,8 @@ const fail = util.fail; const Moment = zt.store.Moment; pub const usage = - \\usage: ztrace dbg [-o FILE] [--stack BYTES] [-s BYTES] [--max-size BYTES] -- PROGRAM [ARGS] + \\usage: ztrace dbg [-o FILE] [--stack BYTES] [-s BYTES] [--max-size BYTES] + \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ ztrace dbg [-o FILE] [--stack BYTES] [-s BYTES] [--max-size BYTES] -p PID \\ \\Starts PROGRAM stopped at its first instruction (or attaches to PID) @@ -34,7 +35,7 @@ pub const usage = \\bytes (default 256) of what a syscall points at. The session ends when \\the file reaches --max-size (default 1G). \\ -; +++ util.Producer.usage; pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const arena = init.arena.allocator(); @@ -44,12 +45,15 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var stack_bytes: usize = 4096; var capture: ?usize = null; var max_bytes: ?u64 = null; + var producer: util.Producer = .{}; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; if (std.mem.eql(u8, arg, "--")) { i += 1; break; + } else if (producer.take(args, &i)) { + // Its own argument, when it has one, is taken. } else if (std.mem.eql(u8, arg, "-o") and i + 1 < args.len) { i += 1; out = args[i]; @@ -71,11 +75,13 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { s.rec.cut = (s.rec.cut | zt.Kind.syscall_enter.bit()) & ~zt.Kind.line.bit(); if (max_bytes) |n| s.rec.max_bytes = n; const sink: zt.Sink = .{ .ctx = &s, .emitFn = Session.emit }; + const produced = if (pid == null) try producer.start(arena, init.minimal.environ.block.slice) else null; s.d = if (pid) |p| zt.Debugger.attach(init.gpa, init.io, p, sink) catch |err| fail("cannot attach to {d}: {t}\n", .{ p, err }) else - try zt.Debugger.spawn(init.gpa, init.io, try util.resolve(init, args[i]), try util.targetArgv(arena, args[i..]), init.minimal.environ.block.slice.ptr, sink); + try zt.Debugger.spawn(init.gpa, init.io, try util.resolve(init, args[i]), try util.targetArgv(arena, args[i..]), if (produced) |p| p.envp.ptr else init.minimal.environ.block.slice.ptr, sink); defer s.d.deinit(); + if (produced) |p| s.d.ring = p.ring; defer s.stop_payload.deinit(init.gpa); s.d.stack_watch = stack_bytes; if (capture) |n| s.d.capture = n; diff --git a/src/cli/record.zig b/src/cli/record.zig index 7b44522..8a30761 100644 --- a/src/cli/record.zig +++ b/src/cli/record.zig @@ -8,7 +8,7 @@ const fail = util.fail; pub const usage = \\usage: ztrace record [-o FILE] [--by insn|line|syscall|break] [-b WHERE]... [-w WHAT[,LEN]]... \\ [--stack BYTES] [-s BYTES] [--steps N] [--max-size BYTES] [--strace] - \\ -- PROGRAM [ARGS] + \\ [--ring] [--preload LIB] [--alloc] -- PROGRAM [ARGS] \\ \\Runs PROGRAM under ptrace into a recording (default out.ztrace). --by \\says where it stops, each stop a moment: @@ -27,7 +27,7 @@ pub const usage = \\--steps ends the recording after N moments, and --max-size when the \\file is that large (default 1G). Sizes take a suffix: 64K, 4G. \\ -; +++ util.Producer.usage; pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { const arena = init.arena.allocator(); @@ -40,6 +40,7 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var strace = false; var breaks: std.ArrayList([]const u8) = .empty; var watches: std.ArrayList([]const u8) = .empty; + var producer: util.Producer = .{}; var i: usize = 0; while (i < args.len) : (i += 1) { const arg = args[i]; @@ -51,6 +52,8 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { by = .syscall; } else if (std.mem.eql(u8, arg, "--strace")) { strace = true; + } else if (producer.take(args, &i)) { + // Its own argument, when it has one, is taken. } else if (value == null) { fail(usage, .{}); } else if (std.mem.eql(u8, arg, "-o")) { @@ -86,8 +89,10 @@ pub fn run(init: std.process.Init, args: []const [:0]const u8) !void { var stderr = std.Io.File.stderr().writer(init.io, &stderr_buf); var printer: zt.syscalls.Printer = .{ .w = &stderr.interface, .next = rec.sink() }; - var d = try zt.Debugger.spawn(init.gpa, init.io, path, argv, init.minimal.environ.block.slice.ptr, if (strace) printer.sink() else rec.sink()); + const produced = try producer.start(arena, init.minimal.environ.block.slice); + var d = try zt.Debugger.spawn(init.gpa, init.io, path, argv, if (produced) |p| p.envp.ptr else init.minimal.environ.block.slice.ptr, if (strace) printer.sink() else rec.sink()); defer d.deinit(); + if (produced) |p| d.ring = p.ring; d.syscalls = if (by == .syscall) .stop else .record; d.stack_watch = stack_bytes; if (capture) |n| d.capture = n; diff --git a/src/cli/util.zig b/src/cli/util.zig index abdedd1..1d7ee4c 100644 --- a/src/cli/util.zig +++ b/src/cli/util.zig @@ -1,5 +1,6 @@ //! What every command needs. const std = @import("std"); +const zt = @import("ztrace"); pub fn fail(comptime fmt: []const u8, args: anytype) noreturn { std.debug.print(fmt, args); @@ -19,6 +20,48 @@ pub fn resolve(init: std.process.Init, name: [:0]const u8) ![*:0]const u8 { fail("{s}: not found in PATH\n", .{name}); } +/// A ring for a target run under the debugger: for one that carries a +/// producer too, so what it emits and the debugger's stops are one recording. +pub const Producer = struct { + wanted: bool = false, + preload: ?[]const u8 = null, + alloc: bool = false, + + pub const usage = + \\--ring gives PROGRAM a shared-memory ring, as `ztrace run` does: for a + \\program compiled by `ztrace cc` or linked with the agent, whose own + \\events are then recorded between the stops. --preload LIB puts the + \\agent into it, and --alloc has the agent record allocations. (The + \\agent's hooks and syscalls are signals, which a debugger takes first: + \\under ptrace use breakpoints and the debugger's own syscall stops.) + \\ + ; + + /// Takes `args[i.*]` when it is one of these options. + pub fn take(p: *Producer, args: []const [:0]const u8, i: *usize) bool { + const arg = args[i.*]; + if (std.mem.eql(u8, arg, "--ring")) { + p.wanted = true; + } else if (std.mem.eql(u8, arg, "--alloc")) { + p.wanted = true; + p.alloc = true; + } else if (std.mem.eql(u8, arg, "--preload") and i.* + 1 < args.len) { + i.* += 1; + p.wanted = true; + p.preload = args[i.*]; + } else return false; + return true; + } + + /// The ring and the environment that hands it to the target. + pub fn start(p: Producer, arena: std.mem.Allocator, env: [:null]const ?[*:0]const u8) !?struct { ring: *zt.Ring, envp: [:null]const ?[*:0]const u8 } { + if (!p.wanted) return null; + const ring = try arena.create(zt.Ring); + ring.* = try zt.Ring.create(1 << 24); + return .{ .ring = ring, .envp = try zt.host.environment(arena, env, ring, p.preload, if (p.alloc) &.{"ZTRACE_ALLOC=1"} else &.{}) }; + } +}; + /// The arguments after a command's own, as the target's argv. pub fn targetArgv(arena: std.mem.Allocator, args: []const [:0]const u8) ![:null]?[*:0]const u8 { const argv = try arena.allocSentinel(?[*:0]const u8, args.len, null); -- cgit v1.3