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