Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #define LOG_TAG "HidlInternal" |
| 18 | |
| 19 | #include <hidl/HidlInternal.h> |
| 20 | |
| 21 | #include <android-base/logging.h> |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 22 | #include <cutils/properties.h> |
| 23 | |
| 24 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 25 | #include <dirent.h> |
| 26 | #include <dlfcn.h> |
| 27 | #include <hidl-util/FQName.h> |
| 28 | #include <regex> |
| 29 | #endif |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | namespace details { |
| 34 | |
| 35 | void hidl_log_base::logAlwaysFatal(const char *message) const { |
| 36 | LOG(FATAL) << message; |
| 37 | } |
| 38 | |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 39 | // ---------------------------------------------------------------------- |
| 40 | // HidlInstrumentor implementation. |
| 41 | HidlInstrumentor::HidlInstrumentor( |
| 42 | const std::string &package, |
| 43 | const std::string &interface) |
| 44 | : mInstrumentationLibPackage(package), mInterfaceName(interface) { |
| 45 | configureInstrumentation(false); |
| 46 | } |
| 47 | |
| 48 | HidlInstrumentor:: ~HidlInstrumentor() {} |
| 49 | |
| 50 | void HidlInstrumentor::configureInstrumentation(bool log) { |
| 51 | bool enable_instrumentation = property_get_bool( |
| 52 | "hal.instrumentation.enable", |
| 53 | false); |
| 54 | if (enable_instrumentation != mEnableInstrumentation) { |
| 55 | mEnableInstrumentation = enable_instrumentation; |
| 56 | if (mEnableInstrumentation) { |
| 57 | if (log) { |
| 58 | LOG(INFO) << "Enable instrumentation."; |
| 59 | } |
| 60 | registerInstrumentationCallbacks (&mInstrumentationCallbacks); |
| 61 | } else { |
| 62 | if (log) { |
| 63 | LOG(INFO) << "Disable instrumentation."; |
| 64 | } |
| 65 | mInstrumentationCallbacks.clear(); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void HidlInstrumentor::registerInstrumentationCallbacks( |
| 71 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
| 72 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 73 | std::vector<std::string> instrumentationLibPaths; |
| 74 | char instrumentation_lib_path[PROPERTY_VALUE_MAX]; |
| 75 | if (property_get("hal.instrumentation.lib.path", |
| 76 | instrumentation_lib_path, |
| 77 | "") > 0) { |
| 78 | instrumentationLibPaths.push_back(instrumentation_lib_path); |
| 79 | } else { |
| 80 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
| 81 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 82 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 83 | } |
| 84 | |
| 85 | for (auto path : instrumentationLibPaths) { |
| 86 | DIR *dir = opendir(path.c_str()); |
| 87 | if (dir == 0) { |
| 88 | LOG(WARNING) << path << " does not exist. "; |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | struct dirent *file; |
| 93 | while ((file = readdir(dir)) != NULL) { |
| 94 | if (!isInstrumentationLib(file)) |
| 95 | continue; |
| 96 | |
| 97 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 98 | char *error; |
| 99 | if (handle == nullptr) { |
| 100 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 101 | << " error: " << dlerror(); |
| 102 | continue; |
| 103 | } |
| 104 | |
| 105 | dlerror(); /* Clear any existing error */ |
| 106 | |
| 107 | using cb_fun = void (*)( |
| 108 | const InstrumentationEvent, |
| 109 | const char *, |
| 110 | const char *, |
| 111 | const char *, |
| 112 | const char *, |
| 113 | std::vector<void *> *); |
| 114 | FQName package_name = FQName(mInstrumentationLibPackage); |
| 115 | auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" |
| 116 | + package_name.tokenName() + "_" |
| 117 | + mInterfaceName).c_str()); |
| 118 | if ((error = dlerror()) != NULL) { |
| 119 | LOG(WARNING) |
| 120 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" |
| 121 | << mInterfaceName << ", error: " << error; |
| 122 | continue; |
| 123 | } |
| 124 | instrumentationCallbacks->push_back(cb); |
| 125 | LOG(INFO) << "Register instrumentation callback from " |
| 126 | << file->d_name; |
| 127 | } |
| 128 | closedir(dir); |
| 129 | } |
| 130 | #else |
| 131 | // No-op for user builds. |
| 132 | return; |
| 133 | #endif |
| 134 | } |
| 135 | |
| 136 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
| 137 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 138 | if (file->d_type != DT_REG) return false; |
| 139 | std::cmatch cm; |
| 140 | std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); |
| 141 | if (std::regex_match(file->d_name, cm, e)) return true; |
| 142 | #endif |
| 143 | return false; |
| 144 | } |
| 145 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 146 | } // namespace details |
| 147 | } // namespace hardware |
| 148 | } // namespace android |