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
15#include "lldb/lldb-forward.h"
16#include <optional>
17
18using namespace lldb;
19using namespace lldb_private;
20using namespace lldb_private::formatters;
21
22namespace lldb_private {
23namespace formatters {
25public:
27
29
30 llvm::Expected<uint32_t> CalculateNumChildren() override;
31
32 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
33
35
36 bool MightHaveChildren() override;
37
38 size_t GetIndexOfChildWithName(ConstString name) override;
39
40private:
41 ValueObject *m_start = nullptr;
44 uint32_t m_element_size = 0;
45};
46
48public:
50
51 llvm::Expected<uint32_t> CalculateNumChildren() override;
52
53 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
54
56
57 bool MightHaveChildren() override { return true; }
58
59 size_t GetIndexOfChildWithName(ConstString name) override;
60
61private:
64 uint64_t m_count = 0;
66 std::map<size_t, lldb::ValueObjectSP> m_children;
67};
68
69} // namespace formatters
70} // namespace lldb_private
71
74 : SyntheticChildrenFrontEnd(*valobj_sp), m_element_type() {
75 if (valobj_sp)
76 Update();
77}
78
81 // these need to stay around because they are child objects who will follow
82 // their parent's life cycle
83 // delete m_start;
84 // delete m_finish;
85}
86
87llvm::Expected<uint32_t> lldb_private::formatters::
89 if (!m_start || !m_finish)
90 return 0;
91 uint64_t start_val = m_start->GetValueAsUnsigned(0);
92 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
93
94 if (start_val == 0 || finish_val == 0)
95 return 0;
96
97 if (start_val >= finish_val)
98 return 0;
99
100 size_t num_children = (finish_val - start_val);
101 if (num_children % m_element_size)
102 return 0;
103 return num_children / m_element_size;
104}
105
108 uint32_t idx) {
109 if (!m_start || !m_finish)
110 return lldb::ValueObjectSP();
111
112 uint64_t offset = idx * m_element_size;
113 offset = offset + m_start->GetValueAsUnsigned(0);
114 StreamString name;
115 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
116 return CreateValueObjectFromAddress(name.GetString(), offset,
117 m_backend.GetExecutionContextRef(),
118 m_element_type);
119}
120
122 if (auto cap_sp = root.GetChildMemberWithName("__cap_"))
123 return cap_sp;
124
125 ValueObjectSP cap_sp = root.GetChildMemberWithName("__end_cap_");
126 if (!cap_sp)
127 return nullptr;
128
129 if (!isOldCompressedPairLayout(*cap_sp))
130 return nullptr;
131
133}
134
137 m_start = m_finish = nullptr;
138 ValueObjectSP data_sp(GetDataPointer(m_backend));
139
140 if (!data_sp)
142
143 m_element_type = data_sp->GetCompilerType().GetPointeeType();
144 if (std::optional<uint64_t> size = m_element_type.GetByteSize(nullptr)) {
145 m_element_size = *size;
146
147 if (m_element_size > 0) {
148 // store raw pointers or end up with a circular dependency
149 m_start = m_backend.GetChildMemberWithName("__begin_").get();
150 m_finish = m_backend.GetChildMemberWithName("__end_").get();
151 }
152 }
154}
155
158 return true;
159}
160
163 if (!m_start || !m_finish)
164 return UINT32_MAX;
165 return ExtractIndexFromString(name.GetCString());
166}
167
170 : SyntheticChildrenFrontEnd(*valobj_sp), m_bool_type(), m_exe_ctx_ref(),
171 m_children() {
172 if (valobj_sp) {
173 Update();
175 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
176 }
177}
178
179llvm::Expected<uint32_t> lldb_private::formatters::
181 return m_count;
182}
183
186 uint32_t idx) {
187 auto iter = m_children.find(idx), end = m_children.end();
188 if (iter != end)
189 return iter->second;
190 if (idx >= m_count)
191 return {};
192 if (m_base_data_address == 0 || m_count == 0)
193 return {};
194 if (!m_bool_type)
195 return {};
196 size_t byte_idx = (idx >> 3); // divide by 8 to get byte index
197 size_t bit_index = (idx & 7); // efficient idx % 8 for bit index
198 lldb::addr_t byte_location = m_base_data_address + byte_idx;
199 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
200 if (!process_sp)
201 return {};
202 uint8_t byte = 0;
203 uint8_t mask = 0;
204 Status err;
205 size_t bytes_read = process_sp->ReadMemory(byte_location, &byte, 1, err);
206 if (err.Fail() || bytes_read == 0)
207 return {};
208 mask = 1 << bit_index;
209 bool bit_set = ((byte & mask) != 0);
210 std::optional<uint64_t> size = m_bool_type.GetByteSize(nullptr);
211 if (!size)
212 return {};
213 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
214 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
215 // regardless of endianness, anything non-zero is true
216 *(buffer_sp->GetBytes()) = 1;
217 }
218 StreamString name;
219 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
220 ValueObjectSP retval_sp(CreateValueObjectFromData(
221 name.GetString(),
222 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
223 process_sp->GetAddressByteSize()),
224 m_exe_ctx_ref, m_bool_type));
225 if (retval_sp)
226 m_children[idx] = retval_sp;
227 return retval_sp;
228}
229
232 m_children.clear();
233 ValueObjectSP valobj_sp = m_backend.GetSP();
234 if (!valobj_sp)
236 m_exe_ctx_ref = valobj_sp->GetExecutionContextRef();
237 ValueObjectSP size_sp(valobj_sp->GetChildMemberWithName("__size_"));
238 if (!size_sp)
240 m_count = size_sp->GetValueAsUnsigned(0);
241 if (!m_count)
243 ValueObjectSP begin_sp(valobj_sp->GetChildMemberWithName("__begin_"));
244 if (!begin_sp) {
245 m_count = 0;
247 }
248 m_base_data_address = begin_sp->GetValueAsUnsigned(0);
249 if (!m_base_data_address) {
250 m_count = 0;
252 }
254}
255
258 if (!m_count || !m_base_data_address)
259 return UINT32_MAX;
260 const char *item_name = name.GetCString();
261 uint32_t idx = ExtractIndexFromString(item_name);
262 if (idx < UINT32_MAX && idx >= CalculateNumChildrenIgnoringErrors())
263 return UINT32_MAX;
264 return idx;
265}
266
270 if (!valobj_sp)
271 return nullptr;
272 CompilerType type = valobj_sp->GetCompilerType();
273 if (!type.IsValid() || type.GetNumTemplateArguments() == 0)
274 return nullptr;
275 CompilerType arg_type = type.GetTypeTemplateArgument(0);
276 if (arg_type.GetTypeName() == "bool")
277 return new LibcxxVectorBoolSyntheticFrontEnd(valobj_sp);
278 return new LibcxxStdVectorSyntheticFrontEnd(valobj_sp);
279}
static ValueObjectSP GetDataPointer(ValueObject &root)
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:40
const char * GetCString() const
Get the string value as a C string.
Definition: ConstString.h:216
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:115
bool Fail() const
Test for error condition.
Definition: Status.cpp:270
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:134
virtual lldb::ValueObjectSP GetChildMemberWithName(llvm::StringRef name, bool can_create=true)
llvm::Expected< uint32_t > CalculateNumChildren() override
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
size_t GetIndexOfChildWithName(ConstString name) override
LibcxxStdVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
std::map< size_t, lldb::ValueObjectSP > m_children
llvm::Expected< uint32_t > CalculateNumChildren() override
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
LibcxxVectorBoolSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
#define UINT32_MAX
Definition: lldb-defines.h:19
size_t ExtractIndexFromString(const char *item_name)
lldb::ValueObjectSP GetFirstValueOfLibCXXCompressedPair(ValueObject &pair)
Definition: LibCxx.cpp:76
SyntheticChildrenFrontEnd * LibcxxStdVectorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP)
bool isOldCompressedPairLayout(ValueObject &pair_obj)
Definition: LibCxx.cpp:50
A class that represents a running process on the host machine.
Definition: SBAddress.h:15
ChildCacheState
Specifies if children need to be re-computed after a call to SyntheticChildrenFrontEnd::Update.
@ eRefetch
Children need to be recomputed dynamically.
@ eReuse
Children did not change and don't need to be recomputed; re-use what we computed the last time we cal...
std::shared_ptr< lldb_private::ValueObject > ValueObjectSP
Definition: lldb-forward.h:484
std::shared_ptr< lldb_private::Process > ProcessSP
Definition: lldb-forward.h:389
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
Definition: lldb-forward.h:337
uint64_t addr_t
Definition: lldb-types.h:80