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