mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Compare commits
5 Commits
v0.1.5
...
903654daff
| Author | SHA1 | Date | |
|---|---|---|---|
| 903654daff | |||
| 263402d137 | |||
| 37d1e1b143 | |||
| edc33733d9 | |||
| 18d62d605a |
33
BCC-Examples/hello_fields.py
Normal file
33
BCC-Examples/hello_fields.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from pythonbpf import bpf, section, bpfglobal, BPF, trace_fields
|
||||||
|
from ctypes import c_void_p, c_int64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
def hello_world(ctx: c_void_p) -> c_int64:
|
||||||
|
print("Hello, World!")
|
||||||
|
return c_int64(0)
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@bpfglobal
|
||||||
|
def LICENSE() -> str:
|
||||||
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
|
# compile
|
||||||
|
b = BPF()
|
||||||
|
b.load_and_attach()
|
||||||
|
|
||||||
|
# header
|
||||||
|
print(f"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}")
|
||||||
|
|
||||||
|
# format output
|
||||||
|
while True:
|
||||||
|
try:
|
||||||
|
(task, pid, cpu, flags, ts, msg) = trace_fields()
|
||||||
|
except ValueError:
|
||||||
|
continue
|
||||||
|
except KeyboardInterrupt:
|
||||||
|
exit()
|
||||||
|
print(f"{ts:<18} {task:<16} {pid:<6} {msg}")
|
||||||
21
BCC-Examples/hello_world.py
Normal file
21
BCC-Examples/hello_world.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
|
||||||
|
from ctypes import c_void_p, c_int64
|
||||||
|
|
||||||
|
|
||||||
|
@bpf
|
||||||
|
@section("tracepoint/syscalls/sys_enter_clone")
|
||||||
|
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()
|
||||||
|
|
||||||
|
trace_pipe()
|
||||||
20
BCC-Examples/sys_sync.py
Normal file
20
BCC-Examples/sys_sync.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from pythonbpf import bpf, section, bpfglobal, BPF, trace_pipe
|
||||||
|
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"
|
||||||
|
|
||||||
|
|
||||||
|
BPF().load_and_attach()
|
||||||
|
print("Tracing sys_sync()... Ctrl-C to end.")
|
||||||
|
trace_pipe()
|
||||||
@ -1,5 +1,6 @@
|
|||||||
from .decorators import bpf, map, section, bpfglobal, struct
|
from .decorators import bpf, map, section, bpfglobal, struct
|
||||||
from .codegen import compile_to_ir, compile, BPF
|
from .codegen import compile_to_ir, compile, BPF
|
||||||
|
from .utils import trace_pipe, trace_fields
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
"bpf",
|
"bpf",
|
||||||
@ -10,4 +11,6 @@ __all__ = [
|
|||||||
"compile_to_ir",
|
"compile_to_ir",
|
||||||
"compile",
|
"compile",
|
||||||
"BPF",
|
"BPF",
|
||||||
|
"trace_pipe",
|
||||||
|
"trace_fields",
|
||||||
]
|
]
|
||||||
|
|||||||
56
pythonbpf/utils.py
Normal file
56
pythonbpf/utils.py
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
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.")
|
||||||
|
|
||||||
|
|
||||||
|
def trace_fields():
|
||||||
|
"""Parse one line from trace_pipe into fields."""
|
||||||
|
with open("/sys/kernel/tracing/trace_pipe", "rb", buffering=0) as f:
|
||||||
|
while True:
|
||||||
|
line = f.readline().rstrip()
|
||||||
|
|
||||||
|
if not line:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Skip lost event lines
|
||||||
|
if line.startswith(b"CPU:"):
|
||||||
|
continue
|
||||||
|
|
||||||
|
# Parse BCC-style: first 16 bytes = task
|
||||||
|
task = line[:16].lstrip().decode("utf-8")
|
||||||
|
line = line[17:] # Skip past task field and space
|
||||||
|
|
||||||
|
# Find the colon that ends "pid cpu flags timestamp"
|
||||||
|
ts_end = line.find(b":")
|
||||||
|
if ts_end == -1:
|
||||||
|
raise ValueError("Cannot parse trace line")
|
||||||
|
|
||||||
|
# Split "pid [cpu] flags timestamp"
|
||||||
|
try:
|
||||||
|
parts = line[:ts_end].split()
|
||||||
|
if len(parts) < 4:
|
||||||
|
raise ValueError("Not enough fields")
|
||||||
|
|
||||||
|
pid = int(parts[0])
|
||||||
|
cpu = parts[1][1:-1] # Remove brackets from [cpu]
|
||||||
|
cpu = int(cpu)
|
||||||
|
flags = parts[2]
|
||||||
|
ts = float(parts[3])
|
||||||
|
except (ValueError, IndexError):
|
||||||
|
raise ValueError("Cannot parse trace line")
|
||||||
|
|
||||||
|
# Get message: skip ": symbol:" part
|
||||||
|
line = line[ts_end + 1 :] # Skip first ":"
|
||||||
|
sym_end = line.find(b":")
|
||||||
|
if sym_end != -1:
|
||||||
|
msg = line[sym_end + 2 :].decode("utf-8") # Skip ": " after symbol
|
||||||
|
else:
|
||||||
|
msg = line.lstrip().decode("utf-8")
|
||||||
|
|
||||||
|
return (task, pid, cpu, flags, ts, msg)
|
||||||
Reference in New Issue
Block a user