LLDB mainline
ValueObjectSyntheticFilter.cpp
Go to the documentation of this file.
1//===-- ValueObjectSyntheticFilter.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"
19
20#include "llvm/ADT/STLExtras.h"
21#include <optional>
22
23namespace lldb_private {
24class Declaration;
25}
26
27using namespace lldb_private;
28
30public:
32 : SyntheticChildrenFrontEnd(backend) {}
33
34 llvm::Expected<uint32_t> CalculateNumChildren() override {
36 }
37
38 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override {
39 return m_backend.GetChildAtIndex(idx);
40 }
41
42 size_t GetIndexOfChildWithName(ConstString name) override {
44 }
45
46 bool MightHaveChildren() override { return m_backend.MightHaveChildren(); }
47
50 }
51};
52
55 : ValueObject(parent), m_synth_sp(std::move(filter)), m_children_byindex(),
56 m_name_toindex(), m_synthetic_children_cache(),
57 m_synthetic_children_count(UINT32_MAX),
58 m_parent_type_name(parent.GetTypeName()),
59 m_might_have_children(eLazyBoolCalculate),
60 m_provides_value(eLazyBoolCalculate) {
61 SetName(parent.GetName());
62 // Copying the data of an incomplete type won't work as it has no byte size.
66}
67
69
71 return m_parent->GetCompilerType();
72}
73
75 return m_parent->GetTypeName();
76}
77
80}
81
83 if (ConstString synth_name = m_synth_filter_up->GetSyntheticTypeName())
84 return synth_name;
85
87}
88
89llvm::Expected<uint32_t>
92
96
97 if (max < UINT32_MAX) {
98 auto num_children = m_synth_filter_up->CalculateNumChildren(max);
99 LLDB_LOGF(log,
100 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
101 "%s and type %s, the filter returned %u child values",
102 GetName().AsCString(), GetTypeName().AsCString(),
103 num_children ? *num_children : 0);
104 return num_children;
105 } else {
106 auto num_children_or_err = m_synth_filter_up->CalculateNumChildren(max);
107 if (!num_children_or_err) {
109 return num_children_or_err;
110 }
111 auto num_children = (m_synthetic_children_count = *num_children_or_err);
112 LLDB_LOGF(log,
113 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
114 "%s and type %s, the filter returned %u child values",
115 GetName().AsCString(), GetTypeName().AsCString(), num_children);
116 return num_children;
117 }
118}
119
122 if (!m_parent)
123 return lldb::ValueObjectSP();
124 if (IsDynamic() && GetDynamicValueType() == valueType)
125 return GetSP();
126 return m_parent->GetDynamicValue(valueType);
127}
128
132 (m_synth_filter_up->MightHaveChildren() ? eLazyBoolYes : eLazyBoolNo);
134}
135
136std::optional<uint64_t> ValueObjectSynthetic::GetByteSize() {
137 return m_parent->GetByteSize();
138}
139
141 return m_parent->GetValueType();
142}
143
145 ValueObject *valobj_for_frontend = m_parent;
146 if (m_synth_sp->WantsDereference())
147 {
149 if (type.IsValid() && type.IsPointerOrReferenceType())
150 {
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())
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_LOGF(log,
181 "[ValueObjectSynthetic::UpdateValue] name=%s, type changed "
182 "from %s to %s, recomputing synthetic filter",
183 GetName().AsCString(), m_parent_type_name.AsCString(),
184 new_parent_type_name.AsCString());
185 m_parent_type_name = new_parent_type_name;
187 }
188
189 // let our backend do its update
191 LLDB_LOGF(log,
192 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
193 "filter said caches are stale - clearing",
194 GetName().AsCString());
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
206 {
207 std::lock_guard<std::mutex> guard(m_child_mutex);
209 }
212 } else {
213 LLDB_LOGF(log,
214 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
215 "filter said caches are still valid",
216 GetName().AsCString());
217 }
218
220
221 lldb::ValueObjectSP synth_val(m_synth_filter_up->GetSyntheticValue());
222
223 if (synth_val && synth_val->CanProvideValue()) {
224 LLDB_LOGF(log,
225 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
226 "filter said it can provide a value",
227 GetName().AsCString());
228
230 CopyValueData(synth_val.get());
231 } else {
232 LLDB_LOGF(log,
233 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
234 "filter said it will not provide a value",
235 GetName().AsCString());
236
238 // Copying the data of an incomplete type won't work as it has no byte size.
241 }
242
243 SetValueIsValid(true);
244 return true;
245}
246
248 bool can_create) {
250
251 LLDB_LOGF(log,
252 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, retrieving "
253 "child at index %u",
254 GetName().AsCString(), 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_LOGF(log,
271 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
272 "index %u not cached and will be created",
273 GetName().AsCString(), idx);
274
275 lldb::ValueObjectSP synth_guy = m_synth_filter_up->GetChildAtIndex(idx);
276
277 LLDB_LOGF(
278 log,
279 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at index "
280 "%u created as %p (is "
281 "synthetic: %s)",
282 GetName().AsCString(), 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 {
291 std::lock_guard<std::mutex> guard(m_child_mutex);
292 if (synth_guy->IsSyntheticChildrenGenerated())
293 m_synthetic_children_cache.push_back(synth_guy);
294 m_children_byindex[idx] = synth_guy.get();
295 }
296 synth_guy->SetPreferredDisplayLanguageIfNeeded(
298 return synth_guy;
299 } else {
300 LLDB_LOGF(log,
301 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
302 "index %u not cached and cannot "
303 "be created (can_create = %s, synth_filter = %p)",
304 GetName().AsCString(), idx, can_create ? "yes" : "no",
305 static_cast<void *>(m_synth_filter_up.get()));
306
307 return lldb::ValueObjectSP();
308 }
309 } else {
310 LLDB_LOGF(log,
311 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
312 "index %u cached as %p",
313 GetName().AsCString(), idx, static_cast<void *>(valobj));
314
315 return valobj->GetSP();
316 }
317}
318
321 bool can_create) {
323
324 uint32_t index = GetIndexOfChildWithName(name);
325
326 if (index == UINT32_MAX)
327 return lldb::ValueObjectSP();
328
329 return GetChildAtIndex(index, can_create);
330}
331
332size_t ValueObjectSynthetic::GetIndexOfChildWithName(llvm::StringRef name_ref) {
334
335 ConstString name(name_ref);
336
337 uint32_t found_index = UINT32_MAX;
338 bool did_find;
339 {
340 std::lock_guard<std::mutex> guard(m_child_mutex);
341 auto name_to_index = m_name_toindex.find(name.GetCString());
342 did_find = name_to_index != m_name_toindex.end();
343 if (did_find)
344 found_index = name_to_index->second;
345 }
346
347 if (!did_find && m_synth_filter_up != nullptr) {
348 uint32_t index = m_synth_filter_up->GetIndexOfChildWithName(name);
349 if (index == UINT32_MAX)
350 return index;
351 std::lock_guard<std::mutex> guard(m_child_mutex);
352 m_name_toindex[name.GetCString()] = index;
353 return index;
354 } else if (!did_find && m_synth_filter_up == nullptr)
355 return UINT32_MAX;
356 else /*if (iter != m_name_toindex.end())*/
357 return found_index;
358}
359
361
363 return m_parent->GetSP();
364}
365
367 m_value = (source->UpdateValueIfNeeded(), source->GetValue());
369 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
370}
371
373 if (!UpdateValueIfNeeded())
374 return false;
376 return true;
377 return m_parent->CanProvideValue();
378}
379
381 Status &error) {
382 return m_parent->SetValueFromCString(value_str, error);
383}
384
386 if (m_parent) {
388 m_parent->SetFormat(format);
389 }
390 this->ValueObject::SetFormat(format);
392}
393
395 lldb::LanguageType lang) {
397 if (m_parent)
399}
400
403 if (m_parent)
406 } else
408}
409
411 if (m_parent)
413 return false;
414}
415
417 if (m_parent)
420}
421
423 if (m_parent)
424 return m_parent->GetDeclaration(decl);
425
426 return ValueObject::GetDeclaration(decl);
427}
428
430 if (m_parent)
431 return m_parent->GetLanguageFlags();
432 return this->ValueObject::GetLanguageFlags();
433}
434
436 if (m_parent)
438 else
440}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:349
llvm::Expected< uint32_t > CalculateNumChildren() override
size_t GetIndexOfChildWithName(ConstString name) 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
DummySyntheticFrontEnd(ValueObject &backend)
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
bool IsPointerOrReferenceType(CompilerType *pointee_type=nullptr) const
A uniqued constant string class.
Definition: ConstString.h:40
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:188
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:214
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:44
void Clear()
Clear the object state.
Definition: Status.cpp:167
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
virtual lldb::DynamicValueType GetDynamicValueType()
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx, bool can_create=true) override
size_t GetIndexOfChildWithName(llvm::StringRef name) override
bool SetValueFromCString(const char *value_str, Status &error) override
lldb::ValueType GetValueType() const override
lldb::LanguageType GetPreferredDisplayLanguage() 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
lldb::ValueObjectSP GetNonSyntheticValue() override
SyntheticChildrenCache m_synthetic_children_cache
Guarded by m_child_mutex;.
std::optional< uint64_t > GetByteSize() 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
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)
Definition: ValueObject.h:976
void ClearUserVisibleData(uint32_t items=ValueObject::eClearUserVisibleDataItemsAllStrings)
virtual bool IsInScope()
Definition: ValueObject.h:420
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx, bool can_create=true)
virtual bool MightHaveChildren()
Find out if a ValueObject might have children.
CompilerType GetCompilerType()
Definition: ValueObject.h:352
void SetPreferredDisplayLanguage(lldb::LanguageType lt)
Definition: ValueObject.h:708
struct lldb_private::ValueObject::Bitflags m_flags
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:545
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
virtual size_t GetIndexOfChildWithName(llvm::StringRef name)
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:846
llvm::Expected< uint32_t > GetNumChildren(uint32_t max=UINT32_MAX)
virtual lldb::ValueType GetValueType() const =0
virtual ConstString GetTypeName()
Definition: ValueObject.h:365
virtual lldb::ModuleSP GetModule()
Return the module associated with this value object in case the value is from an executable file and ...
virtual lldb::ValueObjectSP GetDynamicValue(lldb::DynamicValueType valueType)
virtual ConstString GetDisplayTypeName()
Definition: ValueObject.h:367
lldb::LanguageType m_preferred_display_language
Definition: ValueObject.h:895
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()
virtual lldb::ValueObjectSP Dereference(Status &error)
ConstString GetName() const
Definition: ValueObject.h:463
virtual bool SetValueFromCString(const char *value_str, Status &error)
virtual ConstString GetQualifiedTypeName()
Definition: ValueObject.h:369
virtual void SetFormat(lldb::Format format)
Definition: ValueObject.h:700
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
virtual bool CanProvideValue()
const Value & GetValue() const
Definition: ValueObject.h:487
virtual lldb::LanguageType GetPreferredDisplayLanguage()
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:315
#define UINT32_MAX
Definition: lldb-defines.h:19
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
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
Definition: lldb-forward.h:472
Format
Display format definitions.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP
Definition: lldb-forward.h:433