1 //===-- IRExecutionUnit.h ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef lldb_IRMemoryMap_h_ 11 #define lldb_IRMemoryMap_h_ 12 13 #include "lldb/lldb-public.h" 14 #include "lldb/Core/DataBufferHeap.h" 15 #include "lldb/Core/UserID.h" 16 17 #include <map> 18 19 namespace lldb_private 20 { 21 22 //---------------------------------------------------------------------- 23 /// @class IRMemoryMap IRMemoryMap.h "lldb/Expression/IRMemoryMap.h" 24 /// @brief Encapsulates memory that may exist in the process but must 25 /// also be available in the host process. 26 /// 27 /// This class encapsulates a group of memory objects that must be readable 28 /// or writable from the host process regardless of whether the process 29 /// exists. This allows the IR interpreter as well as JITted code to access 30 /// the same memory. All allocations made by this class are represented as 31 /// disjoint intervals. 32 /// 33 /// Point queries against this group of memory objects can be made by the 34 /// address in the tar at which they reside. If the inferior does not 35 /// exist, allocations still get made-up addresses. If an inferior appears 36 /// at some point, then those addresses need to be re-mapped. 37 //---------------------------------------------------------------------- 38 class IRMemoryMap 39 { 40 public: 41 IRMemoryMap (lldb::TargetSP target_sp); 42 ~IRMemoryMap (); 43 44 enum AllocationPolicy { 45 eAllocationPolicyInvalid = 0, ///< It is an error for an allocation to have this policy. 46 eAllocationPolicyHostOnly, ///< This allocation was created in the host and will never make it into the process. 47 ///< It is an error to create other types of allocations while such allocations exist. 48 eAllocationPolicyMirror, ///< The intent is that this allocation exist both in the host and the process and have 49 ///< the same content in both. 50 eAllocationPolicyProcessOnly ///< The intent is that this allocation exist only in the process. 51 }; 52 53 lldb::addr_t Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, Error &error); 54 void Leak (lldb::addr_t process_address, Error &error); 55 void Free (lldb::addr_t process_address, Error &error); 56 57 void WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error); 58 void WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error); 59 void WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error); 60 void ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error); 61 void ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error); 62 void ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error); 63 bool GetAllocSize(lldb::addr_t address, size_t &size); 64 void GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error); 65 66 lldb::ByteOrder GetByteOrder(); 67 uint32_t GetAddressByteSize(); 68 69 // This function can return NULL. 70 ExecutionContextScope *GetBestExecutionContextScope() const; 71 72 lldb::TargetSP GetTarget()73 GetTarget () 74 { 75 return m_target_wp.lock(); 76 } 77 78 protected: 79 // This function should only be used if you know you are using the JIT. 80 // Any other cases should use GetBestExecutionContextScope(). 81 82 lldb::ProcessWP & GetProcessWP()83 GetProcessWP () 84 { 85 return m_process_wp; 86 } 87 88 private: 89 struct Allocation 90 { 91 lldb::addr_t m_process_alloc; ///< The (unaligned) base for the remote allocation 92 lldb::addr_t m_process_start; ///< The base address of the allocation in the process 93 size_t m_size; ///< The size of the requested allocation 94 uint32_t m_permissions; ///< The access permissions on the memory in the process. In the host, the memory is always read/write. 95 uint8_t m_alignment; ///< The alignment of the requested allocation 96 DataBufferHeap m_data; 97 98 ///< Flags 99 AllocationPolicy m_policy; 100 bool m_leak; 101 public: 102 Allocation (lldb::addr_t process_alloc, 103 lldb::addr_t process_start, 104 size_t size, 105 uint32_t permissions, 106 uint8_t alignment, 107 AllocationPolicy m_policy); 108 AllocationAllocation109 Allocation () : 110 m_process_alloc (LLDB_INVALID_ADDRESS), 111 m_process_start (LLDB_INVALID_ADDRESS), 112 m_size (0), 113 m_permissions (0), 114 m_alignment (0), 115 m_data (), 116 m_policy (eAllocationPolicyInvalid), 117 m_leak (false) 118 { 119 } 120 }; 121 122 lldb::ProcessWP m_process_wp; 123 lldb::TargetWP m_target_wp; 124 typedef std::map<lldb::addr_t, Allocation> AllocationMap; 125 AllocationMap m_allocations; 126 127 lldb::addr_t FindSpace (size_t size); 128 bool ContainsHostOnlyAllocations (); 129 AllocationMap::iterator FindAllocation (lldb::addr_t addr, size_t size); 130 131 // Returns true if the given allocation intersects any allocation in the memory map. 132 bool IntersectsAllocation (lldb::addr_t addr, size_t size) const; 133 134 // Returns true if the two given allocations intersect each other. 135 static bool AllocationsIntersect (lldb::addr_t addr1, size_t size1, lldb::addr_t addr2, size_t size2); 136 }; 137 138 } 139 140 #endif 141