blob: a2ada46f8b96f7171339daff64cd54d31f96c5e1 [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
Steven Moreland7096f542016-11-07 11:20:33 -080017#define LOG_TAG "HidlSupport"
18
Martijn Coenen72110162016-08-19 14:28:25 +020019#include <hidl/HidlSupport.h>
20
Dan Willemsen6365a872016-09-13 16:29:54 -070021#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -070022#include <android-base/logging.h>
23#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
99 char *buf = (char *)malloc(size + 1);
100 memcpy(buf, data, size);
101 buf[size] = '\0';
102 mBuffer = buf;
Martijn Coenen72110162016-08-19 14:28:25 +0200103
104 mSize = size;
105 mOwnsBuffer = true;
Yifan Hong602b85a2016-10-24 13:40:01 -0700106}
Martijn Coenen72110162016-08-19 14:28:25 +0200107
Yifan Hong602b85a2016-10-24 13:40:01 -0700108void 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 Coenen72110162016-08-19 14:28:25 +0200116}
117
118void hidl_string::clear() {
119 if (mOwnsBuffer && (mBuffer != kEmptyString)) {
Yifan Hong602b85a2016-10-24 13:40:01 -0700120 free(const_cast<char *>(mBuffer));
Martijn Coenen72110162016-08-19 14:28:25 +0200121 }
122
Yifan Hong602b85a2016-10-24 13:40:01 -0700123 mBuffer = kEmptyString;
Martijn Coenen72110162016-08-19 14:28:25 +0200124 mSize = 0;
Yifan Hong602b85a2016-10-24 13:40:01 -0700125 mOwnsBuffer = false;
Martijn Coenen72110162016-08-19 14:28:25 +0200126}
127
128void hidl_string::setToExternal(const char *data, size_t size) {
129 clear();
130
Yifan Hong602b85a2016-10-24 13:40:01 -0700131 mBuffer = data;
Martijn Coenen72110162016-08-19 14:28:25 +0200132 mSize = size;
133 mOwnsBuffer = false;
134}
135
136const char *hidl_string::c_str() const {
Yifan Hong602b85a2016-10-24 13:40:01 -0700137 return mBuffer;
Martijn Coenen72110162016-08-19 14:28:25 +0200138}
139
140size_t hidl_string::size() const {
141 return mSize;
142}
143
144bool hidl_string::empty() const {
145 return mSize == 0;
146}
147
148status_t hidl_string::readEmbeddedFromParcel(
149 const Parcel &parcel, size_t parentHandle, size_t parentOffset) {
150 const void *ptr = parcel.readEmbeddedBuffer(
151 nullptr /* buffer_handle */,
152 parentHandle,
153 parentOffset + offsetof(hidl_string, mBuffer));
154
155 return ptr != NULL ? OK : UNKNOWN_ERROR;
156}
157
158status_t hidl_string::writeEmbeddedToParcel(
159 Parcel *parcel, size_t parentHandle, size_t parentOffset) const {
160 return parcel->writeEmbeddedBuffer(
161 mBuffer,
162 mSize + 1,
163 nullptr /* handle */,
164 parentHandle,
165 parentOffset + offsetof(hidl_string, mBuffer));
166}
167
Andreas Huberebfeb362016-08-25 13:39:05 -0700168// static
169const size_t hidl_string::kOffsetOfBuffer = offsetof(hidl_string, mBuffer);
170
Yifan Honga3c31842016-10-21 10:33:14 -0700171const ::android::String16 IHidlInterfaceBase::descriptor(
172 "android.hardware@0.0::IHidlInterfaceBase");
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700173
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700174// ----------------------------------------------------------------------
175// HidlInstrumentor implementation.
176HidlInstrumentor::HidlInstrumentor(const std::string &prefix) {
177 mEnableInstrumentation = property_get_bool("hal.instrumentation.enable",
178 false);
179 registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks);
180}
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700181
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700182HidlInstrumentor:: ~HidlInstrumentor() {}
183
184void HidlInstrumentor::registerInstrumentationCallbacks(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700185 const std::string &profilerPrefix,
186 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700187#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700188 std::vector<std::string> instrumentationLibPaths;
Zhuoyao Zhang8bed09f2016-10-26 18:12:47 -0700189 char instrumentation_lib_path[PROPERTY_VALUE_MAX];
190 if (property_get("hal.instrumentation.lib.path",
191 instrumentation_lib_path,
192 "") > 0) {
193 instrumentationLibPaths.push_back(instrumentation_lib_path);
194 } else {
195 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
196 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
197 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
198 }
199
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700200 for (auto path : instrumentationLibPaths) {
201 DIR *dir = opendir(path.c_str());
202 if (dir == 0) {
Steven Moreland7096f542016-11-07 11:20:33 -0800203 LOG(WARNING) << path << " does not exist. ";
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700204 return;
205 }
206
207 struct dirent *file;
208 while ((file = readdir(dir)) != NULL) {
209 if (!isInstrumentationLib(profilerPrefix, file))
210 continue;
211
212 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
213 if (handle == nullptr) {
214 LOG(WARNING) << "couldn't load file: " << file->d_name
215 << " error: " << dlerror();
216 continue;
217 }
218 using cb_fun = void (*)(
219 const InstrumentationEvent,
220 const char *,
221 const char *,
222 const char *,
223 const char *,
224 std::vector<void *> *);
225 auto cb = (cb_fun)dlsym(handle, "HIDL_INSTRUMENTATION_FUNCTION");
226 if (cb == nullptr) {
227 LOG(WARNING)
228 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION, "
229 "error: "
230 << dlerror();
231 continue;
232 }
233 instrumentationCallbacks->push_back(cb);
234 LOG(INFO) << "Register instrumentation callback from "
235 << file->d_name;
236 }
237 closedir(dir);
238 }
239#else
240 // No-op for user builds.
241 return;
242#endif
243}
244
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700245bool HidlInstrumentor::isInstrumentationLib(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700246 const std::string &profiler_prefix,
247 const dirent *file) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700248#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700249 if (file->d_type != DT_REG) return false;
250 std::cmatch cm;
251 std::regex e("^" + profiler_prefix + "(.*).profiler.so$");
252 if (std::regex_match(file->d_name, cm, e)) return true;
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700253#endif
254 return false;
255}
256
Martijn Coenen72110162016-08-19 14:28:25 +0200257} // namespace hardware
258} // namespace android
259
260