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/Symbol/Type.h"
13#include "lldb/Target/Target.h"
16#include "lldb/Utility/Scalar.h"
17#include "lldb/Utility/Status.h"
19#include "lldb/lldb-types.h"
20#include "llvm/Support/ErrorHandling.h"
21
22#include <cassert>
23#include <memory>
24#include <optional>
25
26namespace lldb_private {
28}
29
30using namespace lldb;
31using namespace lldb_private;
32
34 llvm::StringRef name,
35 const Address &address,
36 lldb::TypeSP &type_sp,
37 ValueObject *parent) {
38
39 std::shared_ptr<ValueObjectManager> manager_sp =
41 return (new ValueObjectMemory(exe_scope, *manager_sp, name, address, type_sp))
42 ->GetSP();
43}
44
46 llvm::StringRef name,
47 const Address &address,
48 const CompilerType &ast_type,
49 ValueObject *parent) {
50 std::shared_ptr<ValueObjectManager> manager_sp =
52 return (new ValueObjectMemory(exe_scope, *manager_sp, name, address,
53 ast_type))
54 ->GetSP();
55}
56
58 ValueObjectManager &manager,
59 llvm::StringRef name,
60 const Address &address,
61 lldb::TypeSP &type_sp)
62 : ValueObject(exe_scope, manager), m_address(address), m_type_sp(type_sp),
64 // Do not attempt to construct one of these objects with no variable!
65 assert(m_type_sp.get() != nullptr);
66 SetName(ConstString(name));
68 TargetSP target_sp(GetTargetSP());
69 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
70 if (load_address != LLDB_INVALID_ADDRESS) {
72 m_value.GetScalar() = load_address;
73 } else {
74 lldb::addr_t file_address = m_address.GetFileAddress();
75 if (file_address != LLDB_INVALID_ADDRESS) {
77 m_value.GetScalar() = file_address;
78 } else {
79 m_value.GetScalar() = m_address.GetOffset();
81 }
82 }
83}
84
86 ValueObjectManager &manager,
87 llvm::StringRef name,
88 const Address &address,
89 const CompilerType &ast_type)
90 : ValueObject(exe_scope, manager), m_address(address), m_type_sp(),
91 m_compiler_type(ast_type) {
92 // Do not attempt to construct one of these objects with no variable!
93 assert(m_compiler_type.IsValid());
94
95 TargetSP target_sp(GetTargetSP());
96
97 SetName(ConstString(name));
98 m_value.SetCompilerType(m_compiler_type);
99 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
100 if (load_address != LLDB_INVALID_ADDRESS) {
102 m_value.GetScalar() = load_address;
103 } else {
104 lldb::addr_t file_address = m_address.GetFileAddress();
105 if (file_address != LLDB_INVALID_ADDRESS) {
107 m_value.GetScalar() = file_address;
108 } else {
109 m_value.GetScalar() = m_address.GetOffset();
110 m_value.SetValueType(Value::ValueType::Scalar);
111 }
112 }
113}
114
116
118 if (m_type_sp)
119 return m_type_sp->GetForwardCompilerType();
120 return m_compiler_type;
121}
122
124 if (m_type_sp)
125 return m_type_sp->GetName();
126 return m_compiler_type.GetTypeName();
127}
128
130 if (m_type_sp)
131 return m_type_sp->GetForwardCompilerType().GetDisplayTypeName();
132 return m_compiler_type.GetDisplayTypeName();
133}
134
135llvm::Expected<uint32_t> ValueObjectMemory::CalculateNumChildren(uint32_t max) {
136 if (m_type_sp) {
137 auto child_count = m_type_sp->GetNumChildren(true);
138 if (!child_count)
139 return child_count;
140 return *child_count <= max ? *child_count : max;
141 }
142
144 const bool omit_empty_base_classes = true;
145 auto child_count =
146 m_compiler_type.GetNumChildren(omit_empty_base_classes, &exe_ctx);
147 if (!child_count)
148 return child_count;
149 return *child_count <= max ? *child_count : max;
150}
151
152llvm::Expected<uint64_t> ValueObjectMemory::GetByteSize() {
154 if (m_type_sp) {
155 if (auto size =
156 m_type_sp->GetByteSize(exe_ctx.GetBestExecutionContextScope()))
157 return *size;
158 else
159 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), size.takeError(),
160 "failed to get byte size from type: {0}");
161 return llvm::createStringError("could not get byte size of memory object");
162 }
163 return m_compiler_type.GetByteSize(exe_ctx.GetBestExecutionContextScope());
164}
165
167 // RETHINK: Should this be inherited from somewhere?
169}
170
172 SetValueIsValid(false);
173 m_error.Clear();
174
176
177 Target *target = exe_ctx.GetTargetPtr();
178 if (target) {
179 m_data.SetByteOrder(target->GetArchitecture().GetByteOrder());
180 m_data.SetAddressByteSize(target->GetArchitecture().GetAddressByteSize());
181 }
182
183 Value old_value(m_value);
184 if (m_address.IsValid()) {
185 Value::ValueType value_type = m_value.GetValueType();
186
187 switch (value_type) {
189 m_error = Status::FromErrorString("Invalid value");
190 return false;
192 // The variable value is in the Scalar value inside the m_value. We can
193 // point our m_data right to it.
194 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
195 break;
196
200 // The DWARF expression result was an address in the inferior process. If
201 // this variable is an aggregate type, we just need the address as the
202 // main value as all child variable objects will rely upon this location
203 // and add an offset and then read their own values as needed. If this
204 // variable is a simple type, we read all data for it into m_data. Make
205 // sure this type has a value before we try and read it
206
207 // If we have a file address, convert it to a load address if we can.
208 if (value_type == Value::ValueType::FileAddress &&
209 exe_ctx.GetProcessPtr()) {
210 lldb::addr_t load_addr = m_address.GetLoadAddress(target);
211 if (load_addr != LLDB_INVALID_ADDRESS) {
213 m_value.GetScalar() = load_addr;
214 }
215 }
216
217 if (!CanProvideValue()) {
218 // this value object represents an aggregate type whose children have
219 // values, but this object does not. So we say we are changed if our
220 // location has changed.
221 SetValueDidChange(value_type != old_value.GetValueType() ||
222 m_value.GetScalar() != old_value.GetScalar());
223 } else {
224 // Copy the Value and set the context to use our Variable so it can
225 // extract read its value into m_data appropriately
226 Value value(m_value);
227 if (m_type_sp)
229 else {
231 }
232
233 m_error = value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
234 }
235 break;
236 }
237
238 SetValueIsValid(m_error.Success());
239 }
240 return m_error.Success();
241}
242
244 // FIXME: Maybe try to read the memory address, and if that works, then
245 // we are in scope?
246 return true;
247}
248
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:394
A section + offset based address class.
Definition Address.h:62
uint32_t GetAddressByteSize() const
Returns the size in bytes of an address of the current architecture.
Definition ArchSpec.cpp:690
lldb::ByteOrder GetByteOrder() const
Returns the byte order for the architecture specification.
Definition ArchSpec.cpp:739
Generic representation of a type in a programming language.
A uniqued constant string class.
Definition ConstString.h:40
"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.
static Status FromErrorString(const char *str)
Definition Status.h:141
const ArchSpec & GetArchitecture() const
Definition Target.h:1282
static lldb::ValueObjectSP Create(ExecutionContextScope *exe_scope, llvm::StringRef name, const Address &address, lldb::TypeSP &type_sp, ValueObject *parent=nullptr)
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
lldb::ValueType GetValueType() const override
llvm::Expected< uint64_t > GetByteSize() override
lldb::ModuleSP GetModule() override
Return the module associated with this value object in case the value is from an executable file and ...
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 GetDisplayTypeName() override
void SetValueIsValid(bool valid)
ClusterManager< ValueObject > ValueObjectManager
ValueObject(ExecutionContextScope *exe_scope, ValueObjectManager &manager, AddressType child_ptr_or_ref_addr_type=eAddressTypeLoad)
Use this constructor to create a "root variable object".
Status m_error
An error object that can describe any errors that occur when updating values.
DataExtractor m_data
A data extractor that can be used to extract the value.
void SetValueDidChange(bool value_changed)
void SetName(ConstString name)
Change the name of the current ValueObject.
lldb::TargetSP GetTargetSP() const
static ValueObjectManagerSP ReuseManagerIfParent(ValueObject *parent)
The following two functions are helpers for Create methods for ValueObject subclasses that need to op...
const ExecutionContextRef & GetExecutionContextRef() const
const Scalar & GetScalar() const
See comment on m_scalar to understand what GetScalar returns.
Definition Value.h:113
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition Value.cpp:323
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).
Definition Value.h:52
@ FileAddress
A file address value.
Definition Value.h:47
@ LoadAddress
A load address value.
Definition Value.h:49
@ Scalar
A raw scalar value.
Definition Value.h:45
ValueType GetValueType() const
Definition Value.cpp:111
void SetCompilerType(const CompilerType &compiler_type)
Definition Value.cpp:276
void SetContext(ContextType context_type, void *p)
Definition Value.h:96
@ LLDBType
lldb_private::Type *.
Definition Value.h:63
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:327
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
std::shared_ptr< lldb_private::Type > TypeSP
@ eValueTypeVariableGlobal
globals variable
uint64_t addr_t
Definition lldb-types.h:80
std::shared_ptr< lldb_private::Target > TargetSP
std::shared_ptr< lldb_private::Module > ModuleSP