1{ lib, ... }:
2{
3 name = "bpf";
4 meta.maintainers = with lib.maintainers; [ martinetd ];
5
6 nodes.machine =
7 { pkgs, ... }:
8 {
9 programs.bcc.enable = true;
10 environment.systemPackages = with pkgs; [ bpftrace ];
11 };
12
13 testScript = ''
14 ## bcc
15 # syscount -d 1 stops 1s after probe started so is good for that
16 print(machine.succeed("syscount -d 1"))
17
18 ## bpftrace
19 # list probes
20 machine.succeed("bpftrace -l")
21 # simple BEGIN probe (user probe on bpftrace itself)
22 print(machine.succeed("bpftrace -e 'BEGIN { print(\"ok\\n\"); exit(); }'"))
23 # tracepoint
24 print(machine.succeed("bpftrace -e 'tracepoint:syscalls:sys_enter_* { print(probe); exit() }'"))
25 # kprobe
26 print(machine.succeed("bpftrace -e 'kprobe:schedule { print(probe); exit() }'"))
27 # BTF
28 print(machine.succeed("bpftrace -e 'kprobe:schedule { "
29 " printf(\"tgid: %d\\n\", ((struct task_struct*) curtask)->tgid); exit() "
30 "}'"))
31 # module BTF (bpftrace >= 0.17)
32 # test is currently disabled on aarch64 as kfunc does not work there yet
33 # https://github.com/iovisor/bpftrace/issues/2496
34 print(machine.succeed("uname -m | grep aarch64 || "
35 "bpftrace -e 'kfunc:nft_trans_alloc_gfp { "
36 " printf(\"portid: %d\\n\", args->ctx->portid); "
37 "} BEGIN { exit() }'"))
38 # glibc includes
39 print(machine.succeed("bpftrace -e '#include <errno.h>\n"
40 "BEGIN { printf(\"ok %d\\n\", EINVAL); exit(); }'"))
41 '';
42}