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 MapIterator &operator=(const MapIterator &) = default;
99 ValueObjectSP value() { return m_entry.GetEntry(); }
101 ValueObjectSP advance(size_t count) {
109 if (m_error || m_entry.null() || (steps > m_max_depth))
112 return m_entry.GetEntry();
119 MapEntry right(m_entry.right());
121 m_entry = tree_min(std::move(right));
125 while (!is_left_child(m_entry)) {
126 if (m_entry.error()) {
130 m_entry.SetEntry(m_entry.parent());
132 if (steps > m_max_depth) {
133 m_entry = MapEntry();
137 m_entry = MapEntry(m_entry.parent());
141 MapEntry tree_min(MapEntry &&x) {
144 MapEntry left(x.left());
146 while (!left.null()) {
152 left.SetEntry(x.left());
154 if (steps > m_max_depth)
160 bool is_left_child(const MapEntry &x) {
163 MapEntry rhs(x.parent());
164 rhs.SetEntry(rhs.left());
165 return x.value() == rhs.value();
173 namespace lldb_private {
174 namespace formatters {
175 class LibcxxStdMapSyntheticFrontEnd : public SyntheticChildrenFrontEnd {
177 LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
179 ~LibcxxStdMapSyntheticFrontEnd() override = default;
181 size_t CalculateNumChildren() override;
183 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
185 bool Update() override;
187 bool MightHaveChildren() override;
189 size_t GetIndexOfChildWithName(ConstString name) override;
194 void GetValueOffset(const lldb::ValueObjectSP &node);
197 ValueObject *m_root_node;
198 CompilerType m_element_type;
199 uint32_t m_skip_size;
201 std::map<size_t, MapIterator> m_iterators;
203 } // namespace formatters
204 } // namespace lldb_private
206 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
207 LibcxxStdMapSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
208 : SyntheticChildrenFrontEnd(*valobj_sp), m_tree(nullptr),
209 m_root_node(nullptr), m_element_type(), m_skip_size(UINT32_MAX),
210 m_count(UINT32_MAX), m_iterators() {
215 size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
216 CalculateNumChildren() {
217 static ConstString g___pair3_("__pair3_");
218 static ConstString g___first_("__first_");
219 static ConstString g___value_("__value_");
221 if (m_count != UINT32_MAX)
223 if (m_tree == nullptr)
225 ValueObjectSP m_item(m_tree->GetChildMemberWithName(g___pair3_, true));
229 switch (m_item->GetCompilerType().GetNumDirectBaseClasses()) {
231 // Assume a pre llvm r300140 __compressed_pair implementation:
232 m_item = m_item->GetChildMemberWithName(g___first_, true);
235 // Assume a post llvm r300140 __compressed_pair implementation:
236 ValueObjectSP first_elem_parent = m_item->GetChildAtIndex(0, true);
237 m_item = first_elem_parent->GetChildMemberWithName(g___value_, true);
246 m_count = m_item->GetValueAsUnsigned(0);
250 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetDataType() {
251 static ConstString g___value_("__value_");
252 static ConstString g_tree_("__tree_");
253 static ConstString g_pair3("__pair3_");
255 if (m_element_type.GetOpaqueQualType() && m_element_type.GetTypeSystem())
257 m_element_type.Clear();
260 deref = m_root_node->Dereference(error);
261 if (!deref || error.Fail())
263 deref = deref->GetChildMemberWithName(g___value_, true);
265 m_element_type = deref->GetCompilerType();
268 deref = m_backend.GetChildAtNamePath({g_tree_, g_pair3});
271 m_element_type = deref->GetCompilerType()
272 .GetTypeTemplateArgument(1)
273 .GetTypeTemplateArgument(1);
274 if (m_element_type) {
276 uint64_t bit_offset_ptr;
277 uint32_t bitfield_bit_size_ptr;
278 bool is_bitfield_ptr;
279 m_element_type = m_element_type.GetFieldAtIndex(
280 0, name, &bit_offset_ptr, &bitfield_bit_size_ptr, &is_bitfield_ptr);
281 m_element_type = m_element_type.GetTypedefedType();
282 return m_element_type.IsValid();
284 m_element_type = m_backend.GetCompilerType().GetTypeTemplateArgument(0);
285 return m_element_type.IsValid();
289 void lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetValueOffset(
290 const lldb::ValueObjectSP &node) {
291 if (m_skip_size != UINT32_MAX)
295 CompilerType node_type(node->GetCompilerType());
297 if (node_type.GetIndexOfFieldWithName("__value_", nullptr, &bit_offset) !=
299 m_skip_size = bit_offset / 8u;
301 ClangASTContext *ast_ctx =
302 llvm::dyn_cast_or_null<ClangASTContext>(node_type.GetTypeSystem());
305 CompilerType tree_node_type = ast_ctx->CreateStructForIdentifier(
307 {{"ptr0", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
308 {"ptr1", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
309 {"ptr2", ast_ctx->GetBasicType(lldb::eBasicTypeVoid).GetPointerType()},
310 {"cw", ast_ctx->GetBasicType(lldb::eBasicTypeBool)},
311 {"payload", (m_element_type.GetCompleteType(), m_element_type)}});
312 std::string child_name;
313 uint32_t child_byte_size;
314 int32_t child_byte_offset = 0;
315 uint32_t child_bitfield_bit_size;
316 uint32_t child_bitfield_bit_offset;
317 bool child_is_base_class;
318 bool child_is_deref_of_parent;
319 uint64_t language_flags;
321 .GetChildCompilerTypeAtIndex(
322 nullptr, 4, true, true, true, child_name, child_byte_size,
323 child_byte_offset, child_bitfield_bit_size,
324 child_bitfield_bit_offset, child_is_base_class,
325 child_is_deref_of_parent, nullptr, language_flags)
327 m_skip_size = (uint32_t)child_byte_offset;
332 lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::GetChildAtIndex(
334 static ConstString g___cc("__cc");
335 static ConstString g___nc("__nc");
336 static ConstString g___value_("__value_");
338 if (idx >= CalculateNumChildren())
339 return lldb::ValueObjectSP();
340 if (m_tree == nullptr || m_root_node == nullptr)
341 return lldb::ValueObjectSP();
343 MapIterator iterator(m_root_node, CalculateNumChildren());
345 const bool need_to_skip = (idx > 0);
346 size_t actual_advancde = idx;
348 auto cached_iterator = m_iterators.find(idx - 1);
349 if (cached_iterator != m_iterators.end()) {
350 iterator = cached_iterator->second;
355 ValueObjectSP iterated_sp(iterator.advance(actual_advancde));
357 // this tree is garbage - stop
359 nullptr; // this will stop all future searches until an Update() happens
365 iterated_sp = iterated_sp->Dereference(error);
366 if (!iterated_sp || error.Fail()) {
368 return lldb::ValueObjectSP();
370 GetValueOffset(iterated_sp);
371 auto child_sp = iterated_sp->GetChildMemberWithName(g___value_, true);
373 iterated_sp = child_sp;
375 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
376 m_skip_size, m_element_type, true);
379 return lldb::ValueObjectSP();
382 // because of the way our debug info is made, we need to read item 0
383 // first so that we can cache information used to generate other elements
384 if (m_skip_size == UINT32_MAX)
386 if (m_skip_size == UINT32_MAX) {
388 return lldb::ValueObjectSP();
390 iterated_sp = iterated_sp->GetSyntheticChildAtOffset(
391 m_skip_size, m_element_type, true);
394 return lldb::ValueObjectSP();
399 return lldb::ValueObjectSP();
401 // at this point we have a valid
402 // we need to copy current_sp into a new object otherwise we will end up with
403 // all items named __value_
406 iterated_sp->GetData(data, error);
409 return lldb::ValueObjectSP();
412 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
413 auto potential_child_sp = CreateValueObjectFromData(
414 name.GetString(), data, m_backend.GetExecutionContextRef(),
416 if (potential_child_sp) {
417 switch (potential_child_sp->GetNumChildren()) {
419 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
420 if (child0_sp && child0_sp->GetName() == g___cc)
421 potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
425 auto child0_sp = potential_child_sp->GetChildAtIndex(0, true);
426 auto child1_sp = potential_child_sp->GetChildAtIndex(1, true);
427 if (child0_sp && child0_sp->GetName() == g___cc && child1_sp &&
428 child1_sp->GetName() == g___nc)
429 potential_child_sp = child0_sp->Clone(ConstString(name.GetString()));
434 m_iterators[idx] = iterator;
435 return potential_child_sp;
438 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::Update() {
439 static ConstString g___tree_("__tree_");
440 static ConstString g___begin_node_("__begin_node_");
441 m_count = UINT32_MAX;
442 m_tree = m_root_node = nullptr;
444 m_tree = m_backend.GetChildMemberWithName(g___tree_, true).get();
447 m_root_node = m_tree->GetChildMemberWithName(g___begin_node_, true).get();
451 bool lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
452 MightHaveChildren() {
456 size_t lldb_private::formatters::LibcxxStdMapSyntheticFrontEnd::
457 GetIndexOfChildWithName(ConstString name) {
458 return ExtractIndexFromString(name.GetCString());
461 SyntheticChildrenFrontEnd *
462 lldb_private::formatters::LibcxxStdMapSyntheticFrontEndCreator(
463 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
464 return (valobj_sp ? new LibcxxStdMapSyntheticFrontEnd(valobj_sp) : nullptr);