blob: dce2585e3543085a9750372e2fb8bc5276c3bd28 [file] [log] [blame]
Martijn Coenen72110162016-08-19 14:28:25 +02001/*
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 Coenen30791002016-12-01 15:40:46 +010016#define LOG_TAG "HidlSupport"
Martijn Coenen72110162016-08-19 14:28:25 +020017
18#include <hidl/HidlSupport.h>
19
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070020#include <android-base/logging.h>
Martijn Coenen30791002016-12-01 15:40:46 +010021
Martijn Coenen4ca39a02016-11-11 15:58:51 +010022#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070023#include <cutils/properties.h>
24#include <regex>
Yifan Hong602b85a2016-10-24 13:40:01 -070025#include <utility>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070026#endif
27
Martijn Coenen72110162016-08-19 14:28:25 +020028namespace android {
29namespace hardware {
30
31static const char *const kEmptyString = "";
32
33hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -070034 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +020035 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -070036 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +020037}
38
39hidl_string::~hidl_string() {
40 clear();
41}
42
Steven Morelande03c0872016-10-24 10:43:50 -070043hidl_string::hidl_string(const char *s) : hidl_string() {
Yifan Hong602b85a2016-10-24 13:40:01 -070044 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -070045}
46
Yifan Hong602b85a2016-10-24 13:40:01 -070047hidl_string::hidl_string(const hidl_string &other): hidl_string() {
48 copyFrom(other.c_str(), other.size());
49}
50
51hidl_string::hidl_string(const std::string &s) : hidl_string() {
52 copyFrom(s.c_str(), s.size());
53}
54
55hidl_string::hidl_string(hidl_string &&other): hidl_string() {
56 moveFrom(std::forward<hidl_string>(other));
57}
58
59hidl_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 Coenen72110162016-08-19 14:28:25 +020065}
66
67hidl_string &hidl_string::operator=(const hidl_string &other) {
68 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -070069 clear();
70 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +020071 }
72
73 return *this;
74}
75
76hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -070077 clear();
78 copyFrom(s, strlen(s));
79 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +020080}
81
Yifan Hong602b85a2016-10-24 13:40:01 -070082hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +020083 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -070084 copyFrom(s.c_str(), s.size());
85 return *this;
86}
Martijn Coenen72110162016-08-19 14:28:25 +020087
Yifan Hong602b85a2016-10-24 13:40:01 -070088hidl_string::operator std::string() const {
89 return std::string(mBuffer, mSize);
90}
91
92hidl_string::operator const char *() const {
93 return mBuffer;
94}
95
96void hidl_string::copyFrom(const char *data, size_t size) {
97 // assume my resources are freed.
98
Martijn Coenen4ca39a02016-11-11 15:58:51 +010099 if (size > UINT32_MAX) {
100 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
101 }
Yifan Hong602b85a2016-10-24 13:40:01 -0700102 char *buf = (char *)malloc(size + 1);
103 memcpy(buf, data, size);
104 buf[size] = '\0';
105 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200106
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100107 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200108 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700109}
Martijn Coenen72110162016-08-19 14:28:25 +0200110
Yifan Hong602b85a2016-10-24 13:40:01 -0700111void hidl_string::moveFrom(hidl_string &&other) {
112 // assume my resources are freed.
113
114 mBuffer = other.mBuffer;
115 mSize = other.mSize;
116 mOwnsBuffer = other.mOwnsBuffer;
117
118 other.mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200119}
120
121void hidl_string::clear() {
122 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100123 free(const_cast<char *>(static_cast<const char *>(mBuffer)));
Martijn Coenen72110162016-08-19 14:28:25 +0200124 }
125
Yifan Hong602b85a2016-10-24 13:40:01 -0700126 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200127 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700128 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200129}
130
131void hidl_string::setToExternal(const char *data, size_t size) {
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100132 if (size > UINT32_MAX) {
133 LOG(FATAL) << "string size can't exceed 2^32 bytes.";
134 }
Martijn Coenen72110162016-08-19 14:28:25 +0200135 clear();
136
Yifan Hong602b85a2016-10-24 13:40:01 -0700137 mBuffer = data;
Martijn Coenen4ca39a02016-11-11 15:58:51 +0100138 mSize = static_cast<uint32_t>(size);
Martijn Coenen72110162016-08-19 14:28:25 +0200139 mOwnsBuffer = false;
140}
141
142const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700143 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200144}
145
146size_t hidl_string::size() const {
147 return mSize;
148}
149
150bool hidl_string::empty() const {
151 return mSize == 0;
152}
153
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700154// ----------------------------------------------------------------------
155// HidlInstrumentor implementation.
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800156HidlInstrumentor::HidlInstrumentor(const std::string &prefix)
157 : mInstrumentationLibPrefix(prefix) {
158 configureInstrumentation(false);
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700159}
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700160
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700161HidlInstrumentor:: ~HidlInstrumentor() {}
162
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800163void HidlInstrumentor::configureInstrumentation(bool log) {
164 bool enable_instrumentation = property_get_bool(
165 "hal.instrumentation.enable",
166 false);
167 if (enable_instrumentation != mEnableInstrumentation) {
168 mEnableInstrumentation = enable_instrumentation;
169 if (mEnableInstrumentation) {
170 if (log) {
171 LOG(INFO) << "Enable instrumentation.";
172 }
173 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
174 } else {
175 if (log) {
176 LOG(INFO) << "Disable instrumentation.";
177 }
178 mInstrumentationCallbacks.clear();
179 }
180 }
181}
182
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700183void HidlInstrumentor::registerInstrumentationCallbacks(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700184 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700185#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700186 std::vector<std::string> instrumentationLibPaths;
Zhuoyao Zhang8bed09f2016-10-26 18:12:47 -0700187 char instrumentation_lib_path[PROPERTY_VALUE_MAX];
188 if (property_get("hal.instrumentation.lib.path",
189 instrumentation_lib_path,
190 "") > 0) {
191 instrumentationLibPaths.push_back(instrumentation_lib_path);
192 } else {
193 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
194 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
195 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
196 }
197
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700198 for (auto path : instrumentationLibPaths) {
199 DIR *dir = opendir(path.c_str());
200 if (dir == 0) {
Steven Moreland7096f542016-11-07 11:20:33 -0800201 LOG(WARNING) << path << " does not exist. ";
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700202 return;
203 }
204
205 struct dirent *file;
206 while ((file = readdir(dir)) != NULL) {
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800207 if (!isInstrumentationLib(file))
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700208 continue;
209
210 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
211 if (handle == nullptr) {
212 LOG(WARNING) << "couldn't load file: " << file->d_name
213 << " error: " << dlerror();
214 continue;
215 }
216 using cb_fun = void (*)(
217 const InstrumentationEvent,
218 const char *,
219 const char *,
220 const char *,
221 const char *,
222 std::vector<void *> *);
223 auto cb = (cb_fun)dlsym(handle, "HIDL_INSTRUMENTATION_FUNCTION");
224 if (cb == nullptr) {
225 LOG(WARNING)
226 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION, "
227 "error: "
228 << dlerror();
229 continue;
230 }
231 instrumentationCallbacks->push_back(cb);
232 LOG(INFO) << "Register instrumentation callback from "
233 << file->d_name;
234 }
235 closedir(dir);
236 }
237#else
238 // No-op for user builds.
239 return;
240#endif
241}
242
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800243bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700244#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700245 if (file->d_type != DT_REG) return false;
246 std::cmatch cm;
Zhuoyao Zhang5a643b92017-01-03 17:31:53 -0800247 std::regex e("^" + mInstrumentationLibPrefix + "(.*).profiler.so$");
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700248 if (std::regex_match(file->d_name, cm, e)) return true;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700249#endif
250 return false;
251}
252
Martijn Coenen72110162016-08-19 14:28:25 +0200253} // namespace hardware
254} // namespace android
255
256