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