Skip to content

Commit

Permalink
Clean Python interface arguments for tracing events
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtenor committed Dec 16, 2017
1 parent b68e67b commit fd24405
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 39 deletions.
35 changes: 13 additions & 22 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,17 +499,15 @@ def _del_kprobe(self, name):
del self.open_kprobes[name]
_num_open_probes -= 1

def attach_kprobe(self, event="", fn_name="", event_re="",
pid=-1, cpu=0, group_fd=-1):
def attach_kprobe(self, event="", fn_name="", event_re=""):

# allow the caller to glob multiple functions together
if event_re:
matches = BPF.get_kprobe_functions(event_re)
self._check_probe_quota(len(matches))
for line in matches:
try:
self.attach_kprobe(event=line, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
self.attach_kprobe(event=line, fn_name=fn_name)
except:
pass
return
Expand Down Expand Up @@ -538,15 +536,13 @@ def detach_kprobe(self, event):
raise Exception("Failed to detach BPF from kprobe")
self._del_kprobe(ev_name)

def attach_kretprobe(self, event="", fn_name="", event_re="",
pid=-1, cpu=0, group_fd=-1):
def attach_kretprobe(self, event="", fn_name="", event_re=""):

# allow the caller to glob multiple functions together
if event_re:
for line in BPF.get_kprobe_functions(event_re):
try:
self.attach_kretprobe(event=line, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
self.attach_kretprobe(event=line, fn_name=fn_name)
except:
pass
return
Expand Down Expand Up @@ -648,10 +644,8 @@ def get_tracepoints(tp_re):
results.append(tp)
return results

def attach_tracepoint(self, tp="", tp_re="", fn_name="", pid=-1,
cpu=0, group_fd=-1):
"""attach_tracepoint(tp="", tp_re="", fn_name="", pid=-1,
cpu=0, group_fd=-1)
def attach_tracepoint(self, tp="", tp_re="", fn_name=""):
"""attach_tracepoint(tp="", tp_re="", fn_name="")
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
Expand All @@ -673,8 +667,7 @@ def attach_tracepoint(self, tp="", tp_re="", fn_name="", pid=-1,

if tp_re:
for tp in BPF.get_tracepoints(tp_re):
self.attach_tracepoint(tp=tp, fn_name=fn_name, pid=pid,
cpu=cpu, group_fd=group_fd)
self.attach_tracepoint(tp=tp, fn_name=fn_name)
return

fn = self.load_func(fn_name, BPF.TRACEPOINT)
Expand Down Expand Up @@ -794,9 +787,9 @@ def _get_uprobe_evname(self, prefix, 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,
fn_name="", pid=-1, cpu=0, group_fd=-1):
fn_name="", pid=-1):
"""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
the library or binary 'name' is encountered. The real address addr may
Expand All @@ -823,8 +816,7 @@ def attach_uprobe(self, name="", sym="", sym_re="", addr=None,
self._check_probe_quota(len(addresses))
for sym_addr in addresses:
self.attach_uprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu,
group_fd=group_fd)
fn_name=fn_name, pid=pid)
return

(path, addr) = BPF._check_path_symbol(name, sym, addr, pid)
Expand Down Expand Up @@ -860,9 +852,9 @@ def detach_uprobe(self, name="", sym="", addr=None, pid=-1):
self._del_uprobe(ev_name)

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=""
pid=-1, cpu=0, group_fd=-1)
pid=-1)
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
Expand All @@ -872,8 +864,7 @@ def attach_uretprobe(self, name="", sym="", sym_re="", addr=None,
if sym_re:
for sym_addr in BPF.get_user_addresses(name, sym_re):
self.attach_uretprobe(name=name, addr=sym_addr,
fn_name=fn_name, pid=pid, cpu=cpu,
group_fd=group_fd)
fn_name=fn_name, pid=pid)
return

name = str(name)
Expand Down
6 changes: 3 additions & 3 deletions tests/python/test_trace1.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ class TestKprobe(TestCase):
def setUp(self):
b = BPF(arg1, arg2, debug=0)
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_read", fn_name="sys_rd", pid=0, cpu=-1)
b.attach_kprobe(event="htab_map_get_next_key", fn_name="sys_rd", pid=0, cpu=-1)
b.attach_kprobe(event="sys_write", fn_name="sys_wr")
b.attach_kprobe(event="sys_read", fn_name="sys_rd")
b.attach_kprobe(event="htab_map_get_next_key", fn_name="sys_rd")

def test_trace1(self):
with open("/dev/null", "a") as f:
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_trace2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TestTracingEvent(TestCase):
def setUp(self):
b = BPF(text=text, debug=0)
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):
for i in range(0, 100):
Expand Down
4 changes: 2 additions & 2 deletions tests/python/test_trace3.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ def setUp(self):
b = BPF(arg1, arg2, debug=0)
self.latency = b.get_table("latency", c_uint, c_ulong)
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",
fn_name="probe_blk_update_request", pid=-1, cpu=0)
fn_name="probe_blk_update_request")

def test_blk1(self):
import subprocess
Expand Down
4 changes: 1 addition & 3 deletions tools/deadlock_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,7 @@ def main():
bpf = BPF(src_file='deadlock_detector.c')

# Trace where threads are created
bpf.attach_kretprobe(
event='sys_clone', fn_name='trace_clone', pid=args.pid
)
bpf.attach_kretprobe(event='sys_clone', fn_name='trace_clone')

# 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
Expand Down
6 changes: 2 additions & 4 deletions tools/funccount.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,7 @@ def attach(self):
for index, function in self.trace_functions.items():
self.bpf.attach_kprobe(
event=function,
fn_name="trace_count_%d" % index,
pid=self.pid or -1)
fn_name="trace_count_%d" % index)
elif self.type == "p" and self.library:
for index, function in self.trace_functions.items():
self.bpf.attach_uprobe(
Expand All @@ -103,8 +102,7 @@ def attach(self):
for index, function in self.trace_functions.items():
self.bpf.attach_tracepoint(
tp=function,
fn_name="trace_count_%d" % index,
pid=self.pid or -1)
fn_name="trace_count_%d" % index)
elif self.type == "u":
pass # Nothing to do -- attach already happened in `load`

Expand Down
6 changes: 2 additions & 4 deletions tools/stackcount.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,11 @@ def attach(self):
self.matched = self.bpf.num_open_uprobes()
else:
self.bpf.attach_kprobe(event_re=self.pattern,
fn_name="trace_count",
pid=self.pid or -1)
fn_name="trace_count")
self.matched = self.bpf.num_open_kprobes()
elif self.type == "t":
self.bpf.attach_tracepoint(tp_re=self.pattern,
fn_name="trace_count",
pid=self.pid or -1)
fn_name="trace_count")
self.matched = self.bpf.num_open_tracepoints()
elif self.type == "u":
pass # Nothing to do -- attach already happened in `load`
Expand Down

0 comments on commit fd24405

Please sign in to comment.