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