blob: 80543c035abefcb4a9ce11664078110f82b1f2df [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#ifndef ANDROID_HIDL_INTERNAL_H
18#define ANDROID_HIDL_INTERNAL_H
19
20#include <cstdint>
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +000021#include <dirent.h>
22#include <functional>
23#include <string>
24#include <vector>
Steven Moreland337c3ae2016-11-22 13:37:32 -080025#include <utility>
26
27namespace android {
28namespace hardware {
29namespace details {
30
31// hidl_log_base is a base class that templatized
32// classes implemented in a header can inherit from,
33// to avoid creating dependencies on liblog.
34struct hidl_log_base {
35 void logAlwaysFatal(const char *message) const;
36};
37
38// HIDL client/server code should *NOT* use this class.
39//
40// hidl_pointer wraps a pointer without taking ownership,
41// and stores it in a union with a uint64_t. This ensures
42// that we always have enough space to store a pointer,
43// regardless of whether we're running in a 32-bit or 64-bit
44// process.
45template<typename T>
46struct hidl_pointer {
47 hidl_pointer()
Hridya Valsarajudda25622017-02-10 16:32:41 -080048 : _pad(0) {
Steven Moreland337c3ae2016-11-22 13:37:32 -080049 }
50 hidl_pointer(T* ptr)
51 : mPointer(ptr) {
52 }
53 hidl_pointer(const hidl_pointer<T>& other) {
54 mPointer = other.mPointer;
55 }
56 hidl_pointer(hidl_pointer<T>&& other) {
57 *this = std::move(other);
58 }
59
60 hidl_pointer &operator=(const hidl_pointer<T>& other) {
61 mPointer = other.mPointer;
62 return *this;
63 }
64 hidl_pointer &operator=(hidl_pointer<T>&& other) {
65 mPointer = other.mPointer;
66 other.mPointer = nullptr;
67 return *this;
68 }
69 hidl_pointer &operator=(T* ptr) {
70 mPointer = ptr;
71 return *this;
72 }
73
74 operator T*() const {
75 return mPointer;
76 }
77 explicit operator void*() const { // requires explicit cast to avoid ambiguity
78 return mPointer;
79 }
80 T& operator*() const {
81 return *mPointer;
82 }
83 T* operator->() const {
84 return mPointer;
85 }
86 T &operator[](size_t index) {
87 return mPointer[index];
88 }
89 const T &operator[](size_t index) const {
90 return mPointer[index];
91 }
Yifan Hongbe7a6882017-01-05 17:30:17 -080092
Steven Moreland337c3ae2016-11-22 13:37:32 -080093private:
94 union {
95 T* mPointer;
96 uint64_t _pad;
97 };
98};
99
Zhuoyao Zhang7a57de62017-02-15 21:04:19 +0000100#if defined(__LP64__)
101#define HAL_LIBRARY_PATH_SYSTEM "/system/lib64/hw/"
102#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib64/hw/"
103#define HAL_LIBRARY_PATH_ODM "/odm/lib64/hw/"
104#else
105#define HAL_LIBRARY_PATH_SYSTEM "/system/lib/hw/"
106#define HAL_LIBRARY_PATH_VENDOR "/vendor/lib/hw/"
107#define HAL_LIBRARY_PATH_ODM "/odm/lib/hw/"
108#endif
109
110// ----------------------------------------------------------------------
111// Class that provides Hidl instrumentation utilities.
112struct HidlInstrumentor {
113 // Event that triggers the instrumentation. e.g. enter of an API call on
114 // the server/client side, exit of an API call on the server/client side
115 // etc.
116 enum InstrumentationEvent {
117 SERVER_API_ENTRY = 0,
118 SERVER_API_EXIT,
119 CLIENT_API_ENTRY,
120 CLIENT_API_EXIT,
121 SYNC_CALLBACK_ENTRY,
122 SYNC_CALLBACK_EXIT,
123 ASYNC_CALLBACK_ENTRY,
124 ASYNC_CALLBACK_EXIT,
125 PASSTHROUGH_ENTRY,
126 PASSTHROUGH_EXIT,
127 };
128
129 // Signature of the instrumentation callback function.
130 using InstrumentationCallback = std::function<void(
131 const InstrumentationEvent event,
132 const char *package,
133 const char *version,
134 const char *interface,
135 const char *method,
136 std::vector<void *> *args)>;
137
138 explicit HidlInstrumentor(
139 const std::string &package,
140 const std::string &insterface);
141 virtual ~HidlInstrumentor();
142
143 protected:
144 // Set mEnableInstrumentation based on system property
145 // hal.instrumentation.enable, register/de-register instrumentation
146 // callbacks if mEnableInstrumentation is true/false.
147 void configureInstrumentation(bool log=true);
148 // Function that lookup and dynamically loads the hidl instrumentation
149 // libraries and registers the instrumentation callback functions.
150 //
151 // The instrumentation libraries should be stored under any of the following
152 // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VENDOR and
153 // HAL_LIBRARY_PATH_ODM. The name of instrumentation libraries should
154 // follow pattern: ^profilerPrefix(.*).profiler.so$
155 //
156 // Each instrumentation library is expected to implement the instrumentation
157 // function called HIDL_INSTRUMENTATION_FUNCTION.
158 //
159 // A no-op for user build.
160 void registerInstrumentationCallbacks(
161 std::vector<InstrumentationCallback> *instrumentationCallbacks);
162
163 // Utility function to determine whether a give file is a instrumentation
164 // library (i.e. the file name follow the expected pattern).
165 bool isInstrumentationLib(const dirent *file);
166
167 // A list of registered instrumentation callbacks.
168 std::vector<InstrumentationCallback> mInstrumentationCallbacks;
169 // Flag whether to enable instrumentation.
170 bool mEnableInstrumentation;
171 // Prefix to lookup the instrumentation libraries.
172 std::string mInstrumentationLibPackage;
173 // Used for dlsym to load the profiling method for given interface.
174 std::string mInterfaceName;
175
176};
177
Steven Moreland337c3ae2016-11-22 13:37:32 -0800178} // namespace details
179} // namespace hardware
180} // namespace android
181
182#endif // ANDROID_HIDL_INTERNAL_H