diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-11 14:43:13 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-11 16:17:33 -0300 |
| commit | 3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6 (patch) | |
| tree | f1bc91879a447f178dfaba39d0df107d01e557bd /anim/build.zig | |
| parent | a72831d5c322078260314c305ddd940e4967947d (diff) | |
| download | computeranim-3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6.tar.gz computeranim-3093eb55ac4a7054f51f06ea2786b5f9c9ec73f6.zip | |
canim: a minimap in each pane; source wherever it can be had, libc's included
From the user. Each pane with more than it shows carries a strip of its
whole column at its right edge, the pane's window outlined and what
happens off it marked there; the corner minimap is gone and the dive
folds each column of the map into its pane's strip. The source panel
follows the pc into any file the build has: glibc's own, from the
distro's debuginfod by an optional step that caches them by build id
(zig build sources; nothing else touches the network), and musl's, from
Zig's lib directory. The syscalls scene shows open64.c on the way down,
and a new scene follows the same call through a Zig-built musl. A
graph's counts and inks are as of the moment shown.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/build.zig')
| -rw-r--r-- | anim/build.zig | 81 |
1 files changed, 69 insertions, 12 deletions
diff --git a/anim/build.zig b/anim/build.zig index 4a2ec76..24ce5e1 100644 --- a/anim/build.zig +++ b/anim/build.zig @@ -16,7 +16,7 @@ const std = @import("std"); const sketchbook = @import("sketchbook"); -const scenes = [_][]const u8{ "title", "fib", "list", "sort", "syscalls", "zoom", "heap", "gallery" }; +const scenes = [_][]const u8{ "title", "fib", "list", "sort", "syscalls", "musl", "zoom", "heap", "gallery" }; pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); @@ -53,6 +53,29 @@ pub fn build(b: *std.Build) void { const agent = tools.namedLazyPath("ztrace-agent"); const musl = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }); + // The debuginfod client's cache, where elfutils and Zig's std look: a + // distribution's library's debug info of the same build id, that the + // recorder names its code and lines by; and where `sources` keeps the + // source files those lines name (<cache>/<build id>/source/<path>). + // Found when a step runs, from its environment (`$DEBUGINFOD_CACHE_PATH`, + // else `$XDG_CACHE_HOME/debuginfod_client`, else `~/.cache/debuginfod_client`), + // never at configure time, whose result zig caches. + const debuginfod_cache = "${DEBUGINFOD_CACHE_PATH:-${XDG_CACHE_HOME:-$HOME/.cache}/debuginfod_client}"; + // The sources a recording's lines name, other than the program's own: + // embedded from the cache and Zig's lib directory (`embed`, every build), + // fetched into the cache (`fetch`, `zig build sources`). + const sources_tool = b.addExecutable(.{ .name = "sources", .root_module = b.createModule(.{ + .root_source_file = b.path("tools/sources.zig"), + .target = b.graph.host, + .optimize = .ReleaseFast, + .imports = &.{.{ .name = "ztrace", .module = tools.module("ztrace") }}, + }) }); + const fetch = b.addRunArtifact(sources_tool); + fetch.addArg("fetch"); + fetch.has_side_effects = true; + b.step("sources", "Fetch the library sources the recordings' lines name into the debuginfod cache (the only step that uses the network)").dependOn(&fetch.step); + var source_modules: std.ArrayList(std.Build.Module.Import) = .empty; + const Recording = struct { name: []const u8, source: []const u8, @@ -97,8 +120,9 @@ pub fn build(b: *std.Build) void { // By instruction from main, through the PLT and libc's wrappers to // the kernel and back: the descent; to main's return. .{ .name = "hello", .source = "hello.c", .by = "insn", .from = "main", .steps = "132", .glibc = true, .reads = "note" }, - // A line a stop, malloc and free run through; glibc's heap at each. - .{ .name = "heap", .source = "heap.c", .by = "line", .from = "main", .glibc = true, .heap = true, .watch = &.{ "0x405000,1024", "head" }, .break_call = .{ "pop", "free" } }, + // A line a stop, malloc and free run through (through the PLT); glibc's heap at each; + // to main's return (libc's lines, named now, would take it on through `exit`). + .{ .name = "heap", .source = "heap.c", .by = "line", .from = "main", .steps = "46", .glibc = true, .heap = true, .watch = &.{ "0x405000,1024", "head" }, .break_call = .{ "pop", "free" } }, // By instruction, as `steps` is: the registers are the program's // own, so they strike and link. Only the variables the scenes name // are told by the instrumentation (each a call into its runtime, @@ -106,6 +130,9 @@ pub fn build(b: *std.Build) void { // (the agent's and the runtime's first calls) run through. .{ .name = "sort", .source = "sort.c", .by = "insn", .instr = "vars", .only = "sort,a", .from = "main" }, .{ .name = "list", .source = "list.c", .by = "insn", .instr = "vars", .only = "push,reverse,head", .from = "reverse", .alloc = true }, + // `hello` again, built by Zig against musl, static: no PLT; musl's + // own source (`open.c`, `syscall_cp`) on the way down to the kernel. + .{ .name = "musl", .source = "hello.c", .by = "insn", .from = "main", .steps = "166", .reads = "note" }, }) |r| { const source = b.path(b.fmt("tools/{s}", .{r.source})); var program: std.Build.LazyPath = undefined; @@ -139,16 +166,26 @@ pub fn build(b: *std.Build) void { const where = b.addWriteFiles(); _ = where.addCopyFile(program, r.name); if (r.reads) |name| _ = where.addCopyFile(b.path(b.fmt("tools/{s}", .{name})), name); + // A dynamic program's libraries are named by their debug info in + // the debuginfod cache: the recorder finds it by DEBUGINFOD_CACHE_PATH, + // which the program inherits, so it is a link beside the program + // (`debuginfod`), a name that says nothing of the machine. + const dynamic = r.glibc or r.instr != null; const run = if (r.break_call) |call| blk: { // The call's address, from the program as built, then the recorder with a breakpoint there. const sh = b.addSystemCommand(&.{ "sh", "-c", b.fmt( \\p=$0 z=$1; shift + \\c={s} \\at=$(objdump -d --no-show-raw-insn "$p" | awk '/<{s}>:/ {{ f = 1 }} f && /call.*<{s}@plt>/ {{ sub(":", "", $1); print "0x" $1; exit }}') \\[ -n "$at" ] || {{ echo "no call of {s} in {s}" >&2; exit 1; }} - \\exec env -i setarch -R "$z" record -b "*$at" "$@" - , .{ call[0], call[1], call[1], call[0] }) }); + \\ln -sfn "$c" debuginfod + \\exec env -i DEBUGINFOD_CACHE_PATH=debuginfod setarch -R "$z" record -b "*$at" "$@" + , .{ debuginfod_cache, call[0], call[1], call[1], call[0] }) }); sh.addFileArg(program); break :blk sh; + } else if (dynamic) blk: { + const sh = b.addSystemCommand(&.{ "sh", "-c", "ln -sfn \"" ++ debuginfod_cache ++ "\" debuginfod && exec env -i DEBUGINFOD_CACHE_PATH=debuginfod setarch -R \"$0\" \"$@\"" }); + break :blk sh; } else b.addSystemCommand(&.{ "env", "-i", "setarch", "-R" }); run.addArtifactArg(ztrace); // More round each stop than the working frame shows: the map of @@ -174,12 +211,16 @@ pub fn build(b: *std.Build) void { run.expectExitCode(0); run.expectStdErrMatch("exit 0"); - // Into the scene's own binary: the recording's bytes and the source it ran. + // Into the scene's own binary: the recording's bytes, the source it + // ran, and the other sources its lines name that the build has. const file = b.fmt("{s}.ztrace", .{r.name}); record_step.dependOn(&b.addInstallFile(trace, b.fmt("recordings/{s}", .{file})).step); _ = embedded.addCopyFile(trace, file); _ = embedded.addCopyFile(source, r.source); - table.print(b.allocator, "pub const {s}: Program = .{{ .trace = @embedFile(\"{s}\"), .file = \"{s}\", .source = @embedFile(\"{s}\") }};\n", .{ r.name, file, r.source, r.source }) catch @panic("OOM"); + fetch.addFileArg(trace); + const sources = embedSources(b, sources_tool, r.name, trace, r.source); + source_modules.append(b.allocator, .{ .name = b.fmt("{s}-sources", .{r.name}), .module = sources }) catch @panic("OOM"); + table.print(b.allocator, "pub const {s}: Program = .{{ .trace = @embedFile(\"{s}\"), .file = \"{s}\", .source = @embedFile(\"{s}\"), .sources = &@import(\"{s}-sources\").list }};\n", .{ r.name, file, r.source, r.source, r.name }) catch @panic("OOM"); } // SQLite, the next rung: its amalgamation is not ours to copy, so the // scene is made only where it is. Built as the bench builds it (-O2, @@ -211,7 +252,8 @@ pub fn build(b: *std.Build) void { \\trap 'rm -rf "$d"' EXIT \\cp "$program" "$d/sqlite" \\cd "$d" - \\env -i setarch -R "$ztrace" record --by break -b sqlite3_step -b sqlite3BtreeInsert -b unixWrite -b "*$back" --stack 1024 --around 256 --disasm 69 -s 4096 -o "$out" -- ./sqlite + \\ln -s "${DEBUGINFOD_CACHE_PATH:-${XDG_CACHE_HOME:-$HOME/.cache}/debuginfod_client}" debuginfod + \\env -i DEBUGINFOD_CACHE_PATH=debuginfod setarch -R "$ztrace" record --by break -b sqlite3_step -b sqlite3BtreeInsert -b unixWrite -b "*$back" --stack 1024 --around 256 --disasm 69 -s 4096 -o "$out" -- ./sqlite }); run.addArtifactArg(ztrace); run.addFileArg(program); @@ -220,7 +262,9 @@ pub fn build(b: *std.Build) void { record_step.dependOn(&b.addInstallFile(trace, "recordings/sqlite.ztrace").step); _ = embedded.addCopyFile(trace, "sqlite.ztrace"); _ = embedded.addCopyFile(.{ .cwd_relative = sqlite_c }, "sqlite3.c"); - table.print(b.allocator, "pub const sqlite: Program = .{{ .trace = @embedFile(\"sqlite.ztrace\"), .file = \"sqlite3.c\", .source = @embedFile(\"sqlite3.c\") }};\n", .{}) catch @panic("OOM"); + fetch.addFileArg(trace); + source_modules.append(b.allocator, .{ .name = "sqlite-sources", .module = embedSources(b, sources_tool, "sqlite", trace, "sqlite3.c") }) catch @panic("OOM"); + table.print(b.allocator, "pub const sqlite: Program = .{{ .trace = @embedFile(\"sqlite.ztrace\"), .file = \"sqlite3.c\", .source = @embedFile(\"sqlite3.c\"), .sources = &@import(\"sqlite-sources\").list }};\n", .{}) catch @panic("OOM"); } // The gallery's own: a recording of the list kept in test/gallery, so its @@ -233,10 +277,10 @@ pub fn build(b: *std.Build) void { table.print(b.allocator, "pub const gallery_{s}: Program = .{{ .trace = @embedFile(\"gallery-{s}.ztrace\"), .file = \"{s}\", .source = @embedFile(\"{s}\") }};\n", .{ kept[0], kept[0], kept[1], kept[1] }) catch @panic("OOM"); } const recordings = b.createModule(.{ .root_source_file = embedded.add("recordings.zig", b.fmt( - \\//! Generated by canim's build.zig: the recordings and the sources they ran. - \\pub const Program = struct {{ trace: []const u8, file: []const u8, source: []const u8 }}; + \\//! Generated by canim's build.zig: the recordings, the sources they ran, and the other sources their lines name that the build found (path, text). + \\pub const Program = struct {{ trace: []const u8, file: []const u8, source: []const u8, sources: []const [2][]const u8 = &.{{}} }}; \\{s} - , .{table.items})) }); + , .{table.items})), .imports = source_modules.items }); // ---- Tests: the library's own, then on the real recordings. const test_step = b.step("test", "Unit tests and the scripted runs"); @@ -329,3 +373,16 @@ pub fn build(b: *std.Build) void { } } } + +/// The sources the lines of recording `name` (`trace`) name (but `own`, the program's +/// file) that this machine has, as a module: `list`, each path and text. +/// It reads the debuginfod cache, which the build does not track: run every build. +fn embedSources(b: *std.Build, tool: *std.Build.Step.Compile, name: []const u8, trace: std.Build.LazyPath, own: []const u8) *std.Build.Module { + const run = b.addRunArtifact(tool); + run.addArg("embed"); + run.addFileArg(trace); + run.addArgs(&.{ own, b.graph.zig_exe }); + const dir = run.addOutputDirectoryArg(b.fmt("{s}-sources", .{name})); + run.has_side_effects = true; + return b.createModule(.{ .root_source_file = dir.path(b, "sources.zig") }); +} |
