Skip to content

Commit 35005f7

Browse files
committed
Replicated the materialization logic for persistent
variables in the Materializer. We don't use this code yet, but will soon once the other materializers are online. llvm-svn: 179390
1 parent e14b765 commit 35005f7

File tree

5 files changed

+388
-153
lines changed

5 files changed

+388
-153
lines changed

lldb/include/lldb/Expression/IRMemoryMap.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class IRMemoryMap
3838
{
3939
public:
4040
IRMemoryMap (lldb::ProcessSP process_sp);
41+
IRMemoryMap (lldb::TargetSP target_sp);
4142
~IRMemoryMap ();
4243

4344
enum AllocationPolicy {
@@ -53,8 +54,15 @@ class IRMemoryMap
5354
void Free (lldb::addr_t process_address, Error &error);
5455

5556
void WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, size_t size, Error &error);
57+
void WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error);
5658
void ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error);
59+
void ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error);
5760

61+
lldb::ByteOrder GetByteOrder();
62+
uint32_t GetAddressByteSize();
63+
64+
ExecutionContextScope *GetBestExecutionContextScope();
65+
5866
protected:
5967
lldb::ProcessWP GetProcessWP ()
6068
{
@@ -76,9 +84,10 @@ class IRMemoryMap
7684
};
7785

7886
lldb::ProcessWP m_process_wp;
87+
lldb::TargetWP m_target_wp;
7988
typedef std::map<lldb::addr_t, Allocation> AllocationMap;
8089
AllocationMap m_allocations;
81-
90+
8291
lldb::addr_t FindSpace (size_t size);
8392
bool ContainsHostOnlyAllocations ();
8493
AllocationMap::iterator FindAllocation (lldb::addr_t addr, size_t size);

lldb/include/lldb/Expression/Materializer.h

Lines changed: 6 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ class Materializer
2929
class Dematerializer
3030
{
3131
public:
32-
void Dematerialize(Error &err);
32+
void Dematerialize(Error &err,
33+
lldb::addr_t frame_top,
34+
lldb::addr_t frame_bottom);
3335
private:
3436
friend class Materializer;
3537

@@ -54,7 +56,7 @@ class Materializer
5456

5557
uint32_t AddPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp, Error &err);
5658
uint32_t AddVariable (lldb::VariableSP &variable_sp, Error &err);
57-
uint32_t AddResultVariable (const ClangASTType &type, Error &err);
59+
uint32_t AddResultVariable (const ClangASTType &type, bool keep_in_memory, Error &err);
5860
uint32_t AddSymbol (const Symbol &symbol_sp, Error &err);
5961
uint32_t AddRegister (const RegisterInfo &register_info, Error &err);
6062

@@ -83,7 +85,8 @@ class Materializer
8385
}
8486

8587
virtual void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err) = 0;
86-
virtual void Dematerialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err) = 0;
88+
virtual void Dematerialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address,
89+
lldb::addr_t frame_top, lldb::addr_t frame_bottom, Error &err) = 0;
8790

8891
uint32_t GetAlignment ()
8992
{
@@ -250,138 +253,3 @@ class Materializer
250253
}
251254

