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 (auto *preferred_runtime =
154 // Try the preferred runtime first.
155 found_dynamic_type = preferred_runtime->GetDynamicTypeAndAddress(
156 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
157 value_type);
158 if (found_dynamic_type)
159 // Set the operative `runtime` for later use in this function.
160 runtime = preferred_runtime;
161 }
162 if (!found_dynamic_type)
163 // Fallback to the runtime for `known_type`.
164 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
165 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
166 value_type);
167 } else {
169 if (runtime)
170 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
171 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
172 value_type);
173
174 if (!found_dynamic_type) {
176 if (runtime)
177 found_dynamic_type = runtime->GetDynamicTypeAndAddress(
178 *m_parent, m_use_dynamic, class_type_or_name, dynamic_address,
179 value_type);
180 }
181 }
182
183 // Getting the dynamic value may have run the program a bit, and so marked us
184 // as needing updating, but we really don't...
185
187
188 if (runtime && found_dynamic_type) {
189 if (class_type_or_name.HasType()) {
192 runtime->FixUpDynamicType(class_type_or_name, *m_parent)
193 .GetCompilerType());
194 } else {
196 }
197 } else {
199 }
200
201 // If we don't have a dynamic type, set ourselves to be invalid and return
202 // false. We used to try to produce a dynamic ValueObject that behaved "like"
203 // its parent, but that failed for ValueObjectConstResult, which is too
204 // complex a beast to try to emulate. If we return an invalid ValueObject,
205 // clients will end up getting the static value instead, which behaves
206 // correctly.
207 if (!found_dynamic_type) {
209 SetValueDidChange(true);
212 m_error.SetErrorString("no dynamic type found");
213 return false;
214 }
215
216 Value old_value(m_value);
217
218 Log *log = GetLog(LLDBLog::Types);
219
220 bool has_changed_type = false;
221
222 if (!m_dynamic_type_info) {
223 m_dynamic_type_info = class_type_or_name;
224 has_changed_type = true;
225 } else if (class_type_or_name != m_dynamic_type_info) {
226 // We are another type, we need to tear down our children...
227 m_dynamic_type_info = class_type_or_name;
228 SetValueDidChange(true);
229 has_changed_type = true;
230 }
231
232 if (has_changed_type)
234
235 if (!m_address.IsValid() || m_address != dynamic_address) {
236 if (m_address.IsValid())
237 SetValueDidChange(true);
238
239 // We've moved, so we should be fine...
240 m_address = dynamic_address;
241 lldb::TargetSP target_sp(GetTargetSP());
242 lldb::addr_t load_address = m_address.GetLoadAddress(target_sp.get());
243 m_value.GetScalar() = load_address;
244 }
245
246 if (runtime)
249
251
252 m_value.SetValueType(value_type);
253
254 if (has_changed_type && log)
255 LLDB_LOGF(log, "[%s %p] has a new dynamic type %s", GetName().GetCString(),
256 static_cast<void *>(this), GetTypeName().GetCString());
257
259 // The variable value is in the Scalar value inside the m_value. We can
260 // point our m_data right to it.
261 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
262 if (m_error.Success()) {
263 if (!CanProvideValue()) {
264 // this value object represents an aggregate type whose children have
265 // values, but this object does not. So we say we are changed if our
266 // location has changed.
268 m_value.GetScalar() != old_value.GetScalar());
269 }
270
271 SetValueIsValid(true);
272 return true;
273 }
274 }
275
276 // We get here if we've failed above...
277 SetValueIsValid(false);
278 return false;
279}
280
282
284 Status &error) {
285 if (!UpdateValueIfNeeded(false)) {
286 error.SetErrorString("unable to read value");
287 return false;
288 }
289
290 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
291 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
292
293 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
294 error.SetErrorString("unable to read value");
295 return false;
296 }
297
298 // if we are at an offset from our parent, in order to set ourselves
299 // correctly we would need to change the new value so that it refers to the
300 // correct dynamic type. we choose not to deal with that - if anything more
301 // than a value overwrite is required, you should be using the expression
302 // parser instead of the value editing facility
303 if (my_value != parent_value) {
304 // but NULL'ing out a value should always be allowed
305 if (strcmp(value_str, "0")) {
306 error.SetErrorString(
307 "unable to modify dynamic value, use 'expression' command");
308 return false;
309 }
310 }
311
312 bool ret_val = m_parent->SetValueFromCString(value_str, error);
314 return ret_val;
315}
316
318 if (!UpdateValueIfNeeded(false)) {
319 error.SetErrorString("unable to read value");
320 return false;
321 }
322
323 uint64_t my_value = GetValueAsUnsigned(UINT64_MAX);
324 uint64_t parent_value = m_parent->GetValueAsUnsigned(UINT64_MAX);
325
326 if (my_value == UINT64_MAX || parent_value == UINT64_MAX) {
327 error.SetErrorString("unable to read value");
328 return false;
329 }
330
331 // if we are at an offset from our parent, in order to set ourselves
332 // correctly we would need to change the new value so that it refers to the
333 // correct dynamic type. we choose not to deal with that - if anything more
334 // than a value overwrite is required, you should be using the expression
335 // parser instead of the value editing facility
336 if (my_value != parent_value) {
337 // but NULL'ing out a value should always be allowed
338 lldb::offset_t offset = 0;
339
340 if (data.GetAddress(&offset) != 0) {
341 error.SetErrorString(
342 "unable to modify dynamic value, use 'expression' command");
343 return false;
344 }
345 }
346
347 bool ret_val = m_parent->SetData(data, error);
349 return ret_val;
350}
351
353 lldb::LanguageType lang) {
355 if (m_parent)
357}
358
361 if (m_parent)
364 } else
366}
367
369 if (m_parent)
371 return false;
372}
373
375 if (m_parent)
378}
379
381 if (m_parent)
382 return m_parent->GetDeclaration(decl);
383
384 return ValueObject::GetDeclaration(decl);
385}
386
388 if (m_parent)
389 return m_parent->GetLanguageFlags();
390 return this->ValueObject::GetLanguageFlags();
391}
392
394 if (m_parent)
396 else
398}
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
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:340
LanguageRuntime * GetLanguageRuntime(lldb::LanguageType language)
Definition: Process.cpp:1522
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:1012
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
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:969
EvaluationPoint m_update_point
Stores both the stop id and the full context at which this value was last updated.
Definition: ValueObject.h:836
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:702
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:780
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:844
virtual uint64_t GetLanguageFlags()
Definition: ValueObject.h:778
virtual void SetSyntheticChildrenGenerated(bool b)
Definition: ValueObject.h:635
virtual std::optional< uint64_t > GetByteSize()=0
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:840
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:965
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:889
lldb::LanguageType GetObjectRuntimeLanguage()
Definition: ValueObject.h:373
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:829
virtual bool GetDeclaration(Declaration &decl)
virtual bool IsSyntheticChildrenGenerated()
Definition: ValueObject.h:631
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:544
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