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"
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#include <optional>
24
25namespace lldb_private {
27}
28
29using namespace lldb;
30using namespace lldb_private;
31
33 llvm::StringRef name,
34 const Address &address,
35 lldb::TypeSP &type_sp) {
36 auto manager_sp = ValueObjectManager::Create();
37 return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
38 ->GetSP();
39}
40
42 llvm::StringRef name,
43 const Address &address,
44 const CompilerType &ast_type) {
45 auto manager_sp = ValueObjectManager::Create();
46 return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
47 ast_type))
48 ->GetSP();
49}
50
52 ValueObjectManager &manager,
53 llvm::StringRef name,
54 const Address &address,
55 lldb::TypeSP &type_sp)
56 : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
57 m_compiler_type() {
58 // Do not attempt to construct one of these objects with no variable!
59 assert(m_type_sp.get() != nullptr);
60 SetName(ConstString(name));
62 TargetSP target_sp(GetTargetSP());
63 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
64 if (load_address != LLDB_INVALID_ADDRESS) {
66 m_value.GetScalar() = load_address;
67 } else {
68 lldb::addr_t file_address = m_address.GetFileAddress();
69 if (file_address != LLDB_INVALID_ADDRESS) {
71 m_value.GetScalar() = file_address;
72 } else {
75 }
76 }
77}
78
80 ValueObjectManager &manager,
81 llvm::StringRef name,
82 const Address &address,
83 const CompilerType &ast_type)
84 : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
85 m_compiler_type(ast_type) {
86 // Do not attempt to construct one of these objects with no variable!
87 assert(m_compiler_type.IsValid());
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();
121}
122
124 if (m_type_sp)
125 return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
127}
128
129llvm::Expected<uint32_t> ValueObjectMemory::CalculateNumChildren(uint32_t max) {
130 if (m_type_sp) {
131 auto child_count = m_type_sp->GetNumChildren(true);
132 if (!child_count)
133 return child_count;
134 return *child_count <= max ? *child_count : max;
135 }
136
138 const bool omit_empty_base_classes = true;
139 auto child_count =
140 m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
141 if (!child_count)
142 return child_count;
143 return *child_count <= max ? *child_count : max;
144}
145
146std::optional<uint64_t> ValueObjectMemory::GetByteSize() {
148 if (m_type_sp)
149 return m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope());
151}
152
154 // RETHINK: Should this be inherited from somewhere?
156}
157
159 SetValueIsValid(false);
160 m_error.Clear();
161
163
164 Target *target = exe_ctx.GetTargetPtr();
165 if (target) {
168 }
169
170 Value old_value(m_value);
171 if (m_address.IsValid()) {
173
174 switch (value_type) {
176 m_error.SetErrorString("Invalid value");
177 return false;
179 // The variable value is in the Scalar value inside the m_value. We can
180 // point our m_data right to it.
181 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
182 break;
183
187 // The DWARF expression result was an address in the inferior process. If
188 // this variable is an aggregate type, we just need the address as the
189 // main value as all child variable objects will rely upon this location
190 // and add an offset and then read their own values as needed. If this
191 // variable is a simple type, we read all data for it into m_data. Make
192 // sure this type has a value before we try and read it
193
194 // If we have a file address, convert it to a load address if we can.
195 if (value_type == Value::ValueType::FileAddress &&
196 exe_ctx.GetProcessPtr()) {
197 lldb::addr_t load_addr = m_address.GetLoadAddress(target);
198 if (load_addr != LLDB_INVALID_ADDRESS) {
200 m_value.GetScalar() = load_addr;
201 }
202 }
203
204 if (!CanProvideValue()) {
205 // this value object represents an aggregate type whose children have
206 // values, but this object does not. So we say we are changed if our
207 // location has changed.
208 SetValueDidChange(value_type != old_value.GetValueType() ||
209 m_value.GetScalar() != old_value.GetScalar());
210 } else {
211 // Copy the Value and set the context to use our Variable so it can
212 // extract read its value into m_data appropriately
213 Value value(m_value);
214 if (m_type_sp)
216 else {
218 }
219
220 m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
221 }
222 break;
223 }
224
226 }
227 return m_error.Success();
228}
229
231 // FIXME: Maybe try to read the memory address, and if that works, then
232 // we are in scope?
233 return true;
234}
235
A section + offset based address class.
Definition: Address.h:62
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:313
lldb::ModuleSP GetModule() const
Get accessor for the module for this address.
Definition: Address.cpp:285
lldb::addr_t GetFileAddress() const
Get the file address.
Definition: Address.cpp:293
lldb::addr_t GetOffset() const
Get the section relative offset value.
Definition: Address.h:329
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:355
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition: ArchSpec.cpp:691
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition: ArchSpec.cpp:738
static std::shared_ptr< ClusterManager > Create()
Definition: SharedCluster.h:24
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
std::optional< uint64_t > GetByteSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bytes.
ConstString GetDisplayTypeName() const
ConstString GetTypeName(bool BaseOnly=false) const
llvm::Expected< uint32_t > GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
A uniqued constant string class.
Definition: ConstString.h:40
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
"lldb/Target/ExecutionContextScope.h" Inherit from this if your object can reconstruct its execution ...
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
void Clear()
Clear the object state.
Definition: Status.cpp:166
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:232
bool Success() const
Test for success condition.
Definition: Status.cpp:278
const ArchSpec & GetArchitecture() const
Definition: Target.h:1023
A ValueObject that represents memory at a given address, viewed as some set lldb type.
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp)
lldb::ValueType GetValueType() const override
lldb::ModuleSP GetModule() override
Return the module associated with this value object in case the value is from an executable file and ...
std::optional< uint64_t > GetByteSize() override
Address m_address
The variable that this value object is based upon.
ValueObjectMemory(ExecutionContextScope *exe_scope, ValueObjectManager &manager, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp)
CompilerType GetCompilerTypeImpl() override
ConstString GetTypeName() override
ConstString GetDisplayTypeName() override
void SetValueIsValid(bool valid)
Definition: ValueObject.h:1059
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:930
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:926
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:1055
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:574
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
virtual bool CanProvideValue()
const Scalar & GetScalar() const
Definition: Value.h:112
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:315
ValueType
Type that describes Value::m_value.
Definition: Value.h:41
@ HostAddress
A host address value (for memory in the process that < A is using liblldb).
@ FileAddress
A file address value.
@ LoadAddress
A load address value.
@ Scalar
A raw scalar value.
ValueType GetValueType() const
Definition: Value.cpp:109
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
void SetContext(ContextType context_type, void *p)
Definition: Value.h:96
void SetValueType(ValueType value_type)
Definition: Value.h:89
@ LLDBType
lldb_private::Type *.
#define LLDB_INVALID_ADDRESS
Definition: lldb-defines.h:82
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:478
std::shared_ptr< lldb_private::Type > TypeSP
Definition: lldb-forward.h:455
uint64_t addr_t
Definition: lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:442
std::shared_ptr< lldb_private::Module > ModuleSP
Definition: lldb-forward.h:369
@ eValueTypeVariableGlobal
globals variable