blob: b41c0b9e0c4d1380327e676cedb6f5312d3150c6 [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>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000027#include <regex>
Ryan Campbell69aff752017-07-27 13:49:46 -070028#include <utils/misc.h>
29
30extern "C" __attribute__((weak)) void __sanitizer_cov_dump();
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000031#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080032
33namespace android {
34namespace hardware {
35namespace details {
36
Ryan Campbell69aff752017-07-27 13:49:46 -070037void logAlwaysFatal(const char* message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080038 LOG(FATAL) << message;
39}
40
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000041// ----------------------------------------------------------------------
42// HidlInstrumentor implementation.
Steven Moreland384792b2017-06-19 18:24:40 -070043HidlInstrumentor::HidlInstrumentor(const std::string& package, const std::string& interface)
44 : mEnableInstrumentation(false),
45 mInstrumentationLibPackage(package),
46 mInterfaceName(interface) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000047 configureInstrumentation(false);
Ryan Campbell69aff752017-07-27 13:49:46 -070048#ifdef LIBHIDL_TARGET_DEBUGGABLE
49 if (__sanitizer_cov_dump != nullptr) {
50 ::android::add_sysprop_change_callback(
51 []() {
52 bool enableCoverage = property_get_bool("hal.coverage.enable", false);
53 if (enableCoverage) {
54 __sanitizer_cov_dump();
55 }
56 },
57 0);
58 }
59#endif
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000060}
61
Ryan Campbell69aff752017-07-27 13:49:46 -070062HidlInstrumentor::~HidlInstrumentor() {}
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000063
64void HidlInstrumentor::configureInstrumentation(bool log) {
Steven Moreland384792b2017-06-19 18:24:40 -070065 bool enableInstrumentation = property_get_bool(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000066 "hal.instrumentation.enable",
67 false);
Steven Moreland384792b2017-06-19 18:24:40 -070068 if (enableInstrumentation != mEnableInstrumentation) {
69 mEnableInstrumentation = enableInstrumentation;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000070 if (mEnableInstrumentation) {
71 if (log) {
72 LOG(INFO) << "Enable instrumentation.";
73 }
74 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
75 } else {
76 if (log) {
77 LOG(INFO) << "Disable instrumentation.";
78 }
79 mInstrumentationCallbacks.clear();
80 }
81 }
82}
83
84void HidlInstrumentor::registerInstrumentationCallbacks(
85 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
86#ifdef LIBHIDL_TARGET_DEBUGGABLE
87 std::vector<std::string> instrumentationLibPaths;
Steven Moreland384792b2017-06-19 18:24:40 -070088 char instrumentationLibPath[PROPERTY_VALUE_MAX];
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000089 if (property_get("hal.instrumentation.lib.path",
Steven Moreland384792b2017-06-19 18:24:40 -070090 instrumentationLibPath,
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000091 "") > 0) {
Steven Moreland384792b2017-06-19 18:24:40 -070092 instrumentationLibPaths.push_back(instrumentationLibPath);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000093 } else {
94 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
Jiyong Park56eb1492017-08-04 16:16:13 +090095 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VNDK_SP);
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000096 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
97 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
98 }
99
Chih-Hung Hsieh41649d52017-08-03 14:27:21 -0700100 for (const auto& path : instrumentationLibPaths) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000101 DIR *dir = opendir(path.c_str());
102 if (dir == 0) {
103 LOG(WARNING) << path << " does not exist. ";
104 return;
105 }
106
107 struct dirent *file;
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700108 while ((file = readdir(dir)) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000109 if (!isInstrumentationLib(file))
110 continue;
111
112 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
113 char *error;
114 if (handle == nullptr) {
115 LOG(WARNING) << "couldn't load file: " << file->d_name
116 << " error: " << dlerror();
117 continue;
118 }
119
120 dlerror(); /* Clear any existing error */
121
Steven Moreland384792b2017-06-19 18:24:40 -0700122 using cbFun = void (*)(
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000123 const InstrumentationEvent,
124 const char *,
125 const char *,
126 const char *,
127 const char *,
128 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700129 std::string package = mInstrumentationLibPackage;
130 for (size_t i = 0; i < package.size(); i++) {
131 if (package[i] == '.') {
132 package[i] = '_';
133 continue;
134 }
135
136 if (package[i] == '@') {
137 package[i] = '_';
138 package.insert(i + 1, "V");
139 continue;
140 }
141 }
Steven Moreland384792b2017-06-19 18:24:40 -0700142 auto cb = (cbFun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700143 + package + "_" + mInterfaceName).c_str());
Stephen Hinesfd9ecee2017-09-27 18:52:52 -0700144 if ((error = dlerror()) != nullptr) {
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000145 LOG(WARNING)
146 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700147 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000148 continue;
149 }
150 instrumentationCallbacks->push_back(cb);
151 LOG(INFO) << "Register instrumentation callback from "
152 << file->d_name;
153 }
154 closedir(dir);
155 }
156#else
157 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800158 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000159 return;
160#endif
161}
162
163bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
164#ifdef LIBHIDL_TARGET_DEBUGGABLE
165 if (file->d_type != DT_REG) return false;
166 std::cmatch cm;
167 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
168 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800169#else
170 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000171#endif
172 return false;
173}
174
Steven Moreland337c3ae2016-11-22 13:37:32 -0800175} // namespace details
176} // namespace hardware
177} // namespace android