//! 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, /// The kinds the target is held at (`Ring.Header.stop_mask`), set before /// it starts: no first event slips by. stop_mask: u64 = 0, }; /// `env` with the ring in it, the agent preloaded when there is one, and /// `extra` (settings for the agent) after them. pub fn environment(arena: std.mem.Allocator, env: [:null]const ?[*:0]const u8, ring: *const Ring, preload: ?[]const u8, extra: []const [*:0]const u8) ![:null]const ?[*:0]const u8 { 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 (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 (preload) |lib| try envp.append(arena, try std.fmt.allocPrintSentinel(arena, "LD_PRELOAD={s}", .{lib}, 0)); for (extra) |v| try envp.append(arena, v); return envp.toOwnedSliceSentinel(arena, 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(); ring.hdr.stop_mask.store(options.stop_mask, .monotonic); const block = try environment(arena, env, &ring, options.preload, &.{}); 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(); } };