Skip to content

Commit

Permalink
Remove asserts on str probe names
Browse files Browse the repository at this point in the history
`open_kprobes` is a dict of open kprobes. Its keys are strings for normal
probes and a tuple for perf buffers. Normal probes need unregistering on script
exit; perf buffers do not. `cleanup` currently looks for string keys
(specifically type `str`) when working out what to unregister, which is a bit
brittle -- in Python2 strings can be both native `str` and `unicode`, depending
what exactly was passed to `attach-*/detach_*` and whether `from __future__
import unicode_literals` is used (e.g. iovisor#623).

This diff makes the API more relaxed by casting the probe name to `str` to
match the expectations of `cleanup`. This works in py2 (with and without
unicode_literals) and py3.
  • Loading branch information
Mark Drayton committed Jul 27, 2016
1 parent 1d6a31b commit 7c14842
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@ def _del_kprobe(self, name):
def attach_kprobe(self, event="", fn_name="", event_re="",
pid=-1, cpu=0, group_fd=-1):

assert isinstance(event, str), "event must be a string"
# allow the caller to glob multiple functions together
if event_re:
for line in self._get_kprobe_functions(event_re):
Expand All @@ -375,6 +374,7 @@ def attach_kprobe(self, event="", fn_name="", event_re="",
pass
return

event = str(event)
self._check_probe_quota(1)
fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = "p_" + event.replace("+", "_").replace(".", "_")
Expand All @@ -389,7 +389,7 @@ def attach_kprobe(self, event="", fn_name="", event_re="",
return self

def detach_kprobe(self, event):
assert isinstance(event, str), "event must be a string"
event = str(event)
ev_name = "p_" + event.replace("+", "_").replace(".", "_")
if ev_name not in self.open_kprobes:
raise Exception("Kprobe %s is not attached" % event)
Expand All @@ -403,7 +403,6 @@ def detach_kprobe(self, event):
def attach_kretprobe(self, event="", fn_name="", event_re="",
pid=-1, cpu=0, group_fd=-1):

assert isinstance(event, str), "event must be a string"
# allow the caller to glob multiple functions together
if event_re:
for line in self._get_kprobe_functions(event_re):
Expand All @@ -414,6 +413,7 @@ def attach_kretprobe(self, event="", fn_name="", event_re="",
pass
return

event = str(event)
self._check_probe_quota(1)
fn = self.load_func(fn_name, BPF.KPROBE)
ev_name = "r_" + event.replace("+", "_").replace(".", "_")
Expand All @@ -428,7 +428,7 @@ def attach_kretprobe(self, event="", fn_name="", event_re="",
return self

def detach_kretprobe(self, event):
assert isinstance(event, str), "event must be a string"
event = str(event)
ev_name = "r_" + event.replace("+", "_").replace(".", "_")
if ev_name not in self.open_kprobes:
raise Exception("Kretprobe %s is not attached" % event)
Expand Down Expand Up @@ -527,6 +527,7 @@ def attach_uprobe(self, name="", sym="", addr=None,
BPF(text).attach_uprobe("/usr/bin/python", "main")
"""

name = str(name)
(path, addr) = BPF._check_path_symbol(name, sym, addr)

self._check_probe_quota(1)
Expand All @@ -549,6 +550,7 @@ def detach_uprobe(self, name="", sym="", addr=None):
or binary 'name'.
"""

name = str(name)
(path, addr) = BPF._check_path_symbol(name, sym, addr)
ev_name = "p_%s_0x%x" % (self._probe_repl.sub("_", path), addr)
if ev_name not in self.open_uprobes:
Expand All @@ -570,6 +572,7 @@ def attach_uretprobe(self, name="", sym="", addr=None,
meaning of additional parameters.
"""

name = str(name)
(path, addr) = BPF._check_path_symbol(name, sym, addr)

self._check_probe_quota(1)
Expand All @@ -592,6 +595,7 @@ def detach_uretprobe(self, name="", sym="", addr=None):
or binary 'name'.
"""

name = str(name)
(path, addr) = BPF._check_path_symbol(name, sym, addr)
ev_name = "r_%s_0x%x" % (self._probe_repl.sub("_", path), addr)
if ev_name not in self.open_uprobes:
Expand Down

0 comments on commit 7c14842

Please sign in to comment.