Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 1 | //===-- IRMemoryMap.cpp -----------------------------------------*- 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 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 10 | #include "lldb/Core/DataBufferHeap.h" |
| 11 | #include "lldb/Core/DataExtractor.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Error.h" |
| 13 | #include "lldb/Core/Log.h" |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Scalar.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 15 | #include "lldb/Expression/IRMemoryMap.h" |
| 16 | #include "lldb/Target/Process.h" |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Target.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb_private; |
| 20 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 21 | IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) : |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 22 | m_target_wp(target_sp) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 23 | { |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 24 | if (target_sp) |
| 25 | m_process_wp = target_sp->GetProcessSP(); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | IRMemoryMap::~IRMemoryMap () |
| 29 | { |
| 30 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 31 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 32 | if (process_sp) |
| 33 | { |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 34 | AllocationMap::iterator iter; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 35 | |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 36 | Error err; |
| 37 | |
| 38 | while ((iter = m_allocations.begin()) != m_allocations.end()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 39 | { |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 40 | err.Clear(); |
Sean Callanan | fbf5c68 | 2013-05-22 22:49:06 +0000 | [diff] [blame] | 41 | if (iter->second.m_leak) |
| 42 | m_allocations.erase(iter); |
| 43 | else |
| 44 | Free(iter->first, err); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | lldb::addr_t |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 50 | IRMemoryMap::FindSpace (size_t size, bool zero_memory) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 51 | { |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 52 | lldb::TargetSP target_sp = m_target_wp.lock(); |
| 53 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 54 | |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 55 | lldb::addr_t ret = LLDB_INVALID_ADDRESS; |
Zachary Turner | 3ddcd31 | 2014-07-09 16:42:27 +0000 | [diff] [blame] | 56 | if (size == 0) |
| 57 | return ret; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 58 | |
Sean Callanan | ad7cc46 | 2013-06-17 21:19:31 +0000 | [diff] [blame] | 59 | if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) |
Sean Callanan | df56540 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 60 | { |
| 61 | Error alloc_error; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 62 | |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 63 | if (!zero_memory) |
| 64 | ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable | lldb::ePermissionsWritable, alloc_error); |
| 65 | else |
| 66 | ret = process_sp->CallocateMemory(size, lldb::ePermissionsReadable | lldb::ePermissionsWritable, alloc_error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 67 | |
Sean Callanan | df56540 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 68 | if (!alloc_error.Success()) |
| 69 | return LLDB_INVALID_ADDRESS; |
| 70 | else |
| 71 | return ret; |
| 72 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 73 | |
Zachary Turner | 3ddcd31 | 2014-07-09 16:42:27 +0000 | [diff] [blame] | 74 | ret = 0; |
| 75 | if (!m_allocations.empty()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 76 | { |
Zachary Turner | 3ddcd31 | 2014-07-09 16:42:27 +0000 | [diff] [blame] | 77 | auto back = m_allocations.rbegin(); |
| 78 | lldb::addr_t addr = back->first; |
| 79 | size_t alloc_size = back->second.m_size; |
Rafael Espindola | a94ae1e | 2016-01-18 20:57:54 +0000 | [diff] [blame] | 80 | ret = llvm::alignTo(addr+alloc_size, 4096); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 81 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 82 | |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 83 | return ret; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | IRMemoryMap::AllocationMap::iterator |
| 87 | IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size) |
| 88 | { |
Sean Callanan | 1582ee6 | 2013-04-18 22:06:33 +0000 | [diff] [blame] | 89 | if (addr == LLDB_INVALID_ADDRESS) |
| 90 | return m_allocations.end(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 91 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 92 | AllocationMap::iterator iter = m_allocations.lower_bound (addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 93 | |
Sean Callanan | 14b1bae | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 94 | if (iter == m_allocations.end() || |
| 95 | iter->first > addr) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 96 | { |
| 97 | if (iter == m_allocations.begin()) |
| 98 | return m_allocations.end(); |
| 99 | iter--; |
| 100 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 101 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 102 | if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size) |
| 103 | return iter; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 104 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 105 | return m_allocations.end(); |
| 106 | } |
| 107 | |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 108 | bool |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 109 | IRMemoryMap::IntersectsAllocation (lldb::addr_t addr, size_t size) const |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 110 | { |
| 111 | if (addr == LLDB_INVALID_ADDRESS) |
| 112 | return false; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 113 | |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 114 | AllocationMap::const_iterator iter = m_allocations.lower_bound (addr); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 115 | |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 116 | // Since we only know that the returned interval begins at a location greater than or |
| 117 | // equal to where the given interval begins, it's possible that the given interval |
| 118 | // intersects either the returned interval or the previous interval. Thus, we need to |
| 119 | // check both. Note that we only need to check these two intervals. Since all intervals |
| 120 | // are disjoint it is not possible that an adjacent interval does not intersect, but a |
| 121 | // non-adjacent interval does intersect. |
| 122 | if (iter != m_allocations.end()) { |
Zachary Turner | cfcd791 | 2014-06-25 18:40:58 +0000 | [diff] [blame] | 123 | if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size)) |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 124 | return true; |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 125 | } |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 126 | |
| 127 | if (iter != m_allocations.begin()) { |
| 128 | --iter; |
Zachary Turner | cfcd791 | 2014-06-25 18:40:58 +0000 | [diff] [blame] | 129 | if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size)) |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 130 | return true; |
| 131 | } |
| 132 | |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 133 | return false; |
| 134 | } |
| 135 | |
Zachary Turner | 1536244 | 2014-06-25 18:37:19 +0000 | [diff] [blame] | 136 | bool |
| 137 | IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1, lldb::addr_t addr2, size_t size2) { |
| 138 | // Given two half open intervals [A, B) and [X, Y), the only 6 permutations that satisfy |
| 139 | // A<B and X<Y are the following: |
| 140 | // A B X Y |
| 141 | // A X B Y (intersects) |
| 142 | // A X Y B (intersects) |
| 143 | // X A B Y (intersects) |
| 144 | // X A Y B (intersects) |
| 145 | // X Y A B |
| 146 | // The first is B <= X, and the last is Y <= A. |
| 147 | // So the condition is !(B <= X || Y <= A)), or (X < B && A < Y) |
| 148 | return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2)); |
| 149 | } |
| 150 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 151 | lldb::ByteOrder |
| 152 | IRMemoryMap::GetByteOrder() |
| 153 | { |
| 154 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 155 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 156 | if (process_sp) |
| 157 | return process_sp->GetByteOrder(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 158 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 159 | lldb::TargetSP target_sp = m_target_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 160 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 161 | if (target_sp) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 162 | return target_sp->GetArchitecture().GetByteOrder(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 163 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 164 | return lldb::eByteOrderInvalid; |
| 165 | } |
| 166 | |
| 167 | uint32_t |
| 168 | IRMemoryMap::GetAddressByteSize() |
| 169 | { |
| 170 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 171 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 172 | if (process_sp) |
| 173 | return process_sp->GetAddressByteSize(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 174 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 175 | lldb::TargetSP target_sp = m_target_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 176 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 177 | if (target_sp) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 178 | return target_sp->GetArchitecture().GetAddressByteSize(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 179 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 180 | return UINT32_MAX; |
| 181 | } |
| 182 | |
| 183 | ExecutionContextScope * |
Greg Clayton | 23f8c95 | 2014-03-24 23:10:19 +0000 | [diff] [blame] | 184 | IRMemoryMap::GetBestExecutionContextScope() const |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 185 | { |
| 186 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 187 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 188 | if (process_sp) |
| 189 | return process_sp.get(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 190 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 191 | lldb::TargetSP target_sp = m_target_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 192 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 193 | if (target_sp) |
| 194 | return target_sp.get(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 195 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 196 | return NULL; |
| 197 | } |
| 198 | |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 199 | IRMemoryMap::Allocation::Allocation (lldb::addr_t process_alloc, |
| 200 | lldb::addr_t process_start, |
| 201 | size_t size, |
| 202 | uint32_t permissions, |
| 203 | uint8_t alignment, |
Michael Sartain | 6fea779 | 2013-08-06 22:21:08 +0000 | [diff] [blame] | 204 | AllocationPolicy policy) : |
| 205 | m_process_alloc (process_alloc), |
| 206 | m_process_start (process_start), |
| 207 | m_size (size), |
| 208 | m_permissions (permissions), |
| 209 | m_alignment (alignment), |
| 210 | m_policy (policy), |
| 211 | m_leak (false) |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 212 | { |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 213 | switch (policy) |
| 214 | { |
| 215 | default: |
| 216 | assert (0 && "We cannot reach this!"); |
| 217 | case eAllocationPolicyHostOnly: |
| 218 | m_data.SetByteSize(size); |
| 219 | memset(m_data.GetBytes(), 0, size); |
| 220 | break; |
| 221 | case eAllocationPolicyProcessOnly: |
| 222 | break; |
| 223 | case eAllocationPolicyMirror: |
| 224 | m_data.SetByteSize(size); |
| 225 | memset(m_data.GetBytes(), 0, size); |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 230 | lldb::addr_t |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 231 | IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Error &error) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 232 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 233 | lldb_private::Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)); |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 234 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 235 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 236 | lldb::ProcessSP process_sp; |
| 237 | lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS; |
| 238 | lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS; |
Matt Kopec | 750dcc3 | 2013-04-26 17:48:01 +0000 | [diff] [blame] | 239 | |
| 240 | size_t alignment_mask = alignment - 1; |
| 241 | size_t allocation_size; |
| 242 | |
| 243 | if (size == 0) |
| 244 | allocation_size = alignment; |
| 245 | else |
| 246 | allocation_size = (size & alignment_mask) ? ((size + alignment) & (~alignment_mask)) : size; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 247 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 248 | switch (policy) |
| 249 | { |
| 250 | default: |
| 251 | error.SetErrorToGenericError(); |
| 252 | error.SetErrorString("Couldn't malloc: invalid allocation policy"); |
| 253 | return LLDB_INVALID_ADDRESS; |
| 254 | case eAllocationPolicyHostOnly: |
| 255 | allocation_address = FindSpace(allocation_size); |
| 256 | if (allocation_address == LLDB_INVALID_ADDRESS) |
| 257 | { |
| 258 | error.SetErrorToGenericError(); |
| 259 | error.SetErrorString("Couldn't malloc: address space is full"); |
| 260 | return LLDB_INVALID_ADDRESS; |
| 261 | } |
| 262 | break; |
| 263 | case eAllocationPolicyMirror: |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 264 | process_sp = m_process_wp.lock(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 265 | if (log) |
| 266 | log->Printf ("IRMemoryMap::%s process_sp=0x%" PRIx64 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s", __FUNCTION__, (lldb::addr_t) process_sp.get (), process_sp && process_sp->CanJIT () ? "true" : "false", process_sp && process_sp->IsAlive () ? "true" : "false"); |
Sean Callanan | ad7cc46 | 2013-06-17 21:19:31 +0000 | [diff] [blame] | 267 | if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 268 | { |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 269 | if (!zero_memory) |
| 270 | allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); |
| 271 | else |
| 272 | allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error); |
| 273 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 274 | if (!error.Success()) |
| 275 | return LLDB_INVALID_ADDRESS; |
| 276 | } |
| 277 | else |
| 278 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 279 | if (log) |
| 280 | log->Printf ("IRMemoryMap::%s switching to eAllocationPolicyHostOnly due to failed condition (see previous expr log message)", __FUNCTION__); |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 281 | policy = eAllocationPolicyHostOnly; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 282 | allocation_address = FindSpace(allocation_size); |
| 283 | if (allocation_address == LLDB_INVALID_ADDRESS) |
| 284 | { |
| 285 | error.SetErrorToGenericError(); |
| 286 | error.SetErrorString("Couldn't malloc: address space is full"); |
| 287 | return LLDB_INVALID_ADDRESS; |
| 288 | } |
| 289 | } |
| 290 | break; |
| 291 | case eAllocationPolicyProcessOnly: |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 292 | process_sp = m_process_wp.lock(); |
| 293 | if (process_sp) |
| 294 | { |
Sean Callanan | ad7cc46 | 2013-06-17 21:19:31 +0000 | [diff] [blame] | 295 | if (process_sp->CanJIT() && process_sp->IsAlive()) |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 296 | { |
Jim Ingham | 2c38141 | 2015-11-04 20:32:27 +0000 | [diff] [blame] | 297 | if (!zero_memory) |
| 298 | allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); |
| 299 | else |
| 300 | allocation_address = process_sp->CallocateMemory(allocation_size, permissions, error); |
| 301 | |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 302 | if (!error.Success()) |
| 303 | return LLDB_INVALID_ADDRESS; |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | error.SetErrorToGenericError(); |
| 308 | error.SetErrorString("Couldn't malloc: process doesn't support allocating memory"); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 309 | return LLDB_INVALID_ADDRESS; |
Sean Callanan | bb9945f | 2013-04-19 01:51:24 +0000 | [diff] [blame] | 310 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 311 | } |
| 312 | else |
| 313 | { |
| 314 | error.SetErrorToGenericError(); |
| 315 | error.SetErrorString("Couldn't malloc: process doesn't exist, and this memory must be in the process"); |
| 316 | return LLDB_INVALID_ADDRESS; |
| 317 | } |
| 318 | break; |
| 319 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 320 | |
| 321 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 322 | lldb::addr_t mask = alignment - 1; |
| 323 | aligned_address = (allocation_address + mask) & (~mask); |
| 324 | |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 325 | m_allocations[aligned_address] = Allocation(allocation_address, |
| 326 | aligned_address, |
Matt Kopec | 750dcc3 | 2013-04-26 17:48:01 +0000 | [diff] [blame] | 327 | allocation_size, |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 328 | permissions, |
| 329 | alignment, |
| 330 | policy); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 331 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 332 | if (log) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 333 | { |
| 334 | const char * policy_string; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 335 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 336 | switch (policy) |
| 337 | { |
| 338 | default: |
| 339 | policy_string = "<invalid policy>"; |
| 340 | break; |
| 341 | case eAllocationPolicyHostOnly: |
| 342 | policy_string = "eAllocationPolicyHostOnly"; |
| 343 | break; |
| 344 | case eAllocationPolicyProcessOnly: |
| 345 | policy_string = "eAllocationPolicyProcessOnly"; |
| 346 | break; |
| 347 | case eAllocationPolicyMirror: |
| 348 | policy_string = "eAllocationPolicyMirror"; |
| 349 | break; |
| 350 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 351 | |
Matt Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 352 | log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", %s) -> 0x%" PRIx64, |
Matt Kopec | 750dcc3 | 2013-04-26 17:48:01 +0000 | [diff] [blame] | 353 | (uint64_t)allocation_size, |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 354 | (uint64_t)alignment, |
| 355 | (uint64_t)permissions, |
| 356 | policy_string, |
| 357 | aligned_address); |
| 358 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 359 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 360 | return aligned_address; |
| 361 | } |
| 362 | |
| 363 | void |
Sean Callanan | fbf5c68 | 2013-05-22 22:49:06 +0000 | [diff] [blame] | 364 | IRMemoryMap::Leak (lldb::addr_t process_address, Error &error) |
| 365 | { |
| 366 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 367 | |
Sean Callanan | fbf5c68 | 2013-05-22 22:49:06 +0000 | [diff] [blame] | 368 | AllocationMap::iterator iter = m_allocations.find(process_address); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 369 | |
Sean Callanan | fbf5c68 | 2013-05-22 22:49:06 +0000 | [diff] [blame] | 370 | if (iter == m_allocations.end()) |
| 371 | { |
| 372 | error.SetErrorToGenericError(); |
| 373 | error.SetErrorString("Couldn't leak: allocation doesn't exist"); |
| 374 | return; |
| 375 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 376 | |
Sean Callanan | fbf5c68 | 2013-05-22 22:49:06 +0000 | [diff] [blame] | 377 | Allocation &allocation = iter->second; |
| 378 | |
| 379 | allocation.m_leak = true; |
| 380 | } |
| 381 | |
| 382 | void |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 383 | IRMemoryMap::Free (lldb::addr_t process_address, Error &error) |
| 384 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 385 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 386 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 387 | AllocationMap::iterator iter = m_allocations.find(process_address); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 388 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 389 | if (iter == m_allocations.end()) |
| 390 | { |
| 391 | error.SetErrorToGenericError(); |
| 392 | error.SetErrorString("Couldn't free: allocation doesn't exist"); |
| 393 | return; |
| 394 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 395 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 396 | Allocation &allocation = iter->second; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 397 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 398 | switch (allocation.m_policy) |
| 399 | { |
| 400 | default: |
| 401 | case eAllocationPolicyHostOnly: |
Sean Callanan | df56540 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 402 | { |
| 403 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 404 | if (process_sp) |
| 405 | { |
Sean Callanan | ad7cc46 | 2013-06-17 21:19:31 +0000 | [diff] [blame] | 406 | if (process_sp->CanJIT() && process_sp->IsAlive()) |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 407 | process_sp->DeallocateMemory(allocation.m_process_alloc); // FindSpace allocated this for real |
Sean Callanan | bb77704 | 2013-05-16 17:30:37 +0000 | [diff] [blame] | 408 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 409 | |
Sean Callanan | df56540 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 410 | break; |
| 411 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 412 | case eAllocationPolicyMirror: |
| 413 | case eAllocationPolicyProcessOnly: |
Sean Callanan | df56540 | 2013-04-27 02:19:33 +0000 | [diff] [blame] | 414 | { |
| 415 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 416 | if (process_sp) |
| 417 | process_sp->DeallocateMemory(allocation.m_process_alloc); |
| 418 | } |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 419 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 420 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 421 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 422 | { |
Matt Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 423 | log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64 "..0x%" PRIx64 ")", |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 424 | (uint64_t)process_address, |
| 425 | iter->second.m_process_start, |
| 426 | iter->second.m_process_start + iter->second.m_size); |
| 427 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 428 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 429 | m_allocations.erase(iter); |
| 430 | } |
| 431 | |
Ewan Crawford | 90ff791 | 2015-07-14 10:56:58 +0000 | [diff] [blame] | 432 | bool |
| 433 | IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) |
| 434 | { |
| 435 | AllocationMap::iterator iter = FindAllocation(address, size); |
| 436 | if (iter == m_allocations.end()) |
| 437 | return false; |
| 438 | |
| 439 | Allocation &al = iter->second; |
| 440 | |
| 441 | if (address > (al.m_process_start + al.m_size)) |
| 442 | { |
| 443 | size = 0; |
| 444 | return false; |
| 445 | } |
| 446 | |
| 447 | if (address > al.m_process_start) |
| 448 | { |
| 449 | int dif = address - al.m_process_start; |
| 450 | size = al.m_size - dif; |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | size = al.m_size; |
| 455 | return true; |
| 456 | } |
| 457 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 458 | void |
| 459 | IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error) |
| 460 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 461 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 462 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 463 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 464 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 465 | if (iter == m_allocations.end()) |
| 466 | { |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 467 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 468 | |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 469 | if (process_sp) |
| 470 | { |
| 471 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 472 | return; |
| 473 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 474 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 475 | error.SetErrorToGenericError(); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 476 | error.SetErrorString("Couldn't write: no allocation contains the target range and the process doesn't exist"); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 477 | return; |
| 478 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 479 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 480 | Allocation &allocation = iter->second; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 481 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 482 | uint64_t offset = process_address - allocation.m_process_start; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 483 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 484 | lldb::ProcessSP process_sp; |
| 485 | |
| 486 | switch (allocation.m_policy) |
| 487 | { |
| 488 | default: |
| 489 | error.SetErrorToGenericError(); |
| 490 | error.SetErrorString("Couldn't write: invalid allocation policy"); |
| 491 | return; |
| 492 | case eAllocationPolicyHostOnly: |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 493 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 494 | { |
| 495 | error.SetErrorToGenericError(); |
| 496 | error.SetErrorString("Couldn't write: data buffer is empty"); |
| 497 | return; |
| 498 | } |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 499 | ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 500 | break; |
| 501 | case eAllocationPolicyMirror: |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 502 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 503 | { |
| 504 | error.SetErrorToGenericError(); |
| 505 | error.SetErrorString("Couldn't write: data buffer is empty"); |
| 506 | return; |
| 507 | } |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 508 | ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 509 | process_sp = m_process_wp.lock(); |
| 510 | if (process_sp) |
| 511 | { |
| 512 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 513 | if (!error.Success()) |
| 514 | return; |
| 515 | } |
| 516 | break; |
| 517 | case eAllocationPolicyProcessOnly: |
| 518 | process_sp = m_process_wp.lock(); |
| 519 | if (process_sp) |
| 520 | { |
| 521 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 522 | if (!error.Success()) |
| 523 | return; |
| 524 | } |
| 525 | break; |
| 526 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 527 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 528 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 529 | { |
Matt Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 530 | log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")", |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 531 | (uint64_t)process_address, |
| 532 | (uint64_t)bytes, |
| 533 | (uint64_t)size, |
| 534 | (uint64_t)allocation.m_process_start, |
| 535 | (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | void |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 540 | IRMemoryMap::WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 541 | { |
| 542 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 543 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 544 | if (size == UINT32_MAX) |
| 545 | size = scalar.GetByteSize(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 546 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 547 | if (size > 0) |
| 548 | { |
| 549 | uint8_t buf[32]; |
| 550 | const size_t mem_size = scalar.GetAsMemoryData (buf, size, GetByteOrder(), error); |
| 551 | if (mem_size > 0) |
| 552 | { |
| 553 | return WriteMemory(process_address, buf, mem_size, error); |
| 554 | } |
| 555 | else |
| 556 | { |
| 557 | error.SetErrorToGenericError(); |
| 558 | error.SetErrorString ("Couldn't write scalar: failed to get scalar as memory data"); |
| 559 | } |
| 560 | } |
| 561 | else |
| 562 | { |
| 563 | error.SetErrorToGenericError(); |
| 564 | error.SetErrorString ("Couldn't write scalar: its size was zero"); |
| 565 | } |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | void |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 570 | IRMemoryMap::WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error) |
| 571 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 572 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 573 | |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 574 | Scalar scalar(address); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 575 | |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 576 | WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error); |
| 577 | } |
| 578 | |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 579 | void |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 580 | IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error) |
| 581 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 582 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 583 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 584 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 585 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 586 | if (iter == m_allocations.end()) |
| 587 | { |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 588 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 589 | |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 590 | if (process_sp) |
| 591 | { |
| 592 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 593 | return; |
| 594 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 595 | |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 596 | lldb::TargetSP target_sp = m_target_wp.lock(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 597 | |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 598 | if (target_sp) |
| 599 | { |
| 600 | Address absolute_address(process_address); |
| 601 | target_sp->ReadMemory(absolute_address, false, bytes, size, error); |
| 602 | return; |
| 603 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 604 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 605 | error.SetErrorToGenericError(); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 606 | error.SetErrorString("Couldn't read: no allocation contains the target range, and neither the process nor the target exist"); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 607 | return; |
| 608 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 609 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 610 | Allocation &allocation = iter->second; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 611 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 612 | uint64_t offset = process_address - allocation.m_process_start; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 613 | |
Sean Callanan | 9bbf3cd | 2014-03-04 21:56:11 +0000 | [diff] [blame] | 614 | if (offset > allocation.m_size) |
| 615 | { |
| 616 | error.SetErrorToGenericError(); |
| 617 | error.SetErrorString("Couldn't read: data is not in the allocation"); |
| 618 | return; |
| 619 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 620 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 621 | lldb::ProcessSP process_sp; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 622 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 623 | switch (allocation.m_policy) |
| 624 | { |
| 625 | default: |
| 626 | error.SetErrorToGenericError(); |
| 627 | error.SetErrorString("Couldn't read: invalid allocation policy"); |
| 628 | return; |
| 629 | case eAllocationPolicyHostOnly: |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 630 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 631 | { |
| 632 | error.SetErrorToGenericError(); |
| 633 | error.SetErrorString("Couldn't read: data buffer is empty"); |
| 634 | return; |
| 635 | } |
Sean Callanan | 9bbf3cd | 2014-03-04 21:56:11 +0000 | [diff] [blame] | 636 | if (allocation.m_data.GetByteSize() < offset + size) |
| 637 | { |
| 638 | error.SetErrorToGenericError(); |
| 639 | error.SetErrorString("Couldn't read: not enough underlying data"); |
| 640 | return; |
| 641 | } |
| 642 | |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 643 | ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 644 | break; |
| 645 | case eAllocationPolicyMirror: |
| 646 | process_sp = m_process_wp.lock(); |
| 647 | if (process_sp) |
| 648 | { |
| 649 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 650 | if (!error.Success()) |
| 651 | return; |
| 652 | } |
| 653 | else |
| 654 | { |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 655 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 656 | { |
| 657 | error.SetErrorToGenericError(); |
| 658 | error.SetErrorString("Couldn't read: data buffer is empty"); |
| 659 | return; |
| 660 | } |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 661 | ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 662 | } |
| 663 | break; |
| 664 | case eAllocationPolicyProcessOnly: |
| 665 | process_sp = m_process_wp.lock(); |
| 666 | if (process_sp) |
| 667 | { |
| 668 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 669 | if (!error.Success()) |
| 670 | return; |
| 671 | } |
| 672 | break; |
| 673 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 674 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 675 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 676 | { |
Matt Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 677 | log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")", |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 678 | (uint64_t)process_address, |
| 679 | (uint64_t)bytes, |
| 680 | (uint64_t)size, |
| 681 | (uint64_t)allocation.m_process_start, |
| 682 | (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); |
| 683 | } |
| 684 | } |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 685 | |
| 686 | void |
| 687 | IRMemoryMap::ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 688 | { |
| 689 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 690 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 691 | if (size > 0) |
| 692 | { |
| 693 | DataBufferHeap buf(size, 0); |
| 694 | ReadMemory(buf.GetBytes(), process_address, size, error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 695 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 696 | if (!error.Success()) |
| 697 | return; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 698 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 699 | DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize()); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 700 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 701 | lldb::offset_t offset = 0; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 702 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 703 | switch (size) |
| 704 | { |
| 705 | default: |
| 706 | error.SetErrorToGenericError(); |
Greg Clayton | e86cef6 | 2013-06-04 21:30:42 +0000 | [diff] [blame] | 707 | error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size); |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 708 | return; |
| 709 | case 1: scalar = extractor.GetU8(&offset); break; |
| 710 | case 2: scalar = extractor.GetU16(&offset); break; |
| 711 | case 4: scalar = extractor.GetU32(&offset); break; |
| 712 | case 8: scalar = extractor.GetU64(&offset); break; |
| 713 | } |
| 714 | } |
| 715 | else |
| 716 | { |
| 717 | error.SetErrorToGenericError(); |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 718 | error.SetErrorString ("Couldn't read scalar: its size was zero"); |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 719 | } |
| 720 | return; |
| 721 | } |
| 722 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 723 | void |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 724 | IRMemoryMap::ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error) |
| 725 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 726 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 727 | |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 728 | Scalar pointer_scalar; |
| 729 | ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(), error); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 730 | |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 731 | if (!error.Success()) |
| 732 | return; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 733 | |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 734 | *address = pointer_scalar.ULongLong(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 735 | |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 736 | return; |
| 737 | } |
| 738 | |
| 739 | void |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 740 | IRMemoryMap::GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error) |
| 741 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 742 | error.Clear(); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 743 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 744 | if (size > 0) |
| 745 | { |
| 746 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 747 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 748 | if (iter == m_allocations.end()) |
| 749 | { |
| 750 | error.SetErrorToGenericError(); |
Matt Kopec | ef14371 | 2013-06-03 18:00:07 +0000 | [diff] [blame] | 751 | error.SetErrorStringWithFormat("Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64 ")", process_address, process_address + size); |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 752 | return; |
| 753 | } |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 754 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 755 | Allocation &allocation = iter->second; |
Sylvestre Ledru | ceab3ac | 2014-07-06 17:54:58 +0000 | [diff] [blame] | 756 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 757 | switch (allocation.m_policy) |
| 758 | { |
| 759 | default: |
| 760 | error.SetErrorToGenericError(); |
| 761 | error.SetErrorString("Couldn't get memory data: invalid allocation policy"); |
| 762 | return; |
| 763 | case eAllocationPolicyProcessOnly: |
| 764 | error.SetErrorToGenericError(); |
| 765 | error.SetErrorString("Couldn't get memory data: memory is only in the target"); |
| 766 | return; |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 767 | case eAllocationPolicyMirror: |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 768 | { |
| 769 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 770 | |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 771 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 772 | { |
| 773 | error.SetErrorToGenericError(); |
| 774 | error.SetErrorString("Couldn't get memory data: data buffer is empty"); |
| 775 | return; |
| 776 | } |
| 777 | if (process_sp) |
| 778 | { |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 779 | process_sp->ReadMemory(allocation.m_process_start, allocation.m_data.GetBytes(), allocation.m_data.GetByteSize(), error); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 780 | if (!error.Success()) |
| 781 | return; |
| 782 | uint64_t offset = process_address - allocation.m_process_start; |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 783 | extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 784 | return; |
| 785 | } |
| 786 | } |
Greg Clayton | cec91ef | 2016-02-26 01:20:20 +0000 | [diff] [blame] | 787 | break; |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 788 | case eAllocationPolicyHostOnly: |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 789 | if (!allocation.m_data.GetByteSize()) |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 790 | { |
| 791 | error.SetErrorToGenericError(); |
| 792 | error.SetErrorString("Couldn't get memory data: data buffer is empty"); |
| 793 | return; |
| 794 | } |
| 795 | uint64_t offset = process_address - allocation.m_process_start; |
Sean Callanan | d256250 | 2013-04-19 17:44:40 +0000 | [diff] [blame] | 796 | extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 797 | return; |
| 798 | } |
| 799 | } |
| 800 | else |
| 801 | { |
| 802 | error.SetErrorToGenericError(); |
| 803 | error.SetErrorString ("Couldn't get memory data: its size was zero"); |
| 804 | return; |
| 805 | } |
| 806 | } |