forked from iovisor/bcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbpf-run
More file actions
executable file
·96 lines (81 loc) · 2.45 KB
/
bpf-run
File metadata and controls
executable file
·96 lines (81 loc) · 2.45 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python
import sys
USAGE = """\
usage: {argv0} <opts> -p probe_func -c cmd
-c cmd contents of the program to run, omitting prototype (required)
-d name dump table <name> upon exit
-f format format string to apply to trace output (see python str.format())
-n sec run for <sec> seconds and then exit (default=-1)
-p probe kernel entry point to trace (required)
-t attach to kernel trace output
-v increase verbosity
example:
{argv0} -p sys_clone -c 'bpf_trace_printk("Hello, World!\\n");' -t\
"""
wrapper = """
int run(void *ctx) {
%s
return 0;
}
"""
def print_usage_and_exit(rc, msg=None):
if rc != 0:
sys.stdout = sys.stderr
if msg:
print(msg)
print(USAGE.format(argv0=sys.argv[0]))
sys.exit(rc)
def main():
import getopt
import os
import signal
try:
opts, args = getopt.getopt(sys.argv[1:], "c:d:f:hn:p:tv")
except getopt.error, msg:
print_usage_and_exit(2, msg)
runcmd = None
probe_fn = None
trace = 0
dump_tables = []
verbose = 0
nsec = 0
format_str = None
for o, a in opts:
if o == "-c": runcmd = a
if o == "-d": dump_tables.append(a)
if o == "-f": format_str = a
if o == "-h": print_usage_and_exit(0)
if o == "-n": nsec = int(a)
if o == "-p": probe_fn = a
if o == "-t": trace = 1
if o == "-v": verbose += 1
if not runcmd or not probe_fn:
print_usage_and_exit(2, "Error: -p and -c arguments are required")
from bpf import BPF
b = BPF(text=wrapper % runcmd, debug=verbose)
fn = b.load_func("run", BPF.KPROBE)
BPF.attach_kprobe(fn, probe_fn)
if nsec:
def receive_alarm(signo, stack):
os.kill(os.getpid(), signal.SIGINT)
signal.signal(signal.SIGALRM, receive_alarm)
signal.alarm(nsec)
try:
if trace:
with open("/sys/kernel/debug/tracing/trace_pipe") as f:
while True:
line = f.readline(128)
line = line.rstrip()
if format_str:
args=line.split(None, 5)
line = format_str.format(*args)
print(line)
sys.stdout.flush()
elif nsec:
signal.pause()
except KeyboardInterrupt:
pass
if dump_tables:
print("Table dump not yet implemented")
if __name__ == "__main__":
main()