| 6 | #define BASE_PROFILER_MODULE_CACHE_H_ |
| 7 |
| 8 | #include <memory> |
| 9 | #include <set> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 |
| 13 | #include "base/base_export.h" |
| 14 | #include "base/containers/flat_set.h" |
| 15 | #include "base/files/file_path.h" |
| 16 | #include "build/build_config.h" |
| 17 |
| 18 | #if defined(OS_WIN) |
| 19 | #include <windows.h> |
| 20 | #endif |
| 21 |
| 22 | namespace base { |
| 23 |
| 24 | // Supports cached lookup of modules by address, with caching based on module |
| 25 | // address ranges. |
| 26 | // |
| 27 | // Cached lookup is necessary on Mac for performance, due to an inefficient |
| 28 | // dladdr implementation. See https://crrev.com/487092. |
| 29 | // |
| 30 | // Cached lookup is beneficial on Windows to minimize use of the loader |
| 31 | // lock. Note however that the cache retains a handle to looked-up modules for |
| 32 | // its lifetime, which may result in pinning modules in memory that were |
| 33 | // transiently loaded by the OS. |
| 34 | class BASE_EXPORT ModuleCache { |
| 35 | public: |
| 36 | // Module represents a binary module (executable or library) and its |
| 37 | // associated state. |
| 38 | class BASE_EXPORT Module { |
| 39 | public: |
| 40 | Module() = default; |
| 41 | virtual ~Module() = default; |
| 42 |
| 43 | Module(const Module&) = delete; |
| 44 | Module& operator=(const Module&) = delete; |
| 45 |
| 46 | // Gets the base address of the module. |
| 47 | virtual uintptr_t GetBaseAddress() const = 0; |
| 48 |
| 49 | // Gets the opaque binary string that uniquely identifies a particular |
| 50 | // program version with high probability. This is parsed from headers of the |
| 51 | // loaded module. |
| 52 | // For binaries generated by GNU tools: |
| 53 | // Contents of the .note.gnu.build-id field. |
| 54 | // On Windows: |
| 55 | // GUID + AGE in the debug image headers of a module. |
| 56 | virtual std::string GetId() const = 0; |
| 57 |
| 58 | // Gets the debug basename of the module. This is the basename of the PDB |
| 59 | // file on Windows and the basename of the binary on other platforms. |
| 60 | virtual FilePath GetDebugBasename() const = 0; |
| 61 |
| 62 | // Gets the size of the module. |
| 63 | virtual size_t GetSize() const = 0; |
| 64 |
| 65 | // True if this is a native module. |
| 66 | virtual bool IsNative() const = 0; |
| 67 | }; |
| 68 |
| 69 | ModuleCache(); |
| 70 | ~ModuleCache(); |
| 71 |
| 72 | // Gets the module containing |address| or nullptr if |address| is not within |
| 73 | // a module. The returned module remains owned by and has the same lifetime as |
| 74 | // the ModuleCache object. |
| 75 | const Module* GetModuleForAddress(uintptr_t address); |
| 76 | std::vector<const Module*> GetModules() const; |
| 77 |
| 78 | // Updates the set of non-native modules maintained by the |
| 79 | // ModuleCache. Non-native modules represent regions of non-native executable |
| 80 | // code such as V8 generated code. |
| 81 | // |
| 82 | // Note that non-native modules may be embedded within native modules, as in |
| 83 | // the case of V8 builtin code compiled within Chrome. In that case |
| 84 | // GetModuleForAddress() will return the non-native module rather than the |
| 85 | // native module for the memory region it occupies. |
| 86 | // |
| 87 | // Modules in |to_remove| are removed from the set of active modules; |
| 88 | // specifically they no longer participate in the GetModuleForAddress() |
| 89 | // lookup. They continue to exist for the lifetime of the ModuleCache, |
| 90 | // however, so that existing references to them remain valid. Modules in |
| 91 | // |to_add| are added to the set of active non-native modules. |
| 92 | void UpdateNonNativeModules( |
| 93 | const std::vector<const Module*>& to_remove, |
| 94 | std::vector<std::unique_ptr<const Module>> to_add); |
| 95 |
| 96 | // Adds a custom native module to the cache. This is intended to support |
| 97 | // native modules that require custom handling. In general, native modules |
| 98 | // will be found and added automatically when invoking GetModuleForAddress(). |
| 99 | void AddCustomNativeModule(std::unique_ptr<const Module> module); |
| 100 |
| 101 | private: |
| 102 | // Heterogenously compares modules by base address, and modules and |
| 103 | // addresses. The module/address comparison considers the address equivalent |
| 104 | // to the module if the address is within the extent of the module. Combined |
| 105 | // with is_transparent this allows modules to be looked up by address in the |
| 106 | // using containers. |
| 107 | struct ModuleAndAddressCompare { |
| 108 | using is_transparent = void; |
| 109 | bool operator()(const std::unique_ptr<const Module>& m1, |
| 110 | const std::unique_ptr<const Module>& m2) const; |
| 111 | bool operator()(const std::unique_ptr<const Module>& m1, |
| 112 | uintptr_t address) const; |
| 113 | bool operator()(uintptr_t address, |
| 114 | const std::unique_ptr<const Module>& m2) const; |
| 115 | }; |
| 116 |
| 117 | // Creates a Module object for the specified memory address. Returns null if |
| 118 | // the address does not belong to a module. |
| 119 | static std::unique_ptr<const Module> CreateModuleForAddress( |
| 120 | uintptr_t address); |
| 121 |
| 122 | // Set of native modules sorted by base address. We use set rather than |
| 123 | // flat_set because the latter type has O(n^2) runtime for adding modules |
| 124 | // one-at-a-time, which is how modules are added on Windows and Mac. |
| 125 | std::set<std::unique_ptr<const Module>, ModuleAndAddressCompare> |
| 126 | native_modules_; |
| 127 |
| 128 | // Set of non-native modules currently mapped into the address space, sorted |
| 129 | // by base address. Represented as flat_set because std::set does not support |
| 130 | // extracting move-only element types prior to C++17's |
| 131 | // std::set<>::extract(). The non-native module insertion/removal patterns -- |
| 132 | // initial bulk insertion, then infrequent inserts/removals -- should work |
| 133 | // reasonably well with the flat_set complexity guarantees. Separate from |
| 134 | // native_modules_ to support preferential lookup of non-native modules |
| 135 | // embedded in native modules; see comment on UpdateNonNativeModules(). |
| 136 | base::flat_set<std::unique_ptr<const Module>, ModuleAndAddressCompare> |
| 137 | non_native_modules_; |
| 138 |
| 139 | // Unsorted vector of inactive non-native modules. Inactive modules are no |
| 140 | // longer mapped in the address space and don't participate in address lookup, |
| 141 | // but are retained by the cache so that existing references to the them |
| 142 | // remain valid. Note that this cannot be represented as a set/flat_set |
| 143 | // because it can contain multiple modules that were loaded (then subsequently |
| 144 | // unloaded) at the same base address. |
| 145 | std::vector<std::unique_ptr<const Module>> inactive_non_native_modules_; |
| 146 | }; |
| 147 |
| 148 | } // namespace base |
| 149 |
| 150 | #endif // BASE_PROFILER_MODULE_CACHE_H_ |
| 151 |