Skip to content

Commit 903654d

Browse files
committed
Add hello_fields BCC Example
1 parent 263402d commit 903654d

File tree

3 files changed

+68
-11
lines changed

3 files changed

+68
-11
lines changed

BCC-Examples/hello_fields.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from pythonbpf import bpf, section, bpfglobal, BPF, trace_fields
2+
from ctypes import c_void_p, c_int64
3+
4+
5+
@bpf
6+
@section("tracepoint/syscalls/sys_enter_clone")
7+
def hello_world(ctx: c_void_p) -> c_int64:
8+
print("Hello, World!")
9+
return c_int64(0)
10+
11+
12+
@bpf
13+
@bpfglobal
14+
def LICENSE() -> str:
15+
return "GPL"
16+
17+
18+
# compile
19+
b = BPF()
20+
b.load_and_attach()
21+
22+
# header
23+
print(f"{'TIME(s)':<18} {'COMM':<16} {'PID':<6} {'MESSAGE'}")
24+
25+
# format output
26+
while True:
27+
try:
28+
(task, pid, cpu, flags, ts, msg) = trace_fields()
29+
except ValueError:
30+
continue
31+
except KeyboardInterrupt:
32+
exit()
33+
print(f"{ts:<18} {task:<16} {pid:<6} {msg}")

BCC-Examples/hello_world.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ def LICENSE() -> str:
1515
return "GPL"
1616

1717

18-
# compile()
1918
b = BPF()
2019
b.load_and_attach()
2120

pythonbpf/utils.py

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
import subprocess
2-
import re
3-
4-
TRACE_PATTERN = re.compile(
5-
rb"^(.{1,16}?)-(\d+)\s+\[(\d+)\]\s+([a-zA-Z.]+)\s+([0-9.]+):\s+.*?:\s+(.*)$"
6-
)
72

83

94
def trace_pipe():
@@ -20,12 +15,42 @@ def trace_fields():
2015
while True:
2116
line = f.readline().rstrip()
2217

23-
if not line or line.startswith(b"CPU:"):
18+
if not line:
19+
continue
20+
21+
# Skip lost event lines
22+
if line.startswith(b"CPU:"):
2423
continue
2524

26-
match = TRACE_PATTERN.match(line)
27-
if not match:
25+
# Parse BCC-style: first 16 bytes = task
26+
task = line[:16].lstrip().decode("utf-8")
27+
line = line[17:] # Skip past task field and space
28+
29+
# Find the colon that ends "pid cpu flags timestamp"
30+
ts_end = line.find(b":")
31+
if ts_end == -1:
2832
raise ValueError("Cannot parse trace line")
2933

30-
task, pid, cpu, flags, ts, msg = match.groups()
31-
return (task.strip(), int(pid), int(cpu), flags, float(ts), msg)
34+
# Split "pid [cpu] flags timestamp"
35+
try:
36+
parts = line[:ts_end].split()
37+
if len(parts) < 4:
38+
raise ValueError("Not enough fields")
39+
40+
pid = int(parts[0])
41+
cpu = parts[1][1:-1] # Remove brackets from [cpu]
42+
cpu = int(cpu)
43+
flags = parts[2]
44+
ts = float(parts[3])
45+
except (ValueError, IndexError):
46+
raise ValueError("Cannot parse trace line")
47+
48+
# Get message: skip ": symbol:" part
49+
line = line[ts_end + 1 :] # Skip first ":"
50+
sym_end = line.find(b":")
51+
if sym_end != -1:
52+
msg = line[sym_end + 2 :].decode("utf-8") # Skip ": " after symbol
53+
else:
54+
msg = line.lstrip().decode("utf-8")
55+
56+
return (task, pid, cpu, flags, ts, msg)

0 commit comments

Comments
 (0)