Skip to content

Commit

Permalink
py3 probe registration compatibility fixes
Browse files Browse the repository at this point in the history
* rework `_get_kprobe_functions` to avoid unclosed blacklist warning
* rework `cleanup` to avoid changing size of dict while iterating
* make handling return of `bpf_function_name` work in py2 and py3
  • Loading branch information
Mark Drayton committed Jul 27, 2016
1 parent 5938394 commit 1d6a31b
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/python/bcc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,12 @@ def attach_raw_socket(fn, dev):
fn.sock = sock

def _get_kprobe_functions(self, event_re):
blacklist = set([line.rstrip().split()[1] for line in
open("%s/../kprobes/blacklist" % TRACEFS)])
with open("%s/../kprobes/blacklist" % TRACEFS) as blacklist_file:
blacklist = set([line.rstrip().split()[1] for line in
blacklist_file])
fns = []
with open("%s/available_filter_functions" % TRACEFS) as f:
for line in f:
with open("%s/available_filter_functions" % TRACEFS) as avail_file:
for line in avail_file:
fn = line.rstrip().split()[0]
if re.match(event_re, fn) and fn not in blacklist:
fns.append(fn)
Expand Down Expand Up @@ -604,7 +605,7 @@ def detach_uretprobe(self, name="", sym="", addr=None):

def _trace_autoload(self):
for i in range(0, lib.bpf_num_functions(self.module)):
func_name = lib.bpf_function_name(self.module, i)
func_name = str(lib.bpf_function_name(self.module, i).decode())
if func_name.startswith("kprobe__"):
fn = self.load_func(func_name, BPF.KPROBE)
self.attach_kprobe(event=fn.name[8:], fn_name=fn.name)
Expand Down Expand Up @@ -768,14 +769,14 @@ def kprobe_poll(self, timeout = -1):
exit()

def cleanup(self):
for k, v in self.open_kprobes.items():
for k, v in list(self.open_kprobes.items()):
lib.perf_reader_free(v)
# non-string keys here include the perf_events reader
if isinstance(k, str):
desc = "-:kprobes/%s" % k
lib.bpf_detach_kprobe(desc.encode("ascii"))
self._del_kprobe(k)
for k, v in self.open_uprobes.items():
for k, v in list(self.open_uprobes.items()):
lib.perf_reader_free(v)
desc = "-:uprobes/%s" % k
lib.bpf_detach_uprobe(desc.encode("ascii"))
Expand Down

0 comments on commit 1d6a31b

Please sign in to comment.