blob: c8a2821e9ed01caeafe227ce24ca59d2bd39a359 [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 */
16
17#include <hidl/HidlSupport.h>
18
Dan Willemsen6365a872016-09-13 16:29:54 -070019#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070020#include <android-base/logging.h>
21#include <cutils/properties.h>
22#include <regex>
Yifan Hong602b85a2016-10-24 13:40:01 -070023#include <utility>
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070024#endif
25
Martijn Coenen72110162016-08-19 14:28:25 +020026namespace android {
27namespace hardware {
28
29static const char *const kEmptyString = "";
30
31hidl_string::hidl_string()
Yifan Hong602b85a2016-10-24 13:40:01 -070032 : mBuffer(kEmptyString),
Martijn Coenen72110162016-08-19 14:28:25 +020033 mSize(0),
Yifan Hong602b85a2016-10-24 13:40:01 -070034 mOwnsBuffer(false) {
Martijn Coenen72110162016-08-19 14:28:25 +020035}
36
37hidl_string::~hidl_string() {
38 clear();
39}
40
Steven Morelande03c0872016-10-24 10:43:50 -070041hidl_string::hidl_string(const char *s) : hidl_string() {
Yifan Hong602b85a2016-10-24 13:40:01 -070042 copyFrom(s, strlen(s));
Steven Morelande03c0872016-10-24 10:43:50 -070043}
44
Yifan Hong602b85a2016-10-24 13:40:01 -070045hidl_string::hidl_string(const hidl_string &other): hidl_string() {
46 copyFrom(other.c_str(), other.size());
47}
48
49hidl_string::hidl_string(const std::string &s) : hidl_string() {
50 copyFrom(s.c_str(), s.size());
51}
52
53hidl_string::hidl_string(hidl_string &&other): hidl_string() {
54 moveFrom(std::forward<hidl_string>(other));
55}
56
57hidl_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 Coenen72110162016-08-19 14:28:25 +020063}
64
65hidl_string &hidl_string::operator=(const hidl_string &other) {
66 if (this != &other) {
Yifan Hong602b85a2016-10-24 13:40:01 -070067 clear();
68 copyFrom(other.c_str(), other.size());
Martijn Coenen72110162016-08-19 14:28:25 +020069 }
70
71 return *this;
72}
73
74hidl_string &hidl_string::operator=(const char *s) {
Yifan Hong602b85a2016-10-24 13:40:01 -070075 clear();
76 copyFrom(s, strlen(s));
77 return *this;
Martijn Coenen72110162016-08-19 14:28:25 +020078}
79
Yifan Hong602b85a2016-10-24 13:40:01 -070080hidl_string &hidl_string::operator=(const std::string &s) {
Martijn Coenen72110162016-08-19 14:28:25 +020081 clear();
Yifan Hong602b85a2016-10-24 13:40:01 -070082 copyFrom(s.c_str(), s.size());
83 return *this;
84}
Martijn Coenen72110162016-08-19 14:28:25 +020085
Yifan Hong602b85a2016-10-24 13:40:01 -070086hidl_string::operator std::string() const {
87 return std::string(mBuffer, mSize);
88}
89
90hidl_string::operator const char *() const {
91 return mBuffer;
92}
93
94void 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 Coenen72110162016-08-19 14:28:25 +0200101
102 mSize = size;
103 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700104}
Martijn Coenen72110162016-08-19 14:28:25 +0200105
Yifan Hong602b85a2016-10-24 13:40:01 -0700106void 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 Coenen72110162016-08-19 14:28:25 +0200114}
115
116void hidl_string::clear() {
117 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700118 free(const_cast<char *>(mBuffer));
Martijn Coenen72110162016-08-19 14:28:25 +0200119 }
120
Yifan Hong602b85a2016-10-24 13:40:01 -0700121 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200122 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700123 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200124}
125
126void hidl_string::setToExternal(const char *data, size_t size) {
127 clear();
128
Yifan Hong602b85a2016-10-24 13:40:01 -0700129 mBuffer = data;
Martijn Coenen72110162016-08-19 14:28:25 +0200130 mSize = size;
131 mOwnsBuffer = false;
132}
133
134const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700135 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200136}
137
138size_t hidl_string::size() const {
139 return mSize;
140}
141
142bool hidl_string::empty() const {
143 return mSize == 0;
144}
145
146status_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
156status_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 Huberebfeb362016-08-25 13:39:05 -0700166// static
167const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
168
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700169
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700170// ----------------------------------------------------------------------
171// HidlInstrumentor implementation.
172HidlInstrumentor::HidlInstrumentor(const std::string &prefix) {
173 mEnableInstrumentation = property_get_bool("hal.instrumentation.enable",
174 false);
175 registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks);
176}
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700177
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700178HidlInstrumentor:: ~HidlInstrumentor() {}
179
180void HidlInstrumentor::registerInstrumentationCallbacks(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700181 const std::string &profilerPrefix,
182 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700183#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700184 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 Zhang28e03af2016-10-21 11:25:49 -0700233bool HidlInstrumentor::isInstrumentationLib(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700234 const std::string &profiler_prefix,
235 const dirent *file) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700236#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700237 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 Zhang7e1286c2016-09-12 17:28:48 -0700241#endif
242 return false;
243}
244
Martijn Coenen72110162016-08-19 14:28:25 +0200245} // namespace hardware
246} // namespace android
247
248