blob: 10e250b8fd4fcf33e58047878c03bd80d68d5a40 [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>
Justin Yun1f048102017-12-01 15:30:08 +090022#include <android-base/properties.h>
23#include <android-base/stringprintf.h>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000024#include <cutils/properties.h>
25
26#ifdef LIBHIDL_TARGET_DEBUGGABLE
27#include <dirent.h>
28#include <dlfcn.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070029#include <link.h>
Ryan Campbell69aff752017-07-27 13:49:46 -070030#include <utils/misc.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070031#include <regex>
Ryan Campbell69aff752017-07-27 13:49:46 -070032
33extern "C" __attribute__((weak)) void __sanitizer_cov_dump();
Ryan Campbell21d59d92017-10-19 14:12:49 -070034const char* kGcovPrefixEnvVar = "GCOV_PREFIX";
Ryan Campbellde6c76a2017-11-14 18:15:13 -080035const char* kGcovPrefixOverrideEnvVar = "GCOV_PREFIX_OVERRIDE";
Ryan Campbell21d59d92017-10-19 14:12:49 -070036const char* kGcovPrefixPath = "/data/misc/trace/";
Ryan Campbellde6c76a2017-11-14 18:15:13 -080037const char* kSysPropHalCoverage = "hal.coverage.enable";
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000038#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080039
40namespace android {
41namespace hardware {
42namespace details {
43
Ryan Campbell69aff752017-07-27 13:49:46 -070044void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080045 LOG(FATAL) << message;
46}
47
Justin Yun1f048102017-12-01 15:30:08 +090048std::string getVndkVersionStr() {
49 static std::string vndkVersion("0");
50 // "0" means the vndkVersion must be initialized with the property value.
51 // Otherwise, return the value.
52 if (vndkVersion == "0") {
53 vndkVersion = android::base::GetProperty("ro.vndk.version", "");
54 if (vndkVersion != "" && vndkVersion != "current") {
55 vndkVersion = "-" + vndkVersion;
56 } else {
57 vndkVersion = "";
58 }
59 }
60 return vndkVersion;
61}
62
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000063// ----------------------------------------------------------------------
64// HidlInstrumentor implementation.
Steven Moreland384792b2017-06-19 18:24:40 -070065HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
66 : mEnableInstrumentation(false),
67 mInstrumentationLibPackage(package),
68 mInterfaceName(interface) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000069 configureInstrumentation(false);
Ryan Campbell69aff752017-07-27 13:49:46 -070070#ifdef LIBHIDL_TARGET_DEBUGGABLE
71 if (__sanitizer_cov_dump != nullptr) {
72 ::android::add_sysprop_change_callback(
73 []() {
Ryan Campbell21d59d92017-10-19 14:12:49 -070074 bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
Ryan Campbell69aff752017-07-27 13:49:46 -070075 if (enableCoverage) {
76 __sanitizer_cov_dump();
77 }
78 },
79 0);
80 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070081 if (property_get_bool("ro.vts.coverage", false)) {
Ryan Campbellde6c76a2017-11-14 18:15:13 -080082 const char* prefixOverride = getenv(kGcovPrefixOverrideEnvVar);
83 if (prefixOverride == nullptr || strcmp(prefixOverride, "true") != 0) {
84 const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid());
85 setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */);
86 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070087 ::android::add_sysprop_change_callback(
88 []() {
89 const bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
90 if (enableCoverage) {
91 dl_iterate_phdr(
92 [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) {
93 if (strlen(info->dlpi_name) == 0) return 0;
94
95 void* handle = dlopen(info->dlpi_name, RTLD_LAZY);
96 if (handle == nullptr) {
97 LOG(INFO) << "coverage dlopen failed: " << dlerror();
98 return 0;
99 }
100 void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush");
101 if (flush == nullptr) {
102 return 0;
103 }
104 flush();
105 return 0;
106 },
107 nullptr /* data */);
108 }
109 },
110 0 /* priority */);
111 }
Ryan Campbell69aff752017-07-27 13:49:46 -0700112#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000113}
114
Ryan Campbell69aff752017-07-27 13:49:46 -0700115HidlInstrumentor::~HidlInstrumentor() {}
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000116
117void HidlInstrumentor::configureInstrumentation(bool log) {
Steven Moreland384792b2017-06-19 18:24:40 -0700118 bool enableInstrumentation = property_get_bool(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000119 "hal.instrumentation.enable",
120 false);
Steven Moreland384792b2017-06-19 18:24:40 -0700121 if (enableInstrumentation != mEnableInstrumentation) {
122 mEnableInstrumentation = enableInstrumentation;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000123 if (mEnableInstrumentation) {
124 if (log) {
125 LOG(INFO) << "Enable instrumentation.";
126 }
127 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
128 } else {
129 if (log) {
130 LOG(INFO) << "Disable instrumentation.";
131 }
132 mInstrumentationCallbacks.clear();
133 }
134 }
135}
136
137void HidlInstrumentor::registerInstrumentationCallbacks(
138 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
139#ifdef LIBHIDL_TARGET_DEBUGGABLE
140 std::vector<std::string> instrumentationLibPaths;
Steven Moreland384792b2017-06-19 18:24:40 -0700141 char instrumentationLibPath[PROPERTY_VALUE_MAX];
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000142 if (property_get("hal.instrumentation.lib.path",
Steven Moreland384792b2017-06-19 18:24:40 -0700143 instrumentationLibPath,
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000144 "") > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -0700145 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000146 } else {
Justin Yun1f048102017-12-01 15:30:08 +0900147 static std::string halLibPathVndkSp = android::base::StringPrintf(
148 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, getVndkVersionStr().c_str());
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000149 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Justin Yun1f048102017-12-01 15:30:08 +0900150 instrumentationLibPaths.push_back(halLibPathVndkSp);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000151 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
152 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
153 }
154
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700155 for (const auto& path : instrumentationLibPaths) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000156 DIR *dir = opendir(path.c_str());
157 if (dir == 0) {
158 LOG(WARNING) << path << " does not exist. ";
159 return;
160 }
161
162 struct dirent *file;
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700163 while ((file = readdir(dir)) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000164 if (!isInstrumentationLib(file))
165 continue;
166
167 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
168 char *error;
169 if (handle == nullptr) {
170 LOG(WARNING) << "couldn't load file: " << file->d_name
171 << " error: " << dlerror();
172 continue;
173 }
174
175 dlerror(); /* Clear any existing error */
176
Steven Moreland384792b2017-06-19 18:24:40 -0700177 using cbFun = void (*)(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000178 const InstrumentationEvent,
179 const char *,
180 const char *,
181 const char *,
182 const char *,
183 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700184 std::string package = mInstrumentationLibPackage;
185 for (size_t i = 0; i < package.size(); i++) {
186 if (package[i] == '.') {
187 package[i] = '_';
188 continue;
189 }
190
191 if (package[i] == '@') {
192 package[i] = '_';
193 package.insert(i + 1, "V");
194 continue;
195 }
196 }
Steven Moreland384792b2017-06-19 18:24:40 -0700197 auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700198 + package + "_" + mInterfaceName).c_str());
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700199 if ((error = dlerror()) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000200 LOG(WARNING)
201 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700202 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000203 continue;
204 }
205 instrumentationCallbacks->push_back(cb);
206 LOG(INFO) << "Register instrumentation callback from "
207 << file->d_name;
208 }
209 closedir(dir);
210 }
211#else
212 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800213 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000214 return;
215#endif
216}
217
218bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
219#ifdef LIBHIDL_TARGET_DEBUGGABLE
220 if (file->d_type != DT_REG) return false;
221 std::cmatch cm;
222 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
223 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800224#else
225 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000226#endif
227 return false;
228}
229
Steven Moreland337c3ae2016-11-22 13:37:32 -0800230} // namespace details
231} // namespace hardware
232} // namespace android