summaryrefslogtreecommitdiff
path: root/src/trace/client.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/client.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/client.zig')
-rw-r--r--src/trace/client.zig56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/trace/client.zig b/src/trace/client.zig
new file mode 100644
index 0000000..c8f2eea
--- /dev/null
+++ b/src/trace/client.zig
@@ -0,0 +1,56 @@
+//! The producer side, inside the traced process: finds the ring its tracer
+//! handed it and emits into it. Raw syscalls only, no libc, no allocation:
+//! callable from a library constructor, from malloc, from a signal handler.
+const std = @import("std");
+const linux = std.os.linux;
+const event = @import("event.zig");
+const Ring = @import("Ring.zig");
+
+/// The tracer sets this to the ring's inherited file descriptor.
+pub const env_fd = "ZTRACE_RING_FD";
+
+var ring: Ring = undefined;
+/// 0 untried, 1 attaching, 2 attached, 3 no ring.
+var state: std.atomic.Value(u8) = .init(0);
+
+/// The process's ring, attached on first use. Null when it is not traced.
+pub fn get() ?*Ring {
+ while (true) switch (state.load(.acquire)) {
+ 2 => return &ring,
+ 3 => return null,
+ 1 => _ = linux.sched_yield(),
+ else => if (state.cmpxchgStrong(0, 1, .acquire, .monotonic) == null) {
+ const attached = if (envFd()) |fd| Ring.attach(fd) catch null else null;
+ if (attached) |r| ring = r;
+ state.store(if (attached != null) 2 else 3, .release);
+ },
+ };
+}
+
+pub fn emit(kind: event.Kind, aux: u16, a: [6]u64, payload: []const u8) void {
+ const r = get() orelse return;
+ // shortcut: a gettid syscall per event; cache it per thread once the agent has TLS.
+ r.emit(.{ .ts = event.now(), .tid = @intCast(linux.gettid()), .kind = kind, .aux = aux, .a = a }, payload);
+}
+
+/// `ZTRACE_RING_FD` from /proc/self/environ: a preloaded library cannot
+/// count on libc's environ being set up yet.
+fn envFd() ?linux.fd_t {
+ const rc = linux.open("/proc/self/environ", .{}, 0);
+ if (linux.errno(rc) != .SUCCESS) return null;
+ const fd: linux.fd_t = @intCast(rc);
+ defer _ = linux.close(fd);
+ var buf: [1 << 16]u8 = undefined;
+ var len: usize = 0;
+ while (len < buf.len) {
+ const n = linux.read(fd, buf[len..].ptr, buf.len - len);
+ if (linux.errno(n) != .SUCCESS or n == 0) break;
+ len += n;
+ }
+ var vars = std.mem.splitScalar(u8, buf[0..len], 0);
+ while (vars.next()) |v| {
+ if (!std.mem.startsWith(u8, v, env_fd ++ "=")) continue;
+ return std.fmt.parseInt(linux.fd_t, v[env_fd.len + 1 ..], 10) catch null;
+ }
+ return null;
+}