Helper to get open k[ret]probes. Fixes #236
diff --git a/src/python/bcc/__init__.py b/src/python/bcc/__init__.py
index 64440c7..d1b3cbc 100644
--- a/src/python/bcc/__init__.py
+++ b/src/python/bcc/__init__.py
@@ -708,3 +708,12 @@
return "[unknown]"
offset = int(addr - ksym_addrs[idx])
return ksym_names[idx] + hex(offset)
+
+ @staticmethod
+ def get_open_kprobes():
+ """ get_open_kprobes()
+
+ Get the number of open K[ret]probes. Can be useful for scenarios where
+ event_re is used while attaching and detaching probes
+ """
+ return len(open_kprobes)
diff --git a/tests/cc/CMakeLists.txt b/tests/cc/CMakeLists.txt
index 5eb82bd..a954f04 100644
--- a/tests/cc/CMakeLists.txt
+++ b/tests/cc/CMakeLists.txt
@@ -32,6 +32,8 @@
COMMAND ${TEST_WRAPPER} py_trace3_c sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_trace3.py test_trace3.c)
add_test(NAME py_test_trace4 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_trace4 sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_trace4.py)
+add_test(NAME py_test_probe_count WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
+ COMMAND ${TEST_WRAPPER} py_probe_count sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_probe_count.py)
add_test(NAME py_test_brb WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND ${TEST_WRAPPER} py_brb_c sudo ${CMAKE_CURRENT_SOURCE_DIR}/test_brb.py test_brb.c)
add_test(NAME py_test_brb2 WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
diff --git a/tests/cc/test_probe_count.py b/tests/cc/test_probe_count.py
new file mode 100755
index 0000000..cb12b4d
--- /dev/null
+++ b/tests/cc/test_probe_count.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python
+# Copyright (c) Suchakra Sharma <suchakrapani.sharma@polymtl.ca>
+# Licensed under the Apache License, Version 2.0 (the "License")
+
+from bcc import BPF
+import os
+import sys
+from unittest import main, TestCase
+
+class TestKprobeCnt(TestCase):
+ def setUp(self):
+ self.b = BPF(text="""
+ int wololo(void *ctx) {
+ return 0;
+ }
+ """)
+ self.b.attach_kprobe(event_re="^vfs_.*", fn_name="wololo")
+
+ def test_attach1(self):
+ actual_cnt = 0
+ with open("/sys/kernel/debug/tracing/available_filter_functions") as f:
+ for line in f:
+ if str(line).startswith("vfs_"):
+ actual_cnt += 1
+ open_cnt = self.b.get_open_kprobes()
+ self.assertEqual(actual_cnt, open_cnt)
+
+if __name__ == "__main__":
+ main()