From 0567e717907baf3c5c6e2420b63932b19c07c1ec Mon Sep 17 00:00:00 2001 From: Hengqi Chen Date: Thu, 17 Jun 2021 00:01:21 +0800 Subject: [PATCH] bcc/python: fix attach kprobe/kretprobe using regex Attach kprobe/kretprobe using regular expression should fail explicitly if no functions are traceable. Currently we catch all exceptions and if no functions are available, program continue with no BPF programs attached. In this commit, change this behavior to explicitly report error to user. Signed-off-by: Hengqi Chen --- src/python/bcc/__init__.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py index e91ec464..96c3dd6b 100644 --- a/src/python/bcc/__init__.py +++ b/src/python/bcc/__init__.py @@ -782,11 +782,17 @@ class BPF(object): if event_re: matches = BPF.get_kprobe_functions(event_re) self._check_probe_quota(len(matches)) + failed = 0 + probes = [] for line in matches: try: self.attach_kprobe(event=line, fn_name=fn_name) except: - pass + failed += 1 + probes.append(line) + if failed == len(matches): + raise Exception("Failed to attach BPF program %s to kprobe %s" % + (fn_name, '/'.join(probes))) return self._check_probe_quota(1) @@ -806,12 +812,19 @@ class BPF(object): # allow the caller to glob multiple functions together if event_re: - for line in BPF.get_kprobe_functions(event_re): + matches = BPF.get_kprobe_functions(event_re) + failed = 0 + probes = [] + for line in matches: try: self.attach_kretprobe(event=line, fn_name=fn_name, maxactive=maxactive) except: - pass + failed += 1 + probes.append(line) + if failed == len(matches): + raise Exception("Failed to attach BPF program %s to kretprobe %s" % + (fn_name, '/'.join(probes))) return self._check_probe_quota(1) -- 2.34.1