summaryrefslogtreecommitdiff
path: root/anim/tools/sources.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/tools/sources.zig')
-rw-r--r--anim/tools/sources.zig164
1 files changed, 164 insertions, 0 deletions
diff --git a/anim/tools/sources.zig b/anim/tools/sources.zig
new file mode 100644
index 0000000..8c8026e
--- /dev/null
+++ b/anim/tools/sources.zig
@@ -0,0 +1,164 @@
+//! The sources a recording's lines name, other than the program's own, for
+//! the source panel to follow the pc into (`Recording.sources`):
+//!
+//! sources embed TRACE OWN ZIG OUT every build, no network
+//! sources fetch TRACE... `zig build sources`
+//!
+//! `embed` copies those it can read into OUT, a file each, and writes
+//! OUT/sources.zig (`list`: each path, as the debug info names it, and its
+//! text) for the scene to embed: Zig's musl from the lib directory of ZIG
+//! (`zig env`: a static musl build's lines name it there), the rest
+//! from the debuginfod client's cache by the build id of the module the pc
+//! was in: <cache>/<build id>/source/<path> (the cache: `$DEBUGINFOD_CACHE_PATH`,
+//! else `$XDG_CACHE_HOME/debuginfod_client`, else `~/.cache/debuginfod_client`). What is in neither is left out: the
+//! panel then waits a frame up, as it did before there were sources.
+//! `fetch` asks the debuginfod server (the first of `$DEBUGINFOD_URLS`,
+//! else of `/etc/debuginfod/*.urls`) for those the cache lacks, of
+//! the modules whose names came from a separate debug file (a
+//! distribution's library: never the program's own): `URL/buildid/<build
+//! id>/source<path>`, with curl, which sends nothing but its defaults.
+//! The only step that uses the network.
+const std = @import("std");
+const zt = @import("ztrace");
+
+/// A file a moment's line names; the build id (hex) of the module the pc
+/// was in, and whether that module's names came from a separate debug file.
+const Named = struct { path: []const u8, id: []const u8, separate: bool };
+
+/// Each file the recording's lines name, but the program's own (`own`, its bare name).
+fn named(a: std.mem.Allocator, io: std.Io, trace_path: []const u8, own: []const u8) ![]Named {
+ var trace = try zt.Trace.open(a, io, trace_path);
+ defer trace.close();
+ var out: std.ArrayList(Named) = .empty;
+ var tid: u32 = 0;
+ for (0..trace.len) |i| {
+ const m = try trace.moment(i);
+ if (tid == 0) {
+ const threads = try m.threads(a);
+ if (threads.len == 0) continue;
+ tid = threads[0];
+ }
+ const line = (try m.line(tid)) orelse continue;
+ if (line.line == 0) continue;
+ const path = trace.strings.get(line.file) orelse continue;
+ if (std.mem.eql(u8, std.fs.path.basename(path), own)) continue;
+ if (for (out.items) |n| {
+ if (std.mem.eql(u8, n.path, path)) break true;
+ } else false) continue;
+ const regs = (try m.regs(tid)) orelse continue;
+ var module: []const u8 = "";
+ var buf: [4096]u8 = undefined;
+ _ = try m.mapping(regs.rip, &module, &buf);
+ const mod = zt.telescope.module(m, module);
+ try out.append(a, .{
+ .path = try a.dupe(u8, path),
+ .id = if (mod) |it| try std.fmt.allocPrint(a, "{x}", .{it.build_id}) else "",
+ .separate = if (mod) |it| it.debug == .separate else false,
+ });
+ }
+ return out.items;
+}
+
+/// Where a file of musl's is in Zig's lib directory, by its path from `libc/musl/`.
+fn musl(a: std.mem.Allocator, path: []const u8, zig_lib: []const u8) !?[]const u8 {
+ const k = std.mem.indexOf(u8, path, "/libc/musl/") orelse return null;
+ return try std.fs.path.join(a, &.{ zig_lib, path[k + 1 ..] });
+}
+
+/// Where the cache keeps a module's source file: `<cache>/<build id>/source/<path>`.
+fn cached(a: std.mem.Allocator, cache: []const u8, n: Named) ![]const u8 {
+ return std.fs.path.join(a, &.{ cache, n.id, "source", n.path });
+}
+
+/// Zig's lib directory, as `zig env` says it.
+fn zigLib(a: std.mem.Allocator, io: std.Io, zig: []const u8) ![]const u8 {
+ const env = try std.process.run(a, io, .{ .argv = &.{ zig, "env" } });
+ const key = ".lib_dir = \"";
+ const at = (std.mem.indexOf(u8, env.stdout, key) orelse return error.NoLibDir) + key.len;
+ return env.stdout[at .. std.mem.indexOfScalarPos(u8, env.stdout, at, '"') orelse return error.NoLibDir];
+}
+
+fn embed(a: std.mem.Allocator, io: std.Io, trace: []const u8, own: []const u8, zig: []const u8, cache: []const u8, out_dir: []const u8) !void {
+ const cwd = std.Io.Dir.cwd();
+ const zig_lib = try zigLib(a, io, zig);
+ var dir = try cwd.createDirPathOpen(io, out_dir, .{});
+ defer dir.close(io);
+ var list: std.ArrayList(u8) = .empty;
+ try list.appendSlice(a, "//! Generated by tools/sources.zig: the files the recording's lines name that the build found.\npub const list = [_][2][]const u8{\n");
+ var k: usize = 0;
+ for (try named(a, io, trace, own)) |n| {
+ const from = (try musl(a, n.path, zig_lib)) orelse if (n.id.len > 0) try cached(a, cache, n) else continue;
+ const text = cwd.readFileAlloc(io, from, a, .limited(1 << 22)) catch continue;
+ // A path a Zig string literal holds as it is: nothing to escape.
+ if (std.mem.indexOfAny(u8, n.path, "\"\\\n") != null) continue;
+ const name = try std.fmt.allocPrint(a, "{d}", .{k});
+ try dir.writeFile(io, .{ .sub_path = name, .data = text });
+ try list.print(a, " .{{ \"{s}\", @embedFile(\"{s}\") }},\n", .{ n.path, name });
+ k += 1;
+ }
+ try list.appendSlice(a, "};\n");
+ try dir.writeFile(io, .{ .sub_path = "sources.zig", .data = list.items });
+}
+
+fn fetch(a: std.mem.Allocator, io: std.Io, url: []const u8, cache: []const u8, traces: []const []const u8) !void {
+ const cwd = std.Io.Dir.cwd();
+ const base = std.mem.trimEnd(u8, url, "/");
+ for (traces) |trace| for (try named(a, io, trace, "")) |n| {
+ if (!n.separate or n.id.len == 0 or !std.fs.path.isAbsolute(n.path)) continue;
+ const to = try cached(a, cache, n);
+ if (cwd.access(io, to, .{})) |_| continue else |_| {}
+ const part = try std.fmt.allocPrint(a, "{s}.part", .{to});
+ const from = try std.fmt.allocPrint(a, "{s}/buildid/{s}/source{s}", .{ base, n.id, n.path });
+ const got = try std.process.run(a, io, .{ .argv = &.{ "curl", "-fsS", "--create-dirs", "-o", part, from } });
+ if (got.term == .exited and got.term.exited == 0) {
+ try cwd.rename(part, cwd, to, io);
+ std.debug.print("sources: {s} {s}\n", .{ n.id, n.path });
+ } else {
+ cwd.deleteFile(io, part) catch {};
+ std.debug.print("sources: not served: {s} {s}: {s}", .{ n.id, n.path, got.stderr });
+ }
+ };
+}
+
+/// The debuginfod client's cache, as elfutils and Zig's std find it.
+fn cacheDir(a: std.mem.Allocator, env: *const std.process.Environ.Map) ![]const u8 {
+ if (env.get("DEBUGINFOD_CACHE_PATH")) |p| return p;
+ if (env.get("XDG_CACHE_HOME")) |x| return std.fs.path.join(a, &.{ x, "debuginfod_client" });
+ return std.fs.path.join(a, &.{ env.get("HOME") orelse "/", ".cache", "debuginfod_client" });
+}
+
+/// The debuginfod server: the first of `$DEBUGINFOD_URLS`, else of `/etc/debuginfod/*.urls`.
+fn server(a: std.mem.Allocator, io: std.Io, env: *const std.process.Environ.Map) ?[]const u8 {
+ const first = struct {
+ fn of(text: []const u8) ?[]const u8 {
+ var words = std.mem.tokenizeAny(u8, text, " \t\n");
+ return words.next();
+ }
+ }.of;
+ if (env.get("DEBUGINFOD_URLS")) |urls| if (first(urls)) |url| return url;
+ var dir = std.Io.Dir.openDirAbsolute(io, "/etc/debuginfod", .{ .iterate = true }) catch return null;
+ defer dir.close(io);
+ var it = dir.iterate();
+ while (it.next(io) catch null) |entry| {
+ if (!std.mem.endsWith(u8, entry.name, ".urls")) continue;
+ const text = dir.readFileAlloc(io, entry.name, a, .limited(1 << 16)) catch continue;
+ if (first(text)) |url| return url;
+ }
+ return null;
+}
+
+pub fn main(init: std.process.Init) !void {
+ const a = init.arena.allocator();
+ const args = try init.minimal.args.toSlice(a);
+ const cache = try cacheDir(a, init.environ_map);
+ if (args.len >= 6 and std.mem.eql(u8, args[1], "embed")) return embed(a, init.io, args[2], args[3], args[4], cache, args[5]);
+ if (args.len >= 2 and std.mem.eql(u8, args[1], "fetch")) {
+ const url = server(a, init.io, init.environ_map) orelse {
+ std.debug.print("sources: no debuginfod server: set DEBUGINFOD_URLS or /etc/debuginfod/*.urls\n", .{});
+ return error.NoServer;
+ };
+ return fetch(a, init.io, url, cache, args[2..]);
+ }
+ std.debug.print("usage: sources embed TRACE OWN ZIG OUT | sources fetch TRACE...\n", .{});
+ return error.Usage;
+}