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> |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 22 | #include <android-base/properties.h> |
| 23 | #include <android-base/stringprintf.h> |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 24 | |
| 25 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 26 | #include <dirent.h> |
| 27 | #include <dlfcn.h> |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 28 | #include <link.h> |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 29 | #include <utils/misc.h> |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 30 | #include <regex> |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 31 | |
| 32 | extern "C" __attribute__((weak)) void __sanitizer_cov_dump(); |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame] | 33 | |
| 34 | const char kGcovPrefixEnvVar[] = "GCOV_PREFIX"; |
| 35 | const char kGcovPrefixOverrideEnvVar[] = "GCOV_PREFIX_OVERRIDE"; |
| 36 | const char kGcovPrefixPath[] = "/data/misc/trace/"; |
| 37 | const char kSysPropHalCoverage[] = "hal.coverage.enable"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 38 | #if defined(__LP64__) |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame] | 39 | const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.64"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 40 | #else |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame] | 41 | const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.32"; |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 42 | #endif |
Logan Chien | 73fe94d | 2018-10-23 13:44:45 +0800 | [diff] [blame] | 43 | #endif // LIBHIDL_TARGET_DEBUGGABLE |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 44 | |
| 45 | namespace android { |
| 46 | namespace hardware { |
| 47 | namespace details { |
| 48 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 49 | void logAlwaysFatal(const char* message) { |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 50 | LOG(FATAL) << message; |
| 51 | } |
| 52 | |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 53 | std::string getVndkVersionStr() { |
Jooyung Han | 0268c93 | 2020-04-07 16:21:35 +0900 | [diff] [blame] | 54 | static std::string vndkVersion = base::GetProperty("ro.vndk.version", ""); |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 55 | return vndkVersion; |
| 56 | } |
| 57 | |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 58 | // ---------------------------------------------------------------------- |
| 59 | // HidlInstrumentor implementation. |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 60 | HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface) |
| 61 | : mEnableInstrumentation(false), |
| 62 | mInstrumentationLibPackage(package), |
| 63 | mInterfaceName(interface) { |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 64 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 65 | configureInstrumentation(false); |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 66 | if (__sanitizer_cov_dump != nullptr) { |
| 67 | ::android::add_sysprop_change_callback( |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 68 | []() { |
| 69 | bool enableCoverage = base::GetBoolProperty(kSysPropHalCoverage, false); |
| 70 | if (enableCoverage) { |
| 71 | __sanitizer_cov_dump(); |
| 72 | } |
| 73 | }, |
| 74 | 0); |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 75 | } |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 76 | if (base::GetBoolProperty("ro.vts.coverage", false)) { |
Ryan Campbell | de6c76a | 2017-11-14 18:15:13 -0800 | [diff] [blame] | 77 | const char* prefixOverride = getenv(kGcovPrefixOverrideEnvVar); |
| 78 | if (prefixOverride == nullptr || strcmp(prefixOverride, "true") != 0) { |
| 79 | const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid()); |
| 80 | setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */); |
| 81 | } |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 82 | ::android::add_sysprop_change_callback( |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 83 | []() { |
| 84 | const bool enableCoverage = base::GetBoolProperty(kSysPropHalCoverage, false); |
| 85 | if (enableCoverage) { |
| 86 | dl_iterate_phdr( |
| 87 | [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) { |
| 88 | if (strlen(info->dlpi_name) == 0) return 0; |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 89 | |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 90 | void* handle = dlopen(info->dlpi_name, RTLD_LAZY); |
| 91 | if (handle == nullptr) { |
| 92 | LOG(INFO) << "coverage dlopen failed: " << dlerror(); |
| 93 | return 0; |
| 94 | } |
| 95 | void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush"); |
| 96 | if (flush == nullptr) { |
| 97 | return 0; |
| 98 | } |
| 99 | flush(); |
| 100 | return 0; |
| 101 | }, |
| 102 | nullptr /* data */); |
| 103 | } |
| 104 | }, |
| 105 | 0 /* priority */); |
Ryan Campbell | 21d59d9 | 2017-10-19 14:12:49 -0700 | [diff] [blame] | 106 | } |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 107 | #endif |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 108 | } |
| 109 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame] | 110 | HidlInstrumentor::~HidlInstrumentor() {} |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 111 | |
| 112 | void HidlInstrumentor::configureInstrumentation(bool log) { |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 113 | mEnableInstrumentation = base::GetBoolProperty("hal.instrumentation.enable", false); |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 114 | if (mEnableInstrumentation) { |
| 115 | if (log) { |
| 116 | LOG(INFO) << "Enable instrumentation."; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 117 | } |
Zhuoyao Zhang | e8f8259 | 2018-02-01 16:18:15 -0800 | [diff] [blame] | 118 | mInstrumentationCallbacks.clear(); |
| 119 | registerInstrumentationCallbacks(&mInstrumentationCallbacks); |
| 120 | } else { |
| 121 | if (log) { |
| 122 | LOG(INFO) << "Disable instrumentation."; |
| 123 | } |
| 124 | mInstrumentationCallbacks.clear(); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
| 128 | void HidlInstrumentor::registerInstrumentationCallbacks( |
| 129 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
| 130 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 131 | std::vector<std::string> instrumentationLibPaths; |
Steven Moreland | 2056cf3 | 2019-09-17 17:41:02 -0700 | [diff] [blame] | 132 | const std::string instrumentationLibPath = base::GetProperty(kSysPropInstrumentationPath, ""); |
| 133 | if (instrumentationLibPath.size() > 0) { |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 134 | instrumentationLibPaths.push_back(instrumentationLibPath); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 135 | } else { |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 136 | static std::string halLibPathVndkSp = android::base::StringPrintf( |
| 137 | HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, getVndkVersionStr().c_str()); |
Justin Yun | 6a323ed | 2018-10-18 18:41:56 +0900 | [diff] [blame] | 138 | #ifndef __ANDROID_VNDK__ |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 139 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
Justin Yun | 6a323ed | 2018-10-18 18:41:56 +0900 | [diff] [blame] | 140 | #endif |
Justin Yun | 1f04810 | 2017-12-01 15:30:08 +0900 | [diff] [blame] | 141 | instrumentationLibPaths.push_back(halLibPathVndkSp); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 142 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 143 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 144 | } |
| 145 | |
Chih-Hung Hsieh | 41649d5 | 2017-08-03 14:27:21 -0700 | [diff] [blame] | 146 | for (const auto& path : instrumentationLibPaths) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 147 | DIR *dir = opendir(path.c_str()); |
Yi Kong | 277f977 | 2018-07-24 14:59:46 -0700 | [diff] [blame] | 148 | if (dir == nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 149 | LOG(WARNING) << path << " does not exist. "; |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | struct dirent *file; |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 154 | while ((file = readdir(dir)) != nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 155 | if (!isInstrumentationLib(file)) |
| 156 | continue; |
| 157 | |
| 158 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 159 | char *error; |
| 160 | if (handle == nullptr) { |
| 161 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 162 | << " error: " << dlerror(); |
| 163 | continue; |
| 164 | } |
| 165 | |
| 166 | dlerror(); /* Clear any existing error */ |
| 167 | |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 168 | using cbFun = void (*)( |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 169 | const InstrumentationEvent, |
| 170 | const char *, |
| 171 | const char *, |
| 172 | const char *, |
| 173 | const char *, |
| 174 | std::vector<void *> *); |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 175 | std::string package = mInstrumentationLibPackage; |
| 176 | for (size_t i = 0; i < package.size(); i++) { |
| 177 | if (package[i] == '.') { |
| 178 | package[i] = '_'; |
| 179 | continue; |
| 180 | } |
| 181 | |
| 182 | if (package[i] == '@') { |
| 183 | package[i] = '_'; |
| 184 | package.insert(i + 1, "V"); |
| 185 | continue; |
| 186 | } |
| 187 | } |
Steven Moreland | 384792b | 2017-06-19 18:24:40 -0700 | [diff] [blame] | 188 | auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 189 | + package + "_" + mInterfaceName).c_str()); |
Stephen Hines | fd9ecee | 2017-09-27 18:52:52 -0700 | [diff] [blame] | 190 | if ((error = dlerror()) != nullptr) { |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 191 | LOG(WARNING) |
| 192 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | 819c05d | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 193 | << package << "_" << mInterfaceName << ", error: " << error; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 194 | continue; |
| 195 | } |
| 196 | instrumentationCallbacks->push_back(cb); |
| 197 | LOG(INFO) << "Register instrumentation callback from " |
| 198 | << file->d_name; |
| 199 | } |
| 200 | closedir(dir); |
| 201 | } |
| 202 | #else |
| 203 | // No-op for user builds. |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 204 | (void) instrumentationCallbacks; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 205 | return; |
| 206 | #endif |
| 207 | } |
| 208 | |
| 209 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
| 210 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 211 | if (file->d_type != DT_REG) return false; |
| 212 | std::cmatch cm; |
| 213 | std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); |
| 214 | if (std::regex_match(file->d_name, cm, e)) return true; |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 215 | #else |
| 216 | (void) file; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 217 | #endif |
| 218 | return false; |
| 219 | } |
| 220 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 221 | } // namespace details |
| 222 | } // namespace hardware |
| 223 | } // namespace android |