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> |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 21 | #include <vintf/VendorManifest.h> |
| 22 | #include <vintf/parse_string.h> |
Martijn Coenen | 3079100 | 2016-12-01 15:40:46 +0100 | [diff] [blame] | 23 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 24 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 25 | #include <cutils/properties.h> |
Steven Moreland | 337e6b6 | 2017-01-18 17:25:13 -0800 | [diff] [blame] | 26 | #include <dlfcn.h> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 27 | #include <regex> |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 28 | #include <utility> |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 29 | #endif |
| 30 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 31 | namespace android { |
| 32 | namespace hardware { |
| 33 | |
Yifan Hong | 44c0e57 | 2017-01-20 15:41:52 -0800 | [diff] [blame] | 34 | vintf::Transport getTransportFromManifest(const std::string &package) { |
| 35 | const vintf::VendorManifest *vm = vintf::VendorManifest::Get(); |
| 36 | if (vm == nullptr) { |
| 37 | LOG(ERROR) << "getTransportFromManifest: Cannot find vendor interface manifest."; |
| 38 | return vintf::Transport::EMPTY; |
| 39 | } |
| 40 | vintf::Transport tr = vm->getTransport(package); |
| 41 | if (tr == vintf::Transport::EMPTY) { |
| 42 | LOG(WARNING) << "getTransportFromManifest: Cannot find entry " |
| 43 | << package << " in vendor interface manifest."; |
| 44 | } else { |
| 45 | LOG(INFO) << "getTransportFromManifest: " << package |
| 46 | << " declares transport method " << to_string(tr); |
| 47 | } |
| 48 | return tr; |
| 49 | } |
| 50 | |
Martijn Coenen | 04b91c0 | 2017-01-19 14:14:21 +0100 | [diff] [blame^] | 51 | hidl_handle::hidl_handle() { |
| 52 | mHandle = nullptr; |
| 53 | mOwnsHandle = false; |
| 54 | } |
| 55 | |
| 56 | hidl_handle::~hidl_handle() { |
| 57 | freeHandle(); |
| 58 | } |
| 59 | |
| 60 | hidl_handle::hidl_handle(const native_handle_t *handle) { |
| 61 | mHandle = handle; |
| 62 | mOwnsHandle = false; |
| 63 | } |
| 64 | |
| 65 | // copy constructor. |
| 66 | hidl_handle::hidl_handle(const hidl_handle &other) { |
| 67 | mOwnsHandle = false; |
| 68 | *this = other; |
| 69 | } |
| 70 | |
| 71 | // move constructor. |
| 72 | hidl_handle::hidl_handle(hidl_handle &&other) { |
| 73 | mOwnsHandle = false; |
| 74 | *this = std::move(other); |
| 75 | } |
| 76 | |
| 77 | // assignment operators |
| 78 | hidl_handle &hidl_handle::operator=(const hidl_handle &other) { |
| 79 | if (this == &other) { |
| 80 | return *this; |
| 81 | } |
| 82 | freeHandle(); |
| 83 | if (other.mHandle != nullptr) { |
| 84 | mHandle = native_handle_clone(other.mHandle); |
| 85 | if (mHandle == nullptr) { |
| 86 | LOG(FATAL) << "Failed to clone native_handle in hidl_handle."; |
| 87 | } |
| 88 | mOwnsHandle = true; |
| 89 | } else { |
| 90 | mHandle = nullptr; |
| 91 | mOwnsHandle = false; |
| 92 | } |
| 93 | return *this; |
| 94 | } |
| 95 | |
| 96 | hidl_handle &hidl_handle::operator=(const native_handle_t *native_handle) { |
| 97 | freeHandle(); |
| 98 | mHandle = native_handle; |
| 99 | mOwnsHandle = false; |
| 100 | return *this; |
| 101 | } |
| 102 | |
| 103 | hidl_handle &hidl_handle::operator=(hidl_handle &&other) { |
| 104 | if (this != &other) { |
| 105 | freeHandle(); |
| 106 | mHandle = other.mHandle; |
| 107 | mOwnsHandle = other.mOwnsHandle; |
| 108 | other.mHandle = nullptr; |
| 109 | other.mOwnsHandle = false; |
| 110 | } |
| 111 | return *this; |
| 112 | } |
| 113 | |
| 114 | void hidl_handle::setTo(native_handle_t* handle, bool shouldOwn) { |
| 115 | mHandle = handle; |
| 116 | mOwnsHandle = shouldOwn; |
| 117 | } |
| 118 | |
| 119 | const native_handle_t* hidl_handle::operator->() const { |
| 120 | return mHandle; |
| 121 | } |
| 122 | |
| 123 | // implicit conversion to const native_handle_t* |
| 124 | hidl_handle::operator const native_handle_t *() const { |
| 125 | return mHandle; |
| 126 | } |
| 127 | |
| 128 | // explicit conversion |
| 129 | const native_handle_t *hidl_handle::getNativeHandle() const { |
| 130 | return mHandle; |
| 131 | } |
| 132 | |
| 133 | void hidl_handle::freeHandle() { |
| 134 | if (mOwnsHandle && mHandle != nullptr) { |
| 135 | // This can only be true if: |
| 136 | // 1. Somebody called setTo() with shouldOwn=true, so we know the handle |
| 137 | // wasn't const to begin with. |
| 138 | // 2. Copy/assignment from another hidl_handle, in which case we have |
| 139 | // cloned the handle. |
| 140 | // 3. Move constructor from another hidl_handle, in which case the original |
| 141 | // hidl_handle must have been non-const as well. |
| 142 | native_handle_t *handle = const_cast<native_handle_t*>( |
| 143 | static_cast<const native_handle_t*>(mHandle)); |
| 144 | native_handle_close(handle); |
| 145 | native_handle_delete(handle); |
| 146 | mHandle = nullptr; |
| 147 | } |
| 148 | } |
| 149 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 150 | static const char *const kEmptyString = ""; |
| 151 | |
| 152 | hidl_string::hidl_string() |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 153 | : mBuffer(kEmptyString), |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 154 | mSize(0), |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 155 | mOwnsBuffer(false) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | hidl_string::~hidl_string() { |
| 159 | clear(); |
| 160 | } |
| 161 | |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 162 | hidl_string::hidl_string(const char *s) : hidl_string() { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 163 | copyFrom(s, strlen(s)); |
Steven Moreland | e03c087 | 2016-10-24 10:43:50 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Steven Moreland | 53120f7 | 2017-01-12 09:39:26 -0800 | [diff] [blame] | 166 | hidl_string::hidl_string(const char *s, size_t length) : hidl_string() { |
| 167 | copyFrom(s, length); |
| 168 | } |
| 169 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 170 | hidl_string::hidl_string(const hidl_string &other): hidl_string() { |
| 171 | copyFrom(other.c_str(), other.size()); |
| 172 | } |
| 173 | |
| 174 | hidl_string::hidl_string(const std::string &s) : hidl_string() { |
| 175 | copyFrom(s.c_str(), s.size()); |
| 176 | } |
| 177 | |
| 178 | hidl_string::hidl_string(hidl_string &&other): hidl_string() { |
| 179 | moveFrom(std::forward<hidl_string>(other)); |
| 180 | } |
| 181 | |
| 182 | hidl_string &hidl_string::operator=(hidl_string &&other) { |
| 183 | if (this != &other) { |
| 184 | clear(); |
| 185 | moveFrom(std::forward<hidl_string>(other)); |
| 186 | } |
| 187 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | hidl_string &hidl_string::operator=(const hidl_string &other) { |
| 191 | if (this != &other) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 192 | clear(); |
| 193 | copyFrom(other.c_str(), other.size()); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | return *this; |
| 197 | } |
| 198 | |
| 199 | hidl_string &hidl_string::operator=(const char *s) { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 200 | clear(); |
| 201 | copyFrom(s, strlen(s)); |
| 202 | return *this; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 203 | } |
| 204 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 205 | hidl_string &hidl_string::operator=(const std::string &s) { |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 206 | clear(); |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 207 | copyFrom(s.c_str(), s.size()); |
| 208 | return *this; |
| 209 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 210 | |
Steven Moreland | a2a8184 | 2017-01-20 14:48:15 -0800 | [diff] [blame] | 211 | bool hidl_string::operator< (const hidl_string &rhs) const { |
| 212 | return strcmp(mBuffer, rhs.mBuffer) < 0; |
| 213 | } |
| 214 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 215 | hidl_string::operator std::string() const { |
| 216 | return std::string(mBuffer, mSize); |
| 217 | } |
| 218 | |
| 219 | hidl_string::operator const char *() const { |
| 220 | return mBuffer; |
| 221 | } |
| 222 | |
| 223 | void hidl_string::copyFrom(const char *data, size_t size) { |
| 224 | // assume my resources are freed. |
| 225 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 226 | if (size > UINT32_MAX) { |
| 227 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 228 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 229 | char *buf = (char *)malloc(size + 1); |
| 230 | memcpy(buf, data, size); |
| 231 | buf[size] = '\0'; |
| 232 | mBuffer = buf; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 233 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 234 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 235 | mOwnsBuffer = true; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 236 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 237 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 238 | void hidl_string::moveFrom(hidl_string &&other) { |
| 239 | // assume my resources are freed. |
| 240 | |
| 241 | mBuffer = other.mBuffer; |
| 242 | mSize = other.mSize; |
| 243 | mOwnsBuffer = other.mOwnsBuffer; |
| 244 | |
| 245 | other.mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | void hidl_string::clear() { |
| 249 | if (mOwnsBuffer && (mBuffer != kEmptyString)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 250 | free(const_cast<char *>(static_cast<const char *>(mBuffer))); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 251 | } |
| 252 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 253 | mBuffer = kEmptyString; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 254 | mSize = 0; |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 255 | mOwnsBuffer = false; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 256 | } |
| 257 | |
| 258 | void hidl_string::setToExternal(const char *data, size_t size) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 259 | if (size > UINT32_MAX) { |
| 260 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 261 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 262 | clear(); |
| 263 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 264 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 265 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 266 | mOwnsBuffer = false; |
| 267 | } |
| 268 | |
| 269 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 270 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | size_t hidl_string::size() const { |
| 274 | return mSize; |
| 275 | } |
| 276 | |
| 277 | bool hidl_string::empty() const { |
| 278 | return mSize == 0; |
| 279 | } |
| 280 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 281 | // ---------------------------------------------------------------------- |
| 282 | // HidlInstrumentor implementation. |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 283 | HidlInstrumentor::HidlInstrumentor(const std::string &prefix) |
| 284 | : mInstrumentationLibPrefix(prefix) { |
| 285 | configureInstrumentation(false); |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 286 | } |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 287 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 288 | HidlInstrumentor:: ~HidlInstrumentor() {} |
| 289 | |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 290 | void HidlInstrumentor::configureInstrumentation(bool log) { |
| 291 | bool enable_instrumentation = property_get_bool( |
| 292 | "hal.instrumentation.enable", |
| 293 | false); |
| 294 | if (enable_instrumentation != mEnableInstrumentation) { |
| 295 | mEnableInstrumentation = enable_instrumentation; |
| 296 | if (mEnableInstrumentation) { |
| 297 | if (log) { |
| 298 | LOG(INFO) << "Enable instrumentation."; |
| 299 | } |
| 300 | registerInstrumentationCallbacks (&mInstrumentationCallbacks); |
| 301 | } else { |
| 302 | if (log) { |
| 303 | LOG(INFO) << "Disable instrumentation."; |
| 304 | } |
| 305 | mInstrumentationCallbacks.clear(); |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 310 | void HidlInstrumentor::registerInstrumentationCallbacks( |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 311 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 312 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 313 | std::vector<std::string> instrumentationLibPaths; |
Zhuoyao Zhang | 8bed09f | 2016-10-26 18:12:47 -0700 | [diff] [blame] | 314 | char instrumentation_lib_path[PROPERTY_VALUE_MAX]; |
| 315 | if (property_get("hal.instrumentation.lib.path", |
| 316 | instrumentation_lib_path, |
| 317 | "") > 0) { |
| 318 | instrumentationLibPaths.push_back(instrumentation_lib_path); |
| 319 | } else { |
| 320 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
| 321 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 322 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 323 | } |
| 324 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 325 | for (auto path : instrumentationLibPaths) { |
| 326 | DIR *dir = opendir(path.c_str()); |
| 327 | if (dir == 0) { |
Steven Moreland | 7096f54 | 2016-11-07 11:20:33 -0800 | [diff] [blame] | 328 | LOG(WARNING) << path << " does not exist. "; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 329 | return; |
| 330 | } |
| 331 | |
| 332 | struct dirent *file; |
| 333 | while ((file = readdir(dir)) != NULL) { |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 334 | if (!isInstrumentationLib(file)) |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 335 | continue; |
| 336 | |
| 337 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 338 | if (handle == nullptr) { |
| 339 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 340 | << " error: " << dlerror(); |
| 341 | continue; |
| 342 | } |
| 343 | using cb_fun = void (*)( |
| 344 | const InstrumentationEvent, |
| 345 | const char *, |
| 346 | const char *, |
| 347 | const char *, |
| 348 | const char *, |
| 349 | std::vector<void *> *); |
| 350 | auto cb = (cb_fun)dlsym(handle, "HIDL_INSTRUMENTATION_FUNCTION"); |
| 351 | if (cb == nullptr) { |
| 352 | LOG(WARNING) |
| 353 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION, " |
| 354 | "error: " |
| 355 | << dlerror(); |
| 356 | continue; |
| 357 | } |
| 358 | instrumentationCallbacks->push_back(cb); |
| 359 | LOG(INFO) << "Register instrumentation callback from " |
| 360 | << file->d_name; |
| 361 | } |
| 362 | closedir(dir); |
| 363 | } |
| 364 | #else |
| 365 | // No-op for user builds. |
| 366 | return; |
| 367 | #endif |
| 368 | } |
| 369 | |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 370 | bool HidlInstrumentor::isInstrumentationLib(const dirent *file) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 371 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 372 | if (file->d_type != DT_REG) return false; |
| 373 | std::cmatch cm; |
Zhuoyao Zhang | 5a643b9 | 2017-01-03 17:31:53 -0800 | [diff] [blame] | 374 | std::regex e("^" + mInstrumentationLibPrefix + "(.*).profiler.so$"); |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 375 | if (std::regex_match(file->d_name, cm, e)) return true; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 376 | #endif |
| 377 | return false; |
| 378 | } |
| 379 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 380 | } // namespace hardware |
| 381 | } // namespace android |
| 382 | |
| 383 | |