LLDB mainline
LibCxxVector.cpp
Go to the documentation of this file.
1//===-- LibCxxVector.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
14#include <optional>
15
16using namespace lldb;
17using namespace lldb_private;
18using namespace lldb_private::formatters;
19
20namespace lldb_private {
21namespace formatters {
23public:
24 LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
25
27
28 size_t CalculateNumChildren() override;
29
30 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
31
32 bool Update() override;
33
34 bool MightHaveChildren() override;
35
36 size_t GetIndexOfChildWithName(ConstString name) override;
37
38private:
39 ValueObject *m_start = nullptr;
43};
44
46public:
47 LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp);
48
49 size_t CalculateNumChildren() override;
50
51 lldb::ValueObjectSP GetChildAtIndex(size_t idx) override;
52
53 bool Update() override;
54
55 bool MightHaveChildren() override { return true; }
56
57 size_t GetIndexOfChildWithName(ConstString name) override;
58
59private:
62 uint64_t m_count = 0;
64 std::map<size_t, lldb::ValueObjectSP> m_children;
65};
66
67} // namespace formatters
68} // namespace lldb_private
69
71 LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
72 : SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() {
73 if (valobj_sp)
74 Update();
75}
76
79 // these need to stay around because they are child objects who will follow
80 // their parent's life cycle
81 // delete m_start;
82 // delete m_finish;
83}
84
87 if (!m_start || !m_finish)
88 return 0;
89 uint64_t start_val = m_start->GetValueAsUnsigned(0);
90 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
91
92 if (start_val == 0 || finish_val == 0)
93 return 0;
94
95 if (start_val >= finish_val)
96 return 0;
97
98 size_t num_children = (finish_val - start_val);
99 if (num_children % m_element_size)
100 return 0;
101 return num_children / m_element_size;
102}
103
104lldb::ValueObjectSP
106 size_t idx) {
107 if (!m_start || !m_finish)
108 return lldb::ValueObjectSP();
109
110 uint64_t offset = idx * m_element_size;
111 offset = offset + m_start->GetValueAsUnsigned(0);
112 StreamString name;
113 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
114 return CreateValueObjectFromAddress(name.GetString(), offset,
115 m_backend.GetExecutionContextRef(),
116 m_element_type);
117}
118
120 m_start = m_finish = nullptr;
121 ValueObjectSP data_type_finder_sp(
122 m_backend.GetChildMemberWithName(ConstString("__end_cap_"), true));
123 if (!data_type_finder_sp)
124 return false;
125
126 switch (data_type_finder_sp->GetCompilerType().GetNumDirectBaseClasses()) {
127 case 1:
128 // Assume a pre llvm r300140 __compressed_pair implementation:
129 data_type_finder_sp = data_type_finder_sp->GetChildMemberWithName(
130 ConstString("__first_"), true);
131 break;
132 case 2: {
133 // Assume a post llvm r300140 __compressed_pair implementation:
134 ValueObjectSP first_elem_parent_sp =
135 data_type_finder_sp->GetChildAtIndex(0, true);
136 data_type_finder_sp = first_elem_parent_sp->GetChildMemberWithName(
137 ConstString("__value_"), true);
138 break;
139 }
140 default:
141 return false;
142 }
143
144 if (!data_type_finder_sp)
145 return false;
146 m_element_type = data_type_finder_sp->GetCompilerType().GetPointeeType();
147 if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
148 m_element_size = *size;
149
150 if (m_element_size > 0) {
151 // store raw pointers or end up with a circular dependency
152 m_start =
153 m_backend.GetChildMemberWithName(ConstString("__begin_"), true).get();
154 m_finish =
155 m_backend.GetChildMemberWithName(ConstString("__end_"), true).get();
156 }
157 }
158 return false;
159}
160
163 return true;
164}
165
168 if (!m_start || !m_finish)
169 return UINT32_MAX;
170 return ExtractIndexFromString(name.GetCString());
171}
172
174 LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
175 : SyntheticChildrenFrontEnd(*valobj_sp), m_bool_type(), m_exe_ctx_ref(),
176 m_children() {
177 if (valobj_sp) {
178 Update();
180 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
181 }
182}
183
186 return m_count;
187}
188
189lldb::ValueObjectSP
191 size_t idx) {
192 auto iter = m_children.find(idx), end = m_children.end();
193 if (iter != end)
194 return iter->second;
195 if (idx >= m_count)
196 return {};
197 if (m_base_data_address == 0 || m_count == 0)
198 return {};
199 if (!m_bool_type)
200 return {};
201 size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
202 size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
203 lldb::addr_t byte_location = m_base_data_address + byte_idx;
204 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
205 if (!process_sp)
206 return {};
207 uint8_t byte = 0;
208 uint8_t mask = 0;
209 Status err;
210 size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
211 if (err.Fail() || bytes_read == 0)
212 return {};
213 mask = 1 << bit_index;
214 bool bit_set = ((byte & mask) != 0);
215 std::optional<uint64_t> size = m_bool_type.GetByteSize(nullptr);
216 if (!size)
217 return {};
218 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
219 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
220 // regardless of endianness, anything non-zero is true
221 *(buffer_sp->GetBytes()) = 1;
222 }
223 StreamString name;
224 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
225 ValueObjectSP retval_sp(CreateValueObjectFromData(
226 name.GetString(),
227 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
228 process_sp->GetAddressByteSize()),
229 m_exe_ctx_ref, m_bool_type));
230 if (retval_sp)
231 m_children[idx] = retval_sp;
232 return retval_sp;
233}
234
235/*(std::__1::vector<std::__1::allocator<bool> >) vBool = {
236 __begin_ = 0x00000001001000e0
237 __size_ = 56
238 __cap_alloc_ = {
239 std::__1::__libcpp_compressed_pair_imp<unsigned long,
240 std::__1::allocator<unsigned long> > = {
241 __first_ = 1
242 }
243 }
244 }*/
245
247 m_children.clear();
248 ValueObjectSP valobj_sp = m_backend.GetSP();
249 if (!valobj_sp)
250 return false;
251 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
252 ValueObjectSP size_sp(
253 valobj_sp->GetChildMemberWithName(ConstString("__size_"), true));
254 if (!size_sp)
255 return false;
256 m_count = size_sp->GetValueAsUnsigned(0);
257 if (!m_count)
258 return true;
259 ValueObjectSP begin_sp(
260 valobj_sp->GetChildMemberWithName(ConstString("__begin_"), true));
261 if (!begin_sp) {
262 m_count = 0;
263 return false;
264 }
265 m_base_data_address = begin_sp->GetValueAsUnsigned(0);
266 if (!m_base_data_address) {
267 m_count = 0;
268 return false;
269 }
270 return false;
271}
272
275 if (!m_count || !m_base_data_address)
276 return UINT32_MAX;
277 const char *item_name = name.GetCString();
278 uint32_t idx = ExtractIndexFromString(item_name);
279 if (idx < UINT32_MAX && idx >= CalculateNumChildren())
280 return UINT32_MAX;
281 return idx;
282}
283
286 CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
287 if (!valobj_sp)
288 return nullptr;
289 CompilerType type = valobj_sp->GetCompilerType();
290 if (!type.IsValid() || type.GetNumTemplateArguments() == 0)
291 return nullptr;
292 CompilerType arg_type = type.GetTypeTemplateArgument(0);
293 if (arg_type.GetTypeName() == "bool")
294 return new LibcxxVectorBoolSyntheticFrontEnd(valobj_sp);
295 return new LibcxxStdVectorSyntheticFrontEnd(valobj_sp);
296}
static size_t CalculateNumChildren(CompilerType container_type, CompilerType element_type, lldb_private::ExecutionContextScope *exe_scope=nullptr)
Definition: VectorType.cpp:172
Generic representation of a type in a programming language.
Definition: CompilerType.h:36
CompilerType GetTypeTemplateArgument(size_t idx, bool expand_pack=false) const
CompilerType GetBasicTypeFromAST(lldb::BasicType basic_type) const
Create related types using the current type's AST.
size_t GetNumTemplateArguments(bool expand_pack=false) const
Return the number of template arguments the type has.
ConstString GetTypeName(bool BaseOnly=false) const
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
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Definition: DataExtractor.h:48
Execution context objects refer to objects in the execution of the program that is being debugged.
An error handling class.
Definition: Status.h:44
bool Fail() const
Test for error condition.
Definition: Status.cpp:181
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:107
size_t GetIndexOfChildWithName(ConstString name) override
LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
std::map< size_t, lldb::ValueObjectSP > m_children
lldb::ValueObjectSP GetChildAtIndex(size_t idx) override
LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
#define UINT32_MAX
Definition: lldb-defines.h:19
size_t ExtractIndexFromString(const char *item_name)
SyntheticChildrenFrontEnd * LibcxxStdVectorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
A class that represents a running process on the host machine.
Definition: SBAttachInfo.h:14
Definition: SBAddress.h:15
uint64_t addr_t
Definition: lldb-types.h:83