30template <StlType Stl>
class ListEntry {
32 ListEntry() =
default;
33 ListEntry(
ValueObjectSP entry_sp) : m_entry_sp(std::move(entry_sp)) {}
34 ListEntry(ValueObject *entry)
37 uint64_t value()
const {
40 return m_entry_sp->GetValueAsUnsigned(0);
46 bool null() {
return (value() == 0); }
48 explicit operator bool() {
return GetEntry() && !null(); }
54 bool operator==(
const ListEntry &rhs)
const {
return value() == rhs.value(); }
56 bool operator!=(
const ListEntry &rhs)
const {
return !(*
this == rhs); }
62template <> ListEntry<StlType::LibCxx> ListEntry<StlType::LibCxx>::next() {
65 return ListEntry(m_entry_sp->GetChildMemberWithName(
"__next_"));
68template <> ListEntry<StlType::LibCxx> ListEntry<StlType::LibCxx>::prev() {
71 return ListEntry(m_entry_sp->GetChildMemberWithName(
"__prev_"));
74template <> ListEntry<StlType::MsvcStl> ListEntry<StlType::MsvcStl>::next() {
77 return ListEntry(m_entry_sp->GetChildMemberWithName(
"_Next"));
80template <> ListEntry<StlType::MsvcStl> ListEntry<StlType::MsvcStl>::prev() {
83 return ListEntry(m_entry_sp->GetChildMemberWithName(
"_Prev"));
86template <StlType Stl>
class ListIterator {
88 ListIterator() =
default;
89 ListIterator(ListEntry<Stl> entry) : m_entry(std::move(entry)) {}
90 ListIterator(
ValueObjectSP entry) : m_entry(std::move(entry)) {}
91 ListIterator(ValueObject *entry) : m_entry(entry) {}
97 return m_entry.GetEntry();
100 return m_entry.GetEntry();
108 return m_entry.GetEntry();
111 bool operator==(
const ListIterator &rhs)
const {
112 return (rhs.m_entry == m_entry);
116 void next() { m_entry = m_entry.next(); }
118 void prev() { m_entry = m_entry.prev(); }
121 ListEntry<Stl> m_entry;
124template <StlType Stl>
127 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name)
override {
130 return llvm::createStringError(
"Type has no child named '%s'",
133 return *optional_idx;
138 AbstractListFrontEnd(ValueObject &valobj)
139 : SyntheticChildrenFrontEnd(valobj) {}
142 ValueObject *m_head =
nullptr;
144 static constexpr bool g_use_loop_detect =
true;
145 size_t m_loop_detected = 0;
147 ListEntry<Stl> m_slow_runner;
148 ListEntry<Stl> m_fast_runner;
150 size_t m_list_capping_size = 0;
151 CompilerType m_element_type;
152 std::map<size_t, ListIterator<Stl>> m_iterators;
154 bool HasLoop(
size_t count);
158class LibCxxForwardListFrontEnd :
public AbstractListFrontEnd<StlType::LibCxx> {
160 LibCxxForwardListFrontEnd(ValueObject &valobj);
167class LibCxxListFrontEnd :
public AbstractListFrontEnd<StlType::LibCxx> {
179 ValueObject *m_tail =
nullptr;
182class MsvcStlForwardListFrontEnd
183 :
public AbstractListFrontEnd<StlType::MsvcStl> {
185 MsvcStlForwardListFrontEnd(ValueObject &valobj);
192class MsvcStlListFrontEnd :
public AbstractListFrontEnd<StlType::MsvcStl> {
203 ValueObject *m_tail =
nullptr;
208template <StlType Stl>
213 m_list_capping_size = 0;
214 m_slow_runner.SetEntry(
nullptr);
215 m_fast_runner.SetEntry(
nullptr);
218 if (m_backend.GetTargetSP())
219 m_list_capping_size =
220 m_backend.GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
221 if (m_list_capping_size == 0)
222 m_list_capping_size = 255;
235template <StlType Stl>
bool AbstractListFrontEnd<Stl>::HasLoop(
size_t count) {
236 if (!g_use_loop_detect)
242 if (m_loop_detected == 0) {
245 m_slow_runner = ListEntry<Stl>(m_head).next();
246 m_fast_runner = m_slow_runner.next();
254 const size_t steps_to_run = std::min(count, m_count);
255 while (m_loop_detected < steps_to_run && m_slow_runner && m_fast_runner &&
256 m_slow_runner != m_fast_runner) {
258 m_slow_runner = m_slow_runner.next();
259 m_fast_runner = m_fast_runner.next().next();
262 if (count <= m_loop_detected)
264 if (!m_slow_runner || !m_fast_runner)
266 return m_slow_runner == m_fast_runner;
269template <StlType Stl>
270ValueObjectSP AbstractListFrontEnd<Stl>::GetItem(
size_t idx) {
271 size_t advance = idx;
272 ListIterator<Stl> current(m_head);
274 auto cached_iterator = m_iterators.find(idx - 1);
275 if (cached_iterator != m_iterators.end()) {
276 current = cached_iterator->second;
281 m_iterators[idx] = current;
285LibCxxForwardListFrontEnd::LibCxxForwardListFrontEnd(
ValueObject &valobj)
286 : AbstractListFrontEnd(valobj) {
290llvm::Expected<uint32_t> LibCxxForwardListFrontEnd::CalculateNumChildren() {
294 ListEntry<StlType::LibCxx> current(m_head);
296 while (current && m_count < m_list_capping_size) {
298 current = current.next();
303ValueObjectSP LibCxxForwardListFrontEnd::GetChildAtIndex(uint32_t idx) {
304 if (idx >= CalculateNumChildrenIgnoringErrors())
310 if (HasLoop(idx + 1))
317 current_sp = current_sp->GetChildAtIndex(1);
325 current_sp->GetData(data,
error);
329 return CreateValueObjectFromData(llvm::formatv(
"[{0}]", idx).str(), data,
330 m_backend.GetExecutionContextRef(),
335 AbstractListFrontEnd::Update();
339 if (err.
Fail() || !backend_addr)
342 auto list_base_sp = m_backend.GetChildAtIndex(0);
347 auto [impl_sp, is_compressed_pair] =
349 "__before_begin_",
"__before_begin_");
351 return ChildCacheState::eRefetch;
353 if (is_compressed_pair)
357 return ChildCacheState::eRefetch;
359 m_head = impl_sp->GetChildMemberWithName(
"__next_").get();
361 return ChildCacheState::eRefetch;
365 : AbstractListFrontEnd(*valobj_sp) {
370llvm::Expected<uint32_t> LibCxxListFrontEnd::CalculateNumChildren() {
373 if (!m_head || !m_tail || m_node_address == 0)
377 m_backend, 1,
"__size_",
"__size_alloc_");
378 if (is_compressed_pair)
382 m_count = size_node_sp->GetValueAsUnsigned(
UINT32_MAX);
387 uint64_t next_val = m_head->GetValueAsUnsigned(0);
389 if (next_val == 0 || prev_val == 0)
391 if (next_val == m_node_address)
393 if (next_val == prev_val)
396 ListEntry<StlType::LibCxx> current(m_head);
397 while (current.next() && current.next().value() != m_node_address) {
399 current = current.next();
400 if (size > m_list_capping_size)
403 return m_count = (size - 1);
407 static ConstString g_value(
"__value_");
408 static ConstString g_next(
"__next_");
410 if (idx >= CalculateNumChildrenIgnoringErrors())
413 if (!m_head || !m_tail || m_node_address == 0)
416 if (HasLoop(idx + 1))
423 current_sp = current_sp->GetChildAtIndex(1);
427 if (current_sp->GetName() == g_next) {
428 ProcessSP process_sp(current_sp->GetProcessSP());
433 lldb::addr_t addr = current_sp->GetParent()->GetPointerValue().address;
434 addr = addr + 2 * process_sp->GetAddressByteSize();
435 ExecutionContext exe_ctx(process_sp);
437 CreateValueObjectFromAddress(
"__value_", addr, exe_ctx, m_element_type);
446 current_sp->GetData(data,
error);
451 name.
Printf(
"[%" PRIu64
"]", (uint64_t)idx);
452 return CreateValueObjectFromData(name.
GetString(), data,
453 m_backend.GetExecutionContextRef(),
458 AbstractListFrontEnd::Update();
464 if (err.
Fail() || !backend_addr)
466 m_node_address = backend_addr->GetValueAsUnsigned(0);
469 ValueObjectSP impl_sp(m_backend.GetChildMemberWithName(
"__end_"));
472 m_head = impl_sp->GetChildMemberWithName(
"__next_").get();
477MsvcStlForwardListFrontEnd::MsvcStlForwardListFrontEnd(ValueObject &valobj)
478 : AbstractListFrontEnd(valobj) {
482llvm::Expected<uint32_t> MsvcStlForwardListFrontEnd::CalculateNumChildren() {
486 ListEntry<StlType::MsvcStl> current(m_head);
488 while (current && m_count < m_list_capping_size) {
490 current = current.next();
495ValueObjectSP MsvcStlForwardListFrontEnd::GetChildAtIndex(uint32_t idx) {
496 if (idx >= CalculateNumChildrenIgnoringErrors())
502 if (HasLoop(idx + 1))
509 current_sp = current_sp->GetChildAtIndex(1);
517 current_sp->GetData(data,
error);
521 return CreateValueObjectFromData(llvm::formatv(
"[{0}]", idx).str(), data,
522 m_backend.GetExecutionContextRef(),
527 AbstractListFrontEnd::Update();
530 m_backend.GetChildAtNamePath({
"_Mypair",
"_Myval2",
"_Myhead"}))
531 m_head = head_sp.get();
533 return ChildCacheState::eRefetch;
537 : AbstractListFrontEnd(*valobj_sp) {
542llvm::Expected<uint32_t> MsvcStlListFrontEnd::CalculateNumChildren() {
545 if (!m_head || !m_tail)
549 m_backend.GetChildAtNamePath({
"_Mypair",
"_Myval2",
"_Mysize"});
551 return llvm::createStringError(
"Failed to resolve size.");
553 m_count = size_sp->GetValueAsUnsigned(
UINT32_MAX);
555 return llvm::createStringError(
"Failed to read size value.");
561 if (idx >= CalculateNumChildrenIgnoringErrors())
564 if (!m_head || !m_tail)
567 if (HasLoop(idx + 1))
574 current_sp = current_sp->GetChildAtIndex(2);
582 current_sp->GetData(data,
error);
587 name.
Printf(
"[%" PRIu64
"]", (uint64_t)idx);
588 return CreateValueObjectFromData(name.
GetString(), data,
589 m_backend.GetExecutionContextRef(),
594 AbstractListFrontEnd::Update();
606 m_head = first.get();
614 return (valobj_sp ?
new LibCxxListFrontEnd(valobj_sp) :
nullptr);
620 return valobj_sp ?
new LibCxxForwardListFrontEnd(*valobj_sp) :
nullptr;
625 return valobj_sp->GetChildMemberWithName(
"_Mypair") !=
nullptr;
633 return (valobj_sp ?
new MsvcStlListFrontEnd(valobj_sp) :
nullptr);
639 return valobj_sp ?
new MsvcStlForwardListFrontEnd(*valobj_sp) :
nullptr;
static llvm::raw_ostream & error(Stream &strm)
static std::optional< size_t > CalculateNumChildren(CompilerType container_elem_type, uint64_t num_elements, CompilerType element_type)
Calculates the number of elements stored in a container (with element type 'container_elem_type') as ...
Generic representation of a type in a programming language.
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
CompilerType GetNonReferenceType() const
If this type is a reference to a type (L value or R value reference), return a new type with the refe...
bool IsReferenceType(CompilerType *pointee_type=nullptr, bool *is_rvalue=nullptr) const
const char * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
const char * GetCString() const
Get the string value as a C string.
bool Fail() const
Test for error condition.
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
virtual uint64_t GetValueAsUnsigned(uint64_t fail_value, bool *success=nullptr)
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
lldb::ValueObjectSP GetChildAtNamePath(llvm::ArrayRef< llvm::StringRef > names)
virtual lldb::ValueObjectSP GetNonSyntheticValue()
#define LLDB_INVALID_ADDRESS
A class that represents a running process on the host machine.
bool operator!=(const Address &lhs, const Address &rhs)
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
std::shared_ptr< lldb_private::Process > ProcessSP
bool LLDB_API operator==(const SBAddress &lhs, const SBAddress &rhs)