diff options
author | Zhuoyao Zhang <zhuoyao@google.com> | 2017-02-16 17:28:54 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2017-02-16 17:28:55 +0000 |
commit | eba5145c8c4b7ad344bd277616d24dc20343a90f (patch) | |
tree | 7224ab80598421b27e1c347b425364c450ca80f1 | |
parent | a749556b39a7f92cdad61932904597c10b2f7116 (diff) | |
parent | 7a57de61a2017790b61ac211729a3b275f29c9ba (diff) | |
download | libhidl-eba5145c8c4b7ad344bd277616d24dc20343a90f.tar.gz |
Merge "Revert "Revert "Move HidlInstrumentor to HidlInternal and namespace details."""
-rw-r--r-- | base/HidlInternal.cpp | 115 | ||||
-rw-r--r-- | base/HidlSupport.cpp | 114 | ||||
-rw-r--r-- | base/include/hidl/HidlInternal.h | 82 | ||||
-rw-r--r-- | base/include/hidl/HidlSupport.h | 81 |
4 files changed, 197 insertions, 195 deletions
diff --git a/base/HidlInternal.cpp b/base/HidlInternal.cpp index 45afda3..9c0efbb 100644 --- a/base/HidlInternal.cpp +++ b/base/HidlInternal.cpp @@ -19,6 +19,14 @@ #include <hidl/HidlInternal.h> #include <android-base/logging.h> +#include <cutils/properties.h> + +#ifdef LIBHIDL_TARGET_DEBUGGABLE +#include <dirent.h> +#include <dlfcn.h> +#include <hidl-util/FQName.h> +#include <regex> +#endif namespace android { namespace hardware { @@ -28,6 +36,113 @@ void hidl_log_base::logAlwaysFatal(const char *message) const { LOG(FATAL) << message; } +// ---------------------------------------------------------------------- +// HidlInstrumentor implementation. +HidlInstrumentor::HidlInstrumentor( + const std::string &package, + const std::string &interface) + : mInstrumentationLibPackage(package), mInterfaceName(interface) { + 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( + std::vector<InstrumentationCallback> *instrumentationCallbacks) { +#ifdef LIBHIDL_TARGET_DEBUGGABLE + std::vector<std::string> instrumentationLibPaths; + char instrumentation_lib_path[PROPERTY_VALUE_MAX]; + if (property_get("hal.instrumentation.lib.path", + instrumentation_lib_path, + "") > 0) { + instrumentationLibPaths.push_back(instrumentation_lib_path); + } else { + instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); + instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); + instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); + } + + for (auto path : instrumentationLibPaths) { + DIR *dir = opendir(path.c_str()); + if (dir == 0) { + LOG(WARNING) << path << " does not exist. "; + return; + } + + struct dirent *file; + while ((file = readdir(dir)) != NULL) { + if (!isInstrumentationLib(file)) + continue; + + void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); + char *error; + if (handle == nullptr) { + LOG(WARNING) << "couldn't load file: " << file->d_name + << " error: " << dlerror(); + continue; + } + + dlerror(); /* Clear any existing error */ + + using cb_fun = void (*)( + const InstrumentationEvent, + const char *, + const char *, + const char *, + const char *, + std::vector<void *> *); + FQName package_name = FQName(mInstrumentationLibPackage); + auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" + + package_name.tokenName() + "_" + + mInterfaceName).c_str()); + if ((error = dlerror()) != NULL) { + LOG(WARNING) + << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" + << mInterfaceName << ", error: " << error; + continue; + } + instrumentationCallbacks->push_back(cb); + LOG(INFO) << "Register instrumentation callback from " + << file->d_name; + } + closedir(dir); + } +#else + // No-op for user builds. + return; +#endif +} + +bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { +#ifdef LIBHIDL_TARGET_DEBUGGABLE + if (file->d_type != DT_REG) return false; + std::cmatch cm; + std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); + if (std::regex_match(file->d_name, cm, e)) return true; +#endif + return false; +} + } // namespace details } // namespace hardware } // namespace android diff --git a/base/HidlSupport.cpp b/base/HidlSupport.cpp index 3bd68af..1544969 100644 --- a/base/HidlSupport.cpp +++ b/base/HidlSupport.cpp @@ -25,13 +25,6 @@ #include <vintf/VendorManifest.h> #include <vintf/parse_string.h> -#ifdef LIBHIDL_TARGET_DEBUGGABLE -#include <cutils/properties.h> -#include <dlfcn.h> -#include <regex> -#include <utility> -#endif - namespace android { namespace hardware { vintf::Transport getTransportForFrameworkPackages(const std::string &name) { @@ -316,113 +309,6 @@ bool hidl_string::empty() const { return mSize == 0; } -// ---------------------------------------------------------------------- -// HidlInstrumentor implementation. -HidlInstrumentor::HidlInstrumentor( - const std::string &package, - const std::string &interface) - : mInstrumentationLibPackage(package), mInterfaceName(interface) { - 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( - std::vector<InstrumentationCallback> *instrumentationCallbacks) { -#ifdef LIBHIDL_TARGET_DEBUGGABLE - std::vector<std::string> instrumentationLibPaths; - char instrumentation_lib_path[PROPERTY_VALUE_MAX]; - if (property_get("hal.instrumentation.lib.path", - instrumentation_lib_path, - "") > 0) { - instrumentationLibPaths.push_back(instrumentation_lib_path); - } else { - instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); - instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); - instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); - } - - for (auto path : instrumentationLibPaths) { - DIR *dir = opendir(path.c_str()); - if (dir == 0) { - LOG(WARNING) << path << " does not exist. "; - return; - } - - struct dirent *file; - while ((file = readdir(dir)) != NULL) { - if (!isInstrumentationLib(file)) - continue; - - void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); - char *error; - if (handle == nullptr) { - LOG(WARNING) << "couldn't load file: " << file->d_name - << " error: " << dlerror(); - continue; - } - - dlerror(); /* Clear any existing error */ - - using cb_fun = void (*)( - const InstrumentationEvent, - const char *, - const char *, - const char *, - const char *, - std::vector<void *> *); - FQName package_name = FQName(mInstrumentationLibPackage); - auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" - + package_name.tokenName() + "_" - + mInterfaceName).c_str()); - if ((error = dlerror()) != NULL) { - LOG(WARNING) - << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" - << mInterfaceName << ", error: " << error; - continue; - } - instrumentationCallbacks->push_back(cb); - LOG(INFO) << "Register instrumentation callback from " - << file->d_name; - } - closedir(dir); - } -#else - // No-op for user builds. - return; -#endif -} - -bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { -#ifdef LIBHIDL_TARGET_DEBUGGABLE - if (file->d_type != DT_REG) return false; - std::cmatch cm; - std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); - if (std::regex_match(file->d_name, cm, e)) return true; -#endif - return false; -} - } // namespace hardware } // namespace android diff --git a/base/include/hidl/HidlInternal.h b/base/include/hidl/HidlInternal.h index 24e17cb..80543c0 100644 --- a/base/include/hidl/HidlInternal.h +++ b/base/include/hidl/HidlInternal.h @@ -18,6 +18,10 @@ #define ANDROID_HIDL_INTERNAL_H #include <cstdint> +#include <dirent.h> +#include <functional> +#include <string> +#include <vector> #include <utility> namespace android { @@ -93,6 +97,84 @@ private: }; }; +#if defined(__LP64__) +#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" +#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" +#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" +#else +#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" +#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" +#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" +#endif + +// ---------------------------------------------------------------------- +// Class that provides Hidl instrumentation utilities. +struct HidlInstrumentor { + // 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, + PASSTHROUGH_ENTRY, + PASSTHROUGH_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)>; + + explicit HidlInstrumentor( + const std::string &package, + const std::string &insterface); + virtual ~HidlInstrumentor(); + + protected: + // Set mEnableInstrumentation based on system property + // hal.instrumentation.enable, register/de-register instrumentation + // callbacks if mEnableInstrumentation is true/false. + void configureInstrumentation(bool log=true); + // 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( + 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 dirent *file); + + // A list of registered instrumentation callbacks. + std::vector<InstrumentationCallback> mInstrumentationCallbacks; + // Flag whether to enable instrumentation. + bool mEnableInstrumentation; + // Prefix to lookup the instrumentation libraries. + std::string mInstrumentationLibPackage; + // Used for dlsym to load the profiling method for given interface. + std::string mInterfaceName; + +}; + } // namespace details } // namespace hardware } // namespace android diff --git a/base/include/hidl/HidlSupport.h b/base/include/hidl/HidlSupport.h index 6c00c3d..80f95b6 100644 --- a/base/include/hidl/HidlSupport.h +++ b/base/include/hidl/HidlSupport.h @@ -19,11 +19,8 @@ #include <algorithm> #include <array> -#include <dirent.h> #include <iterator> #include <cutils/native_handle.h> -#include <cutils/properties.h> -#include <functional> #include <hidl/HidlInternal.h> #include <hidl/Status.h> #include <map> @@ -795,84 +792,6 @@ inline android::hardware::hidl_version make_hidl_version(uint16_t major, uint16_ return hidl_version(major,minor); } -#if defined(__LP64__) -#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/" -#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/" -#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/" -#else -#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/" -#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/" -#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/" -#endif - -// ---------------------------------------------------------------------- -// Class that provides Hidl instrumentation utilities. -struct HidlInstrumentor { - // 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, - PASSTHROUGH_ENTRY, - PASSTHROUGH_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)>; - - explicit HidlInstrumentor( - const std::string &package, - const std::string &insterface); - virtual ~HidlInstrumentor(); - - protected: - // Set mEnableInstrumentation based on system property - // hal.instrumentation.enable, register/de-register instrumentation - // callbacks if mEnableInstrumentation is true/false. - void configureInstrumentation(bool log=true); - // 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( - 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 dirent *file); - - // A list of registered instrumentation callbacks. - std::vector<InstrumentationCallback> mInstrumentationCallbacks; - // Flag whether to enable instrumentation. - bool mEnableInstrumentation; - // Prefix to lookup the instrumentation libraries. - std::string mInstrumentationLibPackage; - // Used for dlsym to load the profiling method for given interface. - std::string mInterfaceName; - -}; - ///////////////////// toString functions namespace details { |