Home | History | Annotate | Download | only in hidl
      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 {
     44     hidl_pointer()
     45         : _pad(0) {
     46     }
     47     hidl_pointer(T* ptr) : hidl_pointer() { mPointer = ptr; }
     48     hidl_pointer(const hidl_pointer<T>& other) : hidl_pointer() { mPointer = other.mPointer; }
     49     hidl_pointer(hidl_pointer<T>&& other) : hidl_pointer() { *this = std::move(other); }
     50 
     51     hidl_pointer &operator=(const hidl_pointer<T>& other) {
     52         mPointer = other.mPointer;
     53         return *this;
     54     }
     55     hidl_pointer &operator=(hidl_pointer<T>&& other) {
     56         mPointer = other.mPointer;
     57         other.mPointer = nullptr;
     58         return *this;
     59     }
     60     hidl_pointer &operator=(T* ptr) {
     61         mPointer = ptr;
     62         return *this;
     63     }
     64 
     65     operator T*() const {
     66         return mPointer;
     67     }
     68     explicit operator void*() const { // requires explicit cast to avoid ambiguity
     69         return mPointer;
     70     }
     71     T& operator*() const {
     72         return *mPointer;
     73     }
     74     T* operator->() const {
     75         return mPointer;
     76     }
     77     T &operator[](size_t index) {
     78         return mPointer[index];
     79     }
     80     const T &operator[](size_t index) const {
     81         return mPointer[index];
     82     }
     83 
     84 private:
     85     union {
     86         T* mPointer;
     87         uint64_t _pad;
     88     };
     89 };
     90 
     91 #define HAL_LIBRARY_PATH_SYSTEM_64BIT "/system/lib64/hw/"
     92 #define HAL_LIBRARY_PATH_VNDK_SP_64BIT "/system/lib64/vndk-sp/hw/"
     93 #define HAL_LIBRARY_PATH_VENDOR_64BIT "/vendor/lib64/hw/"
     94 #define HAL_LIBRARY_PATH_ODM_64BIT    "/odm/lib64/hw/"
     95 #define HAL_LIBRARY_PATH_SYSTEM_32BIT "/system/lib/hw/"
     96 #define HAL_LIBRARY_PATH_VNDK_SP_32BIT "/system/lib/vndk-sp/hw/"
     97 #define HAL_LIBRARY_PATH_VENDOR_32BIT "/vendor/lib/hw/"
     98 #define HAL_LIBRARY_PATH_ODM_32BIT    "/odm/lib/hw/"
     99 
    100 #if defined(__LP64__)
    101 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_64BIT
    102 #define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_64BIT
    103 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_64BIT
    104 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_64BIT
    105 #else
    106 #define HAL_LIBRARY_PATH_SYSTEM HAL_LIBRARY_PATH_SYSTEM_32BIT
    107 #define HAL_LIBRARY_PATH_VNDK_SP HAL_LIBRARY_PATH_VNDK_SP_32BIT
    108 #define HAL_LIBRARY_PATH_VENDOR HAL_LIBRARY_PATH_VENDOR_32BIT
    109 #define HAL_LIBRARY_PATH_ODM    HAL_LIBRARY_PATH_ODM_32BIT
    110 #endif
    111 
    112 // ----------------------------------------------------------------------
    113 // Class that provides Hidl instrumentation utilities.
    114 struct HidlInstrumentor {
    115     // Event that triggers the instrumentation. e.g. enter of an API call on
    116     // the server/client side, exit of an API call on the server/client side
    117     // etc.
    118     enum InstrumentationEvent {
    119         SERVER_API_ENTRY = 0,
    120         SERVER_API_EXIT,
    121         CLIENT_API_ENTRY,
    122         CLIENT_API_EXIT,
    123         SYNC_CALLBACK_ENTRY,
    124         SYNC_CALLBACK_EXIT,
    125         ASYNC_CALLBACK_ENTRY,
    126         ASYNC_CALLBACK_EXIT,
    127         PASSTHROUGH_ENTRY,
    128         PASSTHROUGH_EXIT,
    129     };
    130 
    131     // Signature of the instrumentation callback function.
    132     using InstrumentationCallback = std::function<void(
    133             const InstrumentationEvent event,
    134             const char *package,
    135             const char *version,
    136             const char *interface,
    137             const char *method,
    138             std::vector<void *> *args)>;
    139 
    140     explicit HidlInstrumentor(
    141             const std::string &package,
    142             const std::string &insterface);
    143     virtual ~HidlInstrumentor();
    144 
    145    public:
    146     const std::vector<InstrumentationCallback>& getInstrumentationCallbacks() {
    147         return mInstrumentationCallbacks;
    148     }
    149     bool isInstrumentationEnabled() { return mEnableInstrumentation; }
    150 
    151    protected:
    152     // Set mEnableInstrumentation based on system property
    153     // hal.instrumentation.enable, register/de-register instrumentation
    154     // callbacks if mEnableInstrumentation is true/false.
    155     void configureInstrumentation(bool log=true);
    156     // Function that lookup and dynamically loads the hidl instrumentation
    157     // libraries and registers the instrumentation callback functions.
    158     //
    159     // The instrumentation libraries should be stored under any of the following
    160     // directories: HAL_LIBRARY_PATH_SYSTEM, HAL_LIBRARY_PATH_VNDK_SP,
    161     // HAL_LIBRARY_PATH_VENDOR and HAL_LIBRARY_PATH_ODM.
    162     // The name of instrumentation libraries should follow pattern:
    163     // ^profilerPrefix(.*).profiler.so$
    164     //
    165     // Each instrumentation library is expected to implement the instrumentation
    166     // function called HIDL_INSTRUMENTATION_FUNCTION.
    167     //
    168     // A no-op for user build.
    169     void registerInstrumentationCallbacks(
    170             std::vector<InstrumentationCallback> *instrumentationCallbacks);
    171 
    172     // Utility function to determine whether a give file is a instrumentation
    173     // library (i.e. the file name follow the expected pattern).
    174     bool isInstrumentationLib(const dirent *file);
    175 
    176     // A list of registered instrumentation callbacks.
    177     std::vector<InstrumentationCallback> mInstrumentationCallbacks;
    178     // Flag whether to enable instrumentation.
    179     bool mEnableInstrumentation;
    180     // Prefix to lookup the instrumentation libraries.
    181     std::string mInstrumentationLibPackage;
    182     // Used for dlsym to load the profiling method for given interface.
    183     std::string mInterfaceName;
    184 
    185 };
    186 
    187 }  // namespace details
    188 }  // namespace hardware
    189 }  // namespace android
    190 
    191 #endif  // ANDROID_HIDL_INTERNAL_H
    192