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