1 //===-- Materializer.h ------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef liblldb_Materializer_h
10 #define liblldb_Materializer_h
11 
12 #include <memory>
13 #include <vector>
14 
15 #include "lldb/Expression/IRMemoryMap.h"
16 #include "lldb/Symbol/TaggedASTType.h"
17 #include "lldb/Target/StackFrame.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/lldb-private-types.h"
20 
21 namespace lldb_private {
22 
23 class Materializer {
24 public:
25   Materializer();
26   ~Materializer();
27 
28   class Dematerializer {
29   public:
30     Dematerializer()
31         : m_materializer(nullptr), m_map(nullptr),
32           m_process_address(LLDB_INVALID_ADDRESS) {}
33 
34     ~Dematerializer() { Wipe(); }
35 
36     void Dematerialize(Status &err, lldb::addr_t frame_top,
37                        lldb::addr_t frame_bottom);
38 
39     void Wipe();
40 
41     bool IsValid() {
42       return m_materializer && m_map &&
43              (m_process_address != LLDB_INVALID_ADDRESS);
44     }
45 
46   private:
47     friend class Materializer;
48 
49     Dematerializer(Materializer &materializer, lldb::StackFrameSP &frame_sp,
50                    IRMemoryMap &map, lldb::addr_t process_address)
51         : m_materializer(&materializer), m_map(&map),
52           m_process_address(process_address) {
53       if (frame_sp) {
54         m_thread_wp = frame_sp->GetThread();
55         m_stack_id = frame_sp->GetStackID();
56       }
57     }
58 
59     Materializer *m_materializer;
60     lldb::ThreadWP m_thread_wp;
61     StackID m_stack_id;
62     IRMemoryMap *m_map;
63     lldb::addr_t m_process_address;
64   };
65 
66   typedef std::shared_ptr<Dematerializer> DematerializerSP;
67   typedef std::weak_ptr<Dematerializer> DematerializerWP;
68 
69   DematerializerSP Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
70                                lldb::addr_t process_address, Status &err);
71 
72   class PersistentVariableDelegate {
73   public:
74     virtual ~PersistentVariableDelegate();
75     virtual ConstString GetName() = 0;
76     virtual void DidDematerialize(lldb::ExpressionVariableSP &variable) = 0;
77   };
78 
79   uint32_t
80   AddPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp,
81                         PersistentVariableDelegate *delegate, Status &err);
82   uint32_t AddVariable(lldb::VariableSP &variable_sp, Status &err);
83   uint32_t AddResultVariable(const CompilerType &type, bool is_lvalue,
84                              bool keep_in_memory,
85                              PersistentVariableDelegate *delegate, Status &err);
86   uint32_t AddSymbol(const Symbol &symbol_sp, Status &err);
87   uint32_t AddRegister(const RegisterInfo &register_info, Status &err);
88 
89   uint32_t GetStructAlignment() { return m_struct_alignment; }
90 
91   uint32_t GetStructByteSize() { return m_current_offset; }
92 
93   class Entity {
94   public:
95     Entity() : m_alignment(1), m_size(0), m_offset(0) {}
96 
97     virtual ~Entity() = default;
98 
99     virtual void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
100                              lldb::addr_t process_address, Status &err) = 0;
101     virtual void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
102                                lldb::addr_t process_address,
103                                lldb::addr_t frame_top,
104                                lldb::addr_t frame_bottom, Status &err) = 0;
105     virtual void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
106                            Log *log) = 0;
107     virtual void Wipe(IRMemoryMap &map, lldb::addr_t process_address) = 0;
108 
109     uint32_t GetAlignment() { return m_alignment; }
110 
111     uint32_t GetSize() { return m_size; }
112 
113     uint32_t GetOffset() { return m_offset; }
114 
115     void SetOffset(uint32_t offset) { m_offset = offset; }
116 
117   protected:
118     uint32_t m_alignment;
119     uint32_t m_size;
120     uint32_t m_offset;
121   };
122 
123 private:
124   uint32_t AddStructMember(Entity &entity);
125 
126   typedef std::unique_ptr<Entity> EntityUP;
127   typedef std::vector<EntityUP> EntityVector;
128 
129   DematerializerWP m_dematerializer_wp;
130   EntityVector m_entities;
131   uint32_t m_current_offset;
132   uint32_t m_struct_alignment;
133 };
134 
135 } // namespace lldb_private
136 
137 #endif // liblldb_Materializer_h
138