forked from feiskyer/ebpf-apps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_functions.py
executable file
·39 lines (32 loc) · 1000 Bytes
/
python_functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/python3
#
# Tracing Python functions
import subprocess
from bcc import BPF, USDT
from bcc.utils import printb
# find the PID for "python3 -m http.server"
cmd = subprocess.Popen(
["pgrep", "-f", "http.server"], stdout=subprocess.PIPE, shell=False
).communicate()
if cmd[0]:
pid = int(cmd[0].decode("ascii").strip())
else:
print("ERROR: cannot find PID for python3 -m http.server")
exit()
# load BPF program
u = USDT(pid=pid)
u.enable_probe(probe="function__entry", fn_name="print_functions")
b = BPF(src_file="python-functions.c", usdt_contexts=[u])
# callback for perf event
def print_event(cpu, data, size):
event = b["events"].event(data)
printb(b"%-9s %-6d %s" % (event.filename, event.lineno, event.funcname))
# print header
print("%-9s %-6s %s" % ("FILENAME", "LINENO", "FUNCTION"))
# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()