252255
#endif
253-
//===-- Materializer.h ------------------------------------------*- C++ -*-===//
254-
//
255-
// The LLVM Compiler Infrastructure
256-
//
257-
// This file is distributed under the University of Illinois Open Source
258-
// License. See LICENSE.TXT for details.
259-
//
260-
//===----------------------------------------------------------------------===//
261-
262-
#ifndef lldb_Materializer_h
263-
#define lldb_Materializer_h
264-
265-
#include "lldb/lldb-private-types.h"
266-
#include "lldb/Core/Error.h"
267-
#include "lldb/Expression/IRMemoryMap.h"
268-
#include "lldb/Host/Mutex.h"
269-
#include "lldb/Symbol/SymbolContext.h"
270-
271-
#include <vector>
272-
273-
namespace lldb_private
274-
{
275-
276-
class Materializer
277-
{
278-
public:
279-
Materializer ();
280-
281-
class Dematerializer
282-
{
283-
public:
284-
void Dematerialize(Error &err);
285-
private:
286-
friend class Materializer;
287-
288-
Dematerializer (Materializer &materializer,
289-
lldb::StackFrameSP &frame_sp,
290-
IRMemoryMap &map,
291-
lldb::addr_t process_address) :
292-
m_materializer(materializer),
293-
m_frame_wp(frame_sp),
294-
m_map(map),
295-
m_process_address(process_address)
296-
{
297-
}
298-
299-
Materializer &m_materializer;
300-
lldb::StackFrameWP m_frame_wp;
301-
IRMemoryMap &m_map;
302-
lldb::addr_t m_process_address;
303-
};
304-
305-
Dematerializer Materialize (lldb::StackFrameSP &frame_sp, lldb::ClangExpressionVariableSP &result_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err);
306-
307-
uint32_t AddPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp, Error &err);
308-
uint32_t AddVariable (lldb::VariableSP &variable_sp, Error &err);
309-
uint32_t AddResultVariable (const ClangASTType &type, Error &err);
310-
uint32_t AddSymbol (const Symbol &symbol_sp, Error &err);
311-
uint32_t AddRegister (const RegisterInfo &register_info, Error &err);
312-
313-
uint32_t GetStructAlignment ()
314-
{
315-
return m_struct_alignment;
316-
}
317-
318-
uint32_t GetStructSize ()
319-
{
320-
return m_current_offset;
321-
}
322-
323-
uint32_t GetNumEntities ()
324-
{
325-
return m_entities.size();
326-
}
327-
328-
class Entity
329-
{
330-
public:
331-
Entity () :
332-
m_alignment(1),
333-
m_size(0),
334-
m_offset(0)
335-
{
336-
}
337-
338-
virtual ~Entity ()
339-
{
340-
}
341-
342-
virtual void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err) = 0;
343-
virtual void Dematerialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err) = 0;
344-
345-
uint32_t GetAlignment ()
346-
{
347-
return m_alignment;
348-
}
349-
350-
uint32_t GetSize ()
351-
{
352-
return m_size;
353-
}
354-
355-
uint32_t GetOffset ()
356-
{
357-
return m_offset;
358-
}
359-
360-
void SetOffset (uint32_t offset)
361-
{
362-
m_offset = offset;
363-
}
364-
protected:
365-
void SetSizeAndAlignmentFromType (ClangASTType &type);
366-
367-
uint32_t m_alignment;
368-
uint32_t m_size;
369-
uint32_t m_offset;
370-
};
371-
372-
private:
373-
uint32_t AddStructMember (Entity &entity);
374-
375-
typedef std::unique_ptr<Entity> EntityUP;
376-
typedef std::vector<EntityUP> EntityVector;
377-
378-
unsigned m_result_index;
379-
Mutex m_needs_dematerialize;
380-
EntityVector m_entities;
381-
uint32_t m_current_offset;
382-
uint32_t m_struct_alignment;
383-
};
384-
385-
}
386-
387-
#endif

lldb/source/Expression/ClangExpressionDeclMap.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,11 @@ ClangExpressionDeclMap::AddPersistentVariable
510510
var_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
511511
}
512512

513+
if (m_keep_result_in_memory)
514+
{
515+
var_sp->m_flags |= ClangExpressionVariable::EVKeepInTarget;
516+
}
517+
513518
if (log)
514519
log->Printf("Created persistent variable with flags 0x%hx", var_sp->m_flags);
515520

@@ -523,7 +528,7 @@ ClangExpressionDeclMap::AddPersistentVariable
523528
if (m_parser_vars->m_materializer)
524529
{
525530
Error err;
526-
m_parser_vars->m_materializer->AddResultVariable(user_type, err);
531+
m_parser_vars->m_materializer->AddResultVariable(user_type, m_keep_result_in_memory, err);
527532
}
528533

529534
return true;

lldb/source/Expression/IRMemoryMap.cpp

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,26 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
#include "lldb/Core/DataBufferHeap.h"
11+
#include "lldb/Core/DataExtractor.h"
1012
#include "lldb/Core/Error.h"
1113
#include "lldb/Core/Log.h"
14+
#include "lldb/Core/Scalar.h"
1215
#include "lldb/Expression/IRMemoryMap.h"
1316
#include "lldb/Target/Process.h"
17+
#include "lldb/Target/Target.h"
1418

1519
using namespace lldb_private;
1620

1721
IRMemoryMap::IRMemoryMap (lldb::ProcessSP process_sp) :
18-
m_process_wp(process_sp)
22+
m_process_wp(process_sp),
23+
m_target_wp(process_sp->GetTarget().shared_from_this())
24+
{
25+
}
26+
27+
IRMemoryMap::IRMemoryMap (lldb::TargetSP target_sp) :
28+
m_process_wp(),
29+
m_target_wp(target_sp)
1930
{
2031
}
2132

@@ -97,6 +108,54 @@ IRMemoryMap::FindAllocation (lldb::addr_t addr, size_t size)
97108
return m_allocations.end();
98109
}
99110

