From edc33733d9bbf00e85fb81db96961b3392903338 Mon Sep 17 00:00:00 2001 From: Pragyansh Chaturvedi Date: Tue, 14 Oct 2025 03:51:43 +0530 Subject: [PATCH] Add trace_pipe utility --- BCC-Examples/hello_world.py | 8 ++++++-- BCC-Examples/sys_sync.py | 19 +++++++++++++++++++ pythonbpf/__init__.py | 2 ++ pythonbpf/utils.py | 9 +++++++++ 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 BCC-Examples/sys_sync.py create mode 100644 pythonbpf/utils.py diff --git a/BCC-Examples/hello_world.py b/BCC-Examples/hello_world.py index 447cc1b..a44753c 100644 --- a/BCC-Examples/hello_world.py +++ b/BCC-Examples/hello_world.py @@ -1,4 +1,4 @@ -from pythonbpf import bpf, section, bpfglobal, compile +from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe from ctypes import c_void_p, c_int64 @@ -15,4 +15,8 @@ def LICENSE() -> str: return "GPL" -compile() +# compile() +b = BPF() +b.load_and_attach() + +trace_pipe() diff --git a/BCC-Examples/sys_sync.py b/BCC-Examples/sys_sync.py new file mode 100644 index 0000000..1d872eb --- /dev/null +++ b/BCC-Examples/sys_sync.py @@ -0,0 +1,19 @@ +from pythonbpf import bpf, section, bpfglobal, compile +from ctypes import c_void_p, c_int64 + + +@bpf +@section("tracepoint/syscalls/sys_enter_sync") +def hello_world(ctx: c_void_p) -> c_int64: + print("sys_sync() called") + return c_int64(0) + + +@bpf +@bpfglobal +def LICENSE() -> str: + return "GPL" + + +compile() +print("Tracing sys_sync()... Ctrl-C to end.") diff --git a/pythonbpf/__init__.py b/pythonbpf/__init__.py index 022af1b..3eda126 100644 --- a/pythonbpf/__init__.py +++ b/pythonbpf/__init__.py @@ -1,5 +1,6 @@ from .decorators import bpf, map, section, bpfglobal, struct from .codegen import compile_to_ir, compile, BPF +from .utils import trace_pipe __all__ = [ "bpf", @@ -10,4 +11,5 @@ __all__ = [ "compile_to_ir", "compile", "BPF", + "trace_pipe", ] diff --git a/pythonbpf/utils.py b/pythonbpf/utils.py new file mode 100644 index 0000000..8664d80 --- /dev/null +++ b/pythonbpf/utils.py @@ -0,0 +1,9 @@ +import subprocess + + +def trace_pipe(): + """Util to read from the trace pipe.""" + try: + subprocess.run(["cat", "/sys/kernel/tracing/trace_pipe"]) + except KeyboardInterrupt: + print("Tracing stopped.")