LLDB mainline
ValueObjectSynthetic.cpp
Go to the documentation of this file.
1//===-- ValueObjectSynthetic.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
11#include "lldb/Core/Value.h"
17#include "lldb/Utility/Log.h"
18#include "lldb/Utility/Status.h"
20
21#include "llvm/ADT/STLExtras.h"
22#include "llvm/Support/Error.h"
23#include "llvm/Support/ErrorExtras.h"
24#include <optional>
25
26namespace lldb_private {
27class Declaration;
28}
29
30using namespace lldb_private;
31
33public:
36
37 llvm::Expected<uint32_t> CalculateNumChildren() override {
38 return m_backend.GetNumChildren();
39 }
40
41 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
42 return m_backend.GetChildAtIndex(idx);
43 }
44
45 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override {
46 return m_backend.GetIndexOfChildWithName(name);
47 }
48
49 bool MightHaveChildren() override { return m_backend.MightHaveChildren(); }
50
54};
55
58 : ValueObject(parent), m_synth_sp(std::move(filter)), m_children_byindex(),
64 SetName(parent.GetName());
65 // Copying the data of an incomplete type won't work as it has no byte size.
66 if (m_parent->GetCompilerType().IsCompleteType())
69}
70
72
76
78 return m_parent->GetTypeName();
79}
80
82 return m_parent->GetQualifiedTypeName();
83}
84
86 if (ConstString synth_name = m_synth_filter_up->GetSyntheticTypeName())
87 return synth_name;
88
89 return m_parent->GetDisplayTypeName();
90}
91
92llvm::Expected<uint32_t>
95
99
100 if (max < UINT32_MAX) {
101 auto num_children = m_synth_filter_up->CalculateNumChildren(max);
102 LLDB_LOG(log,
103 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
104 "{0} and type {1}, the filter returned {2} child values",
105 GetName(), GetTypeName(), num_children ? *num_children : 0);
106 return num_children;
107 } else {
108 auto num_children_or_err = m_synth_filter_up->CalculateNumChildren(max);
109 if (!num_children_or_err) {
111 return num_children_or_err;
112 }
113 auto num_children = (m_synthetic_children_count = *num_children_or_err);
114 LLDB_LOG(log,
115 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
116 "{0} and type {1}, the filter returned {2} child values",
117 GetName(), GetTypeName(), num_children);
118 return num_children;
119 }
120}
121
124 if (!m_parent)
125 return lldb::ValueObjectSP();
126 if (IsDynamic() && GetDynamicValueType() == valueType)
127 return GetSP();
128 return m_parent->GetDynamicValue(valueType);
129}
130
137
138llvm::Expected<uint64_t> ValueObjectSynthetic::GetByteSize() {
139 return m_parent->GetByteSize();
140}
141
143 return m_parent->GetValueType();
144}
145
147 ValueObject *valobj_for_frontend = m_parent;
148 if (m_synth_sp->WantsDereference()) {
149 CompilerType type = m_parent->GetCompilerType();
150 if (type.IsValid() && type.IsPointerOrReferenceType()) {
152 lldb::ValueObjectSP deref_sp = m_parent->Dereference(error);
153 if (error.Success())
154 valobj_for_frontend = deref_sp.get();
155 }
156 }
157 m_synth_filter_up = (m_synth_sp->GetFrontEnd(*valobj_for_frontend));
159 m_synth_filter_up = std::make_unique<DummySyntheticFrontEnd>(*m_parent);
160}
161
164
165 SetValueIsValid(false);
166 m_error.Clear();
167
168 if (!m_parent->UpdateValueIfNeeded(false)) {
169 // our parent could not update.. as we are meaningless without a parent,
170 // just stop
171 if (m_parent->GetError().Fail())
172 m_error = m_parent->GetError().Clone();
173 return false;
174 }
175
176 // Regenerate the synthetic filter if our typename changes. When the (dynamic)
177 // type of an object changes, so does their synthetic filter of choice.
178 ConstString new_parent_type_name = m_parent->GetTypeName();
179 if (new_parent_type_name != m_parent_type_name) {
180 LLDB_LOG(log,
181 "[ValueObjectSynthetic::UpdateValue] name={0}, type changed "
182 "from {1} to {2}, recomputing synthetic filter",
183 GetName(), m_parent_type_name, new_parent_type_name);
184 m_parent_type_name = new_parent_type_name;
186 }
187
188 // let our backend do its update
190 LLDB_LOG(log,
191 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
192 "filter said caches are stale - clearing",
193 GetName());
194 // filter said that cached values are stale
195 {
196 std::lock_guard<std::mutex> guard(m_child_mutex);
197 m_children_byindex.clear();
198 m_name_toindex.clear();
199 }
200 // usually, an object's value can change but this does not alter its
201 // children count for a synthetic VO that might indeed happen, so we need
202 // to tell the upper echelons that they need to come back to us asking for
203 // children
204 m_flags.m_children_count_valid = false;
205 {
206 std::lock_guard<std::mutex> guard(m_child_mutex);
208 }
211 } else {
212 LLDB_LOG(log,
213 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
214 "filter said caches are still valid",
215 GetName());
216 }
217
219
220 lldb::ValueObjectSP synth_val(m_synth_filter_up->GetSyntheticValue());
221
222 if (synth_val && synth_val->CanProvideValue()) {
223 LLDB_LOG(log,
224 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
225 "filter said it can provide a value",
226 GetName());
227
229 CopyValueData(synth_val.get());
230 } else {
231 LLDB_LOG(log,
232 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
233 "filter said it will not provide a value",
234 GetName());
235
237 // Copying the data of an incomplete type won't work as it has no byte size.
238 if (m_parent->GetCompilerType().IsCompleteType())
240 }
241
242 SetValueIsValid(true);
243 return true;
244}
245
247 bool can_create) {
249
250 LLDB_LOG(log,
251 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, retrieving "
252 "child at index {1}",
253 GetName(), idx);
254
256
257 ValueObject *valobj;
258 bool child_is_cached;
259 {
260 std::lock_guard<std::mutex> guard(m_child_mutex);
261 auto cached_child_it = m_children_byindex.find(idx);
262 child_is_cached = cached_child_it != m_children_byindex.end();
263 if (child_is_cached)
264 valobj = cached_child_it->second;
265 }
266
267 if (!child_is_cached) {
268 if (can_create && m_synth_filter_up != nullptr) {
269 LLDB_LOG(log,
270 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
271 "index {1} not cached and will be created",
272 GetName(), idx);
273
274 lldb::ValueObjectSP synth_guy = m_synth_filter_up->GetChildAtIndex(idx);
275
276 LLDB_LOG(
277 log,
278 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at index "
279 "{1} created as {2} (is "
280 "synthetic: {3})",
281 GetName(), idx, static_cast<void *>(synth_guy.get()),
282 synth_guy.get()
283 ? (synth_guy->IsSyntheticChildrenGenerated() ? "yes" : "no")
284 : "no");
285
286 if (!synth_guy)
287 return synth_guy;
288
289 if (synth_guy->IsSyntheticChildrenGenerated())
290 synth_guy->SetLogicalParent(this);
291
292 {
293 std::lock_guard<std::mutex> guard(m_child_mutex);
294 if (synth_guy->IsSyntheticChildrenGenerated())
295 m_synthetic_children_cache.push_back(synth_guy);
296 m_children_byindex[idx] = synth_guy.get();
297 }
298 synth_guy->SetPreferredDisplayLanguageIfNeeded(
300
301 if (lldb::ValueObjectSP check_sp =
302 CheckValueObjectOwnership(synth_guy.get()))
303 return check_sp;
304 return synth_guy;
305 } else {
306 LLDB_LOG(log,
307 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
308 "index {1} not cached and cannot "
309 "be created (can_create = {2}, synth_filter = {3})",
310 GetName(), idx, can_create ? "yes" : "no",
311 static_cast<void *>(m_synth_filter_up.get()));
312
313 return lldb::ValueObjectSP();
314 }
315 } else {
316 LLDB_LOG(log,
317 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
318 "index {1} cached as {2}",
319 GetName(), idx, static_cast<void *>(valobj));
321 if (check_sp)
322 return check_sp;
323 return valobj->GetSP();
324 }
325}
326
329 bool can_create) {
331
332 auto index_or_err = GetIndexOfChildWithName(name);
333
334 if (!index_or_err) {
335 llvm::consumeError(index_or_err.takeError());
336 return lldb::ValueObjectSP();
337 }
338
339 return GetChildAtIndex(*index_or_err, can_create);
340}
341
342llvm::Expected<size_t>
345
346 ConstString name(name_ref);
347
348 std::optional<uint32_t> found_index = std::nullopt;
349 {
350 std::lock_guard<std::mutex> guard(m_child_mutex);
351 auto name_to_index = m_name_toindex.find(name.GetCString());
352 if (name_to_index != m_name_toindex.end())
353 found_index = name_to_index->second;
354 }
355
356 if (!found_index && m_synth_filter_up != nullptr) {
357 size_t index = SIZE_MAX;
358 if (auto index_or_err = m_synth_filter_up->GetIndexOfChildWithName(name)) {
359 index = *index_or_err;
360 } else if (!m_synth_sp->CustomSubscripting()) {
361 // Provide automatic support for subscript child names ("[N]").
362 auto maybe_index = formatters::ExtractIndexFromString(name.GetCString());
363 if (!maybe_index)
364 // The child name was not of the form "[N]", return the original error.
365 return index_or_err.takeError();
366
367 // Subscripting succeeded, ignore the original error.
368 llvm::consumeError(index_or_err.takeError());
369 index = *maybe_index;
370
371 // Prevent unnecessary work by limiting max to one past the index.
372 uint32_t max = index + 1;
373 auto num_children = GetNumChildrenIgnoringErrors(max);
374 if (index >= num_children)
375 return llvm::createStringErrorV("subscript index out of range: {0}",
376 index);
377 }
378 std::lock_guard<std::mutex> guard(m_child_mutex);
379 m_name_toindex[name.GetCString()] = index;
380 return index;
381 } else if (!found_index && m_synth_filter_up == nullptr) {
382 return llvm::createStringErrorV("type has no child named '{0}'", name);
383 } else if (found_index)
384 return *found_index;
385
386 return llvm::createStringErrorV("type has no child named '{0}'", name);
387}
388
389bool ValueObjectSynthetic::IsInScope() { return m_parent->IsInScope(); }
390
394
396 if (!source->UpdateValueIfNeeded())
397 return;
398 m_value = source->GetValue();
400 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
401}
402
404 if (!UpdateValueIfNeeded())
405 return false;
407 return true;
408 return m_parent->CanProvideValue();
409}
410
412 Status &error) {
413 return m_parent->SetValueFromCString(value_str, error);
414}
415
417 if (m_parent) {
418 m_parent->ClearUserVisibleData(eClearUserVisibleDataItemsAll);
419 m_parent->SetFormat(format);
420 }
421 this->ValueObject::SetFormat(format);
423}
424
426 lldb::LanguageType lang) {
428 if (m_parent)
429 m_parent->SetPreferredDisplayLanguage(lang);
430}
431
440
442 if (m_parent)
443 return m_parent->IsSyntheticChildrenGenerated();
444 return false;
445}
446
448 if (m_parent)
449 m_parent->SetSyntheticChildrenGenerated(b);
451}
452
454 if (m_parent)
455 return m_parent->GetDeclaration(decl);
456
457 return ValueObject::GetDeclaration(decl);
458}
459
461 if (m_parent)
462 return m_parent->GetLanguageFlags();
463 return this->ValueObject::GetLanguageFlags();
464}
465
467 if (m_parent)
468 m_parent->SetLanguageFlags(flags);
469 else
471}
472
474 GetExpressionPathFormat epformat) {
475 // A synthetic ValueObject may wrap an underlying Register or RegisterSet
476 // ValueObject, which requires a different approach to generating the
477 // expression path. In such cases, delegate to the non-synthetic value object.
478 if (const lldb::ValueType obj_value_type = GetValueType();
479 IsSynthetic() && (obj_value_type == lldb::eValueTypeRegister ||
480 obj_value_type == lldb::eValueTypeRegisterSet)) {
481
482 if (const lldb::ValueObjectSP raw_value = GetNonSyntheticValue())
483 return raw_value->GetExpressionPath(stream, epformat);
484 }
485 return ValueObject::GetExpressionPath(stream, epformat);
486}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:364
llvm::Expected< uint32_t > CalculateNumChildren() override
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
DummySyntheticFrontEnd(ValueObject &backend)
Generic representation of a type in a programming language.
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
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.
An error handling class.
Definition Status.h:118
A stream class that can stream formatted output to a file.
Definition Stream.h:28
SyntheticChildrenFrontEnd(ValueObject &backend)
virtual lldb::DynamicValueType GetDynamicValueType()
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx, bool can_create=true) override
bool SetValueFromCString(const char *value_str, Status &error) override
lldb::ValueType GetValueType() const override
lldb::LanguageType GetPreferredDisplayLanguage() override
void GetExpressionPath(Stream &stream, GetExpressionPathFormat epformat=eGetExpressionPathFormatDereferencePointers) override
void SetFormat(lldb::Format format) override
NameToIndexMap m_name_toindex
Guarded by m_child_mutex;.
ByIndexMap m_children_byindex
Guarded by m_child_mutex;.
std::unique_ptr< SyntheticChildrenFrontEnd > m_synth_filter_up
lldb::ValueObjectSP GetDynamicValue(lldb::DynamicValueType valueType) override
void SetSyntheticChildrenGenerated(bool b) override
void SetPreferredDisplayLanguage(lldb::LanguageType)
lldb::ValueObjectSP GetNonSyntheticValue() override
SyntheticChildrenCache m_synthetic_children_cache
Guarded by m_child_mutex;.
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
bool GetDeclaration(Declaration &decl) override
bool MightHaveChildren() override
Find out if a ValueObject might have children.
lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true) override
llvm::Expected< uint64_t > GetByteSize() override
ValueObjectSynthetic(ValueObject &parent, lldb::SyntheticChildrenSP filter)
void SetLanguageFlags(uint64_t flags) override
llvm::Expected< uint32_t > CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
void SetValueIsValid(bool valid)
lldb::ValueObjectSP CheckValueObjectOwnership(ValueObject *child)
void ClearUserVisibleData(uint32_t items=ValueObject::eClearUserVisibleDataItemsAllStrings)
void SetPreferredDisplayLanguage(lldb::LanguageType lt)
struct lldb_private::ValueObject::Bitflags m_flags
lldb::ValueObjectSP GetSP()
virtual void SetLanguageFlags(uint64_t flags)
Status m_error
An error object that can describe any errors that occur when updating values.
virtual uint64_t GetLanguageFlags()
virtual void SetSyntheticChildrenGenerated(bool b)
DataExtractor m_data
A data extractor that can be used to extract the value.
virtual void GetExpressionPath(Stream &s, GetExpressionPathFormat=eGetExpressionPathFormatDereferencePointers)
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
lldb::LanguageType m_preferred_display_language
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
virtual bool GetDeclaration(Declaration &decl)
void SetName(llvm::StringRef name)
Change the name of the current ValueObject.
bool UpdateValueIfNeeded(bool update_format=true)
ConstString GetName() const
virtual void SetFormat(lldb::Format format)
const ExecutionContextRef & GetExecutionContextRef() const
uint32_t GetNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
Like GetNumChildren but returns 0 on error.
const Value & GetValue() const
#define UINT32_MAX
std::optional< size_t > ExtractIndexFromString(const char *item_name)
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
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Format
Display format definitions.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
@ eValueTypeRegister
stack frame register value
@ eValueTypeRegisterSet
A collection of stack frame register values.
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP