summaryrefslogtreecommitdiff
path: root/build
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 11:49:22 -0300
committerGabriel Schneider <[email protected]>2026-11-10 12:19:06 -0300
commit297943b01e18bb3d1dd41a8d3e220811de25228b (patch)
tree4938e643abada811d3ab28b14c0e31dc94ebda62 /build
parentdb923fa823354270217362db06356f2b1a41315e (diff)
downloadcomputeranim-297943b01e18bb3d1dd41a8d3e220811de25228b.tar.gz
computeranim-297943b01e18bb3d1dd41a8d3e220811de25228b.zip
ztrace probe: BPF on syscalls, scheduling, kprobes, uprobes and USDT
Programs built as instruction arrays for one process (no compiler, no libbpf), writing Events into a BPF ring buffer; a drop counter in a map; task_struct offsets from the kernel's BTF; a USDT probe's arguments read in the kernel at the probe. Without CAP_BPF and CAP_PERFMON it says so; --dry-run builds and lists everything. Tested offline by running the programs in a small BPF machine, and for real by test-kernel-vm, which boots this kernel in QEMU where we are root. ztrace perf: moments are no longer cut per event unless --cut; --max-size, and a full recording stops the target and says so. perf.Sorted merges two backends' events by time. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Diffstat (limited to 'build')
-rw-r--r--build/kernel.zig27
-rw-r--r--build/kernel_vm.sh19
2 files changed, 45 insertions, 1 deletions
diff --git a/build/kernel.zig b/build/kernel.zig
index 48c920f..bbed126 100644
--- a/build/kernel.zig
+++ b/build/kernel.zig
@@ -8,12 +8,14 @@ pub fn add(ctx: Ctx) void {
// 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" } },
- }) |t| {
+ }, &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);
@@ -25,4 +27,27 @@ pub fn add(ctx: Ctx) void {
.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);
}
diff --git a/build/kernel_vm.sh b/build/kernel_vm.sh
new file mode 100644
index 0000000..ac7c576
--- /dev/null
+++ b/build/kernel_vm.sh
@@ -0,0 +1,19 @@
+#!/bin/sh
+# usage: kernel_vm.sh INIT COMMANDS FILE...
+# Boots this machine's kernel in QEMU on an initramfs of the files given
+# (and the C library, for the dynamic ones): INIT is its /init and runs each
+# line of COMMANDS as root. What the console says goes to stdout.
+set -e
+stage=$(mktemp -d)
+trap 'rm -rf "$stage" "$stage.cpio"' EXIT
+mkdir -p "$stage/proc" "$stage/sys" "$stage/dev" "$stage/lib64" "$stage/usr/lib"
+cp "$1" "$stage/init"
+cp "$2" "$stage/commands"
+shift 2
+cp "$@" "$stage/"
+cp /lib64/ld-linux-x86-64.so.2 "$stage/lib64/"
+cp /usr/lib/libc.so.6 "$stage/usr/lib/"
+(cd "$stage" && find . | cpio -o -H newc --quiet) >"$stage.cpio"
+timeout 120 qemu-system-x86_64 -enable-kvm -cpu host -m 512 -display none -serial stdio -no-reboot \
+ -kernel "/usr/lib/modules/$(uname -r)/vmlinuz" -initrd "$stage.cpio" \
+ -append "console=ttyS0 quiet panic=-1"