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
|
//! 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);
// ... and the library's own tests of this area: what needs no target.
step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = ctx.ztrace, .filters = &.{ "perf.test", "usdt.test", "bpf.test" } })).step);
// The static probes without root: the debugger stops at each for as
// long as it takes to record it. start, four ticks, done.
const probed = b.addRunArtifact(ctx.exe);
probed.addArgs(&.{ "record", "--by", "break", "-b", "usdt:ztrace:*", "-o" });
const probes = probed.addOutputFileArg("usdt.ztrace");
probed.addArg("--");
probed.addArtifactArg(targets[2]);
probed.addArg("4");
probed.expectStdErrMatch("exit 0");
const shown = b.addRunArtifact(ctx.exe);
shown.addArg("show");
shown.addFileArg(probes);
shown.expectStdOutMatch("6 probe");
step.dependOn(&shown.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=f166cdb822920ddd 3eb ", " a=a02712eb14a9b985 3ea 7d1 ", " a=ff4909df02928d94 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);
}
|