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/Expression/Materializer.h"
11 #include "lldb/Core/DumpDataExtractor.h"
12 #include "lldb/Core/ValueObjectConstResult.h"
13 #include "lldb/Core/ValueObjectVariable.h"
14 #include "lldb/Expression/ExpressionVariable.h"
15 #include "lldb/Symbol/ClangASTContext.h"
16 #include "lldb/Symbol/Symbol.h"
17 #include "lldb/Symbol/Type.h"
18 #include "lldb/Symbol/Variable.h"
19 #include "lldb/Target/ExecutionContext.h"
20 #include "lldb/Target/RegisterContext.h"
21 #include "lldb/Target/StackFrame.h"
22 #include "lldb/Target/Target.h"
23 #include "lldb/Target/Thread.h"
24 #include "lldb/Utility/Log.h"
25 #include "lldb/Utility/RegisterValue.h"
27 using namespace lldb_private;
29 uint32_t Materializer::AddStructMember(Entity &entity) {
30 uint32_t size = entity.GetSize();
31 uint32_t alignment = entity.GetAlignment();
35 if (m_current_offset == 0)
36 m_struct_alignment = alignment;
38 if (m_current_offset % alignment)
39 m_current_offset += (alignment - (m_current_offset % alignment));
41 ret = m_current_offset;
43 m_current_offset += size;
48 void Materializer::Entity::SetSizeAndAlignmentFromType(CompilerType &type) {
49 if (llvm::Optional<uint64_t> size = type.GetByteSize(nullptr))
52 uint32_t bit_alignment = type.GetTypeBitAlign();
54 if (bit_alignment % 8) {
56 bit_alignment &= ~((uint32_t)0x111u);
59 m_alignment = bit_alignment / 8;
62 class EntityPersistentVariable : public Materializer::Entity {
64 EntityPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp,
65 Materializer::PersistentVariableDelegate *delegate)
66 : Entity(), m_persistent_variable_sp(persistent_variable_sp),
67 m_delegate(delegate) {
68 // Hard-coding to maximum size of a pointer since persistent variables are
69 // materialized by reference
74 void MakeAllocation(IRMemoryMap &map, Status &err) {
75 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
77 // Allocate a spare memory area to store the persistent variable's
80 Status allocate_error;
81 const bool zero_memory = false;
83 lldb::addr_t mem = map.Malloc(
84 m_persistent_variable_sp->GetByteSize(), 8,
85 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
86 IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error);
88 if (!allocate_error.Success()) {
89 err.SetErrorStringWithFormat(
90 "couldn't allocate a memory area to store %s: %s",
91 m_persistent_variable_sp->GetName().GetCString(),
92 allocate_error.AsCString());
97 log->Printf("Allocated %s (0x%" PRIx64 ") successfully",
98 m_persistent_variable_sp->GetName().GetCString(), mem);
100 // Put the location of the spare memory into the live data of the
103 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create(
104 map.GetBestExecutionContextScope(),
105 m_persistent_variable_sp->GetCompilerType(),
106 m_persistent_variable_sp->GetName(), mem, eAddressTypeLoad,
107 map.GetAddressByteSize());
109 // Clear the flag if the variable will never be deallocated.
111 if (m_persistent_variable_sp->m_flags &
112 ExpressionVariable::EVKeepInTarget) {
114 map.Leak(mem, leak_error);
115 m_persistent_variable_sp->m_flags &=
116 ~ExpressionVariable::EVNeedsAllocation;
119 // Write the contents of the variable to the area.
123 map.WriteMemory(mem, m_persistent_variable_sp->GetValueBytes(),
124 m_persistent_variable_sp->GetByteSize(), write_error);
126 if (!write_error.Success()) {
127 err.SetErrorStringWithFormat(
128 "couldn't write %s to the target: %s",
129 m_persistent_variable_sp->GetName().AsCString(),
130 write_error.AsCString());
135 void DestroyAllocation(IRMemoryMap &map, Status &err) {
136 Status deallocate_error;
138 map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue()
143 m_persistent_variable_sp->m_live_sp.reset();
145 if (!deallocate_error.Success()) {
146 err.SetErrorStringWithFormat(
147 "couldn't deallocate memory for %s: %s",
148 m_persistent_variable_sp->GetName().GetCString(),
149 deallocate_error.AsCString());
153 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
154 lldb::addr_t process_address, Status &err) override {
155 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
157 const lldb::addr_t load_addr = process_address + m_offset;
160 log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64
161 ", m_name = %s, m_flags = 0x%hx]",
163 m_persistent_variable_sp->GetName().AsCString(),
164 m_persistent_variable_sp->m_flags);
167 if (m_persistent_variable_sp->m_flags &
168 ExpressionVariable::EVNeedsAllocation) {
169 MakeAllocation(map, err);
170 m_persistent_variable_sp->m_flags |=
171 ExpressionVariable::EVIsLLDBAllocated;
177 if ((m_persistent_variable_sp->m_flags &
178 ExpressionVariable::EVIsProgramReference &&
179 m_persistent_variable_sp->m_live_sp) ||
180 m_persistent_variable_sp->m_flags &
181 ExpressionVariable::EVIsLLDBAllocated) {
184 map.WriteScalarToMemory(
186 m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(),
187 map.GetAddressByteSize(), write_error);
189 if (!write_error.Success()) {
190 err.SetErrorStringWithFormat(
191 "couldn't write the location of %s to memory: %s",
192 m_persistent_variable_sp->GetName().AsCString(),
193 write_error.AsCString());
196 err.SetErrorStringWithFormat(
197 "no materialization happened for persistent variable %s",
198 m_persistent_variable_sp->GetName().AsCString());
203 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
204 lldb::addr_t process_address, lldb::addr_t frame_top,
205 lldb::addr_t frame_bottom, Status &err) override {
206 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
208 const lldb::addr_t load_addr = process_address + m_offset;
212 "EntityPersistentVariable::Dematerialize [address = 0x%" PRIx64
213 ", m_name = %s, m_flags = 0x%hx]",
214 (uint64_t)process_address + m_offset,
215 m_persistent_variable_sp->GetName().AsCString(),
216 m_persistent_variable_sp->m_flags);
220 m_delegate->DidDematerialize(m_persistent_variable_sp);
223 if ((m_persistent_variable_sp->m_flags &
224 ExpressionVariable::EVIsLLDBAllocated) ||
225 (m_persistent_variable_sp->m_flags &
226 ExpressionVariable::EVIsProgramReference)) {
227 if (m_persistent_variable_sp->m_flags &
228 ExpressionVariable::EVIsProgramReference &&
229 !m_persistent_variable_sp->m_live_sp) {
230 // If the reference comes from the program, then the
231 // ClangExpressionVariable's live variable data hasn't been set up yet.
234 lldb::addr_t location;
237 map.ReadPointerFromMemory(&location, load_addr, read_error);
239 if (!read_error.Success()) {
240 err.SetErrorStringWithFormat(
241 "couldn't read the address of program-allocated variable %s: %s",
242 m_persistent_variable_sp->GetName().GetCString(),
243 read_error.AsCString());
247 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create(
248 map.GetBestExecutionContextScope(),
249 m_persistent_variable_sp.get()->GetCompilerType(),
250 m_persistent_variable_sp->GetName(), location, eAddressTypeLoad,
251 m_persistent_variable_sp->GetByteSize());
253 if (frame_top != LLDB_INVALID_ADDRESS &&
254 frame_bottom != LLDB_INVALID_ADDRESS && location >= frame_bottom &&
255 location <= frame_top) {
256 // If the variable is resident in the stack frame created by the
257 // expression, then it cannot be relied upon to stay around. We
258 // treat it as needing reallocation.
259 m_persistent_variable_sp->m_flags |=
260 ExpressionVariable::EVIsLLDBAllocated;
261 m_persistent_variable_sp->m_flags |=
262 ExpressionVariable::EVNeedsAllocation;
263 m_persistent_variable_sp->m_flags |=
264 ExpressionVariable::EVNeedsFreezeDry;
265 m_persistent_variable_sp->m_flags &=
266 ~ExpressionVariable::EVIsProgramReference;
270 lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue()
274 if (!m_persistent_variable_sp->m_live_sp) {
275 err.SetErrorStringWithFormat(
276 "couldn't find the memory area used to store %s",
277 m_persistent_variable_sp->GetName().GetCString());
281 if (m_persistent_variable_sp->m_live_sp->GetValue()
282 .GetValueAddressType() != eAddressTypeLoad) {
283 err.SetErrorStringWithFormat(
284 "the address of the memory area for %s is in an incorrect format",
285 m_persistent_variable_sp->GetName().GetCString());
289 if (m_persistent_variable_sp->m_flags &
290 ExpressionVariable::EVNeedsFreezeDry ||
291 m_persistent_variable_sp->m_flags &
292 ExpressionVariable::EVKeepInTarget) {
295 "Dematerializing %s from 0x%" PRIx64 " (size = %llu)",
296 m_persistent_variable_sp->GetName().GetCString(), (uint64_t)mem,
297 (unsigned long long)m_persistent_variable_sp->GetByteSize());
299 // Read the contents of the spare memory area
301 m_persistent_variable_sp->ValueUpdated();
305 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(), mem,
306 m_persistent_variable_sp->GetByteSize(), read_error);
308 if (!read_error.Success()) {
309 err.SetErrorStringWithFormat(
310 "couldn't read the contents of %s from memory: %s",
311 m_persistent_variable_sp->GetName().GetCString(),
312 read_error.AsCString());
316 m_persistent_variable_sp->m_flags &=
317 ~ExpressionVariable::EVNeedsFreezeDry;
320 err.SetErrorStringWithFormat(
321 "no dematerialization happened for persistent variable %s",
322 m_persistent_variable_sp->GetName().AsCString());
326 lldb::ProcessSP process_sp =
327 map.GetBestExecutionContextScope()->CalculateProcess();
328 if (!process_sp || !process_sp->CanJIT()) {
329 // Allocations are not persistent so persistent variables cannot stay
332 m_persistent_variable_sp->m_flags |=
333 ExpressionVariable::EVNeedsAllocation;
335 DestroyAllocation(map, err);
338 } else if (m_persistent_variable_sp->m_flags &
339 ExpressionVariable::EVNeedsAllocation &&
340 !(m_persistent_variable_sp->m_flags &
341 ExpressionVariable::EVKeepInTarget)) {
342 DestroyAllocation(map, err);
348 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
350 StreamString dump_stream;
354 const lldb::addr_t load_addr = process_address + m_offset;
356 dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n",
358 m_persistent_variable_sp->GetName().AsCString());
361 dump_stream.Printf("Pointer:\n");
363 DataBufferHeap data(m_size, 0);
365 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
367 if (!err.Success()) {
368 dump_stream.Printf(" <could not be read>\n");
370 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
373 dump_stream.PutChar('\n');
378 dump_stream.Printf("Target:\n");
380 lldb::addr_t target_address;
382 map.ReadPointerFromMemory(&target_address, load_addr, err);
384 if (!err.Success()) {
385 dump_stream.Printf(" <could not be read>\n");
387 DataBufferHeap data(m_persistent_variable_sp->GetByteSize(), 0);
389 map.ReadMemory(data.GetBytes(), target_address,
390 m_persistent_variable_sp->GetByteSize(), err);
392 if (!err.Success()) {
393 dump_stream.Printf(" <could not be read>\n");
395 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
398 dump_stream.PutChar('\n');
403 log->PutString(dump_stream.GetString());
406 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
409 lldb::ExpressionVariableSP m_persistent_variable_sp;
410 Materializer::PersistentVariableDelegate *m_delegate;
413 uint32_t Materializer::AddPersistentVariable(
414 lldb::ExpressionVariableSP &persistent_variable_sp,
415 PersistentVariableDelegate *delegate, Status &err) {
416 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
417 iter->reset(new EntityPersistentVariable(persistent_variable_sp, delegate));
418 uint32_t ret = AddStructMember(**iter);
419 (*iter)->SetOffset(ret);
423 class EntityVariable : public Materializer::Entity {
425 EntityVariable(lldb::VariableSP &variable_sp)
426 : Entity(), m_variable_sp(variable_sp), m_is_reference(false),
427 m_temporary_allocation(LLDB_INVALID_ADDRESS),
428 m_temporary_allocation_size(0) {
429 // Hard-coding to maximum size of a pointer since all variables are
430 // materialized by reference
434 m_variable_sp->GetType()->GetForwardCompilerType().IsReferenceType();
437 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
438 lldb::addr_t process_address, Status &err) override {
439 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
441 const lldb::addr_t load_addr = process_address + m_offset;
443 log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64
444 ", m_variable_sp = %s]",
445 (uint64_t)load_addr, m_variable_sp->GetName().AsCString());
448 ExecutionContextScope *scope = frame_sp.get();
451 scope = map.GetBestExecutionContextScope();
453 lldb::ValueObjectSP valobj_sp =
454 ValueObjectVariable::Create(scope, m_variable_sp);
457 err.SetErrorStringWithFormat(
458 "couldn't get a value object for variable %s",
459 m_variable_sp->GetName().AsCString());
463 Status valobj_error = valobj_sp->GetError();
465 if (valobj_error.Fail()) {
466 err.SetErrorStringWithFormat("couldn't get the value of variable %s: %s",
467 m_variable_sp->GetName().AsCString(),
468 valobj_error.AsCString());
472 if (m_is_reference) {
473 DataExtractor valobj_extractor;
474 Status extract_error;
475 valobj_sp->GetData(valobj_extractor, extract_error);
477 if (!extract_error.Success()) {
478 err.SetErrorStringWithFormat(
479 "couldn't read contents of reference variable %s: %s",
480 m_variable_sp->GetName().AsCString(), extract_error.AsCString());
484 lldb::offset_t offset = 0;
485 lldb::addr_t reference_addr = valobj_extractor.GetAddress(&offset);
488 map.WritePointerToMemory(load_addr, reference_addr, write_error);
490 if (!write_error.Success()) {
491 err.SetErrorStringWithFormat("couldn't write the contents of reference "
492 "variable %s to memory: %s",
493 m_variable_sp->GetName().AsCString(),
494 write_error.AsCString());
498 AddressType address_type = eAddressTypeInvalid;
499 const bool scalar_is_load_address = false;
500 lldb::addr_t addr_of_valobj =
501 valobj_sp->GetAddressOf(scalar_is_load_address, &address_type);
502 if (addr_of_valobj != LLDB_INVALID_ADDRESS) {
504 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error);
506 if (!write_error.Success()) {
507 err.SetErrorStringWithFormat(
508 "couldn't write the address of variable %s to memory: %s",
509 m_variable_sp->GetName().AsCString(), write_error.AsCString());
514 Status extract_error;
515 valobj_sp->GetData(data, extract_error);
516 if (!extract_error.Success()) {
517 err.SetErrorStringWithFormat("couldn't get the value of %s: %s",
518 m_variable_sp->GetName().AsCString(),
519 extract_error.AsCString());
523 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
524 err.SetErrorStringWithFormat(
525 "trying to create a temporary region for %s but one exists",
526 m_variable_sp->GetName().AsCString());
530 if (data.GetByteSize() < m_variable_sp->GetType()->GetByteSize()) {
531 if (data.GetByteSize() == 0 &&
532 !m_variable_sp->LocationExpression().IsValid()) {
533 err.SetErrorStringWithFormat("the variable '%s' has no location, "
534 "it may have been optimized out",
535 m_variable_sp->GetName().AsCString());
537 err.SetErrorStringWithFormat(
538 "size of variable %s (%" PRIu64
539 ") is larger than the ValueObject's size (%" PRIu64 ")",
540 m_variable_sp->GetName().AsCString(),
541 m_variable_sp->GetType()->GetByteSize(), data.GetByteSize());
547 m_variable_sp->GetType()->GetLayoutCompilerType().GetTypeBitAlign();
548 size_t byte_align = (bit_align + 7) / 8;
554 const bool zero_memory = false;
556 m_temporary_allocation = map.Malloc(
557 data.GetByteSize(), byte_align,
558 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
559 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error);
561 m_temporary_allocation_size = data.GetByteSize();
563 m_original_data.reset(
564 new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
566 if (!alloc_error.Success()) {
567 err.SetErrorStringWithFormat(
568 "couldn't allocate a temporary region for %s: %s",
569 m_variable_sp->GetName().AsCString(), alloc_error.AsCString());
575 map.WriteMemory(m_temporary_allocation, data.GetDataStart(),
576 data.GetByteSize(), write_error);
578 if (!write_error.Success()) {
579 err.SetErrorStringWithFormat(
580 "couldn't write to the temporary region for %s: %s",
581 m_variable_sp->GetName().AsCString(), write_error.AsCString());
585 Status pointer_write_error;
587 map.WritePointerToMemory(load_addr, m_temporary_allocation,
588 pointer_write_error);
590 if (!pointer_write_error.Success()) {
591 err.SetErrorStringWithFormat(
592 "couldn't write the address of the temporary region for %s: %s",
593 m_variable_sp->GetName().AsCString(),
594 pointer_write_error.AsCString());
600 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
601 lldb::addr_t process_address, lldb::addr_t frame_top,
602 lldb::addr_t frame_bottom, Status &err) override {
603 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
605 const lldb::addr_t load_addr = process_address + m_offset;
607 log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64
608 ", m_variable_sp = %s]",
609 (uint64_t)load_addr, m_variable_sp->GetName().AsCString());
612 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
613 ExecutionContextScope *scope = frame_sp.get();
616 scope = map.GetBestExecutionContextScope();
618 lldb::ValueObjectSP valobj_sp =
619 ValueObjectVariable::Create(scope, m_variable_sp);
622 err.SetErrorStringWithFormat(
623 "couldn't get a value object for variable %s",
624 m_variable_sp->GetName().AsCString());
628 lldb_private::DataExtractor data;
630 Status extract_error;
632 map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(),
635 if (!extract_error.Success()) {
636 err.SetErrorStringWithFormat("couldn't get the data for variable %s",
637 m_variable_sp->GetName().AsCString());
641 bool actually_write = true;
643 if (m_original_data) {
644 if ((data.GetByteSize() == m_original_data->GetByteSize()) &&
645 !memcmp(m_original_data->GetBytes(), data.GetDataStart(),
646 data.GetByteSize())) {
647 actually_write = false;
653 if (actually_write) {
654 valobj_sp->SetData(data, set_error);
656 if (!set_error.Success()) {
657 err.SetErrorStringWithFormat(
658 "couldn't write the new contents of %s back into the variable",
659 m_variable_sp->GetName().AsCString());
666 map.Free(m_temporary_allocation, free_error);
668 if (!free_error.Success()) {
669 err.SetErrorStringWithFormat(
670 "couldn't free the temporary region for %s: %s",
671 m_variable_sp->GetName().AsCString(), free_error.AsCString());
675 m_original_data.reset();
676 m_temporary_allocation = LLDB_INVALID_ADDRESS;
677 m_temporary_allocation_size = 0;
681 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
683 StreamString dump_stream;
685 const lldb::addr_t load_addr = process_address + m_offset;
686 dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr);
690 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
693 dump_stream.Printf("Pointer:\n");
695 DataBufferHeap data(m_size, 0);
697 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
699 if (!err.Success()) {
700 dump_stream.Printf(" <could not be read>\n");
702 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
703 map.GetByteOrder(), map.GetAddressByteSize());
705 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
708 lldb::offset_t offset;
710 ptr = extractor.GetPointer(&offset);
712 dump_stream.PutChar('\n');
716 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) {
717 dump_stream.Printf("Points to process memory:\n");
719 dump_stream.Printf("Temporary allocation:\n");
722 if (ptr == LLDB_INVALID_ADDRESS) {
723 dump_stream.Printf(" <could not be be found>\n");
725 DataBufferHeap data(m_temporary_allocation_size, 0);
727 map.ReadMemory(data.GetBytes(), m_temporary_allocation,
728 m_temporary_allocation_size, err);
730 if (!err.Success()) {
731 dump_stream.Printf(" <could not be read>\n");
733 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
736 dump_stream.PutChar('\n');
740 log->PutString(dump_stream.GetString());
743 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {
744 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
747 map.Free(m_temporary_allocation, free_error);
749 m_temporary_allocation = LLDB_INVALID_ADDRESS;
750 m_temporary_allocation_size = 0;
755 lldb::VariableSP m_variable_sp;
757 lldb::addr_t m_temporary_allocation;
758 size_t m_temporary_allocation_size;
759 lldb::DataBufferSP m_original_data;
762 uint32_t Materializer::AddVariable(lldb::VariableSP &variable_sp, Status &err) {
763 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
764 iter->reset(new EntityVariable(variable_sp));
765 uint32_t ret = AddStructMember(**iter);
766 (*iter)->SetOffset(ret);
770 class EntityResultVariable : public Materializer::Entity {
772 EntityResultVariable(const CompilerType &type, bool is_program_reference,
774 Materializer::PersistentVariableDelegate *delegate)
775 : Entity(), m_type(type), m_is_program_reference(is_program_reference),
776 m_keep_in_memory(keep_in_memory),
777 m_temporary_allocation(LLDB_INVALID_ADDRESS),
778 m_temporary_allocation_size(0), m_delegate(delegate) {
779 // Hard-coding to maximum size of a pointer since all results are
780 // materialized by reference
785 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
786 lldb::addr_t process_address, Status &err) override {
787 if (!m_is_program_reference) {
788 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
789 err.SetErrorString("Trying to create a temporary region for the result "
794 const lldb::addr_t load_addr = process_address + m_offset;
796 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
798 llvm::Optional<uint64_t> byte_size = m_type.GetByteSize(exe_scope);
800 err.SetErrorString("can't get size of type");
803 size_t bit_align = m_type.GetTypeBitAlign();
804 size_t byte_align = (bit_align + 7) / 8;
810 const bool zero_memory = true;
812 m_temporary_allocation = map.Malloc(
813 *byte_size, byte_align,
814 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
815 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error);
816 m_temporary_allocation_size = *byte_size;
818 if (!alloc_error.Success()) {
819 err.SetErrorStringWithFormat(
820 "couldn't allocate a temporary region for the result: %s",
821 alloc_error.AsCString());
825 Status pointer_write_error;
827 map.WritePointerToMemory(load_addr, m_temporary_allocation,
828 pointer_write_error);
830 if (!pointer_write_error.Success()) {
831 err.SetErrorStringWithFormat("couldn't write the address of the "
832 "temporary region for the result: %s",
833 pointer_write_error.AsCString());
838 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
839 lldb::addr_t process_address, lldb::addr_t frame_top,
840 lldb::addr_t frame_bottom, Status &err) override {
843 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
846 err.SetErrorString("Couldn't dematerialize a result variable: invalid "
847 "execution context scope");
851 lldb::addr_t address;
853 const lldb::addr_t load_addr = process_address + m_offset;
855 map.ReadPointerFromMemory(&address, load_addr, read_error);
857 if (!read_error.Success()) {
858 err.SetErrorString("Couldn't dematerialize a result variable: couldn't "
863 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
866 err.SetErrorString("Couldn't dematerialize a result variable: no target");
870 Status type_system_error;
871 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(
872 &type_system_error, m_type.GetMinimumLanguage());
875 err.SetErrorStringWithFormat("Couldn't dematerialize a result variable: "
876 "couldn't get the corresponding type "
878 type_system_error.AsCString());
882 PersistentExpressionState *persistent_state =
883 type_system->GetPersistentExpressionState();
885 if (!persistent_state) {
886 err.SetErrorString("Couldn't dematerialize a result variable: "
887 "corresponding type system doesn't handle persistent "
894 ? m_delegate->GetName()
895 : persistent_state->GetNextPersistentVariableName(
896 *target_sp, persistent_state->GetPersistentVariablePrefix());
898 lldb::ExpressionVariableSP ret = persistent_state->CreatePersistentVariable(
899 exe_scope, name, m_type, map.GetByteOrder(), map.GetAddressByteSize());
902 err.SetErrorStringWithFormat("couldn't dematerialize a result variable: "
903 "failed to make persistent variable %s",
908 lldb::ProcessSP process_sp =
909 map.GetBestExecutionContextScope()->CalculateProcess();
912 m_delegate->DidDematerialize(ret);
916 (m_is_program_reference && process_sp && process_sp->CanJIT() &&
917 !(address >= frame_bottom && address < frame_top));
919 if (can_persist && m_keep_in_memory) {
920 ret->m_live_sp = ValueObjectConstResult::Create(exe_scope, m_type, name,
921 address, eAddressTypeLoad,
922 map.GetAddressByteSize());
927 const size_t pvar_byte_size = ret->GetByteSize();
928 uint8_t *pvar_data = ret->GetValueBytes();
930 map.ReadMemory(pvar_data, address, pvar_byte_size, read_error);
932 if (!read_error.Success()) {
934 "Couldn't dematerialize a result variable: couldn't read its memory");
938 if (!can_persist || !m_keep_in_memory) {
939 ret->m_flags |= ExpressionVariable::EVNeedsAllocation;
941 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
943 map.Free(m_temporary_allocation, free_error);
946 ret->m_flags |= ExpressionVariable::EVIsLLDBAllocated;
949 m_temporary_allocation = LLDB_INVALID_ADDRESS;
950 m_temporary_allocation_size = 0;
953 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
955 StreamString dump_stream;
957 const lldb::addr_t load_addr = process_address + m_offset;
959 dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr);
963 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
966 dump_stream.Printf("Pointer:\n");
968 DataBufferHeap data(m_size, 0);
970 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
972 if (!err.Success()) {
973 dump_stream.Printf(" <could not be read>\n");
975 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
976 map.GetByteOrder(), map.GetAddressByteSize());
978 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
981 lldb::offset_t offset;
983 ptr = extractor.GetPointer(&offset);
985 dump_stream.PutChar('\n');
989 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) {
990 dump_stream.Printf("Points to process memory:\n");
992 dump_stream.Printf("Temporary allocation:\n");
995 if (ptr == LLDB_INVALID_ADDRESS) {
996 dump_stream.Printf(" <could not be be found>\n");
998 DataBufferHeap data(m_temporary_allocation_size, 0);
1000 map.ReadMemory(data.GetBytes(), m_temporary_allocation,
1001 m_temporary_allocation_size, err);
1003 if (!err.Success()) {
1004 dump_stream.Printf(" <could not be read>\n");
1006 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1009 dump_stream.PutChar('\n');
1013 log->PutString(dump_stream.GetString());
1016 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {
1017 if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS) {
1020 map.Free(m_temporary_allocation, free_error);
1023 m_temporary_allocation = LLDB_INVALID_ADDRESS;
1024 m_temporary_allocation_size = 0;
1028 CompilerType m_type;
1029 bool m_is_program_reference;
1030 bool m_keep_in_memory;
1032 lldb::addr_t m_temporary_allocation;
1033 size_t m_temporary_allocation_size;
1034 Materializer::PersistentVariableDelegate *m_delegate;
1037 uint32_t Materializer::AddResultVariable(const CompilerType &type,
1038 bool is_program_reference,
1039 bool keep_in_memory,
1040 PersistentVariableDelegate *delegate,
1042 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1043 iter->reset(new EntityResultVariable(type, is_program_reference,
1044 keep_in_memory, delegate));
1045 uint32_t ret = AddStructMember(**iter);
1046 (*iter)->SetOffset(ret);
1050 class EntitySymbol : public Materializer::Entity {
1052 EntitySymbol(const Symbol &symbol) : Entity(), m_symbol(symbol) {
1053 // Hard-coding to maximum size of a symbol
1058 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1059 lldb::addr_t process_address, Status &err) override {
1060 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1062 const lldb::addr_t load_addr = process_address + m_offset;
1065 log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64
1067 (uint64_t)load_addr, m_symbol.GetName().AsCString());
1070 const Address sym_address = m_symbol.GetAddress();
1072 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
1074 lldb::TargetSP target_sp;
1077 target_sp = map.GetBestExecutionContextScope()->CalculateTarget();
1080 err.SetErrorStringWithFormat(
1081 "couldn't resolve symbol %s because there is no target",
1082 m_symbol.GetName().AsCString());
1086 lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get());
1088 if (resolved_address == LLDB_INVALID_ADDRESS)
1089 resolved_address = sym_address.GetFileAddress();
1091 Status pointer_write_error;
1093 map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error);
1095 if (!pointer_write_error.Success()) {
1096 err.SetErrorStringWithFormat(
1097 "couldn't write the address of symbol %s: %s",
1098 m_symbol.GetName().AsCString(), pointer_write_error.AsCString());
1103 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1104 lldb::addr_t process_address, lldb::addr_t frame_top,
1105 lldb::addr_t frame_bottom, Status &err) override {
1106 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1108 const lldb::addr_t load_addr = process_address + m_offset;
1111 log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64
1113 (uint64_t)load_addr, m_symbol.GetName().AsCString());
1116 // no work needs to be done
1119 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
1120 Log *log) override {
1121 StreamString dump_stream;
1125 const lldb::addr_t load_addr = process_address + m_offset;
1127 dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr,
1128 m_symbol.GetName().AsCString());
1131 dump_stream.Printf("Pointer:\n");
1133 DataBufferHeap data(m_size, 0);
1135 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1137 if (!err.Success()) {
1138 dump_stream.Printf(" <could not be read>\n");
1140 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1143 dump_stream.PutChar('\n');
1147 log->PutString(dump_stream.GetString());
1150 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
1156 uint32_t Materializer::AddSymbol(const Symbol &symbol_sp, Status &err) {
1157 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1158 iter->reset(new EntitySymbol(symbol_sp));
1159 uint32_t ret = AddStructMember(**iter);
1160 (*iter)->SetOffset(ret);
1164 class EntityRegister : public Materializer::Entity {
1166 EntityRegister(const RegisterInfo ®ister_info)
1167 : Entity(), m_register_info(register_info) {
1168 // Hard-coding alignment conservatively
1169 m_size = m_register_info.byte_size;
1170 m_alignment = m_register_info.byte_size;
1173 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1174 lldb::addr_t process_address, Status &err) override {
1175 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1177 const lldb::addr_t load_addr = process_address + m_offset;
1180 log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64
1181 ", m_register_info = %s]",
1182 (uint64_t)load_addr, m_register_info.name);
1185 RegisterValue reg_value;
1187 if (!frame_sp.get()) {
1188 err.SetErrorStringWithFormat(
1189 "couldn't materialize register %s without a stack frame",
1190 m_register_info.name);
1194 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1196 if (!reg_context_sp->ReadRegister(&m_register_info, reg_value)) {
1197 err.SetErrorStringWithFormat("couldn't read the value of register %s",
1198 m_register_info.name);
1202 DataExtractor register_data;
1204 if (!reg_value.GetData(register_data)) {
1205 err.SetErrorStringWithFormat("couldn't get the data for register %s",
1206 m_register_info.name);
1210 if (register_data.GetByteSize() != m_register_info.byte_size) {
1211 err.SetErrorStringWithFormat(
1212 "data for register %s had size %llu but we expected %llu",
1213 m_register_info.name, (unsigned long long)register_data.GetByteSize(),
1214 (unsigned long long)m_register_info.byte_size);
1218 m_register_contents.reset(new DataBufferHeap(register_data.GetDataStart(),
1219 register_data.GetByteSize()));
1223 map.WriteMemory(load_addr, register_data.GetDataStart(),
1224 register_data.GetByteSize(), write_error);
1226 if (!write_error.Success()) {
1227 err.SetErrorStringWithFormat(
1228 "couldn't write the contents of register %s: %s",
1229 m_register_info.name, write_error.AsCString());
1234 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1235 lldb::addr_t process_address, lldb::addr_t frame_top,
1236 lldb::addr_t frame_bottom, Status &err) override {
1237 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1239 const lldb::addr_t load_addr = process_address + m_offset;
1242 log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64
1243 ", m_register_info = %s]",
1244 (uint64_t)load_addr, m_register_info.name);
1247 Status extract_error;
1249 DataExtractor register_data;
1251 if (!frame_sp.get()) {
1252 err.SetErrorStringWithFormat(
1253 "couldn't dematerialize register %s without a stack frame",
1254 m_register_info.name);
1258 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1260 map.GetMemoryData(register_data, load_addr, m_register_info.byte_size,
1263 if (!extract_error.Success()) {
1264 err.SetErrorStringWithFormat("couldn't get the data for register %s: %s",
1265 m_register_info.name,
1266 extract_error.AsCString());
1270 if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(),
1271 register_data.GetByteSize())) {
1272 // No write required, and in particular we avoid errors if the register
1275 m_register_contents.reset();
1279 m_register_contents.reset();
1281 RegisterValue register_value(
1282 const_cast<uint8_t *>(register_data.GetDataStart()),
1283 register_data.GetByteSize(), register_data.GetByteOrder());
1285 if (!reg_context_sp->WriteRegister(&m_register_info, register_value)) {
1286 err.SetErrorStringWithFormat("couldn't write the value of register %s",
1287 m_register_info.name);
1292 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
1293 Log *log) override {
1294 StreamString dump_stream;
1298 const lldb::addr_t load_addr = process_address + m_offset;
1300 dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr,
1301 m_register_info.name);
1304 dump_stream.Printf("Value:\n");
1306 DataBufferHeap data(m_size, 0);
1308 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1310 if (!err.Success()) {
1311 dump_stream.Printf(" <could not be read>\n");
1313 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1316 dump_stream.PutChar('\n');
1320 log->PutString(dump_stream.GetString());
1323 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
1326 RegisterInfo m_register_info;
1327 lldb::DataBufferSP m_register_contents;
1330 uint32_t Materializer::AddRegister(const RegisterInfo ®ister_info,
1332 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1333 iter->reset(new EntityRegister(register_info));
1334 uint32_t ret = AddStructMember(**iter);
1335 (*iter)->SetOffset(ret);
1339 Materializer::Materializer()
1340 : m_dematerializer_wp(), m_current_offset(0), m_struct_alignment(8) {}
1342 Materializer::~Materializer() {
1343 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1345 if (dematerializer_sp)
1346 dematerializer_sp->Wipe();
1349 Materializer::DematerializerSP
1350 Materializer::Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1351 lldb::addr_t process_address, Status &error) {
1352 ExecutionContextScope *exe_scope = frame_sp.get();
1355 exe_scope = map.GetBestExecutionContextScope();
1357 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1359 if (dematerializer_sp) {
1360 error.SetErrorToGenericError();
1361 error.SetErrorString("Couldn't materialize: already materialized");
1364 DematerializerSP ret(
1365 new Dematerializer(*this, frame_sp, map, process_address));
1368 error.SetErrorToGenericError();
1369 error.SetErrorString("Couldn't materialize: target doesn't exist");
1372 for (EntityUP &entity_up : m_entities) {
1373 entity_up->Materialize(frame_sp, map, process_address, error);
1375 if (!error.Success())
1376 return DematerializerSP();
1380 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
1382 "Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64
1384 static_cast<void *>(frame_sp.get()), process_address);
1385 for (EntityUP &entity_up : m_entities)
1386 entity_up->DumpToLog(map, process_address, log);
1389 m_dematerializer_wp = ret;
1394 void Materializer::Dematerializer::Dematerialize(Status &error,
1395 lldb::addr_t frame_bottom,
1396 lldb::addr_t frame_top) {
1397 lldb::StackFrameSP frame_sp;
1399 lldb::ThreadSP thread_sp = m_thread_wp.lock();
1401 frame_sp = thread_sp->GetFrameWithStackID(m_stack_id);
1403 ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope();
1406 error.SetErrorToGenericError();
1407 error.SetErrorString("Couldn't dematerialize: invalid dematerializer");
1411 error.SetErrorToGenericError();
1412 error.SetErrorString("Couldn't dematerialize: target is gone");
1415 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
1416 log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address "
1417 "= 0x%" PRIx64 ") about to dematerialize:",
1418 static_cast<void *>(frame_sp.get()), m_process_address);
1419 for (EntityUP &entity_up : m_materializer->m_entities)
1420 entity_up->DumpToLog(*m_map, m_process_address, log);
1423 for (EntityUP &entity_up : m_materializer->m_entities) {
1424 entity_up->Dematerialize(frame_sp, *m_map, m_process_address, frame_top,
1425 frame_bottom, error);
1427 if (!error.Success())
1435 void Materializer::Dematerializer::Wipe() {
1439 for (EntityUP &entity_up : m_materializer->m_entities) {
1440 entity_up->Wipe(*m_map, m_process_address);
1443 m_materializer = nullptr;
1445 m_process_address = LLDB_INVALID_ADDRESS;
1448 Materializer::PersistentVariableDelegate::~PersistentVariableDelegate() =