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