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