blob: 8c5ac1726aac5a114950ff5a6851278f672cbee6 [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
Steven Moreland5a682322016-11-11 12:32:50 -0800148const char* IBase::descriptor = "android.hardware.base@0.0::IBase";
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700149
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700150// ----------------------------------------------------------------------
151// HidlInstrumentor implementation.
152HidlInstrumentor::HidlInstrumentor(const std::string &prefix) {
153 mEnableInstrumentation = property_get_bool("hal.instrumentation.enable",
154 false);
155 registerInstrumentationCallbacks(prefix, &mInstrumentationCallbacks);
156}
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700157
Zhuoyao Zhang28e03af2016-10-21 11:25:49 -0700158HidlInstrumentor:: ~HidlInstrumentor() {}
159
160void HidlInstrumentor::registerInstrumentationCallbacks(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700161 const std::string &profilerPrefix,
162 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700163#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700164 std::vector<std::string> instrumentationLibPaths;
Zhuoyao Zhang8bed09f2016-10-26 18:12:47 -0700165 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 Zhang7e1286c2016-09-12 17:28:48 -0700176 for (auto path : instrumentationLibPaths) {
177 DIR *dir = opendir(path.c_str());
178 if (dir == 0) {
Steven Moreland7096f542016-11-07 11:20:33 -0800179 LOG(WARNING) << path << " does not exist. ";
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700180 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 Zhang28e03af2016-10-21 11:25:49 -0700221bool HidlInstrumentor::isInstrumentationLib(
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700222 const std::string &profiler_prefix,
223 const dirent *file) {
Dan Willemsen6365a872016-09-13 16:29:54 -0700224#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhang7e1286c2016-09-12 17:28:48 -0700225 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 Zhang7e1286c2016-09-12 17:28:48 -0700229#endif
230 return false;
231}
232
Martijn Coenen72110162016-08-19 14:28:25 +0200233} // namespace hardware
234} // namespace android
235
236