LLDB mainline
ValueObjectDynamicValue.cpp
Go to the documentation of this file.
1//===-- ValueObjectDynamicValue.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"
13#include "lldb/Symbol/Type.h"
16#include "lldb/Target/Process.h"
17#include "lldb/Target/Target.h"
20#include "lldb/Utility/Log.h"
21#include "lldb/Utility/Scalar.h"
22#include "lldb/Utility/Status.h"
23#include "lldb/lldb-types.h"
24
25#include <cstring>
26#include <optional>
27namespace lldb_private {
28class Declaration;
29}
30
31using namespace lldb_private;
32
34 ValueObject &parent, lldb::DynamicValueType use_dynamic)
35 : ValueObject(parent), m_address(), m_dynamic_type_info(),
36 m_use_dynamic(use_dynamic) {
37 SetName(parent.GetName());
38}
39
41 const bool success = UpdateValueIfNeeded(false);
42 if (success) {
44 return m_value.GetCompilerType();
45 else
46 return m_parent->GetCompilerType();
47 }
48 return m_parent->GetCompilerType();
49}
50
52 const bool success = UpdateValueIfNeeded(false);
53 if (success) {
56 }
57 return m_parent->GetTypeName();
58}
59
61 const bool success = UpdateValueIfNeeded(false);
62 if (success && m_type_impl.IsValid()) {
63 return m_type_impl;
64 }
65 return m_parent->GetTypeImpl();
66}
67
69 const bool success = UpdateValueIfNeeded(false);
70 if (success) {
73 }
75}
76
78 const bool success = UpdateValueIfNeeded(false);
79 if (success) {
84 }
86}
87
89 const bool success = UpdateValueIfNeeded(false);
90 if (success && m_dynamic_type_info.HasType()) {
92 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
93 return children_count <= max ? children_count : max;
94 } else
95 return m_parent->GetNumChildren(max);
96}
97
98std::optional<uint64_t> ValueObjectDynamicValue::GetByteSize() {
99 const bool success = UpdateValueIfNeeded(false);
100 if (success && m_dynamic_type_info.HasType()) {
102 return m_value.GetValueByteSize(nullptr, &exe_ctx);
103 } else
104 return m_parent->GetByteSize();
105}
106
108 return m_parent->GetValueType();
109}
110
112 SetValueIsValid(false);
113 m_error.Clear();
114
115 if (!m_parent->UpdateValueIfNeeded(false)) {
116 // The dynamic value failed to get an error, pass the error along
117 if (m_error.Success() && m_parent->GetError().Fail())
119 return false;
120 }
121
122 // Setting our type_sp to NULL will route everything back through our parent
123 // which is equivalent to not using dynamic values.
126 return true;
127 }
128
130 Target *target = exe_ctx.GetTargetPtr();
131 if (target) {
134 }
135
136 // First make sure our Type and/or Address haven't changed:
137 Process *process = exe_ctx.GetProcessPtr();
138 if (!process)
139 return false;
140
141 TypeAndOrName class_type_or_name;
142 Address dynamic_address;
143 bool found_dynamic_type = false;
144 Value::ValueType value_type;
145
146 LanguageRuntime *runtime = nullptr;
147
149 if (known_type != lldb::eLanguageTypeUnknown &&
150 known_type != lldb::eLanguageTypeC) {
151 runtime = process->GetLanguageRuntime(known_type);
152 if (runtime)
153 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
154 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
155 value_type);
156 } else {
158 if (runtime)
159 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
160 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
161 value_type);
162
163 if (!found_dynamic_type) {
165 if (runtime)
166 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
167 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
168 value_type);
169 }
170 }
171
172 // Getting the dynamic value may have run the program a bit, and so marked us
173 // as needing updating, but we really don't...
174
176
177 if (runtime && found_dynamic_type) {
178 if (class_type_or_name.HasType()) {
181 runtime->FixUpDynamicType(class_type_or_name, *m_parent)
182 .GetCompilerType());
183 } else {
185 }
186 } else {
188 }
189
190 // If we don't have a dynamic type, set ourselves to be invalid and return
191 // false. We used to try to produce a dynamic ValueObject that behaved "like"
192 // its parent, but that failed for ValueObjectConstResult, which is too
193 // complex a beast to try to emulate. If we return an invalid ValueObject,
194 // clients will end up getting the static value instead, which behaves
195 // correctly.
196 if (!found_dynamic_type) {
198 SetValueDidChange(true);
201 m_error.SetErrorString("no dynamic type found");
202 return false;
203 }
204
205 Value old_value(m_value);
206
207 Log *log = GetLog(LLDBLog::Types);
208
209 bool has_changed_type = false;
210
211 if (!m_dynamic_type_info) {
212 m_dynamic_type_info = class_type_or_name;
213 has_changed_type = true;
214 } else if (class_type_or_name != m_dynamic_type_info) {
215 // We are another type, we need to tear down our children...
216 m_dynamic_type_info = class_type_or_name;
217 SetValueDidChange(true);
218 has_changed_type = true;
219 }
220
221 if (has_changed_type)
223
224 if (!m_address.IsValid() || m_address != dynamic_address) {
225 if (m_address.IsValid())
226 SetValueDidChange(true);
227
228 // We've moved, so we should be fine...
229 m_address = dynamic_address;
230 lldb::TargetSP target_sp(GetTargetSP());
231 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
232 m_value.GetScalar() = load_address;
233 }
234
235 if (runtime)
238
240
241 m_value.SetValueType(value_type);
242
243 if (has_changed_type && log)
244 LLDB_LOGF(log, "[%s %p] has a new dynamic type %s", GetName().GetCString(),
245 static_cast<void *>(this), GetTypeName().GetCString());
246
248 // The variable value is in the Scalar value inside the m_value. We can
249 // point our m_data right to it.
250 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
251 if (m_error.Success()) {
252 if (!CanProvideValue()) {
253 // this value object represents an aggregate type whose children have
254 // values, but this object does not. So we say we are changed if our
255 // location has changed.
257 m_value.GetScalar() != old_value.GetScalar());
258 }
259
260 SetValueIsValid(true);
261 return true;
262 }
263 }
264
265 // We get here if we've failed above...
266 SetValueIsValid(false);
267 return false;
268}
269
271
273 Status &error) {
274 if (!UpdateValueIfNeeded(false)) {
275 error.SetErrorString("unable to read value");
276 return false;
277 }
278
279 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
280 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
281
282 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
283 error.SetErrorString("unable to read value");
284 return false;
285 }
286
287 // if we are at an offset from our parent, in order to set ourselves
288 // correctly we would need to change the new value so that it refers to the
289 // correct dynamic type. we choose not to deal with that - if anything more
290 // than a value overwrite is required, you should be using the expression
291 // parser instead of the value editing facility
292 if (my_value != parent_value) {
293 // but NULL'ing out a value should always be allowed
294 if (strcmp(value_str, "0")) {
295 error.SetErrorString(
296 "unable to modify dynamic value, use 'expression' command");
297 return false;
298 }
299 }
300
301 bool ret_val = m_parent->SetValueFromCString(value_str, error);
303 return ret_val;
304}
305
307 if (!UpdateValueIfNeeded(false)) {
308 error.SetErrorString("unable to read value");
309 return false;
310 }
311
312 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
313 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
314
315 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
316 error.SetErrorString("unable to read value");
317 return false;
318 }
319
320 // if we are at an offset from our parent, in order to set ourselves
321 // correctly we would need to change the new value so that it refers to the
322 // correct dynamic type. we choose not to deal with that - if anything more
323 // than a value overwrite is required, you should be using the expression
324 // parser instead of the value editing facility
325 if (my_value != parent_value) {
326 // but NULL'ing out a value should always be allowed
327 lldb::offset_t offset = 0;
328
329 if (data.GetAddress(&offset) != 0) {
330 error.SetErrorString(
331 "unable to modify dynamic value, use 'expression' command");
332 return false;
333 }
334 }
335
336 bool ret_val = m_parent->SetData(data, error);
338 return ret_val;
339}
340
342 lldb::LanguageType lang) {
344 if (m_parent)
346}
347
350 if (m_parent)
353 } else
355}
356
358 if (m_parent)
360 return false;
361}
362
364 if (m_parent)
367}
368
370 if (m_parent)
371 return m_parent->GetDeclaration(decl);
372
373 return ValueObject::GetDeclaration(decl);
374}
375
377 if (m_parent)
378 return m_parent->GetLanguageFlags();
379 return this->ValueObject::GetLanguageFlags();
380}
381
383 if (m_parent)
385 else
387}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:344
A section + offset based address class.
Definition: Address.h:59
lldb::addr_t GetLoadAddress(Target *target) const
Get the load address.
Definition: Address.cpp:311
bool IsValid() const
Check if the object state is valid.
Definition: Address.h:345
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
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
ConstString GetDisplayTypeName() const
uint32_t GetNumChildren(bool omit_empty_base_classes, const ExecutionContext *exe_ctx) const
A uniqued constant string class.
Definition: ConstString.h:39
An data extractor class.
Definition: DataExtractor.h:48
void SetByteOrder(lldb::ByteOrder byte_order)
Set the byte_order value.
uint64_t GetAddress(lldb::offset_t *offset_ptr) const
Extract an address from *offset_ptr.
void SetAddressByteSize(uint32_t addr_size)
Set the address byte size.
A class that describes the declaration location of a lldb object.
Definition: Declaration.h:24
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
Target * GetTargetPtr() const
Returns a pointer to the target object.
Process * GetProcessPtr() const
Returns a pointer to the process object.
virtual TypeAndOrName FixUpDynamicType(const TypeAndOrName &type_and_or_name, ValueObject &static_value)=0
virtual bool GetDynamicTypeAndAddress(ValueObject &in_value, lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name, Address &address, Value::ValueType &value_type)=0
A plug-in interface definition class for debugging a process.
Definition: Process.h:333
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition: Process.cpp:1516
An error handling class.
Definition: Status.h:44
void Clear()
Clear the object state.
Definition: Status.cpp:167
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
void SetErrorString(llvm::StringRef err_str)
Set the current error string to err_str.
Definition: Status.cpp:241
bool Success() const
Test for success condition.
Definition: Status.cpp:287
const ArchSpec & GetArchitecture() const
Definition: Target.h:996
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
Definition: Type.h:410
CompilerType GetCompilerType() const
Definition: Type.h:424
bool HasName() const
Definition: Type.cpp:815
ConstString GetName() const
Definition: Type.cpp:776
bool HasType() const
Definition: Type.h:440
bool IsValid() const
Definition: Type.cpp:917
lldb::ValueType GetValueType() const override
lldb::LanguageType GetPreferredDisplayLanguage() override
void SetLanguageFlags(uint64_t flags) override
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
ValueObjectDynamicValue(ValueObject &parent, lldb::DynamicValueType use_dynamic)
bool SetData(DataExtractor &data, Status &error) override
bool GetDeclaration(Declaration &decl) override
bool SetValueFromCString(const char *value_str, Status &error) override
std::optional< uint64_t > GetByteSize() override
Address m_address
The variable that this value object is based upon.
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
EvaluationPoint m_update_point
Stores both the stop id and the full context at which this value was last updated.
Definition: ValueObject.h:847
virtual bool IsInScope()
Definition: ValueObject.h:420
size_t GetNumChildren(uint32_t max=UINT32_MAX)
CompilerType GetCompilerType()
Definition: ValueObject.h:352
void SetPreferredDisplayLanguage(lldb::LanguageType lt)
Definition: ValueObject.h:713
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:791
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
virtual uint64_t GetLanguageFlags()
Definition: ValueObject.h:789
virtual void SetSyntheticChildrenGenerated(bool b)
Definition: ValueObject.h:645
virtual std::optional< uint64_t > GetByteSize()=0
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
virtual lldb::ValueType GetValueType() const =0
virtual ConstString GetTypeName()
Definition: ValueObject.h:365
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:976
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
virtual ConstString GetDisplayTypeName()
Definition: ValueObject.h:367
lldb::LanguageType m_preferred_display_language
Definition: ValueObject.h:900
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:840
virtual bool GetDeclaration(Declaration &decl)
virtual bool IsSyntheticChildrenGenerated()
Definition: ValueObject.h:641
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:560
const Status & GetError()
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
ConstString GetName() const
Definition: ValueObject.h:467
virtual bool SetValueFromCString(const char *value_str, Status &error)
virtual ConstString GetQualifiedTypeName()
Definition: ValueObject.h:369
virtual bool SetData(DataExtractor &data, Status &error)
virtual TypeImpl GetTypeImpl()
Definition: ValueObject.h:355
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
virtual lldb::LanguageType GetObjectRuntimeLanguage()
Definition: ValueObject.h:373
virtual bool CanProvideValue()
virtual lldb::LanguageType GetPreferredDisplayLanguage()
const Scalar & GetScalar() const
Definition: Value.h:112
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:313
ValueType
Type that describes Value::m_value.
Definition: Value.h:41
ValueType GetValueType() const
Definition: Value.cpp:107
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:266
uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx)
Definition: Value.cpp:209
void SetValueType(ValueType value_type)
Definition: Value.h:89
const CompilerType & GetCompilerType()
Definition: Value.cpp:237
#define UINT64_MAX
Definition: lldb-defines.h:23
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition: Log.h:309
uint64_t offset_t
Definition: lldb-types.h:83
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eLanguageTypeC
Non-standardized C, such as K&R.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
uint64_t addr_t
Definition: lldb-types.h:79
@ eNoDynamicValues