LLDB mainline
MsvcStlVector.cpp
Go to the documentation of this file.
1//===-- MsvcStlVector.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 "MsvcStl.h"
10
12
15#include "llvm/Support/ErrorExtras.h"
16
17using namespace lldb;
18
19namespace lldb_private {
20namespace formatters {
21
22SyntheticChildrenFrontEnd *
24 lldb::ValueObjectSP valobj_sp) {
25 return valobj_sp ? new VectorIteratorSyntheticFrontEnd(valobj_sp,
26 {ConstString("_Ptr")})
27 : nullptr;
28}
29
31public:
33
34 llvm::Expected<uint32_t> CalculateNumChildren() override;
35
36 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
37
39
40 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
41
42private:
43 ValueObject *m_start = nullptr;
46 uint32_t m_element_size = 0;
47};
48
50public:
52
53 llvm::Expected<uint32_t> CalculateNumChildren() override;
54
55 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
56
58
59 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
60
61private:
64 uint64_t m_count = 0;
65 uint64_t m_element_bit_size = 0;
67 std::map<size_t, lldb::ValueObjectSP> m_children;
68};
69
70} // namespace formatters
71} // namespace lldb_private
72
79
80llvm::Expected<uint32_t> lldb_private::formatters::
82 if (!m_start || !m_finish)
83 return llvm::createStringError(
84 "failed to determine start/end of vector data");
85
86 uint64_t start_val = m_start->GetValueAsUnsigned(0);
87 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
88
89 // A default-initialized empty vector.
90 if (start_val == 0 && finish_val == 0)
91 return 0;
92
93 if (start_val == 0)
94 return llvm::createStringError("invalid value for start of vector");
95
96 if (finish_val == 0)
97 return llvm::createStringError("invalid value for end of vector");
98
99 if (start_val > finish_val)
100 return llvm::createStringError(
101 "start of vector data begins after end pointer");
102
103 size_t num_children = (finish_val - start_val);
104 if (num_children % m_element_size)
105 return llvm::createStringError("size not multiple of element size");
106
107 return num_children / m_element_size;
108}
109
112 uint32_t idx) {
113 if (!m_start || !m_finish)
114 return lldb::ValueObjectSP();
115
116 uint64_t offset = idx * m_element_size;
117 offset = offset + m_start->GetValueAsUnsigned(0);
118 StreamString name;
119 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
120 return CreateChildValueObjectFromAddress(name.GetString(), offset,
121 m_backend.GetExecutionContextRef(),
123}
124
127 m_start = m_finish = nullptr;
128 ValueObjectSP data_sp(m_backend.GetChildAtNamePath({"_Mypair", "_Myval2"}));
129
130 if (!data_sp)
132
133 m_start = data_sp->GetChildMemberWithName("_Myfirst").get();
134 m_finish = data_sp->GetChildMemberWithName("_Mylast").get();
135 if (!m_start || !m_finish)
137
138 m_element_type = m_start->GetCompilerType().GetPointeeType();
139 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
140 if (size_or_err)
141 m_element_size = *size_or_err;
142 else
143 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
144 "{0}");
145
147}
148
149llvm::Expected<size_t> lldb_private::formatters::
151 if (!m_start || !m_finish)
152 return llvm::createStringErrorV("type has no child named '{0}'", name);
153 auto optional_idx = ExtractIndexFromString(name.GetCString());
154 if (!optional_idx) {
155 return llvm::createStringErrorV("type has no child named '{0}'", name);
156 }
157 return *optional_idx;
158}
159
163 m_children() {
164 if (valobj_sp) {
165 Update();
167 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
168 }
169}
170
175
178 uint32_t idx) {
179 auto iter = m_children.find(idx), end = m_children.end();
180 if (iter != end)
181 return iter->second;
182 if (idx >= m_count)
183 return {};
184 if (m_base_data_address == 0 || m_count == 0)
185 return {};
186 if (!m_bool_type)
187 return {};
188
189 // The vector<bool> is represented as a sequence of `int`s.
190 // The size of an `int` is in `m_element_bit_size` (most often 32b).
191 // To access the element at index `i`:
192 // (bool)((data_address[i / bit_size] >> (i % bit_size)) & 1)
193
194 // int *byte_location = &data_address[i / bit_size]
195 size_t byte_idx = (idx / m_element_bit_size) * (m_element_bit_size / 8);
196 lldb::addr_t byte_location = m_base_data_address + byte_idx;
197
198 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
199 if (!process_sp)
200 return {};
201 Status err;
202 Scalar scalar;
203 size_t bytes_read = process_sp->ReadScalarIntegerFromMemory(
204 byte_location, m_element_bit_size / 8, false, scalar, err);
205 if (err.Fail() || bytes_read == 0 || !scalar.IsValid())
206 return {};
207
208 size_t bit_index = idx % m_element_bit_size;
209 bool bit_set = scalar.GetAPSInt()[bit_index];
210 std::optional<uint64_t> size =
211 llvm::expectedToOptional(m_bool_type.GetByteSize(nullptr));
212 if (!size)
213 return {};
214 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
215 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
216 // regardless of endianness, anything non-zero is true
217 *(buffer_sp->GetBytes()) = 1;
218 }
219 StreamString name;
220 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
222 name.GetString(),
223 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
224 process_sp->GetAddressByteSize()),
226 if (retval_sp)
227 m_children[idx] = retval_sp;
228 return retval_sp;
229}
230
233 m_exe_ctx_ref.Clear();
234 m_count = 0;
237 m_children.clear();
238
239 ValueObjectSP valobj_sp = m_backend.GetSP();
240 if (!valobj_sp)
242 auto exe_ctx_ref = valobj_sp->GetExecutionContextRef();
243
244 ValueObjectSP size_sp = valobj_sp->GetChildMemberWithName("_Mysize");
245 if (!size_sp)
247 uint64_t count = size_sp->GetValueAsUnsigned(0);
248 if (count == 0)
250
251 ValueObjectSP begin_sp(valobj_sp->GetChildAtNamePath(
252 {"_Myvec", "_Mypair", "_Myval2", "_Myfirst"}));
253 if (!begin_sp)
255
256 // FIXME: the STL exposes _EEN_VBITS as a constant - it should be used instead
257 CompilerType begin_ty = begin_sp->GetCompilerType().GetPointeeType();
258 if (!begin_ty.IsValid())
260 llvm::Expected<uint64_t> element_bit_size_or_err =
261 begin_ty.GetBitSize(nullptr);
262 if (!element_bit_size_or_err) {
264 element_bit_size_or_err.takeError(),
265 "failed to get vector<bool> element bit size: {0}");
267 }
268
269 uint64_t base_data_address = begin_sp->GetValueAsUnsigned(0);
270 if (!base_data_address)
272
273 m_exe_ctx_ref = exe_ctx_ref;
274 m_count = count;
275 m_element_bit_size = *element_bit_size_or_err;
276 m_base_data_address = base_data_address;
278}
279
280llvm::Expected<size_t>
284 return llvm::createStringErrorV("type has no child named '{0}'", name);
285 auto optional_idx = ExtractIndexFromString(name.AsCString(nullptr));
286 if (!optional_idx) {
287 return llvm::createStringErrorV("type has no child named '{0}'", name);
288 }
289 uint32_t idx = *optional_idx;
291 return llvm::createStringErrorV("type has no child named '{0}'", name);
292 return idx;
293}
294
297 lldb::ValueObjectSP valobj_sp) {
298 if (!valobj_sp)
299 return nullptr;
300
301 valobj_sp = valobj_sp->GetNonSyntheticValue();
302 if (!valobj_sp)
303 return nullptr;
304
305 // We can't check the template parameter here, because PDB doesn't include
306 // this information.
307
308 // vector<T>
309 if (valobj_sp->GetChildMemberWithName("_Mypair") != nullptr)
310 return new MsvcStlVectorSyntheticFrontEnd(valobj_sp);
311 // vector<bool>
312 if (valobj_sp->GetChildMemberWithName("_Myvec") != nullptr)
313 return new MsvcStlVectorBoolSyntheticFrontEnd(valobj_sp);
314
315 return nullptr;
316}
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:421
#define LLDB_LOG_ERROR(log, error,...)
Definition Log.h:405
Generic representation of a type in a programming language.
llvm::Expected< 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.
const char * AsCString(const char *value_if_empty) const
Get the string value as a C string.
A subclass of DataBuffer that stores a data buffer on the heap.
An data extractor class.
Execution context objects refer to objects in the execution of the program that is being debugged.
bool IsValid() const
Definition Scalar.h:111
llvm::APSInt GetAPSInt() const
Definition Scalar.h:188
An error handling class.
Definition Status.h:118
bool Fail() const
Test for error condition.
Definition Status.cpp:293
llvm::StringRef GetString() const
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition Stream.cpp:134
uint32_t CalculateNumChildrenIgnoringErrors(uint32_t max=UINT32_MAX)
lldb::ValueObjectSP CreateChildValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ValueObjectSP CreateChildValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< uint32_t > CalculateNumChildren() override
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
MsvcStlVectorSyntheticFrontEnd(lldb::ValueObjectSP valobj_sp)
lldb::ChildCacheState Update() override
This function is assumed to always succeed and if it fails, the front-end should know to deal with it...
llvm::Expected< uint32_t > CalculateNumChildren() override
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) override
Determine the index of a named child.
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
std::optional< size_t > ExtractIndexFromString(const char *item_name)
lldb_private::SyntheticChildrenFrontEnd * MsvcStlVectorSyntheticFrontEndCreator(lldb::ValueObjectSP valobj_sp)
SyntheticChildrenFrontEnd * MsvcStlVectorIteratorSyntheticFrontEndCreator(CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
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
std::shared_ptr< lldb_private::Process > ProcessSP
std::shared_ptr< lldb_private::WritableDataBuffer > WritableDataBufferSP
uint64_t addr_t
Definition lldb-types.h:80