blob: e5c8f709c72318d65a6a1408e5f45c62968bd195 [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
Jooyung Han219106c2020-07-17 15:01:19 +090021#ifdef __ANDROID__
22#include <android/api-level.h>
23#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080024#include <android-base/logging.h>
Justin Yun1f048102017-12-01 15:30:08 +090025#include <android-base/properties.h>
26#include <android-base/stringprintf.h>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000027
28#ifdef LIBHIDL_TARGET_DEBUGGABLE
29#include <dirent.h>
30#include <dlfcn.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070031#include <link.h>
Ryan Campbell69aff752017-07-27 13:49:46 -070032#include <utils/misc.h>
Ryan Campbell21d59d92017-10-19 14:12:49 -070033#include <regex>
Ryan Campbell69aff752017-07-27 13:49:46 -070034
35extern "C" __attribute__((weak)) void __sanitizer_cov_dump();
Logan Chien73fe94d2018-10-23 13:44:45 +080036
37const char kGcovPrefixEnvVar[] = "GCOV_PREFIX";
38const char kGcovPrefixOverrideEnvVar[] = "GCOV_PREFIX_OVERRIDE";
39const char kGcovPrefixPath[] = "/data/misc/trace/";
40const char kSysPropHalCoverage[] = "hal.coverage.enable";
Zhuoyao Zhange8f82592018-02-01 16:18:15 -080041#if defined(__LP64__)
Logan Chien73fe94d2018-10-23 13:44:45 +080042const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.64";
Zhuoyao Zhange8f82592018-02-01 16:18:15 -080043#else
Logan Chien73fe94d2018-10-23 13:44:45 +080044const char kSysPropInstrumentationPath[] = "hal.instrumentation.lib.path.32";
Zhuoyao Zhange8f82592018-02-01 16:18:15 -080045#endif
Logan Chien73fe94d2018-10-23 13:44:45 +080046#endif // LIBHIDL_TARGET_DEBUGGABLE
Steven Moreland337c3ae2016-11-22 13:37:32 -080047
48namespace android {
49namespace hardware {
50namespace details {
51
Ryan Campbell69aff752017-07-27 13:49:46 -070052void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080053 LOG(FATAL) << message;
54}
55
Jooyung Han219106c2020-07-17 15:01:19 +090056std::string getVndkSpHwPath(const char* lib) {
57 static std::string vndk_version = base::GetProperty("ro.vndk.version", "");
58#ifdef __ANDROID__
59 static int api_level = android_get_device_api_level();
60 if (api_level >= __ANDROID_API_R__) {
61 return android::base::StringPrintf("/apex/com.android.vndk.v%s/%s/hw/",
62 vndk_version.c_str(), lib);
63 }
64#endif
65 return android::base::StringPrintf("/system/%s/vndk-sp-%s/hw/", lib, vndk_version.c_str());
Justin Yun1f048102017-12-01 15:30:08 +090066}
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(
Steven Moreland2056cf32019-09-17 17:41:02 -070078 []() {
79 bool enableCoverage = base::GetBoolProperty(kSysPropHalCoverage, false);
80 if (enableCoverage) {
81 __sanitizer_cov_dump();
82 }
83 },
84 0);
Ryan Campbell69aff752017-07-27 13:49:46 -070085 }
Steven Moreland2056cf32019-09-17 17:41:02 -070086 if (base::GetBoolProperty("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(
Steven Moreland2056cf32019-09-17 17:41:02 -070093 []() {
94 const bool enableCoverage = base::GetBoolProperty(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;
Ryan Campbell21d59d92017-10-19 14:12:49 -070099
Steven Moreland2056cf32019-09-17 17:41:02 -0700100 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 */);
Ryan Campbell21d59d92017-10-19 14:12:49 -0700116 }
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) {
Steven Moreland2056cf32019-09-17 17:41:02 -0700123 mEnableInstrumentation = base::GetBoolProperty("hal.instrumentation.enable", false);
Zhuoyao Zhange8f82592018-02-01 16:18:15 -0800124 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 Moreland2056cf32019-09-17 17:41:02 -0700142 const std::string instrumentationLibPath = base::GetProperty(kSysPropInstrumentationPath, "");
143 if (instrumentationLibPath.size() > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -0700144 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000145 } else {
Jooyung Han219106c2020-07-17 15:01:19 +0900146 static std::string halLibPathVndkSp = getVndkSpHwPath();
Justin Yun6a323ed2018-10-18 18:41:56 +0900147#ifndef __ANDROID_VNDK__
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000148 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Justin Yun6a323ed2018-10-18 18:41:56 +0900149#endif
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());
Yi Kong277f9772018-07-24 14:59:46 -0700157 if (dir == nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000158 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