Add utility methods to support hidl instrumentation.

* Add method registerInstrumentationCallback which looks up and
  dynamically loads the hidl instrumentation libraries and registers
  the instrumentation callbacks.
* The method looks up interumentation libraries under directories:
  /system/lib(64)/hw, /vender/lib(64)/hw, /odm/lib(64)/hw.
* The method only loads libraries with a particualr name pattern, e.g.
  android.hardware.nfc@1.0.*.profiler.so
* The method is a no-op for user builds.

Bug: 31266145
Test: test pass with hidl_test.

Change-Id: Id11486e36081d75481a45836219f84ddf5b112d7
diff --git a/include/hidl/HidlSupport.h b/include/hidl/HidlSupport.h
index 347d9e4..6b88664 100644
--- a/include/hidl/HidlSupport.h
+++ b/include/hidl/HidlSupport.h
@@ -18,6 +18,7 @@
 #define ANDROID_HIDL_SUPPORT_H
 
 #include <algorithm>
+#include <dirent.h>
 #include <dlfcn.h>
 #include <hwbinder/Parcel.h>
 #include <utils/Errors.h>
@@ -286,6 +287,53 @@
                               I##INTERFACE::version);                                    \
     }
 
+// ----------------------------------------------------------------------
+// Hidl instrumentation utilities.
+
+// Event that triggers the instrumentation. e.g. enter of an API call on
+// the server/client side, exit of an API call on the server/client side etc.
+enum InstrumentationEvent {
+    SERVER_API_ENTRY = 0,
+    SERVER_API_EXIT,
+    CLIENT_API_ENTRY,
+    CLIENT_API_EXIT,
+    SYNC_CALLBACK_ENTRY,
+    SYNC_CALLBACK_EXIT,
+    ASYNC_CALLBACK_ENTRY,
+    ASYNC_CALLBACK_EXIT,
+};
+
+// Signature of the instrumentation callback function.
+using InstrumentationCallback = std::function<void(
+        const InstrumentationEvent event,
+        const char *package,
+        const char *version,
+        const char *interface,
+        const char *method,
+        std::vector<void *> *args)>;
+
+// Function that lookup and dynamically loads the hidl instrumentation libraries
+// and registers the instrumentation callback functions.
+//
+// The instrumentation libraries should be stored under any of the following
+// directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
+// HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should follow
+// pattern: ^profilerPrefix(.*).profiler.so$
+//
+// Each instrumentation library is expected to implement the instrumentation
+// function called HIDL_INSTRUMENTATION_FUNCTION.
+//
+// A no-op for user build.
+void registerInstrumentationCallbacks(
+        const std::string &profilerPrefix,
+        std::vector<InstrumentationCallback> *instrumentationCallbacks);
+
+// Utility function to determine whether a give file is a instrumentation
+// library (i.e. the file name follow the expected pattern).
+bool isInstrumentationLib(
+        const std::string &profilerPrefix,
+        const dirent *file);
+
 }  // namespace hardware
 }  // namespace android