diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 11:58:00 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 11:58:53 -0300 |
| commit | 8beab959b633e6641d07aba053a55e6e8e61eb00 (patch) | |
| tree | 0e5a91c0a00e121fe4094a333438ee90ccc5fdda /build/debugger.zig | |
| parent | 3fdb67c479dcc1ad0f1e51832bc086622692249c (diff) | |
| download | computeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.tar.gz computeranim-8beab959b633e6641d07aba053a55e6e8e61eb00.zip | |
targets, test-debugger, bench-debugger
A linked list, a graph search in Zig, the same C under glibc and musl, two
threads. The tests drive the debugger over them: breakpoint counts, the
lines a step walks (as gdb), a list snapshotted by a hook, a watch's
words, the syscalls that tell glibc from musl, threads, attach, and
scripted dbg and record sessions. The bench measures steps, stops and
memory access from bare ptrace up to a recording.
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'build/debugger.zig')
| -rw-r--r-- | build/debugger.zig | 128 |
1 files changed, 127 insertions, 1 deletions
diff --git a/build/debugger.zig b/build/debugger.zig index d3c7eb3..a2611b2 100644 --- a/build/debugger.zig +++ b/build/debugger.zig @@ -1,5 +1,131 @@ +//! The debugger's part of the build: the small programs it is tried on, and +//! its tests against them. +const std = @import("std"); const Ctx = @import("../build.zig").Ctx; pub fn add(ctx: Ctx) void { - _ = ctx; + const b = ctx.b; + + // Position-independent, so the load bias is exercised. + const fib = ctx.cTarget("fib", &.{"fib.c"}); + fib.pie = true; + const list = ctx.cTarget("list", &.{"list.c"}); + const threads = ctx.cTarget("threads", &.{"threads.c"}); + const libc_glibc = ctx.cTarget("libc-glibc", &.{"libc.c"}); + // The same source against musl, linked statically. + const musl = b.resolveTargetQuery(.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl }); + const musl_mod = b.createModule(.{ .target = musl, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false }); + musl_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"libc.c"}, .flags = &.{ "-O1", "-g", "-fno-omit-frame-pointer" } }); + const libc_musl = b.addExecutable(.{ .name = "libc-musl", .root_module = musl_mod }); + // Zig, no libc: always a debug build, so its lines are the source's. + const graph = b.addExecutable(.{ .name = "graph", .root_module = b.createModule(.{ + .root_source_file = b.path("targets/graph.zig"), + .target = ctx.target, + .optimize = .Debug, + }) }); + + const targets = [_]*std.Build.Step.Compile{ fib, list, threads, libc_glibc, libc_musl, graph }; + for (targets) |t| b.getInstallStep().dependOn(&b.addInstallArtifact(t, .{ .dest_dir = .{ .override = .{ .custom = "targets" } } }).step); + + // The compiler's own checks (UBSan, on by default in a debug build) are + // not the program: without them its code is what its source says. + for (targets) |t| t.root_module.sanitize_c = .off; + + const step = ctx.areaTest("test-debugger", "The debugger against real programs"); + + // The library, driven from Zig: the targets' paths come as options. + const paths = b.addOptions(); + for (targets) |t| paths.addOptionPath(b.dupe(std.mem.replaceOwned(u8, b.allocator, t.name, "-", "_") catch @panic("OOM")), t.getEmittedBin()); + const lib_tests = b.addTest(.{ .root_module = b.createModule(.{ + .root_source_file = b.path("src/trace/debugger_test.zig"), + .target = ctx.target, + .optimize = ctx.optimize, + .imports = &.{ .{ .name = "ztrace", .module = ctx.ztrace }, .{ .name = "targets", .module = paths.createModule() } }, + }) }); + step.dependOn(&b.addRunArtifact(lib_tests).step); + + // The commands. A scripted session: break, look, step, stop at syscalls, run out, go back. + const dbg = b.addRunArtifact(ctx.exe); + dbg.addArgs(&.{ "dbg", "-o" }); + _ = dbg.addOutputFileArg("dbg.ztrace"); + dbg.addArg("--"); + dbg.addArtifactArg(fib); + dbg.addArg("1"); + dbg.setStdIn(.{ .bytes = "b fib\nr\nbt\nx $rsp 16\nsi\ns\nsyscalls write\nc\nc\nsyscalls off\nc\ngoto 3\nregs\nbt\nback 2\nforward\nnope\nquit\n" }); + for ([_][]const u8{ + "breakpoint 1 at ", + "[1] breakpoint 1 at ", + " fib+4 (fib.c:4)\n#1 ", + " main+42 (fib.c:10)\n#2 ", + "[2] step at ", + // A source line on from line 4 is line 5. + "[3] step at ", + " (fib.c:5)\n(ztrace) syscalls write", + // The result is written when the program ends. + "] syscall write(1, \"fib(1) = 1\\n\", 11) at ", + "] syscall write = 11 at ", + "] exited 0\n(ztrace) goto 3", + // Back in the recording, at the step to line 5: the registers and the stack of then. + "goto 3\n[3] step at ", + " rip ", + " fib+12 (fib.c:5)\n#1 ", + "(ztrace) back 2\n[1] breakpoint 1 at ", + "(ztrace) forward\n[2] step at ", + "error: UnknownCommand", + "dbg.ztrace: ", + }) |expected| dbg.expectStdOutMatch(expected); + step.dependOn(&dbg.step); + + // A recording of breakpoints only, and what `show` counts in it: fib(10) is 89 calls. + const record = b.addRunArtifact(ctx.exe); + record.addArgs(&.{ "record", "--by", "break", "-b", "fib", "-o" }); + const recording = record.addOutputFileArg("fib.ztrace"); + record.addArg("--"); + record.addArtifactArg(fib); + record.addArg("10"); + record.expectStdErrMatch("exit 0"); + const show = b.addRunArtifact(ctx.exe); + show.addArg("show"); + show.addFileArg(recording); + show.expectStdOutMatch(" 89 breakpoint\n"); + step.dependOn(&show.step); + + // A source line a moment, a watched global, to the exit code. + const lines = b.addRunArtifact(ctx.exe); + lines.addArgs(&.{ "record", "--by", "line", "-w", "dist", "-w", "queue,8", "-o" }); + _ = lines.addOutputFileArg("graph.ztrace"); + lines.addArg("--"); + lines.addArtifactArg(graph); + lines.expectStdErrMatch("exit 18"); + step.dependOn(&lines.step); + + // A syscall a moment, printed the way strace does. + const strace = b.addRunArtifact(ctx.exe); + strace.addArgs(&.{ "record", "--syscalls", "--strace", "-o" }); + _ = strace.addOutputFileArg("musl.ztrace"); + strace.addArg("--"); + strace.addArtifactArg(libc_musl); + strace.expectStdErrMatch("ioctl(1, 0x5413, "); + strace.expectStdErrMatch(") = -25 ENOTTY\nwritev(1, "); + strace.expectStdErrMatch("exit_group(0) = ?\n"); + strace.expectStdErrMatch("exit 0"); + step.dependOn(&strace.step); + + // The numbers: always optimized, whatever the build is. + const fast = b.createModule(.{ + .root_source_file = b.path("src/trace/root.zig"), + .target = ctx.target, + .optimize = .ReleaseFast, + .imports = &.{.{ .name = "xitdb", .module = ctx.ztrace.import_table.get("xitdb").? }}, + }); + const bench = b.addRunArtifact(b.addExecutable(.{ .name = "bench-debugger", .root_module = b.createModule(.{ + .root_source_file = b.path("bench/debugger.zig"), + .target = ctx.target, + .optimize = .ReleaseFast, + .imports = &.{.{ .name = "ztrace", .module = fast }}, + }) })); + bench.addArtifactArg(fib); + bench.addArtifactArg(graph); + bench.addArg("/usr/bin/dd"); + b.step("bench-debugger", "Measure steps, syscall stops and memory access").dependOn(&bench.step); } |
