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