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!
86  assert(m_compiler_type.IsValid());
87 
88  TargetSP target_sp(GetTargetSP());
89 
90  SetName(ConstString(name));
92  lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
93  if (load_address != LLDB_INVALID_ADDRESS) {
95  m_value.GetScalar() = load_address;
96  } else {
97  lldb::addr_t file_address = m_address.GetFileAddress();
98  if (file_address != LLDB_INVALID_ADDRESS) {
100  m_value.GetScalar() = file_address;
101  } else {
104  }
105  }
106 }
107 
109 
111  if (m_type_sp)
112  return m_type_sp->GetForwardCompilerType();
113  return m_compiler_type;
114 }
115 
117  if (m_type_sp)
118  return m_type_sp->GetName();
119  return m_compiler_type.GetTypeName();
120 }
121 
123  if (m_type_sp)
124  return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
126 }
127 
129  if (m_type_sp) {
130  auto child_count = m_type_sp->GetNumChildren(true);
131  return child_count <= max ? child_count : max;
132  }
133 
135  const bool omit_empty_base_classes = true;
136  auto child_count =
137  m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
138  return child_count <= max ? child_count : max;
139 }
140 
141 llvm::Optional<uint64_t> ValueObjectMemory::GetByteSize() {
143  if (m_type_sp)
144  return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
146 }
147 
149  // RETHINK: Should this be inherited from somewhere?
151 }
152 
154  SetValueIsValid(false);
155  m_error.Clear();
156 
158 
159  Target *target = exe_ctx.GetTargetPtr();
160  if (target) {
163  }
164 
165  Value old_value(m_value);
166  if (m_address.IsValid()) {
167  Value::ValueType value_type = m_value.GetValueType();
168 
169  switch (value_type) {
171  m_error.SetErrorString("Invalid value");
172  return false;
174  // The variable value is in the Scalar value inside the m_value. We can
175  // point our m_data right to it.
176  m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
177  break;
178 
182  // The DWARF expression result was an address in the inferior process. If
183  // this variable is an aggregate type, we just need the address as the
184  // main value as all child variable objects will rely upon this location
185  // and add an offset and then read their own values as needed. If this
186  // variable is a simple type, we read all data for it into m_data. Make
187  // sure this type has a value before we try and read it
188 
189  // If we have a file address, convert it to a load address if we can.
190  if (value_type == Value::ValueType::FileAddress &&
191  exe_ctx.GetProcessPtr()) {
192  lldb::addr_t load_addr = m_address.GetLoadAddress(target);
193  if (load_addr != LLDB_INVALID_ADDRESS) {
195  m_value.GetScalar() = load_addr;
196  }
197  }
198 
199  if (!CanProvideValue()) {
200  // this value object represents an aggregate type whose children have
201  // values, but this object does not. So we say we are changed if our
202  // location has changed.
203  SetValueDidChange(value_type != old_value.GetValueType() ||
204  m_value.GetScalar() != old_value.GetScalar());
205  } else {
206  // Copy the Value and set the context to use our Variable so it can
207  // extract read its value into m_data appropriately
208  Value value(m_value);
209  if (m_type_sp)
211  else {
213  }
214 
215  m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
216  }
217  break;
218  }
219 
221  }
222  return m_error.Success();
223 }
224 
226  // FIXME: Maybe try to read the memory address, and if that works, then
227  // we are in scope?
228  return true;
229 }
230 
231 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:741
Scalar.h
lldb_private::ValueObject::CanProvideValue
virtual bool CanProvideValue()
Definition: ValueObject.cpp:3114
lldb::eValueTypeVariableGlobal
@ eValueTypeVariableGlobal
globals variable
Definition: lldb-enumerations.h:318
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:345
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:206
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:298
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:106
lldb_private::CompilerType::GetTypeName
ConstString GetTypeName(bool BaseOnly=false) const
Definition: CompilerType.cpp:314
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:231
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:148
lldb_private::ValueObjectMemory::IsInScope
bool IsInScope() override
Definition: ValueObjectMemory.cpp:225
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:552
ValueObjectMemory.h
lldb::addr_t
uint64_t addr_t
Definition: lldb-types.h:83
lldb_private::Target
Definition: Target.h:469
lldb_private::ValueObjectMemory::GetByteSize
llvm::Optional< uint64_t > GetByteSize() override
Definition: ValueObjectMemory.cpp:141
lldb_private::Value::SetCompilerType
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:251
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:287
lldb_private::CompilerType::GetDisplayTypeName
ConstString GetDisplayTypeName() const
Definition: CompilerType.cpp:322
lldb_private::ValueObjectMemory::CalculateNumChildren
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
Definition: ValueObjectMemory.cpp:128
lldb_private::ExecutionContextScope
Definition: ExecutionContextScope.h:32
lldb_private::ValueObjectMemory::GetDisplayTypeName
ConstString GetDisplayTypeName() override
Definition: ValueObjectMemory.cpp:122
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:291
Type.h
lldb_private::ValueObjectMemory::GetCompilerTypeImpl
CompilerType GetCompilerTypeImpl() override
Definition: ValueObjectMemory.cpp:110
lldb_private::ConstString
Definition: ConstString.h:39
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
lldb_private::CompilerType::IsValid
bool IsValid() const
Definition: CompilerType.h:123
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:694
lldb_private::CompilerType::GetNumChildren
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
Definition: CompilerType.cpp:579
lldb-types.h
lldb_private::ValueObjectMemory::~ValueObjectMemory
~ValueObjectMemory() override
lldb_private::Target::GetArchitecture
const ArchSpec & GetArchitecture() const
Definition: Target.h:989
lldb_private::ValueObject
ValueObject:
Definition: ValueObject.h:105
lldb::ValueType
ValueType
Definition: lldb-enumerations.h:316
uint32_t
lldb_private::ExecutionContext::GetBestExecutionContextScope
ExecutionContextScope * GetBestExecutionContextScope() const
Definition: ExecutionContext.cpp:214
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::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:241
lldb_private::Address::GetLoadAddress
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:311
LLDB_INVALID_ADDRESS
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:74
DataExtractor.h
lldb_private::CompilerType
Generic representation of a type in a programming language.
Definition: CompilerType.h:35
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:167
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:319
lldb_private::Address::GetModule
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:283
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:153
lldb
Definition: SBAddress.h:15
Value.h
lldb_private::ExecutionContext::GetTargetPtr
Target * GetTargetPtr() const
Returns a pointer to the target object.
Definition: ExecutionContext.cpp:198
ExecutionContext.h
lldb_private::ValueObjectMemory::GetTypeName
ConstString GetTypeName() override
Definition: ValueObjectMemory.cpp:116