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/Expression/Materializer.h"
15 #include "lldb/Core/DumpDataExtractor.h"
16 #include "lldb/Core/RegisterValue.h"
17 #include "lldb/Core/ValueObjectConstResult.h"
18 #include "lldb/Core/ValueObjectVariable.h"
19 #include "lldb/Expression/ExpressionVariable.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"
29 #include "lldb/Utility/Log.h"
31 using namespace lldb_private;
33 uint32_t Materializer::AddStructMember(Entity &entity) {
34 uint32_t size = entity.GetSize();
35 uint32_t alignment = entity.GetAlignment();
39 if (m_current_offset == 0)
40 m_struct_alignment = alignment;
42 if (m_current_offset % alignment)
43 m_current_offset += (alignment - (m_current_offset % alignment));
45 ret = m_current_offset;
47 m_current_offset += size;
52 void Materializer::Entity::SetSizeAndAlignmentFromType(CompilerType &type) {
53 m_size = type.GetByteSize(nullptr);
55 uint32_t bit_alignment = type.GetTypeBitAlign();
57 if (bit_alignment % 8) {
59 bit_alignment &= ~((uint32_t)0x111u);
62 m_alignment = bit_alignment / 8;
65 class EntityPersistentVariable : public Materializer::Entity {
67 EntityPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp,
68 Materializer::PersistentVariableDelegate *delegate)
69 : Entity(), m_persistent_variable_sp(persistent_variable_sp),
70 m_delegate(delegate) {
71 // Hard-coding to maximum size of a pointer since persistent variables are
72 // materialized by reference
77 void MakeAllocation(IRMemoryMap &map, Status &err) {
78 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
80 // Allocate a spare memory area to store the persistent variable's
83 Status allocate_error;
84 const bool zero_memory = false;
86 lldb::addr_t mem = map.Malloc(
87 m_persistent_variable_sp->GetByteSize(), 8,
88 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
89 IRMemoryMap::eAllocationPolicyMirror, zero_memory, allocate_error);
91 if (!allocate_error.Success()) {
92 err.SetErrorStringWithFormat(
93 "couldn't allocate a memory area to store %s: %s",
94 m_persistent_variable_sp->GetName().GetCString(),
95 allocate_error.AsCString());
100 log->Printf("Allocated %s (0x%" PRIx64 ") successfully",
101 m_persistent_variable_sp->GetName().GetCString(), mem);
103 // Put the location of the spare memory into the live data of the
106 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create(
107 map.GetBestExecutionContextScope(),
108 m_persistent_variable_sp->GetCompilerType(),
109 m_persistent_variable_sp->GetName(), mem, eAddressTypeLoad,
110 map.GetAddressByteSize());
112 // Clear the flag if the variable will never be deallocated.
114 if (m_persistent_variable_sp->m_flags &
115 ExpressionVariable::EVKeepInTarget) {
117 map.Leak(mem, leak_error);
118 m_persistent_variable_sp->m_flags &=
119 ~ExpressionVariable::EVNeedsAllocation;
122 // Write the contents of the variable to the area.
126 map.WriteMemory(mem, m_persistent_variable_sp->GetValueBytes(),
127 m_persistent_variable_sp->GetByteSize(), write_error);
129 if (!write_error.Success()) {
130 err.SetErrorStringWithFormat(
131 "couldn't write %s to the target: %s",
132 m_persistent_variable_sp->GetName().AsCString(),
133 write_error.AsCString());
138 void DestroyAllocation(IRMemoryMap &map, Status &err) {
139 Status deallocate_error;
141 map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue()
146 m_persistent_variable_sp->m_live_sp.reset();
148 if (!deallocate_error.Success()) {
149 err.SetErrorStringWithFormat(
150 "couldn't deallocate memory for %s: %s",
151 m_persistent_variable_sp->GetName().GetCString(),
152 deallocate_error.AsCString());
156 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
157 lldb::addr_t process_address, Status &err) override {
158 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
160 const lldb::addr_t load_addr = process_address + m_offset;
163 log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64
164 ", m_name = %s, m_flags = 0x%hx]",
166 m_persistent_variable_sp->GetName().AsCString(),
167 m_persistent_variable_sp->m_flags);
170 if (m_persistent_variable_sp->m_flags &
171 ExpressionVariable::EVNeedsAllocation) {
172 MakeAllocation(map, err);
173 m_persistent_variable_sp->m_flags |=
174 ExpressionVariable::EVIsLLDBAllocated;
180 if ((m_persistent_variable_sp->m_flags &
181 ExpressionVariable::EVIsProgramReference &&
182 m_persistent_variable_sp->m_live_sp) ||
183 m_persistent_variable_sp->m_flags &
184 ExpressionVariable::EVIsLLDBAllocated) {
187 map.WriteScalarToMemory(
189 m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(),
190 map.GetAddressByteSize(), write_error);
192 if (!write_error.Success()) {
193 err.SetErrorStringWithFormat(
194 "couldn't write the location of %s to memory: %s",
195 m_persistent_variable_sp->GetName().AsCString(),
196 write_error.AsCString());
199 err.SetErrorStringWithFormat(
200 "no materialization happened for persistent variable %s",
201 m_persistent_variable_sp->GetName().AsCString());
206 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
207 lldb::addr_t process_address, lldb::addr_t frame_top,
208 lldb::addr_t frame_bottom, Status &err) override {
209 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
211 const lldb::addr_t load_addr = process_address + m_offset;
215 "EntityPersistentVariable::Dematerialize [address = 0x%" PRIx64
216 ", m_name = %s, m_flags = 0x%hx]",
217 (uint64_t)process_address + m_offset,
218 m_persistent_variable_sp->GetName().AsCString(),
219 m_persistent_variable_sp->m_flags);
223 m_delegate->DidDematerialize(m_persistent_variable_sp);
226 if ((m_persistent_variable_sp->m_flags &
227 ExpressionVariable::EVIsLLDBAllocated) ||
228 (m_persistent_variable_sp->m_flags &
229 ExpressionVariable::EVIsProgramReference)) {
230 if (m_persistent_variable_sp->m_flags &
231 ExpressionVariable::EVIsProgramReference &&
232 !m_persistent_variable_sp->m_live_sp) {
233 // If the reference comes from the program, then the
234 // ClangExpressionVariable's live variable data hasn't been set up yet.
237 lldb::addr_t location;
240 map.ReadPointerFromMemory(&location, load_addr, read_error);
242 if (!read_error.Success()) {
243 err.SetErrorStringWithFormat(
244 "couldn't read the address of program-allocated variable %s: %s",
245 m_persistent_variable_sp->GetName().GetCString(),
246 read_error.AsCString());
250 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create(
251 map.GetBestExecutionContextScope(),
252 m_persistent_variable_sp.get()->GetCompilerType(),
253 m_persistent_variable_sp->GetName(), location, eAddressTypeLoad,
254 m_persistent_variable_sp->GetByteSize());
256 if (frame_top != LLDB_INVALID_ADDRESS &&
257 frame_bottom != LLDB_INVALID_ADDRESS && location >= frame_bottom &&
258 location <= frame_top) {
259 // If the variable is resident in the stack frame created by the
260 // expression, then it cannot be relied upon to stay around. We
261 // treat it as needing reallocation.
262 m_persistent_variable_sp->m_flags |=
263 ExpressionVariable::EVIsLLDBAllocated;
264 m_persistent_variable_sp->m_flags |=
265 ExpressionVariable::EVNeedsAllocation;
266 m_persistent_variable_sp->m_flags |=
267 ExpressionVariable::EVNeedsFreezeDry;
268 m_persistent_variable_sp->m_flags &=
269 ~ExpressionVariable::EVIsProgramReference;
273 lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue()
277 if (!m_persistent_variable_sp->m_live_sp) {
278 err.SetErrorStringWithFormat(
279 "couldn't find the memory area used to store %s",
280 m_persistent_variable_sp->GetName().GetCString());
284 if (m_persistent_variable_sp->m_live_sp->GetValue()
285 .GetValueAddressType() != eAddressTypeLoad) {
286 err.SetErrorStringWithFormat(
287 "the address of the memory area for %s is in an incorrect format",
288 m_persistent_variable_sp->GetName().GetCString());
292 if (m_persistent_variable_sp->m_flags &
293 ExpressionVariable::EVNeedsFreezeDry ||
294 m_persistent_variable_sp->m_flags &
295 ExpressionVariable::EVKeepInTarget) {
298 "Dematerializing %s from 0x%" PRIx64 " (size = %llu)",
299 m_persistent_variable_sp->GetName().GetCString(), (uint64_t)mem,
300 (unsigned long long)m_persistent_variable_sp->GetByteSize());
302 // Read the contents of the spare memory area
304 m_persistent_variable_sp->ValueUpdated();
308 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(), mem,
309 m_persistent_variable_sp->GetByteSize(), read_error);
311 if (!read_error.Success()) {
312 err.SetErrorStringWithFormat(
313 "couldn't read the contents of %s from memory: %s",
314 m_persistent_variable_sp->GetName().GetCString(),
315 read_error.AsCString());
319 m_persistent_variable_sp->m_flags &=
320 ~ExpressionVariable::EVNeedsFreezeDry;
323 err.SetErrorStringWithFormat(
324 "no dematerialization happened for persistent variable %s",
325 m_persistent_variable_sp->GetName().AsCString());
329 lldb::ProcessSP process_sp =
330 map.GetBestExecutionContextScope()->CalculateProcess();
331 if (!process_sp || !process_sp->CanJIT()) {
332 // Allocations are not persistent so persistent variables cannot stay
335 m_persistent_variable_sp->m_flags |=
336 ExpressionVariable::EVNeedsAllocation;
338 DestroyAllocation(map, err);
341 } else if (m_persistent_variable_sp->m_flags &
342 ExpressionVariable::EVNeedsAllocation &&
343 !(m_persistent_variable_sp->m_flags &
344 ExpressionVariable::EVKeepInTarget)) {
345 DestroyAllocation(map, err);
351 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
353 StreamString dump_stream;
357 const lldb::addr_t load_addr = process_address + m_offset;
359 dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n",
361 m_persistent_variable_sp->GetName().AsCString());
364 dump_stream.Printf("Pointer:\n");
366 DataBufferHeap data(m_size, 0);
368 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
370 if (!err.Success()) {
371 dump_stream.Printf(" <could not be read>\n");
373 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
376 dump_stream.PutChar('\n');
381 dump_stream.Printf("Target:\n");
383 lldb::addr_t target_address;
385 map.ReadPointerFromMemory(&target_address, load_addr, err);
387 if (!err.Success()) {
388 dump_stream.Printf(" <could not be read>\n");
390 DataBufferHeap data(m_persistent_variable_sp->GetByteSize(), 0);
392 map.ReadMemory(data.GetBytes(), target_address,
393 m_persistent_variable_sp->GetByteSize(), err);
395 if (!err.Success()) {
396 dump_stream.Printf(" <could not be read>\n");
398 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
401 dump_stream.PutChar('\n');
406 log->PutString(dump_stream.GetString());
409 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
412 lldb::ExpressionVariableSP m_persistent_variable_sp;
413 Materializer::PersistentVariableDelegate *m_delegate;
416 uint32_t Materializer::AddPersistentVariable(
417 lldb::ExpressionVariableSP &persistent_variable_sp,
418 PersistentVariableDelegate *delegate, Status &err) {
419 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
420 iter->reset(new EntityPersistentVariable(persistent_variable_sp, delegate));
421 uint32_t ret = AddStructMember(**iter);
422 (*iter)->SetOffset(ret);
426 class EntityVariable : public Materializer::Entity {
428 EntityVariable(lldb::VariableSP &variable_sp)
429 : Entity(), m_variable_sp(variable_sp), m_is_reference(false),
430 m_temporary_allocation(LLDB_INVALID_ADDRESS),
431 m_temporary_allocation_size(0) {
432 // Hard-coding to maximum size of a pointer since all variables are
433 // materialized by reference
437 m_variable_sp->GetType()->GetForwardCompilerType().IsReferenceType();
440 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
441 lldb::addr_t process_address, Status &err) override {
442 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
444 const lldb::addr_t load_addr = process_address + m_offset;
446 log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64
447 ", m_variable_sp = %s]",
448 (uint64_t)load_addr, m_variable_sp->GetName().AsCString());
451 ExecutionContextScope *scope = frame_sp.get();
454 scope = map.GetBestExecutionContextScope();
456 lldb::ValueObjectSP valobj_sp =
457 ValueObjectVariable::Create(scope, m_variable_sp);
460 err.SetErrorStringWithFormat(
461 "couldn't get a value object for variable %s",
462 m_variable_sp->GetName().AsCString());
466 Status valobj_error = valobj_sp->GetError();
468 if (valobj_error.Fail()) {
469 err.SetErrorStringWithFormat("couldn't get the value of variable %s: %s",
470 m_variable_sp->GetName().AsCString(),
471 valobj_error.AsCString());
475 if (m_is_reference) {
476 DataExtractor valobj_extractor;
477 Status extract_error;
478 valobj_sp->GetData(valobj_extractor, extract_error);
480 if (!extract_error.Success()) {
481 err.SetErrorStringWithFormat(
482 "couldn't read contents of reference variable %s: %s",
483 m_variable_sp->GetName().AsCString(), extract_error.AsCString());
487 lldb::offset_t offset = 0;
488 lldb::addr_t reference_addr = valobj_extractor.GetAddress(&offset);
491 map.WritePointerToMemory(load_addr, reference_addr, write_error);
493 if (!write_error.Success()) {
494 err.SetErrorStringWithFormat("couldn't write the contents of reference "
495 "variable %s to memory: %s",
496 m_variable_sp->GetName().AsCString(),
497 write_error.AsCString());
501 AddressType address_type = eAddressTypeInvalid;
502 const bool scalar_is_load_address = false;
503 lldb::addr_t addr_of_valobj =
504 valobj_sp->GetAddressOf(scalar_is_load_address, &address_type);
505 if (addr_of_valobj != LLDB_INVALID_ADDRESS) {
507 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error);
509 if (!write_error.Success()) {
510 err.SetErrorStringWithFormat(
511 "couldn't write the address of variable %s to memory: %s",
512 m_variable_sp->GetName().AsCString(), write_error.AsCString());
517 Status extract_error;
518 valobj_sp->GetData(data, extract_error);
519 if (!extract_error.Success()) {
520 err.SetErrorStringWithFormat("couldn't get the value of %s: %s",
521 m_variable_sp->GetName().AsCString(),
522 extract_error.AsCString());
526 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
527 err.SetErrorStringWithFormat(
528 "trying to create a temporary region for %s but one exists",
529 m_variable_sp->GetName().AsCString());
533 if (data.GetByteSize() < m_variable_sp->GetType()->GetByteSize()) {
534 if (data.GetByteSize() == 0 &&
535 m_variable_sp->LocationExpression().IsValid() == false) {
536 err.SetErrorStringWithFormat("the variable '%s' has no location, "
537 "it may have been optimized out",
538 m_variable_sp->GetName().AsCString());
540 err.SetErrorStringWithFormat(
541 "size of variable %s (%" PRIu64
542 ") is larger than the ValueObject's size (%" PRIu64 ")",
543 m_variable_sp->GetName().AsCString(),
544 m_variable_sp->GetType()->GetByteSize(), data.GetByteSize());
550 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(
560 data.GetByteSize(), byte_align,
561 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
562 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error);
564 m_temporary_allocation_size = data.GetByteSize();
566 m_original_data.reset(
567 new DataBufferHeap(data.GetDataStart(), data.GetByteSize()));
569 if (!alloc_error.Success()) {
570 err.SetErrorStringWithFormat(
571 "couldn't allocate a temporary region for %s: %s",
572 m_variable_sp->GetName().AsCString(), alloc_error.AsCString());
578 map.WriteMemory(m_temporary_allocation, data.GetDataStart(),
579 data.GetByteSize(), write_error);
581 if (!write_error.Success()) {
582 err.SetErrorStringWithFormat(
583 "couldn't write to the temporary region for %s: %s",
584 m_variable_sp->GetName().AsCString(), write_error.AsCString());
588 Status pointer_write_error;
590 map.WritePointerToMemory(load_addr, m_temporary_allocation,
591 pointer_write_error);
593 if (!pointer_write_error.Success()) {
594 err.SetErrorStringWithFormat(
595 "couldn't write the address of the temporary region for %s: %s",
596 m_variable_sp->GetName().AsCString(),
597 pointer_write_error.AsCString());
603 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
604 lldb::addr_t process_address, lldb::addr_t frame_top,
605 lldb::addr_t frame_bottom, Status &err) override {
606 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
608 const lldb::addr_t load_addr = process_address + m_offset;
610 log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64
611 ", m_variable_sp = %s]",
612 (uint64_t)load_addr, m_variable_sp->GetName().AsCString());
615 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
616 ExecutionContextScope *scope = frame_sp.get();
619 scope = map.GetBestExecutionContextScope();
621 lldb::ValueObjectSP valobj_sp =
622 ValueObjectVariable::Create(scope, m_variable_sp);
625 err.SetErrorStringWithFormat(
626 "couldn't get a value object for variable %s",
627 m_variable_sp->GetName().AsCString());
631 lldb_private::DataExtractor data;
633 Status extract_error;
635 map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(),
638 if (!extract_error.Success()) {
639 err.SetErrorStringWithFormat("couldn't get the data for variable %s",
640 m_variable_sp->GetName().AsCString());
644 bool actually_write = true;
646 if (m_original_data) {
647 if ((data.GetByteSize() == m_original_data->GetByteSize()) &&
648 !memcmp(m_original_data->GetBytes(), data.GetDataStart(),
649 data.GetByteSize())) {
650 actually_write = false;
656 if (actually_write) {
657 valobj_sp->SetData(data, set_error);
659 if (!set_error.Success()) {
660 err.SetErrorStringWithFormat(
661 "couldn't write the new contents of %s back into the variable",
662 m_variable_sp->GetName().AsCString());
669 map.Free(m_temporary_allocation, free_error);
671 if (!free_error.Success()) {
672 err.SetErrorStringWithFormat(
673 "couldn't free the temporary region for %s: %s",
674 m_variable_sp->GetName().AsCString(), free_error.AsCString());
678 m_original_data.reset();
679 m_temporary_allocation = LLDB_INVALID_ADDRESS;
680 m_temporary_allocation_size = 0;
684 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
686 StreamString dump_stream;
688 const lldb::addr_t load_addr = process_address + m_offset;
689 dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr);
693 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
696 dump_stream.Printf("Pointer:\n");
698 DataBufferHeap data(m_size, 0);
700 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
702 if (!err.Success()) {
703 dump_stream.Printf(" <could not be read>\n");
705 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
706 map.GetByteOrder(), map.GetAddressByteSize());
708 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
711 lldb::offset_t offset;
713 ptr = extractor.GetPointer(&offset);
715 dump_stream.PutChar('\n');
719 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) {
720 dump_stream.Printf("Points to process memory:\n");
722 dump_stream.Printf("Temporary allocation:\n");
725 if (ptr == LLDB_INVALID_ADDRESS) {
726 dump_stream.Printf(" <could not be be found>\n");
728 DataBufferHeap data(m_temporary_allocation_size, 0);
730 map.ReadMemory(data.GetBytes(), m_temporary_allocation,
731 m_temporary_allocation_size, err);
733 if (!err.Success()) {
734 dump_stream.Printf(" <could not be read>\n");
736 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
739 dump_stream.PutChar('\n');
743 log->PutString(dump_stream.GetString());
746 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {
747 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
750 map.Free(m_temporary_allocation, free_error);
752 m_temporary_allocation = LLDB_INVALID_ADDRESS;
753 m_temporary_allocation_size = 0;
758 lldb::VariableSP m_variable_sp;
760 lldb::addr_t m_temporary_allocation;
761 size_t m_temporary_allocation_size;
762 lldb::DataBufferSP m_original_data;
765 uint32_t Materializer::AddVariable(lldb::VariableSP &variable_sp, Status &err) {
766 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
767 iter->reset(new EntityVariable(variable_sp));
768 uint32_t ret = AddStructMember(**iter);
769 (*iter)->SetOffset(ret);
773 class EntityResultVariable : public Materializer::Entity {
775 EntityResultVariable(const CompilerType &type, bool is_program_reference,
777 Materializer::PersistentVariableDelegate *delegate)
778 : Entity(), m_type(type), m_is_program_reference(is_program_reference),
779 m_keep_in_memory(keep_in_memory),
780 m_temporary_allocation(LLDB_INVALID_ADDRESS),
781 m_temporary_allocation_size(0), m_delegate(delegate) {
782 // Hard-coding to maximum size of a pointer since all results are
783 // materialized by reference
788 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
789 lldb::addr_t process_address, Status &err) override {
790 if (!m_is_program_reference) {
791 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
792 err.SetErrorString("Trying to create a temporary region for the result "
797 const lldb::addr_t load_addr = process_address + m_offset;
799 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
801 size_t byte_size = m_type.GetByteSize(exe_scope);
802 size_t bit_align = m_type.GetTypeBitAlign();
803 size_t byte_align = (bit_align + 7) / 8;
809 const bool zero_memory = true;
811 m_temporary_allocation = map.Malloc(
812 byte_size, byte_align,
813 lldb::ePermissionsReadable | lldb::ePermissionsWritable,
814 IRMemoryMap::eAllocationPolicyMirror, zero_memory, alloc_error);
815 m_temporary_allocation_size = byte_size;
817 if (!alloc_error.Success()) {
818 err.SetErrorStringWithFormat(
819 "couldn't allocate a temporary region for the result: %s",
820 alloc_error.AsCString());
824 Status pointer_write_error;
826 map.WritePointerToMemory(load_addr, m_temporary_allocation,
827 pointer_write_error);
829 if (!pointer_write_error.Success()) {
830 err.SetErrorStringWithFormat("couldn't write the address of the "
831 "temporary region for the result: %s",
832 pointer_write_error.AsCString());
837 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
838 lldb::addr_t process_address, lldb::addr_t frame_top,
839 lldb::addr_t frame_bottom, Status &err) override {
842 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
845 err.SetErrorString("Couldn't dematerialize a result variable: invalid "
846 "execution context scope");
850 lldb::addr_t address;
852 const lldb::addr_t load_addr = process_address + m_offset;
854 map.ReadPointerFromMemory(&address, load_addr, read_error);
856 if (!read_error.Success()) {
857 err.SetErrorString("Couldn't dematerialize a result variable: couldn't "
862 lldb::TargetSP target_sp = exe_scope->CalculateTarget();
865 err.SetErrorString("Couldn't dematerialize a result variable: no target");
869 Status type_system_error;
870 TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(
871 &type_system_error, m_type.GetMinimumLanguage());
874 err.SetErrorStringWithFormat("Couldn't dematerialize a result variable: "
875 "couldn't get the corresponding type "
877 type_system_error.AsCString());
881 PersistentExpressionState *persistent_state =
882 type_system->GetPersistentExpressionState();
884 if (!persistent_state) {
885 err.SetErrorString("Couldn't dematerialize a result variable: "
886 "corresponding type system doesn't handle persistent "
893 ? m_delegate->GetName()
894 : persistent_state->GetNextPersistentVariableName(
895 *target_sp, persistent_state->GetPersistentVariablePrefix());
897 lldb::ExpressionVariableSP ret = persistent_state->CreatePersistentVariable(
898 exe_scope, name, m_type, map.GetByteOrder(), map.GetAddressByteSize());
901 err.SetErrorStringWithFormat("couldn't dematerialize a result variable: "
902 "failed to make persistent variable %s",
907 lldb::ProcessSP process_sp =
908 map.GetBestExecutionContextScope()->CalculateProcess();
911 m_delegate->DidDematerialize(ret);
915 (m_is_program_reference && process_sp && process_sp->CanJIT() &&
916 !(address >= frame_bottom && address < frame_top));
918 if (can_persist && m_keep_in_memory) {
919 ret->m_live_sp = ValueObjectConstResult::Create(exe_scope, m_type, name,
920 address, eAddressTypeLoad,
921 map.GetAddressByteSize());
926 const size_t pvar_byte_size = ret->GetByteSize();
927 uint8_t *pvar_data = ret->GetValueBytes();
929 map.ReadMemory(pvar_data, address, pvar_byte_size, read_error);
931 if (!read_error.Success()) {
933 "Couldn't dematerialize a result variable: couldn't read its memory");
937 if (!can_persist || !m_keep_in_memory) {
938 ret->m_flags |= ExpressionVariable::EVNeedsAllocation;
940 if (m_temporary_allocation != LLDB_INVALID_ADDRESS) {
942 map.Free(m_temporary_allocation, free_error);
945 ret->m_flags |= ExpressionVariable::EVIsLLDBAllocated;
948 m_temporary_allocation = LLDB_INVALID_ADDRESS;
949 m_temporary_allocation_size = 0;
952 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
954 StreamString dump_stream;
956 const lldb::addr_t load_addr = process_address + m_offset;
958 dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr);
962 lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
965 dump_stream.Printf("Pointer:\n");
967 DataBufferHeap data(m_size, 0);
969 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
971 if (!err.Success()) {
972 dump_stream.Printf(" <could not be read>\n");
974 DataExtractor extractor(data.GetBytes(), data.GetByteSize(),
975 map.GetByteOrder(), map.GetAddressByteSize());
977 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
980 lldb::offset_t offset;
982 ptr = extractor.GetPointer(&offset);
984 dump_stream.PutChar('\n');
988 if (m_temporary_allocation == LLDB_INVALID_ADDRESS) {
989 dump_stream.Printf("Points to process memory:\n");
991 dump_stream.Printf("Temporary allocation:\n");
994 if (ptr == LLDB_INVALID_ADDRESS) {
995 dump_stream.Printf(" <could not be be found>\n");
997 DataBufferHeap data(m_temporary_allocation_size, 0);
999 map.ReadMemory(data.GetBytes(), m_temporary_allocation,
1000 m_temporary_allocation_size, err);
1002 if (!err.Success()) {
1003 dump_stream.Printf(" <could not be read>\n");
1005 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1008 dump_stream.PutChar('\n');
1012 log->PutString(dump_stream.GetString());
1015 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {
1016 if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS) {
1019 map.Free(m_temporary_allocation, free_error);
1022 m_temporary_allocation = LLDB_INVALID_ADDRESS;
1023 m_temporary_allocation_size = 0;
1027 CompilerType m_type;
1028 bool m_is_program_reference;
1029 bool m_keep_in_memory;
1031 lldb::addr_t m_temporary_allocation;
1032 size_t m_temporary_allocation_size;
1033 Materializer::PersistentVariableDelegate *m_delegate;
1036 uint32_t Materializer::AddResultVariable(const CompilerType &type,
1037 bool is_program_reference,
1038 bool keep_in_memory,
1039 PersistentVariableDelegate *delegate,
1041 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1042 iter->reset(new EntityResultVariable(type, is_program_reference,
1043 keep_in_memory, delegate));
1044 uint32_t ret = AddStructMember(**iter);
1045 (*iter)->SetOffset(ret);
1049 class EntitySymbol : public Materializer::Entity {
1051 EntitySymbol(const Symbol &symbol) : Entity(), m_symbol(symbol) {
1052 // Hard-coding to maximum size of a symbol
1057 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1058 lldb::addr_t process_address, Status &err) override {
1059 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1061 const lldb::addr_t load_addr = process_address + m_offset;
1064 log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64
1066 (uint64_t)load_addr, m_symbol.GetName().AsCString());
1069 const Address sym_address = m_symbol.GetAddress();
1071 ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
1073 lldb::TargetSP target_sp;
1076 target_sp = map.GetBestExecutionContextScope()->CalculateTarget();
1079 err.SetErrorStringWithFormat(
1080 "couldn't resolve symbol %s because there is no target",
1081 m_symbol.GetName().AsCString());
1085 lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get());
1087 if (resolved_address == LLDB_INVALID_ADDRESS)
1088 resolved_address = sym_address.GetFileAddress();
1090 Status pointer_write_error;
1092 map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error);
1094 if (!pointer_write_error.Success()) {
1095 err.SetErrorStringWithFormat(
1096 "couldn't write the address of symbol %s: %s",
1097 m_symbol.GetName().AsCString(), pointer_write_error.AsCString());
1102 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1103 lldb::addr_t process_address, lldb::addr_t frame_top,
1104 lldb::addr_t frame_bottom, Status &err) override {
1105 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1107 const lldb::addr_t load_addr = process_address + m_offset;
1110 log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64
1112 (uint64_t)load_addr, m_symbol.GetName().AsCString());
1115 // no work needs to be done
1118 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
1119 Log *log) override {
1120 StreamString dump_stream;
1124 const lldb::addr_t load_addr = process_address + m_offset;
1126 dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr,
1127 m_symbol.GetName().AsCString());
1130 dump_stream.Printf("Pointer:\n");
1132 DataBufferHeap data(m_size, 0);
1134 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1136 if (!err.Success()) {
1137 dump_stream.Printf(" <could not be read>\n");
1139 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1142 dump_stream.PutChar('\n');
1146 log->PutString(dump_stream.GetString());
1149 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
1155 uint32_t Materializer::AddSymbol(const Symbol &symbol_sp, Status &err) {
1156 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1157 iter->reset(new EntitySymbol(symbol_sp));
1158 uint32_t ret = AddStructMember(**iter);
1159 (*iter)->SetOffset(ret);
1163 class EntityRegister : public Materializer::Entity {
1165 EntityRegister(const RegisterInfo ®ister_info)
1166 : Entity(), m_register_info(register_info) {
1167 // Hard-coding alignment conservatively
1168 m_size = m_register_info.byte_size;
1169 m_alignment = m_register_info.byte_size;
1172 void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1173 lldb::addr_t process_address, Status &err) override {
1174 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1176 const lldb::addr_t load_addr = process_address + m_offset;
1179 log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64
1180 ", m_register_info = %s]",
1181 (uint64_t)load_addr, m_register_info.name);
1184 RegisterValue reg_value;
1186 if (!frame_sp.get()) {
1187 err.SetErrorStringWithFormat(
1188 "couldn't materialize register %s without a stack frame",
1189 m_register_info.name);
1193 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1195 if (!reg_context_sp->ReadRegister(&m_register_info, reg_value)) {
1196 err.SetErrorStringWithFormat("couldn't read the value of register %s",
1197 m_register_info.name);
1201 DataExtractor register_data;
1203 if (!reg_value.GetData(register_data)) {
1204 err.SetErrorStringWithFormat("couldn't get the data for register %s",
1205 m_register_info.name);
1209 if (register_data.GetByteSize() != m_register_info.byte_size) {
1210 err.SetErrorStringWithFormat(
1211 "data for register %s had size %llu but we expected %llu",
1212 m_register_info.name, (unsigned long long)register_data.GetByteSize(),
1213 (unsigned long long)m_register_info.byte_size);
1217 m_register_contents.reset(new DataBufferHeap(register_data.GetDataStart(),
1218 register_data.GetByteSize()));
1222 map.WriteMemory(load_addr, register_data.GetDataStart(),
1223 register_data.GetByteSize(), write_error);
1225 if (!write_error.Success()) {
1226 err.SetErrorStringWithFormat(
1227 "couldn't write the contents of register %s: %s",
1228 m_register_info.name, write_error.AsCString());
1233 void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1234 lldb::addr_t process_address, lldb::addr_t frame_top,
1235 lldb::addr_t frame_bottom, Status &err) override {
1236 Log *log(lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS));
1238 const lldb::addr_t load_addr = process_address + m_offset;
1241 log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64
1242 ", m_register_info = %s]",
1243 (uint64_t)load_addr, m_register_info.name);
1246 Status extract_error;
1248 DataExtractor register_data;
1250 if (!frame_sp.get()) {
1251 err.SetErrorStringWithFormat(
1252 "couldn't dematerialize register %s without a stack frame",
1253 m_register_info.name);
1257 lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1259 map.GetMemoryData(register_data, load_addr, m_register_info.byte_size,
1262 if (!extract_error.Success()) {
1263 err.SetErrorStringWithFormat("couldn't get the data for register %s: %s",
1264 m_register_info.name,
1265 extract_error.AsCString());
1269 if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(),
1270 register_data.GetByteSize())) {
1271 // No write required, and in particular we avoid errors if the register
1274 m_register_contents.reset();
1278 m_register_contents.reset();
1280 RegisterValue register_value(
1281 const_cast<uint8_t *>(register_data.GetDataStart()),
1282 register_data.GetByteSize(), register_data.GetByteOrder());
1284 if (!reg_context_sp->WriteRegister(&m_register_info, register_value)) {
1285 err.SetErrorStringWithFormat("couldn't write the value of register %s",
1286 m_register_info.name);
1291 void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
1292 Log *log) override {
1293 StreamString dump_stream;
1297 const lldb::addr_t load_addr = process_address + m_offset;
1299 dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr,
1300 m_register_info.name);
1303 dump_stream.Printf("Value:\n");
1305 DataBufferHeap data(m_size, 0);
1307 map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1309 if (!err.Success()) {
1310 dump_stream.Printf(" <could not be read>\n");
1312 DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16,
1315 dump_stream.PutChar('\n');
1319 log->PutString(dump_stream.GetString());
1322 void Wipe(IRMemoryMap &map, lldb::addr_t process_address) override {}
1325 RegisterInfo m_register_info;
1326 lldb::DataBufferSP m_register_contents;
1329 uint32_t Materializer::AddRegister(const RegisterInfo ®ister_info,
1331 EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1332 iter->reset(new EntityRegister(register_info));
1333 uint32_t ret = AddStructMember(**iter);
1334 (*iter)->SetOffset(ret);
1338 Materializer::Materializer()
1339 : m_dematerializer_wp(), m_current_offset(0), m_struct_alignment(8) {}
1341 Materializer::~Materializer() {
1342 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1344 if (dematerializer_sp)
1345 dematerializer_sp->Wipe();
1348 Materializer::DematerializerSP
1349 Materializer::Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
1350 lldb::addr_t process_address, Status &error) {
1351 ExecutionContextScope *exe_scope = frame_sp.get();
1354 exe_scope = map.GetBestExecutionContextScope();
1356 DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1358 if (dematerializer_sp) {
1359 error.SetErrorToGenericError();
1360 error.SetErrorString("Couldn't materialize: already materialized");
1363 DematerializerSP ret(
1364 new Dematerializer(*this, frame_sp, map, process_address));
1367 error.SetErrorToGenericError();
1368 error.SetErrorString("Couldn't materialize: target doesn't exist");
1371 for (EntityUP &entity_up : m_entities) {
1372 entity_up->Materialize(frame_sp, map, process_address, error);
1374 if (!error.Success())
1375 return DematerializerSP();
1379 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
1381 "Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64
1383 static_cast<void *>(frame_sp.get()), process_address);
1384 for (EntityUP &entity_up : m_entities)
1385 entity_up->DumpToLog(map, process_address, log);
1388 m_dematerializer_wp = ret;
1393 void Materializer::Dematerializer::Dematerialize(Status &error,
1394 lldb::addr_t frame_bottom,
1395 lldb::addr_t frame_top) {
1396 lldb::StackFrameSP frame_sp;
1398 lldb::ThreadSP thread_sp = m_thread_wp.lock();
1400 frame_sp = thread_sp->GetFrameWithStackID(m_stack_id);
1402 ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope();
1405 error.SetErrorToGenericError();
1406 error.SetErrorString("Couldn't dematerialize: invalid dematerializer");
1410 error.SetErrorToGenericError();
1411 error.SetErrorString("Couldn't dematerialize: target is gone");
1414 lldb_private::GetLogIfAllCategoriesSet(LIBLLDB_LOG_EXPRESSIONS)) {
1415 log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address "
1416 "= 0x%" PRIx64 ") about to dematerialize:",
1417 static_cast<void *>(frame_sp.get()), m_process_address);
1418 for (EntityUP &entity_up : m_materializer->m_entities)
1419 entity_up->DumpToLog(*m_map, m_process_address, log);
1422 for (EntityUP &entity_up : m_materializer->m_entities) {
1423 entity_up->Dematerialize(frame_sp, *m_map, m_process_address, frame_top,
1424 frame_bottom, error);
1426 if (!error.Success())
1434 void Materializer::Dematerializer::Wipe() {
1438 for (EntityUP &entity_up : m_materializer->m_entities) {
1439 entity_up->Wipe(*m_map, m_process_address);
1442 m_materializer = nullptr;
1444 m_process_address = LLDB_INVALID_ADDRESS;
1447 Materializer::PersistentVariableDelegate::~PersistentVariableDelegate() =