//! 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 { 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); // ... which the library's tests read too. paths.addOptionPath("fib_recording", recording); // From a place on, by instruction: what ran before fib is no moment but its syscalls. const from = b.addRunArtifact(ctx.exe); from.addArgs(&.{ "record", "--from", "fib", "--steps", "64", "-o" }); const from_recording = from.addOutputFileArg("from.ztrace"); from.addArg("--"); from.addArtifactArg(fib); from.addArg("10"); from.expectStdErrMatch(" 64 moments, "); const from_show = b.addRunArtifact(ctx.exe); from_show.addArg("show"); from_show.addFileArg(from_recording); from_show.expectStdOutMatch(" 1 breakpoint\n"); step.dependOn(&from_show.step); // ... and by syscall: the loader's own are passed, not taken for the place. const from_sys = b.addRunArtifact(ctx.exe); from_sys.addArgs(&.{ "record", "--by", "syscall", "--from", "fib", "-o" }); const from_sys_recording = from_sys.addOutputFileArg("from-sys.ztrace"); from_sys.addArg("--"); from_sys.addArtifactArg(fib); from_sys.addArg("10"); from_sys.expectStdErrMatch("exit 0"); const from_sys_show = b.addRunArtifact(ctx.exe); from_sys_show.addArg("show"); from_sys_show.addFileArg(from_sys_recording); from_sys_show.expectStdOutMatch(" 1 breakpoint\n"); step.dependOn(&from_sys_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); }