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
14#include "llvm/Support/ErrorExtras.h"
15
16using namespace lldb;
17
18namespace lldb_private {
19namespace formatters {
20
21SyntheticChildrenFrontEnd *
23 lldb::ValueObjectSP valobj_sp) {
24 return valobj_sp ? new VectorIteratorSyntheticFrontEnd(valobj_sp,
25 {ConstString("_Ptr")})
26 : nullptr;
27}
28
30public:
32
33 llvm::Expected<uint32_t> CalculateNumChildren() override;
34
35 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
36
38
39 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
40
41private:
42 ValueObject *m_start = nullptr;
45 uint32_t m_element_size = 0;
46};
47
49public:
51
52 llvm::Expected<uint32_t> CalculateNumChildren() override;
53
54 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
55
57
58 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
59
60private:
63 uint64_t m_count = 0;
64 uint64_t m_element_bit_size = 0;
66 std::map<size_t, lldb::ValueObjectSP> m_children;
67};
68
69} // namespace formatters
70} // namespace lldb_private
71
78
79llvm::Expected<uint32_t> lldb_private::formatters::
81 if (!m_start || !m_finish)
82 return llvm::createStringError(
83 "failed to determine start/end of vector data");
84
85 uint64_t start_val = m_start->GetValueAsUnsigned(0);
86 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
87
88 // A default-initialized empty vector.
89 if (start_val == 0 && finish_val == 0)
90 return 0;
91
92 if (start_val == 0)
93 return llvm::createStringError("invalid value for start of vector");
94
95 if (finish_val == 0)
96 return llvm::createStringError("invalid value for end of vector");
97
98 if (start_val > finish_val)
99 return llvm::createStringError(
100 "start of vector data begins after end pointer");
101
102 size_t num_children = (finish_val - start_val);
103 if (num_children % m_element_size)
104 return llvm::createStringError("size not multiple of element size");
105
106 return num_children / m_element_size;
107}
108
111 uint32_t idx) {
112 if (!m_start || !m_finish)
113 return lldb::ValueObjectSP();
114
115 uint64_t offset = idx * m_element_size;
116 offset = offset + m_start->GetValueAsUnsigned(0);
117 StreamString name;
118 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
119 return CreateChildValueObjectFromAddress(name.GetString(), offset,
120 m_backend.GetExecutionContextRef(),
122}
123
126 m_start = m_finish = nullptr;
127 ValueObjectSP data_sp(m_backend.GetChildAtNamePath({"_Mypair", "_Myval2"}));
128
129 if (!data_sp)
131
132 m_start = data_sp->GetChildMemberWithName("_Myfirst").get();
133 m_finish = data_sp->GetChildMemberWithName("_Mylast").get();
134 if (!m_start || !m_finish)
136
137 m_element_type = m_start->GetCompilerType().GetPointeeType();
138 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
139 if (size_or_err)
140 m_element_size = *size_or_err;
141 else
142 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
143 "{0}");
144
146}
147
148llvm::Expected<size_t> lldb_private::formatters::
150 if (!m_start || !m_finish)
151 return llvm::createStringErrorV("type has no child named '{0}'", name);
152 auto optional_idx = ExtractIndexFromString(name.GetCString());
153 if (!optional_idx) {
154 return llvm::createStringErrorV("type has no child named '{0}'", name);
155 }
156 return *optional_idx;
157}
158
162 m_children() {
163 if (valobj_sp) {
164 Update();
166 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
167 }
168}
169
174
177 uint32_t idx) {
178 auto iter = m_children.find(idx), end = m_children.end();
179 if (iter != end)
180 return iter->second;
181 if (idx >= m_count)
182 return {};
183 if (m_base_data_address == 0 || m_count == 0)
184 return {};
185 if (!m_bool_type)
186 return {};
187
188 // The vector<bool> is represented as a sequence of `int`s.
189 // The size of an `int` is in `m_element_bit_size` (most often 32b).
190 // To access the element at index `i`:
191 // (bool)((data_address[i / bit_size] >> (i % bit_size)) & 1)
192
193 // int *byte_location = &data_address[i / bit_size]
194 size_t byte_idx = (idx / m_element_bit_size) * (m_element_bit_size / 8);
195 lldb::addr_t byte_location = m_base_data_address + byte_idx;
196
197 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
198 if (!process_sp)
199 return {};
200 Status err;
201 Scalar scalar;
202 size_t bytes_read = process_sp->ReadScalarIntegerFromMemory(
203 byte_location, m_element_bit_size / 8, false, scalar, err);
204 if (err.Fail() || bytes_read == 0 || !scalar.IsValid())
205 return {};
206
207 size_t bit_index = idx % m_element_bit_size;
208 bool bit_set = scalar.GetAPSInt()[bit_index];
209 std::optional<uint64_t> size =
210 llvm::expectedToOptional(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);
221 name.GetString(),
222 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
223 process_sp->GetAddressByteSize()),
225 if (retval_sp)
226 m_children[idx] = retval_sp;
227 return retval_sp;
228}
229
232 m_exe_ctx_ref.Clear();
233 m_count = 0;
236 m_children.clear();
237
238 ValueObjectSP valobj_sp = m_backend.GetSP();
239 if (!valobj_sp)
241 auto exe_ctx_ref = valobj_sp->GetExecutionContextRef();
242
243 ValueObjectSP size_sp = valobj_sp->GetChildMemberWithName("_Mysize");
244 if (!size_sp)
246 uint64_t count = size_sp->GetValueAsUnsigned(0);
247 if (count == 0)
249
250 ValueObjectSP begin_sp(valobj_sp->GetChildAtNamePath(
251 {"_Myvec", "_Mypair", "_Myval2", "_Myfirst"}));
252 if (!begin_sp)
254
255 // FIXME: the STL exposes _EEN_VBITS as a constant - it should be used instead
256 CompilerType begin_ty = begin_sp->GetCompilerType().GetPointeeType();
257 if (!begin_ty.IsValid())
259 llvm::Expected<uint64_t> element_bit_size_or_err =
260 begin_ty.GetBitSize(nullptr);
261 if (!element_bit_size_or_err) {
263 element_bit_size_or_err.takeError(),
264 "failed to get vector<bool> element bit size: {0}");
266 }
267
268 uint64_t base_data_address = begin_sp->GetValueAsUnsigned(0);
269 if (!base_data_address)
271
272 m_exe_ctx_ref = exe_ctx_ref;
273 m_count = count;
274 m_element_bit_size = *element_bit_size_or_err;
275 m_base_data_address = base_data_address;
277}
278
279llvm::Expected<size_t>
283 return llvm::createStringErrorV("type has no child named '{0}'", name);
284 auto optional_idx = ExtractIndexFromString(name.AsCString(nullptr));
285 if (!optional_idx) {
286 return llvm::createStringErrorV("type has no child named '{0}'", name);
287 }
288 uint32_t idx = *optional_idx;
290 return llvm::createStringErrorV("type has no child named '{0}'", name);
291 return idx;
292}
293
296 lldb::ValueObjectSP valobj_sp) {
297 if (!valobj_sp)
298 return nullptr;
299
300 valobj_sp = valobj_sp->GetNonSyntheticValue();
301 if (!valobj_sp)
302 return nullptr;
303
304 // We can't check the template parameter here, because PDB doesn't include
305 // this information.
306
307 // vector<T>
308 if (valobj_sp->GetChildMemberWithName("_Mypair") != nullptr)
309 return new MsvcStlVectorSyntheticFrontEnd(valobj_sp);
310 // vector<bool>
311 if (valobj_sp->GetChildMemberWithName("_Myvec") != nullptr)
312 return new MsvcStlVectorBoolSyntheticFrontEnd(valobj_sp);
313
314 return nullptr;
315}
#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