blob: 6cdc24ef9e819108e09ad9ca614b0f8b464bb75a [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* kGcovPrefixEnvVar = "GCOV_PREFIX";
Ryan Campbellde6c76a2017-11-14 18:15:13 -080033const char* kGcovPrefixOverrideEnvVar = "GCOV_PREFIX_OVERRIDE";
Ryan Campbell21d59d92017-10-19 14:12:49 -070034const char* kGcovPrefixPath = "/data/misc/trace/";
Ryan Campbellde6c76a2017-11-14 18:15:13 -080035const char* kSysPropHalCoverage = "hal.coverage.enable";
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000036#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080037
38namespace android {
39namespace hardware {
40namespace details {
41
Ryan Campbell69aff752017-07-27 13:49:46 -070042void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080043 LOG(FATAL) << message;
44}
45
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000046// ----------------------------------------------------------------------
47// HidlInstrumentor implementation.
Steven Moreland384792b2017-06-19 18:24:40 -070048HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
49 : mEnableInstrumentation(false),
50 mInstrumentationLibPackage(package),
51 mInterfaceName(interface) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000052 configureInstrumentation(false);
Ryan Campbell69aff752017-07-27 13:49:46 -070053#ifdef LIBHIDL_TARGET_DEBUGGABLE
54 if (__sanitizer_cov_dump != nullptr) {
55 ::android::add_sysprop_change_callback(
56 []() {
Ryan Campbell21d59d92017-10-19 14:12:49 -070057 bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
Ryan Campbell69aff752017-07-27 13:49:46 -070058 if (enableCoverage) {
59 __sanitizer_cov_dump();
60 }
61 },
62 0);
63 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070064 if (property_get_bool("ro.vts.coverage", false)) {
Ryan Campbellde6c76a2017-11-14 18:15:13 -080065 const char* prefixOverride = getenv(kGcovPrefixOverrideEnvVar);
66 if (prefixOverride == nullptr || strcmp(prefixOverride, "true") != 0) {
67 const std::string gcovPath = kGcovPrefixPath + std::to_string(getpid());
68 setenv(kGcovPrefixEnvVar, gcovPath.c_str(), true /* overwrite */);
69 }
Ryan Campbell21d59d92017-10-19 14:12:49 -070070 ::android::add_sysprop_change_callback(
71 []() {
72 const bool enableCoverage = property_get_bool(kSysPropHalCoverage, false);
73 if (enableCoverage) {
74 dl_iterate_phdr(
75 [](struct dl_phdr_info* info, size_t /* size */, void* /* data */) {
76 if (strlen(info->dlpi_name) == 0) return 0;
77
78 void* handle = dlopen(info->dlpi_name, RTLD_LAZY);
79 if (handle == nullptr) {
80 LOG(INFO) << "coverage dlopen failed: " << dlerror();
81 return 0;
82 }
83 void (*flush)() = (void (*)())dlsym(handle, "__gcov_flush");
84 if (flush == nullptr) {
85 return 0;
86 }
87 flush();
88 return 0;
89 },
90 nullptr /* data */);
91 }
92 },
93 0 /* priority */);
94 }
Ryan Campbell69aff752017-07-27 13:49:46 -070095#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000096}
97
Ryan Campbell69aff752017-07-27 13:49:46 -070098HidlInstrumentor::~HidlInstrumentor() {}
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000099
100void HidlInstrumentor::configureInstrumentation(bool log) {
Steven Moreland384792b2017-06-19 18:24:40 -0700101 bool enableInstrumentation = property_get_bool(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000102 "hal.instrumentation.enable",
103 false);
Steven Moreland384792b2017-06-19 18:24:40 -0700104 if (enableInstrumentation != mEnableInstrumentation) {
105 mEnableInstrumentation = enableInstrumentation;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000106 if (mEnableInstrumentation) {
107 if (log) {
108 LOG(INFO) << "Enable instrumentation.";
109 }
110 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
111 } else {
112 if (log) {
113 LOG(INFO) << "Disable instrumentation.";
114 }
115 mInstrumentationCallbacks.clear();
116 }
117 }
118}
119
120void HidlInstrumentor::registerInstrumentationCallbacks(
121 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
122#ifdef LIBHIDL_TARGET_DEBUGGABLE
123 std::vector<std::string> instrumentationLibPaths;
Steven Moreland384792b2017-06-19 18:24:40 -0700124 char instrumentationLibPath[PROPERTY_VALUE_MAX];
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000125 if (property_get("hal.instrumentation.lib.path",
Steven Moreland384792b2017-06-19 18:24:40 -0700126 instrumentationLibPath,
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000127 "") > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -0700128 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000129 } else {
130 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Jiyong Park56eb1492017-08-04 16:16:13 +0900131 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VNDK_SP);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000132 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
133 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
134 }
135
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700136 for (const auto& path : instrumentationLibPaths) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000137 DIR *dir = opendir(path.c_str());
138 if (dir == 0) {
139 LOG(WARNING) << path << " does not exist. ";
140 return;
141 }
142
143 struct dirent *file;
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700144 while ((file = readdir(dir)) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000145 if (!isInstrumentationLib(file))
146 continue;
147
148 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
149 char *error;
150 if (handle == nullptr) {
151 LOG(WARNING) << "couldn't load file: " << file->d_name
152 << " error: " << dlerror();
153 continue;
154 }
155
156 dlerror(); /* Clear any existing error */
157
Steven Moreland384792b2017-06-19 18:24:40 -0700158 using cbFun = void (*)(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000159 const InstrumentationEvent,
160 const char *,
161 const char *,
162 const char *,
163 const char *,
164 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700165 std::string package = mInstrumentationLibPackage;
166 for (size_t i = 0; i < package.size(); i++) {
167 if (package[i] == '.') {
168 package[i] = '_';
169 continue;
170 }
171
172 if (package[i] == '@') {
173 package[i] = '_';
174 package.insert(i + 1, "V");
175 continue;
176 }
177 }
Steven Moreland384792b2017-06-19 18:24:40 -0700178 auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700179 + package + "_" + mInterfaceName).c_str());
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700180 if ((error = dlerror()) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000181 LOG(WARNING)
182 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700183 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000184 continue;
185 }
186 instrumentationCallbacks->push_back(cb);
187 LOG(INFO) << "Register instrumentation callback from "
188 << file->d_name;
189 }
190 closedir(dir);
191 }
192#else
193 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800194 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000195 return;
196#endif
197}
198
199bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
200#ifdef LIBHIDL_TARGET_DEBUGGABLE
201 if (file->d_type != DT_REG) return false;
202 std::cmatch cm;
203 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
204 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800205#else
206 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000207#endif
208 return false;
209}
210
Steven Moreland337c3ae2016-11-22 13:37:32 -0800211} // namespace details
212} // namespace hardware
213} // namespace android