Commit fd244056 authored by Teng Qin's avatar Teng Qin

Clean Python interface arguments for tracing events

parent b68e67b4
...@@ -499,8 +499,7 @@ class BPF(object): ...@@ -499,8 +499,7 @@ class BPF(object):
del self.open_kprobes[name] del self.open_kprobes[name]
_num_open_probes -= 1 _num_open_probes -= 1
def attach_kprobe(self, event="", fn_name="", event_re="", def attach_kprobe(self, event="", fn_name="", event_re=""):
pid=-1, cpu=0, group_fd=-1):
# allow the caller to glob multiple functions together # allow the caller to glob multiple functions together
if event_re: if event_re:
...@@ -508,8 +507,7 @@ class BPF(object): ...@@ -508,8 +507,7 @@ class BPF(object):
self._check_probe_quota(len(matches)) self._check_probe_quota(len(matches))
for line in matches: for line in matches:
try: try:
self.attach_kprobe(event=line, fn_name=fn_name, pid=pid, self.attach_kprobe(event=line, fn_name=fn_name)
cpu=cpu, group_fd=group_fd)
except: except:
pass pass
return return
...@@ -538,15 +536,13 @@ class BPF(object): ...@@ -538,15 +536,13 @@ class BPF(object):
raise Exception("Failed to detach BPF from kprobe") raise Exception("Failed to detach BPF from kprobe")
self._del_kprobe(ev_name) self._del_kprobe(ev_name)
def attach_kretprobe(self, event="", fn_name="", event_re="", def attach_kretprobe(self, event="", fn_name="", event_re=""):
pid=-1, cpu=0, group_fd=-1):
# allow the caller to glob multiple functions together # allow the caller to glob multiple functions together
if event_re: if event_re:
for line in BPF.get_kprobe_functions(event_re): for line in BPF.get_kprobe_functions(event_re):
try: try:
self.attach_kretprobe(event=line, fn_name=fn_name, pid=pid, self.attach_kretprobe(event=line, fn_name=fn_name)
cpu=cpu, group_fd=group_fd)
except: except:
pass pass
return return
...@@ -648,10 +644,8 @@ class BPF(object): ...@@ -648,10 +644,8 @@ class BPF(object):
results.append(tp) results.append(tp)
return results return results
def attach_tracepoint(self, tp="", tp_re="", fn_name="", pid=-1, def attach_tracepoint(self, tp="", tp_re="", fn_name=""):
cpu=0, group_fd=-1): """attach_tracepoint(tp="", tp_re="", fn_name="")
"""attach_tracepoint(tp="", tp_re="", fn_name="", pid=-1,
cpu=0, group_fd=-1)
Run the bpf function denoted by fn_name every time the kernel tracepoint Run the bpf function denoted by fn_name every time the kernel tracepoint
specified by 'tp' is hit. The optional parameters pid, cpu, and group_fd specified by 'tp' is hit. The optional parameters pid, cpu, and group_fd
...@@ -673,8 +667,7 @@ class BPF(object): ...@@ -673,8 +667,7 @@ class BPF(object):
if tp_re: if tp_re:
for tp in BPF.get_tracepoints(tp_re): for tp in BPF.get_tracepoints(tp_re):
self.attach_tracepoint(tp=tp, fn_name=fn_name, pid=pid, self.attach_tracepoint(tp=tp, fn_name=fn_name)
cpu=cpu, group_fd=group_fd)
return return
fn = self.load_func(fn_name, BPF.TRACEPOINT) fn = self.load_func(fn_name, BPF.TRACEPOINT)
...@@ -794,9 +787,9 @@ class BPF(object): ...@@ -794,9 +787,9 @@ class BPF(object):
return "%s_%s_0x%x_%d" % (prefix, self._probe_repl.sub("_", path), addr, pid) return "%s_%s_0x%x_%d" % (prefix, self._probe_repl.sub("_", path), addr, pid)
def attach_uprobe(self, name="", sym="", sym_re="", addr=None, def attach_uprobe(self, name="", sym="", sym_re="", addr=None,
fn_name="", pid=-1, cpu=0, group_fd=-1): fn_name="", pid=-1):
"""attach_uprobe(name="", sym="", sym_re="", addr=None, fn_name="" """attach_uprobe(name="", sym="", sym_re="", addr=None, fn_name=""
pid=-1, cpu=0, group_fd=-1) pid=-1)
Run the bpf function denoted by fn_name every time the symbol sym in Run the bpf function denoted by fn_name every time the symbol sym in
the library or binary 'name' is encountered. The real address addr may the library or binary 'name' is encountered. The real address addr may
...@@ -823,8 +816,7 @@ class BPF(object): ...@@ -823,8 +816,7 @@ class BPF(object):
self._check_probe_quota(len(addresses)) self._check_probe_quota(len(addresses))
for sym_addr in addresses: for sym_addr in addresses:
self.attach_uprobe(name=name, addr=sym_addr, self.attach_uprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu, fn_name=fn_name, pid=pid)
group_fd=group_fd)
return return
(path, addr) = BPF._check_path_symbol(name, sym, addr, pid) (path, addr) = BPF._check_path_symbol(name, sym, addr, pid)
...@@ -860,9 +852,9 @@ class BPF(object): ...@@ -860,9 +852,9 @@ class BPF(object):
self._del_uprobe(ev_name) self._del_uprobe(ev_name)
def attach_uretprobe(self, name="", sym="", sym_re="", addr=None, def attach_uretprobe(self, name="", sym="", sym_re="", addr=None,
fn_name="", pid=-1, cpu=0, group_fd=-1): fn_name="", pid=-1):
"""attach_uretprobe(name="", sym="", sym_re="", addr=None, fn_name="" """attach_uretprobe(name="", sym="", sym_re="", addr=None, fn_name=""
pid=-1, cpu=0, group_fd=-1) pid=-1)
Run the bpf function denoted by fn_name every time the symbol sym in Run the bpf function denoted by fn_name every time the symbol sym in
the library or binary 'name' finishes execution. See attach_uprobe for the library or binary 'name' finishes execution. See attach_uprobe for
...@@ -872,8 +864,7 @@ class BPF(object): ...@@ -872,8 +864,7 @@ class BPF(object):
if sym_re: if sym_re:
for sym_addr in BPF.get_user_addresses(name, sym_re): for sym_addr in BPF.get_user_addresses(name, sym_re):
self.attach_uretprobe(name=name, addr=sym_addr, self.attach_uretprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu, fn_name=fn_name, pid=pid)
group_fd=group_fd)
return return
name = str(name) name = str(name)
......
...@@ -27,9 +27,9 @@ class TestKprobe(TestCase): ...@@ -27,9 +27,9 @@ class TestKprobe(TestCase):
def setUp(self): def setUp(self):
b = BPF(arg1, arg2, debug=0) b = BPF(arg1, arg2, debug=0)
self.stats = b.get_table("stats", Key, Leaf) self.stats = b.get_table("stats", Key, Leaf)
b.attach_kprobe(event="sys_write", fn_name="sys_wr", pid=0, cpu=-1) b.attach_kprobe(event="sys_write", fn_name="sys_wr")
b.attach_kprobe(event="sys_read", fn_name="sys_rd", pid=0, cpu=-1) b.attach_kprobe(event="sys_read", fn_name="sys_rd")
b.attach_kprobe(event="htab_map_get_next_key", fn_name="sys_rd", pid=0, cpu=-1) b.attach_kprobe(event="htab_map_get_next_key", fn_name="sys_rd")
def test_trace1(self): def test_trace1(self):
with open("/dev/null", "a") as f: with open("/dev/null", "a") as f:
......
...@@ -28,7 +28,7 @@ class TestTracingEvent(TestCase): ...@@ -28,7 +28,7 @@ class TestTracingEvent(TestCase):
def setUp(self): def setUp(self):
b = BPF(text=text, debug=0) b = BPF(text=text, debug=0)
self.stats = b.get_table("stats") self.stats = b.get_table("stats")
b.attach_kprobe(event="finish_task_switch", fn_name="count_sched", pid=0, cpu=-1) b.attach_kprobe(event="finish_task_switch", fn_name="count_sched")
def test_sched1(self): def test_sched1(self):
for i in range(0, 100): for i in range(0, 100):
......
...@@ -19,9 +19,9 @@ class TestBlkRequest(TestCase): ...@@ -19,9 +19,9 @@ class TestBlkRequest(TestCase):
b = BPF(arg1, arg2, debug=0) b = BPF(arg1, arg2, debug=0)
self.latency = b.get_table("latency", c_uint, c_ulong) self.latency = b.get_table("latency", c_uint, c_ulong)
b.attach_kprobe(event="blk_start_request", b.attach_kprobe(event="blk_start_request",
fn_name="probe_blk_start_request", pid=-1, cpu=0) fn_name="probe_blk_start_request")
b.attach_kprobe(event="blk_update_request", b.attach_kprobe(event="blk_update_request",
fn_name="probe_blk_update_request", pid=-1, cpu=0) fn_name="probe_blk_update_request")
def test_blk1(self): def test_blk1(self):
import subprocess import subprocess
......
...@@ -468,9 +468,7 @@ def main(): ...@@ -468,9 +468,7 @@ def main():
bpf = BPF(src_file='deadlock_detector.c') bpf = BPF(src_file='deadlock_detector.c')
# Trace where threads are created # Trace where threads are created
bpf.attach_kretprobe( bpf.attach_kretprobe(event='sys_clone', fn_name='trace_clone')
event='sys_clone', fn_name='trace_clone', pid=args.pid
)
# We must trace unlock first, otherwise in the time we attached the probe # We must trace unlock first, otherwise in the time we attached the probe
# on lock() and have not yet attached the probe on unlock(), a thread can # on lock() and have not yet attached the probe on unlock(), a thread can
......
...@@ -90,8 +90,7 @@ class Probe(object): ...@@ -90,8 +90,7 @@ class Probe(object):
for index, function in self.trace_functions.items(): for index, function in self.trace_functions.items():
self.bpf.attach_kprobe( self.bpf.attach_kprobe(
event=function, event=function,
fn_name="trace_count_%d" % index, fn_name="trace_count_%d" % index)
pid=self.pid or -1)
elif self.type == "p" and self.library: elif self.type == "p" and self.library:
for index, function in self.trace_functions.items(): for index, function in self.trace_functions.items():
self.bpf.attach_uprobe( self.bpf.attach_uprobe(
...@@ -103,8 +102,7 @@ class Probe(object): ...@@ -103,8 +102,7 @@ class Probe(object):
for index, function in self.trace_functions.items(): for index, function in self.trace_functions.items():
self.bpf.attach_tracepoint( self.bpf.attach_tracepoint(
tp=function, tp=function,
fn_name="trace_count_%d" % index, fn_name="trace_count_%d" % index)
pid=self.pid or -1)
elif self.type == "u": elif self.type == "u":
pass # Nothing to do -- attach already happened in `load` pass # Nothing to do -- attach already happened in `load`
......
...@@ -90,13 +90,11 @@ class Probe(object): ...@@ -90,13 +90,11 @@ class Probe(object):
self.matched = self.bpf.num_open_uprobes() self.matched = self.bpf.num_open_uprobes()
else: else:
self.bpf.attach_kprobe(event_re=self.pattern, self.bpf.attach_kprobe(event_re=self.pattern,
fn_name="trace_count", fn_name="trace_count")
pid=self.pid or -1)
self.matched = self.bpf.num_open_kprobes() self.matched = self.bpf.num_open_kprobes()
elif self.type == "t": elif self.type == "t":
self.bpf.attach_tracepoint(tp_re=self.pattern, self.bpf.attach_tracepoint(tp_re=self.pattern,
fn_name="trace_count", fn_name="trace_count")
pid=self.pid or -1)
self.matched = self.bpf.num_open_tracepoints() self.matched = self.bpf.num_open_tracepoints()
elif self.type == "u": elif self.type == "u":
pass # Nothing to do -- attach already happened in `load` pass # Nothing to do -- attach already happened in `load`
......
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