blob: babdac1d818a7797928ea76217b41cb3323db8be [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 Zhange8f82592018-02-01 16:18:15 -080038#if defined(__LP64__)
39const char* kSysPropInstrumentationPath = "hal.instrumentation.lib.path.64";
40#else
41const char* kSysPropInstrumentationPath = "hal.instrumentation.lib.path.32";
42#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000043#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080044
45namespace android {
46namespace hardware {
47namespace details {
48
Ryan Campbell69aff752017-07-27 13:49:46 -070049void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080050 LOG(FATAL) << message;
51}
52
Justin Yun1f048102017-12-01 15:30:08 +090053std::string getVndkVersionStr() {
54 static std::string vndkVersion("0");
55 // "0" means the vndkVersion must be initialized with the property value.
56 // Otherwise, return the value.
57 if (vndkVersion == "0") {
58 vndkVersion = android::base::GetProperty("ro.vndk.version", "");
59 if (vndkVersion != "" && vndkVersion != "current") {
60 vndkVersion = "-" + vndkVersion;
61 } else {
62 vndkVersion = "";
63 }
64 }
65 return vndkVersion;
66}
67
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000068// ----------------------------------------------------------------------
69// HidlInstrumentor implementation.
Steven Moreland384792b2017-06-19 18:24:40 -070070HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
71 : mEnableInstrumentation(false),
72 mInstrumentationLibPackage(package),
73 mInterfaceName(interface) {
Ryan Campbell69aff752017-07-27 13:49:46 -070074#ifdef LIBHIDL_TARGET_DEBUGGABLE
Zhuoyao Zhange8f82592018-02-01 16:18:15 -080075 configureInstrumentation(false);
Ryan Campbell69aff752017-07-27 13:49:46 -070076 if (__sanitizer_cov_dump != nullptr) {
77 ::android::add_sysprop_change_callback(
78 []() {
Ryan Campbell21d59d92017-10-19 14:12:49 -070079 bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
Ryan Campbell69aff752017-07-27 13:49:46 -070080 if (enableCoverage) {
81 __sanitizer_cov_dump();
82 }
83 },
84 0);
85 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070086 if (property_get_bool("ro.vts.coverage", false)) {
Ryan Campbellde6c76a2017-11-14 18:15:13 -080087 const char* prefixOverride = getenv(kGcovPrefixOverrideEnvVar);
88 if (prefixOverride == nullptr || strcmp(prefixOverride, "true") != 0) {
89 const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid());
90 setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */);
91 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070092 ::android::add_sysprop_change_callback(
93 []() {
94 const bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
95 if (enableCoverage) {
96 dl_iterate_phdr(
97 [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) {
98 if (strlen(info->dlpi_name) == 0) return 0;
99
100 void* handle = dlopen(info->dlpi_name, RTLD_LAZY);
101 if (handle == nullptr) {
102 LOG(INFO) << "coverage dlopen failed: " << dlerror();
103 return 0;
104 }
105 void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush");
106 if (flush == nullptr) {
107 return 0;
108 }
109 flush();
110 return 0;
111 },
112 nullptr /* data */);
113 }
114 },
115 0 /* priority */);
116 }
Ryan Campbell69aff752017-07-27 13:49:46 -0700117#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000118}
119
Ryan Campbell69aff752017-07-27 13:49:46 -0700120HidlInstrumentor::~HidlInstrumentor() {}
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000121
122void HidlInstrumentor::configureInstrumentation(bool log) {
Zhuoyao Zhange8f82592018-02-01 16:18:15 -0800123 mEnableInstrumentation = property_get_bool("hal.instrumentation.enable", false);
124 if (mEnableInstrumentation) {
125 if (log) {
126 LOG(INFO) << "Enable instrumentation.";
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000127 }
Zhuoyao Zhange8f82592018-02-01 16:18:15 -0800128 mInstrumentationCallbacks.clear();
129 registerInstrumentationCallbacks(&mInstrumentationCallbacks);
130 } else {
131 if (log) {
132 LOG(INFO) << "Disable instrumentation.";
133 }
134 mInstrumentationCallbacks.clear();
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000135 }
136}
137
138void HidlInstrumentor::registerInstrumentationCallbacks(
139 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
140#ifdef LIBHIDL_TARGET_DEBUGGABLE
141 std::vector<std::string> instrumentationLibPaths;
Steven Moreland384792b2017-06-19 18:24:40 -0700142 char instrumentationLibPath[PROPERTY_VALUE_MAX];
Zhuoyao Zhange8f82592018-02-01 16:18:15 -0800143 if (property_get(kSysPropInstrumentationPath, instrumentationLibPath, "") > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -0700144 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000145 } else {
Justin Yun1f048102017-12-01 15:30:08 +0900146 static std::string halLibPathVndkSp = android::base::StringPrintf(
147 HAL_LIBRARY_PATH_VNDK_SP_FOR_VERSION, getVndkVersionStr().c_str());
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000148 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Justin Yun1f048102017-12-01 15:30:08 +0900149 instrumentationLibPaths.push_back(halLibPathVndkSp);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000150 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
151 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
152 }
153
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700154 for (const auto& path : instrumentationLibPaths) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000155 DIR *dir = opendir(path.c_str());
156 if (dir == 0) {
157 LOG(WARNING) << path << " does not exist. ";
158 return;
159 }
160
161 struct dirent *file;
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700162 while ((file = readdir(dir)) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000163 if (!isInstrumentationLib(file))
164 continue;
165
166 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
167 char *error;
168 if (handle == nullptr) {
169 LOG(WARNING) << "couldn't load file: " << file->d_name
170 << " error: " << dlerror();
171 continue;
172 }
173
174 dlerror(); /* Clear any existing error */
175
Steven Moreland384792b2017-06-19 18:24:40 -0700176 using cbFun = void (*)(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000177 const InstrumentationEvent,
178 const char *,
179 const char *,
180 const char *,
181 const char *,
182 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700183 std::string package = mInstrumentationLibPackage;
184 for (size_t i = 0; i < package.size(); i++) {
185 if (package[i] == '.') {
186 package[i] = '_';
187 continue;
188 }
189
190 if (package[i] == '@') {
191 package[i] = '_';
192 package.insert(i + 1, "V");
193 continue;
194 }
195 }
Steven Moreland384792b2017-06-19 18:24:40 -0700196 auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700197 + package + "_" + mInterfaceName).c_str());
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700198 if ((error = dlerror()) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000199 LOG(WARNING)
200 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700201 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000202 continue;
203 }
204 instrumentationCallbacks->push_back(cb);
205 LOG(INFO) << "Register instrumentation callback from "
206 << file->d_name;
207 }
208 closedir(dir);
209 }
210#else
211 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800212 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000213 return;
214#endif
215}
216
217bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
218#ifdef LIBHIDL_TARGET_DEBUGGABLE
219 if (file->d_type != DT_REG) return false;
220 std::cmatch cm;
221 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
222 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800223#else
224 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000225#endif
226 return false;
227}
228
Steven Moreland337c3ae2016-11-22 13:37:32 -0800229} // namespace details
230} // namespace hardware
231} // namespace android