1 //===-- Materializer.cpp ----------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
12 // Other libraries and framework includes
14 #include "lldb/Core/Log.h"
15 #include "lldb/Core/RegisterValue.h"
16 #include "lldb/Core/ValueObjectConstResult.h"
17 #include "lldb/Core/ValueObjectVariable.h"
18 #include "lldb/Expression/ExpressionVariable.h"
19 #include "lldb/Expression/Materializer.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Symbol/Symbol.h"
22 #include "lldb/Symbol/Type.h"
23 #include "lldb/Symbol/Variable.h"
24 #include "lldb/Target/ExecutionContext.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/StackFrame.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
30 using namespace lldb_private;
33 Materializer::AddStructMember (Entity &entity)
35 uint32_t size = entity.GetSize();
36 uint32_t alignment = entity.GetAlignment();
40 if (m_current_offset == 0)
41 m_struct_alignment = alignment;
43 if (m_current_offset % alignment)
44 m_current_offset += (alignment - (m_current_offset % alignment));
46 ret = m_current_offset;
48 m_current_offset += size;
54 Materializer::Entity::SetSizeAndAlignmentFromType (CompilerType &type)
56 m_size = type.GetByteSize(nullptr);
58 uint32_t bit_alignment = type.GetTypeBitAlign();
60 if (bit_alignment % 8)
63 bit_alignment &= ~((uint32_t)0x111u);
66 m_alignment = bit_alignment / 8;
69 class EntityPersistentVariable : public Materializer::Entity
72 EntityPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp,
73 Materializer::PersistentVariableDelegate *delegate) :
75 m_persistent_variable_sp(persistent_variable_sp),
78 // Hard-coding to maximum size of a pointer since persistent variables are materialized by reference
83 void MakeAllocation (IRMemoryMap &map, Error &err)
85 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
87 // Allocate a spare memory area to store the persistent variable's contents.
90 const bool zero_memory = false;
92 lldb::addr_t mem = map.Malloc(m_persistent_variable_sp->GetByteSize(),
94 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
95 IRMemoryMap::eAllocationPolicyMirror,
99 if (!allocate_error.Success())
101 err.SetErrorStringWithFormat("couldn't allocate a memory area to store %s: %s", m_persistent_variable_sp->GetName().GetCString(), allocate_error.AsCString());
106 log->Printf("Allocated %s (0x%" PRIx64 ") successfully", m_persistent_variable_sp->GetName().GetCString(), mem);
108 // Put the location of the spare memory into the live data of the ValueObject.
110 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope(),
111 m_persistent_variable_sp->GetCompilerType(),
112 m_persistent_variable_sp->GetName(),
115 map.GetAddressByteSize());
117 // Clear the flag if the variable will never be deallocated.
119 if (m_persistent_variable_sp->m_flags & ExpressionVariable::EVKeepInTarget)
122 map.Leak(mem, leak_error);
123 m_persistent_variable_sp->m_flags &= ~ExpressionVariable::EVNeedsAllocation;
126 // Write the contents of the variable to the area.
130 map.WriteMemory (mem,
131 m_persistent_variable_sp->GetValueBytes(),
132 m_persistent_variable_sp->GetByteSize(),
135 if (!write_error.Success())
137 err.SetErrorStringWithFormat ("couldn't write %s to the target: %s", m_persistent_variable_sp->GetName().AsCString(),
138 write_error.AsCString());
143 void DestroyAllocation (IRMemoryMap &map, Error &err)
145 Error deallocate_error;
147 map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong(), deallocate_error);
149 m_persistent_variable_sp->m_live_sp.reset();
151 if (!deallocate_error.Success())
153 err.SetErrorStringWithFormat ("couldn't deallocate memory for %s: %s", m_persistent_variable_sp->GetName().GetCString(), deallocate_error.AsCString());
157 void Materialize(lldb::StackFrameSP &frame_sp,
159 lldb::addr_t process_address,
162 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
164 const lldb::addr_t load_addr = process_address + m_offset;
168 log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64 ", m_name = %s, m_flags = 0x%hx]",
170 m_persistent_variable_sp->GetName().AsCString(),
171 m_persistent_variable_sp->m_flags);
174 if (m_persistent_variable_sp->m_flags & ExpressionVariable::EVNeedsAllocation)
176 MakeAllocation(map, err);
177 m_persistent_variable_sp->m_flags |= ExpressionVariable::EVIsLLDBAllocated;
183 if ((m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsProgramReference && m_persistent_variable_sp->m_live_sp) ||
184 m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsLLDBAllocated)
188 map.WriteScalarToMemory(load_addr,
189 m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(),
190 map.GetAddressByteSize(),
193 if (!write_error.Success())
195 err.SetErrorStringWithFormat("couldn't write the location of %s to memory: %s", m_persistent_variable_sp->GetName().AsCString(), write_error.AsCString());
200 err.SetErrorStringWithFormat("no materialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
205 void Dematerialize(lldb::StackFrameSP &frame_sp,
207 lldb::addr_t process_address,
208 lldb::addr_t frame_top,
209 lldb::addr_t frame_bottom,
212 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
214 const lldb::addr_t load_addr = process_address + m_offset;
218 log->Printf("EntityPersistentVariable::Dematerialize [address = 0x%" PRIx64 ", m_name = %s, m_flags = 0x%hx]",
219 (uint64_t)process_address + m_offset,
220 m_persistent_variable_sp->GetName().AsCString(),
221 m_persistent_variable_sp->m_flags);
226 m_delegate->DidDematerialize(m_persistent_variable_sp);
229 if ((m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsLLDBAllocated) ||
230 (m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsProgramReference))
232 if (m_persistent_variable_sp->m_flags & ExpressionVariable::EVIsProgramReference &&
233 !m_persistent_variable_sp->m_live_sp)
235 // If the reference comes from the program, then the ClangExpressionVariable's
236 // live variable data hasn't been set up yet. Do this now.
238 lldb::addr_t location;
241 map.ReadPointerFromMemory(&location, load_addr, read_error);
243 if (!read_error.Success())
245 err.SetErrorStringWithFormat("couldn't read the address of program-allocated variable %s: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
249 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope (),
250 m_persistent_variable_sp.get()->GetCompilerType(),
251 m_persistent_variable_sp->GetName(),
254 m_persistent_variable_sp->GetByteSize());
256 if (frame_top != LLDB_INVALID_ADDRESS &&
257 frame_bottom != LLDB_INVALID_ADDRESS &&
258 location >= frame_bottom &&
259 location <= frame_top)
261 // If the variable is resident in the stack frame created by the expression,
262 // then it cannot be relied upon to stay around. We treat it as needing
264 m_persistent_variable_sp->m_flags |= ExpressionVariable::EVIsLLDBAllocated;
265 m_persistent_variable_sp->m_flags |= ExpressionVariable::EVNeedsAllocation;
266 m_persistent_variable_sp->m_flags |= ExpressionVariable::EVNeedsFreezeDry;
267 m_persistent_variable_sp->m_flags &= ~ExpressionVariable::EVIsProgramReference;
271 lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong();
273 if (!m_persistent_variable_sp->m_live_sp)
275 err.SetErrorStringWithFormat("couldn't find the memory area used to store %s", m_persistent_variable_sp->GetName().GetCString());
279 if (m_persistent_variable_sp->m_live_sp->GetValue().GetValueAddressType() != eAddressTypeLoad)
281 err.SetErrorStringWithFormat("the address of the memory area for %s is in an incorrect format", m_persistent_variable_sp->GetName().GetCString());
285 if (m_persistent_variable_sp->m_flags & ExpressionVariable::EVNeedsFreezeDry ||
286 m_persistent_variable_sp->m_flags & ExpressionVariable::EVKeepInTarget)
289 log->Printf("Dematerializing %s from 0x%" PRIx64 " (size = %llu)", m_persistent_variable_sp->GetName().GetCString(), (uint64_t)mem, (unsigned long long)m_persistent_variable_sp->GetByteSize());
291 // Read the contents of the spare memory area
293 m_persistent_variable_sp->ValueUpdated ();
297 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(),
299 m_persistent_variable_sp->GetByteSize(),
302 if (!read_error.Success())
304 err.SetErrorStringWithFormat ("couldn't read the contents of %s from memory: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
308 m_persistent_variable_sp->m_flags &= ~ExpressionVariable::EVNeedsFreezeDry;
313 err.SetErrorStringWithFormat("no dematerialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
317 lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
319 !process_sp->CanJIT())
321 // Allocations are not persistent so persistent variables cannot stay materialized.
323 m_persistent_variable_sp->m_flags |= ExpressionVariable::EVNeedsAllocation;
325 DestroyAllocation(map, err);
329 else if (m_persistent_variable_sp->m_flags & ExpressionVariable::EVNeedsAllocation &&
330 !(m_persistent_variable_sp->m_flags & ExpressionVariable::EVKeepInTarget))
332 DestroyAllocation(map, err);
338 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log) override
340 StreamString dump_stream;
344 const lldb::addr_t load_addr = process_address + m_offset;
346 dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n", load_addr, m_persistent_variable_sp->GetName().AsCString());
349 dump_stream.Printf("Pointer:\n");
351 DataBufferHeap data (m_size, 0);
353 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
357 dump_stream.Printf(" <could not be read>\n");
361 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
363 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
365 dump_stream.PutChar('\n');
370 dump_stream.Printf("Target:\n");
372 lldb::addr_t target_address;
374 map.ReadPointerFromMemory (&target_address, load_addr, err);
378 dump_stream.Printf(" <could not be read>\n");
382 DataBufferHeap data (m_persistent_variable_sp->GetByteSize(), 0);
384 map.ReadMemory(data.GetBytes(), target_address, m_persistent_variable_sp->GetByteSize(), err);
388 dump_stream.Printf(" <could not be read>\n");
392 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
394 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, target_address);
396 dump_stream.PutChar('\n');
401 log->PutCString(dump_stream.GetData());
404 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override
409 lldb::ExpressionVariableSP m_persistent_variable_sp;
410 Materializer::PersistentVariableDelegate *m_delegate;
414 Materializer::AddPersistentVariable (lldb::ExpressionVariableSP &persistent_variable_sp,
415 PersistentVariableDelegate *delegate,
418 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
419 iter->reset (new EntityPersistentVariable (persistent_variable_sp, delegate));
420 uint32_t ret = AddStructMember(**iter);
421 (*iter)->SetOffset(ret);
425 class EntityVariable : public Materializer::Entity
428 EntityVariable (lldb::VariableSP &variable_sp) :
430 m_variable_sp(variable_sp),
431 m_is_reference(false),
432 m_temporary_allocation(LLDB_INVALID_ADDRESS),
433 m_temporary_allocation_size(0)
435 // Hard-coding to maximum size of a pointer since all variables are materialized by reference
438 m_is_reference = m_variable_sp->GetType()->GetForwardCompilerType ().IsReferenceType();
441 void Materialize(lldb::StackFrameSP &frame_sp,
443 lldb::addr_t process_address,
446 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
448 const lldb::addr_t load_addr = process_address + m_offset;
451 log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
453 m_variable_sp->GetName().AsCString());
456 ExecutionContextScope *scope = frame_sp.get();
459 scope = map.GetBestExecutionContextScope();
461 lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
465 err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
469 Error valobj_error = valobj_sp->GetError();
471 if (valobj_error.Fail())
473 err.SetErrorStringWithFormat("couldn't get the value of variable %s: %s", m_variable_sp->GetName().AsCString(), valobj_error.AsCString());
479 DataExtractor valobj_extractor;
481 valobj_sp->GetData(valobj_extractor, extract_error);
483 if (!extract_error.Success())
485 err.SetErrorStringWithFormat("couldn't read contents of reference variable %s: %s", m_variable_sp->GetName().AsCString(), extract_error.AsCString());
489 lldb::offset_t offset = 0;
490 lldb::addr_t reference_addr = valobj_extractor.GetAddress(&offset);
493 map.WritePointerToMemory(load_addr, reference_addr, write_error);
495 if (!write_error.Success())
497 err.SetErrorStringWithFormat("couldn't write the contents of reference variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
503 AddressType address_type = eAddressTypeInvalid;
504 const bool scalar_is_load_address = false;
505 lldb::addr_t addr_of_valobj = valobj_sp->GetAddressOf(scalar_is_load_address, &address_type);
506 if (addr_of_valobj != LLDB_INVALID_ADDRESS)
509 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error);
511 if (!write_error.Success())
513 err.SetErrorStringWithFormat("couldn't write the address of variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
521 valobj_sp->GetData(data, extract_error);
522 if (!extract_error.Success())
524 err.SetErrorStringWithFormat("couldn't get the value of %s: %s", m_variable_sp->GetName().AsCString(), extract_error.AsCString());
528 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
530 err.SetErrorStringWithFormat("trying to create a temporary region for %s but one exists", m_variable_sp->GetName().AsCString());
534 if (data.GetByteSize() < m_variable_sp->GetType()->GetByteSize())
536 if (data.GetByteSize() == 0 && m_variable_sp->LocationExpression().IsValid() == false)
538 err.SetErrorStringWithFormat("the variable '%s' has no location, it may have been optimized out", m_variable_sp->GetName().AsCString());
542 err.SetErrorStringWithFormat("size of variable %s (%" PRIu64 ") is larger than the ValueObject's size (%" PRIu64 ")",
543 m_variable_sp->GetName().AsCString(),
544 m_variable_sp->GetType()->GetByteSize(),
550 size_t bit_align = m_variable_sp->GetType()->GetLayoutCompilerType ().GetTypeBitAlign();
551 size_t byte_align = (bit_align + 7) / 8;
557 const bool zero_memory = false;
559 m_temporary_allocation = map.Malloc(data.GetByteSize(),
561 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
562 IRMemoryMap::eAllocationPolicyMirror,
566 m_temporary_allocation_size = data.GetByteSize();
568 m_original_data.reset(new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
570 if (!alloc_error.Success())
572 err.SetErrorStringWithFormat("couldn't allocate a temporary region for %s: %s", m_variable_sp->GetName().AsCString(), alloc_error.AsCString());
578 map.WriteMemory(m_temporary_allocation, data.GetDataStart(), data.GetByteSize(), write_error);
580 if (!write_error.Success())
582 err.SetErrorStringWithFormat("couldn't write to the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
586 Error pointer_write_error;
588 map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
590 if (!pointer_write_error.Success())
592 err.SetErrorStringWithFormat("couldn't write the address of the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), pointer_write_error.AsCString());
598 void Dematerialize(lldb::StackFrameSP &frame_sp,
600 lldb::addr_t process_address,
601 lldb::addr_t frame_top,
602 lldb::addr_t frame_bottom,
605 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
607 const lldb::addr_t load_addr = process_address + m_offset;
610 log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
612 m_variable_sp->GetName().AsCString());
615 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
617 ExecutionContextScope *scope = frame_sp.get();
620 scope = map.GetBestExecutionContextScope();
622 lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
626 err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
630 lldb_private::DataExtractor data;
634 map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(), extract_error);
636 if (!extract_error.Success())
638 err.SetErrorStringWithFormat("couldn't get the data for variable %s", m_variable_sp->GetName().AsCString());
642 bool actually_write = true;
646 if ((data.GetByteSize() == m_original_data->GetByteSize()) &&
647 !memcmp(m_original_data->GetBytes(), data.GetDataStart(), data.GetByteSize()))
649 actually_write = false;
657 valobj_sp->SetData(data, set_error);
659 if (!set_error.Success())
661 err.SetErrorStringWithFormat("couldn't write the new contents of %s back into the variable", m_variable_sp->GetName().AsCString());
668 map.Free(m_temporary_allocation, free_error);
670 if (!free_error.Success())
672 err.SetErrorStringWithFormat("couldn't free the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), free_error.AsCString());
676 m_original_data.reset();
677 m_temporary_allocation = LLDB_INVALID_ADDRESS;
678 m_temporary_allocation_size = 0;
682 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log) override
684 StreamString dump_stream;
686 const lldb::addr_t load_addr = process_address + m_offset;
687 dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr);
691 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
694 dump_stream.Printf("Pointer:\n");
696 DataBufferHeap data (m_size, 0);
698 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
702 dump_stream.Printf(" <could not be read>\n");
706 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
708 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
710 lldb::offset_t offset;
712 ptr = extractor.GetPointer(&offset);
714 dump_stream.PutChar('\n');
718 if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
720 dump_stream.Printf("Points to process memory:\n");
724 dump_stream.Printf("Temporary allocation:\n");
727 if (ptr == LLDB_INVALID_ADDRESS)
729 dump_stream.Printf(" <could not be be found>\n");
733 DataBufferHeap data (m_temporary_allocation_size, 0);
735 map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
739 dump_stream.Printf(" <could not be read>\n");
743 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
745 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
747 dump_stream.PutChar('\n');
751 log->PutCString(dump_stream.GetData());
754 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override
756 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
760 map.Free(m_temporary_allocation, free_error);
762 m_temporary_allocation = LLDB_INVALID_ADDRESS;
763 m_temporary_allocation_size = 0;
769 lldb::VariableSP m_variable_sp;
771 lldb::addr_t m_temporary_allocation;
772 size_t m_temporary_allocation_size;
773 lldb::DataBufferSP m_original_data;
777 Materializer::AddVariable (lldb::VariableSP &variable_sp, Error &err)
779 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
780 iter->reset (new EntityVariable (variable_sp));
781 uint32_t ret = AddStructMember(**iter);
782 (*iter)->SetOffset(ret);
786 class EntityResultVariable : public Materializer::Entity
789 EntityResultVariable (const CompilerType &type,
790 bool is_program_reference,
792 Materializer::PersistentVariableDelegate *delegate) :
795 m_is_program_reference(is_program_reference),
796 m_keep_in_memory(keep_in_memory),
797 m_temporary_allocation(LLDB_INVALID_ADDRESS),
798 m_temporary_allocation_size(0),
801 // Hard-coding to maximum size of a pointer since all results are materialized by reference
806 void Materialize(lldb::StackFrameSP &frame_sp,
808 lldb::addr_t process_address,
811 if (!m_is_program_reference)
813 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
815 err.SetErrorString("Trying to create a temporary region for the result but one exists");
819 const lldb::addr_t load_addr = process_address + m_offset;
821 size_t byte_size = m_type.GetByteSize(nullptr);
822 size_t bit_align = m_type.GetTypeBitAlign();
823 size_t byte_align = (bit_align + 7) / 8;
829 const bool zero_memory = true;
831 m_temporary_allocation = map.Malloc(byte_size,
833 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
834 IRMemoryMap::eAllocationPolicyMirror,
837 m_temporary_allocation_size = byte_size;
839 if (!alloc_error.Success())
841 err.SetErrorStringWithFormat("couldn't allocate a temporary region for the result: %s", alloc_error.AsCString());
845 Error pointer_write_error;
847 map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
849 if (!pointer_write_error.Success())
851 err.SetErrorStringWithFormat("couldn't write the address of the temporary region for the result: %s", pointer_write_error.AsCString());
856 void Dematerialize(lldb::StackFrameSP &frame_sp,
858 lldb::addr_t process_address,
859 lldb::addr_t frame_top,
860 lldb::addr_t frame_bottom,
865 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
869 err.SetErrorString("Couldn't dematerialize a result variable: invalid execution context scope");
873 lldb::addr_t address;
875 const lldb::addr_t load_addr = process_address + m_offset;
877 map.ReadPointerFromMemory (&address, load_addr, read_error);
879 if (!read_error.Success())
881 err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its address");
885 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
889 err.SetErrorString("Couldn't dematerialize a result variable: no target");
893 Error type_system_error;
894 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(&type_system_error, m_type.GetMinimumLanguage());
898 err.SetErrorStringWithFormat("Couldn't dematerialize a result variable: couldn't get the corresponding type system: %s", type_system_error.AsCString());
902 PersistentExpressionState *persistent_state = type_system->GetPersistentExpressionState();
904 if (!persistent_state)
906 err.SetErrorString("Couldn't dematerialize a result variable: corresponding type system doesn't handle persistent variables");
910 ConstString name = m_delegate ? m_delegate->GetName() : persistent_state->GetNextPersistentVariableName();
912 lldb::ExpressionVariableSP ret = persistent_state->CreatePersistentVariable(exe_scope,
916 map.GetAddressByteSize());
920 err.SetErrorStringWithFormat("couldn't dematerialize a result variable: failed to make persistent variable %s", name.AsCString());
924 lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
928 m_delegate->DidDematerialize(ret);
931 bool can_persist = (m_is_program_reference && process_sp && process_sp->CanJIT() && !(address >= frame_bottom && address < frame_top));
933 if (can_persist && m_keep_in_memory)
935 ret->m_live_sp = ValueObjectConstResult::Create(exe_scope,
940 map.GetAddressByteSize());
945 const size_t pvar_byte_size = ret->GetByteSize();
946 uint8_t *pvar_data = ret->GetValueBytes();
948 map.ReadMemory(pvar_data, address, pvar_byte_size, read_error);
950 if (!read_error.Success())
952 err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its memory");
956 if (!can_persist || !m_keep_in_memory)
958 ret->m_flags |= ExpressionVariable::EVNeedsAllocation;
960 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
963 map.Free(m_temporary_allocation, free_error);
968 ret->m_flags |= ExpressionVariable::EVIsLLDBAllocated;
971 m_temporary_allocation = LLDB_INVALID_ADDRESS;
972 m_temporary_allocation_size = 0;
975 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log) override
977 StreamString dump_stream;
979 const lldb::addr_t load_addr = process_address + m_offset;
981 dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr);
985 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
988 dump_stream.Printf("Pointer:\n");
990 DataBufferHeap data (m_size, 0);
992 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
996 dump_stream.Printf(" <could not be read>\n");
1000 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1002 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1004 lldb::offset_t offset;
1006 ptr = extractor.GetPointer(&offset);
1008 dump_stream.PutChar('\n');
1012 if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
1014 dump_stream.Printf("Points to process memory:\n");
1018 dump_stream.Printf("Temporary allocation:\n");
1021 if (ptr == LLDB_INVALID_ADDRESS)
1023 dump_stream.Printf(" <could not be be found>\n");
1027 DataBufferHeap data (m_temporary_allocation_size, 0);
1029 map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
1033 dump_stream.Printf(" <could not be read>\n");
1037 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1039 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1041 dump_stream.PutChar('\n');
1045 log->PutCString(dump_stream.GetData());
1048 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override
1050 if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS)
1054 map.Free(m_temporary_allocation, free_error);
1057 m_temporary_allocation = LLDB_INVALID_ADDRESS;
1058 m_temporary_allocation_size = 0;
1062 CompilerType m_type;
1063 bool m_is_program_reference;
1064 bool m_keep_in_memory;
1066 lldb::addr_t m_temporary_allocation;
1067 size_t m_temporary_allocation_size;
1068 Materializer::PersistentVariableDelegate *m_delegate;
1072 Materializer::AddResultVariable (const CompilerType &type,
1073 bool is_program_reference,
1074 bool keep_in_memory,
1075 PersistentVariableDelegate *delegate,
1078 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1079 iter->reset (new EntityResultVariable (type, is_program_reference, keep_in_memory, delegate));
1080 uint32_t ret = AddStructMember(**iter);
1081 (*iter)->SetOffset(ret);
1085 class EntitySymbol : public Materializer::Entity
1088 EntitySymbol (const Symbol &symbol) :
1092 // Hard-coding to maximum size of a symbol
1097 void Materialize(lldb::StackFrameSP &frame_sp,
1099 lldb::addr_t process_address,
1100 Error &err) override
1102 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1104 const lldb::addr_t load_addr = process_address + m_offset;
1108 log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64 ", m_symbol = %s]",
1109 (uint64_t)load_addr,
1110 m_symbol.GetName().AsCString());
1113 const Address sym_address = m_symbol.GetAddress();
1115 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
1117 lldb::TargetSP target_sp;
1120 target_sp = map.GetBestExecutionContextScope()->CalculateTarget();
1124 err.SetErrorStringWithFormat("couldn't resolve symbol %s because there is no target", m_symbol.GetName().AsCString());
1128 lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get());
1130 if (resolved_address == LLDB_INVALID_ADDRESS)
1131 resolved_address = sym_address.GetFileAddress();
1133 Error pointer_write_error;
1135 map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error);
1137 if (!pointer_write_error.Success())
1139 err.SetErrorStringWithFormat("couldn't write the address of symbol %s: %s", m_symbol.GetName().AsCString(), pointer_write_error.AsCString());
1144 void Dematerialize(lldb::StackFrameSP &frame_sp,
1146 lldb::addr_t process_address,
1147 lldb::addr_t frame_top,
1148 lldb::addr_t frame_bottom,
1149 Error &err) override
1151 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1153 const lldb::addr_t load_addr = process_address + m_offset;
1157 log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64 ", m_symbol = %s]",
1158 (uint64_t)load_addr,
1159 m_symbol.GetName().AsCString());
1162 // no work needs to be done
1165 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log) override
1167 StreamString dump_stream;
1171 const lldb::addr_t load_addr = process_address + m_offset;
1173 dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr, m_symbol.GetName().AsCString());
1176 dump_stream.Printf("Pointer:\n");
1178 DataBufferHeap data (m_size, 0);
1180 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1184 dump_stream.Printf(" <could not be read>\n");
1188 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1190 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1192 dump_stream.PutChar('\n');
1196 log->PutCString(dump_stream.GetData());
1199 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override
1208 Materializer::AddSymbol (const Symbol &symbol_sp, Error &err)
1210 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1211 iter->reset (new EntitySymbol (symbol_sp));
1212 uint32_t ret = AddStructMember(**iter);
1213 (*iter)->SetOffset(ret);
1217 class EntityRegister : public Materializer::Entity
1220 EntityRegister (const RegisterInfo ®ister_info) :
1222 m_register_info(register_info)
1224 // Hard-coding alignment conservatively
1225 m_size = m_register_info.byte_size;
1226 m_alignment = m_register_info.byte_size;
1229 void Materialize(lldb::StackFrameSP &frame_sp,
1231 lldb::addr_t process_address,
1232 Error &err) override
1234 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1236 const lldb::addr_t load_addr = process_address + m_offset;
1240 log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1241 (uint64_t)load_addr,
1242 m_register_info.name);
1245 RegisterValue reg_value;
1247 if (!frame_sp.get())
1249 err.SetErrorStringWithFormat("couldn't materialize register %s without a stack frame", m_register_info.name);
1253 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1255 if (!reg_context_sp->ReadRegister(&m_register_info, reg_value))
1257 err.SetErrorStringWithFormat("couldn't read the value of register %s", m_register_info.name);
1261 DataExtractor register_data;
1263 if (!reg_value.GetData(register_data))
1265 err.SetErrorStringWithFormat("couldn't get the data for register %s", m_register_info.name);
1269 if (register_data.GetByteSize() != m_register_info.byte_size)
1271 err.SetErrorStringWithFormat("data for register %s had size %llu but we expected %llu", m_register_info.name, (unsigned long long)register_data.GetByteSize(), (unsigned long long)m_register_info.byte_size);
1275 m_register_contents.reset(new DataBufferHeap(register_data.GetDataStart(), register_data.GetByteSize()));
1279 map.WriteMemory(load_addr, register_data.GetDataStart(), register_data.GetByteSize(), write_error);
1281 if (!write_error.Success())
1283 err.SetErrorStringWithFormat("couldn't write the contents of register %s: %s", m_register_info.name, write_error.AsCString());
1288 void Dematerialize(lldb::StackFrameSP &frame_sp,
1290 lldb::addr_t process_address,
1291 lldb::addr_t frame_top,
1292 lldb::addr_t frame_bottom,
1293 Error &err) override
1295 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1297 const lldb::addr_t load_addr = process_address + m_offset;
1301 log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1302 (uint64_t)load_addr,
1303 m_register_info.name);
1306 Error extract_error;
1308 DataExtractor register_data;
1310 if (!frame_sp.get())
1312 err.SetErrorStringWithFormat("couldn't dematerialize register %s without a stack frame", m_register_info.name);
1316 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1318 map.GetMemoryData(register_data, load_addr, m_register_info.byte_size, extract_error);
1320 if (!extract_error.Success())
1322 err.SetErrorStringWithFormat("couldn't get the data for register %s: %s", m_register_info.name, extract_error.AsCString());
1326 if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(), register_data.GetByteSize()))
1328 // No write required, and in particular we avoid errors if the register wasn't writable
1330 m_register_contents.reset();
1334 m_register_contents.reset();
1336 RegisterValue register_value (const_cast<uint8_t*>(register_data.GetDataStart()), register_data.GetByteSize(), register_data.GetByteOrder());
1338 if (!reg_context_sp->WriteRegister(&m_register_info, register_value))
1340 err.SetErrorStringWithFormat("couldn't write the value of register %s", m_register_info.name);
1345 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log) override
1347 StreamString dump_stream;
1351 const lldb::addr_t load_addr = process_address + m_offset;
1354 dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr, m_register_info.name);
1357 dump_stream.Printf("Value:\n");
1359 DataBufferHeap data (m_size, 0);
1361 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1365 dump_stream.Printf(" <could not be read>\n");
1369 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1371 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1373 dump_stream.PutChar('\n');
1377 log->PutCString(dump_stream.GetData());
1380 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override
1385 RegisterInfo m_register_info;
1386 lldb::DataBufferSP m_register_contents;
1390 Materializer::AddRegister (const RegisterInfo ®ister_info, Error &err)
1392 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1393 iter->reset (new EntityRegister (register_info));
1394 uint32_t ret = AddStructMember(**iter);
1395 (*iter)->SetOffset(ret);
1399 Materializer::Materializer () :
1400 m_dematerializer_wp(),
1401 m_current_offset(0),
1402 m_struct_alignment(8)
1406 Materializer::~Materializer ()
1408 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1410 if (dematerializer_sp)
1411 dematerializer_sp->Wipe();
1414 Materializer::DematerializerSP
1415 Materializer::Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &error)
1417 ExecutionContextScope *exe_scope = frame_sp.get();
1420 exe_scope = map.GetBestExecutionContextScope();
1422 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1424 if (dematerializer_sp)
1426 error.SetErrorToGenericError();
1427 error.SetErrorString("Couldn't materialize: already materialized");
1430 DematerializerSP ret(new Dematerializer(*this, frame_sp, map, process_address));
1434 error.SetErrorToGenericError();
1435 error.SetErrorString("Couldn't materialize: target doesn't exist");
1438 for (EntityUP &entity_up : m_entities)
1440 entity_up->Materialize(frame_sp, map, process_address, error);
1442 if (!error.Success())
1443 return DematerializerSP();
1446 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1448 log->Printf("Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64 ") materialized:",
1449 static_cast<void*>(frame_sp.get()), process_address);
1450 for (EntityUP &entity_up : m_entities)
1451 entity_up->DumpToLog(map, process_address, log);
1454 m_dematerializer_wp = ret;
1460 Materializer::Dematerializer::Dematerialize (Error &error,
1461 lldb::addr_t frame_bottom,
1462 lldb::addr_t frame_top)
1464 lldb::StackFrameSP frame_sp;
1466 lldb::ThreadSP thread_sp = m_thread_wp.lock();
1468 frame_sp = thread_sp->GetFrameWithStackID(m_stack_id);
1470 ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope();
1474 error.SetErrorToGenericError();
1475 error.SetErrorString("Couldn't dematerialize: invalid dematerializer");
1480 error.SetErrorToGenericError();
1481 error.SetErrorString("Couldn't dematerialize: target is gone");
1485 if (Log *log =lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1487 log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address = 0x%" PRIx64 ") about to dematerialize:",
1488 static_cast<void*>(frame_sp.get()), m_process_address);
1489 for (EntityUP &entity_up : m_materializer->m_entities)
1490 entity_up->DumpToLog(*m_map, m_process_address, log);
1493 for (EntityUP &entity_up : m_materializer->m_entities)
1495 entity_up->Dematerialize (frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
1497 if (!error.Success())
1506 Materializer::Dematerializer::Wipe ()
1511 for (EntityUP &entity_up : m_materializer->m_entities)
1513 entity_up->Wipe (*m_map, m_process_address);
1516 m_materializer = nullptr;
1518 m_process_address = LLDB_INVALID_ADDRESS;
1521 Materializer::PersistentVariableDelegate::~PersistentVariableDelegate() = default;