LLDB mainline
Materializer.h
Go to the documentation of this file.
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 LLDB_EXPRESSION_MATERIALIZER_H
10#define LLDB_EXPRESSION_MATERIALIZER_H
11
12#include <memory>
13#include <vector>
14
19#include "lldb/Utility/Status.h"
21
22namespace lldb_private {
23
24/// Materializer packs the variables an expression reads and writes into a
25/// single argument struct, and unpacks them again afterward.
26///
27/// Expressions can't have real addresses for the variables they touch baked
28/// into their IR, since some of those addresses (e.g. frame-relative locals)
29/// aren't known until the moment of execution. IRForTarget rewrites such
30/// accesses into reads from one struct instead, registering an Entity per
31/// variable via AddVariable/AddPersistentVariable/AddResultVariable.
32/// Materialize() lays out and populates that struct against a target
33/// IRMemoryMap, independent of whether the expression is later interpreted or
34/// JIT'd and run; the returned Dematerializer reverses the process afterward,
35/// copying side effects back out and extracting the result.
37public:
38 Materializer() = default;
40
42 public:
43 Dematerializer() = default;
44
46
47 void Dematerialize(Status &err, lldb::addr_t frame_top,
48 lldb::addr_t frame_bottom);
49
50 void Wipe();
51
52 bool IsValid() {
53 return m_materializer && m_map &&
55 }
56
57 private:
58 friend class Materializer;
59
61 IRMemoryMap &map, lldb::addr_t process_address)
62 : m_materializer(&materializer), m_map(&map),
63 m_process_address(process_address) {
64 if (frame_sp) {
65 m_thread_wp = frame_sp->GetThread();
66 m_stack_id = frame_sp->GetStackID();
67 }
68 }
69
73 IRMemoryMap *m_map = nullptr;
75 };
76
77 typedef std::shared_ptr<Dematerializer> DematerializerSP;
78 typedef std::weak_ptr<Dematerializer> DematerializerWP;
79
81 lldb::addr_t process_address, Status &err);
82
90
91 uint32_t
93 PersistentVariableDelegate *delegate, Status &err);
94 uint32_t AddVariable(lldb::VariableSP &variable_sp, Status &err);
95
96 /// Create entity from supplied ValueObject and count it as a member
97 /// of the materialized struct.
98 ///
99 /// Behaviour is undefined if 'valobj_provider' is empty.
100 ///
101 /// \param[in] name Name of variable to materialize
102 ///
103 /// \param[in] valobj_provider When materializing values multiple
104 /// times, this callback gets used to fetch a fresh
105 /// ValueObject corresponding to the supplied frame.
106 /// This is mainly used for conditional breakpoints
107 /// that re-apply an expression whatever the frame
108 /// happens to be when the breakpoint got hit.
109 ///
110 /// \param[out] err Error status that gets set on error.
111 ///
112 /// \returns Offset in bytes of the member we just added to the
113 /// materialized struct.
114 uint32_t AddValueObject(ConstString name,
115 ValueObjectProviderTy valobj_provider, Status &err);
116
117 uint32_t AddResultVariable(const CompilerType &type, bool is_lvalue,
118 bool keep_in_memory,
119 PersistentVariableDelegate *delegate, Status &err);
120 uint32_t AddSymbol(const Symbol &symbol_sp, Status &err);
121 uint32_t AddRegister(const RegisterInfo &register_info, Status &err);
122
124
125 uint32_t GetStructByteSize() { return m_current_offset; }
126
127 class Entity {
128 public:
129 Entity() = default;
130
131 virtual ~Entity() = default;
132
133 virtual void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
134 lldb::addr_t process_address, Status &err) = 0;
135 virtual void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map,
136 lldb::addr_t process_address,
137 lldb::addr_t frame_top,
138 lldb::addr_t frame_bottom, Status &err) = 0;
139 virtual void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address,
140 Log *log) = 0;
141 virtual void Wipe(IRMemoryMap &map, lldb::addr_t process_address) = 0;
142
143 uint32_t GetAlignment() { return m_alignment; }
144
145 uint32_t GetSize() { return m_size; }
146
147 uint32_t GetOffset() { return m_offset; }
148
149 void SetOffset(uint32_t offset) { m_offset = offset; }
150
151 protected:
152 uint32_t m_alignment = 1;
153 uint32_t m_size = 0;
154 uint32_t m_offset = 0;
155 };
156
157private:
158 uint32_t AddStructMember(Entity &entity);
159
160 typedef std::unique_ptr<Entity> EntityUP;
161 typedef std::vector<EntityUP> EntityVector;
162
165 uint32_t m_current_offset = 0;
166 uint32_t m_struct_alignment = 8;
167};
168
169} // namespace lldb_private
170
171#endif // LLDB_EXPRESSION_MATERIALIZER_H
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
Encapsulates memory that may exist in the process but must also be available in the host process.
Definition IRMemoryMap.h:35
Dematerializer(Materializer &materializer, lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address)
void Dematerialize(Status &err, lldb::addr_t frame_top, lldb::addr_t frame_bottom)
virtual void Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Status &err)=0
void SetOffset(uint32_t offset)
virtual void Wipe(IRMemoryMap &map, lldb::addr_t process_address)=0
virtual void Dematerialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, lldb::addr_t frame_top, lldb::addr_t frame_bottom, Status &err)=0
virtual void DumpToLog(IRMemoryMap &map, lldb::addr_t process_address, Log *log)=0
virtual void DidDematerialize(lldb::ExpressionVariableSP &variable)=0
uint32_t AddResultVariable(const CompilerType &type, bool is_lvalue, bool keep_in_memory, PersistentVariableDelegate *delegate, Status &err)
uint32_t AddStructMember(Entity &entity)
std::weak_ptr< Dematerializer > DematerializerWP
std::unique_ptr< Entity > EntityUP
std::vector< EntityUP > EntityVector
std::shared_ptr< Dematerializer > DematerializerSP
DematerializerSP Materialize(lldb::StackFrameSP &frame_sp, IRMemoryMap &map, lldb::addr_t process_address, Status &err)
uint32_t AddSymbol(const Symbol &symbol_sp, Status &err)
uint32_t AddRegister(const RegisterInfo &register_info, Status &err)
DematerializerWP m_dematerializer_wp
uint32_t AddValueObject(ConstString name, ValueObjectProviderTy valobj_provider, Status &err)
Create entity from supplied ValueObject and count it as a member of the materialized struct.
uint32_t AddPersistentVariable(lldb::ExpressionVariableSP &persistent_variable_sp, PersistentVariableDelegate *delegate, Status &err)
uint32_t AddVariable(lldb::VariableSP &variable_sp, Status &err)
An error handling class.
Definition Status.h:118
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
std::function< lldb::ValueObjectSP(ConstString, StackFrame *)> ValueObjectProviderTy
Functor that returns a ValueObjectSP for a variable given its name and the StackFrame of interest.
std::shared_ptr< lldb_private::StackFrame > StackFrameSP
std::shared_ptr< lldb_private::ExpressionVariable > ExpressionVariableSP
std::shared_ptr< lldb_private::Variable > VariableSP
uint64_t addr_t
Definition lldb-types.h:80
std::weak_ptr< lldb_private::Thread > ThreadWP
Every register is described in detail including its name, alternate name (optional),...