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 size_t GetIndexOfChildWithName(ConstString name) override {
33 }
34
35 bool MightHaveChildren() override { return true; }
36 bool Update() override;
37 size_t CalculateNumChildren() override { return m_elements.size(); }
38 ValueObjectSP GetChildAtIndex(size_t idx) override;
39
40private:
41 llvm::StringRef GetDataContainerMemberName();
42
43 // The lifetime of a ValueObject and all its derivative ValueObjects
44 // (children, clones, etc.) is managed by a ClusterManager. These
45 // objects are only destroyed when every shared pointer to any of them
46 // is destroyed, so we must not store a shared pointer to any ValueObject
47 // derived from our backend ValueObject (since we're in the same cluster).
48 // Value objects created from raw data (i.e. in a different cluster) must
49 // be referenced via shared pointer to keep them alive, however.
50 std::vector<ValueObjectSP> m_elements;
51 ValueObject *m_first = nullptr;
52 CompilerType m_bool_type;
53 ByteOrder m_byte_order = eByteOrderInvalid;
54 uint8_t m_byte_size = 0;
55 StdLib m_stdlib;
56};
57} // namespace
58
59GenericBitsetFrontEnd::GenericBitsetFrontEnd(ValueObject &valobj, StdLib stdlib)
60 : SyntheticChildrenFrontEnd(valobj), m_stdlib(stdlib) {
62 if (auto target_sp = m_backend.GetTargetSP()) {
63 m_byte_order = target_sp->GetArchitecture().GetByteOrder();
64 m_byte_size = target_sp->GetArchitecture().GetAddressByteSize();
65 Update();
66 }
67}
68
69llvm::StringRef GenericBitsetFrontEnd::GetDataContainerMemberName() {
70 static constexpr llvm::StringLiteral s_libcxx_case("__first_");
71 static constexpr llvm::StringLiteral s_libstdcpp_case("_M_w");
72 switch (m_stdlib) {
73 case StdLib::LibCxx:
74 return s_libcxx_case;
75 case StdLib::LibStdcpp:
76 return s_libstdcpp_case;
77 }
78 llvm_unreachable("Unknown StdLib enum");
79}
80
81bool GenericBitsetFrontEnd::Update() {
82 m_elements.clear();
83 m_first = nullptr;
84
85 TargetSP target_sp = m_backend.GetTargetSP();
86 if (!target_sp)
87 return false;
88
89 size_t size = 0;
90
91 if (auto arg = m_backend.GetCompilerType().GetIntegralTemplateArgument(0))
92 size = arg->value.getLimitedValue();
93
94 m_elements.assign(size, ValueObjectSP());
95 m_first =
96 m_backend.GetChildMemberWithName(GetDataContainerMemberName()).get();
97 return false;
98}
99
100ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(size_t idx) {
101 if (idx >= m_elements.size() || !m_first)
102 return ValueObjectSP();
103
104 if (m_elements[idx])
105 return m_elements[idx];
106
107 ExecutionContext ctx = m_backend.GetExecutionContextRef().Lock(false);
108 CompilerType type;
109 ValueObjectSP chunk;
110 // For small bitsets __first_ is not an array, but a plain size_t.
111 if (m_first->GetCompilerType().IsArrayType(&type)) {
112 std::optional<uint64_t> bit_size =
114 if (!bit_size || *bit_size == 0)
115 return {};
116 chunk = m_first->GetChildAtIndex(idx / *bit_size);
117 } else {
118 type = m_first->GetCompilerType();
119 chunk = m_first->GetSP();
120 }
121 if (!type || !chunk)
122 return {};
123
124 std::optional<uint64_t> bit_size =
126 if (!bit_size || *bit_size == 0)
127 return {};
128 size_t chunk_idx = idx % *bit_size;
129 uint8_t value = !!(chunk->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx));
130 DataExtractor data(&value, sizeof(value), m_byte_order, m_byte_size);
131
132 m_elements[idx] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(),
133 data, ctx, m_bool_type);
134
135 return m_elements[idx];
136}
137
140 if (valobj_sp)
141 return new GenericBitsetFrontEnd(*valobj_sp,
142 GenericBitsetFrontEnd::StdLib::LibStdcpp);
143 return nullptr;
144}
145
148 if (valobj_sp)
149 return new GenericBitsetFrontEnd(*valobj_sp,
150 GenericBitsetFrontEnd::StdLib::LibCxx);
151 return nullptr;
152}
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
std::optional< uint64_t > GetBitSize(ExecutionContextScope *exe_scope) const
Return the size of the type in bits.
A uniqued constant string class.
Definition: ConstString.h:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:205
An data extractor class.
Definition: DataExtractor.h:48
"lldb/Target/ExecutionContext.h" A class that contains an execution context.
ExecutionContextScope * GetBestExecutionContextScope() const
virtual size_t GetIndexOfChildWithName(ConstString name)=0
virtual lldb::ValueObjectSP GetChildAtIndex(size_t idx)=0
CompilerType GetCompilerType()
Definition: ValueObject.h:352
SyntheticChildrenFrontEnd * LibcxxBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
size_t ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibStdcppBitsetSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:458
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid
std::shared_ptr< lldb_private::Target > TargetSP
Definition: lldb-forward.h:423