Support dynamically enable profiling.

* Create method configureInstrumentation to enable/disable instrumentation based
  on the corresponding system property. This method will be called every
  time setHALInstrumention is invoked for any hal.
* Add setHALInstrumention to IBase.hal

Bug: 33923655
Test: make libhidlbase.
Change-Id: I130fb903f65783568a97308e291965960abab41d
diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp
index 3cc4b14..dce2585 100644
--- a/base/HidlSupport.cpp
+++ b/base/HidlSupport.cpp
@@ -153,18 +153,34 @@
 
 // ----------------------------------------------------------------------
 // HidlInstrumentor implementation.
-HidlInstrumentor::HidlInstrumentor(const std::string &prefix) {
-    mEnableInstrumentation = property_get_bool("hal.instrumentation.enable",
-                                               false);
-    if (mEnableInstrumentation) {
-        registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks);
-    }
+HidlInstrumentor::HidlInstrumentor(const std::string &prefix)
+        : mInstrumentationLibPrefix(prefix) {
+    configureInstrumentation(false);
 }
 
 HidlInstrumentor:: ~HidlInstrumentor() {}
 
+void HidlInstrumentor::configureInstrumentation(bool log) {
+    bool enable_instrumentation = property_get_bool(
+            "hal.instrumentation.enable",
+            false);
+    if (enable_instrumentation != mEnableInstrumentation) {
+        mEnableInstrumentation = enable_instrumentation;
+        if (mEnableInstrumentation) {
+            if (log) {
+                LOG(INFO) << "Enable instrumentation.";
+            }
+            registerInstrumentationCallbacks (&mInstrumentationCallbacks);
+        } else {
+            if (log) {
+                LOG(INFO) << "Disable instrumentation.";
+            }
+            mInstrumentationCallbacks.clear();
+        }
+    }
+}
+
 void HidlInstrumentor::registerInstrumentationCallbacks(
-        const std::string &profilerPrefix,
         std::vector<InstrumentationCallback> *instrumentationCallbacks) {
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
     std::vector<std::string> instrumentationLibPaths;
@@ -188,7 +204,7 @@
 
         struct dirent *file;
         while ((file = readdir(dir)) != NULL) {
-            if (!isInstrumentationLib(profilerPrefix, file))
+            if (!isInstrumentationLib(file))
                 continue;
 
             void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
@@ -224,13 +240,11 @@
 #endif
 }
 
-bool HidlInstrumentor::isInstrumentationLib(
-        const std::string &profiler_prefix,
-        const dirent *file) {
+bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
 #ifdef LIBHIDL_TARGET_DEBUGGABLE
     if (file->d_type != DT_REG) return false;
     std::cmatch cm;
-    std::regex e("^" + profiler_prefix + "(.*).profiler.so$");
+    std::regex e("^" + mInstrumentationLibPrefix + "(.*).profiler.so$");
     if (std::regex_match(file->d_name, cm, e)) return true;
 #endif
     return false;