mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2026-12-06 18:08:36 +00:00
Five passing tests: read in condition and print; the get_cgroup_id_kern.c read-one-write-one shape; the counter += 1 idiom; two programs sharing global state (the test_autoattach.c shape); and mixed .bss/.data placement with mixed widths and signedness. Three strict expected failures documenting deliberate errors: assignment without the global statement (shadowing), a global statement naming a non-global, and a non-integer global type. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01L1PX8EuP9C3o3veWGA84RF
32 lines
636 B
Python
32 lines
636 B
Python
# The canonical selftest idiom: a per-object event counter. `counter += 1`
|
|
# desugars to load/add/store on the global; the local augassign checks the
|
|
# desugaring path for stack variables too.
|
|
from pythonbpf import bpf, section, bpfglobal, compile
|
|
from ctypes import c_void_p, c_int64, c_uint64
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def counter() -> c_uint64:
|
|
return c_uint64(0)
|
|
|
|
|
|
@bpf
|
|
@section("tracepoint/raw_syscalls/sys_enter")
|
|
def tick(ctx: c_void_p) -> c_int64:
|
|
global counter
|
|
counter += 1
|
|
x = 5
|
|
x += 2
|
|
print(f"count {counter} x {x}")
|
|
return c_int64(0)
|
|
|
|
|
|
@bpf
|
|
@bpfglobal
|
|
def LICENSE() -> str:
|
|
return "GPL"
|
|
|
|
|
|
compile()
|