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