bcc/python: fix attach kprobe/kretprobe using regex
authorHengqi Chen <chenhengqi@outlook.com>
Wed, 16 Jun 2021 16:01:21 +0000 (00:01 +0800)
committeryonghong-song <ys114321@gmail.com>
Thu, 17 Jun 2021 06:08:01 +0000 (23:08 -0700)
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 <chenhengqi@outlook.com>
src/python/bcc/__init__.py

index e91ec4644dd5ebbc1e737f385d40de528db4e954..96c3dd6b351f8fbe6dfaebbc4296f988342cf938 100644 (file)
@@ -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)