//! sketchbook-probe: prints the live state of any running sketch, from its //! state file alone. The file header carries the layout, so the probe needs //! no knowledge of the sketch: it maps the same pages read-only. const std = @import("std"); const memory = @import("memory.zig"); const fields = @import("fields.zig"); const usage = \\usage: sketchbook-probe [--watch] [--interval MS] STATE_FILE \\Prints every field of the live state; --watch prints again on change. \\ ; pub fn main(init: std.process.Init) !void { var path: ?[]const u8 = null; var watch = false; var interval_ms: i64 = 100; var args = init.minimal.args.iterate(); _ = args.skip(); while (args.next()) |arg| { if (std.mem.eql(u8, arg, "--watch")) { watch = true; } else if (std.mem.eql(u8, arg, "--interval")) { interval_ms = std.fmt.parseInt(i64, args.next() orelse "", 10) catch fatal("--interval needs milliseconds"); } else if (std.mem.startsWith(u8, arg, "-")) { std.debug.print("{s}", .{usage}); return; } else path = arg; } const file = path orelse fatal("no state file given"); const map = memory.mapReadOnly(file); if (map.len < memory.header_len) fatal("not a sketchbook state file"); const magic = map[0..8]; if (!std.mem.eql(u8, magic, &memory.file_magic) and !std.mem.eql(u8, magic, &memory.file_magic_v1)) fatal("not a sketchbook state file"); defer std.posix.munmap(map); var arena: std.heap.ArenaAllocator = .init(init.gpa); defer arena.deinit(); const layout = (try memory.readLayout(arena.allocator(), map)) orelse fatal("state file has no layout yet"); const header: *const memory.Header = @ptrCast(@alignCast(map.ptr)); const live = map[memory.header_len..][0..header.size]; const shown = try arena.allocator().dupe(u8, live); show(file, layout, live); while (watch) { try std.Io.sleep(init.io, .fromMilliseconds(interval_ms), .awake); if (std.mem.eql(u8, shown, live)) continue; @memcpy(shown, live); show(file, layout, live); } } fn show(path: []const u8, layout: memory.Layout, live: []const u8) void { var buf: [4096]u8 = undefined; var w: std.Io.Writer = .fixed(&buf); std.debug.print("{s}: {d} bytes, layout {x:0>16}\n", .{ path, live.len, layout.hash }); for (layout.fields) |leaf| { // Whole arrays are shown by their elements' leaves. if (leaf.whole()) continue; w.end = 0; if (leaf.dims.len == 0) { fields.format(leaf, live, &w) catch {}; } else { // Elements in index order, the first few. var it = leaf.elements(); var n: usize = 0; while (it.next()) |offset| : (n += 1) { if (n == 8) { w.writeAll(" ...") catch {}; break; } if (n > 0) w.writeAll(" ") catch {}; fields.format(leaf.at(offset), live, &w) catch {}; } } var name: [128]u8 = undefined; var nw: std.Io.Writer = .fixed(&name); nw.writeAll(leaf.path) catch {}; for (leaf.dims) |d| nw.print(" [{d}]", .{d.count}) catch {}; std.debug.print(" +0x{x:0>4} {s:<28} {s}\n", .{ leaf.offset, nw.buffered(), w.buffered() }); } } fn fatal(message: []const u8) noreturn { std.debug.print("sketchbook-probe: {s}\n{s}", .{ message, usage }); std.process.exit(2); }