blob: 64b4f260ecaf27a9082401dd2f0d5e71aca07071 [file] [log] [blame]
Steven Moreland337c3ae2016-11-22 13:37:32 -08001/*
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#define LOG_TAG "HidlInternal"
18
19#include <hidl/HidlInternal.h>
20
21#include <android-base/logging.h>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000022#include <cutils/properties.h>
23
24#ifdef LIBHIDL_TARGET_DEBUGGABLE
25#include <dirent.h>
26#include <dlfcn.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070027#include <link.h>
Ryan Campbell69aff752017-07-27 13:49:46 -070028#include <utils/misc.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070029#include <regex>
Ryan Campbell69aff752017-07-27 13:49:46 -070030
31extern "C" __attribute__((weak)) void __sanitizer_cov_dump();
Ryan Campbell21d59d92017-10-19 14:12:49 -070032const char* kSysPropHalCoverage = "hal.coverage.enable";
33const char* kGcovPrefixEnvVar = "GCOV_PREFIX";
34const char* kGcovPrefixPath = "/data/misc/trace/";
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000035#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080036
37namespace android {
38namespace hardware {
39namespace details {
40
Ryan Campbell69aff752017-07-27 13:49:46 -070041void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080042 LOG(FATAL) << message;
43}
44
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000045// ----------------------------------------------------------------------
46// HidlInstrumentor implementation.
Steven Moreland384792b2017-06-19 18:24:40 -070047HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
48 : mEnableInstrumentation(false),
49 mInstrumentationLibPackage(package),
50 mInterfaceName(interface) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000051 configureInstrumentation(false);
Ryan Campbell69aff752017-07-27 13:49:46 -070052#ifdef LIBHIDL_TARGET_DEBUGGABLE
53 if (__sanitizer_cov_dump != nullptr) {
54 ::android::add_sysprop_change_callback(
55 []() {
Ryan Campbell21d59d92017-10-19 14:12:49 -070056 bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
Ryan Campbell69aff752017-07-27 13:49:46 -070057 if (enableCoverage) {
58 __sanitizer_cov_dump();
59 }
60 },
61 0);
62 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070063 if (property_get_bool("ro.vts.coverage", false)) {
64 const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid());
65 setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */);
66 ::android::add_sysprop_change_callback(
67 []() {
68 const bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
69 if (enableCoverage) {
70 dl_iterate_phdr(
71 [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) {
72 if (strlen(info->dlpi_name) == 0) return 0;
73
74 void* handle = dlopen(info->dlpi_name, RTLD_LAZY);
75 if (handle == nullptr) {
76 LOG(INFO) << "coverage dlopen failed: " << dlerror();
77 return 0;
78 }
79 void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush");
80 if (flush == nullptr) {
81 return 0;
82 }
83 flush();
84 return 0;
85 },
86 nullptr /* data */);
87 }
88 },
89 0 /* priority */);
90 }
Ryan Campbell69aff752017-07-27 13:49:46 -070091#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000092}
93
Ryan Campbell69aff752017-07-27 13:49:46 -070094HidlInstrumentor::~HidlInstrumentor() {}
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000095
96void HidlInstrumentor::configureInstrumentation(bool log) {
Steven Moreland384792b2017-06-19 18:24:40 -070097 bool enableInstrumentation = property_get_bool(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000098 "hal.instrumentation.enable",
99 false);
Steven Moreland384792b2017-06-19 18:24:40 -0700100 if (enableInstrumentation != mEnableInstrumentation) {
101 mEnableInstrumentation = enableInstrumentation;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000102 if (mEnableInstrumentation) {
103 if (log) {
104 LOG(INFO) << "Enable instrumentation.";
105 }
106 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
107 } else {
108 if (log) {
109 LOG(INFO) << "Disable instrumentation.";
110 }
111 mInstrumentationCallbacks.clear();
112 }
113 }
114}
115
116void HidlInstrumentor::registerInstrumentationCallbacks(
117 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
118#ifdef LIBHIDL_TARGET_DEBUGGABLE
119 std::vector<std::string> instrumentationLibPaths;
Steven Moreland384792b2017-06-19 18:24:40 -0700120 char instrumentationLibPath[PROPERTY_VALUE_MAX];
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000121 if (property_get("hal.instrumentation.lib.path",
Steven Moreland384792b2017-06-19 18:24:40 -0700122 instrumentationLibPath,
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000123 "") > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -0700124 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000125 } else {
126 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Jiyong Park56eb1492017-08-04 16:16:13 +0900127 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VNDK_SP);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000128 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
129 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
130 }
131
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700132 for (const auto& path : instrumentationLibPaths) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000133 DIR *dir = opendir(path.c_str());
134 if (dir == 0) {
135 LOG(WARNING) << path << " does not exist. ";
136 return;
137 }
138
139 struct dirent *file;
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700140 while ((file = readdir(dir)) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000141 if (!isInstrumentationLib(file))
142 continue;
143
144 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
145 char *error;
146 if (handle == nullptr) {
147 LOG(WARNING) << "couldn't load file: " << file->d_name
148 << " error: " << dlerror();
149 continue;
150 }
151
152 dlerror(); /* Clear any existing error */
153
Steven Moreland384792b2017-06-19 18:24:40 -0700154 using cbFun = void (*)(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000155 const InstrumentationEvent,
156 const char *,
157 const char *,
158 const char *,
159 const char *,
160 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700161 std::string package = mInstrumentationLibPackage;
162 for (size_t i = 0; i < package.size(); i++) {
163 if (package[i] == '.') {
164 package[i] = '_';
165 continue;
166 }
167
168 if (package[i] == '@') {
169 package[i] = '_';
170 package.insert(i + 1, "V");
171 continue;
172 }
173 }
Steven Moreland384792b2017-06-19 18:24:40 -0700174 auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700175 + package + "_" + mInterfaceName).c_str());
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700176 if ((error = dlerror()) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000177 LOG(WARNING)
178 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700179 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000180 continue;
181 }
182 instrumentationCallbacks->push_back(cb);
183 LOG(INFO) << "Register instrumentation callback from "
184 << file->d_name;
185 }
186 closedir(dir);
187 }
188#else
189 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800190 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000191 return;
192#endif
193}
194
195bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
196#ifdef LIBHIDL_TARGET_DEBUGGABLE
197 if (file->d_type != DT_REG) return false;
198 std::cmatch cm;
199 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
200 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800201#else
202 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000203#endif
204 return false;
205}
206
Steven Moreland337c3ae2016-11-22 13:37:32 -0800207} // namespace details
208} // namespace hardware
209} // namespace android