]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - contrib/llvm/tools/lldb/source/Expression/Materializer.cpp
MFC r258054: Update LLDB to upstream r194122 snapshot
[FreeBSD/stable/10.git] / contrib / llvm / tools / lldb / source / Expression / Materializer.cpp
1 //===-- Materializer.cpp ----------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
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"
25
26 using namespace lldb_private;
27
28 uint32_t
29 Materializer::AddStructMember (Entity &entity)
30 {
31     uint32_t size = entity.GetSize();
32     uint32_t alignment = entity.GetAlignment();
33     
34     uint32_t ret;
35     
36     if (m_current_offset == 0)
37         m_struct_alignment = alignment;
38     
39     if (m_current_offset % alignment)
40         m_current_offset += (alignment - (m_current_offset % alignment));
41     
42     ret = m_current_offset;
43     
44     m_current_offset += size;
45     
46     return ret;
47 }
48
49 void
50 Materializer::Entity::SetSizeAndAlignmentFromType (ClangASTType &type)
51 {
52     m_size = type.GetByteSize();
53     
54     uint32_t bit_alignment = type.GetTypeBitAlign();
55     
56     if (bit_alignment % 8)
57     {
58         bit_alignment += 8;
59         bit_alignment &= ~((uint32_t)0x111u);
60     }
61     
62     m_alignment = bit_alignment / 8;
63 }
64
65 class EntityPersistentVariable : public Materializer::Entity
66 {
67 public:
68     EntityPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp) :
69         Entity(),
70         m_persistent_variable_sp(persistent_variable_sp)
71     {
72         // Hard-coding to maximum size of a pointer since persistent variables are materialized by reference
73         m_size = 8;
74         m_alignment = 8;
75     }
76     
77     void MakeAllocation (IRMemoryMap &map, Error &err)
78     {
79         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
80
81         // Allocate a spare memory area to store the persistent variable's contents.
82         
83         Error allocate_error;
84         
85         lldb::addr_t mem = map.Malloc(m_persistent_variable_sp->GetByteSize(),
86                                       8,
87                                       lldb::ePermissionsReadable | lldb::ePermissionsWritable,
88                                       IRMemoryMap::eAllocationPolicyMirror,
89                                       allocate_error);
90         
91         if (!allocate_error.Success())
92         {
93             err.SetErrorStringWithFormat("couldn't allocate a memory area to store %s: %s", m_persistent_variable_sp->GetName().GetCString(), allocate_error.AsCString());
94             return;
95         }
96         
97         if (log)
98             log->Printf("Allocated %s (0x%" PRIx64 ") sucessfully", m_persistent_variable_sp->GetName().GetCString(), mem);
99         
100         // Put the location of the spare memory into the live data of the ValueObject.
101                 
102         m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope(),
103                                                                               m_persistent_variable_sp->GetTypeFromUser(),
104                                                                               m_persistent_variable_sp->GetName(),
105                                                                               mem,
106                                                                               eAddressTypeLoad,
107                                                                               m_persistent_variable_sp->GetByteSize());
108         
109         // Clear the flag if the variable will never be deallocated.
110         
111         if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget)
112         {
113             Error leak_error;
114             map.Leak(mem, leak_error);
115             m_persistent_variable_sp->m_flags &= ~ClangExpressionVariable::EVNeedsAllocation;
116         }
117         
118         // Write the contents of the variable to the area.
119         
120         Error write_error;
121         
122         map.WriteMemory (mem,
123                          m_persistent_variable_sp->GetValueBytes(),
124                          m_persistent_variable_sp->GetByteSize(),
125                          write_error);
126         
127         if (!write_error.Success())
128         {
129             err.SetErrorStringWithFormat ("couldn't write %s to the target: %s", m_persistent_variable_sp->GetName().AsCString(),
130                                           write_error.AsCString());
131             return;
132         }
133     }
134     
135     void DestroyAllocation (IRMemoryMap &map, Error &err)
136     {
137         Error deallocate_error;
138         
139         map.Free((lldb::addr_t)m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong(), deallocate_error);
140         
141         m_persistent_variable_sp->m_live_sp.reset();
142             
143         if (!deallocate_error.Success())
144         {
145             err.SetErrorStringWithFormat ("couldn't deallocate memory for %s: %s", m_persistent_variable_sp->GetName().GetCString(), deallocate_error.AsCString());
146         }
147     }
148     
149     void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
150     {
151         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
152
153         const lldb::addr_t load_addr = process_address + m_offset;
154     
155         if (log)
156         {
157             log->Printf("EntityPersistentVariable::Materialize [address = 0x%" PRIx64 ", m_name = %s, m_flags = 0x%hx]",
158                         (uint64_t)load_addr,
159                         m_persistent_variable_sp->GetName().AsCString(),
160                         m_persistent_variable_sp->m_flags);
161         }
162         
163         if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsAllocation)
164         {
165             MakeAllocation(map, err);
166             m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
167             
168             if (!err.Success())
169                 return;
170         }
171         
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)
174         {
175             Error write_error;
176                         
177             map.WriteScalarToMemory(load_addr,
178                                     m_persistent_variable_sp->m_live_sp->GetValue().GetScalar(),
179                                     map.GetAddressByteSize(),
180                                     write_error);
181             
182             if (!write_error.Success())
183             {
184                 err.SetErrorStringWithFormat("couldn't write the location of %s to memory: %s", m_persistent_variable_sp->GetName().AsCString(), write_error.AsCString());
185             }
186         }
187         else
188         {
189             err.SetErrorStringWithFormat("no materialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
190             return;
191         }
192     }
193     
194     void Dematerialize (lldb::StackFrameSP &frame_sp,
195                         IRMemoryMap &map,
196                         lldb::addr_t process_address,
197                         lldb::addr_t frame_top,
198                         lldb::addr_t frame_bottom,
199                         Error &err)
200     {
201         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
202         
203         const lldb::addr_t load_addr = process_address + m_offset;
204
205         if (log)
206         {
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);
211         }
212         
213         if ((m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsLLDBAllocated) ||
214             (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsProgramReference))
215         {
216             if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVIsProgramReference &&
217                 !m_persistent_variable_sp->m_live_sp)
218             {
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.
221                 
222                 lldb::addr_t location;
223                 Error read_error;
224                 
225                 map.ReadPointerFromMemory(&location, load_addr, read_error);
226                                 
227                 if (!read_error.Success())
228                 {
229                     err.SetErrorStringWithFormat("couldn't read the address of program-allocated variable %s: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
230                     return;
231                 }
232                                 
233                 m_persistent_variable_sp->m_live_sp = ValueObjectConstResult::Create (map.GetBestExecutionContextScope (),
234                                                                                       m_persistent_variable_sp->GetTypeFromUser(),
235                                                                                       m_persistent_variable_sp->GetName(),
236                                                                                       location,
237                                                                                       eAddressTypeLoad,
238                                                                                       m_persistent_variable_sp->GetByteSize());
239                 
240                 if (frame_top != LLDB_INVALID_ADDRESS &&
241                     frame_bottom != LLDB_INVALID_ADDRESS &&
242                     location >= frame_bottom &&
243                     location <= frame_top)
244                 {
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
247                     // reallocation.
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;
252                 }
253             }
254             
255             lldb::addr_t mem = m_persistent_variable_sp->m_live_sp->GetValue().GetScalar().ULongLong();
256             
257             if (!m_persistent_variable_sp->m_live_sp)
258             {
259                 err.SetErrorStringWithFormat("couldn't find the memory area used to store %s", m_persistent_variable_sp->GetName().GetCString());
260                 return;
261             }
262             
263             if (m_persistent_variable_sp->m_live_sp->GetValue().GetValueAddressType() != eAddressTypeLoad)
264             {
265                 err.SetErrorStringWithFormat("the address of the memory area for %s is in an incorrect format", m_persistent_variable_sp->GetName().GetCString());
266                 return;
267             }
268             
269             if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsFreezeDry ||
270                 m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget)
271             {                
272                 if (log)
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());
274                 
275                 // Read the contents of the spare memory area
276                 
277                 m_persistent_variable_sp->ValueUpdated ();
278                 
279                 Error read_error;
280                 
281                 map.ReadMemory(m_persistent_variable_sp->GetValueBytes(),
282                                mem,
283                                m_persistent_variable_sp->GetByteSize(),
284                                read_error);
285                 
286                 if (!read_error.Success())
287                 {
288                     err.SetErrorStringWithFormat ("couldn't read the contents of %s from memory: %s", m_persistent_variable_sp->GetName().GetCString(), read_error.AsCString());
289                     return;
290                 }
291                     
292                 m_persistent_variable_sp->m_flags &= ~ClangExpressionVariable::EVNeedsFreezeDry;
293             }
294         }
295         else
296         {
297             err.SetErrorStringWithFormat("no dematerialization happened for persistent variable %s", m_persistent_variable_sp->GetName().AsCString());
298             return;
299         }
300         
301         lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
302         if (!process_sp ||
303             !process_sp->CanJIT())
304         {
305             // Allocations are not persistent so persistent variables cannot stay materialized.
306             
307             m_persistent_variable_sp->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
308
309             DestroyAllocation(map, err);
310             if (!err.Success())
311                 return;
312         }
313         else if (m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVNeedsAllocation &&
314                  !(m_persistent_variable_sp->m_flags & ClangExpressionVariable::EVKeepInTarget))
315         {
316             DestroyAllocation(map, err);
317             if (!err.Success())
318                 return;
319         }
320     }
321     
322     void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
323     {
324         StreamString dump_stream;
325         
326         Error err;
327         
328         const lldb::addr_t load_addr = process_address + m_offset;
329
330         dump_stream.Printf("0x%" PRIx64 ": EntityPersistentVariable (%s)\n", load_addr, m_persistent_variable_sp->GetName().AsCString());
331         
332         {
333             dump_stream.Printf("Pointer:\n");
334             
335             DataBufferHeap data (m_size, 0);
336             
337             map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
338             
339             if (!err.Success())
340             {
341                 dump_stream.Printf("  <could not be read>\n");
342             }
343             else
344             {
345                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
346                 
347                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
348                 
349                 dump_stream.PutChar('\n');
350             }
351         }
352         
353         {
354             dump_stream.Printf("Target:\n");
355             
356             lldb::addr_t target_address;
357             
358             map.ReadPointerFromMemory (&target_address, load_addr, err);
359                         
360             if (!err.Success())
361             {
362                 dump_stream.Printf("  <could not be read>\n");
363             }
364             else
365             {
366                 DataBufferHeap data (m_persistent_variable_sp->GetByteSize(), 0);
367             
368                 map.ReadMemory(data.GetBytes(), target_address, m_persistent_variable_sp->GetByteSize(), err);
369                 
370                 if (!err.Success())
371                 {
372                     dump_stream.Printf("  <could not be read>\n");
373                 }
374                 else
375                 {
376                     DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
377                     
378                     extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, target_address);
379                     
380                     dump_stream.PutChar('\n');
381                 }
382             }
383         }
384         
385         log->PutCString(dump_stream.GetData());
386     }
387     
388     void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
389     {
390     }
391 private:
392     lldb::ClangExpressionVariableSP m_persistent_variable_sp;
393 };
394
395 uint32_t
396 Materializer::AddPersistentVariable (lldb::ClangExpressionVariableSP &persistent_variable_sp, Error &err)
397 {
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);
402     return ret;
403 }
404
405 class EntityVariable : public Materializer::Entity
406 {
407 public:
408     EntityVariable (lldb::VariableSP &variable_sp) :
409         Entity(),
410         m_variable_sp(variable_sp),
411         m_is_reference(false),
412         m_temporary_allocation(LLDB_INVALID_ADDRESS),
413         m_temporary_allocation_size(0)
414     {
415         // Hard-coding to maximum size of a pointer since all variables are materialized by reference
416         m_size = 8;
417         m_alignment = 8;
418         m_is_reference = m_variable_sp->GetType()->GetClangForwardType().IsReferenceType();
419     }
420     
421     void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
422     {
423         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
424         
425         const lldb::addr_t load_addr = process_address + m_offset;
426         if (log)
427         {
428             log->Printf("EntityVariable::Materialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
429                         (uint64_t)load_addr,
430                         m_variable_sp->GetName().AsCString());
431         }
432         
433         ExecutionContextScope *scope = frame_sp.get();
434         
435         if (!scope)
436             scope = map.GetBestExecutionContextScope();
437         
438         lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
439         
440         if (!valobj_sp)
441         {
442             err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
443             return;
444         }
445         
446         if (m_is_reference)
447         {
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);
452             
453             Error write_error;
454             map.WritePointerToMemory(load_addr, reference_addr, write_error);
455             
456             if (!write_error.Success())
457             {
458                 err.SetErrorStringWithFormat("couldn't write the contents of reference variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
459                 return;
460             }
461         }
462         else
463         {
464             lldb::addr_t addr_of_valobj = valobj_sp->GetAddressOf();
465             if (addr_of_valobj != LLDB_INVALID_ADDRESS)
466             {
467                 Error write_error;
468                 map.WritePointerToMemory(load_addr, addr_of_valobj, write_error);
469                 
470                 if (!write_error.Success())
471                 {
472                     err.SetErrorStringWithFormat("couldn't write the address of variable %s to memory: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
473                     return;
474                 }
475             }
476             else
477             {
478                 DataExtractor data;
479                 valobj_sp->GetData(data);
480                 
481                 if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
482                 {
483                     err.SetErrorStringWithFormat("trying to create a temporary region for %s but one exists", m_variable_sp->GetName().AsCString());
484                     return;
485                 }
486                 
487                 if (data.GetByteSize() != m_variable_sp->GetType()->GetByteSize())
488                 {
489                     if (data.GetByteSize() == 0 && m_variable_sp->LocationExpression().IsValid() == false)
490                     {
491                         err.SetErrorStringWithFormat("the variable '%s' has no location, it may have been optimized out", m_variable_sp->GetName().AsCString());
492                     }
493                     else
494                     {
495                         err.SetErrorStringWithFormat("size of variable %s disagrees with the ValueObject's size", m_variable_sp->GetName().AsCString());
496                     }
497                     return;
498                 }
499                 
500                 size_t bit_align = m_variable_sp->GetType()->GetClangLayoutType().GetTypeBitAlign();
501                 size_t byte_align = (bit_align + 7) / 8;
502                 
503                 Error alloc_error;
504                 
505                 m_temporary_allocation = map.Malloc(data.GetByteSize(), byte_align, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, alloc_error);
506                 m_temporary_allocation_size = data.GetByteSize();
507                 
508                 if (!alloc_error.Success())
509                 {
510                     err.SetErrorStringWithFormat("couldn't allocate a temporary region for %s: %s", m_variable_sp->GetName().AsCString(), alloc_error.AsCString());
511                     return;
512                 }
513                 
514                 Error write_error;
515                 
516                 map.WriteMemory(m_temporary_allocation, data.GetDataStart(), data.GetByteSize(), write_error);
517                 
518                 if (!write_error.Success())
519                 {
520                     err.SetErrorStringWithFormat("couldn't write to the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), write_error.AsCString());
521                     return;
522                 }
523                 
524                 Error pointer_write_error;
525                 
526                 map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
527                 
528                 if (!pointer_write_error.Success())
529                 {
530                     err.SetErrorStringWithFormat("couldn't write the address of the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), pointer_write_error.AsCString());
531                 }
532             }
533         }
534     }
535     
536     void Dematerialize (lldb::StackFrameSP &frame_sp,
537                         IRMemoryMap &map,
538                         lldb::addr_t process_address,
539                         lldb::addr_t frame_top,
540                         lldb::addr_t frame_bottom,
541                         Error &err)
542     {
543         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
544
545         const lldb::addr_t load_addr = process_address + m_offset;
546         if (log)
547         {
548             log->Printf("EntityVariable::Dematerialize [address = 0x%" PRIx64 ", m_variable_sp = %s]",
549                         (uint64_t)load_addr,
550                         m_variable_sp->GetName().AsCString());
551         }
552         
553         if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
554         {
555             ExecutionContextScope *scope = frame_sp.get();
556             
557             if (!scope)
558                 scope = map.GetBestExecutionContextScope();
559             
560             lldb::ValueObjectSP valobj_sp = ValueObjectVariable::Create(scope, m_variable_sp);
561             
562             if (!valobj_sp)
563             {
564                 err.SetErrorStringWithFormat("couldn't get a value object for variable %s", m_variable_sp->GetName().AsCString());
565                 return;
566             }
567             
568             lldb_private::DataExtractor data;
569             
570             Error extract_error;
571             
572             map.GetMemoryData(data, m_temporary_allocation, valobj_sp->GetByteSize(), extract_error);
573             
574             if (!extract_error.Success())
575             {
576                 err.SetErrorStringWithFormat("couldn't get the data for variable %s", m_variable_sp->GetName().AsCString());
577                 return;
578             }
579             
580             Error set_error;
581             
582             valobj_sp->SetData(data, set_error);
583             
584             if (!set_error.Success())
585             {
586                 err.SetErrorStringWithFormat("couldn't write the new contents of %s back into the variable", m_variable_sp->GetName().AsCString());
587                 return;
588             }
589             
590             Error free_error;
591             
592             map.Free(m_temporary_allocation, free_error);
593             
594             if (!free_error.Success())
595             {
596                 err.SetErrorStringWithFormat("couldn't free the temporary region for %s: %s", m_variable_sp->GetName().AsCString(), free_error.AsCString());
597                 return;
598             }
599             
600             m_temporary_allocation = LLDB_INVALID_ADDRESS;
601             m_temporary_allocation_size = 0;
602         }
603     }
604     
605     void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
606     {
607         StreamString dump_stream;
608
609         const lldb::addr_t load_addr = process_address + m_offset;
610         dump_stream.Printf("0x%" PRIx64 ": EntityVariable\n", load_addr);
611         
612         Error err;
613                 
614         lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
615         
616         {
617             dump_stream.Printf("Pointer:\n");
618             
619             DataBufferHeap data (m_size, 0);
620             
621             map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
622             
623             if (!err.Success())
624             {
625                 dump_stream.Printf("  <could not be read>\n");
626             }
627             else
628             {
629                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
630                 
631                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
632                 
633                 lldb::offset_t offset;
634                 
635                 ptr = extractor.GetPointer(&offset);
636                 
637                 dump_stream.PutChar('\n');
638             }
639         }
640         
641         if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
642         {
643             dump_stream.Printf("Points to process memory:\n");
644         }
645         else
646         {
647             dump_stream.Printf("Temporary allocation:\n");
648         }
649         
650         if (ptr == LLDB_INVALID_ADDRESS)
651         {
652             dump_stream.Printf("  <could not be be found>\n");
653         }
654         else
655         {
656             DataBufferHeap data (m_temporary_allocation_size, 0);
657             
658             map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
659             
660             if (!err.Success())
661             {
662                 dump_stream.Printf("  <could not be read>\n");
663             }
664             else
665             {
666                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
667                 
668                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
669                 
670                 dump_stream.PutChar('\n');
671             }
672         }
673         
674         log->PutCString(dump_stream.GetData());
675     }
676     
677     void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
678     {
679         if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
680         {
681             Error free_error;
682             
683             map.Free(m_temporary_allocation, free_error);
684             
685             m_temporary_allocation = LLDB_INVALID_ADDRESS;
686             m_temporary_allocation_size = 0;
687         }
688
689     }
690 private:
691     lldb::VariableSP    m_variable_sp;
692     bool                m_is_reference;
693     lldb::addr_t        m_temporary_allocation;
694     size_t              m_temporary_allocation_size;
695 };
696
697 uint32_t
698 Materializer::AddVariable (lldb::VariableSP &variable_sp, Error &err)
699 {
700     EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
701     iter->reset (new EntityVariable (variable_sp));
702     uint32_t ret = AddStructMember(**iter);
703     (*iter)->SetOffset(ret);
704     return ret;
705 }
706
707 class EntityResultVariable : public Materializer::Entity
708 {
709 public:
710     EntityResultVariable (const TypeFromUser &type, bool is_program_reference, bool keep_in_memory) :
711         Entity(),
712         m_type(type),
713         m_is_program_reference(is_program_reference),
714         m_keep_in_memory(keep_in_memory),
715         m_temporary_allocation(LLDB_INVALID_ADDRESS),
716         m_temporary_allocation_size(0)
717     {
718         // Hard-coding to maximum size of a pointer since all results are materialized by reference
719         m_size = 8;
720         m_alignment = 8;
721     }
722     
723     void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
724     {
725         if (!m_is_program_reference)
726         {
727             if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
728             {
729                 err.SetErrorString("Trying to create a temporary region for the result but one exists");
730                 return;
731             }
732             
733             const lldb::addr_t load_addr = process_address + m_offset;
734
735             size_t byte_size = m_type.GetByteSize();
736             size_t bit_align = m_type.GetTypeBitAlign();
737             size_t byte_align = (bit_align + 7) / 8;
738             
739             Error alloc_error;
740             
741             m_temporary_allocation = map.Malloc(byte_size, byte_align, lldb::ePermissionsReadable | lldb::ePermissionsWritable, IRMemoryMap::eAllocationPolicyMirror, alloc_error);
742             m_temporary_allocation_size = byte_size;
743             
744             if (!alloc_error.Success())
745             {
746                 err.SetErrorStringWithFormat("couldn't allocate a temporary region for the result: %s", alloc_error.AsCString());
747                 return;
748             }
749                         
750             Error pointer_write_error;
751             
752             map.WritePointerToMemory(load_addr, m_temporary_allocation, pointer_write_error);
753             
754             if (!pointer_write_error.Success())
755             {
756                 err.SetErrorStringWithFormat("couldn't write the address of the temporary region for the result: %s", pointer_write_error.AsCString());
757             }
758         }
759     }
760     
761     void Dematerialize (lldb::StackFrameSP &frame_sp,
762                         IRMemoryMap &map,
763                         lldb::addr_t process_address,
764                         lldb::addr_t frame_top,
765                         lldb::addr_t frame_bottom,
766                         Error &err)
767     {
768         err.SetErrorString("Tried to detmaterialize a result variable with the normal Dematerialize method");
769     }
770     
771     void Dematerialize (lldb::ClangExpressionVariableSP &result_variable_sp,
772                         lldb::StackFrameSP &frame_sp,
773                         IRMemoryMap &map,
774                         lldb::addr_t process_address,
775                         lldb::addr_t frame_top,
776                         lldb::addr_t frame_bottom,
777                         Error &err)
778     {
779         err.Clear();
780         
781         ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
782         
783         if (!exe_scope)
784         {
785             err.SetErrorString("Couldn't dematerialize a result variable: invalid execution context scope");
786             return;
787         }
788         
789         lldb::addr_t address;
790         Error read_error;
791         const lldb::addr_t load_addr = process_address + m_offset;
792         
793         map.ReadPointerFromMemory (&address, load_addr, read_error);
794         
795         if (!read_error.Success())
796         {
797             err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its address");
798             return;
799         }
800         
801         lldb::TargetSP target_sp = exe_scope->CalculateTarget();
802         
803         if (!target_sp)
804         {
805             err.SetErrorString("Couldn't dematerialize a result variable: no target");
806             return;
807         }
808         
809         ConstString name = target_sp->GetPersistentVariables().GetNextPersistentVariableName();
810         
811         lldb::ClangExpressionVariableSP ret;
812         
813         ret = target_sp->GetPersistentVariables().CreateVariable(exe_scope,
814                                                                  name,
815                                                                  m_type,
816                                                                  map.GetByteOrder(),
817                                                                  map.GetAddressByteSize());
818         
819         if (!ret)
820         {
821             err.SetErrorStringWithFormat("couldn't dematerialize a result variable: failed to make persistent variable %s", name.AsCString());
822             return;
823         }
824         
825         lldb::ProcessSP process_sp = map.GetBestExecutionContextScope()->CalculateProcess();
826         
827         bool can_persist = (m_is_program_reference && process_sp && process_sp->CanJIT() && !(address >= frame_bottom && address < frame_top));
828
829         if (can_persist && m_keep_in_memory)
830         {
831             ret->m_live_sp = ValueObjectConstResult::Create(exe_scope,
832                                                             m_type,
833                                                             name,
834                                                             address,
835                                                             eAddressTypeLoad,
836                                                             map.GetAddressByteSize());
837         }
838         
839         ret->ValueUpdated();
840         
841         const size_t pvar_byte_size = ret->GetByteSize();
842         uint8_t *pvar_data = ret->GetValueBytes();
843                 
844         map.ReadMemory(pvar_data, address, pvar_byte_size, read_error);
845         
846         if (!read_error.Success())
847         {
848             err.SetErrorString("Couldn't dematerialize a result variable: couldn't read its memory");
849             return;
850         }
851                 
852         result_variable_sp = ret;
853         
854         if (!can_persist || !m_keep_in_memory)
855         {
856             ret->m_flags |= ClangExpressionVariable::EVNeedsAllocation;
857             
858             if (m_temporary_allocation != LLDB_INVALID_ADDRESS)
859             {
860                 Error free_error;
861                 map.Free(m_temporary_allocation, free_error);
862             }
863         }
864         else
865         {
866             ret->m_flags |= ClangExpressionVariable::EVIsLLDBAllocated;
867         }
868         
869         m_temporary_allocation = LLDB_INVALID_ADDRESS;
870         m_temporary_allocation_size = 0;
871     }
872     
873     void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
874     {
875         StreamString dump_stream;
876         
877         const lldb::addr_t load_addr = process_address + m_offset;
878
879         dump_stream.Printf("0x%" PRIx64 ": EntityResultVariable\n", load_addr);
880         
881         Error err;
882         
883         lldb::addr_t ptr = LLDB_INVALID_ADDRESS;
884         
885         {
886             dump_stream.Printf("Pointer:\n");
887             
888             DataBufferHeap data (m_size, 0);
889             
890             map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
891             
892             if (!err.Success())
893             {
894                 dump_stream.Printf("  <could not be read>\n");
895             }
896             else
897             {
898                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
899                 
900                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
901                 
902                 lldb::offset_t offset;
903                 
904                 ptr = extractor.GetPointer(&offset);
905                 
906                 dump_stream.PutChar('\n');
907             }
908         }
909                 
910         if (m_temporary_allocation == LLDB_INVALID_ADDRESS)
911         {
912             dump_stream.Printf("Points to process memory:\n");
913         }
914         else
915         {
916             dump_stream.Printf("Temporary allocation:\n");
917         }
918         
919         if (ptr == LLDB_INVALID_ADDRESS)
920         {
921             dump_stream.Printf("  <could not be be found>\n");
922         }
923         else
924         {
925             DataBufferHeap data (m_temporary_allocation_size, 0);
926             
927             map.ReadMemory(data.GetBytes(), m_temporary_allocation, m_temporary_allocation_size, err);
928             
929             if (!err.Success())
930             {
931                 dump_stream.Printf("  <could not be read>\n");
932             }
933             else
934             {
935                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
936                 
937                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
938                 
939                 dump_stream.PutChar('\n');
940             }
941         }
942         
943         log->PutCString(dump_stream.GetData());
944     }
945     
946     void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
947     {
948         if (!m_keep_in_memory && m_temporary_allocation != LLDB_INVALID_ADDRESS)
949         {
950             Error free_error;
951             
952             map.Free(m_temporary_allocation, free_error);
953         }
954         
955         m_temporary_allocation = LLDB_INVALID_ADDRESS;
956         m_temporary_allocation_size = 0;
957     }
958 private:
959     TypeFromUser    m_type;
960     bool            m_is_program_reference;
961     bool            m_keep_in_memory;
962     
963     lldb::addr_t    m_temporary_allocation;
964     size_t          m_temporary_allocation_size;
965 };
966
967 uint32_t
968 Materializer::AddResultVariable (const TypeFromUser &type, bool is_program_reference, bool keep_in_memory, Error &err)
969 {
970     EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
971     iter->reset (new EntityResultVariable (type, is_program_reference, keep_in_memory));
972     uint32_t ret = AddStructMember(**iter);
973     (*iter)->SetOffset(ret);
974     m_result_entity = iter->get();
975     return ret;
976 }
977
978 class EntitySymbol : public Materializer::Entity
979 {
980 public:
981     EntitySymbol (const Symbol &symbol) :
982         Entity(),
983         m_symbol(symbol)
984     {
985         // Hard-coding to maximum size of a symbol
986         m_size = 8;
987         m_alignment = 8;
988     }
989     
990     void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
991     {
992         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
993
994         const lldb::addr_t load_addr = process_address + m_offset;
995
996         if (log)
997         {
998             log->Printf("EntitySymbol::Materialize [address = 0x%" PRIx64 ", m_symbol = %s]",
999                         (uint64_t)load_addr,
1000                         m_symbol.GetName().AsCString());
1001         }
1002         
1003         Address &sym_address = m_symbol.GetAddress();
1004
1005         ExecutionContextScope *exe_scope = map.GetBestExecutionContextScope();
1006         
1007         lldb::TargetSP target_sp;
1008         
1009         if (exe_scope)
1010             target_sp = map.GetBestExecutionContextScope()->CalculateTarget();
1011         
1012         if (!target_sp)
1013         {
1014             err.SetErrorStringWithFormat("couldn't resolve symbol %s because there is no target", m_symbol.GetName().AsCString());
1015             return;
1016         }
1017         
1018         lldb::addr_t resolved_address = sym_address.GetLoadAddress(target_sp.get());
1019         
1020         if (resolved_address == LLDB_INVALID_ADDRESS)
1021             resolved_address = sym_address.GetFileAddress();
1022         
1023         Error pointer_write_error;
1024         
1025         map.WritePointerToMemory(load_addr, resolved_address, pointer_write_error);
1026         
1027         if (!pointer_write_error.Success())
1028         {
1029             err.SetErrorStringWithFormat("couldn't write the address of symbol %s: %s", m_symbol.GetName().AsCString(), pointer_write_error.AsCString());
1030             return;
1031         }
1032     }
1033     
1034     void Dematerialize (lldb::StackFrameSP &frame_sp,
1035                         IRMemoryMap &map,
1036                         lldb::addr_t process_address,
1037                         lldb::addr_t frame_top,
1038                         lldb::addr_t frame_bottom,
1039                         Error &err)
1040     {
1041         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1042
1043         const lldb::addr_t load_addr = process_address + m_offset;
1044
1045         if (log)
1046         {
1047             log->Printf("EntitySymbol::Dematerialize [address = 0x%" PRIx64 ", m_symbol = %s]",
1048                         (uint64_t)load_addr,
1049                         m_symbol.GetName().AsCString());
1050         }
1051         
1052         // no work needs to be done
1053     }
1054     
1055     void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
1056     {
1057         StreamString dump_stream;
1058         
1059         Error err;
1060         
1061         const lldb::addr_t load_addr = process_address + m_offset;
1062
1063         dump_stream.Printf("0x%" PRIx64 ": EntitySymbol (%s)\n", load_addr, m_symbol.GetName().AsCString());
1064         
1065         {
1066             dump_stream.Printf("Pointer:\n");
1067             
1068             DataBufferHeap data (m_size, 0);
1069             
1070             map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1071             
1072             if (!err.Success())
1073             {
1074                 dump_stream.Printf("  <could not be read>\n");
1075             }
1076             else
1077             {
1078                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1079                 
1080                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1081                 
1082                 dump_stream.PutChar('\n');
1083             }
1084         }
1085         
1086         log->PutCString(dump_stream.GetData());
1087     }
1088     
1089     void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
1090     {
1091     }
1092 private:
1093     Symbol m_symbol;
1094 };
1095
1096 uint32_t
1097 Materializer::AddSymbol (const Symbol &symbol_sp, Error &err)
1098 {
1099     EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1100     iter->reset (new EntitySymbol (symbol_sp));
1101     uint32_t ret = AddStructMember(**iter);
1102     (*iter)->SetOffset(ret);
1103     return ret;
1104 }
1105
1106 class EntityRegister : public Materializer::Entity
1107 {
1108 public:
1109     EntityRegister (const RegisterInfo &register_info) :
1110         Entity(),
1111         m_register_info(register_info)
1112     {
1113         // Hard-coding alignment conservatively
1114         m_size = m_register_info.byte_size;
1115         m_alignment = m_register_info.byte_size;
1116     }
1117     
1118     void Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &err)
1119     {
1120         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1121         
1122         const lldb::addr_t load_addr = process_address + m_offset;
1123
1124         if (log)
1125         {
1126             log->Printf("EntityRegister::Materialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1127                         (uint64_t)load_addr,
1128                         m_register_info.name);
1129         }
1130
1131         RegisterValue reg_value;
1132         
1133         if (!frame_sp.get())
1134         {
1135             err.SetErrorStringWithFormat("couldn't materialize register %s without a stack frame", m_register_info.name);
1136             return;
1137         }
1138         
1139         lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1140         
1141         if (!reg_context_sp->ReadRegister(&m_register_info, reg_value))
1142         {
1143             err.SetErrorStringWithFormat("couldn't read the value of register %s", m_register_info.name);
1144             return;
1145         }
1146         
1147         DataExtractor register_data;
1148         
1149         if (!reg_value.GetData(register_data))
1150         {
1151             err.SetErrorStringWithFormat("couldn't get the data for register %s", m_register_info.name);
1152             return;
1153         }
1154         
1155         if (register_data.GetByteSize() != m_register_info.byte_size)
1156         {
1157             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);
1158             return;
1159         }
1160         
1161         m_register_contents.reset(new DataBufferHeap(register_data.GetDataStart(), register_data.GetByteSize()));
1162         
1163         Error write_error;
1164         
1165         map.WriteMemory(load_addr, register_data.GetDataStart(), register_data.GetByteSize(), write_error);
1166         
1167         if (!write_error.Success())
1168         {
1169             err.SetErrorStringWithFormat("couldn't write the contents of register %s: %s", m_register_info.name, write_error.AsCString());
1170             return;
1171         }
1172     }
1173     
1174     void Dematerialize (lldb::StackFrameSP &frame_sp,
1175                         IRMemoryMap &map,
1176                         lldb::addr_t process_address,
1177                         lldb::addr_t frame_top,
1178                         lldb::addr_t frame_bottom,
1179                         Error &err)
1180     {
1181         Log *log(lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS));
1182         
1183         const lldb::addr_t load_addr = process_address + m_offset;
1184
1185         if (log)
1186         {
1187             log->Printf("EntityRegister::Dematerialize [address = 0x%" PRIx64 ", m_register_info = %s]",
1188                         (uint64_t)load_addr,
1189                         m_register_info.name);
1190         }
1191         
1192         Error extract_error;
1193         
1194         DataExtractor register_data;
1195         
1196         if (!frame_sp.get())
1197         {
1198             err.SetErrorStringWithFormat("couldn't dematerialize register %s without a stack frame", m_register_info.name);
1199             return;
1200         }
1201         
1202         lldb::RegisterContextSP reg_context_sp = frame_sp->GetRegisterContext();
1203         
1204         map.GetMemoryData(register_data, load_addr, m_register_info.byte_size, extract_error);
1205         
1206         if (!extract_error.Success())
1207         {
1208             err.SetErrorStringWithFormat("couldn't get the data for register %s: %s", m_register_info.name, extract_error.AsCString());
1209             return;
1210         }
1211         
1212         if (!memcmp(register_data.GetDataStart(), m_register_contents->GetBytes(), register_data.GetByteSize()))
1213         {
1214             // No write required, and in particular we avoid errors if the register wasn't writable
1215             
1216             m_register_contents.reset();
1217             return;
1218         }
1219         
1220         m_register_contents.reset();
1221         
1222         RegisterValue register_value (const_cast<uint8_t*>(register_data.GetDataStart()), register_data.GetByteSize(), register_data.GetByteOrder());
1223         
1224         if (!reg_context_sp->WriteRegister(&m_register_info, register_value))
1225         {
1226             err.SetErrorStringWithFormat("couldn't write the value of register %s", m_register_info.name);
1227             return;
1228         }
1229     }
1230     
1231     void DumpToLog (IRMemoryMap &map, lldb::addr_t process_address, Log *log)
1232     {
1233         StreamString dump_stream;
1234         
1235         Error err;
1236         
1237         const lldb::addr_t load_addr = process_address + m_offset;
1238
1239         
1240         dump_stream.Printf("0x%" PRIx64 ": EntityRegister (%s)\n", load_addr, m_register_info.name);
1241         
1242         {
1243             dump_stream.Printf("Value:\n");
1244             
1245             DataBufferHeap data (m_size, 0);
1246             
1247             map.ReadMemory(data.GetBytes(), load_addr, m_size, err);
1248             
1249             if (!err.Success())
1250             {
1251                 dump_stream.Printf("  <could not be read>\n");
1252             }
1253             else
1254             {
1255                 DataExtractor extractor (data.GetBytes(), data.GetByteSize(), map.GetByteOrder(), map.GetAddressByteSize());
1256                 
1257                 extractor.DumpHexBytes(&dump_stream, data.GetBytes(), data.GetByteSize(), 16, load_addr);
1258                 
1259                 dump_stream.PutChar('\n');
1260             }
1261         }
1262         
1263         log->PutCString(dump_stream.GetData());
1264     }
1265     
1266     void Wipe (IRMemoryMap &map, lldb::addr_t process_address)
1267     {
1268     }
1269 private:
1270     RegisterInfo m_register_info;
1271     lldb::DataBufferSP m_register_contents;
1272 };
1273
1274 uint32_t
1275 Materializer::AddRegister (const RegisterInfo &register_info, Error &err)
1276 {
1277     EntityVector::iterator iter = m_entities.insert(m_entities.end(), EntityUP());
1278     iter->reset (new EntityRegister (register_info));
1279     uint32_t ret = AddStructMember(**iter);
1280     (*iter)->SetOffset(ret);
1281     return ret;
1282 }
1283
1284 Materializer::Materializer () :
1285     m_dematerializer_wp(),
1286     m_result_entity(NULL),
1287     m_current_offset(0),
1288     m_struct_alignment(8)
1289 {
1290 }
1291
1292 Materializer::~Materializer ()
1293 {
1294     DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1295     
1296     if (dematerializer_sp)
1297         dematerializer_sp->Wipe();
1298 }
1299
1300 Materializer::DematerializerSP
1301 Materializer::Materialize (lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Error &error)
1302 {
1303     ExecutionContextScope *exe_scope = frame_sp.get();
1304         
1305     if (!exe_scope)
1306         exe_scope = map.GetBestExecutionContextScope();
1307     
1308     DematerializerSP dematerializer_sp = m_dematerializer_wp.lock();
1309     
1310     if (dematerializer_sp)
1311     {
1312         error.SetErrorToGenericError();
1313         error.SetErrorString("Couldn't materialize: already materialized");
1314     }
1315     
1316     DematerializerSP ret(new Dematerializer(*this, frame_sp, map, process_address));
1317     
1318     if (!exe_scope)
1319     {
1320         error.SetErrorToGenericError();
1321         error.SetErrorString("Couldn't materialize: target doesn't exist");
1322     }
1323     
1324     for (EntityUP &entity_up : m_entities)
1325     {
1326         entity_up->Materialize(frame_sp, map, process_address, error);
1327         
1328         if (!error.Success())
1329             return DematerializerSP();
1330     }
1331     
1332     if (Log *log = lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1333     {
1334         log->Printf("Materializer::Materialize (frame_sp = %p, process_address = 0x%" PRIx64 ") materialized:", frame_sp.get(), process_address);
1335         for (EntityUP &entity_up : m_entities)
1336             entity_up->DumpToLog(map, process_address, log);
1337     }
1338     
1339     m_dematerializer_wp = ret;
1340     
1341     return ret;
1342 }
1343
1344 void
1345 Materializer::Dematerializer::Dematerialize (Error &error, lldb::ClangExpressionVariableSP &result_sp, lldb::addr_t frame_bottom, lldb::addr_t frame_top)
1346 {
1347     lldb::StackFrameSP frame_sp;
1348
1349     lldb::ThreadSP thread_sp = m_thread_wp.lock();
1350     if (thread_sp)
1351         frame_sp = thread_sp->GetFrameWithStackID(m_stack_id);
1352     
1353     ExecutionContextScope *exe_scope = m_map->GetBestExecutionContextScope();
1354     
1355     if (!IsValid())
1356     {
1357         error.SetErrorToGenericError();
1358         error.SetErrorString("Couldn't dematerialize: invalid dematerializer");
1359     }
1360     
1361     if (!exe_scope)
1362     {
1363         error.SetErrorToGenericError();
1364         error.SetErrorString("Couldn't dematerialize: target is gone");
1365     }
1366     else
1367     {
1368         if (Log *log =lldb_private::GetLogIfAllCategoriesSet (LIBLLDB_LOG_EXPRESSIONS))
1369         {
1370             log->Printf("Materializer::Dematerialize (frame_sp = %p, process_address = 0x%" PRIx64 ") about to dematerialize:", frame_sp.get(), m_process_address);
1371             for (EntityUP &entity_up : m_materializer->m_entities)
1372                 entity_up->DumpToLog(*m_map, m_process_address, log);
1373         }
1374         
1375         for (EntityUP &entity_up : m_materializer->m_entities)
1376         {
1377             if (entity_up.get() == m_materializer->m_result_entity)
1378             {
1379                 static_cast<EntityResultVariable*>(m_materializer->m_result_entity)->Dematerialize (result_sp, frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
1380             }
1381             else
1382             {
1383                 entity_up->Dematerialize (frame_sp, *m_map, m_process_address, frame_top, frame_bottom, error);
1384             }
1385                 
1386             if (!error.Success())
1387                 break;
1388         }
1389     }
1390     
1391     Wipe();
1392 }
1393
1394 void
1395 Materializer::Dematerializer::Wipe ()
1396 {
1397     if (!IsValid())
1398         return;
1399     
1400     for (EntityUP &entity_up : m_materializer->m_entities)
1401     {
1402         entity_up->Wipe (*m_map, m_process_address);
1403     }
1404
1405     m_materializer = NULL;
1406     m_map = NULL;
1407     m_process_address = LLDB_INVALID_ADDRESS;
1408 }