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