1 //===-- LibCxxMap.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;
28 explicit MapEntry(ValueObjectSP entry_sp) : m_entry_sp(entry_sp) {}
29 MapEntry(const MapEntry &rhs) = default;
30 explicit MapEntry(ValueObject *entry)
31 : m_entry_sp(entry ? entry->GetSP() : ValueObjectSP()) {}
33 ValueObjectSP left() const {
34 static ConstString g_left("__left_");
37 return m_entry_sp->GetSyntheticChildAtOffset(
38 0, m_entry_sp->GetCompilerType(), true);
41 ValueObjectSP right() const {
42 static ConstString g_right("__right_");
45 return m_entry_sp->GetSyntheticChildAtOffset(
46 m_entry_sp->GetProcessSP()->GetAddressByteSize(),
47 m_entry_sp->GetCompilerType(), true);
50 ValueObjectSP parent() const {
51 static ConstString g_parent("__parent_");
54 return m_entry_sp->GetSyntheticChildAtOffset(
55 2 * m_entry_sp->GetProcessSP()->GetAddressByteSize(),
56 m_entry_sp->GetCompilerType(), true);
59 uint64_t value() const {
62 return m_entry_sp->GetValueAsUnsigned(0);
68 return m_entry_sp->GetError().Fail();
71 bool null() const { return (value() == 0); }
73 ValueObjectSP GetEntry() const { return m_entry_sp; }
75 void SetEntry(ValueObjectSP entry) { m_entry_sp = entry; }
77 bool operator==(const MapEntry &rhs) const {
78 return (rhs.m_entry_sp.get() == m_entry_sp.get());
82 ValueObjectSP m_entry_sp;
87 MapIterator() = default;
88 MapIterator(MapEntry entry, size_t depth = 0)
89 : m_entry(entry), m_max_depth(depth), m_error(false) {}
90 MapIterator(ValueObjectSP entry, size_t depth = 0)
91 : m_entry(entry), m_max_depth(depth), m_error(false) {}
92 MapIterator(const MapIterator &rhs)
93 : m_entry(rhs.m_entry), m_max_depth(rhs.m_max_depth), m_error(false) {}
94 MapIterator(ValueObject *entry, size_t depth = 0)
95 : m_entry(entry), m_max_depth(depth), m_error(false) {}
97 ValueObjectSP value() { return m_entry.GetEntry(); }
99 ValueObjectSP advance(size_t count) {
107 if (m_error || m_entry.null() || (steps > m_max_depth))
110 return m_entry.GetEntry();
117 MapEntry right(m_entry.right());
119 m_entry = tree_min(std::move(right));
123 while (!is_left_child(m_entry)) {
124 if (m_entry.error()) {
128 m_entry.SetEntry(m_entry.parent());
130 if (steps > m_max_depth) {
131 m_entry = MapEntry();
135 m_entry = MapEntry(m_entry.parent());
139 MapEntry tree_min(MapEntry &&x) {
142 MapEntry left(x.left());
144 while (!left.null()) {
150 left.SetEntry(x.left());
152 if (steps > m_max_depth)
158 bool is_left_child(const MapEntry &x) {
161 MapEntry rhs(x.parent());
162 rhs.SetEntry(rhs.left());
163 return x.value() == rhs.value();
171 namespace lldb_private {
172 namespace formatters {
173 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
175 LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
177 ~LibcxxStdMapSyntheticFrontEnd() override = default;
179 size_t CalculateNumChildren() override;
181 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
183 bool Update() override;
185 bool MightHaveChildren() override;
187 size_t GetIndexOfChildWithName(ConstString name) override;
192 void GetValueOffset(const lldb::ValueObjectSP &node);
195 ValueObject *m_root_node;
196 CompilerType m_element_type;
197 uint32_t m_skip_size;
199 std::map<size_t, MapIterator> m_iterators;
201 } // namespace formatters
202 } // namespace lldb_private
204 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
205 LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
206 : SyntheticChildrenFrontEnd(*valobj_sp), m_tree(nullptr),
207 m_root_node(nullptr), m_element_type(), m_skip_size(UINT32_MAX),
208 m_count(UINT32_MAX), m_iterators() {
213 size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
214 CalculateNumChildren() {
215 static ConstString g___pair3_("__pair3_");
216 static ConstString g___first_("__first_");
217 static ConstString g___value_("__value_");
219 if (m_count != UINT32_MAX)
221 if (m_tree == nullptr)
223 ValueObjectSP m_item(m_tree->GetChildMemberWithName(g___pair3_, true));
227 switch (m_item->GetCompilerType().GetNumDirectBaseClasses()) {
229 // Assume a pre llvm r300140 __compressed_pair implementation:
230 m_item = m_item->GetChildMemberWithName(g___first_, true);
233 // Assume a post llvm r300140 __compressed_pair implementation:
234 ValueObjectSP first_elem_parent = m_item->GetChildAtIndex(0, true);
235 m_item = first_elem_parent->GetChildMemberWithName(g___value_, true);
244 m_count = m_item->GetValueAsUnsigned(0);
248 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType() {
249 static ConstString g___value_("__value_");
250 static ConstString g_tree_("__tree_");
251 static ConstString g_pair3("__pair3_");
253 if (m_element_type.GetOpaqueQualType() && m_element_type.GetTypeSystem())
255 m_element_type.Clear();
258 deref = m_root_node->Dereference(error);
259 if (!deref || error.Fail())
261 deref = deref->GetChildMemberWithName(g___value_, true);
263 m_element_type = deref->GetCompilerType();
266 deref = m_backend.GetChildAtNamePath({g_tree_, g_pair3});
269 m_element_type = deref->GetCompilerType()
270 .GetTypeTemplateArgument(1)
271 .GetTypeTemplateArgument(1);
272 if (m_element_type) {
274 uint64_t bit_offset_ptr;
275 uint32_t bitfield_bit_size_ptr;
276 bool is_bitfield_ptr;
277 m_element_type = m_element_type.GetFieldAtIndex(
278 0, name, &bit_offset_ptr, &bitfield_bit_size_ptr, &is_bitfield_ptr);
279 m_element_type = m_element_type.GetTypedefedType();
280 return m_element_type.IsValid();
282 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
283 return m_element_type.IsValid();
287 void lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset(
288 const lldb::ValueObjectSP &node) {
289 if (m_skip_size != UINT32_MAX)
293 CompilerType node_type(node->GetCompilerType());
295 if (node_type.GetIndexOfFieldWithName("__value_", nullptr, &bit_offset) !=
297 m_skip_size = bit_offset / 8u;
299 ClangASTContext *ast_ctx =
300 llvm::dyn_cast_or_null<ClangASTContext>(node_type.GetTypeSystem());
303 CompilerType tree_node_type = ast_ctx->CreateStructForIdentifier(
305 {{"ptr0", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
306 {"ptr1", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
307 {"ptr2", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
308 {"cw", ast_ctx->GetBasicType(lldb::eBasicTypeBool)},
309 {"payload", (m_element_type.GetCompleteType(), m_element_type)}});
310 std::string child_name;
311 uint32_t child_byte_size;
312 int32_t child_byte_offset = 0;
313 uint32_t child_bitfield_bit_size;
314 uint32_t child_bitfield_bit_offset;
315 bool child_is_base_class;
316 bool child_is_deref_of_parent;
317 uint64_t language_flags;
319 .GetChildCompilerTypeAtIndex(
320 nullptr, 4, true, true, true, child_name, child_byte_size,
321 child_byte_offset, child_bitfield_bit_size,
322 child_bitfield_bit_offset, child_is_base_class,
323 child_is_deref_of_parent, nullptr, language_flags)
325 m_skip_size = (uint32_t)child_byte_offset;
330 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex(
332 static ConstString g___cc("__cc");
333 static ConstString g___nc("__nc");
334 static ConstString g___value_("__value_");
336 if (idx >= CalculateNumChildren())
337 return lldb::ValueObjectSP();
338 if (m_tree == nullptr || m_root_node == nullptr)
339 return lldb::ValueObjectSP();
341 MapIterator iterator(m_root_node, CalculateNumChildren());
343 const bool need_to_skip = (idx > 0);
344 size_t actual_advancde = idx;
346 auto cached_iterator = m_iterators.find(idx - 1);
347 if (cached_iterator != m_iterators.end()) {
348 iterator = cached_iterator->second;
353 ValueObjectSP iterated_sp(iterator.advance(actual_advancde));
355 // this tree is garbage - stop
357 nullptr; // this will stop all future searches until an Update() happens
363 iterated_sp = iterated_sp->Dereference(error);
364 if (!iterated_sp || error.Fail()) {
366 return lldb::ValueObjectSP();
368 GetValueOffset(iterated_sp);
369 auto child_sp = iterated_sp->GetChildMemberWithName(g___value_, true);
371 iterated_sp = child_sp;
373 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
374 m_skip_size, m_element_type, true);
377 return lldb::ValueObjectSP();
380 // because of the way our debug info is made, we need to read item 0
381 // first so that we can cache information used to generate other elements
382 if (m_skip_size == UINT32_MAX)
384 if (m_skip_size == UINT32_MAX) {
386 return lldb::ValueObjectSP();
388 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
389 m_skip_size, m_element_type, true);
392 return lldb::ValueObjectSP();
397 return lldb::ValueObjectSP();
399 // at this point we have a valid
400 // we need to copy current_sp into a new object otherwise we will end up with
401 // all items named __value_
404 iterated_sp->GetData(data, error);
407 return lldb::ValueObjectSP();
410 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
411 auto potential_child_sp = CreateValueObjectFromData(
412 name.GetString(), data, m_backend.GetExecutionContextRef(),
414 if (potential_child_sp) {
415 switch (potential_child_sp->GetNumChildren()) {
417 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
418 if (child0_sp && child0_sp->GetName() == g___cc)
419 potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
423 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
424 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
425 if (child0_sp && child0_sp->GetName() == g___cc && child1_sp &&
426 child1_sp->GetName() == g___nc)
427 potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
432 m_iterators[idx] = iterator;
433 return potential_child_sp;
436 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update() {
437 static ConstString g___tree_("__tree_");
438 static ConstString g___begin_node_("__begin_node_");
439 m_count = UINT32_MAX;
440 m_tree = m_root_node = nullptr;
442 m_tree = m_backend.GetChildMemberWithName(g___tree_, true).get();
445 m_root_node = m_tree->GetChildMemberWithName(g___begin_node_, true).get();
449 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
450 MightHaveChildren() {
454 size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
455 GetIndexOfChildWithName(ConstString name) {
456 return ExtractIndexFromString(name.GetCString());
459 SyntheticChildrenFrontEnd *
460 lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator(
461 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
462 return (valobj_sp ? new LibcxxStdMapSyntheticFrontEnd(valobj_sp) : nullptr);