summaryrefslogtreecommitdiff
path: root/src/trace/host.zig
blob: df2f8454072fd0a2dd50982a8ac60b542949a760 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! 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,
};

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);
        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();
    }
};