mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
remove demos and add examples
Signed-off-by: varun-r-mallya <varunrmallya@gmail.com>
This commit is contained in:
35
examples/pybpf0.py
Normal file
35
examples/pybpf0.py
Normal file
@ -0,0 +1,35 @@
|
||||
from pythonbpf import bpf, section, bpfglobal, BPF
|
||||
import sys
|
||||
from ctypes import c_void_p, c_int64
|
||||
|
||||
# Instructions to how to run this program
|
||||
# 1. Install PythonBPF: pip install pythonbpf
|
||||
# 2. `sudo /path/to/venv/bin/python ./python-bpf/demo/pybpf0.py`
|
||||
|
||||
@bpf
|
||||
@section("tracepoint/syscalls/sys_enter_execve")
|
||||
def hello_world(ctx: c_void_p) -> c_int64:
|
||||
print("Hello, World!")
|
||||
return c_int64(0)
|
||||
|
||||
@bpf
|
||||
@bpfglobal
|
||||
def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
b = BPF()
|
||||
b.load_and_attach()
|
||||
|
||||
def main():
|
||||
try:
|
||||
with open("/sys/kernel/debug/tracing/trace_pipe", "r") as f:
|
||||
for line in f:
|
||||
sys.stdout.write(line)
|
||||
sys.stdout.flush()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
except PermissionError:
|
||||
sys.stderr.write("Need root privileges to read trace_pipe\n")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user