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 ConstString 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
69ConstString GenericBitsetFrontEnd::GetDataContainerMemberName() {
70 switch (m_stdlib) {
71 case StdLib::LibCxx:
72 return ConstString("__first_");
73 case StdLib::LibStdcpp:
74 return ConstString("_M_w");
75 }
76 llvm_unreachable("Unknown StdLib enum");
77}
78
79bool GenericBitsetFrontEnd::Update() {
80 m_elements.clear();
81 m_first = nullptr;
82
83 TargetSP target_sp = m_backend.GetTargetSP();
84 if (!target_sp)
85 return false;
86
87 size_t size = 0;
88
89 if (auto arg = m_backend.GetCompilerType().GetIntegralTemplateArgument(0))
90 size = arg->value.getLimitedValue();
91
92 m_elements.assign(size, ValueObjectSP());
93 m_first = m_backend.GetChildMemberWithName(GetDataContainerMemberName(), true)
94 .get();
95 return false;
96}
97
98ValueObjectSP GenericBitsetFrontEnd::GetChildAtIndex(size_t idx) {
99 if (idx >= m_elements.size() || !m_first)
100 return ValueObjectSP();
101
102 if (m_elements[idx])
103 return m_elements[idx];
104
105 ExecutionContext ctx = m_backend.GetExecutionContextRef().Lock(false);
106 CompilerType type;
107 ValueObjectSP chunk;
108 // For small bitsets __first_ is not an array, but a plain size_t.
109 if (m_first->GetCompilerType().IsArrayType(&type)) {
110 std::optional<uint64_t> bit_size =
112 if (!bit_size || *bit_size == 0)
113 return {};
114 chunk = m_first->GetChildAtIndex(idx / *bit_size, true);
115 } else {
116 type = m_first->GetCompilerType();
117 chunk = m_first->GetSP();
118 }
119 if (!type || !chunk)
120 return {};
121
122 std::optional<uint64_t> bit_size =
124 if (!bit_size || *bit_size == 0)
125 return {};
126 size_t chunk_idx = idx % *bit_size;
127 uint8_t value = !!(chunk->GetValueAsUnsigned(0) & (uint64_t(1) << chunk_idx));
128 DataExtractor data(&value, sizeof(value), m_byte_order, m_byte_size);
129
130 m_elements[idx] = CreateValueObjectFromData(llvm::formatv("[{0}]", idx).str(),
131 data, ctx, m_bool_type);
132
133 return m_elements[idx];
134}
135
137 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
138 if (valobj_sp)
139 return new GenericBitsetFrontEnd(*valobj_sp,
140 GenericBitsetFrontEnd::StdLib::LibStdcpp);
141 return nullptr;
142}
143
145 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
146 if (valobj_sp)
147 return new GenericBitsetFrontEnd(*valobj_sp,
148 GenericBitsetFrontEnd::StdLib::LibCxx);
149 return nullptr;
150}
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:39
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:215
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: SBAddress.h:15
ByteOrder
Byte ordering definitions.
@ eByteOrderInvalid