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