111+
lldb::ByteOrder
112+
IRMemoryMap::GetByteOrder()
113+
{
114+
lldb::ProcessSP process_sp = m_process_wp.lock();
115+
116+
if (process_sp)
117+
return process_sp->GetByteOrder();
118+
119+
lldb::TargetSP target_sp = m_target_wp.lock();
120+
121+
if (target_sp)
122+
return target_sp->GetDefaultArchitecture().GetByteOrder();
123+
124+
return lldb::eByteOrderInvalid;
125+
}
126+
127+
uint32_t
128+
IRMemoryMap::GetAddressByteSize()
129+
{
130+
lldb::ProcessSP process_sp = m_process_wp.lock();
131+
132+
if (process_sp)
133+
return process_sp->GetAddressByteSize();
134+
135+
lldb::TargetSP target_sp = m_target_wp.lock();
136+
137+
if (target_sp)
138+
return target_sp->GetDefaultArchitecture().GetAddressByteSize();
139+
140+
return UINT32_MAX;
141+
}
142+
143+
ExecutionContextScope *
144+
IRMemoryMap::GetBestExecutionContextScope()
145+
{
146+
lldb::ProcessSP process_sp = m_process_wp.lock();
147+
148+
if (process_sp)
149+
return process_sp.get();
150+
151+
lldb::TargetSP target_sp = m_target_wp.lock();
152+
153+
if (target_sp)
154+
return target_sp.get();
155+
156+
return NULL;
157+
}
158+
100159
lldb::addr_t
101160
IRMemoryMap::Malloc (size_t size, uint8_t alignment, uint32_t permissions, AllocationPolicy policy, Error &error)
102161
{
@@ -335,6 +394,34 @@ IRMemoryMap::WriteMemory (lldb::addr_t process_address, const uint8_t *bytes, si
335394
}
336395
}
337396

397+
void
398+
IRMemoryMap::WriteScalarToMemory (lldb::addr_t process_address, Scalar &scalar, size_t size, Error &error)
399+
{
400+
if (size == UINT32_MAX)
401+
size = scalar.GetByteSize();
402+
403+
if (size > 0)
404+
{
405+
uint8_t buf[32];
406+
const size_t mem_size = scalar.GetAsMemoryData (buf, size, GetByteOrder(), error);
407+
if (mem_size > 0)
408+
{
409+
return WriteMemory(process_address, buf, mem_size, error);
410+
}
411+
else
412+
{
413+
error.SetErrorToGenericError();
414+
error.SetErrorString ("Couldn't write scalar: failed to get scalar as memory data");
415+
}
416+
}
417+
else
418+
{
419+
error.SetErrorToGenericError();
420+
error.SetErrorString ("Couldn't write scalar: its size was zero");
421+
}
422+
return;
423+
}
424+
338425
void
339426
IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t size, Error &error)
340427
{
@@ -408,3 +495,39 @@ IRMemoryMap::ReadMemory (uint8_t *bytes, lldb::addr_t process_address, size_t si
408495
(uint64_t)allocation.m_process_start + (uint64_t)allocation.m_size);
409496
}
410497
}
498+
499+
void
500+
IRMemoryMap::ReadScalarFromMemory (Scalar &scalar, lldb::addr_t process_address, size_t size, Error &error)
501+
{
502+
if (size > 0)
503+
{
504+
DataBufferHeap buf(size, 0);
505+
ReadMemory(buf.GetBytes(), process_address, size, error);
506+
507+
if (!error.Success())
508+
return;
509+
510+
DataExtractor extractor(buf.GetBytes(), buf.GetByteSize(), GetByteOrder(), GetAddressByteSize());
511+
512+
lldb::offset_t offset = 0;
513+
514+
switch (size)
515+
{
516+
default:
517+
error.SetErrorToGenericError();
518+
error.SetErrorStringWithFormat("Couldn't read scalar: unsupported size %lld", (unsigned long long)size);
519+
return;
520+
case 1: scalar = extractor.GetU8(&offset); break;
521+
case 2: scalar = extractor.GetU16(&offset); break;
522+
case 4: scalar = extractor.GetU32(&offset); break;
523+
case 8: scalar = extractor.GetU64(&offset); break;
524+
}
525+
}
526+
else
527+
{
528+
error.SetErrorToGenericError();
529+
error.SetErrorString ("Couldn't write scalar: its size was zero");
530+
}
531+
return;
532+
}
533+

0 commit comments

Comments
 (0)