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>
46 GetIndexOfChildWithName(llvm::StringRef name) override {
47 return m_backend.GetIndexOfChildWithName(name);
48 }
49
50 bool MightHaveChildren() override { return m_backend.MightHaveChildren(); }
51
55};
56
59 : ValueObject(parent), m_synth_sp(std::move(filter)), m_children_byindex(),
65 SetName(parent.GetName());
66 // Copying the data of an incomplete type won't work as it has no byte size.
67 if (m_parent->GetCompilerType().IsCompleteType())
70}
71
73
77
79 return m_parent->GetTypeName();
80}
81
83 return m_parent->GetQualifiedTypeName();
84}
85
87 if (ConstString synth_name = m_synth_filter_up->GetSyntheticTypeName())
88 return synth_name;
89
90 return m_parent->GetDisplayTypeName();
91}
92
93llvm::Expected<uint32_t>
96
100
101 if (max < UINT32_MAX) {
102 auto num_children = m_synth_filter_up->CalculateNumChildren(max);
103 LLDB_LOG(log,
104 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
105 "{0} and type {1}, the filter returned {2} child values",
106 GetName(), GetTypeName(), num_children ? *num_children : 0);
107 return num_children;
108 } else {
109 auto num_children_or_err = m_synth_filter_up->CalculateNumChildren(max);
110 if (!num_children_or_err) {
112 return num_children_or_err;
113 }
114 auto num_children = (m_synthetic_children_count = *num_children_or_err);
115 LLDB_LOG(log,
116 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
117 "{0} and type {1}, the filter returned {2} child values",
118 GetName(), GetTypeName(), num_children);
119 return num_children;
120 }
121}
122
125 if (!m_parent)
126 return lldb::ValueObjectSP();
127 if (IsDynamic() && GetDynamicValueType() == valueType)
128 return GetSP();
129 return m_parent->GetDynamicValue(valueType);
130}
131
138
139llvm::Expected<uint64_t> ValueObjectSynthetic::GetByteSize() {
140 return m_parent->GetByteSize();
141}
142
144 return m_parent->GetValueType();
145}
146
148 ValueObject *valobj_for_frontend = m_parent;
149 if (m_synth_sp->WantsDereference()) {
150 CompilerType type = m_parent->GetCompilerType();
151 if (type.IsValid() && type.IsPointerOrReferenceType()) {
153 lldb::ValueObjectSP deref_sp = m_parent->Dereference(error);
154 if (error.Success())
155 valobj_for_frontend = deref_sp.get();
156 }
157 }
158 m_synth_filter_up = (m_synth_sp->GetFrontEnd(*valobj_for_frontend));
160 m_synth_filter_up = std::make_unique<DummySyntheticFrontEnd>(*m_parent);
161}
162
165
166 SetValueIsValid(false);
167 m_error.Clear();
168
169 if (!m_parent->UpdateValueIfNeeded(false)) {
170 // our parent could not update.. as we are meaningless without a parent,
171 // just stop
172 if (m_parent->GetError().Fail())
173 m_error = m_parent->GetError().Clone();
174 return false;
175 }
176
177 // Regenerate the synthetic filter if our typename changes. When the (dynamic)
178 // type of an object changes, so does their synthetic filter of choice.
179 ConstString new_parent_type_name = m_parent->GetTypeName();
180 if (new_parent_type_name != m_parent_type_name) {
181 LLDB_LOG(log,
182 "[ValueObjectSynthetic::UpdateValue] name={0}, type changed "
183 "from {1} to {2}, recomputing synthetic filter",
184 GetName(), m_parent_type_name, new_parent_type_name);
185 m_parent_type_name = new_parent_type_name;
187 }
188
189 // let our backend do its update
191 LLDB_LOG(log,
192 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
193 "filter said caches are stale - clearing",
194 GetName());
195 // filter said that cached values are stale
196 {
197 std::lock_guard<std::mutex> guard(m_child_mutex);
198 m_children_byindex.clear();
199 m_name_toindex.clear();
200 }
201 // usually, an object's value can change but this does not alter its
202 // children count for a synthetic VO that might indeed happen, so we need
203 // to tell the upper echelons that they need to come back to us asking for
204 // children
205 m_flags.m_children_count_valid = false;
206 {
207 std::lock_guard<std::mutex> guard(m_child_mutex);
209 }
212 } else {
213 LLDB_LOG(log,
214 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
215 "filter said caches are still valid",
216 GetName());
217 }
218
220
221 lldb::ValueObjectSP synth_val(m_synth_filter_up->GetSyntheticValue());
222
223 if (synth_val && synth_val->CanProvideValue()) {
224 LLDB_LOG(log,
225 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
226 "filter said it can provide a value",
227 GetName());
228
230 CopyValueData(synth_val.get());
231 } else {
232 LLDB_LOG(log,
233 "[ValueObjectSynthetic::UpdateValue] name={0}, synthetic "
234 "filter said it will not provide a value",
235 GetName());
236
238 // Copying the data of an incomplete type won't work as it has no byte size.
239 if (m_parent->GetCompilerType().IsCompleteType())
241 }
242
243 SetValueIsValid(true);
244 return true;
245}
246
248 bool can_create) {
250
251 LLDB_LOG(log,
252 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, retrieving "
253 "child at index {1}",
254 GetName(), idx);
255
257
258 ValueObject *valobj;
259 bool child_is_cached;
260 {
261 std::lock_guard<std::mutex> guard(m_child_mutex);
262 auto cached_child_it = m_children_byindex.find(idx);
263 child_is_cached = cached_child_it != m_children_byindex.end();
264 if (child_is_cached)
265 valobj = cached_child_it->second;
266 }
267
268 if (!child_is_cached) {
269 if (can_create && m_synth_filter_up != nullptr) {
270 LLDB_LOG(log,
271 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
272 "index {1} not cached and will be created",
273 GetName(), idx);
274
275 lldb::ValueObjectSP synth_guy = m_synth_filter_up->GetChildAtIndex(idx);
276
277 LLDB_LOG(
278 log,
279 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at index "
280 "{1} created as {2} (is "
281 "synthetic: {3})",
282 GetName(), idx, static_cast<void *>(synth_guy.get()),
283 synth_guy.get()
284 ? (synth_guy->IsSyntheticChildrenGenerated() ? "yes" : "no")
285 : "no");
286
287 if (!synth_guy)
288 return synth_guy;
289
290 if (synth_guy->IsSyntheticChildrenGenerated())
291 synth_guy->SetLogicalParent(this);
292
293 {
294 std::lock_guard<std::mutex> guard(m_child_mutex);
295 if (synth_guy->IsSyntheticChildrenGenerated())
296 m_synthetic_children_cache.push_back(synth_guy);
297 m_children_byindex[idx] = synth_guy.get();
298 }
299 synth_guy->SetPreferredDisplayLanguageIfNeeded(
301
302 if (lldb::ValueObjectSP check_sp =
303 CheckValueObjectOwnership(synth_guy.get()))
304 return check_sp;
305 return synth_guy;
306 } else {
307 LLDB_LOG(log,
308 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
309 "index {1} not cached and cannot "
310 "be created (can_create = {2}, synth_filter = {3})",
311 GetName(), idx, can_create ? "yes" : "no",
312 static_cast<void *>(m_synth_filter_up.get()));
313
314 return lldb::ValueObjectSP();
315 }
316 } else {
317 LLDB_LOG(log,
318 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
319 "index {1} cached as {2}",
320 GetName(), idx, static_cast<void *>(valobj));
322 if (check_sp)
323 return check_sp;
324 return valobj->GetSP();
325 }
326}
327
330 bool can_create) {
332
333 auto index_or_err = GetIndexOfChildWithName(name);
334
335 if (!index_or_err) {
336 LLDB_LOG_ERROR(GetLog(LLDBLog::DataFormatters), index_or_err.takeError(),
337 "{0}");
338 return lldb::ValueObjectSP();
339 }
340
341 return GetChildAtIndex(*index_or_err, can_create);
342}
343
344llvm::Expected<size_t>
347
348 ConstString name(name_ref);
349
350 {
351 std::lock_guard<std::mutex> guard(m_child_mutex);
352 auto name_to_index = m_name_toindex.find(name.GetCString());
353 if (name_to_index != m_name_toindex.end())
354 return name_to_index->second;
355 }
356
358 return llvm::createStringErrorV("type has no child named '{0}'", name);
359
360 size_t index;
361 auto index_or_err = m_synth_filter_up->GetIndexOfChildWithName(name);
362 if (index_or_err) {
363 index = *index_or_err;
364 } else {
365 // The front-end does its own subscripting, so its answer is final.
366 if (m_synth_sp->CustomSubscripting())
367 return index_or_err.takeError();
368
369 // Provide automatic support for subscript child names ("[N]").
370 auto maybe_index = formatters::ExtractIndexFromString(name.GetCString());
371 if (!maybe_index)
372 // The child name was not of the form "[N]", return the original error.
373 return index_or_err.takeError();
374
375 // Subscripting succeeded, ignore the original error.
376 llvm::consumeError(index_or_err.takeError());
377 index = *maybe_index;
378
379 // Prevent unnecessary work by limiting max to one past the index.
380 uint32_t max = index + 1;
381 auto num_children = GetNumChildren(max);
382 if (!num_children)
383 return num_children.takeError();
384 if (index >= *num_children)
385 return llvm::createStringErrorV("subscript index out of range: {0}",
386 index);
387 }
388
389 std::lock_guard<std::mutex> guard(m_child_mutex);
390 m_name_toindex[name.GetCString()] = index;
391 return index;
392}
393
394bool ValueObjectSynthetic::IsInScope() { return m_parent->IsInScope(); }
395
399
401 if (!source->UpdateValueIfNeeded())
402 return;
403 m_value = source->GetValue();
405 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
406}
407
409 if (!UpdateValueIfNeeded())
410 return false;
412 return true;
413 return m_parent->CanProvideValue();
414}
415
417 Status &error) {
418 return m_parent->SetValueFromCString(value_str, error);
419}
420
422 if (m_parent) {
423 m_parent->ClearUserVisibleData(eClearUserVisibleDataItemsAll);
424 m_parent->SetFormat(format);
425 }
426 this->ValueObject::SetFormat(format);
428}
429
431 lldb::LanguageType lang) {
433 if (m_parent)
434 m_parent->SetPreferredDisplayLanguage(lang);
435}
436
445
447 if (m_parent)
448 return m_parent->IsSyntheticChildrenGenerated();
449 return false;
450}
451
453 if (m_parent)
454 m_parent->SetSyntheticChildrenGenerated(b);
456}
457
459 if (m_parent)
460 return m_parent->GetDeclaration(decl);
461
462 return ValueObject::GetDeclaration(decl);
463}
464
466 if (m_parent)
467 return m_parent->GetLanguageFlags();
468 return this->ValueObject::GetLanguageFlags();
469}
470
472 if (m_parent)
473 m_parent->SetLanguageFlags(flags);
474 else
476}
477
479 GetExpressionPathFormat epformat) {
480 // A synthetic ValueObject may wrap an underlying Register or RegisterSet
481 // ValueObject, which requires a different approach to generating the
482 // expression path. In such cases, delegate to the non-synthetic value object.
483 if (const lldb::ValueType obj_value_type = GetValueType();
484 IsSynthetic() && (obj_value_type == lldb::eValueTypeRegister ||
485 obj_value_type == lldb::eValueTypeRegisterSet)) {
486
487 if (const lldb::ValueObjectSP raw_value = GetNonSyntheticValue())
488 return raw_value->GetExpressionPath(stream, epformat);
489 }
490 return ValueObject::GetExpressionPath(stream, epformat);
491}
492
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:375
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
llvm::Expected< uint32_t > CalculateNumChildren() override
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
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
Determine the index of a named child.
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
Return the index of the child named name.
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
SyntheticChildrenFrontEnd * GetSyntheticChildrenFrontEnd() 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.
llvm::Expected< uint32_t > GetNumChildren(uint32_t max=UINT32_MAX)
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
const Value & GetValue() const
#define UINT32_MAX
std::optional< size_t > ExtractIndexFromString(llvm::StringRef 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:338
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