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 {
290 std::lock_guard<std::mutex> guard(m_child_mutex);
291 if (synth_guy->IsSyntheticChildrenGenerated())
292 m_synthetic_children_cache.push_back(synth_guy);
293 m_children_byindex[idx] = synth_guy.get();
294 }
295 synth_guy->SetPreferredDisplayLanguageIfNeeded(
297 return synth_guy;
298 } else {
299 LLDB_LOG(log,
300 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
301 "index {1} not cached and cannot "
302 "be created (can_create = {2}, synth_filter = {3})",
303 GetName(), idx, can_create ? "yes" : "no",
304 static_cast<void *>(m_synth_filter_up.get()));
305
306 return lldb::ValueObjectSP();
307 }
308 } else {
309 LLDB_LOG(log,
310 "[ValueObjectSynthetic::GetChildAtIndex] name={0}, child at "
311 "index {1} cached as {2}",
312 GetName(), idx, static_cast<void *>(valobj));
313
314 return valobj->GetSP();
315 }
316}
317
320 bool can_create) {
322
323 auto index_or_err = GetIndexOfChildWithName(name);
324
325 if (!index_or_err) {
326 llvm::consumeError(index_or_err.takeError());
327 return lldb::ValueObjectSP();
328 }
329
330 return GetChildAtIndex(*index_or_err, can_create);
331}
332
333llvm::Expected<size_t>
336
337 ConstString name(name_ref);
338
339 std::optional<uint32_t> found_index = std::nullopt;
340 {
341 std::lock_guard<std::mutex> guard(m_child_mutex);
342 auto name_to_index = m_name_toindex.find(name.GetCString());
343 if (name_to_index != m_name_toindex.end())
344 found_index = name_to_index->second;
345 }
346
347 if (!found_index && m_synth_filter_up != nullptr) {
348 size_t index = SIZE_MAX;
349 if (auto index_or_err = m_synth_filter_up->GetIndexOfChildWithName(name)) {
350 index = *index_or_err;
351 } else if (!m_synth_sp->CustomSubscripting()) {
352 // Provide automatic support for subscript child names ("[N]").
353 auto maybe_index = formatters::ExtractIndexFromString(name.GetCString());
354 if (!maybe_index)
355 // The child name was not of the form "[N]", return the original error.
356 return index_or_err.takeError();
357
358 // Subscripting succeeded, ignore the original error.
359 llvm::consumeError(index_or_err.takeError());
360 index = *maybe_index;
361
362 // Prevent unnecessary work by limiting max to one past the index.
363 uint32_t max = index + 1;
364 auto num_children = GetNumChildrenIgnoringErrors(max);
365 if (index >= num_children)
366 return llvm::createStringErrorV("subscript index out of range: {0}",
367 index);
368 }
369 std::lock_guard<std::mutex> guard(m_child_mutex);
370 m_name_toindex[name.GetCString()] = index;
371 return index;
372 } else if (!found_index && m_synth_filter_up == nullptr) {
373 return llvm::createStringErrorV("type has no child named '{0}'", name);
374 } else if (found_index)
375 return *found_index;
376
377 return llvm::createStringErrorV("type has no child named '{0}'", name);
378}
379
380bool ValueObjectSynthetic::IsInScope() { return m_parent->IsInScope(); }
381
385
387 if (!source->UpdateValueIfNeeded())
388 return;
389 m_value = source->GetValue();
391 m_error = m_value.GetValueAsData(&exe_ctx, m_data, GetModule().get());
392}
393
395 if (!UpdateValueIfNeeded())
396 return false;
398 return true;
399 return m_parent->CanProvideValue();
400}
401
403 Status &error) {
404 return m_parent->SetValueFromCString(value_str, error);
405}
406
408 if (m_parent) {
409 m_parent->ClearUserVisibleData(eClearUserVisibleDataItemsAll);
410 m_parent->SetFormat(format);
411 }
412 this->ValueObject::SetFormat(format);
414}
415
417 lldb::LanguageType lang) {
419 if (m_parent)
420 m_parent->SetPreferredDisplayLanguage(lang);
421}
422
431
433 if (m_parent)
434 return m_parent->IsSyntheticChildrenGenerated();
435 return false;
436}
437
439 if (m_parent)
440 m_parent->SetSyntheticChildrenGenerated(b);
442}
443
445 if (m_parent)
446 return m_parent->GetDeclaration(decl);
447
448 return ValueObject::GetDeclaration(decl);
449}
450
452 if (m_parent)
453 return m_parent->GetLanguageFlags();
454 return this->ValueObject::GetLanguageFlags();
455}
456
458 if (m_parent)
459 m_parent->SetLanguageFlags(flags);
460 else
462}
463
465 GetExpressionPathFormat epformat) {
466 // A synthetic ValueObject may wrap an underlying Register or RegisterSet
467 // ValueObject, which requires a different approach to generating the
468 // expression path. In such cases, delegate to the non-synthetic value object.
469 if (const lldb::ValueType obj_value_type = GetValueType();
470 IsSynthetic() && (obj_value_type == lldb::eValueTypeRegister ||
471 obj_value_type == lldb::eValueTypeRegisterSet)) {
472
473 if (const lldb::ValueObjectSP raw_value = GetNonSyntheticValue())
474 return raw_value->GetExpressionPath(stream, epformat);
475 }
476 return ValueObject::GetExpressionPath(stream, epformat);
477}
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)
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)
bool UpdateValueIfNeeded(bool update_format=true)
void SetName(ConstString name)
Change the name of the current ValueObject.
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.
std::shared_ptr< lldb_private::SyntheticChildren > SyntheticChildrenSP
@ eValueTypeRegister
stack frame register value
@ eValueTypeRegisterSet
A collection of stack frame register values.