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