LLDB  mainline
ValueObjectMemory.cpp
Go to the documentation of this file.
1 //===-- ValueObjectMemory.cpp ---------------------------------------------===//
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 
10 #include "lldb/Core/Value.h"
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Symbol/Type.h"
14 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/Scalar.h"
17 #include "lldb/Utility/Status.h"
18 #include "lldb/lldb-types.h"
19 #include "llvm/Support/ErrorHandling.h"
20 
21 #include <cassert>
22 #include <memory>
23 
24 namespace lldb_private {
26 }
27 
28 using namespace lldb;
29 using namespace lldb_private;
30 
31 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
32  llvm::StringRef name,
33  const Address &address,
34  lldb::TypeSP &type_sp) {
35  auto manager_sp = ValueObjectManager::Create();
36  return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
37  ->GetSP();
38 }
39 
40 ValueObjectSP ValueObjectMemory::Create(ExecutionContextScope *exe_scope,
41  llvm::StringRef name,
42  const Address &address,
43  const CompilerType &ast_type) {
44  auto manager_sp = ValueObjectManager::Create();
45  return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
46  ast_type))
47  ->GetSP();
48 }
49 
50 ValueObjectMemory::ValueObjectMemory(ExecutionContextScope *exe_scope,
51  ValueObjectManager &manager,
52  llvm::StringRef name,
53  const Address &address,
54  lldb::TypeSP &type_sp)
55  : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
56  m_compiler_type() {
57  // Do not attempt to construct one of these objects with no variable!
58  assert(m_type_sp.get() != nullptr);
59  SetName(ConstString(name));
61  TargetSP target_sp(GetTargetSP());
62  lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
63  if (load_address != LLDB_INVALID_ADDRESS) {
65  m_value.GetScalar() = load_address;
66  } else {
67  lldb::addr_t file_address = m_address.GetFileAddress();
68  if (file_address != LLDB_INVALID_ADDRESS) {
70  m_value.GetScalar() = file_address;
71  } else {
74  }
75  }
76 }
77 
79  ValueObjectManager &manager,
80  llvm::StringRef name,
81  const Address &address,
82  const CompilerType &ast_type)
83  : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
84  m_compiler_type(ast_type) {
85  // Do not attempt to construct one of these objects with no variable!
88 
89  TargetSP target_sp(GetTargetSP());
90 
91  SetName(ConstString(name));
93  lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
94  if (load_address != LLDB_INVALID_ADDRESS) {
96  m_value.GetScalar() = load_address;
97  } else {
98  lldb::addr_t file_address = m_address.GetFileAddress();
99  if (file_address != LLDB_INVALID_ADDRESS) {
101  m_value.GetScalar() = file_address;
102  } else {
105  }
106  }
107 }
108 
110 
112  if (m_type_sp)
113  return m_type_sp->GetForwardCompilerType();
114  return m_compiler_type;
115 }
116 
118  if (m_type_sp)
119  return m_type_sp->GetName();
120  return m_compiler_type.GetTypeName();
121 }
122 
124  if (m_type_sp)
125  return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
127 }
128 
130  if (m_type_sp) {
131  auto child_count = m_type_sp->GetNumChildren(true);
132  return child_count <= max ? child_count : max;
133  }
134 
136  const bool omit_empty_base_classes = true;
137  auto child_count =
138  m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
139  return child_count <= max ? child_count : max;
140 }
141 
142 llvm::Optional<uint64_t> ValueObjectMemory::GetByteSize() {
144  if (m_type_sp)
145  return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
147 }
148 
150  // RETHINK: Should this be inherited from somewhere?
152 }
153 
155  SetValueIsValid(false);
156  m_error.Clear();
157 
159 
160  Target *target = exe_ctx.GetTargetPtr();
161  if (target) {
164  }
165 
166  Value old_value(m_value);
167  if (m_address.IsValid()) {
168  Value::ValueType value_type = m_value.GetValueType();
169 
170  switch (value_type) {
172  m_error.SetErrorString("Invalid value");
173  return false;
175  // The variable value is in the Scalar value inside the m_value. We can
176  // point our m_data right to it.
177  m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
178  break;
179 
183  // The DWARF expression result was an address in the inferior process. If
184  // this variable is an aggregate type, we just need the address as the
185  // main value as all child variable objects will rely upon this location
186  // and add an offset and then read their own values as needed. If this
187  // variable is a simple type, we read all data for it into m_data. Make
188  // sure this type has a value before we try and read it
189 
190  // If we have a file address, convert it to a load address if we can.
191  if (value_type == Value::ValueType::FileAddress &&
192  exe_ctx.GetProcessPtr()) {
193  lldb::addr_t load_addr = m_address.GetLoadAddress(target);
194  if (load_addr != LLDB_INVALID_ADDRESS) {
196  m_value.GetScalar() = load_addr;
197  }
198  }
199 
200  if (!CanProvideValue()) {
201  // this value object represents an aggregate type whose children have
202  // values, but this object does not. So we say we are changed if our
203  // location has changed.
204  SetValueDidChange(value_type != old_value.GetValueType() ||
205  m_value.GetScalar() != old_value.GetScalar());
206  } else {
207  // Copy the Value and set the context to use our Variable so it can
208  // extract read its value into m_data appropriately
209  Value value(m_value);
210  if (m_type_sp)
212  else {
214  }
215 
216  m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
217  }
218  break;
219  }
220 
222  }
223  return m_error.Success();
224 }
225 
227  // FIXME: Maybe try to read the memory address, and if that works, then
228  // we are in scope?
229  return true;
230 }
231 
232 lldb::ModuleSP ValueObjectMemory::GetModule() { return m_address.GetModule(); }
lldb_private::Value::ValueType::Scalar
@ Scalar
A raw scalar value.
lldb_private::ExecutionContext
Definition: ExecutionContext.h:292
lldb_private::ArchSpec::GetByteOrder
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition: ArchSpec.cpp:730
Scalar.h
lldb_private::ValueObject::CanProvideValue
virtual bool CanProvideValue()
Definition: ValueObject.cpp:3103
lldb::eValueTypeVariableGlobal
@ eValueTypeVariableGlobal
globals variable
Definition: lldb-enumerations.h:317
lldb_private::ValueObject::SetValueIsValid
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
lldb_private::ValueObjectMemory::m_address
Address m_address
The variable that this value object is based upon.
Definition: ValueObjectMemory.h:62
lldb_private::Address::IsValid
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:336
lldb_private::Value
Definition: Value.h:38
lldb_private::Value::SetValueType
void SetValueType(ValueType value_type)
Definition: Value.h:89
lldb_private::ExecutionContext::GetProcessPtr
Process * GetProcessPtr() const
Returns a pointer to the process object.
Definition: ExecutionContext.cpp:208
lldb_private::Value::ContextType::LLDBType
@ LLDBType
lldb_private::Type *.
lldb_private::Value::GetValueAsData
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:301
lldb_private::Value::ValueType::Invalid
@ Invalid
lldb_private::Value::ValueType::FileAddress
@ FileAddress
A file address value.
lldb_private::Value::GetValueType
ValueType GetValueType() const
Definition: Value.cpp:109
lldb_private::ClusterManager
Definition: SharedCluster.h:22
lldb_private::ValueObjectMemory::GetModule
lldb::ModuleSP GetModule() override
Return the module associated with this value object in case the value is from an executable file and ...
Definition: ValueObjectMemory.cpp:232
lldb_private::ValueObject::SetValueDidChange
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:976
lldb_private::ValueObjectMemory::GetValueType
lldb::ValueType GetValueType() const override
Definition: ValueObjectMemory.cpp:149
lldb_private::ValueObjectMemory::IsInScope
bool IsInScope() override
Definition: ValueObjectMemory.cpp:226
lldb_private::Value::ValueType::LoadAddress
@ LoadAddress
A load address value.
lldb_private::CompilerType::GetByteSize
llvm::Optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
Definition: CompilerType.cpp:489
lldb_private::CompilerType::GetOpaqueQualType
lldb::opaque_compiler_type_t GetOpaqueQualType() const
Definition: CompilerType.h:173
ValueObjectMemory.h
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Target
Definition: Target.h:450
lldb_private::ValueObjectMemory::GetByteSize
llvm::Optional< uint64_t > GetByteSize() override
Definition: ValueObjectMemory.cpp:142
lldb_private::Value::SetCompilerType
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:254
lldb_private::ValueObject::m_data
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
Target.h
lldb_private::DataExtractor::SetByteOrder
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
Definition: DataExtractor.h:931
lldb_private::Value::GetScalar
const Scalar & GetScalar() const
Definition: Value.h:112
lldb_private::Status::Success
bool Success() const
Test for success condition.
Definition: Status.cpp:288
lldb_private::CompilerType::GetDisplayTypeName
ConstString GetDisplayTypeName() const
Definition: CompilerType.cpp:284
lldb_private::ValueObjectMemory::CalculateNumChildren
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
Definition: ValueObjectMemory.cpp:129
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ValueObjectMemory::GetDisplayTypeName
ConstString GetDisplayTypeName() override
Definition: ValueObjectMemory.cpp:123
lldb_private::DataExtractor::SetAddressByteSize
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
Definition: DataExtractor.h:845
lldb_private::Address::GetFileAddress
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:290
Type.h
lldb_private::ValueObjectMemory::GetCompilerTypeImpl
CompilerType GetCompilerTypeImpl() override
Definition: ValueObjectMemory.cpp:111
lldb_private::ConstString
Definition: ConstString.h:40
lldb_private::ValueObjectMemory::ValueObjectMemory
ValueObjectMemory(ExecutionContextScope *exe_scope, ValueObjectManager &manager, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp)
Definition: ValueObjectMemory.cpp:50
lldb_private::Value::ValueType
ValueType
Type that describes Value::m_value.
Definition: Value.h:41
lldb_private::ValueObject::GetExecutionContextRef
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
lldb_private::ValueObjectMemory::m_compiler_type
CompilerType m_compiler_type
Definition: ValueObjectMemory.h:64
lldb_private::ValueObjectMemory::m_type_sp
lldb::TypeSP m_type_sp
Definition: ValueObjectMemory.h:63
lldb_private::ValueObject::SetName
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:560
ValueObject.h
lldb_private::ArchSpec::GetAddressByteSize
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:683
lldb_private::CompilerType::GetNumChildren
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
Definition: CompilerType.cpp:515
lldb-types.h
lldb_private::ValueObjectMemory::~ValueObjectMemory
~ValueObjectMemory() override
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:966
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb::ValueType
ValueType
Definition: lldb-enumerations.h:315
uint32_t
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:216
lldb_private::Address
Definition: Address.h:59
lldb_private::ValueObject::m_value
Value m_value
Definition: ValueObject.h:852
lldb_private::Value::ValueType::HostAddress
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
lldb_private::CompilerType::GetTypeSystem
TypeSystem * GetTypeSystem() const
Accessors.
Definition: CompilerType.h:162
lldb_private::CompilerType::GetTypeName
ConstString GetTypeName() const
Definition: CompilerType.cpp:277
lldb_private::ValueObjectMemory
A ValueObject that represents memory at a given address, viewed as some set lldb type.
Definition: ValueObjectMemory.h:29
lldb_private::Status::SetErrorString
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:242
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:310
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:86
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:33
Status.h
lldb_private
A class that represents a running process on the host machine.
Definition: SBCommandInterpreterRunOptions.h:16
lldb_private::Value::SetContext
void SetContext(ContextType context_type, void *p)
Definition: Value.h:96
lldb_private::Status::Clear
void Clear()
Clear the object state.
Definition: Status.cpp:168
lldb_private::ValueObject::GetTargetSP
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
lldb_private::Address::GetOffset
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:310
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:282
lldb_private::ValueObject::m_error
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
lldb_private::ValueObjectMemory::UpdateValue
bool UpdateValue() override
Definition: ValueObjectMemory.cpp:154
lldb
Definition: SBAddress.h:15
Value.h
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:200
ExecutionContext.h
lldb_private::ValueObjectMemory::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectMemory.cpp:117