blob: 8890b44ced4edb235aeceb13614d486df4d0fb63 [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
Sean Callanan35005f72013-04-12 18:10:34 +000010#include "lldb/Core/DataBufferHeap.h"
11#include "lldb/Core/DataExtractor.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000012#include "lldb/Core/Error.h"
13#include "lldb/Core/Log.h"
Sean Callanan35005f72013-04-12 18:10:34 +000014#include "lldb/Core/Scalar.h"
Sean Callanan5a1af4e2013-04-05 02:22:57 +000015#include "lldb/Expression/IRMemoryMap.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
Sean Callanan35005f72013-04-12 18:10:34 +000023IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) :
Sean Callanan35005f72013-04-12 18:10:34 +000024 m_target_wp(target_sp)
Sean Callanan5a1af4e2013-04-05 02:22:57 +000025{
Sean Callananb024d872013-04-15 17:12:47 +000026 if (target_sp)
27 m_process_wp = target_sp->GetProcessSP();
Sean Callanan5a1af4e2013-04-05 02:22:57 +000028}
29
30IRMemoryMap::~IRMemoryMap ()
31{
32 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000033
Sean Callanan5a1af4e2013-04-05 02:22:57 +000034 if (process_sp)
35 {
Sean Callananbb777042013-05-16 17:30:37 +000036 AllocationMap::iterator iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000037
Sean Callananbb777042013-05-16 17:30:37 +000038 Error err;
39
40 while ((iter = m_allocations.begin()) != m_allocations.end())
Sean Callanan5a1af4e2013-04-05 02:22:57 +000041 {
Sean Callananbb777042013-05-16 17:30:37 +000042 err.Clear();
Sean Callananfbf5c682013-05-22 22:49:06 +000043 if (iter->second.m_leak)
44 m_allocations.erase(iter);
45 else
46 Free(iter->first, err);
Sean Callanan5a1af4e2013-04-05 02:22:57 +000047 }
48 }
49}
50
51lldb::addr_t
Sean Callananf3df7e82016-06-09 20:22:25 +000052IRMemoryMap::FindSpace (size_t size)
Sean Callanan5a1af4e2013-04-05 02:22:57 +000053{
Sean Callananf3df7e82016-06-09 20:22:25 +000054 // 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 Callananbb9945f2013-04-19 01:51:24 +000066 lldb::TargetSP target_sp = m_target_wp.lock();
67 lldb::ProcessSP process_sp = m_process_wp.lock();
Sean Callananf3df7e82016-06-09 20:22:25 +000068
69 const bool process_is_alive = process_sp && process_sp->IsAlive();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000070
Sean Callananbb9945f2013-04-19 01:51:24 +000071 lldb::addr_t ret = LLDB_INVALID_ADDRESS;
Zachary Turner3ddcd312014-07-09 16:42:27 +000072 if (size == 0)
73 return ret;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000074
Sean Callananf3df7e82016-06-09 20:22:25 +000075 if (process_is_alive && process_sp->CanJIT())
Sean Callanandf565402013-04-27 02:19:33 +000076 {
77 Error alloc_error;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000078
Sean Callananf3df7e82016-06-09 20:22:25 +000079 ret = process_sp->AllocateMemory(size, lldb::ePermissionsReadable | lldb::ePermissionsWritable, alloc_error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +000080
Sean Callanandf565402013-04-27 02:19:33 +000081 if (!alloc_error.Success())
82 return LLDB_INVALID_ADDRESS;
83 else
84 return ret;
85 }
Sean Callananf3df7e82016-06-09 20:22:25 +000086
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 Ledruceab3ac2014-07-06 17:54:58 +000091
Sean Callananf3df7e82016-06-09 20:22:25 +000092 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 Callanan5a1af4e2013-04-05 02:22:57 +0000177 {
Zachary Turner3ddcd312014-07-09 16:42:27 +0000178 auto back = m_allocations.rbegin();
179 lldb::addr_t addr = back->first;
180 size_t alloc_size = back->second.m_size;
Rafael Espindolaa94ae1e2016-01-18 20:57:54 +0000181 ret = llvm::alignTo(addr+alloc_size, 4096);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000182 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000183
Sean Callananbb9945f2013-04-19 01:51:24 +0000184 return ret;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000185}
186
187IRMemoryMap::AllocationMap::iterator
188IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size)
189{
Sean Callanan1582ee62013-04-18 22:06:33 +0000190 if (addr == LLDB_INVALID_ADDRESS)
191 return m_allocations.end();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000192
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000193 AllocationMap::iterator iter = m_allocations.lower_bound (addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000194
Sean Callanan14b1bae2013-04-16 23:25:35 +0000195 if (iter == m_allocations.end() ||
196 iter->first > addr)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000197 {
198 if (iter == m_allocations.begin())
199 return m_allocations.end();
200 iter--;
201 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000202
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000203 if (iter->first <= addr && iter->first + iter->second.m_size >= addr + size)
204 return iter;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000205
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000206 return m_allocations.end();
207}
208
Sean Callananbb9945f2013-04-19 01:51:24 +0000209bool
Zachary Turner15362442014-06-25 18:37:19 +0000210IRMemoryMap::IntersectsAllocation (lldb::addr_t addr, size_t size) const
Sean Callananbb9945f2013-04-19 01:51:24 +0000211{
212 if (addr == LLDB_INVALID_ADDRESS)
213 return false;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000214
Zachary Turner15362442014-06-25 18:37:19 +0000215 AllocationMap::const_iterator iter = m_allocations.lower_bound (addr);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000216
Zachary Turner15362442014-06-25 18:37:19 +0000217 // 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 Turnercfcd7912014-06-25 18:40:58 +0000224 if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size))
Sean Callananbb9945f2013-04-19 01:51:24 +0000225 return true;
Sean Callananbb9945f2013-04-19 01:51:24 +0000226 }
Zachary Turner15362442014-06-25 18:37:19 +0000227
228 if (iter != m_allocations.begin()) {
229 --iter;
Zachary Turnercfcd7912014-06-25 18:40:58 +0000230 if (AllocationsIntersect(addr, size, iter->second.m_process_start, iter->second.m_size))
Zachary Turner15362442014-06-25 18:37:19 +0000231 return true;
232 }
233
Sean Callananbb9945f2013-04-19 01:51:24 +0000234 return false;
235}
236
Zachary Turner15362442014-06-25 18:37:19 +0000237bool
238IRMemoryMap::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 Callanan35005f72013-04-12 18:10:34 +0000252lldb::ByteOrder
253IRMemoryMap::GetByteOrder()
254{
255 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000256
Sean Callanan35005f72013-04-12 18:10:34 +0000257 if (process_sp)
258 return process_sp->GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000259
Sean Callanan35005f72013-04-12 18:10:34 +0000260 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000261
Sean Callanan35005f72013-04-12 18:10:34 +0000262 if (target_sp)
Sean Callanan08052af2013-04-17 07:50:58 +0000263 return target_sp->GetArchitecture().GetByteOrder();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000264
Sean Callanan35005f72013-04-12 18:10:34 +0000265 return lldb::eByteOrderInvalid;
266}
267
268uint32_t
269IRMemoryMap::GetAddressByteSize()
270{
271 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000272
Sean Callanan35005f72013-04-12 18:10:34 +0000273 if (process_sp)
274 return process_sp->GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000275
Sean Callanan35005f72013-04-12 18:10:34 +0000276 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000277
Sean Callanan35005f72013-04-12 18:10:34 +0000278 if (target_sp)
Sean Callanan08052af2013-04-17 07:50:58 +0000279 return target_sp->GetArchitecture().GetAddressByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000280
Sean Callanan35005f72013-04-12 18:10:34 +0000281 return UINT32_MAX;
282}
283
284ExecutionContextScope *
Greg Clayton23f8c952014-03-24 23:10:19 +0000285IRMemoryMap::GetBestExecutionContextScope() const
Sean Callanan35005f72013-04-12 18:10:34 +0000286{
287 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000288
Sean Callanan35005f72013-04-12 18:10:34 +0000289 if (process_sp)
290 return process_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000291
Sean Callanan35005f72013-04-12 18:10:34 +0000292 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000293
Sean Callanan35005f72013-04-12 18:10:34 +0000294 if (target_sp)
295 return target_sp.get();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000296
Sean Callanan35005f72013-04-12 18:10:34 +0000297 return NULL;
298}
299
Sean Callanand2562502013-04-19 17:44:40 +0000300IRMemoryMap::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 Sartain6fea7792013-08-06 22:21:08 +0000305 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 Callanand2562502013-04-19 17:44:40 +0000313{
Sean Callanand2562502013-04-19 17:44:40 +0000314 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 Callanan5a1af4e2013-04-05 02:22:57 +0000331lldb::addr_t
Jim Ingham2c381412015-11-04 20:32:27 +0000332IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, bool zero_memory, Error &error)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000333{
Todd Fialaaf245d12014-06-30 21:05:18 +0000334 lldb_private::Log *log (lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
Sean Callanan08052af2013-04-17 07:50:58 +0000335 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000336
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000337 lldb::ProcessSP process_sp;
338 lldb::addr_t allocation_address = LLDB_INVALID_ADDRESS;
339 lldb::addr_t aligned_address = LLDB_INVALID_ADDRESS;
Matt Kopec750dcc32013-04-26 17:48:01 +0000340
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 Ledruceab3ac2014-07-06 17:54:58 +0000348
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000349 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 Callanan5a1af4e2013-04-05 02:22:57 +0000365 process_sp = m_process_wp.lock();
Todd Fialaaf245d12014-06-30 21:05:18 +0000366 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 Callananad7cc462013-06-17 21:19:31 +0000368 if (process_sp && process_sp->CanJIT() && process_sp->IsAlive())
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000369 {
Jim Ingham2c381412015-11-04 20:32:27 +0000370 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 Callanan5a1af4e2013-04-05 02:22:57 +0000375 if (!error.Success())
376 return LLDB_INVALID_ADDRESS;
377 }
378 else
379 {
Todd Fialaaf245d12014-06-30 21:05:18 +0000380 if (log)
381 log->Printf ("IRMemoryMap::%s switching to eAllocationPolicyHostOnly due to failed condition (see previous expr log message)", __FUNCTION__);
Sean Callananbb9945f2013-04-19 01:51:24 +0000382 policy = eAllocationPolicyHostOnly;
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000383 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 Callanan5a1af4e2013-04-05 02:22:57 +0000393 process_sp = m_process_wp.lock();
394 if (process_sp)
395 {
Sean Callananad7cc462013-06-17 21:19:31 +0000396 if (process_sp->CanJIT() && process_sp->IsAlive())
Sean Callananbb9945f2013-04-19 01:51:24 +0000397 {
Jim Ingham2c381412015-11-04 20:32:27 +0000398 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 Callananbb9945f2013-04-19 01:51:24 +0000403 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 Callanan5a1af4e2013-04-05 02:22:57 +0000410 return LLDB_INVALID_ADDRESS;
Sean Callananbb9945f2013-04-19 01:51:24 +0000411 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000412 }
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 Ledruceab3ac2014-07-06 17:54:58 +0000421
422
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000423 lldb::addr_t mask = alignment - 1;
424 aligned_address = (allocation_address + mask) & (~mask);
425
Sean Callanand2562502013-04-19 17:44:40 +0000426 m_allocations[aligned_address] = Allocation(allocation_address,
427 aligned_address,
Matt Kopec750dcc32013-04-26 17:48:01 +0000428 allocation_size,
Sean Callanand2562502013-04-19 17:44:40 +0000429 permissions,
430 alignment,
431 policy);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000432
Todd Fialaaf245d12014-06-30 21:05:18 +0000433 if (log)
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000434 {
435 const char * policy_string;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000436
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000437 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 Ledruceab3ac2014-07-06 17:54:58 +0000452
Matt Kopecef143712013-06-03 18:00:07 +0000453 log->Printf("IRMemoryMap::Malloc (%" PRIu64 ", 0x%" PRIx64 ", 0x%" PRIx64 ", %s) -> 0x%" PRIx64,
Matt Kopec750dcc32013-04-26 17:48:01 +0000454 (uint64_t)allocation_size,
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000455 (uint64_t)alignment,
456 (uint64_t)permissions,
457 policy_string,
458 aligned_address);
459 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000460
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000461 return aligned_address;
462}
463
464void
Sean Callananfbf5c682013-05-22 22:49:06 +0000465IRMemoryMap::Leak (lldb::addr_t process_address, Error &error)
466{
467 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000468
Sean Callananfbf5c682013-05-22 22:49:06 +0000469 AllocationMap::iterator iter = m_allocations.find(process_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000470
Sean Callananfbf5c682013-05-22 22:49:06 +0000471 if (iter == m_allocations.end())
472 {
473 error.SetErrorToGenericError();
474 error.SetErrorString("Couldn't leak: allocation doesn't exist");
475 return;
476 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000477
Sean Callananfbf5c682013-05-22 22:49:06 +0000478 Allocation &allocation = iter->second;
479
480 allocation.m_leak = true;
481}
482
483void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000484IRMemoryMap::Free (lldb::addr_t process_address, Error &error)
485{
Sean Callanan08052af2013-04-17 07:50:58 +0000486 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000487
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000488 AllocationMap::iterator iter = m_allocations.find(process_address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000489
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000490 if (iter == m_allocations.end())
491 {
492 error.SetErrorToGenericError();
493 error.SetErrorString("Couldn't free: allocation doesn't exist");
494 return;
495 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000496
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000497 Allocation &allocation = iter->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000498
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000499 switch (allocation.m_policy)
500 {
501 default:
502 case eAllocationPolicyHostOnly:
Sean Callanandf565402013-04-27 02:19:33 +0000503 {
504 lldb::ProcessSP process_sp = m_process_wp.lock();
Sean Callananbb777042013-05-16 17:30:37 +0000505 if (process_sp)
506 {
Sean Callananad7cc462013-06-17 21:19:31 +0000507 if (process_sp->CanJIT() && process_sp->IsAlive())
Sean Callananbb777042013-05-16 17:30:37 +0000508 process_sp->DeallocateMemory(allocation.m_process_alloc); // FindSpace allocated this for real
Sean Callananbb777042013-05-16 17:30:37 +0000509 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000510
Sean Callanandf565402013-04-27 02:19:33 +0000511 break;
512 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000513 case eAllocationPolicyMirror:
514 case eAllocationPolicyProcessOnly:
Sean Callanandf565402013-04-27 02:19:33 +0000515 {
516 lldb::ProcessSP process_sp = m_process_wp.lock();
517 if (process_sp)
518 process_sp->DeallocateMemory(allocation.m_process_alloc);
519 }
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000520 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000521
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000522 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000523 {
Matt Kopecef143712013-06-03 18:00:07 +0000524 log->Printf("IRMemoryMap::Free (0x%" PRIx64 ") freed [0x%" PRIx64 "..0x%" PRIx64 ")",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000525 (uint64_t)process_address,
526 iter->second.m_process_start,
527 iter->second.m_process_start + iter->second.m_size);
528 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000529
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000530 m_allocations.erase(iter);
531}
532
Ewan Crawford90ff7912015-07-14 10:56:58 +0000533bool
534IRMemoryMap::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 Callanan5a1af4e2013-04-05 02:22:57 +0000559void
560IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error)
561{
Sean Callanan08052af2013-04-17 07:50:58 +0000562 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000563
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000564 AllocationMap::iterator iter = FindAllocation(process_address, size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000565
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000566 if (iter == m_allocations.end())
567 {
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000568 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000569
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000570 if (process_sp)
571 {
572 process_sp->WriteMemory(process_address, bytes, size, error);
573 return;
574 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000575
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000576 error.SetErrorToGenericError();
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000577 error.SetErrorString("Couldn't write: no allocation contains the target range and the process doesn't exist");
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000578 return;
579 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000580
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000581 Allocation &allocation = iter->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000582
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000583 uint64_t offset = process_address - allocation.m_process_start;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000584
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000585 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 Callanand2562502013-04-19 17:44:40 +0000594 if (!allocation.m_data.GetByteSize())
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000595 {
596 error.SetErrorToGenericError();
597 error.SetErrorString("Couldn't write: data buffer is empty");
598 return;
599 }
Sean Callanand2562502013-04-19 17:44:40 +0000600 ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000601 break;
602 case eAllocationPolicyMirror:
Sean Callanand2562502013-04-19 17:44:40 +0000603 if (!allocation.m_data.GetByteSize())
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000604 {
605 error.SetErrorToGenericError();
606 error.SetErrorString("Couldn't write: data buffer is empty");
607 return;
608 }
Sean Callanand2562502013-04-19 17:44:40 +0000609 ::memcpy (allocation.m_data.GetBytes() + offset, bytes, size);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000610 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 Ledruceab3ac2014-07-06 17:54:58 +0000628
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000629 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000630 {
Matt Kopecef143712013-06-03 18:00:07 +0000631 log->Printf("IRMemoryMap::WriteMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") went to [0x%" PRIx64 "..0x%" PRIx64 ")",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000632 (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
640void
Sean Callanan35005f72013-04-12 18:10:34 +0000641IRMemoryMap::WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error)
Sean Callanan08052af2013-04-17 07:50:58 +0000642{
643 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000644
Sean Callanan35005f72013-04-12 18:10:34 +0000645 if (size == UINT32_MAX)
646 size = scalar.GetByteSize();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000647
Sean Callanan35005f72013-04-12 18:10:34 +0000648 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
670void
Sean Callananf8043fa2013-04-12 21:40:34 +0000671IRMemoryMap::WritePointerToMemory (lldb::addr_t process_address, lldb::addr_t address, Error &error)
672{
Sean Callanan08052af2013-04-17 07:50:58 +0000673 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000674
Sean Callananf8043fa2013-04-12 21:40:34 +0000675 Scalar scalar(address);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000676
Sean Callananf8043fa2013-04-12 21:40:34 +0000677 WriteScalarToMemory(process_address, scalar, GetAddressByteSize(), error);
678}
679
Sean Callananf8043fa2013-04-12 21:40:34 +0000680void
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000681IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error)
682{
Sean Callanan08052af2013-04-17 07:50:58 +0000683 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000684
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000685 AllocationMap::iterator iter = FindAllocation(process_address, size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000686
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000687 if (iter == m_allocations.end())
688 {
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000689 lldb::ProcessSP process_sp = m_process_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000690
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000691 if (process_sp)
692 {
693 process_sp->ReadMemory(process_address, bytes, size, error);
694 return;
695 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000696
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000697 lldb::TargetSP target_sp = m_target_wp.lock();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000698
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000699 if (target_sp)
700 {
701 Address absolute_address(process_address);
702 target_sp->ReadMemory(absolute_address, false, bytes, size, error);
703 return;
704 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000705
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000706 error.SetErrorToGenericError();
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000707 error.SetErrorString("Couldn't read: no allocation contains the target range, and neither the process nor the target exist");
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000708 return;
709 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000710
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000711 Allocation &allocation = iter->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000712
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000713 uint64_t offset = process_address - allocation.m_process_start;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000714
Sean Callanan9bbf3cd2014-03-04 21:56:11 +0000715 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 Ledruceab3ac2014-07-06 17:54:58 +0000721
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000722 lldb::ProcessSP process_sp;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000723
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000724 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 Callanand2562502013-04-19 17:44:40 +0000731 if (!allocation.m_data.GetByteSize())
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000732 {
733 error.SetErrorToGenericError();
734 error.SetErrorString("Couldn't read: data buffer is empty");
735 return;
736 }
Sean Callanan9bbf3cd2014-03-04 21:56:11 +0000737 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 Callanand2562502013-04-19 17:44:40 +0000744 ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000745 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 Callanand2562502013-04-19 17:44:40 +0000756 if (!allocation.m_data.GetByteSize())
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000757 {
758 error.SetErrorToGenericError();
759 error.SetErrorString("Couldn't read: data buffer is empty");
760 return;
761 }
Sean Callanand2562502013-04-19 17:44:40 +0000762 ::memcpy (bytes, allocation.m_data.GetBytes() + offset, size);
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000763 }
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 Ledruceab3ac2014-07-06 17:54:58 +0000775
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000776 if (lldb_private::Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
777 {
Matt Kopecef143712013-06-03 18:00:07 +0000778 log->Printf("IRMemoryMap::ReadMemory (0x%" PRIx64 ", 0x%" PRIx64 ", 0x%" PRId64 ") came from [0x%" PRIx64 "..0x%" PRIx64 ")",
Sean Callanan5a1af4e2013-04-05 02:22:57 +0000779 (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 Callanan35005f72013-04-12 18:10:34 +0000786
787void
788IRMemoryMap::ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error)
Sean Callanan08052af2013-04-17 07:50:58 +0000789{
790 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000791
Sean Callanan35005f72013-04-12 18:10:34 +0000792 if (size > 0)
793 {
794 DataBufferHeap buf(size, 0);
795 ReadMemory(buf.GetBytes(), process_address, size, error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000796
Sean Callanan35005f72013-04-12 18:10:34 +0000797 if (!error.Success())
798 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000799
Sean Callanan35005f72013-04-12 18:10:34 +0000800 DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize());
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000801
Sean Callanan35005f72013-04-12 18:10:34 +0000802 lldb::offset_t offset = 0;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000803
Sean Callanan35005f72013-04-12 18:10:34 +0000804 switch (size)
805 {
806 default:
807 error.SetErrorToGenericError();
Greg Claytone86cef62013-06-04 21:30:42 +0000808 error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %" PRIu64, (uint64_t)size);
Sean Callanan35005f72013-04-12 18:10:34 +0000809 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 Callanan458ae1c2013-04-13 02:06:42 +0000819 error.SetErrorString ("Couldn't read scalar: its size was zero");
Sean Callanan35005f72013-04-12 18:10:34 +0000820 }
821 return;
822}
823
Sean Callanan458ae1c2013-04-13 02:06:42 +0000824void
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000825IRMemoryMap::ReadPointerFromMemory (lldb::addr_t *address, lldb::addr_t process_address, Error &error)
826{
Sean Callanan08052af2013-04-17 07:50:58 +0000827 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000828
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000829 Scalar pointer_scalar;
830 ReadScalarFromMemory(pointer_scalar, process_address, GetAddressByteSize(), error);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000831
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000832 if (!error.Success())
833 return;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000834
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000835 *address = pointer_scalar.ULongLong();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000836
Sean Callanan2d37e5a2013-04-15 22:48:23 +0000837 return;
838}
839
840void
Sean Callanan458ae1c2013-04-13 02:06:42 +0000841IRMemoryMap::GetMemoryData (DataExtractor &extractor, lldb::addr_t process_address, size_t size, Error &error)
842{
Sean Callanan08052af2013-04-17 07:50:58 +0000843 error.Clear();
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000844
Sean Callanan458ae1c2013-04-13 02:06:42 +0000845 if (size > 0)
846 {
847 AllocationMap::iterator iter = FindAllocation(process_address, size);
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000848
Sean Callanan458ae1c2013-04-13 02:06:42 +0000849 if (iter == m_allocations.end())
850 {
851 error.SetErrorToGenericError();
Matt Kopecef143712013-06-03 18:00:07 +0000852 error.SetErrorStringWithFormat("Couldn't find an allocation containing [0x%" PRIx64 "..0x%" PRIx64 ")", process_address, process_address + size);
Sean Callanan458ae1c2013-04-13 02:06:42 +0000853 return;
854 }
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000855
Sean Callanan458ae1c2013-04-13 02:06:42 +0000856 Allocation &allocation = iter->second;
Sylvestre Ledruceab3ac2014-07-06 17:54:58 +0000857
Sean Callanan458ae1c2013-04-13 02:06:42 +0000858 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 Callanan458ae1c2013-04-13 02:06:42 +0000868 case eAllocationPolicyMirror:
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000869 {
870 lldb::ProcessSP process_sp = m_process_wp.lock();
871
Sean Callanand2562502013-04-19 17:44:40 +0000872 if (!allocation.m_data.GetByteSize())
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000873 {
874 error.SetErrorToGenericError();
875 error.SetErrorString("Couldn't get memory data: data buffer is empty");
876 return;
877 }
878 if (process_sp)
879 {
Sean Callanand2562502013-04-19 17:44:40 +0000880 process_sp->ReadMemory(allocation.m_process_start, allocation.m_data.GetBytes(), allocation.m_data.GetByteSize(), error);
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000881 if (!error.Success())
882 return;
883 uint64_t offset = process_address - allocation.m_process_start;
Sean Callanand2562502013-04-19 17:44:40 +0000884 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize());
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000885 return;
886 }
887 }
Greg Claytoncec91ef2016-02-26 01:20:20 +0000888 break;
Sean Callananc8c5b8d2013-04-15 21:35:52 +0000889 case eAllocationPolicyHostOnly:
Sean Callanand2562502013-04-19 17:44:40 +0000890 if (!allocation.m_data.GetByteSize())
Sean Callanan458ae1c2013-04-13 02:06:42 +0000891 {
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 Callanand2562502013-04-19 17:44:40 +0000897 extractor = DataExtractor(allocation.m_data.GetBytes() + offset, size, GetByteOrder(), GetAddressByteSize());
Sean Callanan458ae1c2013-04-13 02:06:42 +0000898 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}