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