Skip to content

Commit ccd2bb3

Browse files
Update README.md
1 parent 05c398d commit ccd2bb3

1 file changed

Lines changed: 51 additions & 7 deletions

File tree

README.md

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,63 @@
66

77
This is an LLVM IR generator for eBPF programs in Python. We use llvmlite to generate LLVM IR from pure Python. This is then compiled to LLVM object files, which can be loaded into the kernel for execution. We do not rely on BCC to do our compilation.
88

9-
# Installation
9+
## Installation
1010
- Have `clang` installed.
1111
- `pip install pythonbpf`
1212

13+
## Usage
14+
```python
15+
# pythonbpf_example.py
16+
import pythonbpf as pb
17+
from pythonbpf.helpers import bpf_ktime_get_ns
18+
from pythonbpf.maps import HashMap
19+
20+
from ctypes import c_void_p, c_int64, c_int32, c_uint64
21+
22+
@pb.bpf
23+
@pb.map
24+
def last() -> HashMap:
25+
return HashMap(key_type=c_uint64, value_type=c_uint64, max_entries=1)
26+
27+
@pb.bpf
28+
@pb.section("tracepoint/syscalls/sys_enter_execve")
29+
def hello(ctx: c_void_p) -> c_int32:
30+
print("entered")
31+
return c_int32(0)
32+
33+
@pb.bpf
34+
@pb.section("tracepoint/syscalls/sys_exit_execve")
35+
def hello_again(ctx: c_void_p) -> c_int64:
36+
print("exited")
37+
key = 0
38+
tsp = last().lookup(key)
39+
print(tsp)
40+
ts = bpf_ktime_get_ns()
41+
return c_int64(0)
42+
43+
@pb.bpf
44+
@pb.bpfglobal
45+
def LICENSE() -> str:
46+
return "GPL"
47+
48+
def some_normal_function():
49+
print("normal function")
50+
51+
# compiles and dumps object file in the same directory
52+
pb.compile()
53+
```
54+
- Run `python pythonbpf_example.py` to get the compiled object file that can be then loaded into the kernel.
55+
1356
## Development
14-
Step 0. Make a virtual environment and activate it using `python3 -m venv .venv && source .venv/bin/activate`.
15-
Step 1. Run `make install` to install the required dependencies.
16-
Step 2. Run `make` to see the compilation output of the example.
17-
Step 3. Run `check.sh` to check if generated object file passes through the verifier inside the examples directory.
18-
Step 4. Run `make` in the `examples/c-form` directory to modify the example C BPF program to check the actual LLVM IR generated by clang.
57+
- Make a virtual environment and activate it using `python3 -m venv .venv && source .venv/bin/activate`.
58+
- Run `make install` to install the required dependencies.
59+
- Run `make` to see the compilation output of the example.
60+
- Run `check.sh` to check if generated object file passes through the verifier inside the examples directory.
61+
- Run `make` in the `examples/c-form` directory to modify the example C BPF program to check the actual LLVM IR generated by clang.
1962

2063
### Development Notes
21-
Run ` ./check.sh run execve2.o;` in examples folder
64+
- Run ` ./check.sh check execve2.o;` in examples folder to check if the object code passes the verifier.
65+
- Run ` ./check.sh run execve2.o;` in examples folder to run the object code using `bpftool`.
2266

2367
## Authors
2468
- [@r41k0u](https://github.com/r41k0u)

0 commit comments

Comments
 (0)