1 //===-- LibCxxList.cpp ------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
11 #include "lldb/Core/ValueObject.h"
12 #include "lldb/Core/ValueObjectConstResult.h"
13 #include "lldb/DataFormatters/FormattersHelpers.h"
14 #include "lldb/Symbol/ClangASTContext.h"
15 #include "lldb/Target/Target.h"
16 #include "lldb/Utility/DataBufferHeap.h"
17 #include "lldb/Utility/Endian.h"
18 #include "lldb/Utility/Status.h"
19 #include "lldb/Utility/Stream.h"
22 using namespace lldb_private;
23 using namespace lldb_private::formatters;
29 ListEntry() = default;
30 ListEntry(ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
31 ListEntry(const ListEntry &rhs) = default;
32 ListEntry(ValueObject *entry)
33 : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
36 static ConstString g_next("__next_");
40 return ListEntry(m_entry_sp->GetChildMemberWithName(g_next, true));
44 static ConstString g_prev("__prev_");
48 return ListEntry(m_entry_sp->GetChildMemberWithName(g_prev, true));
51 uint64_t value() const {
54 return m_entry_sp->GetValueAsUnsigned(0);
57 bool null() { return (value() == 0); }
59 explicit operator bool() { return GetEntry() && !null(); }
61 ValueObjectSP GetEntry() { return m_entry_sp; }
63 void SetEntry(ValueObjectSP entry) { m_entry_sp = entry; }
65 bool operator==(const ListEntry &rhs) const { return value() == rhs.value(); }
67 bool operator!=(const ListEntry &rhs) const { return !(*this == rhs); }
70 ValueObjectSP m_entry_sp;
75 ListIterator() = default;
76 ListIterator(ListEntry entry) : m_entry(entry) {}
77 ListIterator(ValueObjectSP entry) : m_entry(entry) {}
78 ListIterator(const ListIterator &rhs) = default;
79 ListIterator(ValueObject *entry) : m_entry(entry) {}
81 ValueObjectSP value() { return m_entry.GetEntry(); }
83 ValueObjectSP advance(size_t count) {
85 return m_entry.GetEntry();
88 return m_entry.GetEntry();
94 return lldb::ValueObjectSP();
96 return m_entry.GetEntry();
99 bool operator==(const ListIterator &rhs) const {
100 return (rhs.m_entry == m_entry);
104 void next() { m_entry = m_entry.next(); }
106 void prev() { m_entry = m_entry.prev(); }
112 class AbstractListFrontEnd : public SyntheticChildrenFrontEnd {
114 size_t GetIndexOfChildWithName(ConstString name) override {
115 return ExtractIndexFromString(name.GetCString());
117 bool MightHaveChildren() override { return true; }
118 bool Update() override;
121 AbstractListFrontEnd(ValueObject &valobj)
122 : SyntheticChildrenFrontEnd(valobj) {}
127 static constexpr bool g_use_loop_detect = true;
128 size_t m_loop_detected; // The number of elements that have had loop detection
130 ListEntry m_slow_runner; // Used for loop detection
131 ListEntry m_fast_runner; // Used for loop detection
133 size_t m_list_capping_size;
134 CompilerType m_element_type;
135 std::map<size_t, ListIterator> m_iterators;
137 bool HasLoop(size_t count);
138 ValueObjectSP GetItem(size_t idx);
141 class ForwardListFrontEnd : public AbstractListFrontEnd {
143 ForwardListFrontEnd(ValueObject &valobj);
145 size_t CalculateNumChildren() override;
146 ValueObjectSP GetChildAtIndex(size_t idx) override;
147 bool Update() override;
150 class ListFrontEnd : public AbstractListFrontEnd {
152 ListFrontEnd(lldb::ValueObjectSP valobj_sp);
154 ~ListFrontEnd() override = default;
156 size_t CalculateNumChildren() override;
158 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
160 bool Update() override;
163 lldb::addr_t m_node_address;
167 } // end anonymous namespace
169 bool AbstractListFrontEnd::Update() {
171 m_count = UINT32_MAX;
173 m_list_capping_size = 0;
174 m_slow_runner.SetEntry(nullptr);
175 m_fast_runner.SetEntry(nullptr);
178 if (m_backend.GetTargetSP())
179 m_list_capping_size =
180 m_backend.GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
181 if (m_list_capping_size == 0)
182 m_list_capping_size = 255;
184 CompilerType list_type = m_backend.GetCompilerType();
185 if (list_type.IsReferenceType())
186 list_type = list_type.GetNonReferenceType();
188 if (list_type.GetNumTemplateArguments() == 0)
190 m_element_type = list_type.GetTypeTemplateArgument(0);
195 bool AbstractListFrontEnd::HasLoop(size_t count) {
196 if (!g_use_loop_detect)
198 // don't bother checking for a loop if we won't actually need to jump nodes
202 if (m_loop_detected == 0) {
203 // This is the first time we are being run (after the last update). Set up
204 // the loop invariant for the first element.
205 m_slow_runner = ListEntry(m_head).next();
206 m_fast_runner = m_slow_runner.next();
211 // Loop detection has been run over the first m_loop_detected elements. If
212 // m_slow_runner == m_fast_runner then the loop has been detected after
213 // m_loop_detected elements.
214 const size_t steps_to_run = std::min(count, m_count);
215 while (m_loop_detected < steps_to_run && m_slow_runner && m_fast_runner &&
216 m_slow_runner != m_fast_runner) {
218 m_slow_runner = m_slow_runner.next();
219 m_fast_runner = m_fast_runner.next().next();
222 if (count <= m_loop_detected)
223 return false; // No loop in the first m_loop_detected elements.
224 if (!m_slow_runner || !m_fast_runner)
225 return false; // Reached the end of the list. Definitely no loops.
226 return m_slow_runner == m_fast_runner;
229 ValueObjectSP AbstractListFrontEnd::GetItem(size_t idx) {
230 size_t advance = idx;
231 ListIterator current(m_head);
233 auto cached_iterator = m_iterators.find(idx - 1);
234 if (cached_iterator != m_iterators.end()) {
235 current = cached_iterator->second;
239 ValueObjectSP value_sp = current.advance(advance);
240 m_iterators[idx] = current;
244 ForwardListFrontEnd::ForwardListFrontEnd(ValueObject &valobj)
245 : AbstractListFrontEnd(valobj) {
249 size_t ForwardListFrontEnd::CalculateNumChildren() {
250 if (m_count != UINT32_MAX)
253 ListEntry current(m_head);
255 while (current && m_count < m_list_capping_size) {
257 current = current.next();
262 ValueObjectSP ForwardListFrontEnd::GetChildAtIndex(size_t idx) {
263 if (idx >= CalculateNumChildren())
269 if (HasLoop(idx + 1))
272 ValueObjectSP current_sp = GetItem(idx);
276 current_sp = current_sp->GetChildAtIndex(1, true); // get the __value_ child
280 // we need to copy current_sp into a new object otherwise we will end up with
281 // all items named __value_
284 current_sp->GetData(data, error);
288 return CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(), data,
289 m_backend.GetExecutionContextRef(),
293 static ValueObjectSP GetValueOfCompressedPair(ValueObject &pair) {
294 ValueObjectSP value = pair.GetChildMemberWithName(ConstString("__value_"), true);
296 // pre-r300140 member name
297 value = pair.GetChildMemberWithName(ConstString("__first_"), true);
302 bool ForwardListFrontEnd::Update() {
303 AbstractListFrontEnd::Update();
306 ValueObjectSP backend_addr(m_backend.AddressOf(err));
307 if (err.Fail() || !backend_addr)
310 ValueObjectSP impl_sp(
311 m_backend.GetChildMemberWithName(ConstString("__before_begin_"), true));
314 impl_sp = GetValueOfCompressedPair(*impl_sp);
317 m_head = impl_sp->GetChildMemberWithName(ConstString("__next_"), true).get();
321 ListFrontEnd::ListFrontEnd(lldb::ValueObjectSP valobj_sp)
322 : AbstractListFrontEnd(*valobj_sp), m_node_address(), m_tail(nullptr) {
327 size_t ListFrontEnd::CalculateNumChildren() {
328 if (m_count != UINT32_MAX)
330 if (!m_head || !m_tail || m_node_address == 0)
332 ValueObjectSP size_alloc(
333 m_backend.GetChildMemberWithName(ConstString("__size_alloc_"), true));
335 ValueObjectSP value = GetValueOfCompressedPair(*size_alloc);
337 m_count = value->GetValueAsUnsigned(UINT32_MAX);
340 if (m_count != UINT32_MAX) {
343 uint64_t next_val = m_head->GetValueAsUnsigned(0);
344 uint64_t prev_val = m_tail->GetValueAsUnsigned(0);
345 if (next_val == 0 || prev_val == 0)
347 if (next_val == m_node_address)
349 if (next_val == prev_val)
352 ListEntry current(m_head);
353 while (current.next() && current.next().value() != m_node_address) {
355 current = current.next();
356 if (size > m_list_capping_size)
359 return m_count = (size - 1);
363 lldb::ValueObjectSP ListFrontEnd::GetChildAtIndex(size_t idx) {
364 static ConstString g_value("__value_");
365 static ConstString g_next("__next_");
367 if (idx >= CalculateNumChildren())
368 return lldb::ValueObjectSP();
370 if (!m_head || !m_tail || m_node_address == 0)
371 return lldb::ValueObjectSP();
373 if (HasLoop(idx + 1))
374 return lldb::ValueObjectSP();
376 ValueObjectSP current_sp = GetItem(idx);
378 return lldb::ValueObjectSP();
380 current_sp = current_sp->GetChildAtIndex(1, true); // get the __value_ child
382 return lldb::ValueObjectSP();
384 if (current_sp->GetName() == g_next) {
385 ProcessSP process_sp(current_sp->GetProcessSP());
387 return lldb::ValueObjectSP();
389 // if we grabbed the __next_ pointer, then the child is one pointer deep-er
390 lldb::addr_t addr = current_sp->GetParent()->GetPointerValue();
391 addr = addr + 2 * process_sp->GetAddressByteSize();
392 ExecutionContext exe_ctx(process_sp);
394 CreateValueObjectFromAddress("__value_", addr, exe_ctx, m_element_type);
396 return lldb::ValueObjectSP();
399 // we need to copy current_sp into a new object otherwise we will end up with
400 // all items named __value_
403 current_sp->GetData(data, error);
405 return lldb::ValueObjectSP();
408 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
409 return CreateValueObjectFromData(name.GetString(), data,
410 m_backend.GetExecutionContextRef(),
414 bool ListFrontEnd::Update() {
415 AbstractListFrontEnd::Update();
420 ValueObjectSP backend_addr(m_backend.AddressOf(err));
421 if (err.Fail() || !backend_addr)
423 m_node_address = backend_addr->GetValueAsUnsigned(0);
424 if (!m_node_address || m_node_address == LLDB_INVALID_ADDRESS)
426 ValueObjectSP impl_sp(
427 m_backend.GetChildMemberWithName(ConstString("__end_"), true));
430 m_head = impl_sp->GetChildMemberWithName(ConstString("__next_"), true).get();
431 m_tail = impl_sp->GetChildMemberWithName(ConstString("__prev_"), true).get();
435 SyntheticChildrenFrontEnd *formatters::LibcxxStdListSyntheticFrontEndCreator(
436 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
437 return (valobj_sp ? new ListFrontEnd(valobj_sp) : nullptr);
440 SyntheticChildrenFrontEnd *
441 formatters::LibcxxStdForwardListSyntheticFrontEndCreator(
442 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
443 return valobj_sp ? new ForwardListFrontEnd(*valobj_sp) : nullptr;