blob: 7b9d26667389d2a5901a4d40ce87f19eaf09a9f1 [file] [log] [blame]
Sean Callanan5a1af4e2013-04-05 02:22:57 +00001//===-- 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 Stoneb9c1b512016-09-06 20:57:50 +000010#include "lldb/Expression/IRMemoryMap.h"
Sean Callanan35005f72013-04-12 18:10:34 +000011#include "lldb/Core/DataBufferHeap.h"
12#include "lldb/Core/DataExtractor.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000013#include "lldb/Core/Error.h"
14#include "lldb/Core/Log.h"
Sean Callanan35005f72013-04-12 18:10:34 +000015#include "lldb/Core/Scalar.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000016#include "lldb/Target/MemoryRegionInfo.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000017#include "lldb/Target/Process.h"
Sean Callanan35005f72013-04-12 18:10:34 +000018#include "lldb/Target/Target.h"
Sean Callananf3df7e82016-06-09 20:22:25 +000019#include "lldb/Utility/LLDBAssert.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000020
21using namespace lldb_private;
22
Kate Stoneb9c1b512016-09-06 20:57:50 +000023IRMemoryMap::IRMemoryMap(lldb::TargetSP target_sp) : m_target_wp(target_sp) {
24 if (target_sp)
25 m_process_wp = target_sp->GetProcessSP();
Sean Callanan5a1af4e2013-04-05 02:22:57 +000026}
27
Kate Stoneb9c1b512016-09-06 20:57:50 +000028IRMemoryMap::~IRMemoryMap() {
29 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000030
Kate Stoneb9c1b512016-09-06 20:57:50 +000031 if (process_sp) {
32 AllocationMap::iterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000033
Kate Stoneb9c1b512016-09-06 20:57:50 +000034 Error err;
Sean Callananbb777042013-05-16 17:30:37 +000035
Kate Stoneb9c1b512016-09-06 20:57:50 +000036 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 Callanan5a1af4e2013-04-05 02:22:57 +000042 }
Kate Stoneb9c1b512016-09-06 20:57:50 +000043 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +000044}
45
Kate Stoneb9c1b512016-09-06 20:57:50 +000046lldb::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
52 // expressions processing real values from the process do not use the
53 // wrong data.
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 Callananf3df7e82016-06-09 20:22:25 +000058
Kate Stoneb9c1b512016-09-06 20:57:50 +000059 lldb::TargetSP target_sp = m_target_wp.lock();
60 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000061
Kate Stoneb9c1b512016-09-06 20:57:50 +000062 const bool process_is_alive = process_sp && process_sp->IsAlive();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000063
Kate Stoneb9c1b512016-09-06 20:57:50 +000064 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
65 if (size == 0)
Sean Callananbb9945f2013-04-19 01:51:24 +000066 return ret;
Kate Stoneb9c1b512016-09-06 20:57:50 +000067
68 if (process_is_alive && process_sp->CanJIT()) {
69 Error alloc_error;
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
96 // regions, walk forward through memory until a region is found that
97 // has adequate space for our allocation.
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;
107 Error err = process_sp->GetMemoryRegionInfo(ret, region_info);
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 Labathf31c9d22017-01-05 13:18:42 +0000129 lldbassert(0 && "GetMemoryRegionInfo() succeeded, then failed");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130 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 Callanan5a1af4e2013-04-05 02:22:57 +0000163}
164
165IRMemoryMap::AllocationMap::iterator
Kate Stoneb9c1b512016-09-06 20:57:50 +0000166IRMemoryMap::FindAllocation(lldb::addr_t addr, size_t size) {
167 if (addr == LLDB_INVALID_ADDRESS)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000168 return m_allocations.end();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000169
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 Callanan5a1af4e2013-04-05 02:22:57 +0000182}
183
Kate Stoneb9c1b512016-09-06 20:57:50 +0000184bool IRMemoryMap::IntersectsAllocation(lldb::addr_t addr, size_t size) const {
185 if (addr == LLDB_INVALID_ADDRESS)
Sean Callananbb9945f2013-04-19 01:51:24 +0000186 return false;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000187
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
191 // than or
192 // equal to where the given interval begins, it's possible that the given
193 // interval
194 // intersects either the returned interval or the previous interval. Thus, we
195 // need to
196 // check both. Note that we only need to check these two intervals. Since all
197 // intervals
198 // are disjoint it is not possible that an adjacent interval does not
199 // intersect, but a
200 // non-adjacent interval does intersect.
201 if (iter != m_allocations.end()) {
202 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
203 iter->second.m_size))
204 return true;
205 }
206
207 if (iter != m_allocations.begin()) {
208 --iter;
209 if (AllocationsIntersect(addr, size, iter->second.m_process_start,
210 iter->second.m_size))
211 return true;
212 }
213
214 return false;
Sean Callananbb9945f2013-04-19 01:51:24 +0000215}
216
Kate Stoneb9c1b512016-09-06 20:57:50 +0000217bool IRMemoryMap::AllocationsIntersect(lldb::addr_t addr1, size_t size1,
218 lldb::addr_t addr2, size_t size2) {
219 // Given two half open intervals [A, B) and [X, Y), the only 6 permutations
220 // that satisfy
Zachary Turner15362442014-06-25 18:37:19 +0000221 // A<B and X<Y are the following:
222 // A B X Y
223 // A X B Y (intersects)
224 // A X Y B (intersects)
225 // X A B Y (intersects)
226 // X A Y B (intersects)
227 // X Y A B
228 // The first is B <= X, and the last is Y <= A.
229 // So the condition is !(B <= X || Y <= A)), or (X < B && A < Y)
230 return (addr2 < (addr1 + size1)) && (addr1 < (addr2 + size2));
231}
232
Kate Stoneb9c1b512016-09-06 20:57:50 +0000233lldb::ByteOrder IRMemoryMap::GetByteOrder() {
234 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000235
Kate Stoneb9c1b512016-09-06 20:57:50 +0000236 if (process_sp)
237 return process_sp->GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000238
Kate Stoneb9c1b512016-09-06 20:57:50 +0000239 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000240
Kate Stoneb9c1b512016-09-06 20:57:50 +0000241 if (target_sp)
242 return target_sp->GetArchitecture().GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000243
Kate Stoneb9c1b512016-09-06 20:57:50 +0000244 return lldb::eByteOrderInvalid;
Sean Callanan35005f72013-04-12 18:10:34 +0000245}
246
Kate Stoneb9c1b512016-09-06 20:57:50 +0000247uint32_t IRMemoryMap::GetAddressByteSize() {
248 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000249
Kate Stoneb9c1b512016-09-06 20:57:50 +0000250 if (process_sp)
251 return process_sp->GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000252
Kate Stoneb9c1b512016-09-06 20:57:50 +0000253 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000254
Kate Stoneb9c1b512016-09-06 20:57:50 +0000255 if (target_sp)
256 return target_sp->GetArchitecture().GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000257
Kate Stoneb9c1b512016-09-06 20:57:50 +0000258 return UINT32_MAX;
Sean Callanan35005f72013-04-12 18:10:34 +0000259}
260
Kate Stoneb9c1b512016-09-06 20:57:50 +0000261ExecutionContextScope *IRMemoryMap::GetBestExecutionContextScope() const {
262 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000263
Kate Stoneb9c1b512016-09-06 20:57:50 +0000264 if (process_sp)
265 return process_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000266
Kate Stoneb9c1b512016-09-06 20:57:50 +0000267 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000268
Kate Stoneb9c1b512016-09-06 20:57:50 +0000269 if (target_sp)
270 return target_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000271
Kate Stoneb9c1b512016-09-06 20:57:50 +0000272 return NULL;
Sean Callanan35005f72013-04-12 18:10:34 +0000273}
274
Kate Stoneb9c1b512016-09-06 20:57:50 +0000275IRMemoryMap::Allocation::Allocation(lldb::addr_t process_alloc,
276 lldb::addr_t process_start, size_t size,
277 uint32_t permissions, uint8_t alignment,
278 AllocationPolicy policy)
279 : m_process_alloc(process_alloc), m_process_start(process_start),
280 m_size(size), m_permissions(permissions), m_alignment(alignment),
281 m_policy(policy), m_leak(false) {
282 switch (policy) {
283 default:
284 assert(0 && "We cannot reach this!");
285 case eAllocationPolicyHostOnly:
286 m_data.SetByteSize(size);
287 memset(m_data.GetBytes(), 0, size);
288 break;
289 case eAllocationPolicyProcessOnly:
290 break;
291 case eAllocationPolicyMirror:
292 m_data.SetByteSize(size);
293 memset(m_data.GetBytes(), 0, size);
294 break;
295 }
296}
297
298lldb::addr_t IRMemoryMap::Malloc(size_t size, uint8_t alignment,
299 uint32_t permissions, AllocationPolicy policy,
300 bool zero_memory, Error &error) {
301 lldb_private::Log *log(
302 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
303 error.Clear();
304
305 lldb::ProcessSP process_sp;
306 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
307 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
308
309 size_t alignment_mask = alignment - 1;
310 size_t allocation_size;
311
312 if (size == 0)
313 allocation_size = alignment;
314 else
315 allocation_size = (size & alignment_mask)
316 ? ((size + alignment) & (~alignment_mask))
317 : size;
318
319 switch (policy) {
320 default:
321 error.SetErrorToGenericError();
322 error.SetErrorString("Couldn't malloc: invalid allocation policy");
323 return LLDB_INVALID_ADDRESS;
324 case eAllocationPolicyHostOnly:
325 allocation_address = FindSpace(allocation_size);
326 if (allocation_address == LLDB_INVALID_ADDRESS) {
327 error.SetErrorToGenericError();
328 error.SetErrorString("Couldn't malloc: address space is full");
329 return LLDB_INVALID_ADDRESS;
Sean Callanand2562502013-04-19 17:44:40 +0000330 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000331 break;
332 case eAllocationPolicyMirror:
333 process_sp = m_process_wp.lock();
Todd Fialaaf245d12014-06-30 21:05:18 +0000334 if (log)
Kate Stoneb9c1b512016-09-06 20:57:50 +0000335 log->Printf("IRMemoryMap::%s process_sp=0x%" PRIx64
336 ", process_sp->CanJIT()=%s, process_sp->IsAlive()=%s",
337 __FUNCTION__, (lldb::addr_t)process_sp.get(),
338 process_sp && process_sp->CanJIT() ? "true" : "false",
339 process_sp && process_sp->IsAlive() ? "true" : "false");
340 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive()) {
341 if (!zero_memory)
342 allocation_address =
343 process_sp->AllocateMemory(allocation_size, permissions, error);
344 else
345 allocation_address =
346 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000347
Kate Stoneb9c1b512016-09-06 20:57:50 +0000348 if (!error.Success())
349 return LLDB_INVALID_ADDRESS;
350 } else {
351 if (log)
352 log->Printf("IRMemoryMap::%s switching to eAllocationPolicyHostOnly "
353 "due to failed condition (see previous expr log message)",
354 __FUNCTION__);
355 policy = eAllocationPolicyHostOnly;
356 allocation_address = FindSpace(allocation_size);
357 if (allocation_address == LLDB_INVALID_ADDRESS) {
Sean Callananfbf5c682013-05-22 22:49:06 +0000358 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000359 error.SetErrorString("Couldn't malloc: address space is full");
360 return LLDB_INVALID_ADDRESS;
361 }
Sean Callananfbf5c682013-05-22 22:49:06 +0000362 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000363 break;
364 case eAllocationPolicyProcessOnly:
365 process_sp = m_process_wp.lock();
366 if (process_sp) {
367 if (process_sp->CanJIT() && process_sp->IsAlive()) {
368 if (!zero_memory)
369 allocation_address =
370 process_sp->AllocateMemory(allocation_size, permissions, error);
Sean Callanan35005f72013-04-12 18:10:34 +0000371 else
Kate Stoneb9c1b512016-09-06 20:57:50 +0000372 allocation_address =
373 process_sp->CallocateMemory(allocation_size, permissions, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000374
Sean Callanan35005f72013-04-12 18:10:34 +0000375 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000376 return LLDB_INVALID_ADDRESS;
377 } else {
Sean Callanan35005f72013-04-12 18:10:34 +0000378 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000379 error.SetErrorString(
380 "Couldn't malloc: process doesn't support allocating memory");
381 return LLDB_INVALID_ADDRESS;
382 }
383 } else {
384 error.SetErrorToGenericError();
385 error.SetErrorString("Couldn't malloc: process doesn't exist, and this "
386 "memory must be in the process");
387 return LLDB_INVALID_ADDRESS;
Sean Callanan35005f72013-04-12 18:10:34 +0000388 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000389 break;
390 }
391
392 lldb::addr_t mask = alignment - 1;
393 aligned_address = (allocation_address + mask) & (~mask);
394
395 m_allocations[aligned_address] =
396 Allocation(allocation_address, aligned_address, allocation_size,
397 permissions, alignment, policy);
398
399 if (zero_memory) {
400 Error write_error;
401 std::vector<uint8_t> zero_buf(size, 0);
402 WriteMemory(aligned_address, zero_buf.data(), size, write_error);
403 }
404
405 if (log) {
406 const char *policy_string;
407
408 switch (policy) {
409 default:
410 policy_string = "<invalid policy>";
411 break;
412 case eAllocationPolicyHostOnly:
413 policy_string = "eAllocationPolicyHostOnly";
414 break;
415 case eAllocationPolicyProcessOnly:
416 policy_string = "eAllocationPolicyProcessOnly";
417 break;
418 case eAllocationPolicyMirror:
419 policy_string = "eAllocationPolicyMirror";
420 break;
421 }
422
423 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64
424 ", %s) -> 0x%" PRIx64,
425 (uint64_t)allocation_size, (uint64_t)alignment,
426 (uint64_t)permissions, policy_string, aligned_address);
427 }
428
429 return aligned_address;
Sean Callanan35005f72013-04-12 18:10:34 +0000430}
431
Kate Stoneb9c1b512016-09-06 20:57:50 +0000432void IRMemoryMap::Leak(lldb::addr_t process_address, Error &error) {
433 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000434
Kate Stoneb9c1b512016-09-06 20:57:50 +0000435 AllocationMap::iterator iter = m_allocations.find(process_address);
436
437 if (iter == m_allocations.end()) {
438 error.SetErrorToGenericError();
439 error.SetErrorString("Couldn't leak: allocation doesn't exist");
440 return;
441 }
442
443 Allocation &allocation = iter->second;
444
445 allocation.m_leak = true;
446}
447
448void IRMemoryMap::Free(lldb::addr_t process_address, Error &error) {
449 error.Clear();
450
451 AllocationMap::iterator iter = m_allocations.find(process_address);
452
453 if (iter == m_allocations.end()) {
454 error.SetErrorToGenericError();
455 error.SetErrorString("Couldn't free: allocation doesn't exist");
456 return;
457 }
458
459 Allocation &allocation = iter->second;
460
461 switch (allocation.m_policy) {
462 default:
463 case eAllocationPolicyHostOnly: {
464 lldb::ProcessSP process_sp = m_process_wp.lock();
465 if (process_sp) {
466 if (process_sp->CanJIT() && process_sp->IsAlive())
467 process_sp->DeallocateMemory(
468 allocation.m_process_alloc); // FindSpace allocated this for real
469 }
470
471 break;
472 }
473 case eAllocationPolicyMirror:
474 case eAllocationPolicyProcessOnly: {
475 lldb::ProcessSP process_sp = m_process_wp.lock();
476 if (process_sp)
477 process_sp->DeallocateMemory(allocation.m_process_alloc);
478 }
479 }
480
481 if (lldb_private::Log *log =
482 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
483 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64
484 "..0x%" PRIx64 ")",
485 (uint64_t)process_address, iter->second.m_process_start,
486 iter->second.m_process_start + iter->second.m_size);
487 }
488
489 m_allocations.erase(iter);
490}
491
492bool IRMemoryMap::GetAllocSize(lldb::addr_t address, size_t &size) {
493 AllocationMap::iterator iter = FindAllocation(address, size);
494 if (iter == m_allocations.end())
495 return false;
496
497 Allocation &al = iter->second;
498
499 if (address > (al.m_process_start + al.m_size)) {
500 size = 0;
501 return false;
502 }
503
504 if (address > al.m_process_start) {
505 int dif = address - al.m_process_start;
506 size = al.m_size - dif;
507 return true;
508 }
509
510 size = al.m_size;
511 return true;
512}
513
514void IRMemoryMap::WriteMemory(lldb::addr_t process_address,
515 const uint8_t *bytes, size_t size, Error &error) {
516 error.Clear();
517
518 AllocationMap::iterator iter = FindAllocation(process_address, size);
519
520 if (iter == m_allocations.end()) {
521 lldb::ProcessSP process_sp = m_process_wp.lock();
522
523 if (process_sp) {
524 process_sp->WriteMemory(process_address, bytes, size, error);
525 return;
526 }
527
528 error.SetErrorToGenericError();
529 error.SetErrorString("Couldn't write: no allocation contains the target "
530 "range and the process doesn't exist");
531 return;
532 }
533
534 Allocation &allocation = iter->second;
535
536 uint64_t offset = process_address - allocation.m_process_start;
537
538 lldb::ProcessSP process_sp;
539
540 switch (allocation.m_policy) {
541 default:
542 error.SetErrorToGenericError();
543 error.SetErrorString("Couldn't write: invalid allocation policy");
544 return;
545 case eAllocationPolicyHostOnly:
546 if (!allocation.m_data.GetByteSize()) {
547 error.SetErrorToGenericError();
548 error.SetErrorString("Couldn't write: data buffer is empty");
549 return;
550 }
551 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
552 break;
553 case eAllocationPolicyMirror:
554 if (!allocation.m_data.GetByteSize()) {
555 error.SetErrorToGenericError();
556 error.SetErrorString("Couldn't write: data buffer is empty");
557 return;
558 }
559 ::memcpy(allocation.m_data.GetBytes() + offset, bytes, size);
560 process_sp = m_process_wp.lock();
561 if (process_sp) {
562 process_sp->WriteMemory(process_address, bytes, size, error);
563 if (!error.Success())
564 return;
565 }
566 break;
567 case eAllocationPolicyProcessOnly:
568 process_sp = m_process_wp.lock();
569 if (process_sp) {
570 process_sp->WriteMemory(process_address, bytes, size, error);
571 if (!error.Success())
572 return;
573 }
574 break;
575 }
576
577 if (lldb_private::Log *log =
578 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
579 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64
580 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
581 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
582 (uint64_t)allocation.m_process_start,
583 (uint64_t)allocation.m_process_start +
584 (uint64_t)allocation.m_size);
585 }
586}
587
588void IRMemoryMap::WriteScalarToMemory(lldb::addr_t process_address,
589 Scalar &scalar, size_t size,
590 Error &error) {
591 error.Clear();
592
593 if (size == UINT32_MAX)
594 size = scalar.GetByteSize();
595
596 if (size > 0) {
597 uint8_t buf[32];
598 const size_t mem_size =
599 scalar.GetAsMemoryData(buf, size, GetByteOrder(), error);
600 if (mem_size > 0) {
601 return WriteMemory(process_address, buf, mem_size, error);
602 } else {
603 error.SetErrorToGenericError();
604 error.SetErrorString(
605 "Couldn't write scalar: failed to get scalar as memory data");
606 }
607 } else {
608 error.SetErrorToGenericError();
609 error.SetErrorString("Couldn't write scalar: its size was zero");
610 }
611 return;
612}
613
614void IRMemoryMap::WritePointerToMemory(lldb::addr_t process_address,
615 lldb::addr_t address, Error &error) {
616 error.Clear();
617
618 Scalar scalar(address);
619
620 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
621}
622
623void IRMemoryMap::ReadMemory(uint8_t *bytes, lldb::addr_t process_address,
624 size_t size, Error &error) {
625 error.Clear();
626
627 AllocationMap::iterator iter = FindAllocation(process_address, size);
628
629 if (iter == m_allocations.end()) {
630 lldb::ProcessSP process_sp = m_process_wp.lock();
631
632 if (process_sp) {
633 process_sp->ReadMemory(process_address, bytes, size, error);
634 return;
635 }
636
637 lldb::TargetSP target_sp = m_target_wp.lock();
638
639 if (target_sp) {
640 Address absolute_address(process_address);
641 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
642 return;
643 }
644
645 error.SetErrorToGenericError();
646 error.SetErrorString("Couldn't read: no allocation contains the target "
647 "range, and neither the process nor the target exist");
648 return;
649 }
650
651 Allocation &allocation = iter->second;
652
653 uint64_t offset = process_address - allocation.m_process_start;
654
655 if (offset > allocation.m_size) {
656 error.SetErrorToGenericError();
657 error.SetErrorString("Couldn't read: data is not in the allocation");
658 return;
659 }
660
661 lldb::ProcessSP process_sp;
662
663 switch (allocation.m_policy) {
664 default:
665 error.SetErrorToGenericError();
666 error.SetErrorString("Couldn't read: invalid allocation policy");
667 return;
668 case eAllocationPolicyHostOnly:
669 if (!allocation.m_data.GetByteSize()) {
670 error.SetErrorToGenericError();
671 error.SetErrorString("Couldn't read: data buffer is empty");
672 return;
673 }
674 if (allocation.m_data.GetByteSize() < offset + size) {
675 error.SetErrorToGenericError();
676 error.SetErrorString("Couldn't read: not enough underlying data");
677 return;
678 }
679
680 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
681 break;
682 case eAllocationPolicyMirror:
683 process_sp = m_process_wp.lock();
684 if (process_sp) {
685 process_sp->ReadMemory(process_address, bytes, size, error);
686 if (!error.Success())
687 return;
688 } else {
689 if (!allocation.m_data.GetByteSize()) {
690 error.SetErrorToGenericError();
691 error.SetErrorString("Couldn't read: data buffer is empty");
692 return;
693 }
694 ::memcpy(bytes, allocation.m_data.GetBytes() + offset, size);
695 }
696 break;
697 case eAllocationPolicyProcessOnly:
698 process_sp = m_process_wp.lock();
699 if (process_sp) {
700 process_sp->ReadMemory(process_address, bytes, size, error);
701 if (!error.Success())
702 return;
703 }
704 break;
705 }
706
707 if (lldb_private::Log *log =
708 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
709 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64
710 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
711 (uint64_t)process_address, (uint64_t)bytes, (uint64_t)size,
712 (uint64_t)allocation.m_process_start,
713 (uint64_t)allocation.m_process_start +
714 (uint64_t)allocation.m_size);
715 }
716}
717
718void IRMemoryMap::ReadScalarFromMemory(Scalar &scalar,
719 lldb::addr_t process_address,
720 size_t size, Error &error) {
721 error.Clear();
722
723 if (size > 0) {
724 DataBufferHeap buf(size, 0);
725 ReadMemory(buf.GetBytes(), process_address, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000726
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000727 if (!error.Success())
Kate Stoneb9c1b512016-09-06 20:57:50 +0000728 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000729
Kate Stoneb9c1b512016-09-06 20:57:50 +0000730 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(),
731 GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000732
Kate Stoneb9c1b512016-09-06 20:57:50 +0000733 lldb::offset_t offset = 0;
734
735 switch (size) {
736 default:
737 error.SetErrorToGenericError();
738 error.SetErrorStringWithFormat(
739 "Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
740 return;
741 case 1:
742 scalar = extractor.GetU8(&offset);
743 break;
744 case 2:
745 scalar = extractor.GetU16(&offset);
746 break;
747 case 4:
748 scalar = extractor.GetU32(&offset);
749 break;
750 case 8:
751 scalar = extractor.GetU64(&offset);
752 break;
753 }
754 } else {
755 error.SetErrorToGenericError();
756 error.SetErrorString("Couldn't read scalar: its size was zero");
757 }
758 return;
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000759}
760
Kate Stoneb9c1b512016-09-06 20:57:50 +0000761void IRMemoryMap::ReadPointerFromMemory(lldb::addr_t *address,
762 lldb::addr_t process_address,
763 Error &error) {
764 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000765
Kate Stoneb9c1b512016-09-06 20:57:50 +0000766 Scalar pointer_scalar;
767 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(),
768 error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000769
Kate Stoneb9c1b512016-09-06 20:57:50 +0000770 if (!error.Success())
771 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000772
Kate Stoneb9c1b512016-09-06 20:57:50 +0000773 *address = pointer_scalar.ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000774
Kate Stoneb9c1b512016-09-06 20:57:50 +0000775 return;
776}
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000777
Kate Stoneb9c1b512016-09-06 20:57:50 +0000778void IRMemoryMap::GetMemoryData(DataExtractor &extractor,
779 lldb::addr_t process_address, size_t size,
780 Error &error) {
781 error.Clear();
782
783 if (size > 0) {
784 AllocationMap::iterator iter = FindAllocation(process_address, size);
785
786 if (iter == m_allocations.end()) {
787 error.SetErrorToGenericError();
788 error.SetErrorStringWithFormat(
789 "Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64
790 ")",
791 process_address, process_address + size);
792 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000793 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000794
795 Allocation &allocation = iter->second;
796
797 switch (allocation.m_policy) {
798 default:
799 error.SetErrorToGenericError();
800 error.SetErrorString(
801 "Couldn't get memory data: invalid allocation policy");
802 return;
803 case eAllocationPolicyProcessOnly:
804 error.SetErrorToGenericError();
805 error.SetErrorString(
806 "Couldn't get memory data: memory is only in the target");
807 return;
808 case eAllocationPolicyMirror: {
809 lldb::ProcessSP process_sp = m_process_wp.lock();
810
811 if (!allocation.m_data.GetByteSize()) {
Sean Callanan458ae1c2013-04-13 02:06:42 +0000812 error.SetErrorToGenericError();
Kate Stoneb9c1b512016-09-06 20:57:50 +0000813 error.SetErrorString("Couldn't get memory data: data buffer is empty");
Sean Callanan458ae1c2013-04-13 02:06:42 +0000814 return;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000815 }
816 if (process_sp) {
817 process_sp->ReadMemory(allocation.m_process_start,
818 allocation.m_data.GetBytes(),
819 allocation.m_data.GetByteSize(), error);
820 if (!error.Success())
821 return;
822 uint64_t offset = process_address - allocation.m_process_start;
823 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
824 GetByteOrder(), GetAddressByteSize());
825 return;
826 }
827 } break;
828 case eAllocationPolicyHostOnly:
829 if (!allocation.m_data.GetByteSize()) {
830 error.SetErrorToGenericError();
831 error.SetErrorString("Couldn't get memory data: data buffer is empty");
832 return;
833 }
834 uint64_t offset = process_address - allocation.m_process_start;
835 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size,
836 GetByteOrder(), GetAddressByteSize());
837 return;
Sean Callanan458ae1c2013-04-13 02:06:42 +0000838 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000839 } else {
840 error.SetErrorToGenericError();
841 error.SetErrorString("Couldn't get memory data: its size was zero");
842 return;
843 }
Sean Callanan458ae1c2013-04-13 02:06:42 +0000844}