LLDB mainline
GenericBitset.cpp
Go to the documentation of this file.
1//===-- GenericBitset.cpp //-----------------------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
9#include "LibCxx.h"
10#include "LibStdcpp.h"
13#include "lldb/Target/Target.h"
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18
19namespace {
20
21/// This class can be used for handling bitsets from both libcxx and libstdcpp.
22class GenericBitsetFrontEnd : public SyntheticChildrenFrontEnd {
23public:
24 enum class StdLib {
25 LibCxx,
26 LibStdcpp,
27 };
28
29 GenericBitsetFrontEnd(ValueObject &valobj, StdLib stdlib);
30
31 lldb::ChildCacheState Update() override;
32 llvm::Expected<uint32_t> CalculateNumChildren() override {
33 return m_elements.size();
34 }
35 ValueObjectSP GetChildAtIndex(uint32_t idx) override;
36
37private:
38 llvm::StringRef GetDataContainerMemberName();
39
40 // The lifetime of a ValueObject and all its derivative ValueObjects
41 // (children, clones, etc.) is managed by a ClusterManager. These
42 // objects are only destroyed when every shared pointer to any of them
43 // is destroyed, so we must not store a shared pointer to any ValueObject
44 // derived from our backend ValueObject (since we're in the same cluster).
45 // Value objects created from raw data (i.e. in a different cluster) must
46 // be referenced via shared pointer to keep them alive, however.
47 std::vector<ValueObjectSP> m_elements;
48 ValueObject *m_first = nullptr;
49 CompilerType m_bool_type;
50 ByteOrder m_byte_order = eByteOrderInvalid;
51 uint8_t m_byte_size = 0;
52 StdLib m_stdlib;
53};
54} // namespace
55
56GenericBitsetFrontEnd::GenericBitsetFrontEnd(ValueObject &valobj, StdLib stdlib)
57 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
58 m_bool_type = valobj.GetCompilerType().GetBasicTypeFromAST(eBasicTypeBool);
59 if (auto target_sp = m_backend.GetTargetSP()) {
60 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
61 m_byte_size = target_sp->GetArchitecture().GetAddressByteSize();
62 Update();
63 }
64}
65
66llvm::StringRef GenericBitsetFrontEnd::GetDataContainerMemberName() {
67 static constexpr llvm::StringLiteral s_libcxx_case("__first_");
68 static constexpr llvm::StringLiteral s_libstdcpp_case("_M_w");
69 switch (m_stdlib) {
70 case StdLib::LibCxx:
71 return s_libcxx_case;
72 case StdLib::LibStdcpp:
73 return s_libstdcpp_case;
74 }
75 llvm_unreachable("Unknown StdLib enum");
76}
77
78lldb::ChildCacheState GenericBitsetFrontEnd::Update() {
79 m_elements.clear();
80 m_first = nullptr;
81
82 TargetSP target_sp = m_backend.GetTargetSP();
83 if (!target_sp)
85
86 size_t size = 0;
87
88 if (auto arg = m_backend.GetCompilerType().GetIntegralTemplateArgument(0))
89 size = arg->value.GetAPSInt().getLimitedValue();
90
91 m_elements.assign(size, ValueObjectSP());
92 m_first =
93 m_backend.GetChildMemberWithName(GetDataContainerMemberName()).get();
95}
96
97ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(uint32_t idx) {
98 if (idx >= m_elements.size() || !m_first)
99 return ValueObjectSP();
100
101 if (m_elements[idx])
102 return m_elements[idx];
103
104 ExecutionContext ctx = m_backend.GetExecutionContextRef().Lock(false);
105 CompilerType type;
106 ValueObjectSP chunk;
107 // For small bitsets __first_ is not an array, but a plain size_t.
108 if (m_first->GetCompilerType().IsArrayType(&type)) {
109 std::optional<uint64_t> bit_size = llvm::expectedToOptional(
111 if (!bit_size || *bit_size == 0)
112 return {};
113 chunk = m_first->GetChildAtIndex(idx / *bit_size);
114 } else {
115 type = m_first->GetCompilerType();
116 chunk = m_first->GetSP();
117 }
118 if (!type || !chunk)
119 return {};
120
121 std::optional<uint64_t> bit_size = llvm::expectedToOptional(
123 if (!bit_size || *bit_size == 0)
124 return {};
125 size_t chunk_idx = idx % *bit_size;
126 uint8_t value = !!(chunk->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx));
127 DataExtractor data(&value, sizeof(value), m_byte_order, m_byte_size);
128
129 m_elements[idx] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(),
130 data, ctx, m_bool_type);
131
132 return m_elements[idx];
133}
134
136 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
137 if (valobj_sp)
138 return new GenericBitsetFrontEnd(*valobj_sp,
139 GenericBitsetFrontEnd::StdLib::LibStdcpp);
140 return nullptr;
141}
142
143SyntheticChildrenFrontEnd *formatters::LibcxxBitsetSyntheticFrontEndCreator(
144 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
145 if (valobj_sp)
146 return new GenericBitsetFrontEnd(*valobj_sp,
147 GenericBitsetFrontEnd::StdLib::LibCxx);
148 return nullptr;
149}
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 ...
bool IsArrayType(CompilerType *element_type=nullptr, uint64_t *size=nullptr, bool *is_incomplete=nullptr) const
llvm::Expected< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
ExecutionContextScope * GetBestExecutionContextScope() const
virtual lldb::ValueObjectSP GetChildAtIndex(uint32_t idx, bool can_create=true)
lldb::ValueObjectSP GetSP()
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
lldb::TargetSP GetTargetSP() const
CompilerType GetCompilerType()
SyntheticChildrenFrontEnd * LibcxxBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
SyntheticChildrenFrontEnd * LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
A class that represents a running process on the host machine.
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
ByteOrder
Byte ordering definitions.
std::shared_ptr< lldb_private::Target > TargetSP