-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathsimple_app_trace.py
executable file
·61 lines (50 loc) · 1.3 KB
/
simple_app_trace.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/python3
#
# Tracing Python USDT.
# Start ./simple_app.py before starting this trace script.
import subprocess
from bcc import BPF, USDT
from bcc.utils import printb
prog = """#include <uapi/linux/ptrace.h>
struct data_t {
int a;
int b;
};
BPF_PERF_OUTPUT(events);
int print_functions(struct pt_regs *ctx)
{
uint64_t argptr;
struct data_t data = { };
/* function__entry params are (int a, int b) */
bpf_usdt_readarg(1, ctx, &data.a);
bpf_usdt_readarg(2, ctx, &data.b);
events.perf_submit(ctx, &data, sizeof(data));
return 0;
};
"""
# find the PID for "simple_app.py"
cmd = subprocess.Popen(
["pgrep", "-f", "simple_app.py"], stdout=subprocess.PIPE, shell=False
).communicate()
if cmd[0]:
pid = int(cmd[0].decode("ascii").strip())
else:
print("ERROR: cannot find PID for simple_app.py")
exit()
# load BPF program
u = USDT(pid=pid)
u.enable_probe(probe="sum", fn_name="print_functions")
b = BPF(text=prog, usdt_contexts=[u])
# callback for perf event
def print_event(cpu, data, size):
event = b["events"].event(data)
printb(b"%-6d %-6d" % (event.a, event.b))
# print header
print("%-6s %-6s" % ("a", "b"))
# loop with callback to print_event
b["events"].open_perf_buffer(print_event)
while 1:
try:
b.perf_buffer_poll()
except KeyboardInterrupt:
exit()