Organize source files

This commit is contained in:
2025-08-30 18:49:08 +05:30
parent d5557d3c01
commit e19aa9fa66
7 changed files with 47 additions and 16 deletions

View File

@ -1,10 +1,10 @@
install:
pip install -e .
compile:
chmod +x ./tools/compile.py
./tools/compile.py ./examples/execve.py
install:
pip install -e .
clean:
rm -rf build dist *.egg-info
rm -rf examples/execve.ll examples/execve.o

View File

@ -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
View 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)

View File

@ -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:
decorators.trace_printk("execve called\n")
print("execve called\n")
return 0
@decorators.license("GPL")
def _():
pass
license("GPL")

View File

@ -1,11 +1,22 @@
import ast
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):
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 = ir.Function(module, func_ty, name="trace_execve")

View File

@ -4,11 +4,8 @@ def tracepoint(name: str):
return fn
return wrapper
def license(name: str):
def wrapper(fn):
fn._license = name
return fn
return wrapper
def license(license_type: str):
return license_type
def trace_printk(msg: str):
# placeholder — real version lowers to IR later