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 //===----------------------------------------------------------------------===//
10 #include "lldb/Core/Log.h"
11 #include "lldb/Core/RegisterValue.h"
12 #include "lldb/Core/ValueObjectConstResult.h"
13 #include "lldb/Core/ValueObjectVariable.h"
14 #include "lldb/Expression/ClangExpressionVariable.h"
15 #include "lldb/Expression/Materializer.h"
16 #include "lldb/Symbol/ClangASTContext.h"
17 #include "lldb/Symbol/Symbol.h"
18 #include "lldb/Symbol/Type.h"
19 #include "lldb/Symbol/Variable.h"
20 #include "lldb/Target/ExecutionContext.h"
21 #include "lldb/Target/RegisterContext.h"
22 #include "lldb/Target/StackFrame.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
26 using namespace lldb_private;
29 Materializer::AddStructMember (Entity &entity)
31 uint32_t size = entity.GetSize();
32 uint32_t alignment = entity.GetAlignment();
36 if (m_current_offset == 0)
37 m_struct_alignment = alignment;
39 if (m_current_offset % alignment)
40 m_current_offset += (alignment - (m_current_offset % alignment));
42 ret = m_current_offset;
44 m_current_offset += size;
50 Materializer::Entity::SetSizeAndAlignmentFromType (ClangASTType &type)
52 m_size = type.GetByteSize();
54 uint32_t bit_alignment = type.GetTypeBitAlign();
56 if (bit_alignment % 8)
59 bit_alignment &= ~((uint32_t)0x111u);
62 m_alignment = bit_alignment / 8;
65 class EntityPersistentVariable : public Materializer::Entity
68 EntityPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp) :
70 m_persistent_variable_sp(persistent_variable_sp)
72 // Hard-coding to maximum size of a pointer since persistent variables are materialized by reference
77 void MakeAllocation (IRMemoryMap &map, Error &err)
79 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
81 // Allocate a spare memory area to store the persistent variable's contents.
85 lldb::addr_t mem = map.Malloc(m_persistent_variable_sp->GetByteSize(),
87 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
88 IRMemoryMap::eAllocationPolicyMirror,
91 if (!allocate_error.Success())
93 err.SetErrorStringWithFormat("couldn't allocate a memory area to store %s: %s", m_persistent_variable_sp->GetName().GetCString(), allocate_error.AsCString());
98 log->Printf("Allocated %s (0x%" PRIx64 ") sucessfully", m_persistent_variable_sp->GetName().GetCString(), mem);
100 // Put the location of the spare memory into the live data of the ValueObject.
102 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope(),
103 m_persistent_variable_sp->GetTypeFromUser(),
104 m_persistent_variable_sp->GetName(),
107 m_persistent_variable_sp->GetByteSize());
109 // Clear the flag if the variable will never be deallocated.
111 if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget)
114 map.Leak(mem, leak_error);
115 m_persistent_variable_sp->m_flags &= ~ClangExpressionVariable::EVNeedsAllocation;
118 // Write the contents of the variable to the area.
122 map.WriteMemory (mem,
123 m_persistent_variable_sp->GetValueBytes(),
124 m_persistent_variable_sp->GetByteSize(),
127 if (!write_error.Success())
129 err.SetErrorStringWithFormat ("couldn't write %s to the target: %s", m_persistent_variable_sp->GetName().AsCString(),
130 write_error.AsCString());
135 void DestroyAllocation (IRMemoryMap &map, Error &err)
137 Error deallocate_error;
139 map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong(), deallocate_error);
141 m_persistent_variable_sp->m_live_sp.reset();
143 if (!deallocate_error.Success())
145 err.SetErrorStringWithFormat ("couldn't deallocate memory for %s: %s", m_persistent_variable_sp->GetName().GetCString(), deallocate_error.AsCString());
149 void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
151 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
153 const lldb::addr_t load_addr = process_address + m_offset;
157 log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64 ", m_name = %s, m_flags = 0x%hx]",
159 m_persistent_variable_sp->GetName().AsCString(),
160 m_persistent_variable_sp->m_flags);
163 if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsAllocation)
165 MakeAllocation(map, err);
166 m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
172 if ((m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsProgramReference && m_persistent_variable_sp->m_live_sp) ||
173 m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsLLDBAllocated)
177 map.WriteScalarToMemory(load_addr,
178 m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(),
179 map.GetAddressByteSize(),
182 if (!write_error.Success())
184 err.SetErrorStringWithFormat("couldn't write the location of %s to memory: %s", m_persistent_variable_sp->GetName().AsCString(), write_error.AsCString());
189 err.SetErrorStringWithFormat("no materialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
194 void Dematerialize (lldb::StackFrameSP &frame_sp,
196 lldb::addr_t process_address,
197 lldb::addr_t frame_top,
198 lldb::addr_t frame_bottom,
201 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
203 const lldb::addr_t load_addr = process_address + m_offset;
207 log->Printf("EntityPersistentVariable::Dematerialize [address = 0x%" PRIx64 ", m_name = %s, m_flags = 0x%hx]",
208 (uint64_t)process_address + m_offset,
209 m_persistent_variable_sp->GetName().AsCString(),
210 m_persistent_variable_sp->m_flags);
213 if ((m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsLLDBAllocated) ||
214 (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsProgramReference))
216 if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsProgramReference &&
217 !m_persistent_variable_sp->m_live_sp)
219 // If the reference comes from the program, then the ClangExpressionVariable's
220 // live variable data hasn't been set up yet. Do this now.
222 lldb::addr_t location;
225 map.ReadPointerFromMemory(&location, load_addr, read_error);
227 if (!read_error.Success())
229 err.SetErrorStringWithFormat("couldn't read the address of program-allocated variable %s: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
233 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope (),
234 m_persistent_variable_sp->GetTypeFromUser(),
235 m_persistent_variable_sp->GetName(),
238 m_persistent_variable_sp->GetByteSize());
240 if (frame_top != LLDB_INVALID_ADDRESS &&
241 frame_bottom != LLDB_INVALID_ADDRESS &&
242 location >= frame_bottom &&
243 location <= frame_top)
245 // If the variable is resident in the stack frame created by the expression,
246 // then it cannot be relied upon to stay around. We treat it as needing
248 m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
249 m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
250 m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsFreezeDry;
251 m_persistent_variable_sp->m_flags &= ~ClangExpressionVariable::EVIsProgramReference;
255 lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong();
257 if (!m_persistent_variable_sp->m_live_sp)
259 err.SetErrorStringWithFormat("couldn't find the memory area used to store %s", m_persistent_variable_sp->GetName().GetCString());
263 if (m_persistent_variable_sp->m_live_sp->GetValue().GetValueAddressType() != eAddressTypeLoad)
265 err.SetErrorStringWithFormat("the address of the memory area for %s is in an incorrect format", m_persistent_variable_sp->GetName().GetCString());
269 if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsFreezeDry ||
270 m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget)
273 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());
275 // Read the contents of the spare memory area
277 m_persistent_variable_sp->ValueUpdated ();
281 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(),
283 m_persistent_variable_sp->GetByteSize(),
286 if (!read_error.Success())
288 err.SetErrorStringWithFormat ("couldn't read the contents of %s from memory: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
292 m_persistent_variable_sp->m_flags &= ~ClangExpressionVariable::EVNeedsFreezeDry;
297 err.SetErrorStringWithFormat("no dematerialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
301 lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
303 !process_sp->CanJIT())
305 // Allocations are not persistent so persistent variables cannot stay materialized.
307 m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
309 DestroyAllocation(map, err);
313 else if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsAllocation &&
314 !(m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget))
316 DestroyAllocation(map, err);
322 void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
324 StreamString dump_stream;
328 const lldb::addr_t load_addr = process_address + m_offset;
330 dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n", load_addr, m_persistent_variable_sp->GetName().AsCString());
333 dump_stream.Printf("Pointer:\n");
335 DataBufferHeap data (m_size, 0);
337 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
341 dump_stream.Printf(" <could not be read>\n");
345 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
347 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
349 dump_stream.PutChar('\n');
354 dump_stream.Printf("Target:\n");
356 lldb::addr_t target_address;
358 map.ReadPointerFromMemory (&target_address, load_addr, err);
362 dump_stream.Printf(" <could not be read>\n");
366 DataBufferHeap data (m_persistent_variable_sp->GetByteSize(), 0);
368 map.ReadMemory(data.GetBytes(), target_address, m_persistent_variable_sp->GetByteSize(), err);
372 dump_stream.Printf(" <could not be read>\n");
376 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
378 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, target_address);
380 dump_stream.PutChar('\n');
385 log->PutCString(dump_stream.GetData());
388 void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
392 lldb::ClangExpressionVariableSP m_persistent_variable_sp;
396 Materializer::AddPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp, Error &err)
398 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
399 iter->reset (new EntityPersistentVariable (persistent_variable_sp));
400 uint32_t ret = AddStructMember(**iter);
401 (*iter)->SetOffset(ret);
405 class EntityVariable : public Materializer::Entity
408 EntityVariable (lldb::VariableSP &variable_sp) :
410 m_variable_sp(variable_sp),
411 m_is_reference(false),
412 m_temporary_allocation(LLDB_INVALID_ADDRESS),
413 m_temporary_allocation_size(0)
415 // Hard-coding to maximum size of a pointer since all variables are materialized by reference
418 m_is_reference = m_variable_sp->GetType()->GetClangForwardType().IsReferenceType();
421 void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
423 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
425 const lldb::addr_t load_addr = process_address + m_offset;
428 log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
430 m_variable_sp->GetName().AsCString());
433 ExecutionContextScope *scope = frame_sp.get();
436 scope = map.GetBestExecutionContextScope();
438 lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
442 err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
448 DataExtractor valobj_extractor;
449 valobj_sp->GetData(valobj_extractor);
450 lldb::offset_t offset = 0;
451 lldb::addr_t reference_addr = valobj_extractor.GetAddress(&offset);
454 map.WritePointerToMemory(load_addr, reference_addr, write_error);
456 if (!write_error.Success())
458 err.SetErrorStringWithFormat("couldn't write the contents of reference variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
464 lldb::addr_t addr_of_valobj = valobj_sp->GetAddressOf();
465 if (addr_of_valobj != LLDB_INVALID_ADDRESS)
468 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error);
470 if (!write_error.Success())
472 err.SetErrorStringWithFormat("couldn't write the address of variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
479 valobj_sp->GetData(data);
481 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
483 err.SetErrorStringWithFormat("trying to create a temporary region for %s but one exists", m_variable_sp->GetName().AsCString());
487 if (data.GetByteSize() != m_variable_sp->GetType()->GetByteSize())
489 if (data.GetByteSize() == 0 && m_variable_sp->LocationExpression().IsValid() == false)
491 err.SetErrorStringWithFormat("the variable '%s' has no location, it may have been optimized out", m_variable_sp->GetName().AsCString());
495 err.SetErrorStringWithFormat("size of variable %s (%" PRIu64 ") disagrees with the ValueObject's size (%" PRIu64 ")",
496 m_variable_sp->GetName().AsCString(),
497 m_variable_sp->GetType()->GetByteSize(),
503 size_t bit_align = m_variable_sp->GetType()->GetClangLayoutType().GetTypeBitAlign();
504 size_t byte_align = (bit_align + 7) / 8;
508 m_temporary_allocation = map.Malloc(data.GetByteSize(), byte_align, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, alloc_error);
509 m_temporary_allocation_size = data.GetByteSize();
511 if (!alloc_error.Success())
513 err.SetErrorStringWithFormat("couldn't allocate a temporary region for %s: %s", m_variable_sp->GetName().AsCString(), alloc_error.AsCString());
519 map.WriteMemory(m_temporary_allocation, data.GetDataStart(), data.GetByteSize(), write_error);
521 if (!write_error.Success())
523 err.SetErrorStringWithFormat("couldn't write to the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
527 Error pointer_write_error;
529 map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
531 if (!pointer_write_error.Success())
533 err.SetErrorStringWithFormat("couldn't write the address of the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), pointer_write_error.AsCString());
539 void Dematerialize (lldb::StackFrameSP &frame_sp,
541 lldb::addr_t process_address,
542 lldb::addr_t frame_top,
543 lldb::addr_t frame_bottom,
546 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
548 const lldb::addr_t load_addr = process_address + m_offset;
551 log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
553 m_variable_sp->GetName().AsCString());
556 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
558 ExecutionContextScope *scope = frame_sp.get();
561 scope = map.GetBestExecutionContextScope();
563 lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
567 err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
571 lldb_private::DataExtractor data;
575 map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(), extract_error);
577 if (!extract_error.Success())
579 err.SetErrorStringWithFormat("couldn't get the data for variable %s", m_variable_sp->GetName().AsCString());
585 valobj_sp->SetData(data, set_error);
587 if (!set_error.Success())
589 err.SetErrorStringWithFormat("couldn't write the new contents of %s back into the variable", m_variable_sp->GetName().AsCString());
595 map.Free(m_temporary_allocation, free_error);
597 if (!free_error.Success())
599 err.SetErrorStringWithFormat("couldn't free the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), free_error.AsCString());
603 m_temporary_allocation = LLDB_INVALID_ADDRESS;
604 m_temporary_allocation_size = 0;
608 void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
610 StreamString dump_stream;
612 const lldb::addr_t load_addr = process_address + m_offset;
613 dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr);
617 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
620 dump_stream.Printf("Pointer:\n");
622 DataBufferHeap data (m_size, 0);
624 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
628 dump_stream.Printf(" <could not be read>\n");
632 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
634 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
636 lldb::offset_t offset;
638 ptr = extractor.GetPointer(&offset);
640 dump_stream.PutChar('\n');
644 if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
646 dump_stream.Printf("Points to process memory:\n");
650 dump_stream.Printf("Temporary allocation:\n");
653 if (ptr == LLDB_INVALID_ADDRESS)
655 dump_stream.Printf(" <could not be be found>\n");
659 DataBufferHeap data (m_temporary_allocation_size, 0);
661 map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
665 dump_stream.Printf(" <could not be read>\n");
669 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
671 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
673 dump_stream.PutChar('\n');
677 log->PutCString(dump_stream.GetData());
680 void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
682 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
686 map.Free(m_temporary_allocation, free_error);
688 m_temporary_allocation = LLDB_INVALID_ADDRESS;
689 m_temporary_allocation_size = 0;
694 lldb::VariableSP m_variable_sp;
696 lldb::addr_t m_temporary_allocation;
697 size_t m_temporary_allocation_size;
701 Materializer::AddVariable (lldb::VariableSP &variable_sp, Error &err)
703 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
704 iter->reset (new EntityVariable (variable_sp));
705 uint32_t ret = AddStructMember(**iter);
706 (*iter)->SetOffset(ret);
710 class EntityResultVariable : public Materializer::Entity
713 EntityResultVariable (const TypeFromUser &type, bool is_program_reference, bool keep_in_memory) :
716 m_is_program_reference(is_program_reference),
717 m_keep_in_memory(keep_in_memory),
718 m_temporary_allocation(LLDB_INVALID_ADDRESS),
719 m_temporary_allocation_size(0)
721 // Hard-coding to maximum size of a pointer since all results are materialized by reference
726 void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
728 if (!m_is_program_reference)
730 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
732 err.SetErrorString("Trying to create a temporary region for the result but one exists");
736 const lldb::addr_t load_addr = process_address + m_offset;
738 size_t byte_size = m_type.GetByteSize();
739 size_t bit_align = m_type.GetTypeBitAlign();
740 size_t byte_align = (bit_align + 7) / 8;
744 m_temporary_allocation = map.Malloc(byte_size, byte_align, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, alloc_error);
745 m_temporary_allocation_size = byte_size;
747 if (!alloc_error.Success())
749 err.SetErrorStringWithFormat("couldn't allocate a temporary region for the result: %s", alloc_error.AsCString());
753 Error pointer_write_error;
755 map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
757 if (!pointer_write_error.Success())
759 err.SetErrorStringWithFormat("couldn't write the address of the temporary region for the result: %s", pointer_write_error.AsCString());
764 void Dematerialize (lldb::StackFrameSP &frame_sp,
766 lldb::addr_t process_address,
767 lldb::addr_t frame_top,
768 lldb::addr_t frame_bottom,
771 err.SetErrorString("Tried to detmaterialize a result variable with the normal Dematerialize method");
774 void Dematerialize (lldb::ClangExpressionVariableSP &result_variable_sp,
775 lldb::StackFrameSP &frame_sp,
777 lldb::addr_t process_address,
778 lldb::addr_t frame_top,
779 lldb::addr_t frame_bottom,
784 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
788 err.SetErrorString("Couldn't dematerialize a result variable: invalid execution context scope");
792 lldb::addr_t address;
794 const lldb::addr_t load_addr = process_address + m_offset;
796 map.ReadPointerFromMemory (&address, load_addr, read_error);
798 if (!read_error.Success())
800 err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its address");
804 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
808 err.SetErrorString("Couldn't dematerialize a result variable: no target");
812 ConstString name = target_sp->GetPersistentVariables().GetNextPersistentVariableName();
814 lldb::ClangExpressionVariableSP ret;
816 ret = target_sp->GetPersistentVariables().CreateVariable(exe_scope,
820 map.GetAddressByteSize());
824 err.SetErrorStringWithFormat("couldn't dematerialize a result variable: failed to make persistent variable %s", name.AsCString());
828 lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
830 bool can_persist = (m_is_program_reference && process_sp && process_sp->CanJIT() && !(address >= frame_bottom && address < frame_top));
832 if (can_persist && m_keep_in_memory)
834 ret->m_live_sp = ValueObjectConstResult::Create(exe_scope,
839 map.GetAddressByteSize());
844 const size_t pvar_byte_size = ret->GetByteSize();
845 uint8_t *pvar_data = ret->GetValueBytes();
847 map.ReadMemory(pvar_data, address, pvar_byte_size, read_error);
849 if (!read_error.Success())
851 err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its memory");
855 result_variable_sp = ret;
857 if (!can_persist || !m_keep_in_memory)
859 ret->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
861 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
864 map.Free(m_temporary_allocation, free_error);
869 ret->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
872 m_temporary_allocation = LLDB_INVALID_ADDRESS;
873 m_temporary_allocation_size = 0;
876 void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
878 StreamString dump_stream;
880 const lldb::addr_t load_addr = process_address + m_offset;
882 dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr);
886 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
889 dump_stream.Printf("Pointer:\n");
891 DataBufferHeap data (m_size, 0);
893 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
897 dump_stream.Printf(" <could not be read>\n");
901 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
903 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
905 lldb::offset_t offset;
907 ptr = extractor.GetPointer(&offset);
909 dump_stream.PutChar('\n');
913 if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
915 dump_stream.Printf("Points to process memory:\n");
919 dump_stream.Printf("Temporary allocation:\n");
922 if (ptr == LLDB_INVALID_ADDRESS)
924 dump_stream.Printf(" <could not be be found>\n");
928 DataBufferHeap data (m_temporary_allocation_size, 0);
930 map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
934 dump_stream.Printf(" <could not be read>\n");
938 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
940 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
942 dump_stream.PutChar('\n');
946 log->PutCString(dump_stream.GetData());
949 void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
951 if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS)
955 map.Free(m_temporary_allocation, free_error);
958 m_temporary_allocation = LLDB_INVALID_ADDRESS;
959 m_temporary_allocation_size = 0;
963 bool m_is_program_reference;
964 bool m_keep_in_memory;
966 lldb::addr_t m_temporary_allocation;
967 size_t m_temporary_allocation_size;
971 Materializer::AddResultVariable (const TypeFromUser &type, bool is_program_reference, bool keep_in_memory, Error &err)
973 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
974 iter->reset (new EntityResultVariable (type, is_program_reference, keep_in_memory));
975 uint32_t ret = AddStructMember(**iter);
976 (*iter)->SetOffset(ret);
977 m_result_entity = iter->get();
981 class EntitySymbol : public Materializer::Entity
984 EntitySymbol (const Symbol &symbol) :
988 // Hard-coding to maximum size of a symbol
993 void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
995 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
997 const lldb::addr_t load_addr = process_address + m_offset;
1001 log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64 ", m_symbol = %s]",
1002 (uint64_t)load_addr,
1003 m_symbol.GetName().AsCString());
1006 Address &sym_address = m_symbol.GetAddress();
1008 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
1010 lldb::TargetSP target_sp;
1013 target_sp = map.GetBestExecutionContextScope()->CalculateTarget();
1017 err.SetErrorStringWithFormat("couldn't resolve symbol %s because there is no target", m_symbol.GetName().AsCString());
1021 lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get());
1023 if (resolved_address == LLDB_INVALID_ADDRESS)
1024 resolved_address = sym_address.GetFileAddress();
1026 Error pointer_write_error;
1028 map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error);
1030 if (!pointer_write_error.Success())
1032 err.SetErrorStringWithFormat("couldn't write the address of symbol %s: %s", m_symbol.GetName().AsCString(), pointer_write_error.AsCString());
1037 void Dematerialize (lldb::StackFrameSP &frame_sp,
1039 lldb::addr_t process_address,
1040 lldb::addr_t frame_top,
1041 lldb::addr_t frame_bottom,
1044 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1046 const lldb::addr_t load_addr = process_address + m_offset;
1050 log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64 ", m_symbol = %s]",
1051 (uint64_t)load_addr,
1052 m_symbol.GetName().AsCString());
1055 // no work needs to be done
1058 void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
1060 StreamString dump_stream;
1064 const lldb::addr_t load_addr = process_address + m_offset;
1066 dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr, m_symbol.GetName().AsCString());
1069 dump_stream.Printf("Pointer:\n");
1071 DataBufferHeap data (m_size, 0);
1073 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1077 dump_stream.Printf(" <could not be read>\n");
1081 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1083 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1085 dump_stream.PutChar('\n');
1089 log->PutCString(dump_stream.GetData());
1092 void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
1100 Materializer::AddSymbol (const Symbol &symbol_sp, Error &err)
1102 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1103 iter->reset (new EntitySymbol (symbol_sp));
1104 uint32_t ret = AddStructMember(**iter);
1105 (*iter)->SetOffset(ret);
1109 class EntityRegister : public Materializer::Entity
1112 EntityRegister (const RegisterInfo ®ister_info) :
1114 m_register_info(register_info)
1116 // Hard-coding alignment conservatively
1117 m_size = m_register_info.byte_size;
1118 m_alignment = m_register_info.byte_size;
1121 void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
1123 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1125 const lldb::addr_t load_addr = process_address + m_offset;
1129 log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1130 (uint64_t)load_addr,
1131 m_register_info.name);
1134 RegisterValue reg_value;
1136 if (!frame_sp.get())
1138 err.SetErrorStringWithFormat("couldn't materialize register %s without a stack frame", m_register_info.name);
1142 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1144 if (!reg_context_sp->ReadRegister(&m_register_info, reg_value))
1146 err.SetErrorStringWithFormat("couldn't read the value of register %s", m_register_info.name);
1150 DataExtractor register_data;
1152 if (!reg_value.GetData(register_data))
1154 err.SetErrorStringWithFormat("couldn't get the data for register %s", m_register_info.name);
1158 if (register_data.GetByteSize() != m_register_info.byte_size)
1160 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);
1164 m_register_contents.reset(new DataBufferHeap(register_data.GetDataStart(), register_data.GetByteSize()));
1168 map.WriteMemory(load_addr, register_data.GetDataStart(), register_data.GetByteSize(), write_error);
1170 if (!write_error.Success())
1172 err.SetErrorStringWithFormat("couldn't write the contents of register %s: %s", m_register_info.name, write_error.AsCString());
1177 void Dematerialize (lldb::StackFrameSP &frame_sp,
1179 lldb::addr_t process_address,
1180 lldb::addr_t frame_top,
1181 lldb::addr_t frame_bottom,
1184 Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1186 const lldb::addr_t load_addr = process_address + m_offset;
1190 log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1191 (uint64_t)load_addr,
1192 m_register_info.name);
1195 Error extract_error;
1197 DataExtractor register_data;
1199 if (!frame_sp.get())
1201 err.SetErrorStringWithFormat("couldn't dematerialize register %s without a stack frame", m_register_info.name);
1205 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1207 map.GetMemoryData(register_data, load_addr, m_register_info.byte_size, extract_error);
1209 if (!extract_error.Success())
1211 err.SetErrorStringWithFormat("couldn't get the data for register %s: %s", m_register_info.name, extract_error.AsCString());
1215 if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(), register_data.GetByteSize()))
1217 // No write required, and in particular we avoid errors if the register wasn't writable
1219 m_register_contents.reset();
1223 m_register_contents.reset();
1225 RegisterValue register_value (const_cast<uint8_t*>(register_data.GetDataStart()), register_data.GetByteSize(), register_data.GetByteOrder());
1227 if (!reg_context_sp->WriteRegister(&m_register_info, register_value))
1229 err.SetErrorStringWithFormat("couldn't write the value of register %s", m_register_info.name);
1234 void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
1236 StreamString dump_stream;
1240 const lldb::addr_t load_addr = process_address + m_offset;
1243 dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr, m_register_info.name);
1246 dump_stream.Printf("Value:\n");
1248 DataBufferHeap data (m_size, 0);
1250 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1254 dump_stream.Printf(" <could not be read>\n");
1258 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1260 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1262 dump_stream.PutChar('\n');
1266 log->PutCString(dump_stream.GetData());
1269 void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
1273 RegisterInfo m_register_info;
1274 lldb::DataBufferSP m_register_contents;
1278 Materializer::AddRegister (const RegisterInfo ®ister_info, Error &err)
1280 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1281 iter->reset (new EntityRegister (register_info));
1282 uint32_t ret = AddStructMember(**iter);
1283 (*iter)->SetOffset(ret);
1287 Materializer::Materializer () :
1288 m_dematerializer_wp(),
1289 m_result_entity(NULL),
1290 m_current_offset(0),
1291 m_struct_alignment(8)
1295 Materializer::~Materializer ()
1297 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1299 if (dematerializer_sp)
1300 dematerializer_sp->Wipe();
1303 Materializer::DematerializerSP
1304 Materializer::Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &error)
1306 ExecutionContextScope *exe_scope = frame_sp.get();
1309 exe_scope = map.GetBestExecutionContextScope();
1311 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1313 if (dematerializer_sp)
1315 error.SetErrorToGenericError();
1316 error.SetErrorString("Couldn't materialize: already materialized");
1319 DematerializerSP ret(new Dematerializer(*this, frame_sp, map, process_address));
1323 error.SetErrorToGenericError();
1324 error.SetErrorString("Couldn't materialize: target doesn't exist");
1327 for (EntityUP &entity_up : m_entities)
1329 entity_up->Materialize(frame_sp, map, process_address, error);
1331 if (!error.Success())
1332 return DematerializerSP();
1335 if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1337 log->Printf("Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64 ") materialized:", frame_sp.get(), process_address);
1338 for (EntityUP &entity_up : m_entities)
1339 entity_up->DumpToLog(map, process_address, log);
1342 m_dematerializer_wp = ret;
1348 Materializer::Dematerializer::Dematerialize (Error &error, lldb::ClangExpressionVariableSP &result_sp, lldb::addr_t frame_bottom, lldb::addr_t frame_top)
1350 lldb::StackFrameSP frame_sp;
1352 lldb::ThreadSP thread_sp = m_thread_wp.lock();
1354 frame_sp = thread_sp->GetFrameWithStackID(m_stack_id);
1356 ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope();
1360 error.SetErrorToGenericError();
1361 error.SetErrorString("Couldn't dematerialize: invalid dematerializer");
1366 error.SetErrorToGenericError();
1367 error.SetErrorString("Couldn't dematerialize: target is gone");
1371 if (Log *log =lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1373 log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address = 0x%" PRIx64 ") about to dematerialize:", frame_sp.get(), m_process_address);
1374 for (EntityUP &entity_up : m_materializer->m_entities)
1375 entity_up->DumpToLog(*m_map, m_process_address, log);
1378 for (EntityUP &entity_up : m_materializer->m_entities)
1380 if (entity_up.get() == m_materializer->m_result_entity)
1382 static_cast<EntityResultVariable*>(m_materializer->m_result_entity)->Dematerialize (result_sp, frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
1386 entity_up->Dematerialize (frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
1389 if (!error.Success())
1398 Materializer::Dematerializer::Wipe ()
1403 for (EntityUP &entity_up : m_materializer->m_entities)
1405 entity_up->Wipe (*m_map, m_process_address);
1408 m_materializer = NULL;
1410 m_process_address = LLDB_INVALID_ADDRESS;