summaryrefslogtreecommitdiff
path: root/targets/kernel_vminit.zig
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 /targets/kernel_vminit.zig
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 'targets/kernel_vminit.zig')
-rw-r--r--targets/kernel_vminit.zig44
1 files changed, 44 insertions, 0 deletions
diff --git a/targets/kernel_vminit.zig b/targets/kernel_vminit.zig
new file mode 100644
index 0000000..a539689
--- /dev/null
+++ b/targets/kernel_vminit.zig
@@ -0,0 +1,44 @@
+//! /init of the virtual machine `zig build test-kernel-vm` boots: this
+//! machine's kernel, where we are root and BPF may be loaded. Mounts /proc,
+//! /sys and /dev, runs each line of /commands, powers off.
+const std = @import("std");
+const linux = std.os.linux;
+
+fn say(text: []const u8) void {
+ _ = linux.write(1, text.ptr, text.len);
+}
+
+pub fn main() void {
+ _ = linux.mount("proc", "/proc", "proc", 0, 0);
+ _ = linux.mount("sysfs", "/sys", "sysfs", 0, 0);
+ _ = linux.mount("devtmpfs", "/dev", "devtmpfs", 0, 0);
+ var commands: [4096]u8 = undefined;
+ const fd = linux.open("/commands", .{}, 0);
+ const len = linux.read(@intCast(fd), &commands, commands.len);
+ if (linux.errno(len) != .SUCCESS) say("init: no /commands\n");
+ var lines = std.mem.tokenizeScalar(u8, commands[0..if (linux.errno(len) == .SUCCESS) len else 0], '\n');
+ while (lines.next()) |line| {
+ say("$ ");
+ say(line);
+ say("\n");
+ // The words, each ended by a 0, in a copy.
+ var text: [512]u8 = @splat(0);
+ @memcpy(text[0..line.len], line);
+ var argv: [32:null]?[*:0]const u8 = @splat(null);
+ var words = std.mem.tokenizeScalar(u8, line, ' ');
+ var count: usize = 0;
+ while (words.next()) |word| : (count += 1) {
+ const at = word.ptr - line.ptr;
+ text[at + word.len] = 0;
+ argv[count] = @ptrCast(&text[at]);
+ }
+ const pid = linux.fork();
+ if (pid == 0) {
+ _ = linux.execve(argv[0].?, &argv, &[_:null]?[*:0]const u8{});
+ linux.exit_group(127);
+ }
+ var status: u32 = 0;
+ _ = linux.waitpid(@intCast(pid), @ptrCast(&status), 0);
+ }
+ _ = linux.reboot(.MAGIC1, .MAGIC2, .POWER_OFF, null);
+}