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 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 19 | #include <android-base/logging.h> |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 20 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 97 | if (size > UINT32_MAX) { |
| 98 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 99 | } |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 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 | |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 105 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 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)) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 121 | free(const_cast<char *>(static_cast<const 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) { |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 130 | if (size > UINT32_MAX) { |
| 131 | LOG(FATAL) << "string size can't exceed 2^32 bytes."; |
| 132 | } |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 133 | clear(); |
| 134 | |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 135 | mBuffer = data; |
Martijn Coenen | 4ca39a0 | 2016-11-11 15:58:51 +0100 | [diff] [blame] | 136 | mSize = static_cast<uint32_t>(size); |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 137 | mOwnsBuffer = false; |
| 138 | } |
| 139 | |
| 140 | const char *hidl_string::c_str() const { |
Yifan Hong | 602b85a | 2016-10-24 13:40:01 -0700 | [diff] [blame] | 141 | return mBuffer; |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | size_t hidl_string::size() const { |
| 145 | return mSize; |
| 146 | } |
| 147 | |
| 148 | bool hidl_string::empty() const { |
| 149 | return mSize == 0; |
| 150 | } |
| 151 | |
Steven Moreland | 5a68232 | 2016-11-11 12:32:50 -0800 | [diff] [blame] | 152 | const char* IBase::descriptor = "android.hardware.base@0.0::IBase"; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 153 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 154 | // ---------------------------------------------------------------------- |
| 155 | // HidlInstrumentor implementation. |
| 156 | HidlInstrumentor::HidlInstrumentor(const std::string &prefix) { |
| 157 | mEnableInstrumentation = property_get_bool("hal.instrumentation.enable", |
| 158 | false); |
| 159 | registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks); |
| 160 | } |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 161 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 162 | HidlInstrumentor:: ~HidlInstrumentor() {} |
| 163 | |
| 164 | void HidlInstrumentor::registerInstrumentationCallbacks( |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 165 | const std::string &profilerPrefix, |
| 166 | std::vector<InstrumentationCallback> *instrumentationCallbacks) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 167 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 168 | std::vector<std::string> instrumentationLibPaths; |
Zhuoyao Zhang | 8bed09f | 2016-10-26 18:12:47 -0700 | [diff] [blame] | 169 | char instrumentation_lib_path[PROPERTY_VALUE_MAX]; |
| 170 | if (property_get("hal.instrumentation.lib.path", |
| 171 | instrumentation_lib_path, |
| 172 | "") > 0) { |
| 173 | instrumentationLibPaths.push_back(instrumentation_lib_path); |
| 174 | } else { |
| 175 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM); |
| 176 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR); |
| 177 | instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM); |
| 178 | } |
| 179 | |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 180 | for (auto path : instrumentationLibPaths) { |
| 181 | DIR *dir = opendir(path.c_str()); |
| 182 | if (dir == 0) { |
Steven Moreland | 7096f54 | 2016-11-07 11:20:33 -0800 | [diff] [blame] | 183 | LOG(WARNING) << path << " does not exist. "; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 184 | return; |
| 185 | } |
| 186 | |
| 187 | struct dirent *file; |
| 188 | while ((file = readdir(dir)) != NULL) { |
| 189 | if (!isInstrumentationLib(profilerPrefix, file)) |
| 190 | continue; |
| 191 | |
| 192 | void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW); |
| 193 | if (handle == nullptr) { |
| 194 | LOG(WARNING) << "couldn't load file: " << file->d_name |
| 195 | << " error: " << dlerror(); |
| 196 | continue; |
| 197 | } |
| 198 | using cb_fun = void (*)( |
| 199 | const InstrumentationEvent, |
| 200 | const char *, |
| 201 | const char *, |
| 202 | const char *, |
| 203 | const char *, |
| 204 | std::vector<void *> *); |
| 205 | auto cb = (cb_fun)dlsym(handle, "HIDL_INSTRUMENTATION_FUNCTION"); |
| 206 | if (cb == nullptr) { |
| 207 | LOG(WARNING) |
| 208 | << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION, " |
| 209 | "error: " |
| 210 | << dlerror(); |
| 211 | continue; |
| 212 | } |
| 213 | instrumentationCallbacks->push_back(cb); |
| 214 | LOG(INFO) << "Register instrumentation callback from " |
| 215 | << file->d_name; |
| 216 | } |
| 217 | closedir(dir); |
| 218 | } |
| 219 | #else |
| 220 | // No-op for user builds. |
| 221 | return; |
| 222 | #endif |
| 223 | } |
| 224 | |
Zhuoyao Zhang | 28e03af | 2016-10-21 11:25:49 -0700 | [diff] [blame] | 225 | bool HidlInstrumentor::isInstrumentationLib( |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 226 | const std::string &profiler_prefix, |
| 227 | const dirent *file) { |
Dan Willemsen | 6365a87 | 2016-09-13 16:29:54 -0700 | [diff] [blame] | 228 | #ifdef LIBHIDL_TARGET_DEBUGGABLE |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 229 | if (file->d_type != DT_REG) return false; |
| 230 | std::cmatch cm; |
| 231 | std::regex e("^" + profiler_prefix + "(.*).profiler.so$"); |
| 232 | if (std::regex_match(file->d_name, cm, e)) return true; |
Zhuoyao Zhang | 7e1286c | 2016-09-12 17:28:48 -0700 | [diff] [blame] | 233 | #endif |
| 234 | return false; |
| 235 | } |
| 236 | |
Martijn Coenen | 7211016 | 2016-08-19 14:28:25 +0200 | [diff] [blame] | 237 | } // namespace hardware |
| 238 | } // namespace android |
| 239 | |
| 240 | |