1 //===-- LibCxxList.cpp -------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/DataFormatters/CXXFormatterFunctions.h"
14 #include "lldb/Core/DataBufferHeap.h"
15 #include "lldb/Core/Error.h"
16 #include "lldb/Core/Stream.h"
17 #include "lldb/Core/ValueObject.h"
18 #include "lldb/Core/ValueObjectConstResult.h"
19 #include "lldb/Host/Endian.h"
20 #include "lldb/Symbol/ClangASTContext.h"
21 #include "lldb/Target/ObjCLanguageRuntime.h"
22 #include "lldb/Target/Target.h"
25 using namespace lldb_private;
26 using namespace lldb_private::formatters;
28 namespace lldb_private {
29 namespace formatters {
30 class LibcxxStdListSyntheticFrontEnd : public SyntheticChildrenFrontEnd
33 LibcxxStdListSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp);
36 CalculateNumChildren ();
38 virtual lldb::ValueObjectSP
39 GetChildAtIndex (size_t idx);
48 GetIndexOfChildWithName (const ConstString &name);
51 ~LibcxxStdListSyntheticFrontEnd ();
56 size_t m_list_capping_size;
57 static const bool g_use_loop_detect = true;
58 size_t m_loop_detected;
59 lldb::addr_t m_node_address;
62 ClangASTType m_element_type;
64 std::map<size_t,lldb::ValueObjectSP> m_children;
73 ListEntry (ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
74 ListEntry (const ListEntry& rhs) : m_entry_sp(rhs.m_entry_sp) {}
75 ListEntry (ValueObject* entry) : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
82 return ListEntry(m_entry_sp->GetChildMemberWithName(ConstString("__next_"), true));
90 return ListEntry(m_entry_sp->GetChildMemberWithName(ConstString("__prev_"), true));
98 return m_entry_sp->GetValueAsUnsigned(0);
104 return (value() == 0);
107 explicit operator bool ()
109 return GetEntry().get() != nullptr && null() == false;
119 SetEntry (ValueObjectSP entry)
125 operator == (const ListEntry& rhs) const
127 return (rhs.m_entry_sp.get() == m_entry_sp.get());
131 ValueObjectSP m_entry_sp;
138 ListIterator (ListEntry entry) : m_entry(entry) {}
139 ListIterator (ValueObjectSP entry) : m_entry(entry) {}
140 ListIterator (const ListIterator& rhs) : m_entry(rhs.m_entry) {}
141 ListIterator (ValueObject* entry) : m_entry(entry) {}
146 return m_entry.GetEntry();
150 advance (size_t count)
153 return m_entry.GetEntry();
157 return m_entry.GetEntry();
164 return lldb::ValueObjectSP();
166 return m_entry.GetEntry();
170 operator == (const ListIterator& rhs) const
172 return (rhs.m_entry == m_entry);
179 m_entry = m_entry.next();
185 m_entry = m_entry.prev();
191 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::LibcxxStdListSyntheticFrontEnd (lldb::ValueObjectSP valobj_sp) :
192 SyntheticChildrenFrontEnd(*valobj_sp.get()),
193 m_list_capping_size(0),
207 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::HasLoop(size_t count)
209 if (g_use_loop_detect == false)
211 // don't bother checking for a loop if we won't actually need to jump nodes
214 auto steps_left = std::min(count,m_count);
215 auto steps_left_save = steps_left;
216 ListEntry slow(m_head);
217 ListEntry fast(m_head);
218 while (steps_left-- > 0)
223 fast = fast.next().next();
231 m_loop_detected = steps_left_save;
236 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::CalculateNumChildren ()
238 if (m_count != UINT32_MAX)
240 if (!m_head || !m_tail || m_node_address == 0)
242 ValueObjectSP size_alloc(m_backend.GetChildMemberWithName(ConstString("__size_alloc_"), true));
245 ValueObjectSP first(size_alloc->GetChildMemberWithName(ConstString("__first_"), true));
248 m_count = first->GetValueAsUnsigned(UINT32_MAX);
251 if (m_count != UINT32_MAX)
257 uint64_t next_val = m_head->GetValueAsUnsigned(0);
258 uint64_t prev_val = m_tail->GetValueAsUnsigned(0);
259 if (next_val == 0 || prev_val == 0)
261 if (next_val == m_node_address)
263 if (next_val == prev_val)
266 ListEntry current(m_head);
267 while (current.next() && current.next().value() != m_node_address)
270 current = current.next();
271 if (size > m_list_capping_size)
274 return m_count = (size-1);
279 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::GetChildAtIndex (size_t idx)
281 if (idx >= CalculateNumChildren())
282 return lldb::ValueObjectSP();
284 if (!m_head || !m_tail || m_node_address == 0)
285 return lldb::ValueObjectSP();
287 auto cached = m_children.find(idx);
288 if (cached != m_children.end())
289 return cached->second;
291 if (m_loop_detected <= idx)
293 return lldb::ValueObjectSP();
295 ListIterator current(m_head);
296 ValueObjectSP current_sp(current.advance(idx));
298 return lldb::ValueObjectSP();
299 current_sp = current_sp->GetChildMemberWithName(ConstString("__value_"), true);
301 return lldb::ValueObjectSP();
302 // we need to copy current_sp into a new object otherwise we will end up with all items named __value_
305 current_sp->GetData(data, error);
307 return lldb::ValueObjectSP();
310 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
311 return (m_children[idx] = CreateValueObjectFromData(name.GetData(), data, m_backend.GetExecutionContextRef(), m_element_type));
315 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::Update()
317 m_head = m_tail = NULL;
319 m_count = UINT32_MAX;
320 m_loop_detected = false;
322 ValueObjectSP backend_addr(m_backend.AddressOf(err));
323 m_list_capping_size = 0;
324 if (m_backend.GetTargetSP())
325 m_list_capping_size = m_backend.GetTargetSP()->GetMaximumNumberOfChildrenToDisplay();
326 if (m_list_capping_size == 0)
327 m_list_capping_size = 255;
328 if (err.Fail() || backend_addr.get() == NULL)
330 m_node_address = backend_addr->GetValueAsUnsigned(0);
331 if (!m_node_address || m_node_address == LLDB_INVALID_ADDRESS)
333 ValueObjectSP impl_sp(m_backend.GetChildMemberWithName(ConstString("__end_"),true));
336 ClangASTType list_type = m_backend.GetClangType();
337 if (list_type.IsReferenceType())
338 list_type = list_type.GetNonReferenceType();
340 if (list_type.GetNumTemplateArguments() == 0)
342 lldb::TemplateArgumentKind kind;
343 m_element_type = list_type.GetTemplateArgument(0, kind);
344 m_head = impl_sp->GetChildMemberWithName(ConstString("__next_"), true).get();
345 m_tail = impl_sp->GetChildMemberWithName(ConstString("__prev_"), true).get();
350 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::MightHaveChildren ()
356 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::GetIndexOfChildWithName (const ConstString &name)
358 return ExtractIndexFromString(name.GetCString());
361 lldb_private::formatters::LibcxxStdListSyntheticFrontEnd::~LibcxxStdListSyntheticFrontEnd ()
364 SyntheticChildrenFrontEnd*
365 lldb_private::formatters::LibcxxStdListSyntheticFrontEndCreator (CXXSyntheticChildren*, lldb::ValueObjectSP valobj_sp)
369 return (new LibcxxStdListSyntheticFrontEnd(valobj_sp));