Files
python-bpf/tests/kernel_selftest_equivalent/tracing/autoattach.py
Pragyansh Chaturvedi 7911436996 Tests: Port three kernel BPF selftests
Ports of programs from tools/testing/selftests/bpf/progs/ in the Linux tree,
each naming its upstream original in a header comment.

    tracing/tracepoint_sched_switch.py   test_tracepoint.c
    tracing/get_cgroup_id.py             get_cgroup_id_kern.c
    tracing/autoattach.py                test_autoattach.c

Deliberately a small spike rather than bulk coverage. The three sample different
global-variable shapes, since substituting for globals is what porting the rest
of the corpus will mostly consist of: none at all (the control case), a scalar
read plus a scalar write, and two flags written from two programs on two
different attach points.

They also widen the range of program types under test.
tracepoint/sched/sched_switch and raw_tp/sys_enter were previously unexercised;
@section writes its string straight into the ELF with no allowlist, so that
pass-through had only ever been tested against a handful of types.

Only the BPF half of each selftest is ported -- upstream pairs every program
with a userspace driver in prog_tests/ that loads and asserts, whereas this
framework compiles and verifies but never runs. tests/README.md now says so
explicitly, along with the WORKAROUND(globals) convention marking each map
substituted for a global.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-09-01 12:47:44 +05:30

43 lines
1.1 KiB
Python

# Ported from Linux tools/testing/selftests/bpf/progs/test_autoattach.c
#
# Two programs on different raw tracepoints, each recording that it ran. The
# upstream test asserts both fired after bpf_object__attach_skeleton().
#
# WORKAROUND(globals): upstream uses `bool prog1_called` / `bool prog2_called`.
# PythonBPF has no global variable support yet, so both live in one HashMap
# keyed by program number. Replace with real globals once they land.
from pythonbpf import bpf, map, section, bpfglobal, compile
from pythonbpf.maps import HashMap
from ctypes import c_void_p, c_int64, c_int32, c_uint64
# WORKAROUND(globals): key 1 -> prog1_called, key 2 -> prog2_called
@bpf
@map
def called() -> HashMap:
return HashMap(key=c_int32, value=c_uint64, max_entries=2)
@bpf
@section("raw_tp/sys_enter")
def prog1(ctx: c_void_p) -> c_int64:
called.update(1, 1)
return c_int64(0)
@bpf
@section("raw_tp/sys_exit")
def prog2(ctx: c_void_p) -> c_int64:
called.update(2, 1)
return c_int64(0)
@bpf
@bpfglobal
def LICENSE() -> str:
return "GPL"
compile()