From 0b29354e1ddf6cf75ac4c426a00dd3435ace5069 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:08:49 -0300 Subject: ztrace: the event, the ring, the recording, a ptrace loop One 64-byte Event for every backend; a shared-memory ring from target to tracer with stop/resume; a Recorder that folds events into xitdb moments (registers, memory in 64-byte chunks with known-byte masks, strings) and a Trace that reads any moment back; a minimal ptrace Process; ztrace record, run and dump. Per-area build files so the areas grow apart. --- src/trace/host.zig | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/trace/host.zig (limited to 'src/trace/host.zig') diff --git a/src/trace/host.zig b/src/trace/host.zig new file mode 100644 index 0000000..f111d55 --- /dev/null +++ b/src/trace/host.zig @@ -0,0 +1,71 @@ +//! The tracer's side of a ring: run a program that carries a ring producer +//! (the preloaded agent, or instrumentation linked into it) and pump what it +//! emits into a sink. No ptrace: the target runs at full speed. +const std = @import("std"); +const linux = std.os.linux; +const event = @import("event.zig"); +const Ring = @import("Ring.zig"); +const client = @import("client.zig"); + +pub const Options = struct { + ring_bytes: usize = 1 << 24, + /// A shared library to LD_PRELOAD into the target (the agent). + preload: ?[]const u8 = null, +}; + +pub const Session = struct { + ring: Ring, + pid: linux.pid_t, + exit_code: ?u8 = null, + + /// Starts `path` with the ring in its environment. `arena` holds the + /// child's environment block. + pub fn spawn(arena: std.mem.Allocator, path: [*:0]const u8, argv: [*:null]const ?[*:0]const u8, env: [:null]const ?[*:0]const u8, options: Options) !Session { + var ring = try Ring.create(options.ring_bytes); + errdefer ring.close(); + var envp: std.ArrayList(?[*:0]const u8) = .empty; + for (env) |v| { + const s = std.mem.span(v.?); + if (std.mem.startsWith(u8, s, client.env_fd ++ "=")) continue; + if (options.preload != null and std.mem.startsWith(u8, s, "LD_PRELOAD=")) continue; + try envp.append(arena, v); + } + try envp.append(arena, try std.fmt.allocPrintSentinel(arena, client.env_fd ++ "={d}", .{ring.fd}, 0)); + if (options.preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0)); + const block = try envp.toOwnedSliceSentinel(arena, null); + + const rc = linux.fork(); + if (linux.errno(rc) != .SUCCESS) return error.Fork; + if (rc == 0) { + _ = linux.execve(path, argv, block.ptr); + linux.exit_group(127); + } + return .{ .ring = ring, .pid = @intCast(rc) }; + } + + /// Moves what the target emitted into `to`, waiting up to `timeout_ns` + /// when there is nothing. False once the target has exited and the ring + /// is empty: `exit_code` is then set. + pub fn pump(s: *Session, to: event.Sink, timeout_ns: u64) !bool { + if (try s.ring.drain(to) > 0) return true; + if (s.exit_code != null) return false; + var status: u32 = 0; + const rc = linux.waitpid(s.pid, @ptrCast(&status), linux.W.NOHANG); + if (linux.errno(rc) == .SUCCESS and rc != 0) { + s.exit_code = if (linux.W.IFEXITED(status)) linux.W.EXITSTATUS(status) else 128 +| @as(u8, @truncate(@intFromEnum(linux.W.TERMSIG(status)))); + // What it emitted before dying is still in the ring. + return true; + } + s.ring.wait(timeout_ns); + return true; + } + + pub fn close(s: *Session) void { + if (s.exit_code == null) { + _ = linux.kill(s.pid, .KILL); + var status: u32 = 0; + _ = linux.waitpid(s.pid, @ptrCast(&status), 0); + } + s.ring.close(); + } +}; -- cgit v1.3