summaryrefslogtreecommitdiff
path: root/build/kernel.zig
blob: bbed126f621a41a00366e6de970a8036e3936ead (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
//! The kernel area: perf events, USDT, BPF. Its targets, and its tests on them.
const std = @import("std");
const Ctx = @import("../build.zig").Ctx;

pub fn add(ctx: Ctx) void {
    const b = ctx.b;
    const step = ctx.areaTest("test-kernel", "Perf events on real targets, USDT notes against readelf, BPF programs offline");

    // The tests run these: they get the paths as `@import("targets")`.
    const paths = b.addOptions();
    var targets: [3]*std.Build.Step.Compile = undefined;
    inline for (.{
        .{ "spin", &.{"kernel_spin.c"} },
        .{ "touch", &.{"kernel_touch.c"} },
        .{ "usdt", &.{ "kernel_usdt.c", "kernel_usdt_tick.c" } },
    }, &targets) |t, *target| {
        const exe = ctx.cTarget("kernel_" ++ t[0], t[1]);
        target.* = exe;
        paths.addOptionPath(t[0], exe.getEmittedBin());
        // ... and `zig build` leaves them in zig-out/targets to try things on.
        b.getInstallStep().dependOn(&b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "targets" } } }).step);
    }
    const tests = b.addTest(.{ .root_module = b.createModule(.{
        .root_source_file = b.path("src/trace/kernel_test.zig"),
        .target = ctx.target,
        .optimize = ctx.optimize,
        .imports = &.{ .{ .name = "ztrace", .module = ctx.ztrace }, .{ .name = "targets", .module = paths.createModule() } },
    }) });
    step.dependOn(&b.addRunArtifact(tests).step);

    // The BPF side for real: this machine's kernel booted in QEMU, where we
    // are root. Not part of `test`: it wants qemu, /dev/kvm, cpio and a
    // kernel image a user can read.
    const vm = b.addSystemCommand(&.{"sh"});
    vm.addFileArg(b.path("build/kernel_vm.sh"));
    vm.addArtifactArg(ctx.zigTarget("kernel_vminit", "targets/kernel_vminit.zig"));
    vm.addFileArg(b.addWriteFiles().add("commands",
        \\/ztrace probe -o /touch.ztrace --syscalls --sched --kprobe do_sys_openat2 --uprobe step --uprobe /usr/lib/libc.so.6:malloc -- /kernel_touch 4 3
        \\/ztrace dump /touch.ztrace
        \\/ztrace probe -o /usdt.ztrace --usdt ztrace:start --usdt ztrace:tick --usdt ztrace:done -- /kernel_usdt 4
        \\/ztrace dump /usdt.ztrace
        \\
    ));
    vm.addArtifactArg(ctx.exe);
    for (targets[1..]) |target| vm.addArtifactArg(target);
    // Nothing lost; a syscall (clock_nanosleep) and a switch; malloc(1003)
    // in libc; step(1002, 2001) in the thread; tick(3, 14) with its semaphore.
    for ([_][]const u8{
        "(0 lost)",                 " syscall_enter ",              " aux=230 ",                " sched ",
        " a=7166cdb822920ddd 3eb ", " a=a02712eb14a9b985 3ea 7d1 ", " a=7f4909df02928d94 3 e ",
    }) |expected| vm.expectStdOutMatch(expected);
    b.step("test-kernel-vm", "Run the BPF programs as root in a QEMU VM of this kernel").dependOn(&vm.step);
}