blob: 3bb27f81af9658141fd3e0a554edc6e96cb885cb [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>
28#endif
Steven Moreland337c3ae2016-11-22 13:37:32 -080029
30namespace android {
31namespace hardware {
32namespace details {
33
Hridya Valsarajub7370302017-03-08 14:39:23 -080034void logAlwaysFatal(const char *message) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080035 LOG(FATAL) << message;
36}
37
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000038// ----------------------------------------------------------------------
39// HidlInstrumentor implementation.
40HidlInstrumentor::HidlInstrumentor(
41 const std::string &package,
42 const std::string &interface)
43 : mInstrumentationLibPackage(package), mInterfaceName(interface) {
44 configureInstrumentation(false);
45}
46
47HidlInstrumentor:: ~HidlInstrumentor() {}
48
49void HidlInstrumentor::configureInstrumentation(bool log) {
50 bool enable_instrumentation = property_get_bool(
51 "hal.instrumentation.enable",
52 false);
53 if (enable_instrumentation != mEnableInstrumentation) {
54 mEnableInstrumentation = enable_instrumentation;
55 if (mEnableInstrumentation) {
56 if (log) {
57 LOG(INFO) << "Enable instrumentation.";
58 }
59 registerInstrumentationCallbacks (&mInstrumentationCallbacks);
60 } else {
61 if (log) {
62 LOG(INFO) << "Disable instrumentation.";
63 }
64 mInstrumentationCallbacks.clear();
65 }
66 }
67}
68
69void HidlInstrumentor::registerInstrumentationCallbacks(
70 std::vector<InstrumentationCallback> *instrumentationCallbacks) {
71#ifdef LIBHIDL_TARGET_DEBUGGABLE
72 std::vector<std::string> instrumentationLibPaths;
73 char instrumentation_lib_path[PROPERTY_VALUE_MAX];
74 if (property_get("hal.instrumentation.lib.path",
75 instrumentation_lib_path,
76 "") > 0) {
77 instrumentationLibPaths.push_back(instrumentation_lib_path);
78 } else {
79 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_SYSTEM);
80 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_VENDOR);
81 instrumentationLibPaths.push_back(HAL_LIBRARY_PATH_ODM);
82 }
83
84 for (auto path : instrumentationLibPaths) {
85 DIR *dir = opendir(path.c_str());
86 if (dir == 0) {
87 LOG(WARNING) << path << " does not exist. ";
88 return;
89 }
90
91 struct dirent *file;
92 while ((file = readdir(dir)) != NULL) {
93 if (!isInstrumentationLib(file))
94 continue;
95
96 void *handle = dlopen((path + file->d_name).c_str(), RTLD_NOW);
97 char *error;
98 if (handle == nullptr) {
99 LOG(WARNING) << "couldn't load file: " << file->d_name
100 << " error: " << dlerror();
101 continue;
102 }
103
104 dlerror(); /* Clear any existing error */
105
106 using cb_fun = void (*)(
107 const InstrumentationEvent,
108 const char *,
109 const char *,
110 const char *,
111 const char *,
112 std::vector<void *> *);
Steven Moreland819c05d2017-04-06 17:24:22 -0700113 std::string package = mInstrumentationLibPackage;
114 for (size_t i = 0; i < package.size(); i++) {
115 if (package[i] == '.') {
116 package[i] = '_';
117 continue;
118 }
119
120 if (package[i] == '@') {
121 package[i] = '_';
122 package.insert(i + 1, "V");
123 continue;
124 }
125 }
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000126 auto cb = (cb_fun)dlsym(handle, ("HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700127 + package + "_" + mInterfaceName).c_str());
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000128 if ((error = dlerror()) != NULL) {
129 LOG(WARNING)
130 << "couldn't find symbol: HIDL_INSTRUMENTATION_FUNCTION_"
Steven Moreland819c05d2017-04-06 17:24:22 -0700131 << package << "_" << mInterfaceName << ", error: " << error;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000132 continue;
133 }
134 instrumentationCallbacks->push_back(cb);
135 LOG(INFO) << "Register instrumentation callback from "
136 << file->d_name;
137 }
138 closedir(dir);
139 }
140#else
141 // No-op for user builds.
Steven Morelandead33e22017-02-21 19:19:39 -0800142 (void) instrumentationCallbacks;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000143 return;
144#endif
145}
146
147bool HidlInstrumentor::isInstrumentationLib(const dirent *file) {
148#ifdef LIBHIDL_TARGET_DEBUGGABLE
149 if (file->d_type != DT_REG) return false;
150 std::cmatch cm;
151 std::regex e("^" + mInstrumentationLibPackage + "(.*).profiler.so$");
152 if (std::regex_match(file->d_name, cm, e)) return true;
Steven Morelandead33e22017-02-21 19:19:39 -0800153#else
154 (void) file;
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000155#endif
156 return false;
157}
158
Steven Moreland337c3ae2016-11-22 13:37:32 -0800159} // namespace details
160} // namespace hardware
161} // namespace android