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(llvm::StringRef 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(llvm::StringRef 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>
151 GetIndexOfChildWithName(llvm::StringRef name) {
152 if (!m_start || !m_finish)
153 return llvm::createStringErrorV("type has no child named '{0}'", name);
154 auto optional_idx = ExtractIndexFromString(name);
155 if (!optional_idx) {
156 return llvm::createStringErrorV("type has no child named '{0}'", name);
157 }
158 return *optional_idx;
159}
160
164 m_children() {
165 if (valobj_sp) {
166 Update();
168 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
169 }
170}
171
176
179 uint32_t idx) {
180 auto iter = m_children.find(idx), end = m_children.end();
181 if (iter != end)
182 return iter->second;
183 if (idx >= m_count)
184 return {};
185 if (m_base_data_address == 0 || m_count == 0)
186 return {};
187 if (!m_bool_type)
188 return {};
189
190 // The vector<bool> is represented as a sequence of `int`s.
191 // The size of an `int` is in `m_element_bit_size` (most often 32b).
192 // To access the element at index `i`:
193 // (bool)((data_address[i / bit_size] >> (i % bit_size)) & 1)
194
195 // int *byte_location = &data_address[i / bit_size]
196 size_t byte_idx = (idx / m_element_bit_size) * (m_element_bit_size / 8);
197 lldb::addr_t byte_location = m_base_data_address + byte_idx;
198
199 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
200 if (!process_sp)
201 return {};
202 Status err;
203 Scalar scalar;
204 size_t bytes_read = process_sp->ReadScalarIntegerFromMemory(
205 byte_location, m_element_bit_size / 8, false, scalar, err);
206 if (err.Fail() || bytes_read == 0 || !scalar.IsValid())
207 return {};
208
209 size_t bit_index = idx % m_element_bit_size;
210 bool bit_set = scalar.GetAPSInt()[bit_index];
211 std::optional<uint64_t> size =
212 llvm::expectedToOptional(m_bool_type.GetByteSize(nullptr));
213 if (!size)
214 return {};
215 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
216 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
217 // regardless of endianness, anything non-zero is true
218 *(buffer_sp->GetBytes()) = 1;
219 }
220 StreamString name;
221 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
223 name.GetString(),
224 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
225 process_sp->GetAddressByteSize()),
227 if (retval_sp)
228 m_children[idx] = retval_sp;
229 return retval_sp;
230}
231
234 m_exe_ctx_ref.Clear();
235 m_count = 0;
238 m_children.clear();
239
240 ValueObjectSP valobj_sp = m_backend.GetSP();
241 if (!valobj_sp)
243 auto exe_ctx_ref = valobj_sp->GetExecutionContextRef();
244
245 ValueObjectSP size_sp = valobj_sp->GetChildMemberWithName("_Mysize");
246 if (!size_sp)
248 uint64_t count = size_sp->GetValueAsUnsigned(0);
249 if (count == 0)
251
252 ValueObjectSP begin_sp(valobj_sp->GetChildAtNamePath(
253 {"_Myvec", "_Mypair", "_Myval2", "_Myfirst"}));
254 if (!begin_sp)
256
257 // FIXME: the STL exposes _EEN_VBITS as a constant - it should be used instead
258 CompilerType begin_ty = begin_sp->GetCompilerType().GetPointeeType();
259 if (!begin_ty.IsValid())
261 llvm::Expected<uint64_t> element_bit_size_or_err =
262 begin_ty.GetBitSize(nullptr);
263 if (!element_bit_size_or_err) {
265 element_bit_size_or_err.takeError(),
266 "failed to get vector<bool> element bit size: {0}");
268 }
269
270 uint64_t base_data_address = begin_sp->GetValueAsUnsigned(0);
271 if (!base_data_address)
273
274 m_exe_ctx_ref = exe_ctx_ref;
275 m_count = count;
276 m_element_bit_size = *element_bit_size_or_err;
277 m_base_data_address = base_data_address;
279}
280
281llvm::Expected<size_t>
283 GetIndexOfChildWithName(llvm::StringRef name) {
285 return llvm::createStringErrorV("type has no child named '{0}'", name);
286 auto optional_idx = ExtractIndexFromString(name);
287 if (!optional_idx) {
288 return llvm::createStringErrorV("type has no child named '{0}'", name);
289 }
290 uint32_t idx = *optional_idx;
292 return llvm::createStringErrorV("type has no child named '{0}'", name);
293 return idx;
294}
295
298 lldb::ValueObjectSP valobj_sp) {
299 if (!valobj_sp)
300 return nullptr;
301
302 valobj_sp = valobj_sp->GetNonSyntheticValue();
303 if (!valobj_sp)
304 return nullptr;
305
306 // We can't check the template parameter here, because PDB doesn't include
307 // this information.
308
309 // vector<T>
310 if (valobj_sp->GetChildMemberWithName("_Mypair") != nullptr)
311 return new MsvcStlVectorSyntheticFrontEnd(valobj_sp);
312 // vector<bool>
313 if (valobj_sp->GetChildMemberWithName("_Myvec") != nullptr)
314 return new MsvcStlVectorBoolSyntheticFrontEnd(valobj_sp);
315
316 return nullptr;
317}
#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
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)
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
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
Determine the index of a named child.
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
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
llvm::Expected< size_t > GetIndexOfChildWithName(llvm::StringRef name) override
Determine the index of a named child.
lldb_private::SyntheticChildrenFrontEnd * MsvcStlVectorSyntheticFrontEndCreator(lldb::ValueObjectSP valobj_sp)
std::optional< size_t > ExtractIndexFromString(llvm::StringRef item_name)
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