1*0b57cec5SDimitry Andric //===-- ValueObjectMemory.cpp ---------------------------------------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric 
9*0b57cec5SDimitry Andric #include "lldb/Core/ValueObjectMemory.h"
10*0b57cec5SDimitry Andric #include "lldb/Core/Value.h"
11*0b57cec5SDimitry Andric #include "lldb/Core/ValueObject.h"
12*0b57cec5SDimitry Andric #include "lldb/Symbol/Type.h"
13*0b57cec5SDimitry Andric #include "lldb/Target/ExecutionContext.h"
14*0b57cec5SDimitry Andric #include "lldb/Target/Target.h"
15*0b57cec5SDimitry Andric #include "lldb/Utility/DataExtractor.h"
16*0b57cec5SDimitry Andric #include "lldb/Utility/Scalar.h"
17*0b57cec5SDimitry Andric #include "lldb/Utility/Status.h"
18*0b57cec5SDimitry Andric #include "lldb/lldb-types.h"
19*0b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
20*0b57cec5SDimitry Andric 
21*0b57cec5SDimitry Andric #include <cassert>
22*0b57cec5SDimitry Andric #include <memory>
23*0b57cec5SDimitry Andric #include <optional>
24*0b57cec5SDimitry Andric 
25*0b57cec5SDimitry Andric namespace lldb_private {
26*0b57cec5SDimitry Andric class ExecutionContextScope;
27*0b57cec5SDimitry Andric }
28*0b57cec5SDimitry Andric 
29*0b57cec5SDimitry Andric using namespace lldb;
30*0b57cec5SDimitry Andric using namespace lldb_private;
31*0b57cec5SDimitry Andric 
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)32*0b57cec5SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
33*0b57cec5SDimitry Andric                                         llvm::StringRef name,
34*0b57cec5SDimitry Andric                                         const Address &address,
35*0b57cec5SDimitry Andric                                         lldb::TypeSP &type_sp) {
36*0b57cec5SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
37*0b57cec5SDimitry Andric   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
38*0b57cec5SDimitry Andric       ->GetSP();
39*0b57cec5SDimitry Andric }
40*0b57cec5SDimitry Andric 
Create(ExecutionContextScope * exe_scope,llvm::StringRef name,const Address & address,const CompilerType & ast_type)41*0b57cec5SDimitry Andric ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
42*0b57cec5SDimitry Andric                                         llvm::StringRef name,
43*0b57cec5SDimitry Andric                                         const Address &address,
44*0b57cec5SDimitry Andric                                         const CompilerType &ast_type) {
45*0b57cec5SDimitry Andric   auto manager_sp = ValueObjectManager::Create();
46*0b57cec5SDimitry Andric   return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
47*0b57cec5SDimitry Andric                                 ast_type))
48*0b57cec5SDimitry Andric       ->GetSP();
49*0b57cec5SDimitry Andric }
50*0b57cec5SDimitry Andric 
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,lldb::TypeSP & type_sp)51*0b57cec5SDimitry Andric ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
52*0b57cec5SDimitry Andric                                      ValueObjectManager &manager,
53*0b57cec5SDimitry Andric                                      llvm::StringRef name,
54*0b57cec5SDimitry Andric                                      const Address &address,
55*0b57cec5SDimitry Andric                                      lldb::TypeSP &type_sp)
56*0b57cec5SDimitry Andric     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
57*0b57cec5SDimitry Andric       m_compiler_type() {
58*0b57cec5SDimitry Andric   // Do not attempt to construct one of these objects with no variable!
59*0b57cec5SDimitry Andric   assert(m_type_sp.get() != nullptr);
60*0b57cec5SDimitry Andric   SetName(ConstString(name));
61*0b57cec5SDimitry Andric   m_value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
62*0b57cec5SDimitry Andric   TargetSP target_sp(GetTargetSP());
63*0b57cec5SDimitry Andric   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
64*0b57cec5SDimitry Andric   if (load_address != LLDB_INVALID_ADDRESS) {
65*0b57cec5SDimitry Andric     m_value.SetValueType(Value::ValueType::LoadAddress);
66*0b57cec5SDimitry Andric     m_value.GetScalar() = load_address;
67*0b57cec5SDimitry Andric   } else {
68*0b57cec5SDimitry Andric     lldb::addr_t file_address = m_address.GetFileAddress();
69*0b57cec5SDimitry Andric     if (file_address != LLDB_INVALID_ADDRESS) {
70*0b57cec5SDimitry Andric       m_value.SetValueType(Value::ValueType::FileAddress);
71*0b57cec5SDimitry Andric       m_value.GetScalar() = file_address;
72*0b57cec5SDimitry Andric     } else {
73*0b57cec5SDimitry Andric       m_value.GetScalar() = m_address.GetOffset();
74*0b57cec5SDimitry Andric       m_value.SetValueType(Value::ValueType::Scalar);
75*0b57cec5SDimitry Andric     }
76*0b57cec5SDimitry Andric   }
77*0b57cec5SDimitry Andric }
78*0b57cec5SDimitry Andric 
ValueObjectMemory(ExecutionContextScope * exe_scope,ValueObjectManager & manager,llvm::StringRef name,const Address & address,const CompilerType & ast_type)79*0b57cec5SDimitry Andric ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
80*0b57cec5SDimitry Andric                                      ValueObjectManager &manager,
81*0b57cec5SDimitry Andric                                      llvm::StringRef name,
82*0b57cec5SDimitry Andric                                      const Address &address,
83*0b57cec5SDimitry Andric                                      const CompilerType &ast_type)
84*0b57cec5SDimitry Andric     : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
85*0b57cec5SDimitry Andric       m_compiler_type(ast_type) {
86*0b57cec5SDimitry Andric   // Do not attempt to construct one of these objects with no variable!
87*0b57cec5SDimitry Andric   assert(m_compiler_type.IsValid());
88*0b57cec5SDimitry Andric 
89*0b57cec5SDimitry Andric   TargetSP target_sp(GetTargetSP());
90*0b57cec5SDimitry Andric 
91*0b57cec5SDimitry Andric   SetName(ConstString(name));
92*0b57cec5SDimitry Andric   m_value.SetCompilerType(m_compiler_type);
93*0b57cec5SDimitry Andric   lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
94*0b57cec5SDimitry Andric   if (load_address != LLDB_INVALID_ADDRESS) {
95*0b57cec5SDimitry Andric     m_value.SetValueType(Value::ValueType::LoadAddress);
96*0b57cec5SDimitry Andric     m_value.GetScalar() = load_address;
97*0b57cec5SDimitry Andric   } else {
98*0b57cec5SDimitry Andric     lldb::addr_t file_address = m_address.GetFileAddress();
99*0b57cec5SDimitry Andric     if (file_address != LLDB_INVALID_ADDRESS) {
100*0b57cec5SDimitry Andric       m_value.SetValueType(Value::ValueType::FileAddress);
101*0b57cec5SDimitry Andric       m_value.GetScalar() = file_address;
102*0b57cec5SDimitry Andric     } else {
103*0b57cec5SDimitry Andric       m_value.GetScalar() = m_address.GetOffset();
104*0b57cec5SDimitry Andric       m_value.SetValueType(Value::ValueType::Scalar);
105*0b57cec5SDimitry Andric     }
106*0b57cec5SDimitry Andric   }
107*0b57cec5SDimitry Andric }
108*0b57cec5SDimitry Andric 
109*0b57cec5SDimitry Andric ValueObjectMemory::~ValueObjectMemory() = default;
110*0b57cec5SDimitry Andric 
GetCompilerTypeImpl()111*0b57cec5SDimitry Andric CompilerType ValueObjectMemory::GetCompilerTypeImpl() {
112*0b57cec5SDimitry Andric   if (m_type_sp)
113*0b57cec5SDimitry Andric     return m_type_sp->GetForwardCompilerType();
114*0b57cec5SDimitry Andric   return m_compiler_type;
115*0b57cec5SDimitry Andric }
116*0b57cec5SDimitry Andric 
GetTypeName()117*0b57cec5SDimitry Andric ConstString ValueObjectMemory::GetTypeName() {
118*0b57cec5SDimitry Andric   if (m_type_sp)
119*0b57cec5SDimitry Andric     return m_type_sp->GetName();
120*0b57cec5SDimitry Andric   return m_compiler_type.GetTypeName();
121*0b57cec5SDimitry Andric }
122*0b57cec5SDimitry Andric 
GetDisplayTypeName()123*0b57cec5SDimitry Andric ConstString ValueObjectMemory::GetDisplayTypeName() {
124*0b57cec5SDimitry Andric   if (m_type_sp)
125*0b57cec5SDimitry Andric     return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
126*0b57cec5SDimitry Andric   return m_compiler_type.GetDisplayTypeName();
127*0b57cec5SDimitry Andric }
128*0b57cec5SDimitry Andric 
CalculateNumChildren(uint32_t max)129*0b57cec5SDimitry Andric size_t ValueObjectMemory::CalculateNumChildren(uint32_t max) {
130*0b57cec5SDimitry Andric   if (m_type_sp) {
131*0b57cec5SDimitry Andric     auto child_count = m_type_sp->GetNumChildren(true);
132*0b57cec5SDimitry Andric     return child_count <= max ? child_count : max;
133*0b57cec5SDimitry Andric   }
134*0b57cec5SDimitry Andric 
135*0b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
136*0b57cec5SDimitry Andric   const bool omit_empty_base_classes = true;
137*0b57cec5SDimitry Andric   auto child_count =
138*0b57cec5SDimitry Andric       m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
139*0b57cec5SDimitry Andric   return child_count <= max ? child_count : max;
140*0b57cec5SDimitry Andric }
141*0b57cec5SDimitry Andric 
GetByteSize()142*0b57cec5SDimitry Andric std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
143*0b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
144*0b57cec5SDimitry Andric   if (m_type_sp)
145*0b57cec5SDimitry Andric     return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
146*0b57cec5SDimitry Andric   return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
147*0b57cec5SDimitry Andric }
148*0b57cec5SDimitry Andric 
GetValueType() const149*0b57cec5SDimitry Andric lldb::ValueType ValueObjectMemory::GetValueType() const {
150*0b57cec5SDimitry Andric   // RETHINK: Should this be inherited from somewhere?
151*0b57cec5SDimitry Andric   return lldb::eValueTypeVariableGlobal;
152*0b57cec5SDimitry Andric }
153*0b57cec5SDimitry Andric 
UpdateValue()154*0b57cec5SDimitry Andric bool ValueObjectMemory::UpdateValue() {
155*0b57cec5SDimitry Andric   SetValueIsValid(false);
156*0b57cec5SDimitry Andric   m_error.Clear();
157*0b57cec5SDimitry Andric 
158*0b57cec5SDimitry Andric   ExecutionContext exe_ctx(GetExecutionContextRef());
159*0b57cec5SDimitry Andric 
160*0b57cec5SDimitry Andric   Target *target = exe_ctx.GetTargetPtr();
161*0b57cec5SDimitry Andric   if (target) {
162*0b57cec5SDimitry Andric     m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
163*0b57cec5SDimitry Andric     m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
164*0b57cec5SDimitry Andric   }
165*0b57cec5SDimitry Andric 
166*0b57cec5SDimitry Andric   Value old_value(m_value);
167*0b57cec5SDimitry Andric   if (m_address.IsValid()) {
168*0b57cec5SDimitry Andric     Value::ValueType value_type = m_value.GetValueType();
169*0b57cec5SDimitry Andric 
170*0b57cec5SDimitry Andric     switch (value_type) {
171*0b57cec5SDimitry Andric     case Value::ValueType::Invalid:
172*0b57cec5SDimitry Andric       m_error.SetErrorString("Invalid value");
173*0b57cec5SDimitry Andric       return false;
174*0b57cec5SDimitry Andric     case Value::ValueType::Scalar:
175*0b57cec5SDimitry Andric       // The variable value is in the Scalar value inside the m_value. We can
176*0b57cec5SDimitry Andric       // point our m_data right to it.
177*0b57cec5SDimitry Andric       m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
178*0b57cec5SDimitry Andric       break;
179*0b57cec5SDimitry Andric 
180*0b57cec5SDimitry Andric     case Value::ValueType::FileAddress:
181*0b57cec5SDimitry Andric     case Value::ValueType::LoadAddress:
182*0b57cec5SDimitry Andric     case Value::ValueType::HostAddress:
183*0b57cec5SDimitry Andric       // The DWARF expression result was an address in the inferior process. If
184*0b57cec5SDimitry Andric       // this variable is an aggregate type, we just need the address as the
185*0b57cec5SDimitry Andric       // main value as all child variable objects will rely upon this location
186*0b57cec5SDimitry Andric       // and add an offset and then read their own values as needed. If this
187*0b57cec5SDimitry Andric       // variable is a simple type, we read all data for it into m_data. Make
188*0b57cec5SDimitry Andric       // sure this type has a value before we try and read it
189*0b57cec5SDimitry Andric 
190*0b57cec5SDimitry Andric       // If we have a file address, convert it to a load address if we can.
191*0b57cec5SDimitry Andric       if (value_type == Value::ValueType::FileAddress &&
192*0b57cec5SDimitry Andric           exe_ctx.GetProcessPtr()) {
193*0b57cec5SDimitry Andric         lldb::addr_t load_addr = m_address.GetLoadAddress(target);
194*0b57cec5SDimitry Andric         if (load_addr != LLDB_INVALID_ADDRESS) {
195*0b57cec5SDimitry Andric           m_value.SetValueType(Value::ValueType::LoadAddress);
196*0b57cec5SDimitry Andric           m_value.GetScalar() = load_addr;
197*0b57cec5SDimitry Andric         }
198*0b57cec5SDimitry Andric       }
199*0b57cec5SDimitry Andric 
200*0b57cec5SDimitry Andric       if (!CanProvideValue()) {
201*0b57cec5SDimitry Andric         // this value object represents an aggregate type whose children have
202*0b57cec5SDimitry Andric         // values, but this object does not. So we say we are changed if our
203*0b57cec5SDimitry Andric         // location has changed.
204*0b57cec5SDimitry Andric         SetValueDidChange(value_type != old_value.GetValueType() ||
205*0b57cec5SDimitry Andric                           m_value.GetScalar() != old_value.GetScalar());
206*0b57cec5SDimitry Andric       } else {
207*0b57cec5SDimitry Andric         // Copy the Value and set the context to use our Variable so it can
208*0b57cec5SDimitry Andric         // extract read its value into m_data appropriately
209*0b57cec5SDimitry Andric         Value value(m_value);
210*0b57cec5SDimitry Andric         if (m_type_sp)
211*0b57cec5SDimitry Andric           value.SetContext(Value::ContextType::LLDBType, m_type_sp.get());
212*0b57cec5SDimitry Andric         else {
213*0b57cec5SDimitry Andric           value.SetCompilerType(m_compiler_type);
214*0b57cec5SDimitry Andric         }
215*0b57cec5SDimitry Andric 
216*0b57cec5SDimitry Andric         m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
217*0b57cec5SDimitry Andric       }
218*0b57cec5SDimitry Andric       break;
219*0b57cec5SDimitry Andric     }
220*0b57cec5SDimitry Andric 
221*0b57cec5SDimitry Andric     SetValueIsValid(m_error.Success());
222*0b57cec5SDimitry Andric   }
223*0b57cec5SDimitry Andric   return m_error.Success();
224*0b57cec5SDimitry Andric }
225*0b57cec5SDimitry Andric 
IsInScope()226*0b57cec5SDimitry Andric bool ValueObjectMemory::IsInScope() {
227*0b57cec5SDimitry Andric   // FIXME: Maybe try to read the memory address, and if that works, then
228*0b57cec5SDimitry Andric   // we are in scope?
229   return true;
230 }
231 
GetModule()232 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
233