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