summaryrefslogtreecommitdiff
path: root/src/trace/host.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:08:49 -0300
committerGabriel Schneider <[email protected]>2026-11-10 11:08:49 -0300
commit0b29354e1ddf6cf75ac4c426a00dd3435ace5069 (patch)
tree918e6f6810a9b8b5a8ed17e698085e92018333b9 /src/trace/host.zig
downloadcomputeranim-0b29354e1ddf6cf75ac4c426a00dd3435ace5069.tar.gz
computeranim-0b29354e1ddf6cf75ac4c426a00dd3435ace5069.zip
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.
Diffstat (limited to 'src/trace/host.zig')
-rw-r--r--src/trace/host.zig71
1 files changed, 71 insertions, 0 deletions
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();
+ }
+};