|
| 1 | +from pythonbpf import bpf, map, section, bpfglobal, compile, struct |
| 2 | +from ctypes import c_void_p, c_int64, c_int32, c_uint64 |
| 3 | +from pythonbpf.maps import HashMap |
| 4 | +from pythonbpf.helper import ktime |
| 5 | + |
| 6 | + |
| 7 | +# NOTE: This is a comprehensive test combining struct, helper, and map features |
| 8 | +# Please note that at line 50, though we have used an absurd expression to test |
| 9 | +# the comiler, it is recommended to used named variables to reduce the amount of |
| 10 | +# scratch space that needs to be allocated. |
| 11 | + |
| 12 | +@bpf |
| 13 | +@struct |
| 14 | +class data_t: |
| 15 | + pid: c_uint64 |
| 16 | + ts: c_uint64 |
| 17 | + |
| 18 | + |
| 19 | +@bpf |
| 20 | +@map |
| 21 | +def last() -> HashMap: |
| 22 | + return HashMap(key=c_uint64, value=c_uint64, max_entries=3) |
| 23 | + |
| 24 | + |
| 25 | +@bpf |
| 26 | +@section("tracepoint/syscalls/sys_enter_execve") |
| 27 | +def hello_world(ctx: c_void_p) -> c_int64: |
| 28 | + dat = data_t() |
| 29 | + dat.pid = 123 |
| 30 | + dat.pid = dat.pid + 1 |
| 31 | + print(f"pid is {dat.pid}") |
| 32 | + tu = 9 |
| 33 | + last.update(0, tu) |
| 34 | + last.update(1, -last.lookup(0)) |
| 35 | + x = last.lookup(0) |
| 36 | + print(f"Map value at index 0: {x}") |
| 37 | + x = x + c_int32(1) |
| 38 | + print(f"x after adding 32-bit 1 is {x}") |
| 39 | + x = ktime() - 121 |
| 40 | + print(f"ktime - 121 is {x}") |
| 41 | + x = last.lookup(0) |
| 42 | + x = x + 1 |
| 43 | + print(f"x is {x}") |
| 44 | + if x == 11: |
| 45 | + jat = data_t() |
| 46 | + jat.ts = 456 |
| 47 | + print(f"Hello, World!, ts is {jat.ts}") |
| 48 | + a = last.lookup(0) |
| 49 | + print(f"a is {a}") |
| 50 | + last.update(0, last.lookup(last.lookup(0)) + |
| 51 | + last.lookup(last.lookup(0)) + last.lookup(last.lookup(0))) |
| 52 | + z = last.lookup(0) |
| 53 | + print(f"new map val at index 0 is {z}") |
| 54 | + else: |
| 55 | + a = last.lookup(0) |
| 56 | + print("Goodbye, World!") |
| 57 | + c = last.lookup(1 - 1) |
| 58 | + print(f"c is {c}") |
| 59 | + return |
| 60 | + |
| 61 | + |
| 62 | +@bpf |
| 63 | +@bpfglobal |
| 64 | +def LICENSE() -> str: |
| 65 | + return "GPL" |
| 66 | + |
| 67 | + |
| 68 | +compile() |
0 commit comments