Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [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 | */ |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 16 | #define LOG_TAG "HidlSupport" |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 17 | |
| 18 | #include <hidl/HidlSupport.h> |
| 19 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 20 | #include <android-base/logging.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 21 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 22 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 23 | #include <cutils/properties.h> |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 24 | #include <dlfcn.h> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 25 | #include <regex> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 26 | #include <utility> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace hardware { |
| 31 | |
| 32 | static const char *const kEmptyString = ""; |
| 33 | |
| 34 | hidl_string::hidl_string() |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 35 | : mBuffer(kEmptyString), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 36 | mSize(0), |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 37 | mOwnsBuffer(false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | hidl_string::~hidl_string() { |
| 41 | clear(); |
| 42 | } |
| 43 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 44 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 45 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 48 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 49 | copyFrom(s, length); |
| 50 | } |
| 51 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 52 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 53 | copyFrom(other.c_str(), other.size()); |
| 54 | } |
| 55 | |
| 56 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 57 | copyFrom(s.c_str(), s.size()); |
| 58 | } |
| 59 | |
| 60 | hidl_string::hidl_string(hidl_string &&other): hidl_string() { |
| 61 | moveFrom(std::forward<hidl_string>(other)); |
| 62 | } |
| 63 | |
| 64 | hidl_string &hidl_string::operator=(hidl_string &&other) { |
| 65 | if (this != &other) { |
| 66 | clear(); |
| 67 | moveFrom(std::forward<hidl_string>(other)); |
| 68 | } |
| 69 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 73 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 74 | clear(); |
| 75 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return *this; |
| 79 | } |
| 80 | |
| 81 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 82 | clear(); |
| 83 | copyFrom(s, strlen(s)); |
| 84 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 87 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 88 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 89 | copyFrom(s.c_str(), s.size()); |
| 90 | return *this; |
| 91 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 92 | |
Steven Moreland | a2a8184 | 2017-01-20 14:48:15 -0800 | [diff] [blame^] | 93 | bool hidl_string::operator< (const hidl_string &rhs) const { |
| 94 | return strcmp(mBuffer, rhs.mBuffer) < 0; |
| 95 | } |
| 96 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 97 | hidl_string::operator std::string() const { |
| 98 | return std::string(mBuffer, mSize); |
| 99 | } |
| 100 | |
| 101 | hidl_string::operator const char *() const { |
| 102 | return mBuffer; |
| 103 | } |
| 104 | |
| 105 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 106 | // assume my resources are freed. |
| 107 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 108 | if (size > UINT32_MAX) { |
| 109 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 110 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 111 | char *buf = (char *)malloc(size + 1); |
| 112 | memcpy(buf, data, size); |
| 113 | buf[size] = '\0'; |
| 114 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 115 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 116 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 117 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 118 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 119 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 120 | void hidl_string::moveFrom(hidl_string &&other) { |
| 121 | // assume my resources are freed. |
| 122 | |
| 123 | mBuffer = other.mBuffer; |
| 124 | mSize = other.mSize; |
| 125 | mOwnsBuffer = other.mOwnsBuffer; |
| 126 | |
| 127 | other.mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | void hidl_string::clear() { |
| 131 | if (mOwnsBuffer && (mBuffer != kEmptyString)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 132 | free(const_cast<char *>(static_cast<const char *>(mBuffer))); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 133 | } |
| 134 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 135 | mBuffer = kEmptyString; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 136 | mSize = 0; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 137 | mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | void hidl_string::setToExternal(const char *data, size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 141 | if (size > UINT32_MAX) { |
| 142 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 143 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 144 | clear(); |
| 145 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 146 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 147 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 148 | mOwnsBuffer = false; |
| 149 | } |
| 150 | |
| 151 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 152 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | size_t hidl_string::size() const { |
| 156 | return mSize; |
| 157 | } |
| 158 | |
| 159 | bool hidl_string::empty() const { |
| 160 | return mSize == 0; |
| 161 | } |
| 162 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 163 | // ---------------------------------------------------------------------- |
| 164 | // HidlInstrumentor implementation. |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 165 | HidlInstrumentor::HidlInstrumentor(const std::string &prefix) |
| 166 | : mInstrumentationLibPrefix(prefix) { |
| 167 | configureInstrumentation(false); |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 168 | } |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 169 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 170 | HidlInstrumentor:: ~HidlInstrumentor() {} |
| 171 | |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 172 | void HidlInstrumentor::configureInstrumentation(bool log) { |
| 173 | bool enable_instrumentation = property_get_bool( |
| 174 | "hal.instrumentation.enable", |
| 175 | false); |
| 176 | if (enable_instrumentation != mEnableInstrumentation) { |
| 177 | mEnableInstrumentation = enable_instrumentation; |
| 178 | if (mEnableInstrumentation) { |
| 179 | if (log) { |
| 180 | LOG(INFO) << "Enable instrumentation."; |
| 181 | } |
| 182 | registerInstrumentationCallbacks (&mInstrumentationCallbacks); |
| 183 | } else { |
| 184 | if (log) { |
| 185 | LOG(INFO) << "Disable instrumentation."; |
| 186 | } |
| 187 | mInstrumentationCallbacks.clear(); |
| 188 | } |
| 189 | } |
| 190 | } |
| 191 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 192 | void HidlInstrumentor::registerInstrumentationCallbacks( |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 193 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 194 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 195 | std::vector<std::string> instrumentationLibPaths; |
Zhuoyao Zhang | 8bed09f | 2016-10-26 18:12:47 -0700 | [diff] [blame] | 196 | char instrumentation_lib_path[PROPERTY_VALUE_MAX]; |
| 197 | if (property_get("hal.instrumentation.lib.path", |
| 198 | instrumentation_lib_path, |
| 199 | "") > 0) { |
| 200 | instrumentationLibPaths.push_back(instrumentation_lib_path); |
| 201 | } else { |
| 202 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
| 203 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 204 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 205 | } |
| 206 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 207 | for (auto path : instrumentationLibPaths) { |
| 208 | DIR *dir = opendir(path.c_str()); |
| 209 | if (dir == 0) { |
Steven Moreland | 7096f54 | 2016-11-07 11:20:33 -0800 | [diff] [blame] | 210 | LOG(WARNING) << path << " does not exist. "; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 211 | return; |
| 212 | } |
| 213 | |
| 214 | struct dirent *file; |
| 215 | while ((file = readdir(dir)) != NULL) { |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 216 | if (!isInstrumentationLib(file)) |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 217 | continue; |
| 218 | |
| 219 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 220 | if (handle == nullptr) { |
| 221 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 222 | << " error: " << dlerror(); |
| 223 | continue; |
| 224 | } |
| 225 | using cb_fun = void (*)( |
| 226 | const InstrumentationEvent, |
| 227 | const char *, |
| 228 | const char *, |
| 229 | const char *, |
| 230 | const char *, |
| 231 | std::vector<void *> *); |
| 232 | auto cb = (cb_fun)dlsym(handle, "HIDL_INSTRUMENTATION_FUNCTION"); |
| 233 | if (cb == nullptr) { |
| 234 | LOG(WARNING) |
| 235 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION, " |
| 236 | "error: " |
| 237 | << dlerror(); |
| 238 | continue; |
| 239 | } |
| 240 | instrumentationCallbacks->push_back(cb); |
| 241 | LOG(INFO) << "Register instrumentation callback from " |
| 242 | << file->d_name; |
| 243 | } |
| 244 | closedir(dir); |
| 245 | } |
| 246 | #else |
| 247 | // No-op for user builds. |
| 248 | return; |
| 249 | #endif |
| 250 | } |
| 251 | |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 252 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 253 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 254 | if (file->d_type != DT_REG) return false; |
| 255 | std::cmatch cm; |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 256 | std::regex e("^" + mInstrumentationLibPrefix + "(.*).profiler.so$"); |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 257 | if (std::regex_match(file->d_name, cm, e)) return true; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 258 | #endif |
| 259 | return false; |
| 260 | } |
| 261 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 262 | } // namespace hardware |
| 263 | } // namespace android |
| 264 | |
| 265 | |