mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
Add hello_fields BCC Example
This commit is contained in:
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}")
|
||||
@ -15,7 +15,6 @@ def LICENSE() -> str:
|
||||
return "GPL"
|
||||
|
||||
|
||||
# compile()
|
||||
b = BPF()
|
||||
b.load_and_attach()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user