summaryrefslogtreecommitdiff
path: root/gui/src/Client.zig
blob: 8ced2b6fc7f7569a09acb40df068468ec6fe2680 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
//! The GUI never waits for ptrace. One std.Io task reads the controller's replies.
const std = @import("std");
const zt = @import("ztrace");
const Client = @This();
io: std.Io,
gpa: std.mem.Allocator,
child: std.process.Child,
reader: ?std.Io.Future(void) = null,
mutex: std.Io.Mutex = .init,
latest: Update = .{},
path: []const u8,

pub const Update = struct {
    generation: u64 = 0,
    status: zt.control.Status = .running,
    pid: i32 = 0,
    tid: u32 = 0,
    moments: u64 = 0,
    pc: u64 = 0,
    elapsed_ns: u64 = 0,
    text: [256]u8 = @splat(0),
    len: usize = 0,

    pub fn reason(u: *const Update) []const u8 {
        return u.text[0..u.len];
    }
};

pub fn start(gpa: std.mem.Allocator, io: std.Io, argv: []const []const u8) !*Client {
    const c = try gpa.create(Client);
    errdefer gpa.destroy(c);
    const path = try std.fmt.allocPrint(gpa, "/tmp/ztrace-gui-{d}-{d}.ztrace", .{ std.os.linux.getpid(), std.Io.Clock.awake.now(io).nanoseconds });
    errdefer gpa.free(path);
    var args: std.ArrayList([]const u8) = .empty;
    defer args.deinit(gpa);
    const exe = try std.process.executablePathAlloc(io, gpa);
    defer gpa.free(exe);
    const tracer = try std.fs.path.join(gpa, &.{ std.fs.path.dirname(exe).?, "ztrace" });
    defer gpa.free(tracer);
    try args.appendSlice(gpa, &.{ tracer, "serve", path, "--" });
    try args.appendSlice(gpa, argv);
    c.* = .{ .gpa = gpa, .io = io, .path = path, .child = try std.process.spawn(io, .{ .argv = args.items, .stdin = .pipe, .stdout = .pipe, .stderr = .inherit }) };
    errdefer c.child.kill(io);
    c.reader = try std.Io.concurrent(io, read, .{c});
    return c;
}

pub fn close(c: *Client) void {
    c.send("quit") catch {};
    if (c.child.stdin) |pipe| pipe.close(c.io);
    c.child.stdin = null;
    if (c.reader) |*r| r.await(c.io);
    _ = c.child.wait(c.io) catch {};
    c.gpa.free(c.path);
    c.gpa.destroy(c);
}

pub fn send(c: *Client, command: []const u8) !void {
    const pipe = c.child.stdin orelse return error.Disconnected;
    if (command.len > 2048 or std.mem.indexOfScalar(u8, command, '\n') != null) return error.InvalidCommand;
    var buffer: [4096]u8 = undefined;
    var writer = pipe.writerStreaming(c.io, &buffer);
    try writer.interface.writeAll(command);
    try writer.interface.writeByte('\n');
    try writer.interface.flush();
}

pub fn poll(c: *Client) ?Update {
    if (!c.mutex.tryLock()) return null;
    defer c.mutex.unlock(c.io);
    return c.latest;
}

fn read(c: *Client) void {
    var buffer: [4096]u8 = undefined;
    var reader = c.child.stdout.?.readerStreaming(c.io, &buffer);
    while (true) {
        const line = (reader.interface.takeDelimiter('\n') catch break) orelse break;
        const parsed = std.json.parseFromSlice(zt.control.Reply, c.gpa, line, .{}) catch break;
        defer parsed.deinit();
        const reply = parsed.value;
        c.mutex.lock(c.io) catch return;
        c.latest = .{ .generation = c.latest.generation + 1, .status = reply.status, .pid = reply.pid, .tid = reply.tid, .moments = reply.moments, .pc = reply.pc, .elapsed_ns = reply.elapsed_ns, .len = @min(reply.reason.len, 256) };
        @memcpy(c.latest.text[0..c.latest.len], reply.reason[0..c.latest.len]);
        c.mutex.unlock(c.io);
    }
    c.mutex.lock(c.io) catch return;
    defer c.mutex.unlock(c.io);
    c.latest.generation += 1;
    c.latest.status = .exited;
    const ended = "controller disconnected";
    @memcpy(c.latest.text[0..ended.len], ended);
    c.latest.len = ended.len;
}