summaryrefslogtreecommitdiff
path: root/gui/src/Client.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-12 13:19:32 -0300
committerGabriel Schneider <[email protected]>2026-11-13 00:41:47 -0300
commit4b4113a0217ee27c78e7efddb66ff5a455d49083 (patch)
treeb848dc1e92c3126dec11da8547e8033ff3990140 /gui/src/Client.zig
parenteab50d1c040774351f332a1568fa332c377860bd (diff)
downloadcomputeranim-4b4113a0217ee27c78e7efddb66ff5a455d49083.tar.gz
computeranim-4b4113a0217ee27c78e7efddb66ff5a455d49083.zip
debugger: add a native GUI with live V8 JIT debugging
Diffstat (limited to 'gui/src/Client.zig')
-rw-r--r--gui/src/Client.zig94
1 files changed, 94 insertions, 0 deletions
diff --git a/gui/src/Client.zig b/gui/src/Client.zig
new file mode 100644
index 0000000..8ced2b6
--- /dev/null
+++ b/gui/src/Client.zig
@@ -0,0 +1,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;
+}