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