Commit bb95ef2f authored by yonghong-song's avatar yonghong-song Committed by GitHub

Merge pull request #1531 from natoscott/master

Allow for sharing of python tools/ scripts
parents fb8bbfe1 1a197dbf
...@@ -287,6 +287,8 @@ class BPF(object): ...@@ -287,6 +287,8 @@ class BPF(object):
if text: if text:
self.module = lib.bpf_module_create_c_from_string(text.encode("ascii"), self.module = lib.bpf_module_create_c_from_string(text.encode("ascii"),
self.debug, cflags_array, len(cflags_array)) self.debug, cflags_array, len(cflags_array))
if not self.module:
raise Exception("Failed to compile BPF text:\n%s" % text)
else: else:
src_file = BPF._find_file(src_file) src_file = BPF._find_file(src_file)
hdr_file = BPF._find_file(hdr_file) hdr_file = BPF._find_file(hdr_file)
...@@ -296,9 +298,8 @@ class BPF(object): ...@@ -296,9 +298,8 @@ class BPF(object):
else: else:
self.module = lib.bpf_module_create_c(src_file.encode("ascii"), self.module = lib.bpf_module_create_c(src_file.encode("ascii"),
self.debug, cflags_array, len(cflags_array)) self.debug, cflags_array, len(cflags_array))
if not self.module:
if not self.module: raise Exception("Failed to compile BPF module %s" % src_file)
raise Exception("Failed to compile BPF module %s" % src_file)
for usdt_context in usdt_contexts: for usdt_context in usdt_contexts:
usdt_context.attach_uprobes(self) usdt_context.attach_uprobes(self)
......
...@@ -40,6 +40,8 @@ parser.add_argument("interval", nargs="?", default=99999999, ...@@ -40,6 +40,8 @@ parser.add_argument("interval", nargs="?", default=99999999,
help="output interval, in seconds") help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999, parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs") help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args() args = parser.parse_args()
countdown = int(args.count) countdown = int(args.count)
debug = 0 debug = 0
...@@ -103,8 +105,10 @@ else: ...@@ -103,8 +105,10 @@ else:
bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
bpf_text = bpf_text.replace('STORE', bpf_text = bpf_text.replace('STORE',
'dist.increment(bpf_log2l(delta));') 'dist.increment(bpf_log2l(delta));')
if debug: if debug or args.ebpf:
print(bpf_text) print(bpf_text)
if args.ebpf:
exit()
# load BPF program # load BPF program
b = BPF(text=bpf_text) b = BPF(text=bpf_text)
......
...@@ -40,6 +40,8 @@ parser.add_argument("interval", nargs="?", default=1, ...@@ -40,6 +40,8 @@ parser.add_argument("interval", nargs="?", default=1,
help="output interval, in seconds") help="output interval, in seconds")
parser.add_argument("count", nargs="?", default=99999999, parser.add_argument("count", nargs="?", default=99999999,
help="number of outputs") help="number of outputs")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args() args = parser.parse_args()
interval = int(args.interval) interval = int(args.interval)
countdown = int(args.count) countdown = int(args.count)
...@@ -55,7 +57,7 @@ def signal_ignore(signal, frame): ...@@ -55,7 +57,7 @@ def signal_ignore(signal, frame):
print() print()
# load BPF program # load BPF program
b = BPF(text=""" bpf_text = """
#include <uapi/linux/ptrace.h> #include <uapi/linux/ptrace.h>
#include <linux/blkdev.h> #include <linux/blkdev.h>
...@@ -163,7 +165,13 @@ int trace_req_completion(struct pt_regs *ctx, struct request *req) ...@@ -163,7 +165,13 @@ int trace_req_completion(struct pt_regs *ctx, struct request *req)
return 0; return 0;
} }
""", debug=0) """
if args.ebpf:
print(bpf_text)
exit()
b = BPF(text=bpf_text)
b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start") b.attach_kprobe(event="blk_account_io_start", fn_name="trace_pid_start")
b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start") b.attach_kprobe(event="blk_start_request", fn_name="trace_req_start")
b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start") b.attach_kprobe(event="blk_mq_start_request", fn_name="trace_req_start")
......
...@@ -58,6 +58,8 @@ parser.add_argument("-L", "--localport", ...@@ -58,6 +58,8 @@ parser.add_argument("-L", "--localport",
help="comma-separated list of local ports to trace.") help="comma-separated list of local ports to trace.")
parser.add_argument("-D", "--remoteport", parser.add_argument("-D", "--remoteport",
help="comma-separated list of remote ports to trace.") help="comma-separated list of remote ports to trace.")
parser.add_argument("--ebpf", action="store_true",
help=argparse.SUPPRESS)
args = parser.parse_args() args = parser.parse_args()
debug = 0 debug = 0
...@@ -375,8 +377,10 @@ bpf_text = bpf_text.replace('FILTER_PID', '') ...@@ -375,8 +377,10 @@ bpf_text = bpf_text.replace('FILTER_PID', '')
bpf_text = bpf_text.replace('FILTER_DPORT', '') bpf_text = bpf_text.replace('FILTER_DPORT', '')
bpf_text = bpf_text.replace('FILTER_LPORT', '') bpf_text = bpf_text.replace('FILTER_LPORT', '')
if debug: if debug or args.ebpf:
print(bpf_text) print(bpf_text)
if args.ebpf:
exit()
# event data # event data
TASK_COMM_LEN = 16 # linux/sched.h TASK_COMM_LEN = 16 # linux/sched.h
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment