mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Update imports to use direct namespace imports
WARNING: OLD STYLE IMPORTS DO NOT WORK The old style of importing the full module and using pb.* prefixes has been replaced with direct imports of the needed names. This makes the code more explicit about what is being used and removes the unnecessary pb prefix.
This commit is contained in:
20
README.md
20
README.md
@ -13,25 +13,25 @@ This is an LLVM IR generator for eBPF programs in Python. We use llvmlite to gen
|
|||||||
## Usage
|
## Usage
|
||||||
```python
|
```python
|
||||||
# pythonbpf_example.py
|
# pythonbpf_example.py
|
||||||
import pythonbpf as pb
|
from pythonbpf import bpf, map, bpfglobal, section, compile
|
||||||
from pythonbpf.helpers import bpf_ktime_get_ns
|
from pythonbpf.helpers import bpf_ktime_get_ns
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.section("tracepoint/syscalls/sys_enter_execve")
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
def hello(ctx: c_void_p) -> c_int32:
|
def hello(ctx: c_void_p) -> c_int32:
|
||||||
print("entered")
|
print("entered")
|
||||||
return c_int32(0)
|
return c_int32(0)
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.section("tracepoint/syscalls/sys_exit_execve")
|
@section("tracepoint/syscalls/sys_exit_execve")
|
||||||
def hello_again(ctx: c_void_p) -> c_int64:
|
def hello_again(ctx: c_void_p) -> c_int64:
|
||||||
print("exited")
|
print("exited")
|
||||||
key = 0
|
key = 0
|
||||||
@ -40,8 +40,8 @@ def hello_again(ctx: c_void_p) -> c_int64:
|
|||||||
ts = bpf_ktime_get_ns()
|
ts = bpf_ktime_get_ns()
|
||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.bpfglobal
|
@bpfglobal
|
||||||
def LICENSE() -> str:
|
def LICENSE() -> str:
|
||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ def some_normal_function():
|
|||||||
print("normal function")
|
print("normal function")
|
||||||
|
|
||||||
# compiles and dumps object file in the same directory
|
# compiles and dumps object file in the same directory
|
||||||
pb.compile()
|
compile()
|
||||||
```
|
```
|
||||||
- Run `python pythonbpf_example.py` to get the compiled object file that can be then loaded into the kernel.
|
- Run `python pythonbpf_example.py` to get the compiled object file that can be then loaded into the kernel.
|
||||||
|
|
||||||
|
|||||||
@ -1,25 +1,25 @@
|
|||||||
import pythonbpf as pb
|
from pythonbpf import bpf, map, section, bpfglobal, compile
|
||||||
from pythonbpf.helpers import bpf_ktime_get_ns
|
from pythonbpf.helpers import bpf_ktime_get_ns
|
||||||
from pythonbpf.maps import HashMap
|
from pythonbpf.maps import HashMap
|
||||||
|
|
||||||
from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
from ctypes import c_void_p, c_int64, c_int32, c_uint64
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.map
|
@map
|
||||||
def last() -> HashMap:
|
def last() -> HashMap:
|
||||||
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
|
||||||
|
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.section("tracepoint/syscalls/sys_enter_execve")
|
@section("tracepoint/syscalls/sys_enter_execve")
|
||||||
def hello(ctx: c_void_p) -> c_int32:
|
def hello(ctx: c_void_p) -> c_int32:
|
||||||
print("entered")
|
print("entered")
|
||||||
print("multi constant support")
|
print("multi constant support")
|
||||||
return c_int32(0)
|
return c_int32(0)
|
||||||
|
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.section("tracepoint/syscalls/sys_exit_execve")
|
@section("tracepoint/syscalls/sys_exit_execve")
|
||||||
def hello_again(ctx: c_void_p) -> c_int64:
|
def hello_again(ctx: c_void_p) -> c_int64:
|
||||||
print("exited")
|
print("exited")
|
||||||
key = 0
|
key = 0
|
||||||
@ -34,9 +34,9 @@ def hello_again(ctx: c_void_p) -> c_int64:
|
|||||||
return c_int64(0)
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
@pb.bpf
|
@bpf
|
||||||
@pb.bpfglobal
|
@bpfglobal
|
||||||
def LICENSE() -> str:
|
def LICENSE() -> str:
|
||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
pb.compile()
|
compile()
|
||||||
|
|||||||
@ -6,8 +6,8 @@ from .maps_pass import maps_proc
|
|||||||
from .globals_pass import globals_processing
|
from .globals_pass import globals_processing
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import pathlib
|
import inspect
|
||||||
import __main__
|
from pathlib import Path
|
||||||
|
|
||||||
def find_bpf_chunks(tree):
|
def find_bpf_chunks(tree):
|
||||||
"""Find all functions decorated with @bpf in the AST."""
|
"""Find all functions decorated with @bpf in the AST."""
|
||||||
@ -96,12 +96,14 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
def compile():
|
def compile():
|
||||||
main_file = pathlib.Path(__main__.__file__).resolve()
|
# Look one level up the stack to the caller of this function
|
||||||
|
caller_frame = inspect.stack()[1]
|
||||||
|
caller_file = Path(caller_frame.filename).resolve()
|
||||||
|
|
||||||
ll_file = pathlib.Path("/tmp") / main_file.with_suffix(".ll").name
|
ll_file = Path("/tmp") / caller_file.with_suffix(".ll").name
|
||||||
o_file = main_file.with_suffix(".o")
|
o_file = caller_file.with_suffix(".o")
|
||||||
|
|
||||||
compile_to_ir(str(main_file), str(ll_file))
|
compile_to_ir(str(caller_file), str(ll_file))
|
||||||
|
|
||||||
subprocess.run([
|
subprocess.run([
|
||||||
"llc", "-march=bpf", "-filetype=obj", "-O2",
|
"llc", "-march=bpf", "-filetype=obj", "-O2",
|
||||||
|
|||||||
Reference in New Issue
Block a user