summaryrefslogtreecommitdiff
path: root/src/cli/run.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/cli/run.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/cli/run.zig')
-rw-r--r--src/cli/run.zig63
1 files changed, 63 insertions, 0 deletions
diff --git a/src/cli/run.zig b/src/cli/run.zig
new file mode 100644
index 0000000..c4f1b25
--- /dev/null
+++ b/src/cli/run.zig
@@ -0,0 +1,63 @@
+//! ztrace run: a program that carries a ring producer (the preloaded agent,
+//! or instrumentation linked in), recorded at full speed, no ptrace.
+const std = @import("std");
+const zt = @import("ztrace");
+const util = @import("util.zig");
+const fail = util.fail;
+
+pub const usage =
+ \\usage: ztrace run [-o FILE] [--preload LIB] [--ring BYTES] -- PROGRAM [ARGS]
+ \\
+ \\Runs PROGRAM with a shared-memory ring in its environment and records
+ \\what it emits (default out.ztrace). --preload puts a library (the agent)
+ \\into it with LD_PRELOAD. --ring sizes the ring (a power of two, default
+ \\16 MiB).
+ \\
+;
+
+pub fn run(init: std.process.Init, args: []const [:0]const u8) !void {
+ const arena = init.arena.allocator();
+ var out: []const u8 = "out.ztrace";
+ var options: zt.host.Options = .{};
+ 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 (std.mem.eql(u8, arg, "-o") and i + 1 < args.len) {
+ i += 1;
+ out = args[i];
+ } else if (std.mem.eql(u8, arg, "--preload") and i + 1 < args.len) {
+ i += 1;
+ options.preload = args[i];
+ } else if (std.mem.eql(u8, arg, "--ring") and i + 1 < args.len) {
+ i += 1;
+ options.ring_bytes = std.fmt.parseInt(usize, args[i], 0) catch 0;
+ if (!std.math.isPowerOfTwo(options.ring_bytes) or options.ring_bytes < 4096) fail("--ring needs a power of two, 4096 or more\n", .{});
+ } else fail(usage, .{});
+ }
+ if (i >= args.len) fail(usage, .{});
+
+ const path = try util.resolve(init, args[i]);
+ const argv = try util.targetArgv(arena, args[i..]);
+ var session = try zt.host.Session.spawn(arena, path, argv, init.minimal.environ.block.slice, options);
+ defer session.close();
+ var rec = try zt.Recorder.create(init.gpa, init.io, out);
+
+ const start = zt.event.now();
+ while (try session.pump(rec.sink(), 10 * std.time.ns_per_ms)) {}
+ const ns = zt.event.now() - start;
+
+ const events = rec.count;
+ const moments = rec.moments + @intFromBool(rec.delta.items.len > 0);
+ const dropped = session.ring.hdr.dropped.load(.monotonic);
+ try rec.close();
+ const size = (try std.Io.Dir.cwd().statFile(init.io, out, .{})).size;
+ std.debug.print("{s}: {d} moments, {d} events ({d} dropped), {d} bytes, {d:.1} ms, {d:.0} events/s, exit {d}\n", .{
+ out, moments,
+ events, dropped,
+ size, @as(f64, @floatFromInt(ns)) / 1e6,
+ @as(f64, @floatFromInt(events)) / (@as(f64, @floatFromInt(ns)) / 1e9), session.exit_code.?,
+ });
+}