Skip to content

Commit e19aa9f

Browse files
Organize source files
1 parent d5557d3 commit e19aa9f

7 files changed

Lines changed: 47 additions & 16 deletions

File tree

Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
install:
2-
pip install -e .
3-
41
compile:
52
chmod +x ./tools/compile.py
63
./tools/compile.py ./examples/execve.py
74

5+
install:
6+
pip install -e .
7+
88
clean:
99
rm -rf build dist *.egg-info
1010
rm -rf examples/execve.ll examples/execve.o

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python-BPF
2+
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.
3+
4+
## Development
5+
Step 1. Run `make install` to install the required dependencies.
6+
Step 2. Run `make` to see the compilation output of the example.
7+
8+
## Authors
9+
- [@r41k0u](https://github.com/r41k0u)
10+
- [@varun-r-mallya](https://github.com/varun-r-mallya)

examples/c-form/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
BPF_CLANG := clang
2+
CFLAGS := -O2 -emit-llvm -target bpf -c
3+
4+
SRC := example.bpf.c
5+
OUT := example.bpf.ll
6+
7+
.PHONY: all clean
8+
9+
all: $(OUT)
10+
11+
$(OUT): $(SRC)
12+
$(BPF_CLANG) $(CFLAGS) -S $< -o $@
13+
14+
clean:
15+
rm -f $(OUT)

examples/execve.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
from pythonbpf import decorators
1+
from pythonbpf.decorators import tracepoint, license
22

3-
@decorators.tracepoint("syscalls:sys_enter_execve")
3+
@tracepoint("syscalls:sys_enter_execve")
44
def trace_execve(ctx) -> int:
5-
decorators.trace_printk("execve called\n")
5+
print("execve called\n")
66
return 0
77

8-
@decorators.license("GPL")
9-
def _():
10-
pass
8+
license("GPL")

pythonbpf/codegen.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import ast
22
from llvmlite import ir
33

4+
def parser(source_code, filename):
5+
tree = ast.parse(source_code, filename)
6+
7+
for node in tree.body:
8+
if isinstance(node, ast.FunctionDef):
9+
print("Function:", node.name)
10+
for dec in node.decorator_list:
11+
print(" Decorator AST:", ast.dump(dec))
12+
413
def compile_to_ir(filename: str, output: str):
514
with open(filename) as f:
6-
ast.parse(f.read(), filename)
15+
parser(f.read(), filename)
16+
module = ir.Module(name=filename)
17+
module.data_layout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
18+
module.triple = "bpf"
719

8-
module = ir.Module(name="pythonbpf")
920
func_ty = ir.FunctionType(ir.IntType(64), [], False)
1021
func = ir.Function(module, func_ty, name="trace_execve")
1122

pythonbpf/decorators.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@ def wrapper(fn):
44
return fn
55
return wrapper
66

7-
def license(name: str):
8-
def wrapper(fn):
9-
fn._license = name
10-
return fn
11-
return wrapper
7+
def license(license_type: str):
8+
return license_type
129

1310
def trace_printk(msg: str):
1411
# placeholder — real version lowers to IR later

0 commit comments

Comments
 (0)