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"
16#include "lldb/Utility/Log.h"
17#include "lldb/Utility/Status.h"
18
19#include "llvm/ADT/STLExtras.h"
20#include <optional>
21
22namespace lldb_private {
23class Declaration;
24}
25
26using namespace lldb_private;
27
29public:
31 : SyntheticChildrenFrontEnd(backend) {}
32
33 size_t CalculateNumChildren() override { return m_backend.GetNumChildren(); }
34
35 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override {
36 return m_backend.GetChildAtIndex(idx, true);
37 }
38
39 size_t GetIndexOfChildWithName(ConstString name) override {
41 }
42
43 bool MightHaveChildren() override { return true; }
44
45 bool Update() override { return false; }
46};
47
49 lldb::SyntheticChildrenSP filter)
50 : ValueObject(parent), m_synth_sp(std::move(filter)), m_children_byindex(),
51 m_name_toindex(), m_synthetic_children_cache(),
52 m_synthetic_children_count(UINT32_MAX),
53 m_parent_type_name(parent.GetTypeName()),
54 m_might_have_children(eLazyBoolCalculate),
55 m_provides_value(eLazyBoolCalculate) {
56 SetName(parent.GetName());
57 // Copying the data of an incomplete type won't work as it has no byte size.
61}
62
64
66 return m_parent->GetCompilerType();
67}
68
70 return m_parent->GetTypeName();
71}
72
75}
76
78 if (ConstString synth_name = m_synth_filter_up->GetSyntheticTypeName())
79 return synth_name;
80
82}
83
86
90
91 if (max < UINT32_MAX) {
92 size_t num_children = m_synth_filter_up->CalculateNumChildren(max);
93 LLDB_LOGF(log,
94 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
95 "%s and type %s, the filter returned %zu child values",
96 GetName().AsCString(), GetTypeName().AsCString(), num_children);
97 return num_children;
98 } else {
99 size_t num_children = (m_synthetic_children_count =
100 m_synth_filter_up->CalculateNumChildren(max));
101 LLDB_LOGF(log,
102 "[ValueObjectSynthetic::CalculateNumChildren] for VO of name "
103 "%s and type %s, the filter returned %zu child values",
104 GetName().AsCString(), GetTypeName().AsCString(), num_children);
105 return num_children;
106 }
107}
108
109lldb::ValueObjectSP
111 if (!m_parent)
112 return lldb::ValueObjectSP();
113 if (IsDynamic() && GetDynamicValueType() == valueType)
114 return GetSP();
115 return m_parent->GetDynamicValue(valueType);
116}
117
121 (m_synth_filter_up->MightHaveChildren() ? eLazyBoolYes : eLazyBoolNo);
123}
124
125std::optional<uint64_t> ValueObjectSynthetic::GetByteSize() {
126 return m_parent->GetByteSize();
127}
128
130 return m_parent->GetValueType();
131}
132
134 ValueObject *valobj_for_frontend = m_parent;
135 if (m_synth_sp->WantsDereference())
136 {
138 if (type.IsValid() && type.IsPointerOrReferenceType())
139 {
141 lldb::ValueObjectSP deref_sp = m_parent->Dereference(error);
142 if (error.Success())
143 valobj_for_frontend = deref_sp.get();
144 }
145 }
146 m_synth_filter_up = (m_synth_sp->GetFrontEnd(*valobj_for_frontend));
148 m_synth_filter_up = std::make_unique<DummySyntheticFrontEnd>(*m_parent);
149}
150
153
154 SetValueIsValid(false);
155 m_error.Clear();
156
157 if (!m_parent->UpdateValueIfNeeded(false)) {
158 // our parent could not update.. as we are meaningless without a parent,
159 // just stop
160 if (m_parent->GetError().Fail())
162 return false;
163 }
164
165 // regenerate the synthetic filter if our typename changes
166 // <rdar://problem/12424824>
167 ConstString new_parent_type_name = m_parent->GetTypeName();
168 if (new_parent_type_name != m_parent_type_name) {
169 LLDB_LOGF(log,
170 "[ValueObjectSynthetic::UpdateValue] name=%s, type changed "
171 "from %s to %s, recomputing synthetic filter",
172 GetName().AsCString(), m_parent_type_name.AsCString(),
173 new_parent_type_name.AsCString());
174 m_parent_type_name = new_parent_type_name;
176 }
177
178 // let our backend do its update
179 if (!m_synth_filter_up->Update()) {
180 LLDB_LOGF(log,
181 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
182 "filter said caches are stale - clearing",
183 GetName().AsCString());
184 // filter said that cached values are stale
185 {
186 std::lock_guard<std::mutex> guard(m_child_mutex);
187 m_children_byindex.clear();
188 m_name_toindex.clear();
189 }
190 // usually, an object's value can change but this does not alter its
191 // children count for a synthetic VO that might indeed happen, so we need
192 // to tell the upper echelons that they need to come back to us asking for
193 // children
195 {
196 std::lock_guard<std::mutex> guard(m_child_mutex);
198 }
201 } else {
202 LLDB_LOGF(log,
203 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
204 "filter said caches are still valid",
205 GetName().AsCString());
206 }
207
209
210 lldb::ValueObjectSP synth_val(m_synth_filter_up->GetSyntheticValue());
211
212 if (synth_val && synth_val->CanProvideValue()) {
213 LLDB_LOGF(log,
214 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
215 "filter said it can provide a value",
216 GetName().AsCString());
217
219 CopyValueData(synth_val.get());
220 } else {
221 LLDB_LOGF(log,
222 "[ValueObjectSynthetic::UpdateValue] name=%s, synthetic "
223 "filter said it will not provide a value",
224 GetName().AsCString());
225
227 // Copying the data of an incomplete type won't work as it has no byte size.
230 }
231
232 SetValueIsValid(true);
233 return true;
234}
235
236lldb::ValueObjectSP ValueObjectSynthetic::GetChildAtIndex(size_t idx,
237 bool can_create) {
239
240 LLDB_LOGF(log,
241 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, retrieving "
242 "child at index %zu",
243 GetName().AsCString(), idx);
244
246
247 ValueObject *valobj;
248 bool child_is_cached;
249 {
250 std::lock_guard<std::mutex> guard(m_child_mutex);
251 auto cached_child_it = m_children_byindex.find(idx);
252 child_is_cached = cached_child_it != m_children_byindex.end();
253 if (child_is_cached)
254 valobj = cached_child_it->second;
255 }
256
257 if (!child_is_cached) {
258 if (can_create && m_synth_filter_up != nullptr) {
259 LLDB_LOGF(log,
260 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
261 "index %zu not cached and will be created",
262 GetName().AsCString(), idx);
263
264 lldb::ValueObjectSP synth_guy = m_synth_filter_up->GetChildAtIndex(idx);
265
266 LLDB_LOGF(
267 log,
268 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at index "
269 "%zu created as %p (is "
270 "synthetic: %s)",
271 GetName().AsCString(), idx, static_cast<void *>(synth_guy.get()),
272 synth_guy.get()
273 ? (synth_guy->IsSyntheticChildrenGenerated() ? "yes" : "no")
274 : "no");
275
276 if (!synth_guy)
277 return synth_guy;
278
279 {
280 std::lock_guard<std::mutex> guard(m_child_mutex);
281 if (synth_guy->IsSyntheticChildrenGenerated())
282 m_synthetic_children_cache.push_back(synth_guy);
283 m_children_byindex[idx] = synth_guy.get();
284 }
285 synth_guy->SetPreferredDisplayLanguageIfNeeded(
287 return synth_guy;
288 } else {
289 LLDB_LOGF(log,
290 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
291 "index %zu not cached and cannot "
292 "be created (can_create = %s, synth_filter = %p)",
293 GetName().AsCString(), idx, can_create ? "yes" : "no",
294 static_cast<void *>(m_synth_filter_up.get()));
295
296 return lldb::ValueObjectSP();
297 }
298 } else {
299 LLDB_LOGF(log,
300 "[ValueObjectSynthetic::GetChildAtIndex] name=%s, child at "
301 "index %zu cached as %p",
302 GetName().AsCString(), idx, static_cast<void *>(valobj));
303
304 return valobj->GetSP();
305 }
306}
307
308lldb::ValueObjectSP
310 bool can_create) {
312
313 uint32_t index = GetIndexOfChildWithName(name);
314
315 if (index == UINT32_MAX)
316 return lldb::ValueObjectSP();
317
318 return GetChildAtIndex(index, can_create);
319}
320
323
324 uint32_t found_index = UINT32_MAX;
325 bool did_find;
326 {
327 std::lock_guard<std::mutex> guard(m_child_mutex);
328 auto name_to_index = m_name_toindex.find(name.GetCString());
329 did_find = name_to_index != m_name_toindex.end();
330 if (did_find)
331 found_index = name_to_index->second;
332 }
333
334 if (!did_find && m_synth_filter_up != nullptr) {
335 uint32_t index = m_synth_filter_up->GetIndexOfChildWithName(name);
336 if (index == UINT32_MAX)
337 return index;
338 std::lock_guard<std::mutex> guard(m_child_mutex);
339 m_name_toindex[name.GetCString()] = index;
340 return index;
341 } else if (!did_find && m_synth_filter_up == nullptr)
342 return UINT32_MAX;
343 else /*if (iter != m_name_toindex.end())*/
344 return found_index;
345}
346
348
350 return m_parent->GetSP();
351}
352
354 m_value = (source->UpdateValueIfNeeded(), source->GetValue());
356 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
357}
358
360 if (!UpdateValueIfNeeded())
361 return false;
363 return true;
364 return m_parent->CanProvideValue();
365}
366
368 Status &error) {
369 return m_parent->SetValueFromCString(value_str, error);
370}
371
373 if (m_parent) {
375 m_parent->SetFormat(format);
376 }
377 this->ValueObject::SetFormat(format);
379}
380
382 lldb::LanguageType lang) {
384 if (m_parent)
386}
387
390 if (m_parent)
393 } else
395}
396
398 if (m_parent)
400 return false;
401}
402
404 if (m_parent)
407}
408
410 if (m_parent)
411 return m_parent->GetDeclaration(decl);
412
413 return ValueObject::GetDeclaration(decl);
414}
415
417 if (m_parent)
418 return m_parent->GetLanguageFlags();
419 return this->ValueObject::GetLanguageFlags();
420}
421
423 if (m_parent)
425 else
427}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition: Log.h:344
size_t GetIndexOfChildWithName(ConstString name) override
DummySyntheticFrontEnd(ValueObject &backend)
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
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:39
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
Definition: ConstString.h:195
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:218
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 GetChildMemberWithName(ConstString name, bool can_create) override
bool SetValueFromCString(const char *value_str, Status &error) override
size_t CalculateNumChildren(uint32_t max) override
Should only be called by ValueObject::GetNumChildren().
lldb::ValueType GetValueType() const override
lldb::LanguageType GetPreferredDisplayLanguage() override
void SetFormat(lldb::Format format) override
NameToIndexMap m_name_toindex
Guarded by m_child_mutex;.
lldb::ValueObjectSP GetChildAtIndex(size_t idx, bool can_create) override
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.
size_t GetIndexOfChildWithName(ConstString name) override
ValueObjectSynthetic(ValueObject &parent, lldb::SyntheticChildrenSP filter)
void SetLanguageFlags(uint64_t flags) override
void SetValueIsValid(bool valid)
Definition: ValueObject.h:980
virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx, bool can_create)
void ClearUserVisibleData(uint32_t items=ValueObject::eClearUserVisibleDataItemsAllStrings)
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:713
struct lldb_private::ValueObject::Bitflags m_flags
lldb::ValueObjectSP GetSP()
Definition: ValueObject.h:555
virtual void SetLanguageFlags(uint64_t flags)
Definition: ValueObject.h:791
Status m_error
An error object that can describe any errors that occur when updating values.
Definition: ValueObject.h:855
virtual uint64_t GetLanguageFlags()
Definition: ValueObject.h:789
virtual void SetSyntheticChildrenGenerated(bool b)
Definition: ValueObject.h:645
virtual std::optional< uint64_t > GetByteSize()=0
DataExtractor m_data
A data extractor that can be used to extract the value.
Definition: ValueObject.h:851
virtual size_t GetIndexOfChildWithName(ConstString name)
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:900
ValueObject * m_parent
The parent value object, or nullptr if this has no parent.
Definition: ValueObject.h:840
virtual bool GetDeclaration(Declaration &decl)
virtual bool IsSyntheticChildrenGenerated()
Definition: ValueObject.h:641
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
Definition: ValueObject.h:560
const Status & GetError()
virtual lldb::ValueObjectSP Dereference(Status &error)
ConstString GetName() const
Definition: ValueObject.h:467
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:705
const ExecutionContextRef & GetExecutionContextRef() const
Definition: ValueObject.h:330
virtual bool CanProvideValue()
const Value & GetValue() const
Definition: ValueObject.h:497
virtual lldb::LanguageType GetPreferredDisplayLanguage()
Status GetValueAsData(ExecutionContext *exe_ctx, DataExtractor &data, Module *module)
Definition: Value.cpp:313
#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:309
Format
Display format definitions.
LanguageType
Programming language type.
@ eLanguageTypeUnknown
Unknown or invalid language value.