Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 1 | //===-- IRMemoryMap.cpp -----------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 10 | #include "lldb/Core/DataBufferHeap.h" |
| 11 | #include "lldb/Core/DataExtractor.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 12 | #include "lldb/Core/Error.h" |
| 13 | #include "lldb/Core/Log.h" |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 14 | #include "lldb/Core/Scalar.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 15 | #include "lldb/Expression/IRMemoryMap.h" |
| 16 | #include "lldb/Target/Process.h" |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Target.h" |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace lldb_private; |
| 20 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 21 | IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) : |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 22 | m_target_wp(target_sp) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 23 | { |
Sean Callanan | b024d87 | 2013-04-15 17:12:47 +0000 | [diff] [blame] | 24 | if (target_sp) |
| 25 | m_process_wp = target_sp->GetProcessSP(); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | IRMemoryMap::~IRMemoryMap () |
| 29 | { |
| 30 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 31 | |
| 32 | if (process_sp) |
| 33 | { |
| 34 | for (AllocationMap::value_type &allocation : m_allocations) |
| 35 | { |
| 36 | if (allocation.second.m_policy == eAllocationPolicyMirror || |
| 37 | allocation.second.m_policy == eAllocationPolicyHostOnly) |
| 38 | process_sp->DeallocateMemory(allocation.second.m_process_alloc); |
| 39 | |
| 40 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 41 | { |
| 42 | log->Printf("IRMemoryMap::~IRMemoryMap deallocated [0x%llx..0x%llx)", |
| 43 | (uint64_t)allocation.second.m_process_start, |
| 44 | (uint64_t)allocation.second.m_process_start + (uint64_t)allocation.second.m_size); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | lldb::addr_t |
| 51 | IRMemoryMap::FindSpace (size_t size) |
| 52 | { |
| 53 | // Yup, this is just plain O(n) insertion. We'll use a range tree if we |
| 54 | // start caring. |
| 55 | |
| 56 | lldb::addr_t remote_address = 0x1000; // skip first page of memory |
| 57 | |
| 58 | for (AllocationMap::value_type &allocation : m_allocations) |
| 59 | { |
| 60 | if (remote_address < allocation.second.m_process_start && |
| 61 | remote_address + size <= allocation.second.m_process_start) |
| 62 | return remote_address; |
| 63 | |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 64 | remote_address = allocation.second.m_process_start + allocation.second.m_size; |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (remote_address + size < remote_address) |
| 68 | return LLDB_INVALID_ADDRESS; // massively unlikely |
| 69 | |
| 70 | return remote_address; |
| 71 | } |
| 72 | |
| 73 | bool |
| 74 | IRMemoryMap::ContainsHostOnlyAllocations () |
| 75 | { |
| 76 | for (AllocationMap::value_type &allocation : m_allocations) |
| 77 | { |
| 78 | if (allocation.second.m_policy == eAllocationPolicyHostOnly) |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | IRMemoryMap::AllocationMap::iterator |
| 86 | IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size) |
| 87 | { |
| 88 | AllocationMap::iterator iter = m_allocations.lower_bound (addr); |
| 89 | |
Sean Callanan | 14b1bae | 2013-04-16 23:25:35 +0000 | [diff] [blame] | 90 | if (iter == m_allocations.end() || |
| 91 | iter->first > addr) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 92 | { |
| 93 | if (iter == m_allocations.begin()) |
| 94 | return m_allocations.end(); |
| 95 | iter--; |
| 96 | } |
| 97 | |
| 98 | if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size) |
| 99 | return iter; |
| 100 | |
| 101 | return m_allocations.end(); |
| 102 | } |
| 103 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 104 | lldb::ByteOrder |
| 105 | IRMemoryMap::GetByteOrder() |
| 106 | { |
| 107 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 108 | |
| 109 | if (process_sp) |
| 110 | return process_sp->GetByteOrder(); |
| 111 | |
| 112 | lldb::TargetSP target_sp = m_target_wp.lock(); |
| 113 | |
| 114 | if (target_sp) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 115 | return target_sp->GetArchitecture().GetByteOrder(); |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 116 | |
| 117 | return lldb::eByteOrderInvalid; |
| 118 | } |
| 119 | |
| 120 | uint32_t |
| 121 | IRMemoryMap::GetAddressByteSize() |
| 122 | { |
| 123 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 124 | |
| 125 | if (process_sp) |
| 126 | return process_sp->GetAddressByteSize(); |
| 127 | |
| 128 | lldb::TargetSP target_sp = m_target_wp.lock(); |
| 129 | |
| 130 | if (target_sp) |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 131 | return target_sp->GetArchitecture().GetAddressByteSize(); |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 132 | |
| 133 | return UINT32_MAX; |
| 134 | } |
| 135 | |
| 136 | ExecutionContextScope * |
| 137 | IRMemoryMap::GetBestExecutionContextScope() |
| 138 | { |
| 139 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 140 | |
| 141 | if (process_sp) |
| 142 | return process_sp.get(); |
| 143 | |
| 144 | lldb::TargetSP target_sp = m_target_wp.lock(); |
| 145 | |
| 146 | if (target_sp) |
| 147 | return target_sp.get(); |
| 148 | |
| 149 | return NULL; |
| 150 | } |
| 151 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 152 | lldb::addr_t |
| 153 | IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, Error &error) |
| 154 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 155 | error.Clear(); |
| 156 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 157 | lldb::ProcessSP process_sp; |
| 158 | lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS; |
| 159 | lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS; |
| 160 | |
| 161 | size_t allocation_size = (size ? size : 1) + alignment - 1; |
| 162 | |
| 163 | switch (policy) |
| 164 | { |
| 165 | default: |
| 166 | error.SetErrorToGenericError(); |
| 167 | error.SetErrorString("Couldn't malloc: invalid allocation policy"); |
| 168 | return LLDB_INVALID_ADDRESS; |
| 169 | case eAllocationPolicyHostOnly: |
| 170 | allocation_address = FindSpace(allocation_size); |
| 171 | if (allocation_address == LLDB_INVALID_ADDRESS) |
| 172 | { |
| 173 | error.SetErrorToGenericError(); |
| 174 | error.SetErrorString("Couldn't malloc: address space is full"); |
| 175 | return LLDB_INVALID_ADDRESS; |
| 176 | } |
| 177 | break; |
| 178 | case eAllocationPolicyMirror: |
| 179 | if (ContainsHostOnlyAllocations()) |
| 180 | { |
| 181 | error.SetErrorToGenericError(); |
| 182 | error.SetErrorString("Couldn't malloc: host-only allocations are polluting the address space"); |
| 183 | return LLDB_INVALID_ADDRESS; |
| 184 | } |
| 185 | process_sp = m_process_wp.lock(); |
| 186 | if (process_sp) |
| 187 | { |
| 188 | allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); |
| 189 | if (!error.Success()) |
| 190 | return LLDB_INVALID_ADDRESS; |
| 191 | } |
| 192 | else |
| 193 | { |
| 194 | allocation_address = FindSpace(allocation_size); |
| 195 | if (allocation_address == LLDB_INVALID_ADDRESS) |
| 196 | { |
| 197 | error.SetErrorToGenericError(); |
| 198 | error.SetErrorString("Couldn't malloc: address space is full"); |
| 199 | return LLDB_INVALID_ADDRESS; |
| 200 | } |
| 201 | } |
| 202 | break; |
| 203 | case eAllocationPolicyProcessOnly: |
| 204 | if (ContainsHostOnlyAllocations()) |
| 205 | { |
| 206 | error.SetErrorToGenericError(); |
| 207 | error.SetErrorString("Couldn't malloc: host-only allocations are polluting the address space"); |
| 208 | return LLDB_INVALID_ADDRESS; |
| 209 | } |
| 210 | process_sp = m_process_wp.lock(); |
| 211 | if (process_sp) |
| 212 | { |
| 213 | allocation_address = process_sp->AllocateMemory(allocation_size, permissions, error); |
| 214 | if (!error.Success()) |
| 215 | return LLDB_INVALID_ADDRESS; |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | error.SetErrorToGenericError(); |
| 220 | error.SetErrorString("Couldn't malloc: process doesn't exist, and this memory must be in the process"); |
| 221 | return LLDB_INVALID_ADDRESS; |
| 222 | } |
| 223 | break; |
| 224 | } |
| 225 | |
| 226 | |
| 227 | lldb::addr_t mask = alignment - 1; |
| 228 | aligned_address = (allocation_address + mask) & (~mask); |
| 229 | |
| 230 | Allocation &allocation(m_allocations[aligned_address]); |
| 231 | |
| 232 | allocation.m_process_alloc = allocation_address; |
| 233 | allocation.m_process_start = aligned_address; |
| 234 | allocation.m_size = size; |
| 235 | allocation.m_permissions = permissions; |
| 236 | allocation.m_alignment = alignment; |
| 237 | allocation.m_policy = policy; |
| 238 | |
| 239 | switch (policy) |
| 240 | { |
| 241 | default: |
| 242 | assert (0 && "We cannot reach this!"); |
| 243 | case eAllocationPolicyHostOnly: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 244 | allocation.m_data_ap.reset(new DataBufferHeap(size, 0)); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 245 | break; |
| 246 | case eAllocationPolicyProcessOnly: |
| 247 | break; |
| 248 | case eAllocationPolicyMirror: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 249 | allocation.m_data_ap.reset(new DataBufferHeap(size, 0)); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 250 | break; |
| 251 | } |
| 252 | |
| 253 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 254 | { |
| 255 | const char * policy_string; |
| 256 | |
| 257 | switch (policy) |
| 258 | { |
| 259 | default: |
| 260 | policy_string = "<invalid policy>"; |
| 261 | break; |
| 262 | case eAllocationPolicyHostOnly: |
| 263 | policy_string = "eAllocationPolicyHostOnly"; |
| 264 | break; |
| 265 | case eAllocationPolicyProcessOnly: |
| 266 | policy_string = "eAllocationPolicyProcessOnly"; |
| 267 | break; |
| 268 | case eAllocationPolicyMirror: |
| 269 | policy_string = "eAllocationPolicyMirror"; |
| 270 | break; |
| 271 | } |
| 272 | |
| 273 | log->Printf("IRMemoryMap::Malloc (%llu, 0x%llx, 0x%llx, %s) -> 0x%llx", |
| 274 | (uint64_t)size, |
| 275 | (uint64_t)alignment, |
| 276 | (uint64_t)permissions, |
| 277 | policy_string, |
| 278 | aligned_address); |
| 279 | } |
| 280 | |
| 281 | return aligned_address; |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | IRMemoryMap::Free (lldb::addr_t process_address, Error &error) |
| 286 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 287 | error.Clear(); |
| 288 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 289 | AllocationMap::iterator iter = m_allocations.find(process_address); |
| 290 | |
| 291 | if (iter == m_allocations.end()) |
| 292 | { |
| 293 | error.SetErrorToGenericError(); |
| 294 | error.SetErrorString("Couldn't free: allocation doesn't exist"); |
| 295 | return; |
| 296 | } |
| 297 | |
| 298 | Allocation &allocation = iter->second; |
| 299 | |
| 300 | switch (allocation.m_policy) |
| 301 | { |
| 302 | default: |
| 303 | case eAllocationPolicyHostOnly: |
| 304 | break; |
| 305 | case eAllocationPolicyMirror: |
| 306 | case eAllocationPolicyProcessOnly: |
| 307 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 308 | if (process_sp) |
| 309 | process_sp->DeallocateMemory(allocation.m_process_alloc); |
| 310 | } |
| 311 | |
| 312 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 313 | { |
| 314 | log->Printf("IRMemoryMap::Free (0x%llx) freed [0x%llx..0x%llx)", |
| 315 | (uint64_t)process_address, |
| 316 | iter->second.m_process_start, |
| 317 | iter->second.m_process_start + iter->second.m_size); |
| 318 | } |
| 319 | |
| 320 | m_allocations.erase(iter); |
| 321 | } |
| 322 | |
| 323 | void |
| 324 | IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error) |
| 325 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 326 | error.Clear(); |
| 327 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 328 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
| 329 | |
| 330 | if (iter == m_allocations.end()) |
| 331 | { |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 332 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 333 | |
| 334 | if (process_sp) |
| 335 | { |
| 336 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 337 | return; |
| 338 | } |
| 339 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 340 | error.SetErrorToGenericError(); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 341 | 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] | 342 | return; |
| 343 | } |
| 344 | |
| 345 | Allocation &allocation = iter->second; |
| 346 | |
| 347 | uint64_t offset = process_address - allocation.m_process_start; |
| 348 | |
| 349 | lldb::ProcessSP process_sp; |
| 350 | |
| 351 | switch (allocation.m_policy) |
| 352 | { |
| 353 | default: |
| 354 | error.SetErrorToGenericError(); |
| 355 | error.SetErrorString("Couldn't write: invalid allocation policy"); |
| 356 | return; |
| 357 | case eAllocationPolicyHostOnly: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 358 | if (!allocation.m_data_ap.get()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 359 | { |
| 360 | error.SetErrorToGenericError(); |
| 361 | error.SetErrorString("Couldn't write: data buffer is empty"); |
| 362 | return; |
| 363 | } |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 364 | ::memcpy (allocation.m_data_ap->GetBytes() + offset, bytes, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 365 | break; |
| 366 | case eAllocationPolicyMirror: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 367 | if (!allocation.m_data_ap.get()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 368 | { |
| 369 | error.SetErrorToGenericError(); |
| 370 | error.SetErrorString("Couldn't write: data buffer is empty"); |
| 371 | return; |
| 372 | } |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 373 | ::memcpy (allocation.m_data_ap->GetBytes() + offset, bytes, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 374 | process_sp = m_process_wp.lock(); |
| 375 | if (process_sp) |
| 376 | { |
| 377 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 378 | if (!error.Success()) |
| 379 | return; |
| 380 | } |
| 381 | break; |
| 382 | case eAllocationPolicyProcessOnly: |
| 383 | process_sp = m_process_wp.lock(); |
| 384 | if (process_sp) |
| 385 | { |
| 386 | process_sp->WriteMemory(process_address, bytes, size, error); |
| 387 | if (!error.Success()) |
| 388 | return; |
| 389 | } |
| 390 | break; |
| 391 | } |
| 392 | |
| 393 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 394 | { |
| 395 | log->Printf("IRMemoryMap::WriteMemory (0x%llx, 0x%llx, 0x%lld) went to [0x%llx..0x%llx)", |
| 396 | (uint64_t)process_address, |
| 397 | (uint64_t)bytes, |
| 398 | (uint64_t)size, |
| 399 | (uint64_t)allocation.m_process_start, |
| 400 | (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 405 | 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] | 406 | { |
| 407 | error.Clear(); |
| 408 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 409 | if (size == UINT32_MAX) |
| 410 | size = scalar.GetByteSize(); |
| 411 | |
| 412 | if (size > 0) |
| 413 | { |
| 414 | uint8_t buf[32]; |
| 415 | const size_t mem_size = scalar.GetAsMemoryData (buf, size, GetByteOrder(), error); |
| 416 | if (mem_size > 0) |
| 417 | { |
| 418 | return WriteMemory(process_address, buf, mem_size, error); |
| 419 | } |
| 420 | else |
| 421 | { |
| 422 | error.SetErrorToGenericError(); |
| 423 | error.SetErrorString ("Couldn't write scalar: failed to get scalar as memory data"); |
| 424 | } |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | error.SetErrorToGenericError(); |
| 429 | error.SetErrorString ("Couldn't write scalar: its size was zero"); |
| 430 | } |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | void |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 435 | IRMemoryMap::WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error) |
| 436 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 437 | error.Clear(); |
| 438 | |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 439 | Scalar scalar(address); |
| 440 | |
| 441 | WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error); |
| 442 | } |
| 443 | |
Sean Callanan | f8043fa | 2013-04-12 21:40:34 +0000 | [diff] [blame] | 444 | void |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 445 | IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error) |
| 446 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 447 | error.Clear(); |
| 448 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 449 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
| 450 | |
| 451 | if (iter == m_allocations.end()) |
| 452 | { |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 453 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 454 | |
| 455 | if (process_sp) |
| 456 | { |
| 457 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | lldb::TargetSP target_sp = m_target_wp.lock(); |
| 462 | |
| 463 | if (target_sp) |
| 464 | { |
| 465 | Address absolute_address(process_address); |
| 466 | target_sp->ReadMemory(absolute_address, false, bytes, size, error); |
| 467 | return; |
| 468 | } |
| 469 | |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 470 | error.SetErrorToGenericError(); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 471 | 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] | 472 | return; |
| 473 | } |
| 474 | |
| 475 | Allocation &allocation = iter->second; |
| 476 | |
| 477 | uint64_t offset = process_address - allocation.m_process_start; |
| 478 | |
| 479 | lldb::ProcessSP process_sp; |
| 480 | |
| 481 | switch (allocation.m_policy) |
| 482 | { |
| 483 | default: |
| 484 | error.SetErrorToGenericError(); |
| 485 | error.SetErrorString("Couldn't read: invalid allocation policy"); |
| 486 | return; |
| 487 | case eAllocationPolicyHostOnly: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 488 | if (!allocation.m_data_ap.get()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 489 | { |
| 490 | error.SetErrorToGenericError(); |
| 491 | error.SetErrorString("Couldn't read: data buffer is empty"); |
| 492 | return; |
| 493 | } |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 494 | ::memcpy (bytes, allocation.m_data_ap->GetBytes() + offset, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 495 | break; |
| 496 | case eAllocationPolicyMirror: |
| 497 | process_sp = m_process_wp.lock(); |
| 498 | if (process_sp) |
| 499 | { |
| 500 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 501 | if (!error.Success()) |
| 502 | return; |
| 503 | } |
| 504 | else |
| 505 | { |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 506 | if (!allocation.m_data_ap.get()) |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 507 | { |
| 508 | error.SetErrorToGenericError(); |
| 509 | error.SetErrorString("Couldn't read: data buffer is empty"); |
| 510 | return; |
| 511 | } |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 512 | ::memcpy (bytes, allocation.m_data_ap->GetBytes() + offset, size); |
Sean Callanan | 5a1af4e | 2013-04-05 02:22:57 +0000 | [diff] [blame] | 513 | } |
| 514 | break; |
| 515 | case eAllocationPolicyProcessOnly: |
| 516 | process_sp = m_process_wp.lock(); |
| 517 | if (process_sp) |
| 518 | { |
| 519 | process_sp->ReadMemory(process_address, bytes, size, error); |
| 520 | if (!error.Success()) |
| 521 | return; |
| 522 | } |
| 523 | break; |
| 524 | } |
| 525 | |
| 526 | if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS)) |
| 527 | { |
| 528 | log->Printf("IRMemoryMap::ReadMemory (0x%llx, 0x%llx, 0x%lld) came from [0x%llx..0x%llx)", |
| 529 | (uint64_t)process_address, |
| 530 | (uint64_t)bytes, |
| 531 | (uint64_t)size, |
| 532 | (uint64_t)allocation.m_process_start, |
| 533 | (uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size); |
| 534 | } |
| 535 | } |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 536 | |
| 537 | void |
| 538 | 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] | 539 | { |
| 540 | error.Clear(); |
| 541 | |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 542 | if (size > 0) |
| 543 | { |
| 544 | DataBufferHeap buf(size, 0); |
| 545 | ReadMemory(buf.GetBytes(), process_address, size, error); |
| 546 | |
| 547 | if (!error.Success()) |
| 548 | return; |
| 549 | |
| 550 | DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize()); |
| 551 | |
| 552 | lldb::offset_t offset = 0; |
| 553 | |
| 554 | switch (size) |
| 555 | { |
| 556 | default: |
| 557 | error.SetErrorToGenericError(); |
| 558 | error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %lld", (unsigned long long)size); |
| 559 | return; |
| 560 | case 1: scalar = extractor.GetU8(&offset); break; |
| 561 | case 2: scalar = extractor.GetU16(&offset); break; |
| 562 | case 4: scalar = extractor.GetU32(&offset); break; |
| 563 | case 8: scalar = extractor.GetU64(&offset); break; |
| 564 | } |
| 565 | } |
| 566 | else |
| 567 | { |
| 568 | error.SetErrorToGenericError(); |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 569 | error.SetErrorString ("Couldn't read scalar: its size was zero"); |
Sean Callanan | 35005f7 | 2013-04-12 18:10:34 +0000 | [diff] [blame] | 570 | } |
| 571 | return; |
| 572 | } |
| 573 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 574 | void |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 575 | IRMemoryMap::ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error) |
| 576 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 577 | error.Clear(); |
| 578 | |
Sean Callanan | 2d37e5a | 2013-04-15 22:48:23 +0000 | [diff] [blame] | 579 | Scalar pointer_scalar; |
| 580 | ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(), error); |
| 581 | |
| 582 | if (!error.Success()) |
| 583 | return; |
| 584 | |
| 585 | *address = pointer_scalar.ULongLong(); |
| 586 | |
| 587 | return; |
| 588 | } |
| 589 | |
| 590 | void |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 591 | IRMemoryMap::GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error) |
| 592 | { |
Sean Callanan | 08052af | 2013-04-17 07:50:58 +0000 | [diff] [blame] | 593 | error.Clear(); |
| 594 | |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 595 | if (size > 0) |
| 596 | { |
| 597 | AllocationMap::iterator iter = FindAllocation(process_address, size); |
| 598 | |
| 599 | if (iter == m_allocations.end()) |
| 600 | { |
| 601 | error.SetErrorToGenericError(); |
| 602 | error.SetErrorStringWithFormat("Couldn't find an allocation containing [0x%llx..0x%llx)", (unsigned long long)process_address, (unsigned long long)(process_address + size)); |
| 603 | return; |
| 604 | } |
| 605 | |
| 606 | Allocation &allocation = iter->second; |
| 607 | |
| 608 | switch (allocation.m_policy) |
| 609 | { |
| 610 | default: |
| 611 | error.SetErrorToGenericError(); |
| 612 | error.SetErrorString("Couldn't get memory data: invalid allocation policy"); |
| 613 | return; |
| 614 | case eAllocationPolicyProcessOnly: |
| 615 | error.SetErrorToGenericError(); |
| 616 | error.SetErrorString("Couldn't get memory data: memory is only in the target"); |
| 617 | return; |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 618 | case eAllocationPolicyMirror: |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 619 | { |
| 620 | lldb::ProcessSP process_sp = m_process_wp.lock(); |
| 621 | |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 622 | if (!allocation.m_data_ap.get()) |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 623 | { |
| 624 | error.SetErrorToGenericError(); |
| 625 | error.SetErrorString("Couldn't get memory data: data buffer is empty"); |
| 626 | return; |
| 627 | } |
| 628 | if (process_sp) |
| 629 | { |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 630 | process_sp->ReadMemory(allocation.m_process_start, allocation.m_data_ap->GetBytes(), allocation.m_data_ap->GetByteSize(), error); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 631 | if (!error.Success()) |
| 632 | return; |
| 633 | uint64_t offset = process_address - allocation.m_process_start; |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 634 | extractor = DataExtractor(allocation.m_data_ap->GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); |
Sean Callanan | c8c5b8d | 2013-04-15 21:35:52 +0000 | [diff] [blame] | 635 | return; |
| 636 | } |
| 637 | } |
| 638 | case eAllocationPolicyHostOnly: |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 639 | if (!allocation.m_data_ap.get()) |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 640 | { |
| 641 | error.SetErrorToGenericError(); |
| 642 | error.SetErrorString("Couldn't get memory data: data buffer is empty"); |
| 643 | return; |
| 644 | } |
| 645 | uint64_t offset = process_address - allocation.m_process_start; |
Greg Clayton | e0c64e1 | 2013-04-18 22:01:06 +0000 | [diff] [blame] | 646 | extractor = DataExtractor(allocation.m_data_ap->GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize()); |
Sean Callanan | 458ae1c | 2013-04-13 02:06:42 +0000 | [diff] [blame] | 647 | return; |
| 648 | } |
| 649 | } |
| 650 | else |
| 651 | { |
| 652 | error.SetErrorToGenericError(); |
| 653 | error.SetErrorString ("Couldn't get memory data: its size was zero"); |
| 654 | return; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | |