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