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
88llvm::Expected<uint32_t>
90 const bool success = UpdateValueIfNeeded(false);
91 if (success && m_dynamic_type_info.HasType()) {
93 auto children_count = GetCompilerType().GetNumChildren(true, &exe_ctx);
94 if (!children_count)
95 return children_count;
96 return *children_count <= max ? *children_count : max;
97 } else
98 return m_parent->GetNumChildren(max);
99}
100
101std::optional<uint64_t> ValueObjectDynamicValue::GetByteSize() {
102 const bool success = UpdateValueIfNeeded(false);
103 if (success && m_dynamic_type_info.HasType()) {
105 return m_value.GetValueByteSize(nullptr, &exe_ctx);
106 } else
107 return m_parent->GetByteSize();
108}
109
111 return m_parent->GetValueType();
112}
113
115 SetValueIsValid(false);
116 m_error.Clear();
117
118 if (!m_parent->UpdateValueIfNeeded(false)) {
119 // The dynamic value failed to get an error, pass the error along
120 if (m_error.Success() && m_parent->GetError().Fail())
122 return false;
123 }
124
125 // Setting our type_sp to NULL will route everything back through our parent
126 // which is equivalent to not using dynamic values.
129 return true;
130 }
131
133 Target *target = exe_ctx.GetTargetPtr();
134 if (target) {
137 }
138
139 // First make sure our Type and/or Address haven't changed:
140 Process *process = exe_ctx.GetProcessPtr();
141 if (!process)
142 return false;
143
144 TypeAndOrName class_type_or_name;
145 Address dynamic_address;
146 bool found_dynamic_type = false;
147 Value::ValueType value_type;
148
149 LanguageRuntime *runtime = nullptr;
150
152 if (known_type != lldb::eLanguageTypeUnknown &&
153 known_type != lldb::eLanguageTypeC) {
154 runtime = process->GetLanguageRuntime(known_type);
155 if (auto *preferred_runtime =
157 // Try the preferred runtime first.
158 found_dynamic_type = preferred_runtime->GetDynamicTypeAndAddress(
159 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
160 value_type);
161 if (found_dynamic_type)
162 // Set the operative `runtime` for later use in this function.
163 runtime = preferred_runtime;
164 }
165 if (!found_dynamic_type)
166 // Fallback to the runtime for `known_type`.
167 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
168 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
169 value_type);
170 } else {
172 if (runtime)
173 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
174 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
175 value_type);
176
177 if (!found_dynamic_type) {
179 if (runtime)
180 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
181 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
182 value_type);
183 }
184 }
185
186 // Getting the dynamic value may have run the program a bit, and so marked us
187 // as needing updating, but we really don't...
188
190
191 if (runtime && found_dynamic_type) {
192 if (class_type_or_name.HasType()) {
195 runtime->FixUpDynamicType(class_type_or_name, *m_parent)
196 .GetCompilerType());
197 } else {
199 }
200 } else {
202 }
203
204 // If we don't have a dynamic type, set ourselves to be invalid and return
205 // false. We used to try to produce a dynamic ValueObject that behaved "like"
206 // its parent, but that failed for ValueObjectConstResult, which is too
207 // complex a beast to try to emulate. If we return an invalid ValueObject,
208 // clients will end up getting the static value instead, which behaves
209 // correctly.
210 if (!found_dynamic_type) {
212 SetValueDidChange(true);
215 m_error.SetErrorString("no dynamic type found");
216 return false;
217 }
218
219 Value old_value(m_value);
220
221 Log *log = GetLog(LLDBLog::Types);
222
223 bool has_changed_type = false;
224
225 if (!m_dynamic_type_info) {
226 m_dynamic_type_info = class_type_or_name;
227 has_changed_type = true;
228 } else if (class_type_or_name != m_dynamic_type_info) {
229 // We are another type, we need to tear down our children...
230 m_dynamic_type_info = class_type_or_name;
231 SetValueDidChange(true);
232 has_changed_type = true;
233 }
234
235 if (has_changed_type)
237
238 if (!m_address.IsValid() || m_address != dynamic_address) {
239 if (m_address.IsValid())
240 SetValueDidChange(true);
241
242 // We've moved, so we should be fine...
243 m_address = dynamic_address;
244 lldb::TargetSP target_sp(GetTargetSP());
245 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
246 m_value.GetScalar() = load_address;
247 }
248
249 if (runtime)
252
254
255 m_value.SetValueType(value_type);
256
257 if (has_changed_type && log)
258 LLDB_LOGF(log, "[%s %p] has a new dynamic type %s", GetName().GetCString(),
259 static_cast<void *>(this), GetTypeName().GetCString());
260
262 // The variable value is in the Scalar value inside the m_value. We can
263 // point our m_data right to it.
264 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
265 if (m_error.Success()) {
266 if (!CanProvideValue()) {
267 // this value object represents an aggregate type whose children have
268 // values, but this object does not. So we say we are changed if our
269 // location has changed.
271 m_value.GetScalar() != old_value.GetScalar());
272 }
273
274 SetValueIsValid(true);
275 return true;
276 }
277 }
278
279 // We get here if we've failed above...
280 SetValueIsValid(false);
281 return false;
282}
283
285
287 Status &error) {
288 if (!UpdateValueIfNeeded(false)) {
289 error.SetErrorString("unable to read value");
290 return false;
291 }
292
293 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
294 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
295
296 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
297 error.SetErrorString("unable to read value");
298 return false;
299 }
300
301 // if we are at an offset from our parent, in order to set ourselves
302 // correctly we would need to change the new value so that it refers to the
303 // correct dynamic type. we choose not to deal with that - if anything more
304 // than a value overwrite is required, you should be using the expression
305 // parser instead of the value editing facility
306 if (my_value != parent_value) {
307 // but NULL'ing out a value should always be allowed
308 if (strcmp(value_str, "0")) {
309 error.SetErrorString(
310 "unable to modify dynamic value, use 'expression' command");
311 return false;
312 }
313 }
314
315 bool ret_val = m_parent->SetValueFromCString(value_str, error);
317 return ret_val;
318}
319
321 if (!UpdateValueIfNeeded(false)) {
322 error.SetErrorString("unable to read value");
323 return false;
324 }
325
326 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
327 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
328
329 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
330 error.SetErrorString("unable to read value");
331 return false;
332 }
333
334 // if we are at an offset from our parent, in order to set ourselves
335 // correctly we would need to change the new value so that it refers to the
336 // correct dynamic type. we choose not to deal with that - if anything more
337 // than a value overwrite is required, you should be using the expression
338 // parser instead of the value editing facility
339 if (my_value != parent_value) {
340 // but NULL'ing out a value should always be allowed
341 lldb::offset_t offset = 0;
342
343 if (data.GetAddress(&offset) != 0) {
344 error.SetErrorString(
345 "unable to modify dynamic value, use 'expression' command");
346 return false;
347 }
348 }
349
350 bool ret_val = m_parent->SetData(data, error);
352 return ret_val;
353}
354
356 lldb::LanguageType lang) {
358 if (m_parent)
360}
361
364 if (m_parent)
367 } else
369}
370
372 if (m_parent)
374 return false;
375}
376
378 if (m_parent)
381}
382
384 if (m_parent)
385 return m_parent->GetDeclaration(decl);
386
387 return ValueObject::GetDeclaration(decl);
388}
389
391 if (m_parent)
392 return m_parent->GetLanguageFlags();
393 return this->ValueObject::GetLanguageFlags();
394}
395
397 if (m_parent)
399 else
401}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
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
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
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
ConstString GetDisplayTypeName() 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
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 LanguageRuntime * GetPreferredLanguageRuntime(ValueObject &in_value)
Return the preferred language runtime instance, which in most cases will be the current instance.
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:341
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition: Process.cpp:1523
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:233
bool Success() const
Test for success condition.
Definition: Status.cpp:279
const ArchSpec & GetArchitecture() const
Definition: Target.h:1014
Sometimes you can find the name of the type corresponding to an object, but we don't have debug infor...
Definition: Type.h:712
CompilerType GetCompilerType() const
Definition: Type.h:726
bool HasName() const
Definition: Type.cpp:907
ConstString GetName() const
Definition: Type.cpp:864
bool HasType() const
Definition: Type.h:744
bool IsValid() const
Definition: Type.cpp:1009
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
lldb::ValueType GetValueType() const override
lldb::LanguageType GetPreferredDisplayLanguage() override
void SetLanguageFlags(uint64_t flags) override
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:976
EvaluationPoint m_update_point
Stores both the stop id and the full context at which this value was last updated.
Definition: ValueObject.h:842
virtual bool IsInScope()
Definition: ValueObject.h:420
CompilerType GetCompilerType()
Definition: ValueObject.h:352
void SetPreferredDisplayLanguage(lldb::LanguageType lt)
Definition: ValueObject.h:708
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:786
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:850
virtual uint64_t GetLanguageFlags()
Definition: ValueObject.h:784
virtual void SetSyntheticChildrenGenerated(bool b)
Definition: ValueObject.h:641
virtual std::optional< uint64_t > GetByteSize()=0
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:846
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
llvm::Expected< uint32_t > GetNumChildren(uint32_t max=UINT32_MAX)
virtual lldb::ValueType GetValueType() const =0
virtual ConstString GetTypeName()
Definition: ValueObject.h:365
void SetValueDidChange(bool value_changed)
Definition: ValueObject.h:972
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:895
lldb::LanguageType GetObjectRuntimeLanguage()
Definition: ValueObject.h:373
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:835
virtual bool GetDeclaration(Declaration &decl)
virtual bool IsSyntheticChildrenGenerated()
Definition: ValueObject.h:637
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:550
const Status & GetError()
lldb::TargetSP GetTargetSP() const
Definition: ValueObject.h:334
ConstString GetName() const
Definition: ValueObject.h:463
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 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:315
ValueType
Type that describes Value::m_value.
Definition: Value.h:41
ValueType GetValueType() const
Definition: Value.cpp:109
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
uint64_t GetValueByteSize(Status *error_ptr, ExecutionContext *exe_ctx)
Definition: Value.cpp:211
void SetValueType(ValueType value_type)
Definition: Value.h:89
const CompilerType & GetCompilerType()
Definition: Value.cpp:239
#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:314
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
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:436
@ eNoDynamicValues