mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Organize source files
This commit is contained in:
6
Makefile
6
Makefile
@ -1,10 +1,10 @@
|
|||||||
install:
|
|
||||||
pip install -e .
|
|
||||||
|
|
||||||
compile:
|
compile:
|
||||||
chmod +x ./tools/compile.py
|
chmod +x ./tools/compile.py
|
||||||
./tools/compile.py ./examples/execve.py
|
./tools/compile.py ./examples/execve.py
|
||||||
|
|
||||||
|
install:
|
||||||
|
pip install -e .
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -rf build dist *.egg-info
|
rm -rf build dist *.egg-info
|
||||||
rm -rf examples/execve.ll examples/execve.o
|
rm -rf examples/execve.ll examples/execve.o
|
||||||
|
|||||||
10
README.md
10
README.md
@ -0,0 +1,10 @@
|
|||||||
|
# Python-BPF
|
||||||
|
This is an LLVM IR generator for eBPF program. We use `llvmlite` to generate LLVM IR code from pure Python code. This is then compiled to LLVM object files, which can be loaded into the kernel for execution.
|
||||||
|
|
||||||
|
## Development
|
||||||
|
Step 1. Run `make install` to install the required dependencies.
|
||||||
|
Step 2. Run `make` to see the compilation output of the example.
|
||||||
|
|
||||||
|
## Authors
|
||||||
|
- [@r41k0u](https://github.com/r41k0u)
|
||||||
|
- [@varun-r-mallya](https://github.com/varun-r-mallya)
|
||||||
|
|||||||
15
examples/c-form/Makefile
Normal file
15
examples/c-form/Makefile
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
BPF_CLANG := clang
|
||||||
|
CFLAGS := -O2 -emit-llvm -target bpf -c
|
||||||
|
|
||||||
|
SRC := example.bpf.c
|
||||||
|
OUT := example.bpf.ll
|
||||||
|
|
||||||
|
.PHONY: all clean
|
||||||
|
|
||||||
|
all: $(OUT)
|
||||||
|
|
||||||
|
$(OUT): $(SRC)
|
||||||
|
$(BPF_CLANG) $(CFLAGS) -S $< -o $@
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f $(OUT)
|
||||||
@ -1,10 +1,8 @@
|
|||||||
from pythonbpf import decorators
|
from pythonbpf.decorators import tracepoint, license
|
||||||
|
|
||||||
@decorators.tracepoint("syscalls:sys_enter_execve")
|
@tracepoint("syscalls:sys_enter_execve")
|
||||||
def trace_execve(ctx) -> int:
|
def trace_execve(ctx) -> int:
|
||||||
decorators.trace_printk("execve called\n")
|
print("execve called\n")
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
@decorators.license("GPL")
|
license("GPL")
|
||||||
def _():
|
|
||||||
pass
|
|
||||||
@ -1,11 +1,22 @@
|
|||||||
import ast
|
import ast
|
||||||
from llvmlite import ir
|
from llvmlite import ir
|
||||||
|
|
||||||
|
def parser(source_code, filename):
|
||||||
|
tree = ast.parse(source_code, filename)
|
||||||
|
|
||||||
|
for node in tree.body:
|
||||||
|
if isinstance(node, ast.FunctionDef):
|
||||||
|
print("Function:", node.name)
|
||||||
|
for dec in node.decorator_list:
|
||||||
|
print(" Decorator AST:", ast.dump(dec))
|
||||||
|
|
||||||
def compile_to_ir(filename: str, output: str):
|
def compile_to_ir(filename: str, output: str):
|
||||||
with open(filename) as f:
|
with open(filename) as f:
|
||||||
ast.parse(f.read(), filename)
|
parser(f.read(), filename)
|
||||||
|
module = ir.Module(name=filename)
|
||||||
|
module.data_layout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
|
||||||
|
module.triple = "bpf"
|
||||||
|
|
||||||
module = ir.Module(name="pythonbpf")
|
|
||||||
func_ty = ir.FunctionType(ir.IntType(64), [], False)
|
func_ty = ir.FunctionType(ir.IntType(64), [], False)
|
||||||
func = ir.Function(module, func_ty, name="trace_execve")
|
func = ir.Function(module, func_ty, name="trace_execve")
|
||||||
|
|
||||||
|
|||||||
@ -4,11 +4,8 @@ def tracepoint(name: str):
|
|||||||
return fn
|
return fn
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
def license(name: str):
|
def license(license_type: str):
|
||||||
def wrapper(fn):
|
return license_type
|
||||||
fn._license = name
|
|
||||||
return fn
|
|
||||||
return wrapper
|
|
||||||
|
|
||||||
def trace_printk(msg: str):
|
def trace_printk(msg: str):
|
||||||
# placeholder — real version lowers to IR later
|
# placeholder — real version lowers to IR later
|
||||||
|
|||||||
Reference in New Issue
Block a user