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