blob: 843fcad549a438ddc898153a181c6f556717555c [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.
43HidlInstrumentor::HidlInstrumentor(
44 const std::string &package,
45 const std::string &interface)
46 : mInstrumentationLibPackage(package), mInterfaceName(interface) {
47 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) {
65 bool enable_instrumentation = property_get_bool(
66 "hal.instrumentation.enable",
67 false);
68 if (enable_instrumentation != mEnableInstrumentation) {
69 mEnableInstrumentation = enable_instrumentation;
70 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;
88 char instrumentation_lib_path[PROPERTY_VALUE_MAX];
89 if (property_get("hal.instrumentation.lib.path",
90 instrumentation_lib_path,
91 "") > 0) {
92 instrumentationLibPaths.push_back(instrumentation_lib_path);
93 } else {
94 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
95 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
96 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
97 }
98
99 for (auto path : instrumentationLibPaths) {
100 DIR *dir = opendir(path.c_str());
101 if (dir == 0) {
102 LOG(WARNING) << path << " does not exist. ";
103 return;
104 }
105
106 struct dirent *file;
107 while ((file = readdir(dir)) != NULL) {
108 if (!isInstrumentationLib(file))
109 continue;
110
111 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
112 char *error;
113 if (handle == nullptr) {
114 LOG(WARNING) << "couldn't load file: " << file->d_name
115 << " error: " << dlerror();
116 continue;
117 }
118
119 dlerror(); /* Clear any existing error */
120
121 using cb_fun = void (*)(
122 const InstrumentationEvent,
123 const char *,
124 const char *,
125 const char *,
126 const char *,
127 std::vector<void *> *);
Steven Morelandda8e6172017-04-06 17:24:22 -0700128 std::string package = mInstrumentationLibPackage;
129 for (size_t i = 0; i < package.size(); i++) {
130 if (package[i] == '.') {
131 package[i] = '_';
132 continue;
133 }
134
135 if (package[i] == '@') {
136 package[i] = '_';
137 package.insert(i + 1, "V");
138 continue;
139 }
140 }
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000141 auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Morelandda8e6172017-04-06 17:24:22 -0700142 + package + "_" + mInterfaceName).c_str());
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000143 if ((error = dlerror()) != NULL) {
144 LOG(WARNING)
145 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Morelandda8e6172017-04-06 17:24:22 -0700146 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000147 continue;
148 }
149 instrumentationCallbacks->push_back(cb);
150 LOG(INFO) << "Register instrumentation callback from "
151 << file->d_name;
152 }
153 closedir(dir);
154 }
155#else
156 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800157 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000158 return;
159#endif
160}
161
162bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
163#ifdef LIBHIDL_TARGET_DEBUGGABLE
164 if (file->d_type != DT_REG) return false;
165 std::cmatch cm;
166 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
167 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800168#else
169 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000170#endif
171 return false;
172}
173
Steven Moreland337c3ae2016-11-22 13:37:32 -0800174} // namespace details
175} // namespace hardware
176} // namespace android