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#include "llvm/Support/ErrorExtras.h"
14
15using namespace lldb;
16
17namespace lldb_private {
18namespace formatters {
19
21public:
23
24 llvm::Expected<uint32_t> CalculateNumChildren() override;
25
26 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
27
29
30 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
31
32private:
33 ValueObject *m_start = nullptr;
36 uint32_t m_element_size = 0;
37};
38
40public:
42
43 llvm::Expected<uint32_t> CalculateNumChildren() override;
44
45 lldb::ValueObjectSP GetChildAtIndex(uint32_t idx) override;
46
48
49 llvm::Expected<size_t> GetIndexOfChildWithName(ConstString name) override;
50
51private:
54 uint64_t m_count = 0;
55 uint64_t m_element_bit_size = 0;
57 std::map<size_t, lldb::ValueObjectSP> m_children;
58};
59
60} // namespace formatters
61} // namespace lldb_private
62
69
70llvm::Expected<uint32_t> lldb_private::formatters::
72 if (!m_start || !m_finish)
73 return llvm::createStringError(
74 "failed to determine start/end of vector data");
75
76 uint64_t start_val = m_start->GetValueAsUnsigned(0);
77 uint64_t finish_val = m_finish->GetValueAsUnsigned(0);
78
79 // A default-initialized empty vector.
80 if (start_val == 0 && finish_val == 0)
81 return 0;
82
83 if (start_val == 0)
84 return llvm::createStringError("invalid value for start of vector");
85
86 if (finish_val == 0)
87 return llvm::createStringError("invalid value for end of vector");
88
89 if (start_val > finish_val)
90 return llvm::createStringError(
91 "start of vector data begins after end pointer");
92
93 size_t num_children = (finish_val - start_val);
94 if (num_children % m_element_size)
95 return llvm::createStringError("size not multiple of element size");
96
97 return num_children / m_element_size;
98}
99
102 uint32_t idx) {
103 if (!m_start || !m_finish)
104 return lldb::ValueObjectSP();
105
106 uint64_t offset = idx * m_element_size;
107 offset = offset + m_start->GetValueAsUnsigned(0);
108 StreamString name;
109 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
110 return CreateValueObjectFromAddress(name.GetString(), offset,
111 m_backend.GetExecutionContextRef(),
113}
114
117 m_start = m_finish = nullptr;
118 ValueObjectSP data_sp(m_backend.GetChildAtNamePath({"_Mypair", "_Myval2"}));
119
120 if (!data_sp)
122
123 m_start = data_sp->GetChildMemberWithName("_Myfirst").get();
124 m_finish = data_sp->GetChildMemberWithName("_Mylast").get();
125 if (!m_start || !m_finish)
127
128 m_element_type = m_start->GetCompilerType().GetPointeeType();
129 llvm::Expected<uint64_t> size_or_err = m_element_type.GetByteSize(nullptr);
130 if (size_or_err)
131 m_element_size = *size_or_err;
132 else
133 LLDB_LOG_ERRORV(GetLog(LLDBLog::DataFormatters), size_or_err.takeError(),
134 "{0}");
135
137}
138
139llvm::Expected<size_t> lldb_private::formatters::
141 if (!m_start || !m_finish)
142 return llvm::createStringErrorV("type has no child named '{0}'", name);
143 auto optional_idx = ExtractIndexFromString(name.GetCString());
144 if (!optional_idx) {
145 return llvm::createStringErrorV("type has no child named '{0}'", name);
146 }
147 return *optional_idx;
148}
149
153 m_children() {
154 if (valobj_sp) {
155 Update();
157 valobj_sp->GetCompilerType().GetBasicTypeFromAST(lldb::eBasicTypeBool);
158 }
159}
160
165
168 uint32_t idx) {
169 auto iter = m_children.find(idx), end = m_children.end();
170 if (iter != end)
171 return iter->second;
172 if (idx >= m_count)
173 return {};
174 if (m_base_data_address == 0 || m_count == 0)
175 return {};
176 if (!m_bool_type)
177 return {};
178
179 // The vector<bool> is represented as a sequence of `int`s.
180 // The size of an `int` is in `m_element_bit_size` (most often 32b).
181 // To access the element at index `i`:
182 // (bool)((data_address[i / bit_size] >> (i % bit_size)) & 1)
183
184 // int *byte_location = &data_address[i / bit_size]
185 size_t byte_idx = (idx / m_element_bit_size) * (m_element_bit_size / 8);
186 lldb::addr_t byte_location = m_base_data_address + byte_idx;
187
188 ProcessSP process_sp(m_exe_ctx_ref.GetProcessSP());
189 if (!process_sp)
190 return {};
191 Status err;
192 Scalar scalar;
193 size_t bytes_read = process_sp->ReadScalarIntegerFromMemory(
194 byte_location, m_element_bit_size / 8, false, scalar, err);
195 if (err.Fail() || bytes_read == 0 || !scalar.IsValid())
196 return {};
197
198 size_t bit_index = idx % m_element_bit_size;
199 bool bit_set = scalar.GetAPSInt()[bit_index];
200 std::optional<uint64_t> size =
201 llvm::expectedToOptional(m_bool_type.GetByteSize(nullptr));
202 if (!size)
203 return {};
204 WritableDataBufferSP buffer_sp(new DataBufferHeap(*size, 0));
205 if (bit_set && buffer_sp && buffer_sp->GetBytes()) {
206 // regardless of endianness, anything non-zero is true
207 *(buffer_sp->GetBytes()) = 1;
208 }
209 StreamString name;
210 name.Printf("[%" PRIu64 "]", (uint64_t)idx);
212 name.GetString(),
213 DataExtractor(buffer_sp, process_sp->GetByteOrder(),
214 process_sp->GetAddressByteSize()),
216 if (retval_sp)
217 m_children[idx] = retval_sp;
218 return retval_sp;
219}
220
223 m_exe_ctx_ref.Clear();
224 m_count = 0;
227 m_children.clear();
228
229 ValueObjectSP valobj_sp = m_backend.GetSP();
230 if (!valobj_sp)
232 auto exe_ctx_ref = valobj_sp->GetExecutionContextRef();
233
234 ValueObjectSP size_sp = valobj_sp->GetChildMemberWithName("_Mysize");
235 if (!size_sp)
237 uint64_t count = size_sp->GetValueAsUnsigned(0);
238 if (count == 0)
240
241 ValueObjectSP begin_sp(valobj_sp->GetChildAtNamePath(
242 {"_Myvec", "_Mypair", "_Myval2", "_Myfirst"}));
243 if (!begin_sp)
245
246 // FIXME: the STL exposes _EEN_VBITS as a constant - it should be used instead
247 CompilerType begin_ty = begin_sp->GetCompilerType().GetPointeeType();
248 if (!begin_ty.IsValid())
250 llvm::Expected<uint64_t> element_bit_size = begin_ty.GetBitSize(nullptr);
251 if (!element_bit_size)
253
254 uint64_t base_data_address = begin_sp->GetValueAsUnsigned(0);
255 if (!base_data_address)
257
258 m_exe_ctx_ref = exe_ctx_ref;
259 m_count = count;
260 m_element_bit_size = *element_bit_size;
261 m_base_data_address = base_data_address;
263}
264
265llvm::Expected<size_t>
269 return llvm::createStringErrorV("type has no child named '{0}'", name);
270 auto optional_idx = ExtractIndexFromString(name.AsCString(nullptr));
271 if (!optional_idx) {
272 return llvm::createStringErrorV("type has no child named '{0}'", name);
273 }
274 uint32_t idx = *optional_idx;
276 return llvm::createStringErrorV("type has no child named '{0}'", name);
277 return idx;
278}
279
282 lldb::ValueObjectSP valobj_sp) {
283 if (!valobj_sp)
284 return nullptr;
285
286 valobj_sp = valobj_sp->GetNonSyntheticValue();
287 if (!valobj_sp)
288 return nullptr;
289
290 // We can't check the template parameter here, because PDB doesn't include
291 // this information.
292
293 // vector<T>
294 if (valobj_sp->GetChildMemberWithName("_Mypair") != nullptr)
295 return new MsvcStlVectorSyntheticFrontEnd(valobj_sp);
296 // vector<bool>
297 if (valobj_sp->GetChildMemberWithName("_Myvec") != nullptr)
298 return new MsvcStlVectorBoolSyntheticFrontEnd(valobj_sp);
299
300 return nullptr;
301}
#define LLDB_LOG_ERRORV(log, error,...)
Definition Log.h:410
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:132
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
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)
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:327
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