summaryrefslogtreecommitdiff
path: root/build/debugger.zig
blob: af6e2e03a6ff888c1422fec46ed593f7a8e00384 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! 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 no_lines = b.addExecutable(.{ .name = "fib-nolines", .root_module = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .sanitize_c = .off }) });
    no_lines.root_module.addCSourceFile(.{ .file = b.path("targets/fib.c"), .flags = &.{ "-O0", "-g0" } });
    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 });
    // The list at -O0, where every local has its place on the stack.
    const o0_mod = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false });
    o0_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"list.c"}, .flags = &.{ "-O0", "-g" } });
    const list_o0 = b.addExecutable(.{ .name = "list-O0", .root_module = o0_mod });
    // Calls through the PLT, bound at their first call (Zig links for binding at load).
    const hello = ctx.cTarget("hello", &.{"hello.c"});
    hello.link_z_lazy = true;
    const clock = ctx.cTarget("clock", &.{"clock.c"});
    // A stack overflow, plain: -O0 and no canary, so it overflows as written.
    const overflow_mod = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false, .stack_protector = false });
    overflow_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"overflow.c"}, .flags = &.{ "-O0", "-g" } });
    const overflow = b.addExecutable(.{ .name = "overflow", .root_module = overflow_mod });
    // The stack's own instructions and the string ones; -O0 with a canary.
    const stack_mod = b.createModule(.{ .target = ctx.target, .optimize = .Debug, .link_libc = true, .omit_frame_pointer = false, .stack_protector = true });
    stack_mod.addCSourceFiles(.{ .root = b.path("targets"), .files = &.{"stack.c"}, .flags = &.{ "-O0", "-g" } });
    const stack = b.addExecutable(.{ .name = "stack", .root_module = stack_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, list_o0, hello, clock, overflow, stack, 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();
    paths.addOptionPath("fib_nolines", no_lines.getEmittedBin());
    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);
    // The overflow built with ASan by the system's compilers and recorded by the command, in the tests.
    paths.addOptionPath("overflow_source", b.path("targets/overflow.c"));
    paths.addOptionPath("stack_source", b.path("targets/stack.c"));
    paths.addOptionPath("ztrace", ctx.exe.getEmittedBin());

    // A call followed down by instruction: through the PLT, the loader's
    // resolver and libc to the kernel, and back; read by the library's tests.
    const descent = b.addRunArtifact(ctx.exe);
    descent.addArgs(&.{ "record", "--by", "insn", "--from", "main", "-o" });
    paths.addOptionPath("hello_recording", descent.addOutputFileArg("hello.ztrace"));
    descent.addArg("--");
    descent.addArtifactArg(hello);
    descent.expectStdErrMatch("exit 0");

    // Two loops by instruction, `reverse` and `list_done`: their blocks.
    const loops = b.addRunArtifact(ctx.exe);
    loops.addArgs(&.{ "record", "--by", "insn", "--from", "reverse", "--steps", "100", "-o" });
    paths.addOptionPath("list_recording", loops.addOutputFileArg("list.ztrace"));
    loops.addArg("--");
    loops.addArtifactArg(list);
    loops.addArg("3");
    loops.expectStdErrMatch("exit 0");

    // The stack's own instructions, the string ones, a canary, by instruction from main to the end.
    const stack_run = b.addRunArtifact(ctx.exe);
    stack_run.addArgs(&.{ "record", "--by", "insn", "--from", "main", "-o" });
    paths.addOptionPath("stack_recording", stack_run.addOutputFileArg("stack.ztrace"));
    stack_run.addArg("--");
    stack_run.addArtifactArg(stack);
    stack_run.expectStdErrMatch("exit 0");

    // Into libc by its PLT stub, syscalls not followed: the libraries'
    // mappings are read when a name is wanted in them, and the callee's
    // first instruction is named in the recording, its caller under it.
    const entry = b.addRunArtifact(ctx.exe);
    entry.addArgs(&.{ "dbg", "-o" });
    _ = entry.addOutputFileArg("entry.ztrace");
    entry.addArg("--");
    entry.addArtifactArg(libc_glibc);
    entry.setStdIn(.{ .bytes = "b main\nr\nsi\nsi\nsi\ncontext\nquit\n" });
    for ([_][]const u8{ " malloc@plt+0\n", "  rip    ", " (libc.so.6!malloc+0)\n", "  #0 ", " libc.so.6!malloc+0", "  #1 ", " main+14" }) |expected| entry.expectStdOutMatch(expected);
    step.dependOn(&entry.step);

    // 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(" moments, ");
    const from_show = b.addRunArtifact(ctx.exe);
    from_show.addArg("show");
    from_show.addFileArg(from_recording);
    from_show.expectStdOutMatch(" 1 breakpoint\n");
    // The 64 are counted from fib, whatever the loader did before.
    from_show.expectStdOutMatch(" 64 step\n");
    step.dependOn(&from_show.step);
    // From main, by instruction, to fib: --until ends the recording there.
    const until = b.addRunArtifact(ctx.exe);
    until.addArgs(&.{ "record", "--from", "main", "--until", "fib", "-o" });
    const until_recording = until.addOutputFileArg("until.ztrace");
    until.addArg("--");
    until.addArtifactArg(fib);
    until.addArg("10");
    until.expectStdErrMatch("ended at fib, as --until asked");
    const until_show = b.addRunArtifact(ctx.exe);
    until_show.addArgs(&.{"show"});
    until_show.addFileArg(until_recording);
    until_show.addArg("-1");
    // WHERE resolves as -b's does: a function's place past its prologue.
    until_show.expectStdOutMatch("rip    ");
    until_show.expectStdOutMatch(" (fib+");
    step.dependOn(&until_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);
}