summaryrefslogtreecommitdiff
path: root/build/debugger.zig
blob: a2611b2b2240a73c83ad12284daf6d97e235658d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 {
    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);
}