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> |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 27 | #include <regex> |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame^] | 28 | #include <utils/misc.h> |
| 29 | |
| 30 | extern "C" __attribute__((weak)) void __sanitizer_cov_dump(); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 31 | #endif |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace hardware { |
| 35 | namespace details { |
| 36 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame^] | 37 | void logAlwaysFatal(const char* message) { |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 38 | LOG(FATAL) << message; |
| 39 | } |
| 40 | |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 41 | // ---------------------------------------------------------------------- |
| 42 | // HidlInstrumentor implementation. |
| 43 | HidlInstrumentor::HidlInstrumentor( |
| 44 | const std::string &package, |
| 45 | const std::string &interface) |
| 46 | : mInstrumentationLibPackage(package), mInterfaceName(interface) { |
| 47 | configureInstrumentation(false); |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame^] | 48 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 49 | if (__sanitizer_cov_dump != nullptr) { |
| 50 | ::android::add_sysprop_change_callback( |
| 51 | []() { |
| 52 | bool enableCoverage = property_get_bool("hal.coverage.enable", false); |
| 53 | if (enableCoverage) { |
| 54 | __sanitizer_cov_dump(); |
| 55 | } |
| 56 | }, |
| 57 | 0); |
| 58 | } |
| 59 | #endif |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Ryan Campbell | 69aff75 | 2017-07-27 13:49:46 -0700 | [diff] [blame^] | 62 | HidlInstrumentor::~HidlInstrumentor() {} |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 63 | |
| 64 | void HidlInstrumentor::configureInstrumentation(bool log) { |
| 65 | bool enable_instrumentation = property_get_bool( |
| 66 | "hal.instrumentation.enable", |
| 67 | false); |
| 68 | if (enable_instrumentation != mEnableInstrumentation) { |
| 69 | mEnableInstrumentation = enable_instrumentation; |
| 70 | if (mEnableInstrumentation) { |
| 71 | if (log) { |
| 72 | LOG(INFO) << "Enable instrumentation."; |
| 73 | } |
| 74 | registerInstrumentationCallbacks (&mInstrumentationCallbacks); |
| 75 | } else { |
| 76 | if (log) { |
| 77 | LOG(INFO) << "Disable instrumentation."; |
| 78 | } |
| 79 | mInstrumentationCallbacks.clear(); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | void HidlInstrumentor::registerInstrumentationCallbacks( |
| 85 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
| 86 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 87 | std::vector<std::string> instrumentationLibPaths; |
| 88 | char instrumentation_lib_path[PROPERTY_VALUE_MAX]; |
| 89 | if (property_get("hal.instrumentation.lib.path", |
| 90 | instrumentation_lib_path, |
| 91 | "") > 0) { |
| 92 | instrumentationLibPaths.push_back(instrumentation_lib_path); |
| 93 | } else { |
| 94 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
| 95 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 96 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 97 | } |
| 98 | |
| 99 | for (auto path : instrumentationLibPaths) { |
| 100 | DIR *dir = opendir(path.c_str()); |
| 101 | if (dir == 0) { |
| 102 | LOG(WARNING) << path << " does not exist. "; |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | struct dirent *file; |
| 107 | while ((file = readdir(dir)) != NULL) { |
| 108 | if (!isInstrumentationLib(file)) |
| 109 | continue; |
| 110 | |
| 111 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 112 | char *error; |
| 113 | if (handle == nullptr) { |
| 114 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 115 | << " error: " << dlerror(); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | dlerror(); /* Clear any existing error */ |
| 120 | |
| 121 | using cb_fun = void (*)( |
| 122 | const InstrumentationEvent, |
| 123 | const char *, |
| 124 | const char *, |
| 125 | const char *, |
| 126 | const char *, |
| 127 | std::vector<void *> *); |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 128 | std::string package = mInstrumentationLibPackage; |
| 129 | for (size_t i = 0; i < package.size(); i++) { |
| 130 | if (package[i] == '.') { |
| 131 | package[i] = '_'; |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | if (package[i] == '@') { |
| 136 | package[i] = '_'; |
| 137 | package.insert(i + 1, "V"); |
| 138 | continue; |
| 139 | } |
| 140 | } |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 141 | auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 142 | + package + "_" + mInterfaceName).c_str()); |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 143 | if ((error = dlerror()) != NULL) { |
| 144 | LOG(WARNING) |
| 145 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_" |
Steven Moreland | da8e617 | 2017-04-06 17:24:22 -0700 | [diff] [blame] | 146 | << package << "_" << mInterfaceName << ", error: " << error; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 147 | continue; |
| 148 | } |
| 149 | instrumentationCallbacks->push_back(cb); |
| 150 | LOG(INFO) << "Register instrumentation callback from " |
| 151 | << file->d_name; |
| 152 | } |
| 153 | closedir(dir); |
| 154 | } |
| 155 | #else |
| 156 | // No-op for user builds. |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 157 | (void) instrumentationCallbacks; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 158 | return; |
| 159 | #endif |
| 160 | } |
| 161 | |
| 162 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
| 163 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
| 164 | if (file->d_type != DT_REG) return false; |
| 165 | std::cmatch cm; |
| 166 | std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$"); |
| 167 | if (std::regex_match(file->d_name, cm, e)) return true; |
Steven Moreland | ead33e2 | 2017-02-21 19:19:39 -0800 | [diff] [blame] | 168 | #else |
| 169 | (void) file; |
Zhuoyao Zhang | 7a57de6 | 2017-02-15 21:04:19 +0000 | [diff] [blame] | 170 | #endif |
| 171 | return false; |
| 172 | } |
| 173 | |
Steven Moreland | 337c3ae | 2016-11-22 13:37:32 -0800 | [diff] [blame] | 174 | } // namespace details |
| 175 | } // namespace hardware |
| 176 | } // namespace android |