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
13
14using namespace lldb;
15
16namespace lldb_private {
17namespace formatters {
18
20public:
22
23 llvm::Expected<uint32_t> CalculateNumChildren() override;
24
25 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
26
28
29 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
30
31private:
32 ValueObject *m_start = nullptr;
35 uint32_t m_element_size = 0;
36};
37
39public:
41
42 llvm::Expected<uint32_t> CalculateNumChildren() override;
43
44 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
45
47
48 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
49
50private:
53 uint64_t m_count = 0;
54 uint64_t m_element_bit_size = 0;
56 std::map<size_t, lldb::ValueObjectSP> m_children;
57};
58
59} // namespace formatters
60} // namespace lldb_private
61
68
69llvm::Expected<uint32_t> lldb_private::formatters::
71 if (!m_start || !m_finish)
72 return llvm::createStringError(
73 "failed to determine start/end of vector data");
74
75 uint64_t start_val = m_start->GetValueAsUnsigned(0);
76 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
77
78 // A default-initialized empty vector.
79 if (start_val == 0 && finish_val == 0)
80 return 0;
81
82 if (start_val == 0)
83 return llvm::createStringError("invalid value for start of vector");
84
85 if (finish_val == 0)
86 return llvm::createStringError("invalid value for end of vector");
87
88 if (start_val > finish_val)
89 return llvm::createStringError(
90 "start of vector data begins after end pointer");
91
92 size_t num_children = (finish_val - start_val);
93 if (num_children % m_element_size)
94 return llvm::createStringError("size not multiple of element size");
95
96 return num_children / m_element_size;
97}
98
101 uint32_t idx) {
102 if (!m_start || !m_finish)
103 return lldb::ValueObjectSP();
104
105 uint64_t offset = idx * m_element_size;
106 offset = offset + m_start->GetValueAsUnsigned(0);
107 StreamString name;
108 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
109 return CreateValueObjectFromAddress(name.GetString(), offset,
110 m_backend.GetExecutionContextRef(),
112}
113
116 m_start = m_finish = nullptr;
117 ValueObjectSP data_sp(m_backend.GetChildAtNamePath({"_Mypair", "_Myval2"}));
118
119 if (!data_sp)
121
122 m_start = data_sp->GetChildMemberWithName("_Myfirst").get();
123 m_finish = data_sp->GetChildMemberWithName("_Mylast").get();
124 if (!m_start || !m_finish)
126
127 m_element_type = m_start->GetCompilerType().GetPointeeType();
128 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
129 if (size_or_err)
130 m_element_size = *size_or_err;
131 else
132 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
133 "{0}");
134
136}
137
138llvm::Expected<size_t> lldb_private::formatters::
140 if (!m_start || !m_finish)
141 return llvm::createStringError("Type has no child named '%s'",
142 name.AsCString());
143 auto optional_idx = ExtractIndexFromString(name.GetCString());
144 if (!optional_idx) {
145 return llvm::createStringError("Type has no child named '%s'",
146 name.AsCString());
147 }
148 return *optional_idx;
149}
150
154 m_children() {
155 if (valobj_sp) {
156 Update();
158 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
159 }
160}
161
166
169 uint32_t idx) {
170 auto iter = m_children.find(idx), end = m_children.end();
171 if (iter != end)
172 return iter->second;
173 if (idx >= m_count)
174 return {};
175 if (m_base_data_address == 0 || m_count == 0)
176 return {};
177 if (!m_bool_type)
178 return {};
179
180 // The vector<bool> is represented as a sequence of `int`s.
181 // The size of an `int` is in `m_element_bit_size` (most often 32b).
182 // To access the element at index `i`:
183 // (bool)((data_address[i / bit_size] >> (i % bit_size)) & 1)
184
185 // int *byte_location = &data_address[i / bit_size]
186 size_t byte_idx = (idx / m_element_bit_size) * (m_element_bit_size / 8);
187 lldb::addr_t byte_location = m_base_data_address + byte_idx;
188
189 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
190 if (!process_sp)
191 return {};
192 Status err;
193 Scalar scalar;
194 size_t bytes_read = process_sp->ReadScalarIntegerFromMemory(
195 byte_location, m_element_bit_size / 8, false, scalar, err);
196 if (err.Fail() || bytes_read == 0 || !scalar.IsValid())
197 return {};
198
199 size_t bit_index = idx % m_element_bit_size;
200 bool bit_set = scalar.GetAPSInt()[bit_index];
201 std::optional<uint64_t> size =
202 llvm::expectedToOptional(m_bool_type.GetByteSize(nullptr));
203 if (!size)
204 return {};
205 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
206 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
207 // regardless of endianness, anything non-zero is true
208 *(buffer_sp->GetBytes()) = 1;
209 }
210 StreamString name;
211 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
213 name.GetString(),
214 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
215 process_sp->GetAddressByteSize()),
217 if (retval_sp)
218 m_children[idx] = retval_sp;
219 return retval_sp;
220}
221
224 m_exe_ctx_ref.Clear();
225 m_count = 0;
228 m_children.clear();
229
230 ValueObjectSP valobj_sp = m_backend.GetSP();
231 if (!valobj_sp)
233 auto exe_ctx_ref = valobj_sp->GetExecutionContextRef();
234
235 ValueObjectSP size_sp = valobj_sp->GetChildMemberWithName("_Mysize");
236 if (!size_sp)
238 uint64_t count = size_sp->GetValueAsUnsigned(0);
239 if (count == 0)
241
242 ValueObjectSP begin_sp(valobj_sp->GetChildAtNamePath(
243 {"_Myvec", "_Mypair", "_Myval2", "_Myfirst"}));
244 if (!begin_sp)
246
247 // FIXME: the STL exposes _EEN_VBITS as a constant - it should be used instead
248 CompilerType begin_ty = begin_sp->GetCompilerType().GetPointeeType();
249 if (!begin_ty.IsValid())
251 llvm::Expected<uint64_t> element_bit_size = begin_ty.GetBitSize(nullptr);
252 if (!element_bit_size)
254
255 uint64_t base_data_address = begin_sp->GetValueAsUnsigned(0);
256 if (!base_data_address)
258
259 m_exe_ctx_ref = exe_ctx_ref;
260 m_count = count;
261 m_element_bit_size = *element_bit_size;
262 m_base_data_address = base_data_address;
264}
265
266llvm::Expected<size_t>
270 return llvm::createStringError("Type has no child named '%s'",
271 name.AsCString());
272 auto optional_idx = ExtractIndexFromString(name.AsCString());
273 if (!optional_idx) {
274 return llvm::createStringError("Type has no child named '%s'",
275 name.AsCString());
276 }
277 uint32_t idx = *optional_idx;
279 return llvm::createStringError("Type has no child named '%s'",
280 name.AsCString());
281 return idx;
282}
283
286 lldb::ValueObjectSP valobj_sp) {
287 if (!valobj_sp)
288 return nullptr;
289
290 valobj_sp = valobj_sp->GetNonSyntheticValue();
291 if (!valobj_sp)
292 return nullptr;
293
294 // We can't check the template parameter here, because PDB doesn't include
295 // this information.
296
297 // vector<T>
298 if (valobj_sp->GetChildMemberWithName("_Mypair") != nullptr)
299 return new MsvcStlVectorSyntheticFrontEnd(valobj_sp);
300 // vector<bool>
301 if (valobj_sp->GetChildMemberWithName("_Myvec") != nullptr)
302 return new MsvcStlVectorBoolSyntheticFrontEnd(valobj_sp);
303
304 return nullptr;
305}
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:408
Generic representation of a type in a programming language.
CompilerType GetPointeeType() const
If this type is a pointer type, return the type that the pointer points to, else return an invalid ty...
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 * AsCString(const char *value_if_empty=nullptr) const
Get the string value as a C string.
const char * GetCString() 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:294
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 CreateValueObjectFromData(llvm::StringRef name, const DataExtractor &data, const ExecutionContext &exe_ctx, CompilerType type)
SyntheticChildrenFrontEnd(ValueObject &backend)
lldb::ValueObjectSP CreateValueObjectFromAddress(llvm::StringRef name, uint64_t address, const ExecutionContext &exe_ctx, CompilerType type, bool do_deref=true)
llvm::Expected< size_t > GetIndexOfChildWithName(ConstString name) 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...
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
lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override
std::optional< size_t > ExtractIndexFromString(const char *item_name)
lldb_private::SyntheticChildrenFrontEnd * MsvcStlVectorSyntheticFrontEndCreator(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:332
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